Author message: https://www.tradingview.com/v/bvYZ1CdF/
CODE:
CSS:
#// https://www.tradingview.com/v/bvYZ1CdF/
#// © kocurekc
#// Binary Entropy Function
#study("Bernoulli - V2", overlay=false, shorttitle="Bernoulli", precision=2)
#//so many ways to measure the same thing...except acceleration, that one is neat
# Converted by [email protected] - 04/2023
declare lower;
#percentrank(Source, length) =>
script percentrank {
input src = close;
input len = 10;
def percentRankCount = fold i = 1 to len + 1 with count=0 do
if src[i] <= src then count + 1 else count;
def percentRank = (percentRankCount / len) * 100;
plot return = percentRank;
}
#measure(_type, _src, _lbl) =>
script measure {
input _type = "Value";
input _src = close;
input _lbl = 20;
def result;#
def Vaa = Log(_src / If(IsNaN(_src[1]), _src, _src[1]));
def Vam = Average(Vaa, _lbl);
def srcChange = _src - _src[1];
def srcMom = _src - _src[_lbl];
if _type == "Value" { # Average value over lookback
result = _src;
} else
if _type == "SMA" { # Average value over lookback
result = Average(_src, _lbl);
} else
if _type == "Change" { # Average difference (1-bar) over lookback
result = Average(srcChange, _lbl);
} else
if _type == "Momentum" { # Average difference (lookback bars) over lookback
result = Average(srcMom, _lbl);
} else
if _type == "Acceleration" { # Average Change of the Change over lookback, average acceleration
result = Average(srcMom - srcMom[_lbl], _lbl);
} else
if _type == "Contribution" { # Today ratiod to total over lookback, how much did this bar contirbute to the total
result = _src / Sum(_src, _lbl);
} else
if _type == "%-Change" { # Percent Change over lookback
result = Average(srcChange / If(IsNaN(_src[_lbl]), _src, _src[_lbl]), _lbl);
} else
if _type == "Volatility" { # Natural Log Volatility adjusted
result = Sqrt(Sum(Power(Vaa - Vam, 2), _lbl) / _lbl);
} else {
result = result[1];
}
plot out = result;
}
#/Returns the normal distribution CDF
#n_CDF(_src,_len,_smo) =>
script n_CDF {
input _src = close;
input _len = 20;
input _smo = 10;
def x_bar = Average(_src, _len);
def s = Sqrt(Sum(Power(_src - x_bar, 2), _len) / (_len - 1));
def z_bar = Max(Min((_src - x_bar) / s, 5.55), -5.55);
def CDF = Average((1 / (1 + Power((1 - z_bar / 5.555), (1 / 0.1186)))), _smo);
plot out = CDF;
}
#//Returns the Burn, Bernoulli calc
#bern(_src,_avg,_len,_xn) =>
script bern {
input _src = close;
input _avg = 88;
input _len = 20;
input _xn = no;
def _r2 = Min(Max(percentrank(_src, _avg) / 100, 0.001), 0.999);
def burn = Sum(((If(_xn, -1, 1)) * _r2 * Log(_r2) / Log(2)) - (1 - _r2) * Log(1 - _r2) / Log(2), _len);
plot out = burn;
}
input barColor = yes;
input src = close;
input EntropyLength = 22; # "Entropy Length"
input TradeBand = 0.67; # "Trade Band - confirmation"
input AveragingLength = 88; # "Averaging Length"
input MeasurementType = {"Value", "SMA", "Change", "%-Change", "Momentum", "Acceleration", default "Contribution", "Volatility"};
input PercentRankLimit = 5; # "Percent Rank Limit"
input IncludeSource = yes; # "Include Source"
input IncludeVolume = yes; # "Include Volume"
input PrintBands = yes; # "Print Bands"
input ExtraHighlighting = no; # "Extra Highlighting"
input BernoulliPurest = no; # "Bernoulli Purest"
input ProbabilitySmoother = 3; # "Probability Smoother"
def na = Double.NaN;
def range = TradeBand;
def vPR = PercentRankLimit;
def bc = IncludeSource;
def vc = IncludeVolume;
def pb = PrintBands;
def xc = ExtraHighlighting;
def xn = BernoulliPurest;
def smo = ProbabilitySmoother;
#//Entropy Calculation, Bernoulli Process source (close) or for Volume or both
def cr = measure(MeasurementType, src, EntropyLength);
def vr = measure(MeasurementType, Log(volume), EntropyLength);
def cr2 = if xn then n_CDF(cr, EntropyLength, smo) else cr;
def vr2 = if xn then n_CDF(vr, EntropyLength, smo) else vr;
def infoc = if bc then bern(cr2, AveragingLength, EntropyLength, xn) else 0;
def infov = if vc then bern(vr2, AveragingLength, EntropyLength, xn) else 0;
def info2 = if (bc and vc) then (infoc - infov) else
if xn then (0 + (infoc + infov) / (- EntropyLength + 1)) else (infoc + infov);
def hvp = percentrank(info2, AveragingLength);
#//Plotting
def col1 = if info2>(range) then 1 else if info2<(-range) then -1 else 0;
def col2 = if info2>(range) and hvp>(100-vPR) then 2 else
if info2<(-range) and hvp<vPR then -2 else
if info2>(range) then 1 else if info2<(-range) then -1 else 0;
plot priceLine = if (pb and bc and vc and xn) then na else
if (pb and bc and vc) then infoc else na; # "price"
plot volumeLine = if (pb and bc and vc and xn) then na else
if (pb and bc and vc) then -infov else na; # "volume"
priceLine.SetDefaultColor(Color.CYAN);
volumeLine.SetDefaultColor(Color.DARK_ORANGE);
priceLine.SetLineWeight(2);
volumeLine.SetLineWeight(2);
plot Bernoulli = info2; # "Bernoulli"
Bernoulli.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
Bernoulli.AssignValueColor(if xc then
if col2==2 then Color.GREEN else
if col2==1 then Color.DARK_GREEN else
if col2==-1 then Color.DARK_RED else
if col2==-2 then Color.RED else Color.GRAY else
if col1>0 then Color.GREEN else
if col1<0 then Color.RED else Color.GRAY);
#/Percent Rank and ploting
plot dnAlert = if (hvp>(100-vPR) and (AbsValue(info2) > AbsValue(range))) then info2 else na;
plot upAlert = if hvp<vPR and (AbsValue(info2) > AbsValue(range)) then info2 else na;
dnAlert.SetPaintingStrategy(PaintingStrategy.SQUARES);
upAlert.SetPaintingStrategy(PaintingStrategy.SQUARES);
dnAlert.SetDefaultColor(Color.RED);
upAlert.SetDefaultColor(Color.GREEN);
def Signal = Average(info2, 3);
def nColor = if signal>0 and signal>=range then 1 else
if signal<0 and signal<=-range then -1 else 0;
AssignPriceColor(if !BarColor then Color.CURRENT else
if nColor>0 then Color.GREEN else
if nColor<0 then Color.RED else Color.GRAY);
#--- END of Code
Last edited by a moderator: