happydasch / btoandav20

Support for Oanda-V20 API in backtrader
Apache License 2.0
131 stars 52 forks source link

Simple backtest example #56

Closed blake-camp closed 4 years ago

blake-camp commented 4 years ago

Can you guys please provide some simple documentation explaining how to execute a simple backtest on a particular strategy? The code as is for oandav20test will run, but no backtest is performed (no trades are simulated). I don't understand the line if status == data.Live. This is the case even when specific historical dates are provided, when the --trade flag is set to True, etc. Also, I am unclear about the distinction between the Store and the Broker class. Are both needed? Why or why not? Also I've noticed that the code does not handle exceptions for live streaming data in an intuitive way. The code will halt when live bars are encountered, haven't figured out why as of yet. Any guidance would be helpful. Thanks for your work so far.

happydasch commented 4 years ago

Can you guys please provide some simple documentation explaining how to execute a simple backtest on a particular strategy?

Since the oandav20 is mostly done and maintained by me, there will be no documentation that soon, since i do not have that time to do it. For a simple startegy, you will find some examples in the backtrader documentation. Then you just config your data feeds, broker, store and set them as described in the backtrader docu. Nothing more needed.

Feel free to contribute some documentation, while working on this.

Backtrader documentation: https://www.backtrader.com/docu/

For the old oanda store, you can find the docu here: https://www.backtrader.com/docu/live/oanda/oanda/ most params are the same, but for all params available you can find a description in the source code.

The oandav20test is just to show how to initialize the parts and use different options.

I am not sure, why the oandav20test does not generate any trades. Do you get any auth errors?

For the status, live means, the data feed is up to date. so for historical data, it will always be live (i think, but am not sure about this statement). The status can also be delayed, disconnected, etc. you can look into the feed code to see the different states: https://github.com/ftomassetti/backtrader-oandav20/blob/master/btoandav20/feeds/oandav20feed.py

The store is basically the central part which talks to oanda. broker and feeds are using the store, which connects to streams for trasactions and data. so you need to initalize the store with your credentials, also if you use your practice or live account, etc. The broker is resposible for getting orders from backtrader and send them to oanda. Thats why you need both. You would not need the broker, if you just want to fetch data from oanda and simulate/backtest the orders in backtrader.

Also I've noticed that the code does not handle exceptions for live streaming data in an intuitive way. The code will halt when live bars are encountered, haven't figured out why as of yet.

I am not really sure what you mean with that? Could you show some example what you mean? If you set the historical param to true, then only historical data will be returned.

blake-camp commented 4 years ago

Thanks very much for the fast reply. I suspect much of my confusion is due to the fact that I've only just begun playing with this code. The clarification re Store/Broker is helpful, and I think I can probably work out the rest. Out of curiosity, are you trading heavily using this code? I may be be able to provide some additional examples w/ documentation , e.g. simple backtest, if that would be helpful for anyone else.

happydasch commented 4 years ago

Yes, i use the code on a daily basis. Here is some dev code, maybe this helps you. You will just need to enter your credentials in code for oanda (token and account id) there are two files, one which uses the broker, so orders will go to oanda. The other file will run using backtest broker and only use a data feed from oanda.

btoandav20.zip

blake-camp commented 4 years ago

great, thanks a lot!

happydasch commented 4 years ago

one issue you may encounter, when data is switching to live (after backfill) that you recieve tick data. So even if you set your data to a timeframe and compression, this is ignored in live. To get data in your timeframe and compression, you will need to add your data using resampledata(data, timeframe=x, compression=y) or data.resample(...)

happydasch commented 4 years ago

This is because backfill is using historical data (up to 5seconds) while the live data streams provides up to 4 ticks a second, which are not in the requested timeframe and compression. Thats why backtrader needs to do the job.

happydasch commented 4 years ago

i close this issue here, if you have some issues with the exception handing, just create a new issue. Feel free to open new issues if you need some help or clarifications.

blake-camp commented 4 years ago

all of this is very helpful, thanks. you'll probably hear from me again down the road. thanks again.

happydasch commented 4 years ago

you can also use the backtrader forum for questions, there you will get helpful responses regarding strategy development and backtesting questions. The people there are very helpful.

https://community.backtrader.com/