Backtrader Community

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

    jguerre5

    @jguerre5

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

    jguerre5 Unfollow Follow

    Latest posts made by jguerre5

    • Time Management Question

      I've got minute spaced data which I want to perform some testing and analysis on. One idea that I wanted to try involved using the data from the first minute of each day to make decisions on the rest of the day. That means I want to save the data from the first minute and then compare it to current values in the data. this is my data: 0_1554921835487_data.PNG
      and this is what my code looks like right now:

      class TestStrategy(bt.Strategy):
      
          def log(self, txt, dt=None):
              ''' Logging function for this strategy'''
              dt = dt or self.datas[0].datetime.date(0)
              print('%s, %s' % (dt.isoformat(), txt))
      
          def __init__(self):
              self.range = self.datas[0].high-self.datas[0].low
              self.datalow = self.datas[0].low
              self.datahigh = self.datas[0].high
              if self.data.datetime.time() == datetime.time(9, 31):
                inithigh=self.datas[0].high
                initlow=self.datas[0].low
      
          def next(self):
          
              if self.data.datetime.time() > datetime.time(9, 31):
                if self.datalow<initlow:
                  self.buy()
                elif self.datahigh>inithigh:
                  self.sell()
      
      if __name__ == '__main__':
          
          cerebro = bt.Cerebro()
      
          cerebro.addstrategy(TestStrategy)
      
          data = bt.feeds.GenericCSVData(
              dataname="NYSETICK.txt",
              separator="\t",
              timeframe=bt.TimeFrame.Minutes,
              fromdate=datetime.datetime(2019, 2, 14),
              todate=datetime.datetime(2018, 2, 14),
              dtformat=('%Y-%m-%d'),
              tmformat=('%H:%M:%S'),
              datetime=0,
              time=1,
              open=4,
              high=5,
              low=6,
              close=7,
              volume=-1,
              openinterest=-1,
              reverse=True)
          
          cerebro.adddata(data)
      
          cerebro.broker.setcash(100000.0)
      
          print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
      
          cerebro.run()
      
          print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
      

      This code yields this error which I haven't yet been able to fix:
      0_1554921997420_error.PNG

      Could you help me figure out whats causing the error?

      posted in General Discussion
      J
      jguerre5
    • RE: Error due to time management

      Could you point me to the general section of the code that's causing the problem? Alternatively I could paste my code as text.

      posted in General Code/Help
      J
      jguerre5
    • Error due to time management

      Given csv data that looks like this:
      0_1552519781029_data.PNG
      I want to save the low/high at 9:31(the earliest time for each day) and use it to compare against current values. This is what my implementation looks like right now:
      0_1552519913005_strategy.PNG
      0_1552519920989_impl.PNG
      I get this error, but I can't figure out exactly what is causing it:
      0_1552520057905_error.PNG
      Is there anything glaringly wrong with what I did? What do I need to do to get the desired effect? Thanks.

      posted in General Code/Help
      J
      jguerre5