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!
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)
+2 回复
@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")
回复
首页 股票筛选器 外汇筛选器 加密货币筛选器 财经日历 如何运作 图表功能 价格 网站规则 版主 网站 & 经纪商解决方案 插件 图表解决方案 帮助中心 功能请求 博客 & 新闻 常见问题 维基百科 Twitter
概述 个人资料设置 账号和账单 TradingView代币 我的客服工单 帮助中心 已发表观点 粉丝 正在关注 私人消息 聊天 退出