ZenAndTheArtOfTrading

ATR+ (Stop Loss Indicator)

This script is designed to aid in back-testing and trade execution.

It displays three sets of values - the teal colored value is the current ATR, the green colored value is your stop loss distance (in pips) below the most recent swing low for long trades, and the red colored value is your stop loss distance (in pips) above the most recent swing high for short trades.

You can change the stop loss settings to base your stop loss on a set pip amount or by however many multiples of the current ATR as you wish (eg. 1.5x ATR).

Feel free to ask any questions or edit the script without permission :)

- Matt.
เอกสารเผยแพร่: Minor improvements
เอกสารเผยแพร่: Converted ATR and S/L into whole numbers.
Added optional T1 (Profit Target).
เอกสารเผยแพร่: Minor improvements to display chart.
เอกสารเผยแพร่: Minor improvements.
เอกสารเผยแพร่: Converted to Pine Script v4 and cleaned up code to remove unnecessary lines.
ลบออกจากสคริปส์โปรด เพิ่มเข้าไปในสคริปส์โปรด
Follow @TradeWisdom on Twitter for Daily inspiration from the best trading wizards, psychologists and philosophers.

Go to http://www.zenandtheartoftrading.com for articles on forex trading, trading psychology, trading resources, Pine Script lessons and more!

ความคิดเห็น

Hey, i like the indicator! i am just a bit confused sometimes it shows like great pip value for example 50, and then like 17 pips that is way too close. How should be the settings for trading the 1H impulses? i would want to have like a 30 pips room
+2 ตอบกลับ
@JansIdeas, Hey friend! The ATR is designed to contract when volatility contracts and expand when volatility expands, so if price isn't moving very much over the past 14 candles then you will have a tighter stop loss. If it is too close, then one way you can deal with that is to have a rule in your trading plan that says the stop loss must be 30 pips minimum. So if the indicator plots a stop loss less than 30 pips, then you over-ride it and set a 30 pip stop.

I hope that helps understand what's going on, good luck with your trading :)
+1 ตอบกลับ
Hi, I wanted to add the code to a strategy that I have. I added your code and then fix the stop loss and take profit this way. I don't know why but the calculation is not correct, any idea what I have wrong? Thanks.

strategy.exit("Exit Long", from_entry = "BUY", profit = longEntrySize, loss = longEntrySize)
strategy.exit("Exit Short", from_entry = "SELL", profit = shortEntrySize, loss = shortEntrySize)
+2 ตอบกลับ
@correa17, For strategies you need to use the actual price not the pips, so change longEntrySize and shortEntrySize to this:

stopSizeATR = atr(length)
longStopPrice = (close - lowest(low, lookback)) - stopSizeATR
longTargetPrice = (close - lowest(low, lookback)) + stopSizeATR
shortStopPrice = (highest(high, lookback) - close) + stopSizeATR
shortTargetPrice = (highest(high, lookback) - close) - stopSizeATR

And then use:

strategy.exit("Exit Long", from_entry = "BUY", profit = longTargetPrice, loss = longStopPrice)
strategy.exit("Exit Short", from_entry = "SELL", profit = shortTargetPrice, loss = shortStopPrice)

That should do the trick :) let me know if it doesn't, but that's basically what I use in my personal strategies.
ตอบกลับ
ZenAndTheArtOfTrading ZenAndTheArtOfTrading
@ZenAndTheArtOfTrading, Wait sorry I messed that code up. Use this instead:

stopSizeATR = atr(length)
longStopPrice = (close - lowest(low, lookback)) - stopSizeATR
longTargetPrice = close + stopSizeATR
shortStopPrice = (highest(high, lookback) - close) + stopSizeATR
shortTargetPrice = close - stopSizeATR

