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

Custom data from pandas with pre-calculated signal



  • Hi, I have a custom pandas dataframe with a pre-calculated signal column. The data time frame is daily, so it does not have any time, instead, it has date.
    Data:

    	Date	        open	  high	low	close	volume	Signal
    0	2019-08-24	14645.0	15200.0	14400.0	14824.0	982174	0
    1	2019-08-25	14824.0	15565.0	14824.0	15388.0	1207412	0
    2	2019-08-26	15388.0	16157.0	15617.0	16150.0	560070	0
    3	2019-08-27	16150.0	16957.0	16150.0	16856.0	809007	0
    4	2019-08-28	16856.0	17370.0	16200.0	16948.0	874259	0
    ...	...	...	...	...	...	...	...
    227	2020-08-15	55790.0	55800.0	54000.0	55740.0	2524688	0
    228	2020-08-16	58520.0	58520.0	58520.0	57190.0	491828	0
    229	2020-08-17	60040.0	60040.0	60040.0	58580.0	459756	0
    230	2020-08-18	61500.0	61500.0	61500.0	59880.0	420032	0
    231	2020-08-19	62870.0	62870.0	62870.0	62230.0	
    

    I want to define a strategy to buy or sell on signal value.
    My strategy:

    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):
            # Keep a reference to the "close" line in the data[0] dataseries
            self.dataclose = self.datas[0].close
            self.datasignal = self.datas[0].Signal
            #self.test = 'test'
    
        def next(self):
            # Simply log the closing price of the series from the reference
            self.log('Signal, %.2f' % self.datasignal)
            self.log('test')
            if int(self.datasignal) > 0:
                #print('cheked')
                    # BUY, BUY, BUY!!! (with all possible default parameters)
                self.log('BUY CREATE, %.2f' % self.dataclose[0])
                self.buy()
            if int(self.datasignal) < 0:
                self.log('Sell, %.2f' % self.dataclose[0])
                self.Sell()
    

    and my datafeed class:

    class PandasData(bt.feed.DataBase):
        linesoverride = True  # discard usual OHLC structure
        # datetime must be present and last
        lines = ('Signal',)
        params = (
            ('dtformat', '%Y-%m-%d'),
            ('Date', 0),
            ('time', None),
            ('open', 1),
            ('high', 2),
            ('low', 3),
            ('close', 4),
            ('volume', 5),
            ('Signal', 6),
            ('timeframe', bt.TimeFrame.Days),
            ('compression', 1),
            ('openinterest', None)
        )
    

    Main code:

    ready_data = PandasData(dataname=strategy_data)
    cerebro = bt.Cerebro()
    
    cerebro.addstrategy(TestStrategy)
    cerebro.adddata(ready_data)
    

    And running the cerebro:

    cerebro.broker.setcash(1000000000.0)
    cerebro.run()
    

    gives me this error:

    ---------------------------------------------------------------------------
    AttributeError                            Traceback (most recent call last)
     in 
          1 cerebro.broker.setcash(1000000000.0)
    ----> 2 cerebro.run()
    
    C:\ProgramData\Anaconda3\envs\djg\lib\site-packages\backtrader\cerebro.py in run(self, **kwargs)
       1125             # let's skip process "spawning"
       1126             for iterstrat in iterstrats:
    -> 1127                 runstrat = self.runstrategies(iterstrat)
       1128                 self.runstrats.append(runstrat)
       1129                 if self._dooptimize:
    
    C:\ProgramData\Anaconda3\envs\djg\lib\site-packages\backtrader\cerebro.py in runstrategies(self, iterstrat, predata)
       1208                 if self._exactbars < 1:  # datas can be full length
       1209                     data.extend(size=self.params.lookahead)
    -> 1210                 data._start()
       1211                 if self._dopreload:
       1212                     data.preload()
    
    C:\ProgramData\Anaconda3\envs\djg\lib\site-packages\backtrader\feed.py in _start(self)
        204 
        205         if not self._started:
    --> 206             self._start_finish()
        207 
        208     def _timeoffset(self):
    
    C:\ProgramData\Anaconda3\envs\djg\lib\site-packages\backtrader\feed.py in _start_finish(self)
        172         self._tz = self._gettz()
        173         # Lines have already been create, set the tz
    --> 174         self.lines.datetime._settz(self._tz)
        175 
        176         # This should probably be also called from an override-able method
    
    AttributeError: 'Lines_LineSeries_DataSeries_OHLC_OHLCDateTime_Abst' object has no attribute 'datetime'
    

    Please help how can I resolve the problem.



  • maybe just convert the date to datetime first in the pandas dataframe?
    better solution should be available though :-)



  • My data is like formall example of docs. I don't think type is the problem.



  • @cynthiameow Edit: Done that but it raises same error.



  • @Arta-Asadi @backtrader14 can you please help.


Log in to reply
 

});