Backtrader Community

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

    General Code/Help
    5
    23
    8449
    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.
    • I
      Ivan last edited by

      Hi,
      when I run oandatest.py with minimum parameters (only token, account and data0) I get this error:

      Traceback (most recent call last):
      File "oandatest.py", line 481, in <module>
      runstrategy()
      File "oandatest.py", line 202, in runstrategy
      timeframe = bt.TimeFrame.TFrame(args.timeframe)
      File "D:\Anaconda3\envs\python3.4.5\lib\site-packages\backtrader\dataseries.py
      ", line 53, in TFrame
      return getattr(cls, name)
      AttributeError: type object 'TimeFrame' has no attribute ''

      Any help? Thanks!

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

        @Ivan run with --timeframe XXXX

        The sample made an assumption about an internal structure remaining static ... and the structure moved forward. But if you pass a name like Ticks, Days or other, should still run.

        1 Reply Last reply Reply Quote 0
        • I
          Ivan last edited by

          @backtrader

          python oandatest.py --token XXX --account XXX --data0 EUR_USD --timeframe Days


          Strategy Created

          Datetime, Open, High, Low, Close, Volume, OpenInterest, SMA
          Traceback (most recent call last):
          File "oandatest.py", line 481, in <module>
          runstrategy()
          File "oandatest.py", line 297, in runstrategy
          cerebro.run(exactbars=args.exactbars)
          File "D:\Anaconda3\envs\python3.4.5\lib\site-packages\backtrader\cerebro.py",
          line 810, in run
          runstrat = self.runstrategies(iterstrat)
          File "D:\Anaconda3\envs\python3.4.5\lib\site-packages\backtrader\cerebro.py",
          line 940, in runstrategies
          self._runnext(runstrats)
          File "D:\Anaconda3\envs\python3.4.5\lib\site-packages\backtrader\cerebro.py",
          line 1153, in _runnext
          newqcheck = not any(d.haslivedata() for d in datas)
          File "D:\Anaconda3\envs\python3.4.5\lib\site-packages\backtrader\cerebro.py",
          line 1153, in <genexpr>
          newqcheck = not any(d.haslivedata() for d in datas)
          File "D:\Anaconda3\envs\python3.4.5\lib\site-packages\backtrader\feeds\oanda.p
          y", line 255, in haslivedata
          return bool(self._storedmsg or self.qlive) # do not return the objs
          File "D:\Anaconda3\envs\python3.4.5\lib\site-packages\backtrader\lineseries.py
          ", line 429, in getattr
          return getattr(self.lines, name)
          AttributeError: 'Lines_LineSeries_DataSeries_OHLC_OHLCDateTime_Abst' object has
          no attribute '_storedmsg'

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

            Some code should be executed before to create the _storedmsg, but that isn't actually the real problem, because under normal circumstances it should be created, because the feed is started.

            An educated guess would be that you are running with Python 3.4.5 and oandpy contains things like the __metaclass__ attribute in some classes, which points to Python 2.x only support.

            1 Reply Last reply Reply Quote 0
            • H
              holicst last edited by

              Hi,

              I also tried to use this but got the error below (only account, token and data0 specified):


              Strategy Created

              Traceback (most recent call last):
              File "C:\Baixinho\oanda_live.py", line 498, in <module>
              runstrategy()
              File "C:\Baixinho\oanda_live.py", line 310, in runstrategy
              cerebro.run(exactbars=args.exactbars)
              File "C:\Users\peter\Anaconda3\lib\site-packages\backtrader\cerebro.py", line 873, in run
              runstrat = self.runstrategies(iterstrat)
              File "C:\Users\peter\Anaconda3\lib\site-packages\backtrader\cerebro.py", line 988, in runstrategies
              strat._start()
              File "C:\Users\peter\Anaconda3\lib\site-packages\backtrader\strategy.py", line 353, in _start
              self.start()
              File "C:\Baixinho\oanda_live.py", line 180, in start
              if self.data0.contractdetails is not None:
              File "C:\Users\peter\Anaconda3\lib\site-packages\backtrader\lineseries.py", line 429, in getattr
              return getattr(self.lines, name)
              AttributeError: 'Lines_LineSeries_DataSeries_OHLC_OHLCDateTime_Abst' object has no attribute 'contractdetails'

              Do you have any idea what can cause this? I use Python 3.5.2 64-bit (Anaconda) on Windows 10. How can I solve this?

              Thanks in advance.

              Best regards,
              Tamás

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

                That's insufficient to make any statement beyond that you are running Python 3 and as stated above, oandapy seems to be tailored for Python 2.

                At a minimum the execution line would be needed. It might also be that connectivity to Oanda failed.

                1 Reply Last reply Reply Quote 0
                • H
                  holicst last edited by

                  Has anyone tested oandatest.py in a Python 3.* environment? Or is the only option for me to set up a Python 2.7 environment for backtesting/trading with Oanda?

                  Cheers,
                  Tamás

                  1 Reply Last reply Reply Quote 0
                  • H
                    holicst last edited by

                    Hi,

                    I've created a 2.7 environment in Anaconda, but I get the same error:

                    --------------------------------------------------
                    Strategy Created
                    --------------------------------------------------
                    Traceback (most recent call last):
                      File "C:\Baixinho\oanda_live.py", line 498, in <module>
                        runstrategy()
                      File "C:\Baixinho\oanda_live.py", line 310, in runstrategy
                        cerebro.run(exactbars=args.exactbars)
                      File "C:\Users\peter\Anaconda3\envs\python27\lib\site-packages\backtrader\cerebro.py", line 873, in run
                        runstrat = self.runstrategies(iterstrat)
                      File "C:\Users\peter\Anaconda3\envs\python27\lib\site-packages\backtrader\cerebro.py", line 988, in runstrategies
                        strat._start()
                      File "C:\Users\peter\Anaconda3\envs\python27\lib\site-packages\backtrader\strategy.py", line 353, in _start
                        self.start()
                      File "C:\Baixinho\oanda_live.py", line 180, in start
                        if self.data0.contractdetails is not None:
                      File "C:\Users\peter\Anaconda3\envs\python27\lib\site-packages\backtrader\lineseries.py", line 429, in __getattr__
                        return getattr(self.lines, name)
                    AttributeError: 'Lines_LineSeries_DataSeries_OHLC_OHLCDateTime_Abst' object has no attribute 'contractdetails'
                    

                    I run the program - without modifications - with the following arguments:

                    --token XXX --account YYY --data0 EUR_USD

                    Best regards,
                    Tamás

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

                      Oanda doesn't support Tick historical downloads and you are not specifying neither a historical download nor a specific timeframe/compression (for example: Minutes/5) resampling.

                      The sample is not a swiss-knife for each and every possible use case, it is just a play tool.

                      1 Reply Last reply Reply Quote 0
                      • H
                        holicst last edited by

                        Hi Backtrader,

                        Many thanks for the info. Now I managet to get past this error but now I receive a notification from Oanda:

                        --------------------------------------------------
                        Strategy Created
                        --------------------------------------------------
                        Datetime, Open, High, Low, Close, Volume, OpenInterest, SMA
                        ***** DATA NOTIF: NOTSUBSCRIBED
                        

                        Do I need to subscribe to instruments at Oanda?

                        Thanks and BR,
                        Tamás

                        chuka 1 Reply Last reply Reply Quote 0
                        • H
                          holicst last edited by

                          Hi Everybody,

                          I "fixed it": I used my user name instead of the account number... :D

                          I hope you haven't debugged this for hours :)

                          Cheers,
                          Tamás

                          1 Reply Last reply Reply Quote 0
                          • H
                            holicst last edited by

                            Hi Everybody,

                            I finally managed to use oandatest.py properly now, but I faced two problems today:

                            1, The program stops with the following error, when I want to get the data reference using self.getdatabyname():

                            -- Contract Details:
                            {'displayName': 'EUR/CZK', 'instrument': 'EUR_CZK', 'maxTradeUnits': 10000000, 'pip': '0.0001'}
                            Datetime, Open, High, Low, Close, Volume, OpenInterest, SMA
                            ***** DATA NOTIF: DELAYED
                            Data0, 0001, 733775.787500, 2010-01-03T18:54:00.000000, 26.399000, 26.399000, 26.387500, 26.387500,      3, 0, nan
                            Traceback (most recent call last):
                              File "C:\Program Files\Brainwy\LiClipse 3.4.0\plugins\org.python.pydev_5.5.0.201701191611\pysrc\pydevd.py", line 1537, in <module>
                                globals = debugger.run(setup['file'], None, None, is_module)
                              File "C:\Program Files\Brainwy\LiClipse 3.4.0\plugins\org.python.pydev_5.5.0.201701191611\pysrc\pydevd.py", line 976, in run
                                pydev_imports.execfile(file, globals, locals)  # execute the script
                              File "C:\Program Files\Brainwy\LiClipse 3.4.0\plugins\org.python.pydev_5.5.0.201701191611\pysrc\_pydev_imps\_pydev_execfile.py", line 25, in execfile
                                exec(compile(contents+"\n", file, 'exec'), glob, loc)
                              File "C:\Baixinho\oanda_live.py", line 500, in <module>
                                runstrategy()
                              File "C:\Baixinho\oanda_live.py", line 312, in runstrategy
                                cerebro.run(exactbars=args.exactbars)
                              File "C:\Users\peter\Anaconda3\lib\site-packages\backtrader\cerebro.py", line 873, in run
                                runstrat = self.runstrategies(iterstrat)
                              File "C:\Users\peter\Anaconda3\lib\site-packages\backtrader\cerebro.py", line 1010, in runstrategies
                                self._runnext(runstrats)
                              File "C:\Users\peter\Anaconda3\lib\site-packages\backtrader\cerebro.py", line 1326, in _runnext
                                strat._next()
                              File "C:\Users\peter\Anaconda3\lib\site-packages\backtrader\strategy.py", line 296, in _next
                                super(Strategy, self)._next()
                              File "C:\Users\peter\Anaconda3\lib\site-packages\backtrader\lineiterator.py", line 258, in _next
                                self.prenext()
                              File "C:\Baixinho\oanda_live.py", line 88, in prenext
                                self.next(frompre=True)
                              File "C:\Baixinho\oanda_live.py", line 106, in next
                                d = self.getdatabyname('EUR_CZK')
                              File "C:\Users\peter\Anaconda3\lib\site-packages\backtrader\strategy.py", line 556, in getdatabyname
                                return self.env.datasbyname[name]
                            KeyError: 'EUR_CZK'
                            

                            2, There is no method get_cash() for OandaBroker:

                            
                            Traceback (most recent call last):
                              File "C:\Baixinho\oanda_live.py", line 501, in <module>
                                runstrategy()
                              File "C:\Baixinho\oanda_live.py", line 313, in runstrategy
                                cerebro.run(exactbars=args.exactbars)
                              File "C:\Users\peter\Anaconda3\lib\site-packages\backtrader\cerebro.py", line 873, in run
                                runstrat = self.runstrategies(iterstrat)
                              File "C:\Users\peter\Anaconda3\lib\site-packages\backtrader\cerebro.py", line 1010, in runstrategies
                                self._runnext(runstrats)
                              File "C:\Users\peter\Anaconda3\lib\site-packages\backtrader\cerebro.py", line 1326, in _runnext
                                strat._next()
                              File "C:\Users\peter\Anaconda3\lib\site-packages\backtrader\strategy.py", line 296, in _next
                                super(Strategy, self)._next()
                              File "C:\Users\peter\Anaconda3\lib\site-packages\backtrader\lineiterator.py", line 258, in _next
                                self.prenext()
                              File "C:\Baixinho\oanda_live.py", line 88, in prenext
                                self.next(frompre=True)
                              File "C:\Baixinho\oanda_live.py", line 106, in next
                                print(str(self.broker.get_cash()))
                            AttributeError: 'OandaBroker' object has no attribute 'get_cash'
                            

                            Both are executed in the next() method:

                                def next(self, frompre=False):
                                    txt = list()
                                    txt.append('Data0')
                                    txt.append('%04d' % len(self.data0))
                                    dtfmt = '%Y-%m-%dT%H:%M:%S.%f'
                                    txt.append('{:f}'.format(self.data.datetime[0]))
                                    txt.append('%s' % self.data.datetime.datetime(0).strftime(dtfmt))
                                    txt.append('{:f}'.format(self.data.open[0]))
                                    txt.append('{:f}'.format(self.data.high[0]))
                                    txt.append('{:f}'.format(self.data.low[0]))
                                    txt.append('{:f}'.format(self.data.close[0]))
                                    txt.append('{:6d}'.format(int(self.data.volume[0])))
                                    txt.append('{:d}'.format(int(self.data.openinterest[0])))
                                    txt.append('{:f}'.format(self.sma[0]))
                                    print(', '.join(txt))
                            
                                    print(str(self.broker.get_cash()))
                                    d = self.getdatabyname('EUR_CZK')
                            

                            Am I doing something wrong?

                            Thanks and best regards,
                            Tamás

                            B 1 Reply Last reply Reply Quote 0
                            • H
                              holicst last edited by

                              Hi,

                              Additionally, I found that self.datas[0]._name does not exist with Oanda, but there is self.datas[0]._dataname. And vice versa, so depending on my data source, I have to use different code. Would it be possible to have an attribute for all data sources (IB, VC, Oanda, Pandas etc.) with the same name, holding the name of the instrument?

                              The only other way is for me to write a getname() method that returns either _name or _dataname based on the data source, right?

                              Cheers,
                              Tamás

                              B 1 Reply Last reply Reply Quote 0
                              • H
                                holicst last edited by

                                Hi,

                                Sorry, I don't want to spam this topic but I encountered another error:

                                Exception in thread Thread-60:
                                Traceback (most recent call last):
                                  File "C:\Users\peter\Anaconda3\lib\threading.py", line 914, in _bootstrap_inner
                                    self.run()
                                  File "C:\Users\peter\Anaconda3\lib\threading.py", line 862, in run
                                    self._target(*self._args, **self._kwargs)
                                  File "C:\Users\peter\Anaconda3\lib\site-packages\backtrader\stores\oandastore.py", line 379, in _t_candles
                                    **dtkwargs)
                                  File "C:\Users\peter\Anaconda3\lib\site-packages\oandapy\oandapy.py", line 38, in get_history
                                    return self.request(endpoint, params=params)
                                  File "C:\Users\peter\Anaconda3\lib\site-packages\backtrader\stores\oandastore.py", line 89, in request****
                                    content = json.loads(content)
                                  File "C:\Users\peter\Anaconda3\lib\json\__init__.py", line 319, in loads
                                    return _default_decoder.decode(s)
                                  File "C:\Users\peter\Anaconda3\lib\json\decoder.py", line 339, in decode
                                    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
                                  File "C:\Users\peter\Anaconda3\lib\json\decoder.py", line 357, in raw_decode
                                    raise JSONDecodeError("Expecting value", s, err.value) from None
                                json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
                                
                                

                                I subscribed for all 70 forex instruments Oanda offers, could this be a problem?

                                Best regards,
                                Tamás

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

                                  @holicst said in Oanda test:

                                  I subscribed for all 70 forex instruments Oanda offers, could this be a problem?

                                  That's for sure outside of the backtrader scope. The messages returned by Oanda are in JSON format. Something was very wrong in that request.

                                  Notwithstanding that the code could be more robust.

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

                                    @holicst said in Oanda test:

                                    Hi,
                                    Additionally, I found that self.datas[0]._name does not exist with Oanda

                                    You probably mean that _name is the empty string. This is so because you haven't added a name during adddata(data, name='xxxx')

                                    • _dataname is what was specified as ticker for the specific data source.

                                    • _name is what specified by the user (call it 'alias')

                                    That's the same reason that breaks getdatabyname. In any case it seems reasonable to make a default assignment of the actual dataname to name if nothing has been specified.

                                    1 Reply Last reply Reply Quote 0
                                    • H
                                      holicst last edited by backtrader

                                      Hi,

                                      Thanks for the quick answers. I've adjusted the programs and the _name stuff is solved.

                                      Do you maybe have plans to tune the performance for subscribing to 100 instruments? Or should I try to take care of it and feed my data to my strategy?

                                      Cheers,
                                      Tamás

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

                                        @holicst said in Oanda test:

                                        There is no method get_cash() for OandaBroker:

                                        The canonical name was getcash (and also getvalue) but along the way the simulation broker got the aliases get_cash and get_value. The latest commit in the development branch supports always filling in a _name if none was provided by the user and adding the get_cash and get_value aliases if missing in broker subclasses.

                                        @holicst said in Oanda test:

                                        Do you maybe have plans to tune the performance for subscribing to 100 instruments? Or should I try to take care of it and feed my data to my strategy?

                                        100 or 1000 instruments in backtesting mode should be no problem because the data feeds are (there could be exceptions) taken from frozen data and provide prices when requested to.

                                        Without knowing what the actual json decoder error above is (and what causes it) there is not an actual performance to be tuned. The explanation:

                                        • Many platforms choose a 100% event oriented approach. A tick comes in, a tick is delivered. If you have 70 instruments, there will be 70 ticks.

                                        To overcome this, some platforms add this constraint:

                                        • Only deliver ticks every x milliseconds. This buffers some of the ticks and reduces the number of calls, but obviously delays some of the ticks. But the delivery is not tied to the resampling target

                                        backtrader takes another approach which favors working with (so to say) resampled data rather than ticks. Considering a 1-minute timeframe, backtrader resamples all data feeds to the 1-minute timeframe and delivers all of them at the same time, because it is synchronizing the data feeds.

                                        Each approach has advantages and disadvantages.

                                        Latest commit: https://github.com/mementum/backtrader/commit/e79404a4db3aaf4d7dea841cb4505dec92adae78

                                        H 1 Reply Last reply Reply Quote 0
                                        • H
                                          holicst @backtrader last edited by backtrader

                                          @backtrader Hi,

                                          This is great. I'll try to figure out what the problem is today.

                                          Cheers,
                                          Tamás

                                          1 Reply Last reply Reply Quote 0
                                          • T
                                            Taewoo Kim last edited by Taewoo Kim

                                            get_orders_open() seems to be missing from OandaBroker too

                                            AttributeError: 'OandaBroker' object has no attribute 'get_orders_open'
                                            

                                            Is it also possible to get bracket_order information (including stopprice and limitprice) passed on to the broker as well?

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