//THANKS GSPIKER!!! var raw_K = null; 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; if(getBarState() == BARSTATE_ALLBARS){ raw_K = new StochStudy(nLength, 1 , 1); } var sum_K = 0.0; var sum_D = 0.0; var wma_K = 0.0; var wma_D = 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 i; var k; for(i=0;i < nSmoothingD; i++){ var ncount = 0; sum_K = 0.0; for(k=i; k < (i+nSmoothingK); k++){ sum_K += KVals[k] * (nSmoothingK - ncount); ncount++; } sum_K /= (nSmoothingK * (nSmoothingK + 1) /2); if(i == 0) wma_K = sum_K; sum_D += sum_K * (nSmoothingD -i); } wma_D = sum_D / (nSmoothingD * (nSmoothingD + 1) / 2); return new Array(wma_K , wma_D); }