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

Trouble with import 1 minute data feed



  • Hello guys, I had a problem importing data with a 1 minute timeframe. When I start cerebro, and the strategy, in the end, the size of my portfolio remains unchanged.

    import backtrader as bt
    import os
    import matplotlib.pyplot
    
    cerebro = bt.Cerebro()
    cerebro.broker.setcash(100.0)
    
    data = bt.feeds.GenericCSVData(
       dataname='data.csv',
       nullvalue = 0.0,
       datetime=0,
       timeframe=bt.TimeFrame.Minutes,
       compression=1,
       dtformat=('%Y-%m-%d %H:%M:%S'),
       open=1,
       high=2,
       low=3,
       close=4,
       volume=5,
       openinterest=None,
       reverse=False,
       header=0)
    
    cerebro.adddata(data)
    
    import backtrader.indicators as btind
    
    
    class MA_CrossOver(bt.Strategy):
    
       alias = ('SMA_CrossOver',)
    
       params = (
           # period for the fast Moving Average
           ('fast', 10),
           # period for the slow moving average
           ('slow', 30),
           # moving average to use
           ('_movav', btind.MovAv.SMA)
       )
    
       def __init__(self):
           sma_fast = self.p._movav(period=self.p.fast)
           sma_slow = self.p._movav(period=self.p.slow)
    
           self.buysig = btind.CrossOver(sma_fast, sma_slow)
    
       def next(self):
           if self.position.size:
               if self.buysig < 0:
                   self.sell()
    
           elif self.buysig > 0:
               self.buy()
    
    cerebro.addstrategy(MA_CrossOver)
    cerebro.run()
    print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
    

    The data



  • I fixed it. Most likely, the error was in a bad strategy and in the fact that the date was presented as an index.


Log in to reply
 

});