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/

    timer problem

    General Discussion
    2
    7
    67
    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.
    • Li Mike
      Li Mike last edited by

      Dear Backtrader, I am testing timer. It seems the notify_timer is triggered on wrong. In the follow code, I expect it triggers on first day on each month.
      But the output is as follows, you can see that : timer 2013-02-28, 2013-03-28, 2013-04-30 is wrong. Am I missing something. The problem seems relate to data. you can downloan the code and data at this link.
      link:https://pan.baidu.com/s/1mgH4nuPEoiRrSdiGLAXM6w
      password:3n45

      output:
      ticker AAPL
      ticker XOM
      timer 2012-02-28
      timer 2012-03-01
      timer 2012-04-02
      timer 2012-05-01
      timer 2012-06-01
      timer 2012-07-02
      timer 2012-08-01
      timer 2012-09-04
      timer 2012-10-01
      timer 2012-11-01
      timer 2012-12-03
      timer 2013-01-02
      timer 2013-02-01
      timer 2013-02-28
      timer 2013-03-28
      timer 2013-04-30
      timer 2013-05-31
      timer 2013-06-28
      timer 2013-07-31
      timer 2013-08-30
      timer 2013-09-30
      timer 2013-10-31
      ...

      from datetime import datetime
      import pandas as pd
      import backtrader as bt
      import os.path 
      import sys  
      
      
      
      modpath = os.path.dirname(os.path.abspath(sys.argv[0]))
      
      datapath = os.path.join(modpath, './survivorship-free/tickers.csv')
      tickers = pd.read_csv(datapath, header=None)[1].tolist()
      
      maxtickersNum = 2
      tickers = tickers[0:maxtickersNum] 
      
      ####################################
      
      
      class Strategy(bt.Strategy):
      
      
          def __init__(self): 
              
      
              self.add_timer(
                  when=bt.Timer.SESSION_START,
                  monthdays=[1], 
                  monthcarry=True, 
                  
              )
      
             
      
          def notify_timer(self, timer, when, *args, **kwargs):        
              print('timer',self.data0.datetime.date(0))
      
      
          
      
      
      ##########################
      # main
      #########################
      
      cerebro = bt.Cerebro(stdstats=False)
      
      
      spy = bt.feeds.YahooFinanceCSVData(
          dataname=os.path.join(modpath, './survivorship-free/SPY.csv'),
          fromdate=datetime(2012, 2, 28),
          todate=datetime(2018, 2, 28),
          plot=False)
      cerebro.adddata(spy)  # S&P 500
      
      for ticker in tickers:
          print('ticker', ticker)
          df = pd.read_csv(
              f"survivorship-free/{ticker}.csv", parse_dates=True, index_col=0)
          
          data = bt.feeds.PandasData(
              dataname=df,
              datetime=None,  
              open=0, 
              high=1,  
              low=2,  
              close=3,  
              volume=4,  
              openinterest=-1,  
              plot=False
      
          )
          
          if len(df) > 100: 
              cerebro.adddata(data)
      
      cerebro.addstrategy(Strategy)
      results = cerebro.run()
      
      
      
      1 Reply Last reply Reply Quote 0
      • vladisld
        vladisld last edited by

        Could it be the same problem as described in the following post ? : https://community.backtrader.com/topic/3093/timer-monthday-reports-trading-on-days-prior-to-target

        Li Mike 1 Reply Last reply Reply Quote 0
        • Li Mike
          Li Mike last edited by

          Seems not the same with that post. I use self.data0.datetime.date(0), but the result is still wrong.

          1 Reply Last reply Reply Quote 0
          • Li Mike
            Li Mike @vladisld last edited by

            @vladisld if output when, the when is correct, see below. Any idea?
            ```
            def notify_timer(self, timer, when, *args, **kwargs):
            print('timer',self.data0.datetime.date(0), 'when', when)

            
            ticker AAPL
            ticker XOM
            timer 2012-02-28 when 2012-02-28 00:00:00
            timer 2012-03-01 when 2012-03-01 00:00:00
            timer 2012-04-02 when 2012-04-02 00:00:00
            timer 2012-05-01 when 2012-05-01 00:00:00
            timer 2012-06-01 when 2012-06-01 00:00:00
            timer 2012-07-02 when 2012-07-02 00:00:00
            timer 2012-08-01 when 2012-08-01 00:00:00
            timer 2012-09-04 when 2012-09-04 00:00:00
            timer 2012-10-01 when 2012-10-01 00:00:00
            timer 2012-11-01 when 2012-11-01 00:00:00
            timer 2012-12-03 when 2012-12-03 00:00:00
            timer 2013-01-02 when 2013-01-02 00:00:00
            timer 2013-02-01 when 2013-02-01 00:00:00
            timer 2013-02-28 when 2013-03-01 00:00:00
            timer 2013-03-28 when 2013-04-01 00:00:00
            timer 2013-04-30 when 2013-05-01 00:00:00
            timer 2013-05-31 when 2013-06-03 00:00:00
            timer 2013-06-28 when 2013-07-01 00:00:00
            timer 2013-07-31 when 2013-08-01 00:00:00
            timer 2013-08-30 when 2013-09-03 00:00:00
            timer 2013-09-30 when 2013-10-01 00:00:00
            1 Reply Last reply Reply Quote 0
            • vladisld
              vladisld last edited by

              I still think it could be related to the time zone difference as described in the referenced post above. Could you please re check? (See the last response in the referenced post)

              Li Mike 1 Reply Last reply Reply Quote 0
              • Li Mike
                Li Mike @vladisld last edited by

                @vladisld When I use

                def notify_timer(self, timer, when, *args, **kwargs):        
                       print('timer',self.data0.datetime.date(0), self.data0.datetime.datetime(0), 'when', when)
                

                Then output is as follows. See the last row. I use daily bars. I found that if we use data = bt.feeds.PandasData(...), then the datetime of the data bar is for example 2013-03-28 00:00:00, not 2013-03-28 23:59:59. This seems cause the problem. Any idea?

                timer 2012-02-28 2012-02-28 23:59:59.999989 when 2012-02-28 00:00:00
                timer 2012-03-01 2012-03-01 23:59:59.999989 when 2012-03-01 00:00:00
                timer 2012-04-02 2012-04-02 23:59:59.999989 when 2012-04-02 00:00:00
                timer 2012-05-01 2012-05-01 23:59:59.999989 when 2012-05-01 00:00:00
                timer 2012-06-01 2012-06-01 23:59:59.999989 when 2012-06-01 00:00:00
                timer 2012-07-02 2012-07-02 23:59:59.999989 when 2012-07-02 00:00:00
                timer 2012-08-01 2012-08-01 23:59:59.999989 when 2012-08-01 00:00:00
                timer 2012-09-04 2012-09-04 23:59:59.999989 when 2012-09-04 00:00:00
                timer 2012-10-01 2012-10-01 23:59:59.999989 when 2012-10-01 00:00:00
                timer 2012-11-01 2012-11-01 23:59:59.999989 when 2012-11-01 00:00:00
                timer 2012-12-03 2012-12-03 23:59:59.999989 when 2012-12-03 00:00:00
                timer 2013-01-02 2013-01-02 23:59:59.999989 when 2013-01-02 00:00:00
                timer 2013-02-01 2013-02-01 23:59:59.999989 when 2013-02-01 00:00:00
                timer 2013-02-28 2013-02-28 23:59:59.999989 when 2013-03-01 00:00:00
                timer 2013-03-28 2013-03-28 23:59:59.999989 when 2013-04-01 00:00:00

                1 Reply Last reply Reply Quote 0
                • vladisld
                  vladisld last edited by

                  good point - you may be right. I'll try to debug it further - will update.

                  1 Reply Last reply Reply Quote 0
                  • 1 / 1
                  • First post
                    Last post
                  Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
                  $(document).ready(function () { app.coldLoad(); }); }