Backtest using any historical data

IBridgePy can backtest algo trading strategies using historical data not only from Interactive Brokers but any other data providers. The basic idea is to save historical data from any data providers to local csv files and then supply them to IBridgePy backtester. In this post, we will explain how to backtest a sample algo strategy by supplying historical data from local csv files.

,close,high,low,open,volume
1540166400,275.01,277.36,274.41,277.0,649920
1540252800,273.61,274.87,268.61,270.94,1137894
1540339200,265.32,273.76,264.7,273.33,1291493
1540425600,270.08,271.81,266.23,267.37,972500
1540512000,265.33,271.0,262.29,265.92,1600199
1540771200,263.86,270.25,259.85,268.78,1195387
1540857600,267.77,268.12,263.12,263.67,1226114
1540944000,270.63,273.23,270.12,270.65,967075
1541030400,273.51,273.73,270.38,271.62,721217
1541116800,271.89,275.23,269.59,274.74,935397
1541376000,273.39,274.01,271.35,272.44,468150
1541462400,275.12,275.3,273.25,273.32,447683

The above content is the excerpt from a csv file containing the historical data of SPY, and the time frame of the data is daily. There are 6 columns in this csv file and the columns are delimited by commas. The first row is the column names, open, high, low, close and volume in lower cases. The first column is integers, representing UNIX Epoch time, which is the number of seconds that have elapsed since 00:00:00 Thursday, 1 January 1970. The other columns are open price, high price, low price, close price and volume. Assuming that the name of this file is SPY_200D_1day.csv and there is another csv file called SPY_10D_1minute.csv that has minute candlesticks of SPY of the past 1o days. Both of the files are saved at the folder of IBridgePyRoot/Input. Then we can use these files in backtesting.

fileName = 'demo_close_price_reversion.py'

backtest = True
accountCode = 'yourAccountCode'
dataProviderName = 'LOCAL_FILE'
runMode = 'RUN_LIKE_QUANTOPIAN'

histIngestionPlan = HistIngestionPlan()
histIngestionPlan.add(Plan(security=symbol('SPY'), barSize='1 min', goBack='10 D', fileName='SPY_10D_1minute.csv'))
histIngestionPlan.add(Plan(security=symbol('SPY'), barSize='1 day', goBack='30 D', fileName='SPY_200D_1day.csv'))

TimeGeneratorType = 'AUTO'
endTime = dt.datetime.now().replace(second=0)
startTime = endTime - dt.timedelta(days=4)
freq = '1T'  # 1S = 1 second; 1T = 1 minute; 1H = 1 hour

The above is the content of TEST_ME.py, which shows how to backtest a strategy using historical data from local csv files.

Explanations:

dataProviderName = 'LOCAL_FILE'

It specifies that IBridgePy should use local files to supply historical data instead of fetching from Interactive Brokers.

histIngestionPlan.add(Plan(security=symbol('SPY'), barSize='1 min', goBack='10 D', fileName='SPY_10D_1minute.csv'))

Parameter of fileName tells IBridgePy to supply SPY minute bars using the file of SPY_10D_1minute.csv.

histIngestionPlan.add(Plan(security=symbol('SPY'), barSize='1 day', goBack='30 D', fileName='SPY_200D_1day.csv'))

Parameter of fileName tells IBridgePy to supply SPY minute bars using the file of SPY_200D_1day.csv.

Actually, users do not necessarily need to put csv files at the folder of IBridgePyRoot/Input. If the files are placed at other folder, for example, C:\abc\def

histIngestionPlan = HistIngestionPlan(defaultFolderName='C:\abc\def')

The above setting tells IBridgePy that the default folder to store csv files is C:\abc\def. IBridgePy will go to this folder and look for the specified fileName, and then, load historical data there.

The last step of backtest is to execute “TEST_ME.py”