#[email protected]
#Attempt at Volume Projection on low TICK charts
#Thanks to YungTraderFromMontana and evanevans for the ideas
#v9.8.2020
declare lower;
def NA = Double.NaN;
input Sentiment = {default None, Inertia, HeikenAshiTrend, AboveBelowMovAvg, AboveBelowHighVolTriggeredVWAP};
input strengthType = {CandleStrength, default OnBalanceVol};
input showCumulativeStr = no;
input showCumulativeStrDiff = yes;
input aggregationInSeconds = 60;
def aggtomin = aggregationInSeconds / 60;
input toggleProjection = {enabled, default disabled};
input elapsedPercentSmoothValue = 5;
input elapsedProjectionDivisor = 2;
input showVerticalExceedPrevCvol = yes;
input showVerticalCycleEndCvol = yes;
def start = 0000;
def end = 1600;
def o = open;
def h = high;
def l = low;
def c = close;
def v = volume;
def t = tick_count;
def bull = c > c[1];
def bear = c < c[1];
def neutral = c == o;
def min = Floor(SecondsFromTime(start) / aggregationInSeconds);
def till = SecondsTillTime(end) / aggregationInSeconds;
def test = min != min[1];
def vc = if test and !test[1] then v else if !test then vc[1] + v else vc[1];
def b = ((c - l) / (h - l)) * v;
def s = ((h - c) / (h - l)) * v;
def cb;
def cs;
switch (strengthType){
case CandleStrength:
cb = if test and !test[1] then b else if !test then cb[1] + b else cb[1];
cs = if test and !test[1] then s else if !test then cs[1] + s else cs[1];
case OnBalanceVol:
cb = if test and !test[1] and bull then v else if test and !test[1] and !bull then 0 else if !test and bull then cb[1] + v else cb[1];
cs = if test and !test[1] and bear then v else if test and !test[1] and !bear then 0 else if !test and bear then cs[1] + v else cs[1];
}
#def tickcount = if test and !test[1] then t else if !test then tickcount[1] + t else tickcount[1];
#def prevtickcount = if test and !test[1] then tickcount[1] else prevtickcount[1];
#def tickperminute = prevtickcount/aggtomin;
#AddLabel(1, prevtickcount, COlor.Magenta);
#AddLabel(1,tickcount,Color.MAGENTA);
#AddLabel(1,tickperminute+" TPM",Color.MAGENTA);
def secondselapsed = (AbsValue(till - Ceil(till)) * aggregationInSeconds);
def secondsleft = aggregationInSeconds - secondselapsed;
def multipliertest = aggregationInSeconds / secondselapsed;
def multiplier = if IsInfinite(multipliertest) then NA else multipliertest;
def percntelapsed = Round(((secondselapsed / aggregationInSeconds) * 100), 0);
def cl = if test then 1 else cl[1] + 1;
def ch = if test then cl[1] else ch[1];
def prevcumulativehigh = if test and !test[1] then vc[1] else prevcumulativehigh[1];
def cvolpassprev = vc >= prevcumulativehigh and vc[1] < prevcumulativehigh;
def highestprevchigh = HighestAll(prevcumulativehigh);
def projectioncalc = vc * multiplier;
######################
#Inertia
input inertiaLength = 20;
input inertiaPrice = close;
def inertia = Inertia(inertiaPrice, inertiaLength);
######################
#Heiken Ashi
def HAopen;
def HAhigh;
def HAlow;
def HAclose;
HAopen = CompoundValue(1, (HAopen[1] + HAclose[1]) / 2, (o[1] + c[1]) / 2);
HAhigh = Max(Max(h, HAopen), HAclose[1]);
HAlow = Min(Min(l, HAopen), HAclose[1]);
HAclose = ohlc4;
######################
#Above Below Moving Average
input movAvgPrice = close;
input movAvgType = AverageType.EXPONENTIAL;
input movAvg1Length = 20;
input movAvg2Length = 30;
input movAvg3Length = 40;
input movAvg4Length = 50;
def MA1 = MovingAverage(movAvgType, movAvgPrice, movAvg1Length);
def MA2 = MovingAverage(movAvgType, movAvgPrice, movAvg2Length);
def MA3 = MovingAverage(movAvgType, movAvgPrice, movAvg3Length);
def MA4 = MovingAverage(movAvgType, movAvgPrice, movAvg4Length);
def MA12test = MA1 > MA2;
def MA23test = MA2 > MA3;
#def MA34test = MA3 > MA4;
######################
#Sigma Vol Filters
input avgVolLength = 20;
input volAverageType = AverageType.SIMPLE;
input Sigma2 = 2.0;
input Sigma3 = 3.0;
def VolAvg = MovingAverage(volAverageType, v, avgVolLength);
def sDev = StDev(data = v, length = avgVolLength);
def VolSigma2 = VolAvg + Sigma2 * sDev;
def VolSigma3 = VolAvg + Sigma3 * sDev;
######################
#VWAP
def vconf = if v > VolSigma3 then 1 else if v > VolSigma2 then 1 else 0;
def VolumeSum = if vconf then v else CompoundValue(1, VolumeSum[1] + v, v);
def VolumeVwapSum = if vconf then v * vwap else CompoundValue(1, VolumeVwapSum[1] + v * vwap, v * vwap);
def volumeVwap2Sum = if vconf then v * Sqr(vwap) else CompoundValue(1, volumeVwap2Sum[1] + v * Sqr(vwap), v * Sqr(vwap));
def VW = if !IsNaN(c) then VolumeVwapSum / VolumeSum else VW[1];
######################
plot cvolpassprevsig = if cvolpassprev then vc else NA;
plot CVol = vc;
plot prevchigh = prevcumulativehigh;
plot cycleavg = Round(TotalSum(if test and !test[1] then ch[1] else 0) / min, 1);
plot cyclehigh = ch;
plot cyclelength = cl;
plot projection;
switch (toggleProjection){
case enabled:
projection = if (projectioncalc >= highestprevchigh) and (percntelapsed < elapsedPercentSmoothValue) then projectioncalc / elapsedProjectionDivisor else projectioncalc;
case disabled:
projection = NA;
}
cycleavg.Hide();
cyclehigh.Hide();
cyclelength.Hide();
plot bsdiff = prevchigh + (cb - cs);
bsdiff.SetHiding(!showCumulativeStrDiff);
def bsdiffaboveztest = bsdiff > prevchigh;
bsdiff.AssignValueColor(if bsdiffaboveztest and bsdiff > bsdiff[1] then Color.GREEN else if bsdiffaboveztest and bsdiff <= bsdiff[1] then Color.DARK_GREEN else if !bsdiffaboveztest and bsdiff < bsdiff[1] then Color.RED else Color.DARK_RED);
plot cbuy = cb;
cbuy.SetHiding(!showCumulativeStr);
plot csell = cs;
csell.SetHiding(!showCumulativeStr);
def Pos;
def Neg;
switch (Sentiment){
case None:
Pos = NA;
Neg = NA;
case Inertia:
Pos = inertia > inertia[1];
Neg = !Pos;
case HeikenAshiTrend:
Pos = HAclose > HAopen;
Neg = !Pos;
case AboveBelowMovAvg:
Pos = MA12test and MA23test;
Neg = !Pos;
case AboveBelowHighVolTriggeredVWAP:
Pos = close > VW;
Neg = !Pos;
}
#Vertical Lines
AddVerticalLine(showVerticalExceedPrevCvol and !IsNaN(cvolpassprevsig), " " + secondselapsed + "s / " + aggregationInSeconds + " | " + percntelapsed + "%", Color.CYAN);
AddVerticalLine(showVerticalCycleEndCvol and test, "Lngth " + cyclelength[1] + " CVol " + prevcumulativehigh + " B/S " + Round((cb[1] / vc[1]) * 100, 0) + "/" + +Round((cs[1] / vc[1]) * 100, 0) + "%", Color.GRAY, Curve.FIRM);
#Labels
input showCycleLengthLabel = yes;
input showPrevCycleLengthLabel = yes;
input showCycleAvgLengthLabel = no;
input showTotalCyclesLabel = no;
input showSecsElapsedLabel = yes;
input showCumulativeVolLabel = yes;
input showBuyStrLabel = yes;
input showSellStrLabel = yes;
input showPrevCVolLabel = yes;
input showRelToPrevCVolLabel = yes;
input showProjectedVolLabel = yes;
input showProjMultiplierLabel = no;
AddLabel(showCycleLengthLabel, "Cycle Length: " + cyclelength[1], Color.GRAY);
AddLabel(showPrevCycleLengthLabel, "Prev Cycle Length: " + cyclehigh, Color.GRAY);
AddLabel(showCycleAvgLengthLabel, "Cycle Avg Length: " + cycleavg, Color.GRAY);
AddLabel(showTotalCyclesLabel, "Cycles Since Start: " + min, Color.GRAY);
AddLabel(showSecsElapsedLabel, "Secs " + secondselapsed + "/" + aggregationInSeconds + " " + percntelapsed + "%", Color.YELLOW);
AddLabel(showCumulativeVolLabel, "CVol: " + vc, Color.WHITE);
AddLabel(1, "BuyStr: " + Round(cb, 0) + " / " + Round((cb / vc) * 100, 2) + "%", Color.GREEN);
AddLabel(1, "SellStr: " + Round(cs, 0) + " / " + Round((cs / vc) * 100, 2) + "%", Color.RED);
AddLabel(showPrevCVolLabel, "Prev CVol: " + prevcumulativehigh, Color.WHITE);
AddLabel(showRelToPrevCVolLabel, "x" + Round(vc / prevcumulativehigh, 2) + " Prev CVol", Color.WHITE);
AddLabel(showProjectedVolLabel and toggleProjection == toggleProjection.enabled, "Proj Vol: " + Round(projection, 0), Color.DARK_ORANGE);
AddLabel(showProjMultiplierLabel and toggleProjection == toggleProjection.enabled, "Proj Multiplier x" + Round(multiplier, 2), Color.DARK_ORANGE);
CVol.SetDefaultColor(Color.DARK_GRAY);
CVol.AssignValueColor(if toggleProjection == toggleProjection.disabled and Pos then Color.GREEN else if Neg then Color.RED else Color.GRAY);
CVol.SetLineWeight(2);
prevchigh.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
prevchigh.SetDefaultColor(Color.YELLOW);
projection.SetDefaultColor(Color.DARK_ORANGE);
projection.AssignValueColor(if Pos then Color.GREEN else if Neg then Color.RED else Color.GRAY);
projection.SetLineWeight(2);
cvolpassprevsig.SetPaintingStrategy(PaintingStrategy.POINTS);
cvolpassprevsig.SetLineWeight(3);
AddCloud(if showCumulativeStr then cbuy else NA, csell, Color.GREEN, Color.RED);
AddCloud(if showCumulativeStrDiff then bsdiff else NA, prevchigh, Color.GREEN, Color.RED);
AddCloud(projection, CVol, CreateColor(0, 100, 200), CreateColor(0, 100, 200));
AddCloud(CVol, 0, Color.DARK_GRAY, Color.DARK_GRAY);
AddCloud(CVol, prevchigh, Color.YELLOW, Color.BLACK);