///////////////////////////
///// breakout
////////////////////////////
btf = [Link]("",title="breakout trend timeframe")
[open2,high2,low2,close2] = [Link]([Link], btf,
[open,high,low,close], barmerge.gaps_off, barmerge.lookahead_off)
// === BACKTEST RANGE ===
From_Year = input(defval=2019, title='From Year')
From_Month = [Link](defval=1, title='From Month', minval=1, maxval=12)
From_Day = [Link](defval=1, title='From Day', minval=1, maxval=31)
To_Year = input(defval=9999, title='To Year')
To_Month = [Link](defval=1, title='To Month', minval=1, maxval=12)
To_Day = [Link](defval=1, title='To Day', minval=1, maxval=31)
Start = timestamp(From_Year, From_Month, From_Day, 00, 00) // backtest start
window
Finish = timestamp(To_Year, To_Month, To_Day, 23, 59) // backtest finish window
// 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?', defval=false)
testPeriodBackgroundColor = testPeriodBackground and time >= Start and time <=
Finish ? #00FF00 : na
//bgcolor(testPeriodBackgroundColor, transp=95)
// == FILTERING ==
// Inputs
useMaFilter = input(title='Use MA for Filtering?', defval=false)
maType = [Link](defval='EMA', options=['EMA', 'SMA'], title='MA Type For
Filtering')
maLength = [Link](defval=200, title='MA Period for Filtering', minval=1)
// Declare function to be able to swap out EMA/SMA
ma(maType, src, length) =>
maType == 'EMA' ? [Link](src, length) : [Link](src, length) //Ternary Operator
(if maType equals EMA, then do ema calc, else do sma calc)
maFilter = ma(maType, close2, maLength)
//plot(maFilter, title = "Trend Filter MA", color = [Link], linewidth = 3,
style = plot.style_line, transp = 50)
// 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 ===
// Inputs
//pvtLenL = input(3, minval=1, title="Pivot Length Left Hand Side") //use if
you want to change this to an input
//pvtLenR = input(3, minval=1, title="Pivot Length Right Hand Side") //use if
you want to change this to an input
pvtLenL = 3
pvtLenR = 3
// Get High and Low Pivot Points
pvthi_ = [Link](high2, pvtLenL, pvtLenR)
pvtlo_ = [Link](low2, pvtLenL, pvtLenR)
// Force Pivot completion before plotting.
Shunt = 1 //Wait for close before printing pivot? 1 for true 0 for flase
maxLvlLen = 0 //Maximum Extension Length
pvthi = pvthi_[Shunt]
pvtlo = pvtlo_[Shunt]
// Count How many candles for current Pivot Level, If new reset.
counthi = [Link](not na(pvthi))
countlo = [Link](not na(pvtlo))
pvthis = fixnan(pvthi)
pvtlos = fixnan(pvtlo)
hipc = [Link](pvthis) != 0 ? na : [Link]
lopc = [Link](pvtlos) != 0 ? na : [Link]
// Display Pivot lines
//plot(maxLvlLen == 0 or counthi < maxLvlLen ? pvthis : na, color=hipc,
linewidth=1, offset=-pvtLenR - Shunt, title='Top Levels', transp=0)
//plot(maxLvlLen == 0 or countlo < maxLvlLen ? pvtlos : na, color=lopc,
linewidth=1, offset=-pvtLenR - Shunt, title='Bottom Levels', transp=0)
//plot(maxLvlLen == 0 or counthi < maxLvlLen ? pvthis : na, color=hipc,
linewidth=1, offset=0, title='Top Levels 2', transp=0)
//plot(maxLvlLen == 0 or countlo < maxLvlLen ? pvtlos : na, color=lopc,
linewidth=1, offset=0, title='Bottom Levels 2', transp=0)
// Stop Levels
stopBuff = [Link](0.0, minval=-2, title='Stop Loss Buffer off Swing Low (%)')
stopPerc = stopBuff * .01 // Turn stop buffer input into a percentage
stopLevel = [Link](pvtlo_, low2[pvtLenR], 0) //Stop Level at Swing Low
stopLevel2 = stopLevel - stopLevel * stopPerc // Stop Level with user-defined
buffer to avoid stop hunts and give breathing room
//plot(stopLevel2, style=plot.style_line, color=[Link]([Link], 50),
show_last=1, linewidth=1, trackprice=true)
buyLevel = [Link](pvthi_, high2[pvtLenR], 0) //Buy level at Swing High
buyLevel2 = buyLevel + buyLevel * stopPerc // Buy-stop level with user-defined
buffer to avoid stop hunts and give breathing room
//plot(buyLevel2, style=plot.style_line, color=[Link]([Link], 50),
show_last=1, linewidth=1, trackprice=true)
// Conditions for entry and exit
buySignal = high2 > buyLevel2
buy1 = buySignal and time > Start and time < Finish and buyLevel2 >
maFilterCheck // All these conditions need to be met to buy
sellSignal = low2 < stopLevel2 // Code to act like a stop-loss for the Study
// (STRATEGY ONLY) Comment out for Study
// [Link]("Long", [Link], stop = buyLevel2, when = time > Start and
time < Finish and buyLevel2 > maFilterCheck)
// [Link]("Exit Long", from_entry = "Long", stop=stopLevel2)
// == (STUDY ONLY) Comment out for Strategy ==
// Check if in position or not
inPosition = bool(na)
inPosition := buy1[1] ? true : sellSignal[1] ? false : inPosition[1]
flat = bool(na)
flat := not inPosition
buyStudy = buy1 and flat
sellStudy = sellSignal and inPosition
//Plot indicators on chart and set up alerts for Study
//plotshape(buyStudy, style=[Link], location=[Link],
color=[Link](#1E90FF, 0), text='', size=[Link])
//plotshape(sellStudy, style=[Link], location=[Link],
color=[Link](#EE82EE, 0), text='', size=[Link])
if buyStudy
[Link](x=bar_index, y=[Link](low, low[1]), text="B", color=[Link],
textcolor=[Link], style=label.style_label_up, size=[Link])
if sellStudy
[Link](x=bar_index, y=[Link](high, high[1]), text="S",
color=[Link], textcolor=[Link], style=label.style_label_down,
size=[Link])