augerpro
Pine Script Rookie
Pine Script Rookie
Posts: 10
Joined: July 31st, 2022

Strategy help needed

I'm writing a simple strategy and having an issue. The basic idea of the strategy is: 1) identify a downtrend, 2) identify a reversal candle, 3) buy at close of reversal candle and enter profit and stoploss orders.

It appears my stoploss orders are executing at the correct price, but the profit order never does. It always seems to execute a few bars later regardless of price. Can anyone offer any help on this?
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © augerpro

//@version=5
strategy("Scalp1", overlay=true)

//////////////////INPUTS///////////////

downtrendlength = input.int(title="Minimum downtrend length", defval=4)
downtrendstrength = input.float(title="Downtrend %", defval=2, step=0.1)
//bargain = input.float(title="Bar Gain", defval=2, step=0.1, tooltip="Defines size of the reversal candle. Calculated using close-open")
percentbodyinput = input.float(title="Close strength %", defval=75, tooltip="Close must be above this % of candle size")
emalength = input.int(title="EMA length", defval=100)

////////////////VARIABLES/////////////

var bool step1 = na
var bool step2 = na
bool step3 = na
var float trackLow = na
varip barcount = 0
var float entry = na
var float exitprofit = na
var float exitloss = na

percentbody = percentbodyinput / 100
volconfirm = volume > volume[1]
reversalcandle = (close > open + (percentbody * (high-open))) and volconfirm
ema = ta.ema(close, emalength)

///////////////IDENTIFY DOWNTREND////

if close > ema and na(step1[1]) and close < high[downtrendlength] - (high[downtrendlength]*(downtrendstrength/100))
step1 := true
trackLow := low
barcount := 0

///////////////IDENTIFY SETUP BAR///

if step1 and low < trackLow
trackLow := low
barcount := 0

if step1 and low > trackLow
barcount := barcount + 1

if step1 and barcount > 3
step1 := na
step2 := na
trackLow := na
barcount := 0
entry := na

if step1 and reversalcandle
step2 := true
entry := close
exitprofit := close + (close - trackLow)
exitloss := close - (close - trackLow)

///////////////RESET AFTER TRADE///////////

if step2[1]
step1 := na
step2 := na
trackLow := na
barcount := 0
entry := na


//////////////ENTRY & EXIT//////////

if step2
strategy.entry(id="Long", direction=strategy.long)

if step2[1]
strategy.exit(id="Long Exit", from_entry="Long", profit=exitprofit, stop=exitloss, when=strategy.position_size > 0)



//////////////PLOTS/////////////////

plotchar(step1, color=color.gray, location=location.abovebar, title="Step 1")
plotchar(reversalcandle, color=color.green, location=location.belowbar, title="Reversal candle", display=display.none)
plotchar(step2, title="Step 2", color=color.green, char='⬆', location=location.belowbar, size=size.small)
plot(trackLow, title="Mark Low", color=color.red,display=display.none)
plot(ema, color=color.green, title="EMA")
plot(exitprofit, title="Profit Target", color=color.green)
plot(exitloss, title="Stop Loss", color=color.red)

Return to “Pine Script Q&A”