g_KVals = new Array(); var g_counter = 0; 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 nLength = inputLength; var nSmoothingK = inputSmoothingK; var nSmoothingD = inputSmoothingD; var d_wma_k = 0.0; var d_wma_d = 0.0; var nBarState = getBarState(); d_wma_k = GetK(nLength , nSmoothingK ); g_KVals[g_counter] = d_wma_k; d_wma_d = GetD(nSmoothingD); g_counter++; return new Array(d_wma_k , d_wma_d); } function GetK(nLength , nSmoothingK){ var raw_K = new StochStudy(nLength, 1 , 1); var i; var sum_K = 0.0; var KVals; // Returns an array of raw K's KVals = raw_K.getValue(StochStudy.FAST , 0 , -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(nSmoothingD){ var i; var KVal; var sum_D = 0.0; var nWeight = nSmoothingD; var counter = g_counter; for(i= counter; i > counter - nSmoothingD; i--){ KVal = g_KVals[i]; if(KVal == null) return null; sum_D += KVal * (nWeight) ; nWeight--; } sum_D /= (nSmoothingD * (nSmoothingD + 1) / 2 ); return sum_D; }