If you want to adjust the RR, change every reference of "stopSizeATR" to "(stopSizeATR * RR)". Let me know if you need help or have any questions :)
ตอบกลับ
correa17 ZenAndTheArtOfTrading
@ZenAndTheArtOfTrading, Thank you for your response. I tried but I have a problem. When I use close or a any indicator like (atr(period)) in strategy.exit, the problem that I found is that the script go and find those values in the future, not in the time the script put the trade, and of course the close price when I open the trade is different that the close price in the future when the script is looking to close that trade.
Of course your code is not wrong, is just how trading view code is.
Do you know any way to make this right? I need to use the close price and ATR price WHEN I open the trade, that way I can calculate the position size and know my risk before the trade.
Thank you!
ตอบกลับ
@correa17, Damn that sucks, I'm not really sure how to fix that. I'll do some digging when I get time and see what I can come up with, but it sounds like this is a problem inherent in PineScript according to this post - https://www.tradingview.com/wiki/Indicator_repainting

And this post - https://www.tradingview.com/wiki/Pine_Script:_Release_Notes#Repainting_issue

Are you using security() to get any values in your script? If you want, PM me your code and I'll see if I can get it to work on my end. Otherwise you may have to contact TradingView support for that one. Sorry I can't be of more help! Good luck :)
ตอบกลับ
correa17 ZenAndTheArtOfTrading
@ZenAndTheArtOfTrading, Thank for your help, I found a way, is not very elegant but is something : )
Here it is, basically is finding the value of the ATR at the moment the scrip open a trade (and the position changed). Consider factor_profit variable as an option to have more or less profit target regarding the stop loss:

bought = strategy.position_size > strategy.position_size
sold = strategy.position_size < strategy.position_size

longStop = mult_keltner * valuewhen(bought, Atr, 0)
shortStop = mult_keltner * valuewhen(sold, Atr, 0)
longProfit = factor_profit * longStop
shortProfit = factor_profit * shortStop


if(decimals == 5)
longStop := longStop * 100000
longProfit := longProfit *100000
if(decimals == 4)
longStop := longStop * 10000
longProfit := longProfit * 10000
if(decimals == 3)
longStop := longStop * 1000
longProfit := longProfit * 1000
if(decimals == 2)
longStop := longStop * 100
longProfit := longProfit *100
if(decimals == 5)
shortStop := shortStop * 100000
shortProfit := shortProfit * 100000
if(decimals == 4)
shortStop := shortStop * 10000
shortProfit := shortProfit * 10000
if(decimals == 3)
shortStop := shortStop * 1000
shortProfit := shortProfit * 1000
if(decimals == 2)
shortStop := shortStop * 100
shortProfit := shortProfit * 100

strategy.exit("Exit Long", from_entry = "BUY", loss =longStop, profit = longProfit)
strategy.exit("Exit Short", from_entry = "SELL", loss =shortStop, profit = shortProfit)
+3 ตอบกลับ
@correa17, Sweet! Well done man, very interesting. Thanks for sharing that code :)
ตอบกลับ
correa17 ZenAndTheArtOfTrading
@ZenAndTheArtOfTrading, Thank you for sharing your code also!
ตอบกลับ
หน้าแรก ตัวช่วยคัดกรองหุ้น ตัวคัดกรองฟอเร็กซ์ ตัวคัดกรองคริปโต ปฏิทินเศรษฐกิจ มันทำงานได้อย่างไร ฟีเจอร์ของชาร์ต อัตราค่าบริการ แนะนำเพื่อน กฎระเบียบการใช้งาน ศูนย์ช่วยเหลือ โซลูชั่นสำหรับเว็บไซต์ & โบรกเกอร์ วิดเจ็ต ชาร์ตโซลูชั่น Lightweight ชาร์ตไลบรารี บล็อก & ข่าว Twitter
โปรไฟล์ ตั้งค่าโปรไฟล์ บัญชีและการเรียกเก็บเงิน เพื่อนที่ชวนมา เหรียญ ตั๋วการขอความสนับสนุนของฉัน ศูนย์ช่วยเหลือ ไอเดียที่เผยแพร่ ผู้ติดตาม ติดตาม ข้อความส่วนตัว แชท ออกจากระบบ