Tradingview Pine Script strategy.exit() how to stop loss move to break even

Emre Mert picture Emre Mert · Jul 29, 2019 · Viewed 7.4k times · Source

Firstly, sorry my english. Im not native speaker.

I send a strategy skeleton written on pine script. As you seen in the strategy for example; when long entry signals come, L1 and L2 position open.

I want to change the strategy.exit part.

When L1 reach long_tp (target point), stop loss for L2 should come the entry price (break even) but stoploss in the script act like trailing stoploss changing up to price and ATR every step.

ATR changes every step so my initial stoploss level (entryprice-1.5xATR) always changes. I dont want to it changes. It should stay where its initial level (stop loss =initial price - 1.5x initial ATR).

In summary: when i get the long position L1 and L2, stoploss level should be (entryprice-1.5xinitialATR) and if L1 reaches the take profit level, stoploss for L2 move to entryprice and the entry price should stay fixed not change with ATR.

//@version=3
strategy(title="MA Crossover", overlay = true, pyramiding=0, initial_capital=10000, currency=currency.USD, calc_on_order_fills=1,default_qty_type=strategy.fixed, default_qty_value=10000)

price = close
fastlength = input(5,"fast length", minval=1, maxval=300)
slowlength = input(13,"slow length", minval=1, maxval=300)

sl_coefficent = input(1.5, "SL")
tp_coefficient = input(1, "TP")

///ATR alculation
atrlength = input(title="ATR Length", defval=14, minval=1)
atrsmoothing = input(title="ATR Smoothing", defval="SMA", options=["RMA", "SMA", "EMA", "WMA"])

ma_function(source, atrlength) => 
    if atrsmoothing == "RMA"
        rma(source, atrlength)
    else
        if atrsmoothing == "SMA"
            sma(source, atrlength)
        else
            if atrsmoothing == "EMA"
                ema(source, atrlength)
            else
                wma(source, atrlength)
atr = ma_function(tr(true), atrlength)

//Moving Averagers
fastMA = sma(close,fastlength)
slowMA = sma(close, slowlength)
plot(fastMA, title = "fast", color = blue, linewidth=2, transp=0)
plot(slowMA, title = "slow", color = red, linewidth=2, transp=0)

//Signals 
short_signal = slowMA > fastMA and price < slowMA
long_signal = slowMA < fastMA and price > fastMA


//Entry and Exit Conditions
enterLong = (long_signal) 
enterShort = (short_signal) 

exitLong = (short_signal) 
exitShort = (long_signal) 

//STRATEGY
if (year>2018)

    //Long entries with standard 1.5 ATR for SL, 1 ATR for TP
    long_sl = price - atr * sl_coefficent
    long_tp = price + atr * tp_coefficient
    strategy.entry("L1", strategy.long, when = enterLong)
    strategy.exit("L1 Limit Exit", "L1", stop = long_sl, limit = long_tp)
    strategy.close("L1", when = exitLong)

    //Long entries with no TP
    strategy.entry("L2", strategy.long, when = enterLong)
    strategy.exit("L2 Limit Exit", "L2", stop = long_sl)
    strategy.close("L2", when = exitLong)

    //Short entries with standard 1.5 ATR for SL, 1 ATR for TP
    short_sl = price + atr * sl_coefficent
    short_tp = price - atr * tp_coefficient
    strategy.entry("S1", strategy.short, when = enterShort)
    strategy.exit("S1 Limit Exit", "S1", stop = short_sl, limit = short_tp)
    strategy.close("S1", when = exitShort)

   //Short entries with no TP
   strategy.entry("S2", strategy.short, when = enterShort)
   strategy.exit("S2 Limit Exit", "S2", stop = short_sl)
   strategy.close("S2", when = exitShort)

Answer

Tessa picture Tessa · Jul 31, 2019

I'm not sure I understood you well if this is what you're looking for:

//Long entries with standard 1.5 ATR for SL, 1 ATR for TP
    long_sl = strategy.position_avg_price - atr * sl_coefficent
    long_tp = strategy.position_avg_price + atr * tp_coefficient