Help coding my sell signal based on ATR

C

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

rad14733

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

cornbread

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

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

rad14733

Active member
VIP
@cornbread I'll be reviewing your code as soon as I'm freed up...
 
C

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

RickAns

Member
VIP
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

Top