/*[[ Name := Keltner Channel Author := mteemant Notes := Basic Keltner Channel - Separate Window := No First Color := Yellow First Draw Type := Line First Symbol := 217 Use Second Data := Yes Second Color := Yellow Second Draw Type := Line Second Symbol := 218 ]]*/ input: ma_period(12), ma_type(MODE_SMA); Var: shift(0); Var: upper_Keltner(0), loopbegin(0), lower_Keltner(0); Var: avg_3_price(0), diff_price(0),K(0),first(True),; var: Current_EMA(0), Prev_EMA(0), Diff_EMA(0), prev_EMA_Diff(0); Variables : bar(0), prevbars(0), start(0), cs(0), prevcs(0),commodt(0); var: cum_diff_price(0),cum_price(0),sh(0),cnt(0); SetLoopCount(0); If Bars < prevbars Or Bars-prevbars>1 Then first = True; prevbars = Bars; If first Then Begin // loopbegin prevent couning of counted bars exclude current loopbegin = Bars-ma_period-1; If ma_type = 1 then Begin // exponential MA initialization loopbegin = Bars-3; // smconst = 2 / (1+ma_period); SetIndexValue(Bars-1,Close[Bars-1]); End; If loopbegin < 0 Then Exit; // not enough bars for counting first = False; End; loopbegin = loopbegin+1; // current bar is to be recounted too //cum_price = 0; For shift = loopbegin Downto 0 Begin print("shift: = ", shift); //cum_price = 0; avg_3_price = 0; diff_price = 0; For cnt = 0 To ma_period-1 Begin avg_3_price = avg_3_price + (H[shift+cnt]+L[shift+cnt]+C[shift+cnt])/3; diff_price = diff_price + (H[shift+cnt]-L[shift+cnt]); End; Current_EMA = avg_3_price/ma_period; Diff_EMA = diff_price/ma_period; print("cum_price: " ,cum_price); print("Current_EMA: ", Current_EMA); print("Diff_EMA: ", Diff_EMA); upper_Keltner = Current_EMA + Diff_EMA; lower_Keltner = Current_EMA - Diff_EMA; //Formula#2: //NAME: Upper Keltner Band //FORMULA: MAvg((H+L+C)/3, Period) + MAvg((H-L), Period) //Formula#3: //NAME: Lower Keltner Band //FORMULA: MAvg((H+L+C)/3, Period) - MAvg((H-L), Period) print("upper_Keltner: ", upper_Keltner); print("lower_Keltner: ", lower_Keltner); SetIndexValue(shift, upper_Keltner); SetIndexValue2(shift, lower_Keltner); loopbegin = loopbegin-1; End;