/*[[ Name := Real RSI Author := (VG) Link := 4vg@mail.ru Notes := 2 methods - Notes := 1. Method = 0 - простое сглаживание Notes := 2. Method = 1 - экспоненциальное сглаживание. Notes := Has possibility to build 2 lines with diff. periods in 1 window Notes := Levels are shifted by ShiftLev=1 (trouble with 0 values) Notes := RSI = 100 + ShiftLev - (100/(1+RS)); Notes := RS = UpC/DnC; Separate Window := Yes First Color := Blue First Draw Type := Line First Symbol := 217 Use Second Data := Yes Second Color := Red Second Draw Type := Line Second Symbol := 218 ]]*/ Inputs : IndPeriod1(9),IndPeriod2(14),Method(0),ShiftLev(1); Variables : shift(0), i(0), j(0), k(0), v1(0), dC(0), sum(0), loopbegin(0), first(True), prevbars(0); Variables : RSI(0),RS(0),RS1(0), UpC(0),DnC(0), prev_UpC(0),prev_DnC(0); Array: Ind[3](0); /* RSI = 100 - [ 100 / ( 1 + RS ) ] ; RS = AUx / Adx, - index х = period of RSI - AU - sum of + price deltas - AD - sum of - price deltas */ SetLoopCount(0); // initial checkings If IndPeriod1 < 1 Then Exit; // check for additional bars loading or total reloading //Comment("Real RSI - Best Regards from Vladislav Goshkov (VG) - 4vg@mail.ru"); If Bars < prevbars Or Bars-prevbars>1 Then first = True; prevbars = Bars; // loopbegin1 and loopbegin2 prevent couning of counted bars exclude current If first Then Begin loopbegin = Bars-Max(IndPeriod1,IndPeriod2)-1; if Method = 1 then { UpC=0; DnC=0; for i = 0 to IndPeriod1 { dC = Close[shift+i]-Close[shift+i+1]; if dC>0 then UpC += dC; if dC<0 then DnC += Abs(dC); }; prev_UpC=UpC; prev_DnC=DnC; }; first = False; // this block is to be evaluated once only End; For shift = loopbegin Downto 0 Begin //For shift = Bars-IndPeriod-1 Downto 0 Begin if IndPeriod1 <>0 then { if Method = 0 then { UpC=0; DnC=0; for i = 0 to IndPeriod1 { dC = Close[shift+i]-Close[shift+i+1]; if dC>0 then UpC += dC; if dC<0 then DnC += Abs(dC); }; }; if Method = 1 then { dC = Close[shift]-Close[shift+1]; if dC>0 then UpC = (prev_UpC*(IndPeriod1-1)+dC)/IndPeriod1; if dC<0 then DnC = (prev_DnC*(IndPeriod1-1)+Abs(dC))/IndPeriod1; }; RS = UpC/DnC; RSI = 100 + ShiftLev - (100/(1+RS)); SetIndexValue(shift,RSI); prev_UpC = UpC; prev_DnC = DnC; }; if IndPeriod2 <>0 then { if Method = 0 then { UpC=0; DnC=0; for i = 0 to IndPeriod2 { dC = Close[shift+i]-Close[shift+i+1]; if dC>0 then UpC += dC; if dC<0 then DnC += Abs(dC); }; }; if Method = 1 then { dC = Close[shift]-Close[shift+1]; if dC>0 then UpC = (prev_UpC*(IndPeriod2-1)+dC)/IndPeriod2; if dC<0 then DnC = (prev_DnC*(IndPeriod2-1)+Abs(dC))/IndPeriod2; }; RS = UpC/DnC; RSI = 100 + ShiftLev - (100/(1+RS)); SetIndexValue2(shift,RSI); prev_UpC = UpC; prev_DnC = DnC; }; loopbegin--; // prevent to previous bars recounting End;