Search code examples
pine-scriptpine-script-v5

Move trailing stop only upwards


I'd like my stop to move in 5% increments, for now just want to get the first increment working.

At the moment it moves when price goes up 5% as expected, but if price drops down again so does the stop. How can I make it only go upwards? Thanks

stopLoss = lastEntryPrice * 0.8
if (close / lastEntryPrice) > 1.05
    stopLoss := stopLoss * 1.05

Edit:

I tried this from last reply:

stopLoss = lastEntryPrice * 0.8
stopLossToBeConfirmed = lastEntryPrice * 0.8
if (close / lastEntryPrice) > 1.05
    if stopLossToBeConfirmed * 1.05 > stopLoss
        stopLoss := stopLossToBeConfirmed * 1.05

plot (stopLossToBeConfirmed,color=color.purple)
plot (stopLoss,color=color.red)

Blockquote

Whole code as requested

    // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © T-Meister

//@version=5
strategy("EMA STACK",
     overlay = true,
     initial_capital=500,
     default_qty_type=strategy.percent_of_equity,
     default_qty_value=100,
     commission_type=strategy.commission.cash_per_contract,
     commission_value=0.00) // how much commission per trade.
    

start = timestamp(2000,1,1,0,0)
end = timestamp (3000,1,1,0,0)

//////////////////////////////////////////////////////////
//RSI Entry section
//get the rsi line
emaOne = ta.ema(close,20)
emaTwo = ta.ema(close,50)
emaThree = ta.ema(close,100)
emaFour = ta.ema(close,200)
//plot (emaOne,color=color.red)
//plot (emaTwo,color=color.yellow)
//plot (emaThree,color=color.blue)
//plot (emaFour,color=color.green)
/////////////////////////////////////////////////////////////////////

// Get price of last entry in current position
lastEntryPrice = strategy.opentrades.entry_price(strategy.opentrades - 1)


////////////////////////////////////////////////////////////////////////////////////////////////////
//Trailing Stop Section

stopLoss = lastEntryPrice * 0.8
stopLossToBeConfirmed = lastEntryPrice * 0.8
if (close / lastEntryPrice) > 1.05
      
    if stopLossToBeConfirmed * 1.05 > stopLoss
        stopLoss := stopLossToBeConfirmed * 1.05

plot (stopLossToBeConfirmed,color=color.purple)
plot (stopLoss,color=color.red)

///////////////////////////////////////////////////////////////////////////////////////////////////////
// Take Profit Section
//Set percent for first take profit
FirstProfitPercent = 10
// Calculate Exit Price
exitPrice = (lastEntryPrice / 100) * (100+FirstProfitPercent)
//plot(exitPrice, color=color.yellow, style=plot.style_circles,linewidth=3, title="exitPrice")
//////////////////////////////////////////////////////////////////////////////////////////////////////////////


///////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Fixed Stop Section
fixedStop = 20
// Calculate stop Price
fixedStopPrice = (lastEntryPrice / 100) * (100-fixedStop)
//plot(fixedStopPrice, color=color.yellow, style=plot.style_circles,linewidth=3, title="exitPrice")
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

LongEntry = emaOne > emaTwo and emaTwo > emaThree and emaThree > emaFour and ta.rsi(close,14) < 50 

LongExit = close > exitPrice or close < fixedStopPrice 


if LongEntry
    strategy.entry(id = "Long", direction=strategy.long, comment = "Entry")

if LongExit
    strategy.close(id = "Long", comment = "Exit")

Solution

  • EDIT from the entire code : You should define your stoploss only when you take the trade and you should modify it only if it is higher than the preceding.

    Then, I supposed that each time your price gets 5% more, your stoploss will also be higher.

    Here is the code :

    // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
    // © T-Meister
    
    //@version=5
    strategy("EMA STACK",
         overlay = true,
         initial_capital=500,
         default_qty_type=strategy.percent_of_equity,
         default_qty_value=100,
         commission_type=strategy.commission.cash_per_contract,
         commission_value=0.00) // how much commission per trade.
        
    
    start = timestamp(2000,1,1,0,0)
    end = timestamp (3000,1,1,0,0)
    
    //////////////////////////////////////////////////////////
    //RSI Entry section
    //get the rsi line
    emaOne = ta.ema(close,20)
    emaTwo = ta.ema(close,50)
    emaThree = ta.ema(close,100)
    emaFour = ta.ema(close,200)
    //plot (emaOne,color=color.red)
    //plot (emaTwo,color=color.yellow)
    //plot (emaThree,color=color.blue)
    //plot (emaFour,color=color.green)
    /////////////////////////////////////////////////////////////////////
    
    // Get price of last entry in current position
    var PriceForSLCalcul = 0.0
    var stopLoss = 0.0
    
    ////////////////////////////////////////////////////////////////////////////////////////////////////
    //Trailing Stop Section
    if (close / PriceForSLCalcul) > 1.05
        if stopLoss * 1.05 > stopLoss
            stopLoss := stopLoss * 1.05
            PriceForSLCalcul := PriceForSLCalcul *1.05
    plot (stopLoss,color=color.red)
    
    ///////////////////////////////////////////////////////////////////////////////////////////////////////
    // Take Profit Section
    //Set percent for first take profit
    FirstProfitPercent = 10
    // Calculate Exit Price
    exitPrice = (PriceForSLCalcul / 100) * (100+FirstProfitPercent)
    //plot(exitPrice, color=color.yellow, style=plot.style_circles,linewidth=3, title="exitPrice")
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
    
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //Fixed Stop Section
    fixedStop = 20
    // Calculate stop Price
    fixedStopPrice = (PriceForSLCalcul / 100) * (100-fixedStop)
    //plot(fixedStopPrice, color=color.yellow, style=plot.style_circles,linewidth=3, title="exitPrice")
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
    LongEntry = emaOne > emaTwo and emaTwo > emaThree and emaThree > emaFour and ta.rsi(close,14) < 50 
    
    LongExit = close > exitPrice or close < fixedStopPrice 
    
    
    if LongEntry and strategy.opentrades==0
        strategy.entry(id = "Long", direction=strategy.long, comment = "Entry")
        PriceForSLCalcul := close
        stopLoss := PriceForSLCalcul * 0.8
    if LongExit
        strategy.close(id = "Long", comment = "Exit")
    

    You will see your stop loss increment each time your price gains 5% : enter image description here