# Use of ATR at entry candle for TP and 1.5xATR at entry candle for SL in ThinkScript?

#### GreySpecter

##### New member
I am working a money management strategy that uses 1xATR at the entry candle for TP and 1.5xATR at the entry candle for SL. I have not been successful in figuring out how to save that value in ThinkScript as the ATR adjusts. Does anyone have some good ideas?

Last edited:

#### mashume

##### Well-known member
VIP
@GreySpecter

The method I've worked with is to define your TP and SL as double.nan when the code initializes, but with a conditional statement that says, in essence:
if there is a value here, use that value otherwise use NaN
so that when your buy signal is triggered, say by a crossover, you put a value into TP and SL. The only time there will be a value in those two is when you are inside a trade. when you exit the trade, you put NaNs back into them.
If that doesn't make any sense to you, I can try to code up something if you have some trigger conditions you can share for entry and exit.

On an aside, I'm not huge on Risk:Reward ratios, but it seems that 1x price target and 1.5x stop loss works against you on the R:R rather severely. Unless I'm reading something poorly (always an option Monday pre-coffee).

Happy trading,
mashume

#### GreySpecter

##### New member
@mashume
Thank you for the response. I may not fully understand what you are talking about, but I will post my attempt to code your suggestion. The part that I added to an HMA crossover strat is denoted by the comment. I only did a test with the LongTP, the others are just placeholders. The issue I have with this version is that the TP adjusts with every candle as the ATR adjusts.

Code:
``````input Price = close;
input Length = 20;
input Displace = 0;

plot HMA = HullMovingAvg(Price, Length, Displace);
plot Long = close crosses above HMA;
plot Short = close crosses below HMA;

#Custom Code Start
def ATR = ATR(14);
plot LongTP = if EntryPrice() then EntryPrice() + ATR else Double.NaN;
plot LongSL = Double.NaN;
plot ShortTP = Double.NaN;
plot shortSL = Double.NaN;

Long.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
Long.SetDefaultColor(GetColor(1));
Short.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
Short.SetDefaultColor(GetColor(2));
LongTP.SetPaintingStrategy(PaintingStrategy.DASHES);
LongTP.SetDefaultColor(GetColor(1));

AddOrder(OrderType.BUY_TO_OPEN, Long, tickColor = GetColor(1), arrowColor = GetColor(1));
AddOrder(OrderType.SELL_TO_CLOSE, high >= LongTP, tickColor = GetColor(1), arrowColor = GetColor(1));
#Custom Code End

HMA.DefineColor("Up", GetColor(1));
HMA.DefineColor("Down", GetColor(0));
HMA.AssignValueColor(if HMA > HMA[1] then HMA.Color("Up") else HMA.Color("Down"));``````

Regarding the 1:1.5 ratio, if it is a 50% win rate, you are very correct that it is not profitable. But it often hits the 1xATR TP before it hits the 1.5xATR SL. I also close the trade if one of my primary indicators gives an opposing signal. I am currently working toward a 70% win rate on my algorithm while taking 50% profit at 1xATR and setting SL to breakeven, allowing runners to grab the rest of the profit on big moves. This would be using the daily chart in Forex.

#### mashume

##### Well-known member
VIP
Code:
``````def LTP = if EntryPrice() then
if isNan(EntryPrice()[1]) then
EntryPrice() + ATR
else LTP[1]
else double.NaN;
plot LongTP = LTP;``````

I'm not certain this is exactly right, but the idea is this:
1. if there is an entryPrice, then there is a trade open so we want to do something
2. if this is the first bar for which there is an EntryPrice (and this is what I'm not sure is correct) then we want to use the entry Price + ATR
3. but if this isn't the first bar that there is an entry price for (the trade has been open longer than this bar) then we use the value from last time

Or something like that.

Happy trading,
Mashume

#### Zara2

##### New member
Hi mashume, this line
if isNan(EntryPrice()[1]) then
Gets an error message: Function "EntryPrice" is not dynamic, indexer is senseless

How can we use your method to store the entry bar's ATR, instead of each new bar's ATR?

#### mashume

##### Well-known member
VIP
Hi mashume, this line

Gets an error message: Function "EntryPrice" is not dynamic, indexer is senseless

How can we use your method to store the entry bar's ATR, instead of each new bar's ATR?
I'll look into it