not in a trade condition breaks BUY/SELL labels

Viewed 245

The issue is pretty straightforward. The labels 'BUY' and 'SELL' are not always displayed, because of enterLong and enterShort and more specifically not inTrade in their conditions. It's because sometimes the exit becomes an entry.

enter image description here

The whole point is to simulate a strategy (the following), but in an indicator. LucF helped me about that back in the time in this question, so you can use the example there as a reference.

FLAT                        = strategy.position_size == 0
LONG                        = strategy.position_size > 0
SHORT                       = strategy.position_size < 0
entry                       = strategy.position_avg_price

Snippet

//@version=4
study("not in a trade - StackOverflow issue", overlay = true, precision = 6)

// —————————— Constants {
// ————— Input options
var string TD1 = "Both"
var string TD2 = "Longs Only"
var string TD3 = "Shorts Only"
// }

// —————————— Inputs {
// ————— Entries
var string GP1 = "Entries"
string  i_tradeDirection    = input(TD1, "Trade Direction", options = [TD1, TD2, TD3], group = GP1)

// ————— User-selected trade directions
var bool doLongs  = (i_tradeDirection == TD2 or i_tradeDirection == TD1)
var bool doShorts = (i_tradeDirection == TD3 or i_tradeDirection == TD1)
// }

// —————————— Calculations {
ema = ema(close, 200)
// }

// —————————— Strategy Calculations {
// ————— States
var float entryPrice = na
var bool inLong = false
var bool inShort = false
bool inTrade = inLong or inShort

// ————— Conditions
a = close > ema
enterLongCondition = not a[1] and a
enterShortCondition = a[1] and not a
exitLongCondition = enterShortCondition
exitShortCondition = enterLongCondition

// ————— Filter
filterLong = true
filterShort = true

// ————— Entries
enterLong  = doLongs  and not inTrade and enterLongCondition
enterShort = doShorts and not inTrade and enterShortCondition

// ————— Stops
atr = atr(14)
stopLong  = min(lowest(5), min(close, open) - atr * 1.5)
stopShort = max(highest(5), max(close, open) + atr * 1.5)

// ————— Exits
exitLong  = inLong  and (exitLongCondition  or close < stopLong[1])
exitShort = inShort and (exitShortCondition or close > stopShort[1])

// ————— States continuation
// plotchar(enterShort, color = color.yellow)
// plotchar(exitShort, color = color.yellow)

// plotchar(enterLong, color = color.green)
// plotchar(exitLong, color = color.green)

if enterLong
    inLong := true
    entryPrice := close
else if enterShort
    inShort := true
    entryPrice := close
else if exitLong
    inLong := false
    entryPrice := na
else if exitShort
    inShort := false
    entryPrice := na
// }

// —————————— Plots {
color colorBar = close > ema ? color.green : color.red
plot(ema, "EMA", color = colorBar, linewidth = 2, style = plot.style_circles)

// ————— Shapes
plotshape(enterLong, style = shape.labelup, location = location.belowbar, color = color.new(color.green, 20), text = "BUY", textcolor = color.white, size = size.normal)
plotshape(enterShort, style = shape.labeldown, location = location.abovebar, color = color.new(color.red, 20), text = "SELL", textcolor = color.white, size = size.normal)
// }
1 Answers

Nop,

The way this is coded now we have isolated trades, meaning we cant go short if we are in a long, and we cant go long if we are in a short. I will assume that is intended, and thus we need to display our exit criteria to show when a trade of a specific bias has ended. These variables exist within the code under the labels "exitLong" and "exitShort". This means the script is actually exiting trades without us seeing the points of exit. As such, I have added 2 more trade flags (one for each bias exit) and have titled them LE and SE for long exit and short exit. Please see the code below with the added conditions to display the actual state of your simulated emulator. Notice the added plotshapes at the end and their locations on the chart.

//@version=4
study("not in a trade - StackOverflow issue", overlay = true, precision = 6)

// —————————— Constants {
// ————— Input options
var string TD1 = "Both"
var string TD2 = "Longs Only"
var string TD3 = "Shorts Only"
// }

// —————————— Inputs {
// ————— Entries
var string GP1 = "Entries"
string  i_tradeDirection    = input(TD1, "Trade Direction", options = [TD1, TD2, TD3], group = GP1)
FLIP = input(false, "Allow Reversal Trades", input.bool)       


// ————— User-selected trade directions
var bool doLongs  = (i_tradeDirection == TD2 or i_tradeDirection == TD1)
var bool doShorts = (i_tradeDirection == TD3 or i_tradeDirection == TD1)
// }

// —————————— Calculations {
ema = ema(close, 200)
// }

// —————————— Strategy Calculations {
// ————— States
var float entryPrice = na
var bool inLong = false
var bool inShort = false
bool inTrade = inLong or inShort

// ————— Conditions
a = close > ema
enterLongCondition = not a[1] and a
enterShortCondition = a[1] and not a
exitLongCondition = enterShortCondition
exitShortCondition = enterLongCondition

// ————— Filter
filterLong = true
filterShort = true

// ————— Entries
enterLong  = doLongs  and (not inTrade or FLIP) and enterLongCondition
enterShort = doShorts and (not inTrade or FLIP) and enterShortCondition

// ————— Stops
atr = atr(14)
stopLong  = min(lowest(5), min(close, open) - atr * 1.5)
stopShort = max(highest(5), max(close, open) + atr * 1.5)

// ————— Exits
exitLong  = inLong  and (exitLongCondition  or close < stopLong[1]) and not (FLIP and enterShort)
exitShort = inShort and (exitShortCondition or close > stopShort[1]) and not (FLIP and enterLong)

// ————— States continuation
// plotchar(enterShort, color = color.yellow)
// plotchar(exitShort, color = color.yellow)

// plotchar(enterLong, color = color.green)
// plotchar(exitLong, color = color.green)

if enterLong
    inLong := true
    if FLIP
        inShort := false
    entryPrice := close
else if enterShort
    inShort := true
    if FLIP
        inLong := false
    entryPrice := close
else if exitLong
    inLong := false
    entryPrice := na
else if exitShort
    inShort := false
    entryPrice := na
// }

// —————————— Plots {
color colorBar = close > ema ? color.green : color.red
plot(ema, "EMA", color = colorBar, linewidth = 2, style = plot.style_circles)

// ————— Shapes
plotshape(enterLong,  style = shape.labelup,   location = location.belowbar, color = color.new(color.green, 20), text = "BUY",  textcolor = color.white, size = size.normal)
plotshape(enterShort, style = shape.labeldown, location = location.abovebar, color = color.new(color.red, 20),   text = "SELL", textcolor = color.white, size = size.normal)
plotshape(exitLong,   style = shape.labeldown, location = location.abovebar, color = color.new(color.green, 20), text = "LE",   textcolor = color.white, size = size.normal)
plotshape(exitShort,  style = shape.labelup,   location = location.belowbar, color = color.new(color.red, 20),   text = "SE",   textcolor = color.white, size = size.normal)
// }

Cheers my friend and take care!

** EDIT **

Added optional boolean in settings to toggle isolated trades on or off. It has been named "flip" within the code and if toggled true will allow reversal trades from the opposite bias.

Cheers again!

Related