TradingView
ZenAndTheArtOfTrading
2 ноя 2017 г., 11:10

ATR+ (Stop Loss Indicator) 

NZD/USDOANDA

Описание

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.

Информация о релизе

Fixed to work on all markets (previously was bugged on some markets)

Информация о релизе

Updated to Pine Script v5
Комментарии
Ronan67
I just installed this indicator, but why dont I see anything on the chart? Do I need to adjust the settings?
NOBSForex
@Ronan67, Open the settings for the indicator and change the colors to be opposite of whatever background you are using (i.e. if your background is black then change it to white). Also, look at the Opacity of the color in the settings. I had a black background and didn't see anything either when I opened it but changing the color and opacity fixed this problem. Good luck!
JansIdeas
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
ZenAndTheArtOfTrading
@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 :)
correa17
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)
ZenAndTheArtOfTrading
@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
@mjslabosz, 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
@mjslabosz, 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!
ZenAndTheArtOfTrading
@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 - tradingview.com/wiki/Indicator_repainting

And this post - tradingview.com/wiki/Pine_Script:_Release_Notes

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
@mjslabosz, 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[0] > strategy.position_size[1]
sold = strategy.position_size[0] < strategy.position_size[1]

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)
Ещё