Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    Oanda data feed

    General Code/Help
    oanda live data oandapy oanda access
    3
    14
    3208
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • D
      D. Virant last edited by D. Virant

      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)
      ![alt text](image url)
          # 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!
      0_1547706184351_9aa3b22a-747a-4fd2-816e-590924efd9cd-image.png This is the output I get.

      B 1 Reply Last reply Reply Quote 0
      • B
        backtrader administrators @D. Virant last edited by

        @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

        D 1 Reply Last reply Reply Quote 0
        • D
          D. Virant @backtrader last edited by

          @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.

          1 Reply Last reply Reply Quote 0
          • B
            backtrader administrators last edited by

            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.

            D 1 Reply Last reply Reply Quote 0
            • D
              D. Virant @backtrader last edited by

              @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 1 Reply Last reply Reply Quote 0
              • D
                dasch @D. Virant last edited by dasch

                @d-virant you could give the btoandav20 chance. This is still maintained and works.

                D 1 Reply Last reply Reply Quote 0
                • D
                  D. Virant @dasch last edited by D. Virant

                  @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 1 Reply Last reply Reply Quote 0
                  • D
                    dasch @D. Virant last edited by

                    @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.

                    D 1 Reply Last reply Reply Quote 0
                    • D
                      D. Virant @dasch last edited by

                      @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?

                      1. I'm using the Anaconda distribution, so I do msot stuff through the Anaconda prompt.
                      2. I run a "pip install backtrader" in the Anaconda prompt, it runs
                      3. I clone the btoandaV20 github repository into some folder on my PC (wherever my script is saved)
                      4. I navigate Anaconda prompt to that folder and run a "pip install v20"

                      That sound right or am I missing something?

                      D 1 Reply Last reply Reply Quote 0
                      • D
                        dasch @D. Virant last edited by

                        @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)

                        1 Reply Last reply Reply Quote 0
                        • B
                          backtrader administrators last edited by

                          @d-virant said in Oanda data feed:

                          1. 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:

                          1. 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 as

                          from .btoandav20 import OandaV20Store
                          

                          because the __init__.py of the package is already importing everything from the stores subpackage which in turn is importing the OandaV20Store from the actual module containing the Store.

                          1 Reply Last reply Reply Quote 0
                          • D
                            D. Virant last edited by D. Virant

                            Hm, when I try to pip install backtrader v20, I get an error in the command prompt (oh, I'm on a Windows system):
                            0_1548223522693_7370b212-9fb0-4f05-aff9-4d9199bd5921-image.png

                            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).

                            1 Reply Last reply Reply Quote 0
                            • B
                              backtrader administrators last edited by

                              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 name v30)

                              v20 requires ujson (you see the name in the error traces) and this is a non-pure Python package (it is actually written in C)

                              • 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:

                              • https://www.lfd.uci.edu/~gohlke/pythonlibs/

                              Once you have it installed, the dependency will be already satisfied by the time you try to install v20

                              D 1 Reply Last reply Reply Quote 0
                              • D
                                D. Virant @backtrader last edited by

                                @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.

                                1 Reply Last reply Reply Quote 0
                                • 1 / 1
                                • First post
                                  Last post
                                Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors