1
  1. My exit signals are not working. Entries are made if a long signal is above the 200 ema and the other way around for shorts. BUT exits are still given if a long signal is below the 200 ema and the other way around for shorts. I keep getting the error
    Compilation error. Line 34: Mismatched input 'longExitForShort' expecting 'end of line without line continuation'.
    The next few lines probably have the same error as well.

  2. My Take Profit and Stop Loss signals don't have effect, they don't show up on the chart even if the value is changed. There is no error while compiling, it just doesn't work.

  3. Here is the code:

    //@version=4
    strategy("sma")
    
    //dropdown menu to change the values of the EMA periods, stop loss, and take profit levels:
    emaShortPeriod = input(100, minval=1)
    emaLongPeriod = input(150, minval=1)
    stopLoss = input(100, minval=0)
    takeProfit = input(5, minval=0)
    
    //use the input values to define the EMA series:
    emaShort = sma(close, emaShortPeriod)
    emaLong = sma(close, emaLongPeriod)
    ema200 = sma(close, 200)
    
    //plot the EMA series on the chart:
    plot(emaShort, title="EMA (short period)", color=color.green)
    plot(emaLong, title="EMA (long period)", color=color.orange)
    plot(ema200, title="EMA (200 period)", color=color.blue)
    
    //the input values to define the long and short entry signals:
    longEntry = crossover(emaShort, emaLong) and close > ema200
    shortEntry = crossover(emaLong, emaShort) and close < ema200
    
    //plot the long and short entry signals on the chart:
    plotchar(longEntry, "Long Entry", "▲", location.top, color = #00FF00, transp = 0)
    plotchar(shortEntry, "Short Entry", "▼", location.top, color = #FF0000, transp = 0)
    
    //function to generate market orders when the entry signals are triggered:
    strategy.entry("Long", strategy.long, when=longEntry)
    strategy.entry("Short", strategy.short, when=shortEntry)
    
    //exit signals
    if (useLongAsExitForShort)
    longExitForShort = crossover(emaLong, ema200)
    else
    longExitForShort = crossover(emaShort, ema200)
    
    if (useShortAsExitForLong)
    shortExitForLong = crossover(emaShort, ema200)
    else
    shortExitForLong = crossover(emaLong, ema200)
    
    //plot the exit signals
    plotchar(longExitForShort, "Long Exit for Short", "▼", location.bottom, color = #FF0000, transp = 0)
    plotchar(shortExitForLong, "Short Exit for Long", "▲", location.bottom, color = #00FF00, transp = 0)
    
    //function to generate market orders when the exit signals are triggered
    strategy.exit("Long Exit", "Short", when=longExitForShort)
    strategy.exit("Short Exit", "Long", when=shortExitForLong)
    
    strategy.exit("Long Exit", "Long", stop=strategy.position_avg_price * (1 - stopLoss), limit=strategy.position_avg_price * (1 + takeProfit))
    strategy.exit("Short Exit", "Short", stop=strategy.position_avg_price * (1 + stopLoss), limit=strategy.position_avg_price * (1 - takeProfit))
    

I am expecting my exits signals to work as implied also for the TP and SL to show/work in general

Gittiwilo
  • 11
  • 2

1 Answers1

1

Two things:

  1. A local block must be indented by a tab or four spaces

So, you should fix your if blocks like below.

if (useLongAsExitForShort)
    longExitForShort = crossover(emaLong, ema200)
else
    longExitForShort = crossover(emaShort, ema200)

if (useShortAsExitForLong)
    shortExitForLong = crossover(emaShort, ema200)
else
    shortExitForLong = crossover(emaLong, ema200)
  1. You should use strategy.exit() for your SL and TP. If you want to close your position based on a condition, you should use strategy.close().

Instead of:

strategy.exit("Long Exit", "Short", when=longExitForShort)
strategy.exit("Short Exit", "Long", when=shortExitForLong)

You should do:

if (longExitForShort)
    strategy.close("Long")

if (shortExitForLong)
    strategy.close("Short")
vitruvius
  • 15,740
  • 3
  • 16
  • 26
  • Based on past bad experience with Pine executing local `if` blocks, I use ternary operator whenever it is possible and where it remains readable. In this case: `longExitForShort = useLongAsExitForShort ? crossover(emaLong, ema200) : crossover(emaShort, ema200)`. I think the devs have now fixed issues with IF blocks and Pine will not throw warnings about it, but habits stuck. – karatedog Dec 15 '22 at 14:37