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.
Feb 16
Phát hành các Ghi chú: Minor improvements
Mar 03
Phát hành các Ghi chú: Converted ATR and S/L into whole numbers.
Added optional T1 (Profit Target).
Mar 03
Phát hành các Ghi chú: Minor improvements to display chart.
Mar 03
Phát hành các Ghi chú: Minor improvements.
Sep 15
Phát hành các Ghi chú: Converted to Pine Script v4 and cleaned up code to remove unnecessary lines.
Gỡ bỏ khỏi Script Ưa thích Thêm vào Script Ưa thích
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!
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)
+1 Phản hồi
@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.
Phản hồi
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 :)
Phản hồi
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!
Phản hồi
@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 :)
Phản hồi
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 Phản hồi
@correa17, Sweet! Well done man, very interesting. Thanks for sharing that code :)
Phản hồi
correa17 ZenAndTheArtOfTrading
@ZenAndTheArtOfTrading, Thank you for sharing your code also!
Phản hồi
Is there a way to ring the cross alert? Thanks
Phản hồi
@msY, Sorry friend but I'm not sure what type of alert you want for this indicator. You can add this to the end of the script to get alerts (but you need to fill in the CONDITION part):

alertcondition(CONDITION , title="Alert Title", message="Alert Message")
Phản hồi
Trang chủ Bộ lọc cổ phiếu Bộ lọc Forex Bộ lọc Tiền điện tử Lịch kinh tế Shows Cách thức hoạt động Tính năng Biểu đồ Giá Quy tắc Áp dụng Người điều hành Giải pháp cho Website & Nhà môi giới Widget Giải pháp biểu đồ Thư viện Biểu đồ Lightweight Trung tâm Trợ giúp Giới thiệu bạn Yêu cầu tính năng Blog & Tin tức Câu hỏi thường gặp Wiki Twitter
Hồ sơ Tùy chỉnh Hồ sơ Tài khoản và Thanh toán Giới thiệu bạn Ticket Hỗ trợ của tôi Trung tâm Trợ giúp Các ý tưởng đã đăng Người theo dõi Đang theo dõi Tin nhắn riêng Trò chuyện Đăng xuất