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

Categories

0 votes
595 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 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...