Search code examples
pine-scriptalgorithmic-tradingtradingpine-script-v4

PineScript - Strategy.exit not responding


I have some issues with the strategy.exit. It does not work. As shown in the picture below, both exit arguments should have worked!

enter image description here

RSI Script:

// RSI parameters
var RSI = "RSI"
overBought = input(title="OverBought RSI",group=RSI, defval=75)
overSold = input(title="OverSold RSI",group=RSI, defval=20)
rsi50 = input(50,title="RSI average",group=RSI)
rsi14exit = input(title="RSI 14 OverSold Exit Strategy", group=RSI, defval=70)

// RSI Short/Long
rsiLong = rsi(close,6) <= overSold
rsiShort = rsi(close,6) >= overBought
rsiBelowAverage = rsi(close,6) <= rsi50
rsi14_exit = rsi(close,14) >= rsi14exit

// Calculating
MACD Script:
fast_ma = sma_source == "SMA" ? sma(src, fast_length) : ema(src, fast_length)
slow_ma = sma_source == "SMA" ? sma(src, slow_length) : ema(src, slow_length)
macd = fast_ma - slow_ma
signal = sma_signal == "SMA" ? sma(macd, signal_length) : ema(macd, signal_length)
hist = macd - signal

// MACD Long/Short
Macd_long = macd >= signal
Macd_short = macd < signal

// Strategy close
strategy.exit("LongA Closed","LongA", loss=longStop, when= Macd_short or rsi14_exit)

Solution

  • You actually want to close a trade, so use strategy.close() instead.

    strategy.close(id="LongA", when= Macd_short or rsi14_exit)
    

    You can add your stop loss parameter to your strategy.entry call. It has a stop parameter.