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/

    Plot is no working, and I don't know why. Help please!

    General Code/Help
    2
    5
    56
    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.
    • H
      HoloDevOne last edited by

      Hello, I am a newbie here. First, thanks for the framework and the work put in it.

      I'm still doing the samples from the quickstart guide, but I can't plot the prices or anything.

      I had the Matplotlib 'warnings' error, and I delete it from the plot/locator.py. as I read in another post, but now I had no error, just didn't plot anything.

      This is my code:

      from __future__ import (absolute_import, division, print_function,
                              unicode_literals)
      
      import argparse
      import datetime
      import backtrader as bt
      # from backtrader.utils import flushfile  # win32 quick stdout flushing
      import json
      import btoandav20 as bto
      import sma_crossover as st
      
      
      class TestStrategy(bt.Strategy):
      
          def log(self, txt, dt=None):
              ''' Logging function for this strategy'''
              dt = dt or self.datas[0].datetime.date(0) # Ver como incluir las horas. ???
              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, %.4f' % self.dataclose[0])
      
      
      
      with open("config.json", "r") as file:
          config = json.load(file)
      
      storekwargs = dict(
          token=config["oanda"]["token"],
          account=config["oanda"]["account"],
          practice=config["oanda"]["practice"],
          notif_transactions=True,
          stream_timeout=10,
      )
      store = bto.stores.OandaV20Store(**storekwargs)
      
      datakwargs = dict(
          timeframe=bt.TimeFrame.Days,
          compression=1,
          tz='America/Bogota',
          historical=True,
          fromdate=datetime.datetime(2021, 1, 1),
          todate=datetime.datetime.today(),
          backfill=False,
          backfill_start=True
      )
      
      data = store.getdata(dataname="EUR_USD", **datakwargs)
      data.resample(
          timeframe=bt.TimeFrame.Days,
          compression=1)  # rightedge=True, boundoff=1)
      
      
      cerebro = bt.Cerebro()
      cerebro.broker.setcash(500.0)
      print('Starting Portfolio Value:  %.2f' % cerebro.broker.getvalue())
      cerebro.adddata(data)
      #cerebro.setbroker()  # tal vez no deba cargar el broker aun
      cerebro.addstrategy(TestStrategy)  # Utilizo estrategia comodin
      cerebro.run()
      print(data)
      print('Final Portfolio Value; %.2f' % cerebro.broker.getvalue())
      cerebro.plot()
      

      I'm taking the data from Oanda directly:

      Starting Portfolio Value:  500.00
      2020-01-02, Close, 1.1172
      2020-01-03, Close, 1.1154
      2020-01-06, Close, 1.1196
      2020-01-07, Close, 1.1146
      2020-01-08, Close, 1.1100
      2020-01-09, Close, 1.1105
      2020-01-10, Close, 1.1116
      2020-01-13, Close, 1.1133
      2020-01-14, Close, 1.1124
      2020-01-15, Close, 1.1149
      2020-01-16, Close, 1.1134
      2020-01-17, Close, 1.1087
      2020-01-20, Close, 1.1094
      2020-01-21, Close, 1.1081
      2020-01-22, Close, 1.1091
      2020-01-23, Close, 1.1051
      2020-01-24, Close, 1.1022
      2020-01-27, Close, 1.1018
      2020-01-28, Close, 1.1021
      2020-01-29, Close, 1.1009
      2020-01-30, Close, 1.1030
      2020-01-31, Close, 1.1090
      2020-02-03, Close, 1.1056
      2020-02-04, Close, 1.1043
      2020-02-05, Close, 1.0998
      2020-02-06, Close, 1.0980
      2020-02-07, Close, 1.0943
      2020-02-10, Close, 1.0910
      2020-02-11, Close, 1.0915
      2020-02-12, Close, 1.0872
      2020-02-13, Close, 1.0839
      2020-02-14, Close, 1.0829
      2020-02-17, Close, 1.0831
      2020-02-18, Close, 1.0791
      2020-02-19, Close, 1.0805
      2020-02-20, Close, 1.0783
      2020-02-21, Close, 1.0846
      2020-02-24, Close, 1.0852
      2020-02-25, Close, 1.0880
      2020-02-26, Close, 1.0879
      2020-02-27, Close, 1.1000
      2020-02-28, Close, 1.1023
      2020-03-02, Close, 1.1132
      2020-03-03, Close, 1.1169
      2020-03-04, Close, 1.1132
      2020-03-05, Close, 1.1236
      2020-03-06, Close, 1.1280
      2020-03-09, Close, 1.1435
      2020-03-10, Close, 1.1278
      2020-03-11, Close, 1.1268
      2020-03-12, Close, 1.1184
      2020-03-13, Close, 1.1100
      2020-03-16, Close, 1.1179
      2020-03-17, Close, 1.0996
      2020-03-18, Close, 1.0911
      2020-03-19, Close, 1.0690
      2020-03-20, Close, 1.0691
      2020-03-23, Close, 1.0722
      2020-03-24, Close, 1.0786
      2020-03-25, Close, 1.0880
      2020-03-26, Close, 1.1031
      2020-03-27, Close, 1.1137
      2020-03-30, Close, 1.1043
      2020-03-31, Close, 1.1029
      2020-04-01, Close, 1.0961
      2020-04-02, Close, 1.0855
      2020-04-03, Close, 1.0801
      2020-04-06, Close, 1.0791
      2020-04-07, Close, 1.0891
      2020-04-08, Close, 1.0854
      2020-04-09, Close, 1.0928
      2020-04-10, Close, 1.0931
      2020-04-13, Close, 1.0906
      2020-04-14, Close, 1.0978
      2020-04-15, Close, 1.0906
      2020-04-16, Close, 1.0837
      2020-04-17, Close, 1.0869
      2020-04-20, Close, 1.0862
      2020-04-21, Close, 1.0855
      2020-04-22, Close, 1.0823
      2020-04-23, Close, 1.0776
      2020-04-24, Close, 1.0818
      2020-04-27, Close, 1.0827
      2020-04-28, Close, 1.0819
      2020-04-29, Close, 1.0871
      2020-04-30, Close, 1.0951
      2020-05-01, Close, 1.0974
      2020-05-04, Close, 1.0900
      2020-05-05, Close, 1.0836
      2020-05-06, Close, 1.0794
      2020-05-07, Close, 1.0831
      2020-05-08, Close, 1.0833
      2020-05-11, Close, 1.0805
      2020-05-12, Close, 1.0846
      2020-05-13, Close, 1.0818
      2020-05-14, Close, 1.0804
      2020-05-15, Close, 1.0815
      2020-05-18, Close, 1.0913
      2020-05-19, Close, 1.0922
      2020-05-20, Close, 1.0978
      2020-05-21, Close, 1.0949
      2020-05-22, Close, 1.0897
      2020-05-25, Close, 1.0894
      2020-05-26, Close, 1.0981
      2020-05-27, Close, 1.1008
      2020-05-28, Close, 1.1076
      2020-05-29, Close, 1.1100
      2020-06-01, Close, 1.1133
      2020-06-02, Close, 1.1168
      2020-06-03, Close, 1.1232
      2020-06-04, Close, 1.1335
      2020-06-05, Close, 1.1290
      2020-06-08, Close, 1.1293
      2020-06-09, Close, 1.1338
      2020-06-10, Close, 1.1371
      2020-06-11, Close, 1.1297
      2020-06-12, Close, 1.1250
      2020-06-15, Close, 1.1322
      2020-06-16, Close, 1.1262
      2020-06-17, Close, 1.1242
      2020-06-18, Close, 1.1204
      2020-06-19, Close, 1.1176
      2020-06-22, Close, 1.1257
      2020-06-23, Close, 1.1308
      2020-06-24, Close, 1.1249
      2020-06-25, Close, 1.1217
      2020-06-26, Close, 1.1216
      2020-06-29, Close, 1.1241
      2020-06-30, Close, 1.1232
      2020-07-01, Close, 1.1250
      2020-07-02, Close, 1.1238
      2020-07-03, Close, 1.1243
      2020-07-06, Close, 1.1308
      2020-07-07, Close, 1.1273
      2020-07-08, Close, 1.1329
      2020-07-09, Close, 1.1284
      2020-07-10, Close, 1.1298
      2020-07-13, Close, 1.1342
      2020-07-14, Close, 1.1399
      2020-07-15, Close, 1.1411
      2020-07-16, Close, 1.1383
      2020-07-17, Close, 1.1425
      2020-07-20, Close, 1.1446
      2020-07-21, Close, 1.1526
      2020-07-22, Close, 1.1569
      2020-07-23, Close, 1.1595
      2020-07-24, Close, 1.1654
      2020-07-27, Close, 1.1750
      2020-07-28, Close, 1.1714
      2020-07-29, Close, 1.1790
      2020-07-30, Close, 1.1846
      2020-07-31, Close, 1.1775
      2020-08-03, Close, 1.1761
      2020-08-04, Close, 1.1801
      2020-08-05, Close, 1.1863
      2020-08-06, Close, 1.1876
      2020-08-07, Close, 1.1784
      2020-08-10, Close, 1.1736
      2020-08-11, Close, 1.1739
      2020-08-12, Close, 1.1783
      2020-08-13, Close, 1.1813
      2020-08-14, Close, 1.1840
      2020-08-17, Close, 1.1869
      2020-08-18, Close, 1.1930
      2020-08-19, Close, 1.1836
      2020-08-20, Close, 1.1859
      2020-08-21, Close, 1.1795
      2020-08-24, Close, 1.1786
      2020-08-25, Close, 1.1833
      2020-08-26, Close, 1.1829
      2020-08-27, Close, 1.1821
      2020-08-28, Close, 1.1903
      2020-08-31, Close, 1.1935
      2020-09-01, Close, 1.1911
      2020-09-02, Close, 1.1854
      2020-09-03, Close, 1.1851
      2020-09-04, Close, 1.1837
      2020-09-07, Close, 1.1811
      2020-09-08, Close, 1.1773
      2020-09-09, Close, 1.1801
      2020-09-10, Close, 1.1813
      2020-09-11, Close, 1.1845
      2020-09-14, Close, 1.1863
      2020-09-15, Close, 1.1846
      2020-09-16, Close, 1.1815
      2020-09-17, Close, 1.1847
      2020-09-18, Close, 1.1838
      2020-09-21, Close, 1.1770
      2020-09-22, Close, 1.1707
      2020-09-23, Close, 1.1659
      2020-09-24, Close, 1.1662
      2020-09-25, Close, 1.1628
      2020-09-28, Close, 1.1659
      2020-09-29, Close, 1.1742
      2020-09-30, Close, 1.1719
      2020-10-01, Close, 1.1745
      2020-10-02, Close, 1.1715
      2020-10-05, Close, 1.1784
      2020-10-06, Close, 1.1733
      2020-10-07, Close, 1.1761
      2020-10-08, Close, 1.1759
      2020-10-09, Close, 1.1823
      2020-10-12, Close, 1.1810
      2020-10-13, Close, 1.1745
      2020-10-14, Close, 1.1745
      2020-10-15, Close, 1.1706
      2020-10-16, Close, 1.1712
      2020-10-19, Close, 1.1769
      2020-10-20, Close, 1.1821
      2020-10-21, Close, 1.1859
      2020-10-22, Close, 1.1818
      2020-10-23, Close, 1.1859
      2020-10-26, Close, 1.1808
      2020-10-27, Close, 1.1794
      2020-10-28, Close, 1.1745
      2020-10-29, Close, 1.1671
      2020-10-30, Close, 1.1643
      2020-11-02, Close, 1.1640
      2020-11-03, Close, 1.1718
      2020-11-04, Close, 1.1719
      2020-11-05, Close, 1.1825
      2020-11-06, Close, 1.1870
      2020-11-09, Close, 1.1813
      2020-11-10, Close, 1.1814
      2020-11-11, Close, 1.1778
      2020-11-12, Close, 1.1804
      2020-11-13, Close, 1.1832
      2020-11-16, Close, 1.1852
      2020-11-17, Close, 1.1860
      2020-11-18, Close, 1.1853
      2020-11-19, Close, 1.1873
      2020-11-20, Close, 1.1855
      2020-11-23, Close, 1.1840
      2020-11-24, Close, 1.1892
      2020-11-25, Close, 1.1912
      2020-11-26, Close, 1.1910
      2020-11-27, Close, 1.1962
      2020-11-30, Close, 1.1923
      2020-12-01, Close, 1.2071
      2020-12-02, Close, 1.2115
      2020-12-03, Close, 1.2147
      2020-12-04, Close, 1.2119
      2020-12-07, Close, 1.2107
      2020-12-08, Close, 1.2103
      2020-12-09, Close, 1.2081
      2020-12-10, Close, 1.2141
      2020-12-11, Close, 1.2107
      2020-12-14, Close, 1.2142
      2020-12-15, Close, 1.2151
      2020-12-16, Close, 1.2198
      2020-12-17, Close, 1.2266
      2020-12-18, Close, 1.2251
      2020-12-21, Close, 1.2242
      2020-12-22, Close, 1.2161
      2020-12-23, Close, 1.2183
      2020-12-24, Close, 1.2183
      2020-12-28, Close, 1.2214
      2020-12-29, Close, 1.2249
      2020-12-30, Close, 1.2298
      2020-12-31, Close, 1.2212
      2021-01-04, Close, 1.2252
      2021-01-05, Close, 1.2297
      2021-01-06, Close, 1.2323
      2021-01-07, Close, 1.2268
      2021-01-08, Close, 1.2216
      2021-01-11, Close, 1.2150
      2021-01-12, Close, 1.2207
      2021-01-13, Close, 1.2157
      2021-01-14, Close, 1.2150
      2021-01-15, Close, 1.2075
      2021-01-18, Close, 1.2075
      2021-01-19, Close, 1.2127
      2021-01-20, Close, 1.2105
      2021-01-21, Close, 1.2167
      2021-01-22, Close, 1.2171
      2021-01-25, Close, 1.2141
      2021-01-26, Close, 1.2159
      2021-01-27, Close, 1.2111
      2021-01-28, Close, 1.2121
      2021-01-29, Close, 1.2136
      2021-02-01, Close, 1.2059
      2021-02-02, Close, 1.2043
      2021-02-03, Close, 1.2034
      2021-02-04, Close, 1.1958
      2021-02-05, Close, 1.2042
      2021-02-08, Close, 1.2047
      2021-02-09, Close, 1.2117
      2021-02-10, Close, 1.2117
      2021-02-11, Close, 1.2128
      2021-02-12, Close, 1.2119
      2021-02-15, Close, 1.2127
      2021-02-16, Close, 1.2104
      <btoandav20.feeds.oandav20feed.OandaV20Data object at 0x7fe21187a820>
      Final Portfolio Value; 500.00
      

      And went it got to this point just fall into an infinite loop or something. I must kill the process.

      Any help must be appreciated. Thanks.

      run-out 1 Reply Last reply Reply Quote 0
      • run-out
        run-out @HoloDevOne last edited by

        @holodevone Your code is working fine on my machine, although I used local data. There's something amis with the installation of your matplotlib I think.

        H 1 Reply Last reply Reply Quote 1
        • H
          HoloDevOne @run-out last edited by

          @run-out Thanks for your help. 😁👍🏻

          I'm still unable to plot. So, I'll continue in the quest for a solution. Seems some problems with Tk and matplotlib are common in the recent versions of MacOs. The Python Launcher just does not finish starting so I need to kill the process.

          I use Mac 10.15, python 3.9, backtrader 1.9.76.123, btoandav20 0.2.0, matplotlib 3.3.4, v20 3.0.25.0.

          run-out 1 Reply Last reply Reply Quote 0
          • run-out
            run-out @HoloDevOne last edited by

            @holodevone There have been some discussion regarding matplotlib versions in the community. Have a look there. Sorry I can't be of more help.

            H 1 Reply Last reply Reply Quote 1
            • H
              HoloDevOne @run-out last edited by

              @run-out Hello, finally I find the issue. I'm using data from Oanda, and seems the problem was the param timeframe in the creation of the data feed. When I setup the timeframe to bt.TimeFrame.Minutes and Compression to 5 it works.

              I share the code, maybe someone can benefit of that!

              Again, thanks for your help!

              data = bt.feeds.GenericCSVData(
                  dataname='data/audusd_M5.csv',
                  fromdate=datetime.datetime(2005, 1, 2, 18, 45),
                  todate=datetime.datetime(2005, 1, 31, 23, 45),
                  timeframe = bt.TimeFrame.Minutes,
                  compression = 5, 
                  index= -1, 
                  dtformat=("%Y-%m-%dT%H:%M:%S.000000000Z"), 
                  datetime=0, 
                  high=1, 
                  low=2, 
                  open=3,
                  close=4, 
                  volume=5, 
                  openinterest=-1
                  
                  )
              
              1 Reply Last reply Reply Quote 1
              • 1 / 1
              • First post
                Last post
              Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
              $(document).ready(function () { app.coldLoad(); }); }