Search code examples
pine-scripttradingview-api

I try to use ATR band and "valuewhen" function to set stoploss for my strategy but it is not working properly


First I will explain what I want the code to do: 1.I draw a ATR band(with upline and bottomline) as per my own definition. 2.Suppose when one bar is complete and it triggers the longcondition(2 EMA crossover). Let's call this bar as "bar 1". 3.Then the code will open a position at the open price of next bar(let's call it "bar 2"). 4.At bar 2, I want to save the value of the upline and bottomline as a static number which don't keep updaing as the bars going on. 5.In strategy.exit I use above mentioned statisc numbers as the stoploss and takeprofit. My codes are as below:

//@version=5
strategy("strategy learning", overlay=true, initial_capital=10000000)

//Draw 2 EMA lines.
ema1 = ta.ema(close, 10)
ema2 = ta.ema(close, 90)
plot(ema1, color=color.yellow)
plot(ema2, color=color.blue)

//Define a ATR band upline and bottome line.
atr = ta.atr(14)
upline = open + atr[1]
bottomline = open - atr[1]

plot(upline, color=color.white)
plot(bottomline, color=color.white)

//use "valuewhen()" function to grab the upline value and bottomline value at the moment of longcondition. It will be used for stoploss and takeprofit.
longcondition = ta.crossover(ema1, ema2)
longlimit = ta.valuewhen(longcondition, upline, 1)
longstop = ta.valuewhen(longcondition, bottomline, 1)

if(longcondition) 
    strategy.entry("buy", strategy.long, qty=1)
strategy.exit("buy", limit=longlimit, stop=longstop)


//use "valuewhen()" function to grab the upline value and bottomline value at the moment of shortcondition. It will be used for stoploss and takeprofit. 
shortcondition = ta.crossunder(ema1, ema2)
shortlimit = ta.valuewhen(shortcondition, bottomline, 1)
shortstop = ta.valuewhen(shortcondition, upline, 1)

if(shortcondition)
    strategy.entry("sell", strategy.short, qty=1)
strategy.exit("sell", limit=shortlimit, stop=shortstop)

Question1: Please help check my codes are correct or not? Does it match with my demand? Question2: I check the backtesting orders list carfully and found that many orders are not closed at the expected price as I described in the begaining of this post(sorry my reputation is not enough to add a image here). I asked similar question and others said maybe it is due to slippage which I still not able to verify it.


Solution

  • I can help you with your first question, I think. When I load your code your take-profit and stop-loss orders aren't firing at the ATR bands. If you change the last argument of your ta.valuewhen() functions from 1 to 0, that fixes the issue. If they're at 1 then they fetch the value for the occurence before last of your condition - in this case, not the last ema cross, but the one before that. Zero fetches the value for the last ema cross.

    I also added some plots for your take profit and stop loss lines so I could check what was going on, added strategy.position_size == 0 to longcondition and shortcondition (so you only have one order open at a time), and indented your exit orders so they're placed at the same time as your entry orders.

    So instead of:

     if(shortcondition) and strategy.position_size == 0
        strategy.entry("sell", strategy.short, qty=1)
             strategy.exit("exit", from_entry="sell", limit=shortlimit, stop=shortstop)
    

    you have this:

    if(shortcondition) and strategy.position_size == 0
       strategy.entry("sell", strategy.short, qty=1)
       strategy.exit("exit", from_entry="sell", limit=shortlimit, stop=shortstop)
    

    All the lines you want to treat as consequences of the same if statement should be indented beneath it.

    //@version=5
    strategy("strategy learning", overlay=true, initial_capital=10000000)
    
    //Draw 2 EMA lines.
    ema1 = ta.ema(close, 10)
    ema2 = ta.ema(close, 90)
    plot(ema1, color=color.yellow)
    plot(ema2, color=color.blue)
    
    //Define a ATR band upline and bottome line.
    atr = ta.atr(14)
    upline = open + atr[1]
    bottomline = open - atr[1]
    
    plot(upline, color=color.white)
    plot(bottomline, color=color.white)
    
    //use "valuewhen()" function to grab the upline value and bottomline value at the moment of longcondition. It will be used for stoploss and takeprofit.
    longcondition = ta.crossover(ema1, ema2)
    longlimit = ta.valuewhen(longcondition, upline, 0)
    longstop = ta.valuewhen(longcondition, bottomline, 0)
    
    if(longcondition) and strategy.position_size == 0
        strategy.entry("buy", strategy.long, qty=1)
        strategy.exit("exit", from_entry="buy", limit=longlimit, stop=longstop)
    
    //use "valuewhen()" function to grab the upline value and bottomline value at the moment of shortcondition. It will be used for stoploss and takeprofit. 
    shortcondition = ta.crossunder(ema1, ema2)
    shortlimit = ta.valuewhen(shortcondition, bottomline, 0)
    shortstop = ta.valuewhen(shortcondition, upline, 0)
    
    if(shortcondition) and strategy.position_size == 0
        strategy.entry("sell", strategy.short, qty=1)
        strategy.exit("exit", from_entry="sell", limit=shortlimit, stop=shortstop)
    
    //Plot SL and TP
        
    longs = strategy.position_size > 0
    shorts = strategy.position_size < 0
    
    plot(shorts ? shortstop : na, color = color.red, style=plot.style_linebr, linewidth=2)
    plot(shorts ? shortlimit : na, color = color.orange, style=plot.style_linebr, linewidth=2)
    plot(longs ? longstop : na, color = color.red, style=plot.style_linebr, linewidth=2)
    plot(longs ? longlimit : na, color = color.orange, style=plot.style_linebr, linewidth=2)