Closed GL-Software closed 2 years ago
This looks like a bug I've introduced in v0.7.0. I'll work on a fix for it now.
In the meantime, a workaround would be to download the data first, then run the backtest by providing the local data filepath.
To download the data:
from autotrader import AutoData
data_config = {
'data_source': 'oanda',
'API': 'api-fxpractice.oanda.com',
'ACCESS_TOKEN': 'xxx-yyy',
'PORT': 443,
'ACCOUNT_ID': 'xxx-xxx-xxxxxxxx-xxx'
}
ad = AutoData(data_config)
data = ad.fetch(instrument='EUR_USD', granularity='1h', count=100)
To run a backtest with local data, refer to the docs for add_data.
I'll aim to have a fix out in the next few hours.
@GL-Software This is patched now with v0.7.1
, so you can either update by pulling the latest from the repo or by running:
pip install autotrader --upgrade
Note that the configuration keys required for Oanda have changed slightly to clarify what they are to be used for. You can refer to the account configuration docs for the new keys, but feel free to ask here if it is not clear.
For complete reference, this is the runfile I have used to verify the fix:
from autotrader import AutoTrader
at = AutoTrader()
at.configure(verbosity=1, show_plot=True, feed='oanda', mode='periodic')
at.add_strategy('macd')
at.backtest(start = '1/1/2022', end = '1/5/2022')
at.virtual_account_config(initial_balance=1000, leverage = 30)
at.run()
And this is my account config for Oanda (config/keys.yaml
):
OANDA:
LIVE_API: "api-fxtrade.oanda.com"
LIVE_ACCESS_TOKEN: "xxx-yyy"
PRACTICE_API: "api-fxpractice.oanda.com"
PRACTICE_ACCESS_TOKEN: "xxx-yyy"
DEFAULT_ACCOUNT_ID: "xxx-xxx-xxxxxxxx-xxx"
PORT: 443
Thx Kieran now it works fine!
Is it possible to see all orders opened/close in backtest as a report ? Actually I can see only summary statistical info and backtest graph, there's no way to check if strategy works as expected by inspecting a report trade by trade.
Glad to hear.
Absolutely, you can access all of this info via at.trade_results
- this is an instance of the TradeAnalysis
class.
To look at the trades (ie. fills), you can inspect at.trade_results.trade_history
, giving you a DataFrame like this:
In [5]: at.trade_results.trade_history
Out[5]:
order_time order_price order_type fill_time fill_price direction size fee instrument id order_id broker
2022-01-19 11:00:00+00:00 2022-01-19 10:00:00+00:00 1.061250 market 2022-01-19 11:00:00+00:00 1.06120 1 3663.00 0.0 AUD_NZD 1 1 broker
2022-01-20 00:00:00+00:00 2022-01-20 00:00:00+00:00 1.065345 market 2022-01-20 00:00:00+00:00 1.06535 -1 3663.00 0.0 AUD_NZD 2 1 broker
Since you are using Oanda, you might be more interested in at.trade_results.isolated_position_history
. This will closer reflect how Oanda manages 'trades', which is a bit more like treating them as positions (hence the name 'isolated position'). This DataFrame looks as so:
In [8]: at.trade_results.isolated_position_history
Out[8]:
instrument status ID order_price order_time ... exit_time exit_price trade_duration fees broker
2022-01-19 11:00:00+00:00 AUD_NZD closed 1 1.06125 2022-01-19 10:00:00+00:00 ... 2022-01-20 00:00:00+00:00 1.06535 46800.0 None broker
2022-01-24 21:00:00+00:00 AUD_NZD closed 2 1.06631 2022-01-24 20:00:00+00:00 ... 2022-01-25 08:00:00+00:00 1.07047 39600.0 None broker
There is a whole lot more info to be found in the TradeAnalysis
class stored in at.trade_results
- try inspecting at.trade_results.__dict__.keys()
to start. Note that if you are running a strategy with multiple trading bots, you can access their specific trade analysis object in the same manner, perhaps first by using the get_bots_deployed
method to get the bot. Some pseudo code:
EURUSD_bot = at.get_bots_deployed('EUR_USD')
EURUSD_bot.trade_results
Hope this helps!
Thx so much Kieran, I'll take a look
Hi, I'm trying to backtest your macd demo strategy using Oanda as broker but I'm not able to do so.
Here's steps I've done :
1) Create a demo account in Oanda , so to receive api v20 access token and account id
2) Generate keys.yaml in /config folder where I've put previous data (ACCESS_TOKEN, DEFAULT_ACCOUNT_ID)
3) Modified runfile.py to use oanda as broker at line: at.configure(verbosity=1, show_plot=True, feed='oanda', mode='periodic')
4) Changed macd.yaml like to match EURUSD Oanda symbol at line : WATCHLIST: ['EUR_USD']
5) Execute python runfile.py and received this error :
(base) C:\Users\Giacomo\Downloads_AutoTrader\AT_DEMO>python runfile.py Traceback (most recent call last): File "C:\Users\Giacomo\Downloads_AutoTrader\AT_DEMO\runfile.py", line 9, in
at.run()
File "C:\Users\Giacomo\anaconda3\lib\site-packages\autotrader\autotrader.py", line 1049, in run
self._main()
File "C:\Users\Giacomo\anaconda3\lib\site-packages\autotrader\autotrader.py", line 1428, in _main
self._assign_broker(broker_config)
File "C:\Users\Giacomo\anaconda3\lib\site-packages\autotrader\autotrader.py", line 1635, in _assign_broker
broker.configure(account_config, autodata_config=autodata_config)
File "C:\Users\Giacomo\anaconda3\lib\site-packages\autotrader\brokers\virtual\broker.py", line 256, in configure
self._autodata = AutoData(data_config, autodata_config)
File "C:\Users\Giacomo\anaconda3\lib\site-packages\autotrader\autodata.py", line 82, in init
self.ACCOUNT_ID = data_config["ACCOUNT_ID"]
KeyError: 'ACCOUNT_ID'
Is there something else that I have to set in order to be able to backtest whatever strategy using oanda historical prices ?
Thanks, Regards Giacomo