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

13,154

Solution 1

Based on your comment valuewhen will help to keep the value of the ATR when you entered the trade :

ATR_Long := (strategy.position_size == 0 and strategy.long == 1 and strategy.position_entry_name == "Long" )? na : valuewhen(GoLong, one_atr, 0)
ATR_Short := (strategy.position_size == 0 and strategy.short == 1 and strategy.position_entry_name == "Short")? na : valuewhen(GoShort, one_atr, 0)

Solution 2

"if ATR value equals 1.2 when i enter the position, after 8 bar later it still should be 1.2 until my algorithim open new position. thanks again for reply"

I tried to explain you previously : You can store the ATR avlue when you entered the trade with valuewhen()

Eg :

ATR_Long := (strategy.position_size == 0 and strategy.long == 1 and strategy.position_entry_name == "Long" )? na : valuewhen(GoLong, one_atr, 0)

In this example, ATR_LONG is set when you entered a long position. ATR_LONG will not change until you reset it (so, at the next long entry). one_atr is the ATR current value.

Solution 3

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
Share:
13,154
Emre Mert
Author by

Emre Mert

Updated on June 13, 2022

Comments

  • Emre Mert
    Emre Mert about 2 years

    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)
    
  • Emre Mert
    Emre Mert almost 5 years
    Thanks for your answer. strategy.position_avg_price is useful to save initial entry price but ATR still changes every step. I want to save atr value. example; if ATR value equals 1.2 when i enter the position, after 8 bar later it still should be 1.2 until my algorithim open new position. thanks again for reply.