Extreme spreads with Oanda - how to manage this?
I'm testing with my Oanda practice account and found some huge differences between the current price (1-minute timeframe) and the price on which I could buy/sell. Then I checked the historical spreads on the Oanda website and found that the spread is extremely high every day around 23:00. Actually that was the time when my strategy has lost a lot of money (21:00 local time). Other periods were fine, buy and sell prices were very close to the actual price.
How can we avoid trading when the spread is so high? Is it possible to get the current spread of an instrument somehow?
This is a tricky one, because unless you are monitoring the ticks, you might not see a spike until late in the game.
A couple of ideas..... May not be perfect.
- Move to a higher TF where the spread is less of an issue.
- Use the Oandpy library to monitor the bid and ask spread
- Close positions before that particular spike zone each day. Also avoid adding new positions.
I guess since you are on the 1min timeframe, you are not a long term trader so picking your trading times might be the best way to do it.
A very simple example....
import datetime import backtrader as bt trading_open = datetime.time(0,0) trading_close = datetime.time(8,0) class testStrategy(bt.Strategy): def __init__(self): self.dataclose = self.datas.close def next(self): self.data_time = self.datas.datetime.time() if self.data_time > trading_open and self.data_time < trading_close: print('I can trade!') else: print('Trading closed')
Thanks for the tips. Probably number 2 would be the best. I wonder if it is interesting for @backtrader to include that in one of the future versions. Like to have self.datas.current_spread or something like that ;)
Number 3 is also a good way to go but then I'll need to check the historical spreads for all the instruments and of course I need to sync the timezones.
Thanks again and cheers,
Something which can be looked into, with a general model being necessary. For example: the
5-secondsreal-time bars offered by Interactive Brokers deliver no Bid-Ask information and people can switch them on.
Is it possible to get bid/ask lines in an Oanda datasource, similarly to what you've described in your "Escape from OHLC land" post? That would also solve the spread problem because that way calculating the spread would be super easy.
As I can see there is an option ("bidask") that controls what is retrieved from Oanda, so I believe it is already partially implemented.
Would it be possible to add the bid/ask lines to the data if the timeframe is set to bt.TimeFrame.Ticks?