Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    1. Home
    2. Ajay Nainani
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
    • Profile
    • Following 0
    • Followers 0
    • Topics 0
    • Posts 3
    • Best 0
    • Groups 0

    Ajay Nainani

    @Ajay Nainani

    0
    Reputation
    1
    Profile views
    3
    Posts
    0
    Followers
    0
    Following
    Joined Last Online

    Ajay Nainani Unfollow Follow

    Latest posts made by Ajay Nainani

    • RE: Can get backtrader to buy on the 1st of the month

      Modified data to have EOD time stamp attached. Still doesn't work. Output:

                           Open      High       Low       Close     Volume
      

      Date
      2000-01-03 17:30:00 101.3166 101.3166 98.3267 99.394493 8164300
      2000-01-04 17:30:00 98.0917 98.4548 95.4328 95.507530 8089800
      2000-01-05 17:30:00 95.6357 96.7249 93.7990 95.678391 12177900
      2000-01-06 17:30:00 95.4221 96.7035 94.1407 94.140717 6227200
      2000-01-07 17:30:00 95.8920 99.6081 95.7211 99.608055 8066500
      ... ... ... ... ... ...
      2020-04-20 17:30:00 282.6100 286.7900 281.3500 281.589996 100109300
      2020-04-21 17:30:00 276.7300 278.0400 272.0200 273.040009 126385700
      2020-04-22 17:30:00 278.3500 281.0000 276.9100 279.100006 93524600
      2020-04-23 17:30:00 280.4900 283.9400 278.7500 279.079987 104709700
      2020-04-24 17:30:00 280.7300 283.7000 278.5000 282.970001 85063200

      [5110 rows x 5 columns]
      ROI: 0.00%

      posted in General Code/Help
      Ajay Nainani
      Ajay Nainani
    • RE: Can get backtrader to buy on the 1st of the month

      Is it possible that this is due to the date format of the pandas date? i.e. there is no time in my data source, vs in the examples on

      https://www.backtrader.com/docu/timers/timers/

      they all have times attached to their dates.

      posted in General Code/Help
      Ajay Nainani
      Ajay Nainani
    • RE: Can get backtrader to buy on the 1st of the month

      Having the same problem! Sharing whole code snippet below.

      import datetime
      import backtrader as bt
      
      class BuyAndHold_More(bt.Strategy):
          params = dict(
              monthly_cash=1000.0,  # amount of cash to buy every month
          )
      
          def start(self):
              self.cash_start = self.broker.get_cash()
              self.val_start = 100.0
      
              # Add a timer which will be called on the 1st trading day of the month
              self.add_timer(
                  bt.timer.SESSION_END,  # when it will be called
                  monthdays=[1],  # called on the 1st day of the month
                  monthcarry=True,  # called on the 2nd day if the 1st is holiday
              )
      
          def notify_timer(self, timer, when, *args, **kwargs):
              # Add the influx of monthly cash to the broker
              self.broker.add_cash(self.p.monthly_cash)
              print('Cash Added!')
              # buy available cash
              target_value = self.broker.get_value() + self.p.monthly_cash
              self.order_target_value(target=target_value)
      
          def stop(self):
              # calculate the actual returns
              self.roi = (self.broker.get_value() / self.cash_start) - 1.0
              print('ROI:        {:.2f}%'.format(self.roi))
      
      
      if __name__ == '__main__':
          cerebro = bt.Cerebro()
      
         # Get a pandas dataframe
      
          class PandasData(bt.feeds.DataBase):
            #lines = ('adj_close')
            params = (
                ('datetime', 'Date'),
                ('open','Open'),
                ('high','High'),
                ('low','Low'),
                ('close','Close'),
                ('volume','Volume'),
                ('openinterest',None),
                #('adj_close','Adj Close'),
              )
              
          # import data
          print(df_new)
          adj_data = bt.feeds.PandasData(dataname=df_new)
          
          #add data to cerebro
          cerebro.adddata(adj_data)
      
          cerebro.addstrategy(BuyAndHold_More)
      
          # Execute
          cerebro.run()
      
      

      Output when run. Note that my "Cash Added!" never prints, so it looks like the timer is never getting run.

                  Open      High       Low       Close     Volume
      

      Date
      2000-01-03 101.3166 101.3166 98.3267 99.394493 8164300
      2000-01-04 98.0917 98.4548 95.4328 95.507530 8089800
      2000-01-05 95.6357 96.7249 93.7990 95.678391 12177900
      2000-01-06 95.4221 96.7035 94.1407 94.140717 6227200
      2000-01-07 95.8920 99.6081 95.7211 99.608055 8066500
      ... ... ... ... ... ...
      2020-04-20 282.6100 286.7900 281.3500 281.589996 100109300
      2020-04-21 276.7300 278.0400 272.0200 273.040009 126385700
      2020-04-22 278.3500 281.0000 276.9100 279.100006 93524600
      2020-04-23 280.4900 283.9400 278.7500 279.079987 104709700
      2020-04-24 280.7300 283.7000 278.5000 282.970001 85063200

      [5110 rows x 5 columns]
      ROI: 0.00%

      posted in General Code/Help
      Ajay Nainani
      Ajay Nainani