For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

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?


  • administrators

    Let me try to address your problems by asking two simple questions, which I guess will lead you in the right direction

    • How many times do you expect the code to go through __init__?
    • How do you expect local variables from __init__ to be magically available somewhere else?