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



  • This post is very similar to this question.
    I have been following backtrader tutorial, Quickstart Guide, but I don't get an expected result at some point. I will provide the code and my output as well as the expected result.

    import backtrader as bt
    import btoandav20 as bto2
    import datetime
    
    # Create a Stratey
    class TestStrategy(bt.Strategy):
    
        def log(self, txt, dt=None):
            ''' Logging function fot 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 self.dataclose[0] < self.dataclose[-1]:
                # current close less than previous close
    
                if self.dataclose[-1] < self.dataclose[-2]:
                    # previous close less than the previous close
    
                    # BUY, BUY, BUY!!! (with all possible default parameters)
                    self.log('BUY CREATE, %.2f' % self.dataclose[0])
                    self.buy()
    
    
    cerebro = bt.Cerebro()
    
    # Add a strategy
    cerebro.addstrategy(TestStrategy)
    
    # Create oandastore
    oandastore =  bto2.stores.OandaV20Store(token = ACCESS_TOKEN,
                                                            account = ACCOUNT_ID,
                                                            practice=True)
    
    # 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)
    
    cerebro.broker.setcash(100000.0)  # Set buy-in
    print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
    
    cerebro.run()
    print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
    

    <strong>Here is my output</strong>

    Starting Portfolio Value: 100000.00
    Final Portfolio Value: 100000.00
    

    <br>
    <strong>Here is an expected output shown in the tutorial.</strong>

    Starting Portfolio Value: 100000.00
    2000-01-03, Close, 27.85
    2000-01-04, Close, 25.39
    2000-01-05, Close, 24.05
    2000-01-05, BUY CREATE, 24.05
    2000-01-06, Close, 22.63
    2000-01-06, BUY CREATE, 22.63
    2000-01-07, Close, 24.37
    ...
    ...
    ...
    2000-12-20, BUY CREATE, 26.88
    2000-12-21, Close, 27.82
    2000-12-22, Close, 30.06
    2000-12-26, Close, 29.17
    2000-12-27, Close, 28.94
    2000-12-27, BUY CREATE, 28.94
    2000-12-28, Close, 29.29
    2000-12-29, Close, 27.41
    Final Portfolio Value: 99725.08
    

    They use bt.feeds.YahooFinanceCSVData which is the only the difference from my code in which I create bto2.stores.OandaV20Store instance and use oandastore.getdata to get data object.

    I guess the solution of the similar post that I mentioned at the top was to install ujson which I have already installed into my conda env.

    I will provide packages that I think maybe related to this question.

    Python 3.7.0
    backtrader                1.9.74.123
    backtrader-oandav20       0.1.0
    oandapyv20                0.6.3 
    ujson                     1.35
    v20                       3.0.25.0
    

    If anyone can help me to solve, I'd be grateful!



  • @boblef In addition, I have already generated API token on my Oanda account and defined it and account id in my local code so you don't see them in the code I provided.



});