Hi, Im trying to replicate tradingview VMA indicator:
Here is the link : https://www.tradingview.com/script/6...rage-LazyBear/
Vma value on my study is empty :
Note : I think problems starts from line 136 function fxs.
Here is my complete code :
var fpArray = new Array();
function preMain()
{
setPlotType(PLOTTYPE_INSTANTCOLORLINE);
setPriceStudy(true);
setStudyTitle("VMA");
setCursorLabelName("VMA", 0);
setDefaultBarThickness(2, 0);
var x=0;
fpArray[x] = new FunctionParameter("gNbrLength", FunctionParameter.NUMBER);
with(fpArray[x++])
{
setName("Number of Length");
setLowerLimit(1);
setDefault(10);
}
fpArray[x] = new FunctionParameter("gColorUp", FunctionParameter.COLOR);
with(fpArray[x++])
{
setName("Up Color");
setDefault(Color.lime);
}
fpArray[x] = new FunctionParameter("gColorDn", FunctionParameter.COLOR);
with (fpArray[x++])
{
setName("Down Color");
setDefault(Color.red);
}
fpArray[x] = new FunctionParameter("gColorFlat", FunctionParameter.COLOR);
with (fpArray[x++])
{
setName("Flat Color");
setDefault(Color.blue);
}
}
var bMainInit = false;
var xSrc = null;
var xK = null;
var xPdm = null;
var xMdm = null;
var xPdms = null;
var xMdms = null;
var xS = null;
var xPdi = null;
var xMdi = null;
var xPdiS = null;
var xMdiS = null;
var xD = null;
var xS1 = null;
var xIS = null;
var xHhv = null;
var xLlv = null;
var xD1 = null;
var xVi = null;
var xVMA = null;
function main(gNbrLength, gColorFlat,gColorUp, gColorDn)
{
if(!bMainInit)
{
xK = efsInternal("fK",gNbrLength);
xSrc = close();
xPdm = efsInternal("fPDM",xSrc);
xMdm = efsInternal("fMdm",xSrc);
xPdms = efsInternal("fPdms",xK, xPdms, xPdm);
xMdms = efsInternal("fMdms",xK, xMdms, xMdm);
xS = efsInternal("fxs",xPdms, xMdms);
/*xPdi = efsInternal("fPdi",xPdms, xS);
xMdi = efsInternal("fMdi",xMdms, xS);
xPdiS = efsInternal("fPdiS",xK, xPdiS, xPdi);
xMdiS = efsInternal("fMdiS",xK, xMdiS, xMdi);
xD = efsInternal("fD",xPdiS, xMdiS);
xS1 = efsInternal("fS1",xPdiS, xMdiS);
xIS = efsInternal("fIS",xK, xIS, xD, xS1);
xHhv = efsInternal("fHhv",xIS, gNbrLength);
xLlv = efsInternal("fLlv",xIS, gNbrLength);
xD1 = efsInternal("fD1",xHhv, xLlv);
xVi = efsInternal("fVi",xIS, xLlv, xD1);
xVMA = efsInternal("fVMA",xK, xVi, xVMA, xSrc);
//*/
bMainInit = true;
}
return xS.getValue(0);
}
function fVMA(xK, xVi, xVMA, xSrc)
{
if (xVMA == null){
return ((1 - xK.getValue(0)*xVi.getValue(0)) * 0 + xK.getValue(0)*xVi.getValue(0) * xSrc.getValue(0));
} else {
return ((1 - xK.getValue(0)*xVi.getValue(0)) * xVMA.getValue(-1) + xK.getValue(0)*xVi.getValue(0) * xSrc.getValue(0));
}
}
function fK(gNbrLength)
{
return 1.0/gNbrLength;
}
function fPDM(xSrc)
{
return Math.max((xSrc.getValue(0) - xSrc.getValue(-1)), 0);
}
function fMdm(xSrc)
{
return Math.max((xSrc.getValue(-1) - xSrc.getValue(0)), 0);
}
function fPdms(xK, xPdms, xPdm)
{
if (xPdms == null){
return ((1 - xK.getValue(0)) * 0 + xK.getValue(0)*xPdm.getValue(0));
} else{
return ((1 - xK.getValue(0)) * xPdms.getValue(-1) + xK.getValue(0)*xPdm.getValue(0));
}
}
function fMdms(xK, xMdms, xMdm)
{
if (xPdms == null){
return ((1 - xK.getValue(0)) * 0 + xK.getValue(0)*xMdm.getValue(0));
} else {
return ((1 - xK.getValue(0)) * xMdms.getValue(-1) + xK.getValue(0)*xMdm.getValue(0));
}
}
function fxs(xPdms, xMdms)
{
return xPdms.getValue(0) + xMdms.getValue(0);
}
function fPdi(xPdms, xS)
{
if (xPdms.getValue(0) == 0 && xS.getValue(0)) {
return 0;
} else {
return xPdms.getValue(0)/xS.getValue(0);
}
}
function fMdi(xMdms, xS)
{
return xMdms.getValue(0) / xS.getValue(0);
}
function fPdiS(xK, xPdiS, xPdi)
{
if (xPdiS == null){
return ((1 - xK.getValue(0)) * 0 + xK.getValue(0)*xPdi.getValue(0));
} else {
return ((1 - xK.getValue(0)) * xPdiS.getValue(-1) + xK.getValue(0)*xPdi.getValue(0));
}
}
function fMdiS(xK, xMdiS, xMdi)
{
if (xMdiS == null){
return ((1 - xK.getValue(0)) * 0 + xK.getValue(0)*xMdi.getValue(0));
} else {
return ((1 - xK.getValue(0)) * xMdiS.getValue(-1) + xK.getValue(0)*xMdi.getValue(0));
}
}
function fD(xPdiS, xMdiS)
{
return Math.abs(xPdiS.getValue(0) - xMdiS.getValue(0));
}
function fS1(xPdiS, xMdiS)
{
return xPdiS.getValue(0) + xMdiS.getValue(0);
}
function fIS(xK, xIS, xD, xS1)
{
if (xIS == null){
return ((1 - xK.getValue(0)) * 0 + xK.getValue(0)*xD.getValue(0)/xS1.getValue(0));
} else {
return ((1 - xK.getValue(0)) * xIS.getValue(-1) + xK.getValue(0)*xD.getValue(0)/xS1.getValue(0));
}
}
function fHhv(xIS, gNbrLength)
{
return highest(gNbrLength, xIS);
}
function fLlv(xIS, gNbrLength)
{
return lowest(gNbrLength, xIS);
}
function fD1(xHhv, xLlv)
{
return xHhv.getValue(0) - xLlv.getValue(0);
}
function fVi(xIS, xLlv, xD1)
{
return (xIS.getValue(0) - xLlv.getValue(0))/xD1.getValue(0);
}
Here is the link : https://www.tradingview.com/script/6...rage-LazyBear/
Vma value on my study is empty :
Note : I think problems starts from line 136 function fxs.
Here is my complete code :
var fpArray = new Array();
function preMain()
{
setPlotType(PLOTTYPE_INSTANTCOLORLINE);
setPriceStudy(true);
setStudyTitle("VMA");
setCursorLabelName("VMA", 0);
setDefaultBarThickness(2, 0);
var x=0;
fpArray[x] = new FunctionParameter("gNbrLength", FunctionParameter.NUMBER);
with(fpArray[x++])
{
setName("Number of Length");
setLowerLimit(1);
setDefault(10);
}
fpArray[x] = new FunctionParameter("gColorUp", FunctionParameter.COLOR);
with(fpArray[x++])
{
setName("Up Color");
setDefault(Color.lime);
}
fpArray[x] = new FunctionParameter("gColorDn", FunctionParameter.COLOR);
with (fpArray[x++])
{
setName("Down Color");
setDefault(Color.red);
}
fpArray[x] = new FunctionParameter("gColorFlat", FunctionParameter.COLOR);
with (fpArray[x++])
{
setName("Flat Color");
setDefault(Color.blue);
}
}
var bMainInit = false;
var xSrc = null;
var xK = null;
var xPdm = null;
var xMdm = null;
var xPdms = null;
var xMdms = null;
var xS = null;
var xPdi = null;
var xMdi = null;
var xPdiS = null;
var xMdiS = null;
var xD = null;
var xS1 = null;
var xIS = null;
var xHhv = null;
var xLlv = null;
var xD1 = null;
var xVi = null;
var xVMA = null;
function main(gNbrLength, gColorFlat,gColorUp, gColorDn)
{
if(!bMainInit)
{
xK = efsInternal("fK",gNbrLength);
xSrc = close();
xPdm = efsInternal("fPDM",xSrc);
xMdm = efsInternal("fMdm",xSrc);
xPdms = efsInternal("fPdms",xK, xPdms, xPdm);
xMdms = efsInternal("fMdms",xK, xMdms, xMdm);
xS = efsInternal("fxs",xPdms, xMdms);
/*xPdi = efsInternal("fPdi",xPdms, xS);
xMdi = efsInternal("fMdi",xMdms, xS);
xPdiS = efsInternal("fPdiS",xK, xPdiS, xPdi);
xMdiS = efsInternal("fMdiS",xK, xMdiS, xMdi);
xD = efsInternal("fD",xPdiS, xMdiS);
xS1 = efsInternal("fS1",xPdiS, xMdiS);
xIS = efsInternal("fIS",xK, xIS, xD, xS1);
xHhv = efsInternal("fHhv",xIS, gNbrLength);
xLlv = efsInternal("fLlv",xIS, gNbrLength);
xD1 = efsInternal("fD1",xHhv, xLlv);
xVi = efsInternal("fVi",xIS, xLlv, xD1);
xVMA = efsInternal("fVMA",xK, xVi, xVMA, xSrc);
//*/
bMainInit = true;
}
return xS.getValue(0);
}
function fVMA(xK, xVi, xVMA, xSrc)
{
if (xVMA == null){
return ((1 - xK.getValue(0)*xVi.getValue(0)) * 0 + xK.getValue(0)*xVi.getValue(0) * xSrc.getValue(0));
} else {
return ((1 - xK.getValue(0)*xVi.getValue(0)) * xVMA.getValue(-1) + xK.getValue(0)*xVi.getValue(0) * xSrc.getValue(0));
}
}
function fK(gNbrLength)
{
return 1.0/gNbrLength;
}
function fPDM(xSrc)
{
return Math.max((xSrc.getValue(0) - xSrc.getValue(-1)), 0);
}
function fMdm(xSrc)
{
return Math.max((xSrc.getValue(-1) - xSrc.getValue(0)), 0);
}
function fPdms(xK, xPdms, xPdm)
{
if (xPdms == null){
return ((1 - xK.getValue(0)) * 0 + xK.getValue(0)*xPdm.getValue(0));
} else{
return ((1 - xK.getValue(0)) * xPdms.getValue(-1) + xK.getValue(0)*xPdm.getValue(0));
}
}
function fMdms(xK, xMdms, xMdm)
{
if (xPdms == null){
return ((1 - xK.getValue(0)) * 0 + xK.getValue(0)*xMdm.getValue(0));
} else {
return ((1 - xK.getValue(0)) * xMdms.getValue(-1) + xK.getValue(0)*xMdm.getValue(0));
}
}
function fxs(xPdms, xMdms)
{
return xPdms.getValue(0) + xMdms.getValue(0);
}
function fPdi(xPdms, xS)
{
if (xPdms.getValue(0) == 0 && xS.getValue(0)) {
return 0;
} else {
return xPdms.getValue(0)/xS.getValue(0);
}
}
function fMdi(xMdms, xS)
{
return xMdms.getValue(0) / xS.getValue(0);
}
function fPdiS(xK, xPdiS, xPdi)
{
if (xPdiS == null){
return ((1 - xK.getValue(0)) * 0 + xK.getValue(0)*xPdi.getValue(0));
} else {
return ((1 - xK.getValue(0)) * xPdiS.getValue(-1) + xK.getValue(0)*xPdi.getValue(0));
}
}
function fMdiS(xK, xMdiS, xMdi)
{
if (xMdiS == null){
return ((1 - xK.getValue(0)) * 0 + xK.getValue(0)*xMdi.getValue(0));
} else {
return ((1 - xK.getValue(0)) * xMdiS.getValue(-1) + xK.getValue(0)*xMdi.getValue(0));
}
}
function fD(xPdiS, xMdiS)
{
return Math.abs(xPdiS.getValue(0) - xMdiS.getValue(0));
}
function fS1(xPdiS, xMdiS)
{
return xPdiS.getValue(0) + xMdiS.getValue(0);
}
function fIS(xK, xIS, xD, xS1)
{
if (xIS == null){
return ((1 - xK.getValue(0)) * 0 + xK.getValue(0)*xD.getValue(0)/xS1.getValue(0));
} else {
return ((1 - xK.getValue(0)) * xIS.getValue(-1) + xK.getValue(0)*xD.getValue(0)/xS1.getValue(0));
}
}
function fHhv(xIS, gNbrLength)
{
return highest(gNbrLength, xIS);
}
function fLlv(xIS, gNbrLength)
{
return lowest(gNbrLength, xIS);
}
function fD1(xHhv, xLlv)
{
return xHhv.getValue(0) - xLlv.getValue(0);
}
function fVi(xIS, xLlv, xD1)
{
return (xIS.getValue(0) - xLlv.getValue(0))/xD1.getValue(0);
}
Comment