'Pine Script issue with repeated entries after strategy.exit
I've created a trading strategy in PineScript. I'm using strategy.entry to open longs and shorts (only one position at a time). And I am closing the current position with strategy.exit if it hits my stop loss and I am closing the position with strategy.close if conditions are right for the next opposite order (i.e. if a long position reaches a point where a short is now called for).
My problem is with the strategy.exit stop loss orders. Whenever one of these is used to close the trade, the strategy tries the same trade again shortly after, until it hits the opposite type of order. EXAMPLE: It opens a short and it is stopped out. Then it tries that order again maybe a few bars later and then maybe that one is stopped out as well. It will continue this until it hits a long order, etc.
I am trying to solve this so that if a long is stopped out that it will NOT try any more longs until a short is executed in the strategy (and vice versa).
I have attempted this using two vars (var bool stoppedOutLong and var bool stoppedOutShort) to track whether a long or short was stopped out and have applied this logic to the conditions for opening trades. But for some reason this is being completely ignored by the strategy and these repeated orders keep being taken.
I'm very stumped as to why.
Here is the full strategy code:
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © CryptoEatsTheWorld
//@version=4
strategy("15-Minute MACD Support/Resistance", overlay=true)
var bool stoppedOutLong = na
var bool stoppedOutShort = na
ema = ema(close, 65)
plot(ema, "EMA 65", #00CCFF, linewidth=2)
//functions
xrf(values, length) =>
r_val = float(na)
if length >= 1
for i = 0 to length by 1
if na(r_val) or not na(values[i])
r_val := values[i]
r_val
[diff, dea, macdhisto] = macd(close, 8, 17, 9)
a1 = barssince(crossover(diff,dea)[1])
oversold = (xrf(close,a1+1)>close) and (diff>xrf(diff,a1+1)) and crossover(diff,dea)
a2 = barssince(crossunder(dea,diff)[1])
overbought = (xrf(close,a2+1)<close) and (xrf(diff,a2+1)>diff) and crossunder(dea,diff)
plotshape(oversold, style=shape.triangleup, color=color.green, size=size.small, location=location.belowbar)
plotshape(overbought, style=shape.triangledown, color=color.red, size=size.small, location=location.abovebar)
barColour = (overbought or oversold) ? #FFFFFF : na
barcolor(color=barColour)
OBcheck = 0
OScheck = 0
if (overbought)
OBcheck := 1
else
OBcheck := 0
if (oversold)
OScheck := 1
else
OScheck := 0
OStimelapse = barssince(OScheck > 0)
OBtimelapse = barssince(OBcheck > 0)
whichOver = OStimelapse > OBtimelapse
//if whichOver = TRUE then OVERBOUGHT, if FALSE then OVERSOLD
longcondition = not whichOver and close > ema and not stoppedOutLong
shortcondition = whichOver and close < ema and not stoppedOutShort
// Make inputs that set the take profit %
GainPerc = 0.00668
LossPerc = 0.00668
// Figure out take profit price
LongGainPrice = strategy.position_avg_price * (1 + GainPerc)
LongLossPrice = strategy.position_avg_price * (1 - LossPerc)
ShortGainPrice = strategy.position_avg_price * (1 - GainPerc)
ShortLossPrice = strategy.position_avg_price * (1 + LossPerc)
starttime = timestamp(2021,1,1,0,0)
strategy.entry("Long", strategy.long, 5, when = longcondition and time >= starttime, alert_message = "Long" )
strategy.entry("Short", strategy.short, 5, when = shortcondition and time >= starttime, alert_message = "Short")
if strategy.position_size > 0 and close <= LongLossPrice
strategy.exit("Close Long", stop=LongLossPrice, alert_message = "Long Stopped Out")
stoppedOutLong := true
stoppedOutShort := false
else
strategy.close("Close Long", when = longcondition)
stoppedOutLong := false
stoppedOutShort := false
if strategy.position_size < 0 and close >= ShortLossPrice
strategy.exit("Close Short", stop=ShortLossPrice, alert_message = "Short Stopped Out")
stoppedOutLong := false
stoppedOutShort := true
else
strategy.close("CLose Short", when = shortcondition)
stoppedOutLong := false
stoppedOutShort := false
Solution 1:[1]
I sorted it out! It turns out I did not realize that the "else" on the two strategy if/else statements were being evaluated on each bar. I needed to modify them to be else if statements like so:
else if strategy.position_size > 0 and longcondition else if strategy.position_size < 0 and shortcondition
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | CryptoEatsTheWorld |