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); if (d_wma_K == null || d_wma_D == null) return; 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 null; 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 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 ,0 , -(nSmoothingK+nSmoothingD+1)); if (KVals == null) return null; var ema_K = 0.0; var sum_D = 0.0; var sum_K = 0.0; var i; var k; for(i=0;i < nSmoothingD; i++){ // ema_K = GetK(nLength , nSmoothingK , -i); var ncount = 0; for(k=i; k < (i+nSmoothingK); k++){ sum_K += KVals[k] * (nSmoothingK - ncount); ncount++; } sum_K /= (nSmoothingK * (nSmoothingK + 1) /2); sum_D += sum_K * (nSmoothingD -i); } sum_D /= (nSmoothingD * (nSmoothingD + 1) / 2); return sum_D; }