Search code examples
pine-scriptpine-script-v5

Why am I getting an "Undeclared identifier" error when it appears that I did identify it?



Inside_Bar = high[1] > high[0] and low[1] < low[0]
Prev_Range = high[1] - low[1]
Bullish = open[1] < close[1]
Bearish = open[1] > close[1]

// Get Key Levels 
Long_Stop_Buy_Level = high[1] + Prev_Range * Stop_Buy_Perc
Short_Stop_Buy_Level = low[1] - Prev_Range * Stop_Buy_Perc
Long_Stop_Loss_Level = high[1] - Prev_Range * Stop_Loss_Perc
Short_Stop_Loss_Level = low[1] + Prev_Range * Stop_Loss_Perc
Long_Take_Prof_Level = high[1] + Prev_Range * Take_Prof_Perc
Short_Take_Prof_Level = low[1] - Prev_Range * Take_Prof_Perc

// Position Sizing
long_qty = math.floor(strategy.equity * Risk)
short_qty = math.floor(strategy.equity * Risk)

// -------------------------- LONG CONDITIONS --------------------------------//
Long_Condition = Window and Inside_Bar and Bullish
if Long_Condition
    // Incase we still have a buy stop order in the market
    strategy.cancel_all()
    // Close any existing positions according to the rules
    strategy.close_all()
    strategy.entry('Bullish IB', strategy.long, stop=Long_Stop_Buy_Level, qty=long_qty)
    strategy.exit('Bullish Exit', 'Bullish IB', stop=Long_Stop_Loss_Level, limit=Long_Take_Prof_Level)

//@version=5
indicator("Selective `alert()` calls")
detectLongsInput  = input.bool(true,  "Detect Longs")
detectShortsInput = input.bool(true,  "Detect Shorts")
repaintInput      = input.bool(false, "Allow Repainting")

// Only generate entries when the trade's direction is allowed in inputs.
enterLong  = detectLongsInput  and Long_Condition and (repaintInput or barstate.isconfirmed)
exitLong   = (Long_Stop_Loss_Level or Long_Take_Prof_Level)
enterShort = detectShortsInput and Short_Condition and (repaintInput or barstate.isconfirmed)
exitShort  = (Short_Stop_Loss_Level or Short_Take_Prof_Level)

// Trigger the alerts only when the compound condition is met.
if enterLong 
    alert("Go long")
else if enterShort
    alert("Go short")
if exitLong
    alert("Exit long")
else if exitShort
    alert("Exit ya shorts!")

// -------------------------- SHORT CONDITIONS -------------------------------//
Short_Condition = Window and Inside_Bar and Bearish
if Short_Condition
    // Incase we still have a buy stop order in the market
    strategy.cancel_all()
    // Close any existing positions according to the rules
    strategy.close_all()
    strategy.entry('Bearish IB', strategy.short, stop=Short_Stop_Buy_Level, qty=short_qty)
    strategy.exit('Bearish Exit', 'Bearish IB', stop=Short_Stop_Loss_Level, limit=Short_Take_Prof_Level)

// ----------------------------- PLOTTING ------------------------------------//
plotshape(Inside_Bar, style=shape.arrowdown, location=location.abovebar, color=color.new(color.purple, 0))

The error

Error at 58:36 Undeclared identifier 'Short_Condition'

I copied the alert script from the V5 manual and modified it a bit to suit my strategy. I'm trying to get the alerts to go off at the strategy's entry and exit points but I'm running into (Undeclared identifier 'Short_Condition').

What's really strange is that the "Short_Condition" error I get with the pasted in alert script is not showing up when I run the same strategy script without the alert script. Any advice is greatly appreciated.


Solution

  • Your Short_Condition was in use before you defined it used at line 38, defined at line 52.

    All variables should be defined before using