pine-scriptentry-point

how to start a long position by adding a percentage lower condition with Pine Script


I'm trying to figure out how to start a long entry with a percentage lower. For instance, when ema 20 crossover ema 50, the long entry will trigger only 1% lower than the pricelevel once the crossover happens.


Solution

  • you can use the limit paramater on the strategy.entry() function. This will set the max price you are willing to enter a long trade. In your case, we will set the max price to be 99% of the close price when the crossover happened.

    //@version=5
    strategy("My strategy", overlay=true)
    
    ema20 = ta.ema(close, 20)
    ema50 = ta.ema(close, 50)
    
    longCondition = ta.crossover(ema20, ema50)
    shortCondition = ta.crossunder(ema20, ema50)
    
    if (longCondition)
        strategy.entry("Long", strategy.long, limit=close * 0.99)
    
    if (shortCondition)
        strategy.close_all()
    
    plot(ema20, color=color.green)
    plot(ema50, color=color.fuchsia)
    

    enter image description here