Help coding my sell signal based on ATR

New member
I am trying to code a profit target sell at 3x ATR above the buy signal and a sell stop 2x ATR below the buy signal not sure how I can accomplish this. Sorry, I am new to thinkscript my code is pretty messy.

Code:

input displace = 0;
input factor = 1.5;
input length = 20;
input price = close;
input averageType = AverageType.SIMPLE;
input trueRangeAverageType = AverageType.SIMPLE;

def shift = factor * MovingAverage(trueRangeAverageType, TrueRange(high, close, low), length);

def average = MovingAverage(averageType, price, length);

def Avg = average[-displace];

def Upper_Band = average[-displace] + shift[-displace];
def Lower_Band = average[-displace] - shift[-displace];

input Num_Dev_Dn = -2.0;
input Num_Dev_up = 2.0;

def sDev = StDev(data = price[-displace], length = length);

def MidLine = MovingAverage(averageType, data = price[-displace], length = length);
def BBLowerBand = MidLine + Num_Dev_Dn * sDev;
def BBUpperBand = MidLine + Num_Dev_up * sDev;

def avg1 = MovingAverage(AverageType.EXPONENTIAL, price, 8);
def avg2 = MovingAverage(AverageType.EXPONENTIAL, price, 21);

def squeeze = Upper_Band > BBUpperBand and Lower_Band < BBLowerBand;
def unsqueeze = Upper_Band < BBUpperBand and Lower_Band > BBLowerBand;

input period = 252;
input choice = {default High, Low, "High or Low"};
input choice1 = 10.0;

def hi = high;
def lo = low;

plot scan;
switch (choice) {
case High:
scan = price >= Highest(hi, period) * ((100 - choice1) / 100);
case Low:
scan = price <= Lowest(lo, period) * ((100 + choice1) / 100);
case "High or Low":
scan = price >= Highest(hi, period) * ((100 - choice1) / 100) or price <= Lowest(lo, period) * ((100 + choice1) / 100)
;
}

def buy = scan and squeeze and avg1 > avg2 and price > avg2;

input trailType = {default modified, unmodified};
input ATRPeriod = 14;
input ATRFactor = 2.0;
input firstTrade = {default long, short};
input ATRAverage = AverageType.WILDERS;

def sell = ATRTrailingStop(trailType, ATRPeriod, ATRFactor, firstTrade, ATRAverage).sellSignal;

def profittarget = unsqueeze and price < low[2];

AddOrder(OrderType.SELL_TO_CLOSE, sell, tickcolor = GetColor(1), arrowcolor = GetColor(1), name = "2 atr sell stop");

AddOrder(OrderType.SELL_TO_CLOSE, profittarget, tickcolor = GetColor(1), arrowcolor = GetColor(1), name = "profit target");

Last edited by a moderator:
I'm not seeing EntryPrice() anywhere in your script... You would calculate your 3ATR target from that price... That is unless you only want it for backtesting using your own trade price...

oh I didn't realize that I was using the buy variable as my entry point.

this is what I came up with but something is messed up it is hitting the 2 atr sell stop on candles that were above the the entry candle.

Code:

input displace = 0;
input factor = 1.5;
input length = 20;
input price = close;
input averageType = AverageType.SIMPLE;
input trueRangeAverageType = AverageType.SIMPLE;

def shift = factor * MovingAverage(trueRangeAverageType, TrueRange(high, close, low), length);

def average = MovingAverage(averageType, price, length);

def Avg = average[-displace];

def Upper_Band = average[-displace] + shift[-displace];
def Lower_Band = average[-displace] - shift[-displace];

input Num_Dev_Dn = -2.0;
input Num_Dev_up = 2.0;

def sDev = StDev(data = price[-displace], length = length);

def MidLine = MovingAverage(averageType, data = price[-displace], length = length);
def BBLowerBand = MidLine + Num_Dev_Dn * sDev;
def BBUpperBand = MidLine + Num_Dev_up * sDev;

def avg1 = MovingAverage(AverageType.EXPONENTIAL, price, 8);
def avg2 = MovingAverage(AverageType.EXPONENTIAL, price, 21);

def squeeze = Upper_Band > BBUpperBand and Lower_Band < BBLowerBand;
def unsqueeze = Upper_Band < BBUpperBand and Lower_Band > BBLowerBand;

input period = 252;
input choice = {default High, Low, "High or Low"};
input choice1 = 10.0;

def hi = high;
def lo = low;

plot scan;
switch (choice) {
case High:
scan = price >= Highest(hi, period) * ((100 - choice1) / 100);
case Low:
scan = price <= Lowest(lo, period) * ((100 + choice1) / 100);
case "High or Low":
scan = price >= Highest(hi, period) * ((100 - choice1) / 100) or price <= Lowest(lo, period) * ((100 + choice1) / 100)
;
}

def buy = scan and squeeze and avg1 > avg2 and price > avg2;

def sell = entryprice() - (2*ATR());
def profittarget = entryprice() + (3*ATR());
def profittarget2 = unsqueeze and price < low[2];

AddOrder(OrderType.SELL_TO_CLOSE, sell, tickcolor = GetColor(1), arrowcolor = GetColor(1), name = "2 atr sell stop");

AddOrder(OrderType.SELL_TO_CLOSE, profittarget, tickcolor = GetColor(1), arrowcolor = GetColor(1), name = "3 atr profit target");

I think I fixed it. Need to figure out how to add partial sells to it now.
Code:

input displace = 0;
input factor = 1.5;
input length = 20;
input price = close;
input averageType = AverageType.SIMPLE;
input trueRangeAverageType = AverageType.SIMPLE;

def shift = factor * MovingAverage(trueRangeAverageType, TrueRange(high, close, low), length);

def average = MovingAverage(averageType, price, length);

def Avg = average[-displace];

def Upper_Band = average[-displace] + shift[-displace];
def Lower_Band = average[-displace] - shift[-displace];

input Num_Dev_Dn = -2.0;
input Num_Dev_up = 2.0;

def sDev = StDev(data = price[-displace], length = length);

def MidLine = MovingAverage(averageType, data = price[-displace], length = length);
def BBLowerBand = MidLine + Num_Dev_Dn * sDev;
def BBUpperBand = MidLine + Num_Dev_up * sDev;

def avg1 = MovingAverage(AverageType.EXPONENTIAL, price, 8);
def avg2 = MovingAverage(AverageType.EXPONENTIAL, price, 21);

def squeeze = Upper_Band > BBUpperBand and Lower_Band < BBLowerBand;
def unsqueeze = Upper_Band < BBUpperBand and Lower_Band > BBLowerBand;

input period = 252;
input choice = {default High, Low, "High or Low"};
input choice1 = 10.0;

def hi = high;
def lo = low;

plot scan;
switch (choice) {
case High:
scan = price >= Highest(hi, period) * ((100 - choice1) / 100);
case Low:
scan = price <= Lowest(lo, period) * ((100 + choice1) / 100);
case "High or Low":
scan = price >= Highest(hi, period) * ((100 - choice1) / 100) or price <= Lowest(lo, period) * ((100 + choice1) / 100)
;
}

def buy = scan and squeeze and avg1 > avg2 and price > avg2;

def sell = close <= (entryprice() - (ATR()*2));
def profittarget = close >= (entryprice() + (ATR()*3));
def profittarget2 = unsqueeze and price < low[2];

AddOrder(OrderType.SELL_TO_CLOSE, sell, tickcolor = GetColor(1), arrowcolor = GetColor(1), name = "2 atr sell stop");

AddOrder(OrderType.SELL_TO_CLOSE, profittarget, tickcolor = GetColor(1), arrowcolor = GetColor(1), name = "3 atr profit target");

Please let us know if you get the partial sells to work. I have gotten partial adds to work in another project, but not the sells yet. Always seems to close out the entire position.

Not the exact question you're looking for?

87k+ Posts
128 Online

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?