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: