###### TIME ######
input zoneStartAM = 930;
input zoneEndAM = 1200;
input zoneStartPM = 1330;
input zoneEndPM = 1600;
input zoneEndclose = 1609;
input type = {default NOTRADE, REVERSAL};
def highBar;
def lowBar;
def highBar2;
def lowBar2;
switch (type){
case NOTRADE:
highBar = if SecondsTillTime(zoneStartAM) <= 0 and SecondsTillTime(zoneEndAM) >= 0 then HighestAll(open) else Double.NaN;
lowBar = if SecondsTillTime(zoneStartAM) <= 0 and SecondsTillTime(zoneEndAM) >= 0 then LowestAll(close) else Double.NaN;
case REVERSAL:
lowBar = if SecondsTillTime(zoneStartAM) <= 0 and SecondsTillTime(zoneEndAM) >= 0 then HighestAll(open) else Double.NaN;
highBar = if SecondsTillTime(zoneStartAM) <= 0 and SecondsTillTime(zoneEndAM) >= 0 then LowestAll(close) else Double.NaN;
}
switch (type){
case NOTRADE:
highBar2 = if SecondsTillTime(zoneStartPM) <= 0 and SecondsTillTime(zoneEndPM) >= 0 then HighestAll(open) else Double.NaN;
lowBar2 = if SecondsTillTime(zoneStartPM) <= 0 and SecondsTillTime(zoneEndPM) >= 0 then LowestAll(close) else Double.NaN;
case REVERSAL:
lowBar2 = if SecondsTillTime(zoneStartPM) <= 0 and SecondsTillTime(zoneEndPM) >= 0 then HighestAll(open) else Double.NaN;
highBar2 = if SecondsTillTime(zoneStartPM) <= 0 and SecondsTillTime(zoneEndPM) >= 0 then LowestAll(close) else Double.NaN;
}
###### TIME END #####
def adspdl = low("$adspd");
def adspdh = high("$adspd");
def adspdc = close("$adspd");
def volspd = close("$volspd");
def HMA = MovingAverage(AverageType.HULL, close, 21);
input numDevDn = -2.0;
input numDevUp = 2.0;
input timeFrame = {default DAY, WEEK, MONTH};
def cap = getAggregationPeriod();
def errorInAggregation =
timeFrame == timeFrame.DAY and cap >= AggregationPeriod.WEEK or
timeFrame == timeFrame.WEEK and cap >= AggregationPeriod.MONTH;
assert(!errorInAggregation, "timeFrame should be not less than current chart aggregation period");
def yyyyMmDd = getYyyyMmDd();
def periodIndx;
switch (timeFrame) {
case DAY:
periodIndx = yyyyMmDd;
case WEEK:
periodIndx = Floor((daysFromDate(first(yyyyMmDd)) + getDayOfWeek(first(yyyyMmDd))) / 7);
case MONTH:
periodIndx = roundDown(yyyyMmDd / 100, 0);
}
def isPeriodRolled = compoundValue(1, periodIndx != periodIndx[1], yes);
def volumeSum;
def volumeVwapSum;
def volumeVwap2Sum;
if (isPeriodRolled) {
volumeSum = volume;
volumeVwapSum = volume * vwap;
volumeVwap2Sum = volume * Sqr(vwap);
} else {
volumeSum = compoundValue(1, volumeSum[1] + volume, volume);
volumeVwapSum = compoundValue(1, volumeVwapSum[1] + volume * vwap, volume * vwap);
volumeVwap2Sum = compoundValue(1, volumeVwap2Sum[1] + volume * Sqr(vwap), volume * Sqr(vwap));
}
def price = volumeVwapSum / volumeSum;
def deviation = Sqrt(Max(volumeVwap2Sum / volumeSum - Sqr(price), 0));
def VWAP = price;
AssignPriceColor(if adspdl < adspdl[1] && close < vwap && close< hma && volume > volume[1] then Color.RED else if adspdh > adspdh[1] && close > vwap && close > hma && volume>volume[1] then Color.GREEN else Color.GRAY);
####LONGS####
AddOrder(OrderType.BUY_TO_OPEN,secondsTillTime(zoneStartAM) <= 0 && secondsTillTime(zoneEndAM) >= 0 and adspdh > adspdh[1] && close > vwap && vwap>vwap[1]&& volume>volume[1] , name = "Buy open AM @"+open[-1], 1, Color.ORANGE, Color.green);
AddOrder(OrderType.BUY_TO_OPEN,secondsTillTime(zoneStartpM) <= 0 && secondsTillTime(zoneEndpM) >= 0 and adspdh > adspdh[1] && close > vwap && vwap>vwap[1]&& volume>volume[1] , name = "Buy open AM @"+open[-1], 1, Color.ORANGE, Color.green);
##SHORTS###
AddOrder(OrderType.sell_TO_OPEN,secondsTillTime(zoneStartAM) <= 0 && secondsTillTime(zoneEndAM) >= 0 and adspdl < adspdl[1] && close < vwap && vwap<vwap[1]&& volume>volume[1] , name = "Sell open AM @"+open[-1], 1, Color.ORANGE, Color.RED);
AddOrder(OrderType.sell_TO_OPEN,secondsTillTime(zoneStartpM) <= 0 && secondsTillTime(zoneEndpM) >= 0 and adspdl < adspdl[1] && close < vwap && volume>volume[1] , name = "Sell open PM @"+open[-1], 1, Color.ORANGE, Color.RED);
####Close orders######
addorder(orderType.SELL_TO_CLOSE, adspdh < adspdh[1], name = "Sell close @"+open[-1],1,color.orange, color.red);
addorder(orderType.buy_TO_CLOSE, adspdl > adspdl[1],name = "Buy close @"+open[-1],1,color.orange, color.red);
###Market close orders####
addorder(orderType.buy_TO_CLOSE, secondsTillTime(zoneEndclose) <= 0 , name = "Buy Market Close @"+open[-1],1,color.orange, color.red);
addorder(orderType.sell_TO_CLOSE, secondsTillTime(zoneEndclose) <= 0 , name = "Sell Market Close @"+open[-1],1,color.orange, color.red);