Oanda data feed
-
Hey everyone!
I'm trying to get the Quickstart example to work with Oanda EURUSD data instead of the provided csv example. I'm a Oanda customer (in Europe), I've generated the API token and installed OandaPy through pip. I was hoping I would get a similar output to what is shown in the quickstart guide (starting equity, some close prices, final equity). However, the code doesn't seem to be getting any data from Oanda and only prints out the equity twice.
Just as a disclaimer, I really appreciate what you're doing with Backtrader, I love the concept of it. I'm just not a very experienced programmer and it's difficult for me to learn stuff from just the documentation, without a bit of input.Create a Strategy
class TestStrategy(bt.Strategy): def log(self, txt, dt=None): ''' Logging function for this strategy''' dt = dt or self.datas[0].datetime.date(0) print('%s, %s' % (dt.isoformat(), txt)) def __init__(self): # Keep a reference to the "close" line in the data[0] dataseries self.dataclose = self.datas[0].close def next(self): # Simply log the closing price of the series from the reference self.log('Close, %.2f' % self.dataclose[0]) if __name__ == '__main__': # Create a cerebro entity cerebro = bt.Cerebro() # Create oandastore oandastore = bt.stores.OandaStore(token = token, account = account)  # instantiate data data = oandastore.getdata(dataname='EUR_USD', compression=1, backfill=False, fromdate=datetime.datetime(2018, 1, 1), todate=datetime.datetime(2019, 1, 1), qcheck=0.5, timeframe=bt.TimeFrame.Minutes, backfill_start=False, historical=False) # Add the Data Feed to Cerebro cerebro.adddata(data) # Set cash of default broker cerebro.broker.setcash(10000.0) # Add a strategy cerebro.addstrategy(TestStrategy) # Print out the starting conditions print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue()) # Run over everything cerebro.run() # Print out the final result print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
I know there's been a similar post recently, but I assume that installing OandaPy avoids the issue of V20 not being supported? Or, does Oanda simply not work at the moment? If anyone has any advice on why this isn't working, I'd be super grateful!
This is the output I get.
-
@d-virant said in Oanda data feed:
but I assume that installing OandaPy avoids the issue of V20 not being supported?
How?
A user wrote a wrapper for the v2: https://github.com/ftomassetti/backtrader-oandav20
-
@backtrader A previous post mentioned that oandapy was based on an older version of the oanda API. I guess I assumed wrong, but that's why I posted this question on a platform frequented by people far more knowledgeable than me.
Thanks for the link, I'll check it out. -
The messages from the API are completely different, same as the keys. Your best bet is to check with the creators of the v20 broker module.
-
@backtrader Ok, I'll see what I can do. This isn't meant as a prod or anything, but will the v20 eventually be supported by the "official" Backtrader? Just so I know if I should invest lots of time in trying to make it work now (not in a hurry), or wait until someone who actually knows their stuff does it.
-
@d-virant you could give the btoandav20 chance. This is still maintained and works.
-
@dasch I'm trying it and would love to keep using it, but I can't even seem to get past the set-upstage. No matter what I try, I always get an error saying
AttributeError: module 'btoandav20.stores' has no attribute 'OandaV20Store'
Tried manually installing it, pip installing it and just putting the whole folder into the same directory as my script, nothing really helps. If I add
from btoandav20.stores import oandav20store
I just get
ModuleNotFoundError: No module named 'v20'
So it seems I get errors every step of the way, even with just the sample script from the github repo...
-
@d-virant you need v20 for it. So install v20:
sudo pip install v20
then make btoandav20 available for python, you need to put it manually where python will find it. You could copy the whole folder into the directory where your backorder script is or add the path to your script:
import sys sys.path.append('../lib')
and copy btoandav20 into this lib directory.
Hope that helps.
-
@dasch I think it does, but just in case, can you tell me if I'm doing something horribly wrong if I go through it step by step?
- I'm using the Anaconda distribution, so I do msot stuff through the Anaconda prompt.
- I run a "pip install backtrader" in the Anaconda prompt, it runs
- I clone the btoandaV20 github repository into some folder on my PC (wherever my script is saved)
- I navigate Anaconda prompt to that folder and run a "pip install v20"
That sound right or am I missing something?
-
@d-virant i am not sure about anaconda but you would go:
I run a "pip install backtrader v20" in the Anaconda prompt, it runs
I clone the btoandaV20 github repository (the btoandav20 directory from repository) into some folder on my PC (wherever my script is saved)
-
@d-virant said in Oanda data feed:
- I navigate Anaconda prompt to that folder and run a "pip install v20"
This can be done from anywhere in the prompt, because
pip
downloads and install the package from PyPi. It has nothing to do with the files you download from the repository.@d-virant said in Oanda data feed:
- I clone the btoandaV20 github repository into some folder on my PC (wherever my script is saved)
You need the directory which is inside the repository, which is named
btoandav20
. This is where the actual package is contained. You can then proceed with the imports in your script asfrom .btoandav20 import OandaV20Store
because the
__init__.py
of the package is already importing everything from thestores
subpackage which in turn is importing theOandaV20Store
from the actual module containing the Store. -
Hm, when I try to pip install backtrader v20, I get an error in the command prompt (oh, I'm on a Windows system):
I'm not sure if that's the main issue, backtrader itself seems to be isntalled. However, if I proceed and try to import the package with
from .btoandav20 import OandaV20Store
I get this error:
ModuleNotFoundError: No module named '__main__.btoandav20'; '__main__' is not a package
I do have the btoandav20 folder in my directory. Thanks for all the help, I've been struggling with this for a while. Even tried with a friend that's a little bit more IT savvy and he couldn't figure it out either. I installed Visual C++ Tools and added the cl.exe to my PATH, and I jsut get a different error (error: cl.exe failed with exit status 2).
-
Remove the
.
in the import, because you obviously don't have a package.from btoandav20 import ...
@d-virant said in Oanda data feed:
backtrader itself seems to be isntalled
backtrader is a pure Python package and self-contained with no external dependencies (plotting is optional) and doesn't require a
C
compiler.It would seem that
v20
does. (That's really a very stupid name for a package, I am considering registering a dummy package with the namev30
)v20
requiresujson
(you see the name in the error traces) and this is a non-pure Python package (it is actually written inC
)- https://github.com/oanda/v20-python/blob/master/src/setup.py (see the
requires
section) - https://pypi.org/project/ujson/
Your best option is to download
ujson
from here:Once you have it installed, the dependency will be already satisfied by the time you try to install
v20
- https://github.com/oanda/v20-python/blob/master/src/setup.py (see the
-
@backtrader Thank you!! I didn't realize I needed an external thing, but it actually works now. Can finally get to the actual fun part of coding out the strategy.