2 Indicator
2 Indicator
// Revision: 2
// Author: @millerrh
// Strategy: Enter long when recent swing high breaks out, using recent swing low
as stop level. Move stops up as higher lows print to act
// as trailing stops. Ride trend as long as it is there and the higher lows aren't
breached.
// Conditions/Variables
// 1. Can place a user-defined percentage below swing low and swing high to use
as a buffer for your stop to help avoid stop hunts
// 2. Can add a filter to only take setups that are above a user-defined moving
average (helps avoid trading counter trend)
// 3. Manually configure which dates to back test
// 4. Color background of backtested dates - allows for easier measuring buy &
hold return of time periods that don't go up to current date
// == FILTERING ==
// Inputs
useMaFilter = input(title = "Use MA for Filtering?", type = input.bool, defval =
true,
tooltip = "Signals will be ignored when price is under this moving average. The
intent is to keep you out of bear periods and only buying when price is showing
strength.")
maType = input(defval="SMA", options=["EMA", "SMA"], title = "MA Type For
Filtering")
maLength = input(defval = 50, title = "MA Period for Filtering", minval = 1)
// Check to see if the useMaFilter check box is checked, this then inputs this
conditional "maFilterCheck" variable into the strategy entry
maFilterCheck = if useMaFilter == true
maFilter
else
0
// === PLOT SWING HIGH/LOW AND MOST RECENT LOW TO USE AS STOP LOSS EXIT POINT ===
// Change these values to adjust the look back and look forward periods for your
swing high/low calculations
pvtLenL = 3
pvtLenR = 3
// Count How many candles for current Pivot Level, If new reset.
counthi = barssince(not na(pvthi))
countlo = barssince(not na(pvtlo))
pvthis = fixnan(pvthi)
pvtlos = fixnan(pvtlo)
hipc = change(pvthis) != 0 ? na : color.maroon
lopc = change(pvtlos) != 0 ? na : color.green
// Stop Levels
stopLevel = valuewhen(pvtlo_, low[pvtLenR], 0) //Stop Level at Swing Low
plot(stopLevel, style=plot.style_line, color=color.orange, show_last=1,
linewidth=1, transp=50, trackprice=true)
buyLevel = valuewhen(pvthi_, high[pvtLenR], 0) //Buy level at Swing High
plot(buyLevel, style=plot.style_line, color=color.blue, show_last=1, linewidth=1,
transp=50, trackprice=true)
// Color background when trade active (for easier visual on what charts are OK to
enter on)
tradeBackground = input(title="Color Background for Trades?", type=input.bool,
defval=true)
tradeBackgroundColor = tradeBackground and inPosition ? #00FF00 : na
bgcolor(tradeBackgroundColor, transp=95)
noTradeBackgroundColor = tradeBackground and flat ? #FF0000 : na
bgcolor(noTradeBackgroundColor, transp=90)
// A switch to control background coloring of the test period - Use for easy
visualization of backtest range and manual calculation of
// buy and hold (via measurement) if doing prior periods since value in Strategy
Tester extends to current date by default
testPeriodBackground = input(title="Color Background - Test Period?",
type=input.bool, defval=false)
testPeriodBackgroundColor = testPeriodBackground and (time >= Start) and (time <=
Finish) ? #00FF00 : na
bgcolor(testPeriodBackgroundColor, transp=95)