1

Code:

//@version=5
strategy("BTCTEST", overlay=true)
len = input(7)
nATRs = input(0.4)
atr = ta.sma(ta.tr, len)*nATRs

if (not na(close[len]))
    strategy.entry("Long", strategy.long, stop=close+atr, comment = "Long")
    strategy.entry("Short", strategy.short, stop=close-atr, comment = "Short")

This is my modify

//@version=5
indicator("BTCTEST", overlay=true)
len = input(7)
nATRs = input(0.4)
atr = ta.sma(ta.tr, len)*nATRs
        
//if (not na(close[len]))
  //strategy.entry("Long", strategy.long, stop=close+atr, comment = "Long")
  //strategy.entry("Short", strategy.short, stop=close-atr, comment = "Short")

ebuy = ???
esell = ???
    
plotshape(ebuy,  title = "Buy",  text = 'Buy',  style = shape.labelup,   location = location.belowbar, color= color.green, textcolor = color.white, transp = 0, size = size.tiny)
plotshape(esell, title = "Sell", text = 'Sell', style = shape.labeldown, location = location.abovebar, color= color.red,   textcolor = color.white, transp = 0, size = size.tiny)   
        
alertcondition(ebuy, title='BUY', message='e=bitmex s=xbtusd delay=1 b=buy t=market q=100% d=1')
alertcondition(esell, title='SELL', message='e=bitmex s=xbtusd delay=1 b=sell t=market q=100% d=1')

i don't understand stop=close+atr and stop=close-atr , if (not na(close[len]))

How should I check long and short? I spent many hours for this. I hope someone can help me.

1 Answers1

0
if (not na(close[len]))
    ...

is false only first len bars on the chart, it is the same as

if bar_index > len
    ...

So basically the strategy. functions will be called on every bar after bar 7 (no long and short conditions).

For checking for short and long take for example default strategy sctipt (Open --> Strategy) with ta.crossover as the conditions


//@version=5
strategy("My strategy", overlay=true, margin_long=100, margin_short=100)


len = input(7)
nATRs = input(0.4)
atr = ta.sma(ta.tr, len)*nATRs


ebuy = ta.crossover(ta.sma(close, 14), ta.sma(close, 28))
if (ebuy)
    strategy.entry("Long", strategy.long, stop=close+atr, comment = "Long")

esell = ta.crossunder(ta.sma(close, 14), ta.sma(close, 28))
if (esell)
    strategy.entry("Short", strategy.short, stop=close-atr, comment = "Short")


plotshape(ebuy,  title = "Buy",  text = 'Buy',  style = shape.labelup,   location = location.belowbar, color= color.green, textcolor = color.white, transp = 0, size = size.tiny)
plotshape(esell, title = "Sell", text = 'Sell', style = shape.labeldown, location = location.abovebar, color= color.red,   textcolor = color.white, transp = 0, size = size.tiny)   
        ```
Starr Lucky
  • 1,578
  • 1
  • 7
  • 8