Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
634 views
in Technique[技术] by (71.8m points)

pine script - Stop loss with ATR

img - https://prnt.sc/xr8q0u

strategy("Stop Loss Example: Simple Stoploss", overlay=true)
sma_per = input(200, title='SMA Lookback Period', minval=1)
sl_inp = input(2.0, title='Stop Loss %', type=float)/100
tp_inp = input(3.0, title='Take Profit %', type=float)/100

stop_level = strategy.position_avg_price * (1 - sl_inp)
take_level = strategy.position_avg_price * (1 + tp_inp)

sma = sma(close, sma_per)
strategy.entry("Simple SMA Entry", strategy.long, when=crossover(close, sma))

strategy.exit("Stop Loss/TP","Simple SMA Entry", stop=stop_level, limit=take_level)

plot(stop_level, color=red, style=linebr, linewidth=2)
plot(take_level, color=green, style=linebr, linewidth=2)

Like in the picture above I would like the TP&SL in a nice horizontal line, when the future candle hits the TP or SL it should exit the trade.

img - https://prnt.sc/xr8nu1

strategy("Stop Loss Example: Simple Stoploss", overlay=true)

ATR = atr(input(defval=14,title="ATR",type=integer))[1]
Multip = input(defval=1,title="Multiplier",type=integer)


sma_per = input(200, title='SMA Lookback Period', minval=1)
sl_inp = input(2.0, title='Stop Loss %', type=float)/100
tp_inp = input(3.0, title='Take Profit %', type=float)/100
Longstop =low - ATR * Multip

test1 = strategy.position_avg_price - Longstop
test2 = (100 / strategy.position_avg_price * test1) / 100

sma = sma(close, sma_per)

stop_level = strategy.position_avg_price * (1 - test2)
take_level = strategy.position_avg_price * (1 + tp_inp)

strategy.entry("Simple SMA Entry", strategy.long, when=crossover(close, sma))

strategy.exit("Stop Loss/TP","Simple SMA Entry", stop=stop_level, limit=take_level)

plot(stop_level, color=red, style=linebr, linewidth=2)
plot(take_level, color=green, style=linebr, linewidth=2)

Only I want to use ATR for SL. I've tried something myself, but the SL line is recalculated by every candle.

How do I get the SL in a horizontal line?

question from:https://stackoverflow.com/questions/65936463/stop-loss-with-atr

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...