pine-scripttradingview-apicrossover

PineScript Simple EMA crossover script, long condition met but not initiating long positions


// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © AtlasTrading712

//@version=5
strategy("Trendfollowing EMA", overlay=true, margin_long=100, margin_short=100)

symbol_price = request.security("SPY","1",close)            // Good symbol request, every minute candle close

plot(symbol_price + ta.atr(14), "AtrHigh",color.white)    // Good plots ATR
plot(symbol_price - ta.atr(14), "AtrLow",color.white)     // Good plots ATR

plot(ta.ema(symbol_price, 14),"1", color.blue)            // Good plots EMA
plot(ta.ema(symbol_price, 20),"2", color.green)           // Good plots EMA
plot(ta.ema(symbol_price, 25),"3", color.yellow)          // Good plots EMA
plot(ta.ema(symbol_price, 30),"4", color.orange)          // Good plots EMA
plot(ta.ema(symbol_price, 40),"5", color.red)             // Good plots EMA
plot(ta.ema(symbol_price, 50),"6", color.purple)          // Good plots

RSI = ta.rsi(open,14)                                       // Simple RSI pull

shortEMA = ta.ema(symbol_price, 14)                         // Assigned shortEMA to variable
longEMA = ta.ema(symbol_price, 21)                          // Assigned longEMA to variable

longCondition = ta.crossover(shortEMA, longEMA)             //long entry Condition

if (longCondition)                                         //if long entry Condition True,
    strategy.entry("long",strategy.long,1)                 // do this

Problem is with the last few lines. I've used the if (longCondition) function on a different strategy and everything works fine. I don't see any technical errors in my code, and even went as far as annotating each line to ensure I'm not missing anything.

I expected to see some simulated entries when the short term EMA of SPY crosses over the long term EMA of SPY. The script runs fine, the plots are all nicely shown on the chart, yet no data points for any trades opened (and none showed open). It must be something simple I imagine.


Solution

  • In your code there are only conditions for long
    Missing conditions for short

    shortCondition = ta.crossunder(shortEMA, longEMA)           //short entry Condition
    
    if (shortCondition)                                         //if short entry Condition True,
        strategy.entry("short",strategy.short,1)                // do this