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 , 0); d_wma_D = GetD(nLength , nSmoothingK , nSmoothingD); return new Array(d_wma_K , d_wma_D); } function GetK(nLength , nSmoothingK , nOffset){ var raw_K = new StochStudy(nLength, 1 , 1); var sum_K = 0.0; var KVals; // Returns an array of raw K's KVals = raw_K.getValue(StochStudy.FAST , nOffset , -nSmoothingK); if (KVals == null) return; for(i=0; i < nSmoothingK; i++){ sum_K += KVals[i] * (nSmoothingK - i); } sum_K /= (nSmoothingK * (nSmoothingK + 1) /2); return sum_K; } function GetD(nLength , nSmoothingK , nSmoothingD){ var ema_K = 0.0; var sum_D = 0.0; var i; for(i=0;i < nSmoothingD; i++){ ema_K = GetK(nLength , nSmoothingK , -i); if(ema_K == null) return; sum_D += ema_K * (nSmoothingD -i); } sum_D /= (nSmoothingD * (nSmoothingD + 1) / 2); return sum_D; }