For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

Calculating RSI mismatch with Ninjatrader/Excel



  • Hi All,

    I started to compare back test results in backtrader with ninjatrader and found that the RSI was different across the two platforms. I then used excel to calculate RSI and found that the ninjatrader results were much closer. Any thoughts?

    Here is how I have implemented RSI:

            self.rsi = bt.indicators.RSI_SMA(self.data.close, period=self.params.period)
    
        params = (
            ("period", 14),
            ("rsi_low", 30),
            ("rsi_high", 70),
        )
    
    
    
    Relative Strength Index																				
    																				
    Date	 Close	Change	Gain	Loss 	Avg Gain	Avg Loss	RS	14-Day RSI		Backtrader										
    20170110 0000	10.47441656									nan										
    20170110 0015	10.41867814	-0.06		0.06						 nan										
    20170110 0030	10.45	0.03	0.03							 nan										
    20170110 0045	10.475	0.03	0.03							 nan										
    20170110 0100	10.42873472	-0.05		0.05						 nan										
    20170110 0115	10.48	0.05	0.05							 nan										
    20170110 0130	10.41867859	-0.06		0.06						 nan										
    20170110 0145	10.44251791	0.02	0.02							 nan										
    20170110 0200	10.43046593	-0.01		0.01						 nan										
    20170110 0215	10.42365979	-0.01		0.01						 nan										
    20170110 0230	10.4397672	0.02	0.02							 nan										
    20170110 0245	10.45	0.01	0.01							 nan										
    20170110 0300	10.39934476	-0.05		0.05						 nan										
    20170110 0315	10.48598604	0.09	0.09							 nan										
    20170110 0330	10.4225	-0.06		0.06	0.02	0.02	0.82	45.20		45.18518519										
    20170110 0345	10.45	0.03	0.03		0.02	0.02	0.92	48.04		53.0214425										
    20170110 0400	10.451	0.00	0.00		0.02	0.02	0.93	48.15		50.10351967										
    20170110 0415	10.45	0.00		0.00	0.02	0.02	0.92	48.05		47.27668845										
    20170110 0430	10.45				0.01	0.02	0.92	48.05		52.54237288										
    20170110 0445	10.49999991	0.05	0.05		0.02	0.01	1.17	53.88		52.42718447										
    20170110 0500	10.45000018	-0.05		0.05	0.02	0.02	0.93	48.07		53.86533666										
    20170110 0515	10.45000005	0.00		0.00	0.01	0.02	0.93	48.07		50.92838196										
    20170110 0530	10.45000001	0.00		0.00	0.01	0.01	0.93	48.07		52.74725275										
    20170110 0545	10.45000001				0.01	0.01	0.93	48.07		53.63128492										
    20170110 0600	10.47363907	0.02	0.02		0.01	0.01	1.06	51.40		54.64480874										
    20170110 0615	10.45	-0.02		0.02	0.01	0.01	0.93	48.08		50										
    20170110 0630	10.38078549	-0.07		0.07	0.01	0.02	0.66	39.94		47.73869347										
    20170110 0645	10.37896211	0.00		0.00	0.01	0.02	0.66	39.74		32.90734824										
    20170110 0700	10.39299999	0.01	0.01		0.01	0.02	0.73	42.04		44.48669202										
    20170110 0715	10.33099753	-0.06		0.06	0.01	0.02	0.55	35.59		29.96632997										
    20170110 0730	10.39999996	0.07	0.07		0.01	0.02	0.84	45.60		43.01369863										
    20170110 0745	10.32100859	-0.08		0.08	0.01	0.02	0.62	38.27		35.44018059										
    20170110 0800	10.25	-0.07		0.07	0.01	0.03	0.50	33.11		30.54474708										
    20170110 0815	10.31106544	0.06	0.06		0.02	0.02	0.68	40.53		32										
    20170110 0830	10.29851782	-0.01		0.01	0.01	0.02	0.65	39.56		34.49691992										
    20170110 0845	10.38999754	0.09	0.09		0.02	0.02	0.97	49.13		44.80968858										
    20170110 0900	10.26039929	-0.13		0.13	0.02	0.03	0.65	39.57		36.5819209										
    20170110 0915	10.28092716	0.02	0.02		0.02	0.03	0.71	41.51		38.40877915										
    20170110 0930	10.41500297	0.13	0.13		0.03	0.02	1.10	52.29		46.48390942										
    20170110 0945	10.41446338	0.00		0.00	0.03	0.02	1.09	52.25		47.79411765										
    20170110 1000	10.3	-0.11		0.11	0.02	0.03	0.79	44.19		45.29616725										
    20170110 1015	10.29151733	-0.01		0.01	0.02	0.03	0.77	43.65		44.98269896										
    20170110 1030	10.26753076	-0.02		0.02	0.02	0.03	0.73	42.10		42.87343216										
    20170110 1045	10.26753076				0.02	0.03	0.73	42.10		46.13496933										
    20170110 1100	10.36798778	0.10	0.10		0.02	0.02	1.03	50.65		48.10874704										
    20170110 1115	10.39981794	0.03	0.03		0.03	0.02	1.13	53.02		54.9436796										
    20170110 1130	10.37865704	-0.02		0.02	0.02	0.02	1.05	51.26		58.61148198										
    20170110 1145	10.37866488	0.00	0.00		0.02	0.02	1.05	51.26		54.94186047										
    20170110 1200	10.48375725	0.11	0.11		0.03	0.02	1.44	59.09		61.84379001										
    20170110 1215	10.60447758	0.12	0.12		0.03	0.02	1.93	65.87		63.20987654										
    20170110 1230	10.62000044	0.02	0.02		0.03	0.02	2.00	66.64		75.86206897										
    20170110 1245	10.62522697	0.01	0.01		0.03	0.02	2.02	66.91		75.29411765										
    20170110 1300	10.54	-0.09		0.09	0.03	0.02	1.42	58.60		59.90491284										
    20170110 1315	10.57	0.03	0.03		0.03	0.02	1.53	60.46		61.81818182										
    20170110 1330	10.4873114	-0.08		0.08	0.03	0.02	1.14	53.34		64.86486486										
    20170110 1345	10.48082444	-0.01		0.01	0.02	0.02	1.12	52.82		65.07177033										
    20170110 1400	10.56144109	0.08	0.08		0.03	0.02	1.40	58.31		71.44948755										
    20170110 1415	10.54820276	-0.01		0.01	0.03	0.02	1.33	57.14		70.11494253										
    20170110 1430	10.55881489	0.01	0.01		0.03	0.02	1.37	57.87		65.73311367										
    20170110 1445	10.48833087	-0.07		0.07	0.02	0.02	1.06	51.56		56.81114551										
    20170110 1500	10.49210894	0.00	0.00		0.02	0.02	1.08	51.86		58.98251192										
    20170110 1515	10.5696268	0.08	0.08		0.03	0.02	1.36	57.71		63.50777935										
    20170110 1530	10.57971704	0.01	0.01		0.03	0.02	1.40	58.42		57.84313725										
    20170110 1545	10.6	0.02	0.02		0.02	0.02	1.49	59.87		49.609375										
    20170110 1600	10.5367546	-0.06		0.06	0.02	0.02	1.15	53.58		42.57602862										
    20170110 1615	10.49412539	-0.04		0.04	0.02	0.02	0.99	49.78		39.02847571										
    20170110 1630	10.49412539				0.02	0.02	0.99	49.78		45.5078125										
    20170110 1645	10.54204356	0.05	0.05		0.02	0.02	1.18	54.03		47.35849057										
    20170110 1700	10.59999996	0.06	0.06		0.02	0.02	1.41	58.59		61.18811881										
    20170110 1715	10.54297	-0.06		0.06	0.02	0.02	1.13	53.01		55.57553957										
    20170110 1730	10.53042645	-0.01		0.01	0.02	0.02	1.08	51.85		46.83026585										
    20170110 1745	10.53177183	0.00	0.00		0.02	0.02	1.08	51.97		48.32635983										
    20170110 1800	10.53177183				0.02	0.02	1.08	51.97		47.10920771										
    20170110 1815	10.53177183				0.02	0.02	1.08	51.97		55.55555556										
    20170110 1830	10.50000001	-0.03		0.03	0.02	0.02	0.94	48.35		50.94339623										
    20170110 1845	10.501	0.00	0.00		0.01	0.02	0.94	48.47		40.05763689										
    20170110 1900	10.4841257	-0.02		0.02	0.01	0.02	0.87	46.48		36.44067797										
    20170110 1915	10.69000001	0.21	0.21		0.03	0.01	1.88	65.24		58.33333333										
    20170110 1930	10.69207163	0.00	0.00		0.03	0.01	1.89	65.37		66.17954071										
    20170110 1945	10.7	0.01	0.01		0.02	0.01	1.93	65.90		73.1981982										
    20170110 2000	10.68555171	-0.01		0.01	0.02	0.01	1.78	63.97		70.96069869										
    20170110 2015	10.61000001	-0.08		0.08	0.02	0.02	1.22	54.92		56.99588477										
    20170110 2030	10.62635517	0.02	0.02		0.02	0.02	1.29	56.36		52.92792793										
    20170110 2045	10.64551937	0.02	0.02		0.02	0.01	1.38	58.05		62.65356265										
    20170110 2100	10.59000002	-0.06		0.06	0.02	0.02	1.07	51.80		56.66666667										
    20170110 2115	10.59007388	0.00	0.00		0.02	0.02	1.07	51.80		56.47321429										
    20170110 2130	10.68261187	0.09	0.09		0.02	0.02	1.51	60.11		63.95563771										
    20170110 2145	10.57531175	-0.11		0.11	0.02	0.02	0.98	49.46		53.31278891										
    20170110 2200	10.65	0.07	0.07		0.03	0.02	1.24	55.39		60.83815029										
    20170110 2215	10.64999997	0.00		0.00	0.02	0.02	1.24	55.39		60.78147612										
    20170110 2230	10.59781898	-0.05		0.05	0.02	0.02	1.02	50.58		57.85123967										
    20170110 2245	10.59501682	0.00		0.00	0.02	0.02	1.01	50.33		40.91778203										
    20170110 2300	10.64996841	0.05	0.05		0.02	0.02	1.23	55.07		46.35416667										
    20170110 2315	10.58174336	-0.07		0.07	0.02	0.02	0.95	48.84		40.72327044										
    20170110 2330	10.63890145	0.06	0.06		0.02	0.02	1.15	53.58		46.53902798										
    20170110 2345	10.55001523	-0.09		0.09	0.02	0.03	0.87	46.38		45.66473988										
    20170111 0000	10.55001523				0.02	0.02	0.87	46.38		44.37869822										
    20170111 0015	10.55000001	0.00		0.00	0.02	0.02	0.87	46.38		42.68292683										
    20170111 0030	10.6	0.05	0.05		0.02	0.02	1.04	51.00		50.76923077										
    20170111 0045	10.64384486	0.04	0.04		0.02	0.02	1.21	54.69		53.89048991										
    20170111 0100	10.59467128	-0.05		0.05	0.02	0.02	1.01	50.13		43.23076923										
    20170111 0115	10.6	0.01	0.01		0.02	0.02	1.02	50.61		52.28519196										
    20170111 0130	10.65	0.05	0.05		0.02	0.02	1.22	54.99		50										
    20170111 0145	10.60000005	-0.05		0.05	0.02	0.02	1.01	50.20		45.62937063										
    20170111 0200	10.60000005				0.02	0.02	1.01	50.20		50.19230769										
    20170111 0215	10.60000005				0.02	0.02	1.01	50.20		50.48355899										
    20170111 0230	10.57900005	-0.02		0.02	0.02	0.02	0.92	48.00		42.65010352										
    20170111 0245	10.57900005				0.02	0.02	0.92	48.00		49.63855422										
    20170111 0300	10.63829879	0.06	0.06		0.02	0.02	1.20	54.52		49.88009592										
    20170111 0315	10.58000001	-0.06		0.06	0.02	0.02	0.93	48.13		53.88601036										
    20170111 0330	10.58000001				0.02	0.02	0.93	48.13		53.88601036										
    20170111 0345	10.62	0.04	0.04		0.02	0.02	1.11	52.56		58.21596244										
    20170111 0400	10.62				0.02	0.01	1.11	52.56		52.65957447										
    20170111 0415	10.59	-0.03		0.03	0.02	0.02	0.96	48.93		42.54143646										
    20170111 0430	10.58000026	-0.01		0.01	0.01	0.02	0.91	47.74		47.67801858										
    20170111 0445	10.63999999	0.06	0.06		0.02	0.01	1.21	54.81		55.29100529										
    20170111 0500	10.68247875	0.04	0.04		0.02	0.01	1.44	59.03		54.32432432										
    20170111 0515	10.66961308	-0.01		0.01	0.02	0.01	1.34	57.28		60.54216867										
    20170111 0530	10.65	-0.02		0.02	0.02	0.01	1.20	54.63		57.10227273										
    20170111 0545	10.64	-0.01		0.01	0.02	0.01	1.14	53.28		55.52486188										
    20170111 0600	10.66999998	0.03	0.03		0.02	0.01	1.31	56.74		62.26415094										
    20170111 0615	10.66999998				0.02	0.01	1.31	56.74		62.26415094										
    20170111 0630	10.58623347	-0.08		0.08	0.01	0.02	0.84	45.75		43.43434343										
    20170111 0645	10.58579206	0.00		0.00	0.01	0.02	0.84	45.70		50.88757396										
    20170111 0700	10.64999864	0.06	0.06		0.02	0.01	1.16	53.67		58.70646766										
    20170111 0715	10.63	-0.02		0.02	0.02	0.01	1.05	51.15		51.30890052										
    20170111 0730	10.58579207	-0.04		0.04	0.01	0.02	0.85	46.01		46.00938967										
    20170111 0745	10.58579209	0.00	0.00		0.01	0.02	0.85	46.01		49.49494949										
    20170111 0800	10.59082391	0.01	0.01		0.01	0.01	0.88	46.72		51.40664962										
    20170111 0815	10.64218629	0.05	0.05		0.02	0.01	1.15	53.42		50.2617801										
    20170111 0830	10.5910003	-0.05		0.05	0.01	0.02	0.89	47.07		38.36317136										
    20170111 0845	10.59100059	0.00	0.00		0.01	0.02	0.89	47.07		39.57783641										
    20170111 0900	10.50065316	-0.09		0.09	0.01	0.02	0.61	37.85		33.40757238										
    20170111 0915	10.26335002	-0.24		0.24	0.01	0.04	0.32	24.36		22.15657312										
    20170111 0930	10.19	-0.07		0.07	0.01	0.04	0.28	21.78		16.66666667										
    20170111 0945	10.18307636	-0.01		0.01	0.01	0.04	0.27	21.55		16.50618982										
    20170111 1000	10.09613043	-0.09		0.09	0.01	0.04	0.23	18.83		16.43835616										
    20170111 1015	10.16097308	0.06	0.06		0.01	0.04	0.36	26.29		23.27044025										
    20170111 1030	10.09000042	-0.07		0.07	0.01	0.04	0.31	23.72		15.0872818										
    20170111 1045	10.22018135	0.13	0.13		0.02	0.04	0.56	36.07		27.52192982										
    20170111 1100	10.25708866	0.04	0.04		0.02	0.03	0.64	39.08		31.82320442										
    20170111 1115	9.9	-0.36		0.36	0.02	0.06	0.36	26.22		22.82091918										
    20170111 1130	10.10184103	0.20	0.20		0.03	0.05	0.63	38.53		33.24194654										
    20170111 1145	10.0860799	-0.02		0.02	0.03	0.05	0.61	38.00		30.47752809										
    
    

  • administrators

    @j45p41 said in Calculating RSI mismatch with Ninjatrader/Excel:

    I started to compare back test results in backtrader with ninjatrader

    Do you have the source code of Ninjatrader to understand how they have implemented RSI?

    @j45p41 said in Calculating RSI mismatch with Ninjatrader/Excel:

    I then used excel to calculate RSI and found that the ninjatrader results were much closer

    What is closer? RSI is well defined.

    @j45p41 said in Calculating RSI mismatch with Ninjatrader/Excel:

    Here is how I have implemented RSI:

    No. You show a series of values. That's not an implementation and gives no clues as to what you may or may not be doing (in any case the intended tabular output is badly formatted, killing the intended tabular presentation)

    You may want to read something like this additionally:

    And understand that many commercial platforms do whatever they want without telling you.



  • @backtrader thank you for your explanation and for taking the time to reply, I could have been more clear with my post. I didn’t realise that there were many ways of implementing RSI :). In that case is there a way of specifying how backtrder calculates the moving average?

    I do think this is a great project and have invested a lot of time in it and benefitted a lot from the support on this forum. I am just trying to port my work on NT and got stuck here.


  • administrators

    @j45p41 said in Calculating RSI mismatch with Ninjatrader/Excel:

    In that case is there a way of specifying how backtrder calculates the moving average?

    Docs - Indicator Reference - Look for SmoothedMovingAverage

    The calculation method is not a secret, it is in Welles Wilder book and even in Wikipedia and in the Reddit post linked above.