The following code I wish to implement a strategy that automatically enters a new position when long or short position previous has failed. It should enter a short when long has failed and the opposite. And, if the trade is successful it should enter another identical trade above the last.
My code is only creating long positions and it is only doing so as though the first condition is met and ignoring my other if conditions.
Please help.
// © theCrypster 2020
// @version=5
strategy('Fixed Percent Stop Loss & Take Profit %', overlay=true)
// User Options to Change Inputs (%)
stopPer = input(0.5, title='Stop Loss %') / 100
takePer = input(0.5, title='Take Profit %') / 100
// Determine where you've entered and in what direction
longStop = strategy.position_avg_price * (1 - stopPer)
shortStop = strategy.position_avg_price * (1 + stopPer)
shortTake = strategy.position_avg_price * (1 - takePer)
longTake = strategy.position_avg_price * (1 + takePer)
if strategy.position_size == 0 and strategy.position_size[1] == 0
strategy.entry('LONG', strategy.long)
if strategy.position_size > 0
strategy.exit(id='Close Long', stop=longStop, limit=longTake)
if high >= longTake
strategy.entry('LONG', strategy.long)
if low <= longStop
strategy.entry('SHORT', strategy.short)
if strategy.position_size < 0
strategy.exit(id='Close Short', stop=shortStop, limit=shortTake)
if high >= shortStop
strategy.entry('LONG', strategy.long)
if low <= shortTake
strategy.entry('SHORT', strategy.short)
// PLOT FIXED SLTP LINE
plot(strategy.position_size > 0 ? longStop : na, style=plot.style_linebr, color=color.new(color.red, 0), linewidth=1, title='Long Fixed SL')
plot(strategy.position_size < 0 ? shortStop : na, style=plot.style_linebr, color=#7252ff, linewidth=1, title='Short Fixed SL')
plot(strategy.position_size > 0 ? longTake : na, style=plot.style_linebr, color=color.new(color.green, 0), linewidth=1, title='Long Take Profit')
plot(strategy.position_size < 0 ? shortTake : na, style=plot.style_linebr, color=color.rgb(231, 171, 40), linewidth=1, title='Short Take Profit')