Combining historical data into streaming data to get previous periods data for Simple Moving Average-MVA

I have plan to using Simple Moving Average (MVA) cross between 5 and 15 periods to initiate trading. However using streaming data there will be gap where data is nan while waiting full periods. Because data is actually available in historical data, please advice how to include this 5 and 15 periods historical data into the streaming data, so i don’t have to wait for 15 periods streaming data before i can get non nan value.

Basically instead having:

              Bid Ask Mid MVA5  MVA15 

2018-12-12 20:01 1.12 1.13 1.125 nan nan
2018-12-12 20:02 1.12 1.13 1.125 nan nan
2018-12-12 20:03 1.12 1.13 1.125 nan nan
2018-12-12 20:04 1.12 1.13 1.125 nan nan
2018-12-12 20:05 1.12 1.13 1.125 nan nan
2018-12-12 20:06 1.12 1.13 1.125 1.125 nan
2018-12-12 20:07 1.12 1.13 1.125 1.125 nan
2018-12-12 20:08 1.12 1.13 1.125 1.125 nan
2018-12-12 20:09 1.12 1.13 1.125 1.125 nan
2018-12-12 20:10 1.12 1.13 1.125 1.125 nan
2018-12-12 20:11 1.12 1.13 1.125 1.125 nan
2018-12-12 20:12 1.12 1.13 1.123 1.123 nan
2018-12-12 20:13 1.12 1.13 1.125 1.125 nan
2018-12-12 20:14 1.12 1.13 1.123 1.123 nan
2018-12-12 20:15 1.12 1.13 1.125 1.125 nan
2018-12-12 20:16 1.12 1.13 1.125 1.125 1.111

i want to have below data as soon as I start subscribing using https://fxcmpy.tpq.io/03_streaming_market_data.html

              Bid Ask Mid MVA5  MVA15 

2018-12-12 20:01 1.12 1.13 1.125 1.123 1.13
2018-12-12 20:02 1.12 1.13 1.125 1.13 1.123
2018-12-12 20:03 1.12 1.13 1.125 1.13 1.13
2018-12-12 20:04 1.12 1.13 1.125 1.123 1.12
2018-12-12 20:05 1.12 1.13 1.125 1.12 1.12
2018-12-12 20:06 1.12 1.13 1.125 1.125 1.12
2018-12-12 20:07 1.12 1.13 1.125 1.125 1.123
2018-12-12 20:08 1.12 1.13 1.125 1.125 1.12
2018-12-12 20:09 1.12 1.13 1.125 1.125 1.123
2018-12-12 20:10 1.12 1.13 1.125 1.125 1.123
2018-12-12 20:11 1.12 1.13 1.125 1.125 1.125
2018-12-12 20:12 1.12 1.13 1.123 1.123 1.123
2018-12-12 20:13 1.12 1.13 1.125 1.125 1.125
2018-12-12 20:14 1.12 1.13 1.123 1.123 1.123
2018-12-12 20:15 1.12 1.13 1.125 1.125 1.125
2018-12-12 20:16 1.12 1.13 1.125 1.125 1.111

both example just using random number but I hope you get my point. Please advice

Hi @adrian, we have a strategy that should do what you’re looking for: https://github.com/fxcm/RestAPI/blob/master/Python-Live-Trading-Examples/SMA%20Crossover%20Strategy.py
The Prepare function runs once to gather the data and make the calculations then the strategyHeartBeat function runs at the close of every candle to re-calculate the moving averages.

2 Likes