function preMain() { setDefaultBarFgColor(Color.yellow , 0); // %K setDefaultBarFgColor(Color.cyan , 1); // %D } function main(inputLength, inputSmoothingK, inputSmoothingD) { if(inputLength == null) inputLength = 15; if(inputSmoothingK == null) inputSmoothingK = 3; if(inputSmoothingD == null) inputSmoothingD = 5; var nOffset = 0; var nLength = inputLength; var nSmoothingK = inputSmoothingK; var nSmoothingD = inputSmoothingD; var d_wma_k = 0.0; var d_wma_d = 0.0; d_wma_k = GetK(nLength , nSmoothingK , nOffset); d_wma_d = GetD(nLength , nSmoothingK , nSmoothingD); return new Array(d_wma_k , d_wma_d); } function GetK(nLength , nSmoothingK , nOffset){ var percentK; var close = 0.0; var ll, hh; var i; var sum = 0; var nDO; for(i = 0; i < nSmoothingK; i++) { nDO = -i + nOffset; close = getValue("Close", nDO); if(close == null) { return null; } ll = callFunction("/library/StochasticLL.efs", "main" , nDO, -nLength); if(ll == null) { return null; } hh = callFunction("/library/StochasticHH.efs", "main" , nDO, -nLength); if(hh == null) { return null; } percentK = ((close - ll) / (hh - ll)) * 100; sum += percentK * (nSmoothingK - i); } sum /= (nSmoothingK * (nSmoothingK + 1) / 2); return sum; } function GetD(nLength , nSmoothingK , nSmoothingD){ var i; var ret = 0.0 var sum = 0.0; for(i = 0; i < nSmoothingD; i++) { ret = GetK(nLength , nSmoothingK , -i); if(ret == null) return -1.0; sum += ret * (nSmoothingD - i); } sum /= (nSmoothingD * (nSmoothingD + 1) / 2); return sum; }