2

Im new to pine script but have been working on an indicator/strategy that combines multiple indicators to generate a more complex trading strategy. For the most part I have worked through and succeeded with most of that stuff but looking for ideas and feedback on a few things.

To note: code is a simplified version using 2 indicators (easier and cleaner to share here) and for each individual indicator ive added an input to turn its buy/sell plot on/off via a checkbox (handy for strategy entry/exit testing, etc)

What id like to do is add an input box to include/exclude an indicator from the final buy/sell/exit sections of the strategy so I can backtest and dial things in per trading pair. Ive partially achieved this already but its not working properly with last bar memory and would love some feedback on how to achieve this properly. Thanks

Original (minus final inputs)

strategy("Test Strategy", overlay=true)

//SUPERTREND

atrPeriod = input(8, "ATR Length", group="SuperTrend")
factor = input.float(1.6, "Factor", group="SuperTrend", step = 0.01)
showSTMem = input(title='Show Signals', defval=true, group="SuperTrend")

[supertrend, direction] = ta.supertrend(factor, atrPeriod)

bodyMiddle = plot((open + close) / 2, display=display.none)
upTrend = plot(direction < 0 ? supertrend : na, "Up Trend", color = color.green, style=plot.style_linebr)
downTrend = plot(direction < 0 ? na : supertrend, "Down Trend", color = color.red, style=plot.style_linebr)

buy1 = (direction < 0)
sell1 = (direction > 0)


plotshape(buy1 and not(buy1[1]) and(showSTMem ? true : na), title='ST Buy', text='ST', textcolor=color.new(color.white, 0), style=shape.labelup, size=size.normal, location=location.belowbar, color=color.new(color.green, 0))
plotshape(sell1 and not(sell1[1]) and(showSTMem ? true : na), title='ST Sell', text='ST', textcolor=color.new(color.white, 0), style=shape.labeldown, size=size.normal, location=location.abovebar, color=color.new(color.red, 0))

//Wavetrend with Crosses


n1 = input(10, "Channel Length", group="WaveTrend")
n2 = input(21, "Average Length", group="WaveTrend")
obLevel1 = input(60, "Over Bought Level 1", group="WaveTrend")
obLevel2 = input(53, "Over Bought Level 2", group="WaveTrend")
osLevel1 = input(-60, "Over Sold Level 1", group="WaveTrend")
osLevel2 = input(-53, "Over Sold Level 2", group="WaveTrend")
showSTMem1 = input(title='Show Signals', defval=true, group="WaveTrend")

ap = hlc3 
esa = ta.ema(ap, n1)
d = ta.ema(math.abs(ap - esa), n1)
ci = (ap - esa) / (0.015 * d)
tci = ta.ema(ci, n2)

wt1 = tci
wt2 = ta.sma(wt1,4)

buy2 = wt1 > wt2
sell2 = wt1 < wt2

//Plot Signals

plotshape(buy2 and not(buy2[1]) and(showSTMem1 ? true : na), title='Buy Signal', text='WT', location=location.belowbar, style=shape.labelup, size=size.tiny, color=color.new(color.green, 0), textcolor=color.new(color.white, 0))
plotshape(buy2 and not(buy2[1]) and(showSTMem1 ? true : na), title='Sell Signal', text='WT', location=location.abovebar, style=shape.labeldown, size=size.tiny, color=color.new(color.red, 0), textcolor=color.new(color.white, 0))


//Trade Signals

buy = buy1 and buy2
sell = sell1 and sell2

//Signal Plot Memory

buyPosMem = false
sellPosMem = false

buyPosMem := buy ? true : sell ? false : buyPosMem[1]
sellPosMem := sell ? true : buy ? false : sellPosMem[1]

//Plots

plotshape(buy and not(buyPosMem[1]),location = location.belowbar,style=shape.triangleup,color=color.green,size=size.small)
plotshape(sell and not(sellPosMem[1]),location = location.abovebar,style=shape.triangledown,color=color.red,size=size.small)

//Strategy

if (buy)
    strategy.entry("Long", strategy.long)


if (sell)
    strategy.entry("Short", strategy.short)

Alternate Trade Signals Code with Inputs

//Trade Signals


//LONG Inputs

longIn1 = input(title='SuperTrend', defval=false, group="Long Signals")
if longIn1 == true
    buy1
    
longIn2 = input(title='WaveTrend', defval=false, group="Long Signals")
if longIn2 == true
    buy2

//SHORT Inputs

shortIn1 = input(title='SuperTrend', defval=false, group='Short Signals')
if shortIn1 == true
    sell1
    
shortIn2 = input(title='WaveTrend', defval=false, group='Short Signals')
if shortIn2 == true
    sell2

buy = longIn1 and longIn2
sell = shortIn1 and shortIn2

//Signal Plot Memory

buyPosMem = false
sellPosMem = false

buyPosMem := buy ? true : sell ? false : buyPosMem[1]
sellPosMem := sell ? true : buy ? false : sellPosMem[1]

plotshape(buy and not(buyPosMem[1]),location = location.belowbar,style=shape.triangleup,color=color.green,size=size.small)
plotshape(sell and not(sellPosMem[1]),location = location.abovebar,style=shape.triangledown,color=color.red,size=size.small)



if (buy)
    strategy.entry("Long", strategy.long)


if (sell)
    strategy.entry("Short", strategy.short)
Johnny
  • 21
  • 2

0 Answers0