#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © loxx
#indicator('Zero-line Volatility Quality Index (VQI) [Loxx]',
# converted by Sam4Cok@Samer800 - 11/2022
declare lower;
#calc_jma(_src, _length, _phase, _power) =>
script jma {
input _src = close;
input _length = 0;
input _power = 0;
def beta = 0.45 * (_length - 1) / (0.45 * (_length - 1) + 2);
def alpha = Power(beta, _power);
def e0;
def e1;
def e2;
def jma;
e0 = (1 - alpha) * _src + alpha * (e0[1]);
e1 = (_src - e0) * (1 - beta) + beta * (e1[1]);
e2 = (e0 - jma[1]) * Power(1 - alpha, 2) + Power(alpha, 2) * (e2[1]);
jma = CompoundValue(1, e2 + (jma[1]), _src) ;
plot return = jma;
}
#ma(type, src, len) =>
script ma {
input type = "SMA";
input src = close;
input len = 10;
input power = 2;
def sig;
sig = if type == "SMA" then SimpleMovingAvg(src, len) else
if type == "EMA" then ExpAverage(src, len) else
if type == "WMA" then WMA(src, len) else
if type == "JMA" then JMA(src, len, power) else
if type == "RMA" then WildersAverage(src, len) else SimpleMovingAvg(src, len);
plot return = sig;
}
input PriceSmoothing = 15;#(10, "Source Smoothing Period"
input atrPercentage = 7.5;#(7.5, "ATR Percentage %"
input studyStyle = {Default Trend, Oscillator};
input type = {default "WMA", "JMA", "EMA", "RMA", "SMA"};# "Smoothing Type"
input power = 2; # "Power"
input colorBars = yes; # "Color bars?"
input showSigs = yes;# "Show signals?"
def na = Double.NaN;
def bar = BarNumber();
def style = if studyStyle==studyStyle.Trend then 1 else 0;
def inpFilter = atrPercentage / 100;
def nATR = ATR(Length = PriceSmoothing);
def chigh = ma(type,high , PriceSmoothing, power);
def clow = ma(type,low , PriceSmoothing, power);
def cclose = ma(type,close , PriceSmoothing, power);
def copen = ma(type,open , PriceSmoothing, power);
def pclose = ma(type,close[1], PriceSmoothing, power);
def truerange = Max(cHigh,pClose)- Min(cLow,pClose);
def range = chigh - clow;
def vqi_raw;
if (range != 0 and trueRange!=0) {
vqi_raw = ((cClose-pClose)/trueRange + (cClose-cOpen)/range)*0.5;
} else {
vqi_raw = vqi_raw[1];
}
def vqi = AbsValue(Vqi_raw) * (cclose - pclose + cclose - copen) * 0.5;
def sumVqi = if inpFilter > 0 and bar > 0 then
if AbsValue(vqi-vqi[1]) < inpFilter * nATR then sumVqi[1] else vqi else vqi;
def valc = if (sumVqi > 0) then 1 else if (sumVqi < 0) then -1 else if bar>0 then valc[1] else 0;
def trend = if sumVqi > 0 then 1 else if sumVqi < 0 then -1 else 0;
plot VQIline = if style then sumVqi else vqi;
VQIline.AssignValueColor(if valc > 0 then Color.GREEN else
if valc < 0 then Color.RED else Color.GRAY);
VQIline.SetLineWeight(2);
plot mid = if IsNaN(close) then na else 0;
mid.SetDefaultColor(Color.GRAY);
mid.SetStyle(Curve.SHORT_DASH);
def crossUp = if trend != trend[1] and trend== 1 then sumVqi - 5* stDev(sumVqi,PriceSmoothing) else na;
def crossDn = if trend != trend[1] and trend==-1 then sumVqi + 5* stDev(sumVqi,PriceSmoothing) else na;
plot long = if showSigs then crossUp[-1] else na;
long.SetDefaultColor(Color.GREEN);
long.SetStyle(Curve.POINTS);
long.SetLineWeight(3);
plot short = if showSigs then crossDn[-1] else na;
short.SetDefaultColor(Color.RED);
short.SetStyle(Curve.POINTS);
short.SetLineWeight(3);
#---Bar Color
AssignPriceColor(if !colorbars then Color.CURRENT else
if valc > 0 then Color.GREEN else
if valc < 0 then color.RED else Color.GRAY);
#--- END CODE