Help coding my sell signal based on ATR

cornbread

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:
#code written by cornbread

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;

AddOrder(OrderType.BUY_TO_OPEN, buy, name = "squeezestrat buy");

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:

rad14733

Well-known member
VIP
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...
 

cornbread

New member
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:
#code written by cornbread

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;


AddOrder(OrderType.BUY_TO_OPEN, buy, name = "squeezestrat buy");

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");
 

cornbread

New member
I think I fixed it. Need to figure out how to add partial sells to it now.
Code:
#code written by cornbread

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;


AddOrder(OrderType.BUY_TO_OPEN, buy, name = "squeezestrat buy");

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");
 

RickAns

Active member
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.
 

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

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?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Top