Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    1. Home
    2. Arta Asadi
    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 2
    • Posts 5
    • Best 0
    • Groups 0

    Arta Asadi

    @Arta Asadi

    0
    Reputation
    4
    Profile views
    5
    Posts
    0
    Followers
    0
    Following
    Joined Last Online

    Arta Asadi Unfollow Follow

    Latest posts made by Arta Asadi

    • Nothing happens when running cerebro

      Hi, I have written my code just like strategy example but it won't run anything. Please help me.
      Strategy code:

      class TestStrategy2(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
      
          def next(self):
              # Simply log the closing price of the series from the reference
              self.log('Close, %.2f' % self.dataclose[0])
      

      Datafeed:

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

      Running:

      ready_data = PandasData(dataname=strategy_data2)
      cerebro = bt.Cerebro()
      cerebro.adddata(ready_data)
      cerebro.addstrategy(TestStrategy2)
      cerebro.broker.setcash(1000000000.0)
      cerebro.run()
      

      in the jupyter it gives this:

      [<__main__.TestStrategy at 0x150489c3ba8>]
      

      but running in normal file gives nothing. At least I want it to log the close, so, I can make that cerebro is working on my data.
      Please help me. Thanks

      posted in General Code/Help
      Arta Asadi
      Arta Asadi
    • RE: Custom data from pandas with pre-calculated signal

      @Arta-Asadi @backtrader14 can you please help.

      posted in Indicators/Strategies/Analyzers
      Arta Asadi
      Arta Asadi
    • RE: Custom data from pandas with pre-calculated signal

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

      posted in Indicators/Strategies/Analyzers
      Arta Asadi
      Arta Asadi
    • RE: Custom data from pandas with pre-calculated signal

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

      posted in Indicators/Strategies/Analyzers
      Arta Asadi
      Arta Asadi
    • 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.

      posted in Indicators/Strategies/Analyzers
      Arta Asadi
      Arta Asadi