#RCONNER7 - 4/2020
#VWAP REVERSAL/BOUNCE - CANDLE FORMATION
#v1
# Length of the candle's wick
def UpperWick = high - Max(open, close);
def LowerWick = Min(open, close) - low;
def FullLength = AbsValue(high - low);
# Length of the candle's body
def CandleBody = AbsValue(open - close);
def AvgBodyFull = Round((CandleBody / FullLength) * 100, 1);
def smallbody = if AvgBodyFull < 10 then 1 else 0;
#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));
def VWAP = price;
def UpperBand = price + numDevUp * deviation;
def LowerBand = price + numDevDn * deviation;
##True Range
input RangeGreater = .01;
def Range = TrueRangeindicator();
def OK_Range = if Range > RangeGreater then 1 else 0;
#IMI
input length = 21;
input smooth_length = 1;
def CloseOpenDiff = if close > open then close - open else 0;
def OpenCloseDiff = if close < open then open - close else 0;
def avgCloseOpen = Average(CloseOpenDiff, length);
def avgOpenClose = Average(OpenCloseDiff, length);
def IMI = avgCloseOpen / (avgCloseOpen + avgOpenClose) * 100;
#IMI.SetDefaultColor(GetColor(8));
def avgIMI = Average(IMI, smooth_length);
def IMI_VWAPLower = if low < LowerBand and avgIMI < 30 then 1 else 0;
def IMI_VWAPHigher = if high > UpperBand and avgIMI > 70 then 1 else 0;
def IMI_Low = if avgIMI < 30 then 1 else 0;
def IMI_High = if avgIMI > 70 then 1 else 0;
# Compare body to wicks
input CandleWickVar = 1.75;
def Hammer = (lowerWick / CandleBody >= CandleWickVar) and if smallbody then lowerwick > upperWick * candleWickVar else upperWick / CandleBody <= 1.33;
def STAR = (UpperWick / CandleBody >= CandleWickVar) and if smallbody then LowerWick * candleWickVar < UpperWick else (LowerWick / CandleBody <= 1.33);
#trend
input trendlookback = 10;
def uptrend = isAscending(open, trendlookback)[1];
def downtrend = isdescending(open, trendlookback)[1];
#lowest/Highest within
input HiLowLookback = 10;
def hh = high == Highest(high, HiLowLookback);
def ll = low == Lowest(low, HiLowLookback);
def bullVWAP = if open >= reference VWAP()."VWAP" and close >= reference VWAP()."VWAP" then 1 else 0;
def bearVWAP = if open <= reference VWAP()."VWAP" and close <= reference VWAP()."VWAP" then 1 else 0;
plot Hammer_Signal_IMI = Hammer and ll and bullVWAP and IMI_Low and OK_Range;
plot STAR_Signal_IMI = STAR and hh and bearVWAP and IMI_High and OK_Range;
#plot Hammer_Signal = Hammer and ll and bullVWAP and OK_Range and !Hammer_Signal_IMI and downtrend;
#plot STAR_Signal = STAR and hh and bearVWAP and OK_Range and !STAR_Signal_IMI and uptrend;
plot BullRev = Hammer and ll and IMI_VWAPLower and OK_Range;
plot BearRev = STAR and hh and IMI_VWAPHigher and OK_Range;
#Hammer_Signal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
#Hammer_Signal.AssignValueColor(Color.GREEN );
Hammer_Signal_IMI.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
Hammer_Signal_IMI.AssignValueColor(Color.YELLOW);
BullRev.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
BullRev.AssignValueColor(Color.BLUE );
#STAR_Signal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
#STAR_Signal.AssignValueColor(Color.GREEN );
STAR_Signal_IMI.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
STAR_Signal_IMI.AssignValueColor(Color.YELLOW);
BearRev.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
BearRev.AssignValueColor(Color.BLUE );
alert(Hammer_Signal_IMI or BullRev, "BUY BUY BUY", Alert.Bar, Sound.Bell);
alert(STAR_Signal_IMI or BearRev, "SELL SELL SELL", Alert.Bar, Sound.Ring);