# MTF Moving Average
input Period1 = AggregationPeriod.MIN;
input Period2 = AggregationPeriod.FIVE_MIN;
input Period3 = AggregationPeriod.FIFTEEN_MIN;
input Period14 = AggregationPeriod.HOUR;
input AvgType = AverageType.EXPONENTIAL;
input Length1 = 9;
input Length2 = 20;
input priceclose = close;
#15 min – 9EMA above 20 EMA
def AVG1 = MovingAverage(AvgType, close(period = Period3), Length1);
def AVG2 = MovingAverage(AvgType, close(period = Period3), Length2);
#5 min – 9 EMA above 20 EMA
def AVG3 = MovingAverage(AvgType, close(period = Period2), Length1);
def AVG4 = MovingAverage(AvgType, close(period = Period2), Length2);
#1 min price close above 9EMA
def AVG5 = MovingAverage(AvgType, close(period = Period1), Length1);
def AVG6 = MovingAverage(AvgType, close(period = Period1), Length2);
#vwap
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));
plot VWAP = price;
plot UpperBand = price + numDevUp * deviation;
plot LowerBand = price + numDevDn * deviation;
#RSI
input length = 14;
input over_Bought = 50;
input over_Sold = 30;
input price2 = close;
input averageType = AverageType.WILDERS;
input showBreakoutSignals = no;
def NetChgAvg = MovingAverage(averageType, price2 - price2[1], length);
def TotChgAvg = MovingAverage(averageType, AbsValue(price2 - price2[1]), length);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;
plot RSI = 50 * (ChgRatio + 1);
plot OverSold = over_Sold;
plot OverBought = over_Bought;
plot UpSignal = if RSI crosses above OverSold then OverSold else Double.NaN;
plot DownSignal = if RSI crosses below OverBought then OverBought else Double.NaN;
UpSignal.SetHiding(!showBreakoutSignals);
DownSignal.SetHiding(!showBreakoutSignals);
plot long = VWAP < close and AVG1 > AVG2 and AVG3 > AVG4 and AVG3 < close and AVG5 < close and RSI > over_Bought;
#long.SetPaintingStrategy(
#PaintingStrategy.BOOLEAN_ARROW_UP);
#Alert(long, "arrow up", Alert.BAR, Sound.Ring);
AssignPriceColor(if long then Color.white else Color.current);
plot short = VWAP > close and AVG1 < AVG2 and AVG3 < AVG4 and AVG3 > close and AVG5 > close and RSI < over_Bought;
#short.SetPaintingStrategy(
#PaintingStrategy.BOOLEAN_ARROW_down);
#Alert(short, "arrow down", Alert.BAR, Sound.Ring);
AssignPriceColor(if short then Color.white else Color.current);
AddOrder(OrderType.buy_AUTO,long, arrowcolor = Color.Dark_Orange, name = "buy");
AddOrder(OrderType.SELL_TO_CLOSE, vwap>close or avg3>close or avg6>close, tickcolor = Color.CYAN, arrowcolor = Color.CYAN, name = "TAKE PROFIT");
AddOrder(OrderType.sell_AUTO,short, arrowcolor = Color.Dark_Orange, name = "sell");
AddOrder(OrderType.buy_TO_CLOSE, vwap<close or avg3<close or avg6<close, tickcolor = Color.CYAN, arrowcolor = Color.CYAN, name = "TAKE PROFIT");