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
הערות שחרור: Minor improvements
Mar 03
הערות שחרור: Converted ATR and S/L into whole numbers.
Added optional T1 (Profit Target).
Mar 03
הערות שחרור: Minor improvements to display chart.
Mar 03
הערות שחרור: Minor improvements.
Sep 15
הערות שחרור: 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!
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 השב
@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!
השב
Is there a way to ring the cross alert? Thanks
השב
@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")
השב
בית סורק מניות סורק מט"ח סורק מטבעות-קריפטו יומן כלכלי סדרות איך זה עובד‏ תכונות גרף מחירון חוקי הבית מנהלים כלים לאתרים וברוקרים יישומונים פתרונות גרפיים ספריית גרפים קלי משקל מרכז תמיכה הפנה חבר בקשת פונקציה בלוג וחדשות שאלות נפוצות ויקי טוויטר‏
פרופיל הגדרות פרופיל חשבון וחיוב הפנה חבר לשוניות התמיכה שלי מרכז תמיכה רעיונות שפורסמו עוקבים עוקב אחרי... הודעות פרטיות צ'אט התנתק