kieran-mackle / AutoTrader

A Python-based development platform for automated trading systems - from backtesting to optimisation to livetrading.
https://kieran-mackle.github.io/AutoTrader/
GNU General Public License v3.0
945 stars 217 forks source link

HCF when providing backtesting data files? #21

Closed bb01100100 closed 2 years ago

bb01100100 commented 2 years ago

Hello,

Firstly, thanks very much for creating this framework, I'm really enjoying focussing on my trading logic instead of the surrounding infrastructure.

When I backtest with my own data files (e.g. EUR_JPY M5, H8 and M timeframes), I noticed that the trade sizing was "off", e.g. a position for EUR_JPY that should have been about 3200 units was being placed for 20 units.

I eventually realised that the sizing function was correct but the HCF it used was set to 1. If it was set to the _JPY rate then the sizing was correct for the given pair.

When using the Oanda broker, it returns the HCF values as part of the REST response for get_price (https://github.com/kieran-mackle/AutoTrader/blob/bbea33fb61f5f7bad82332a6e85d088add31b8dc/autotrader/brokers/oanda/Oanda.py#L105).

So my question is whether I can supply the correct historic HCF data when backtesting with files? If not, do you have any thoughts on where in the framework I should look if I wanted to create a pull request to support this?

Thanks, Kel

kieran-mackle commented 2 years ago

Hi Kel,

Glad you are enjoying it!

This is another good suggestion. I have just added the necessary methods to provide locally stored quote data to backtests with the release of v0.5.30. To do so, provide a dictionary to the quote_data argument of the add_data() method, containing the filenames of the quote data for each instrument being traded as the key. See the example below:

at.add_data(data_dict={'EUR_USD': 'EU1H.csv'},
            quote_data={'EUR_USD': 'EU_quote_data_1H.csv'})

The same method can be used when running multiple-timeframe strategies, but note only one quote data file is required per instrument traded (rather than one for each timeframe). This is because the backtest will run on the first timeframe provided in the data_dict (which should also be the highest resolution timeframe), and so only that timeframe of data requires quote data. For example, if you are running a strategy with 1-hour and 1-day bars, the quote data should be provided as 1-hour bars. For example:

at.add_data(data_dict={'EUR_USD': {'1H': 'EU1H.csv',
                                   '1D': 'EU1D.csv'}
                       },
            quote_data={'EUR_USD': 'EU_quote_data_1H.csv'})

Let me know if you have any questions.

Cheers, Kieran