#[email protected]
#Attempt at Volume Projection and Buy/Sell Strength on low TICK charts
#Thanks to YungTraderFromMontana and evanevans for the ideas
#v9.6.2020
# Positioning --------------------------------------------------------
input usetimefilter = Yes;
input rthopen = 0932;
input rthclose = 1555;
def RTH = if usetimefilter == yes then if SecondsFromTime(rthopen) >= 0 and
SecondsTillTime(rthclose) >= 0
then 1
else 0 else 1;
input StopLoss = Yes;
input stoplosspercent = .08;
input fastLength = 9;
input slowLength = 26;
input MACDLength = 3;
input averageType = AverageType.EXPONENTIAL;
def Diff = MACD(fastLength, slowLength, MACDLength, averageType).Diff;
def DiffCondition = (if Diff >= 0 then if Diff > Diff[1] then 1 else 2 else if Diff < Diff[1] then 3 else 4);
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];
######################
def cvolpassprevsig = if cvolpassprev then vc else NA;
#def CVol = vc;
def prevchigh = prevcumulativehigh;
def cycleavg = Round(TotalSum(if test and !test[1] then ch[1] else 0) / min, 1);
def cyclehigh = ch;
def cyclelength = cl;
def projection;
switch (toggleProjection){
case enabled:
projection = if (projectioncalc >= highestprevchigh) and (percntelapsed < elapsedPercentSmoothValue) then projectioncalc / elapsedProjectionDivisor else projectioncalc;
case disabled:
projection = NA;
}
#ycleavg.Hide();
#cyclehigh.Hide();
#cyclelength.Hide();
def 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);
def cbuy = cb;
#cbuy.SetHiding(!showCumulativeStr);
def 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;
}
# PrevDayClose -------------------------------------------------------
input stopbuyPrevClose = No;
input aggregationPeriod = AggregationPeriod.DAY;
def showOnlyLastPeriod = yes;
def prevPrice = open(period = aggregationPeriod)[-1];
def price3 = open(period = aggregationPeriod);
def DailyOpen = if showOnlyLastPeriod and !IsNaN(prevPrice) then Double.NaN else price3;
def length3 = 1;
def displace3 = -1;
def PrevDayClose;
if showOnlyLastPeriod and !IsNaN(close(period = aggregationPeriod)[-1]) {
PrevDayClose = Double.NaN;
} else {
PrevDayClose = Highest(close(period = aggregationPeriod)[-displace3], length3);
}
def pclose = if stopbuyPrevClose == yes then if PrevDayClose < close then 1 else 0 else 1;
def BuyStr = Round(cb, 0);
def SellStr = Round(cs, 0);
#def BuyStr = (cb / vc);
#def SellStr = (cs / vc);
def VDiff = Round(((BuyStr - SellStr) / BuyStr) * 100, 2);
def entryPrice = EntryPrice();
def pstoploss = entryPrice * (stoplosspercent / 100);
def pStopSell = entryPrice - pstoploss > close;
def StopSell = if StopLoss == 1 then pStopSell else 0;
def EOD = RTH == 1 > SecondsTillTime(2000);
def BuyorderPrice = open[-1] + ((close[-1] - open[-1]) / 2);
def SellorderPrice = open[-1] - ((open[-1] - close[-1]) / 2);
# Buy_Signals --------------------------------------------------------
def BuySignal1 = VDiff>2;
def SellSignal1 = VDiff<-2;
def BuySignal2 = bsdiffaboveztest and bsdiff > bsdiff[1];
def SellSignal2 = !bsdiffaboveztest and bsdiff < bsdiff[1];
def BuySignal = BuySignal1 and BuySignal2 and Diff > Diff[1];
def SellSignal = SellSignal1 and SellSignal2 and Diff < Diff[1] and DiffCondition == 3;
AddOrder(OrderType.BUY_TO_OPEN, RTH and BuySignal[-1], price = buyorderprice, tickcolor = GetColor(0), arrowcolor = GetColor(0), name = "CDS_LE_G");
AddOrder(OrderType.SELL_TO_CLOSE, SellSignal[-1], price = sellorderprice, tickcolor = GetColor(1), arrowcolor = GetColor(1), name = "CDS_SE");
AddOrder(OrderType.SELL_TO_CLOSE, StopSell[-1], price = sellorderprice, tickcolor = GetColor(7), arrowcolor = GetColor(7), name = "CDS_SL");
AddOrder(OrderType.SELL_TO_CLOSE, EOD, price = sellorderprice, tickcolor = GetColor(9), arrowcolor = GetColor(9), name = "CDS_EOD");
#input showBreakoutSignals = no;
def begin = rthopen;
# Entry Calculations. Note: Only parses on a Strategy Chart
def entry = EntryPrice();
input showSignal = no;
plot Buy_Signal = RTH and BuySignal[0];
Buy_Signal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
Buy_Signal.SetDefaultColor(Color.CYAN);
Buy_Signal.SetLineWeight(4);
Buy_Signal.HideTitle();
Buy_Signal.SetHiding(!showSignal);
plot Sell_Signal = RTH and SellSignal[0];
Sell_Signal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
Sell_Signal.SetDefaultColor(Color.MAGENTA);
Sell_Signal.SetLineWeight(4);
Sell_Signal.HideTitle();
Sell_Signal.SetHiding(!showSignal);
#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);
AddChartBubble(!IsNaN(close) and IsNaN(close[-1]), high,
if RTH and VDiff < 0 then
" * S E L L _ A L E R T * " + " * Strenght " + VDiff + "% " else
if VDiff > 0 then
" * B U Y _ A L E R T * " + " * Strenght " + VDiff + "% " else
" * H O L D * ",
if VDiff+10 >0 then Color.GREEN else
if VDiff>0 then Color.DARK_GREEN else
if VDiff-10<0 then Color.RED else
if VDiff<0 then Color.DARK_RED else
Color.YELLOW);
#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);