For more feasibility of our platform, ALGOGENE now supports new features:
- import multiple custom data files
- specify user-defined file formats
- include user data for backtest
This article provides a stepwise example to demonstrate how to perform these tasks on ALGOGENE platform.
Suppose we are interested to conduct research on several stocks that are currently not available on ALGOGENE. We downloaded the market data from Yahoo Finance.
For example, we downloaded the daily prices of 2020 for
- 0005.HK, the stock price of HSBC listed on HKEx (https://finance.yahoo.com/quote/0005.HK/history?p=0005.HK)
- 0939.HK, the stock price of China Construction Bank listed on HKEx (https://finance.yahoo.com/quote/0939.HK/history?p=0005.HK)
The downloaded files from Yahoo Finance are in csv format. When we open the files using Notepad or other plain text editors, we can see the data structure as follows:
As we can see, the files contains 7 columns in total and structured below.
|Column Index||Column Name||Data Type|
|0||Date||in format of YYYY-MM-DD|
Now, let's import our data files as follows:
- After login the portal, go to [My History] > [Custom File Viewer]
- Select '/data' directory, then upload data files
- We can then 'Edit' to view the uploaded content
- Now, we need to create a meta file '_meta_.json' to instruct the system how to process the data files.
- '_meta_.json' is in JSON format where we can specify the instrument name in the first key
- in this example, we label them as '0005.HK' and '0939.HK' respectively
- it should be noticed that our specified name has to be distinct from ALGOGENE's existing instruments. Otherwise, the system will skip processing our data files.
- the second key of '_meta_.json' should contain the following
- 'file': the file name on the cloud directory
- 'file_delimiter': the file delimiter used in a data file
- 'period_start': the starting date of a data file, in the format of YYYY-MM-DD
- 'period_end': the end date of a data file, in the format of YYYY-MM-DD
- 'settleCurrency': the settlement currency of the instrument (it is HKD in our example)
- 'contractSize': the number of share per each lot of the instrument
- 'fmt_time': specified the date format in a data file, in Python date encoding
- %Y: the year in four-digit format, eg. "2018"
- %y: the year in two-digit format, that is, without the century. For example, "18" instead of "2018"
- %m: the month in 2-digit format, from 01 to 12
- %b: the first three characters of the month name. eg. "Sep"
- %d: day of the month, from 1 to 31
- %H: the hour, from 0 to 23
- %M: the minute, from 00 to 59
- %S: the second, from 00 to 59
- %f: the microsecond from 000000 to 999999
- %Z: the timezone
- %z: UTC offset
- %j: the number of the day in the year, from 001 to 366
- %W: the week number of the year, from 00 to 53, with Monday being counted as the first day of the week
- %U: the week number of the year, from 00 to 53, with Sunday counted as the first day of each week
- %a: the first three characters of the weekday, e.g. Wed
- %A: the full name of the weekday, e.g. Wednesday
- %B: the full name of the month, e.g. September
- %w: the weekday as a number, from 0 to 6, with Sunday being 0
- %p: AM/PM for time
- 'col_time': the column position of data date (first column index = 0)
- 'col_open': the column position of open price (first column index = 0)
- 'col_high': the column position of high price (first column index = 0)
- 'col_low': the column position of low price (first column index = 0)
- 'col_close': the column position of closing price (first column index = 0)
- 'col_volume': the column position of volume (first column index = 0)
The sample meta file used in the example can be copied here:
After we properly setup the '_meta_.json', we can then include our custom instruments for backtest.
- Go to [Backtest] > [Setting]
- Select '0005.HK' and '0939.HK' in the instrument panel
- 'Start Period' and 'End Period' set to '2020-01' and '2020-12' respectively
- 'Initial Capital' set to 1,000,000
- 'Base Currency' set to 'HKD'
In our example '0005.HK' and '0939.HK', the 2 stocks are both in banking sector. Suppose we found that the 2 companies are correlated and therefore we try to test a pair trading strategy on them! A simple trading idea is as follows:
- Based on a sliding window approach to collect the last 5 closing price
- Fit a simple linear regression model without intercept Y = b*X for the 2 series
- if residual > certain level of the stadard error, buy 1 lot of X and sell b lot of Y
- if residual < -1* certain level of the stadard error, sell 1 lot of X and buy b lot of Y
- for any opened pair, close the position 5 day later
The full source code can be referred below:
The backtest result can be generated as usual.
Now, you learnt how to plugin your own data files on the platform. Try backtest with a custom dataset today! Happy Trading!