input price = close;
input MainLength = 1000;
input Sensitivity = 30;
input Sensitivity2 = 15;
input BuyAmount = 20;
input SellAmount = 1;
input SellHeavyAmount = 35;
input AvgDownAmount = 20;
def linDev = LinDev(price, 1000);
def CCI = if linDev == 0 then 0 else (price - Average(price, 1000)) / linDev / 0.015;
def CCIDrop = CCI > CCI[1];
def EMA1 = MovingAverage(AverageType.EXPONENTIAL, price, 90);
def HMA1 = MovingAverage(AverageType.HULL, price, MainLength);
def HMA2 = MovingAverage(AverageType.HULL, price, 180);
def HMA3 = MovingAverage(AverageType.HULL, price, 40);
def MainBullishZone = HMA1 > HMA1[Sensitivity];
def MainBearishZone = HMA1 < HMA1[Sensitivity2] and CCIDrop;
def SubBullishZone = HMA2 > HMA2[200];
def SubBearishZone = HMA2 < HMA2[35];
def IntraBullishZone = HMA3 > HMA3[10];
def IntraBearishZone = HMA3 < HMA3[10];
def BUYSignal = MainBullishZone and SubBearishZone and IntraBullishZone and (HMA1 < HMA2) and (HMA1 < HMA3) and (HMA3 < HMA2) and price < HMA3 and price < EMA1 and CCI < 100;
def AvgDownSignal = MainBullishZone and SubBearishZone and IntraBullishZone and price < HMA1 and price < HMA2 and CCI < 100 and HMA2 < HMA2[155];
def BuyExitSignal = price crosses below HMA2 and MainBullishZone and SubBullishZone and IntraBearishZone and price > HMA1 and CCI < 100;
def SELLSignal = MainBearishZone and SubBullishZone and IntraBearishZone and (HMA1 > HMA2) and (HMA1 > HMA3) and (HMA3 > HMA2) and price < HMA3 and price > EMA1;
def EmergencySELL = SubBearishZone and CCI < -30;
def EmergencyCOVER = CCI > 5 and CCI < 100 and CCI > CCI[200];
def Short = price crosses below HMA2 and MainBullishZone and SubBullishZone and IntraBearishZone and price > HMA1 and CCI > 140;
input crossingType = {default above, below};
def avg1 = MovingAverage(AverageType.EXPONENTIAL, close, 60);
def avg2 = MovingAverage(AverageType.EXPONENTIAL, close, 180);
def signal = Crosses(avg1, avg2, crossingType == crossingType.above);
def signal2 = avg1 crosses above avg2;
AddOrder(condition = BUYSignal, name = "BUY", price = close, tradeSize = BuyAmount);
AddOrder(condition = AvgDownSignal, name = "AvgDown", tradeSize = AvgDownAmount);
AddOrder(OrderType.SELL_AUTO, BuyExitSignal and !MainBullishZone[30], name = "Sell", tradeSize = SellAmount);
AddOrder(OrderType.SELL_AUTO, Short, name = "Sell Heavy", tradeSize = SellHeavyAmount);
AddOrder(condition = MainBearishZone, name = "Close", type = OrderType.SELL_TO_CLOSE);
AddOrder(OrderType.BUY_TO_CLOSE, SELLSignal, name = "COVER");