Volume Breakout

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

///////////////////////////

///// breakout
////////////////////////////

btf = input.string("",title="breakout trend timeframe")


[open2,high2,low2,close2] = request.security(syminfo.tickerid, btf,
[open,high,low,close], barmerge.gaps_off, barmerge.lookahead_off)

// === BACKTEST RANGE ===


From_Year = input(defval=2019, title='From Year')
From_Month = input.int(defval=1, title='From Month', minval=1, maxval=12)
From_Day = input.int(defval=1, title='From Day', minval=1, maxval=31)
To_Year = input(defval=9999, title='To Year')
To_Month = input.int(defval=1, title='To Month', minval=1, maxval=12)
To_Day = input.int(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 = input.string(defval='EMA', options=['EMA', 'SMA'], title='MA Type For
Filtering')
maLength = input.int(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' ? ta.ema(src, length) : ta.sma(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 = color.green, 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_ = ta.pivothigh(high2, pvtLenL, pvtLenR)
pvtlo_ = ta.pivotlow(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 = ta.barssince(not na(pvthi))
countlo = ta.barssince(not na(pvtlo))

pvthis = fixnan(pvthi)
pvtlos = fixnan(pvtlo)
hipc = ta.change(pvthis) != 0 ? na : color.maroon
lopc = ta.change(pvtlos) != 0 ? na : color.green

// 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 = input.float(0.0, minval=-2, title='Stop Loss Buffer off Swing Low (%)')
stopPerc = stopBuff * .01 // Turn stop buffer input into a percentage
stopLevel = ta.valuewhen(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=color.new(color.orange, 50),
show_last=1, linewidth=1, trackprice=true)
buyLevel = ta.valuewhen(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=color.new(color.aqua, 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


// strategy.entry("Long", strategy.long, stop = buyLevel2, when = time > Start and
time < Finish and buyLevel2 > maFilterCheck)
// strategy.exit("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=shape.triangleup, location=location.belowbar,
color=color.new(#1E90FF, 0), text='', size=size.normal)
//plotshape(sellStudy, style=shape.triangledown, location=location.abovebar,
color=color.new(#EE82EE, 0), text='', size=size.normal)

if buyStudy
label.new(x=bar_index, y=math.min(low, low[1]), text="B", color=color.blue,
textcolor=color.white, style=label.style_label_up, size=size.small)

if sellStudy
label.new(x=bar_index, y=math.max(high, high[1]), text="S",
color=color.fuchsia, textcolor=color.white, style=label.style_label_down,
size=size.small)

You might also like