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

1-min data date error



  • Dear Community,

    I am trying to add a csv file to cerebro, in which I have 1-minute data:

    data = bt.feeds.GenericCSVData(
        dataname="myfile.csv",
    
        fromdate=datetime(2013,3,31,22,7),
        todate=datetime(2018,11,24,23,18),
    
        dtformat=("%Y-%m-%dT%H:%M:%S.%fZ"),
        timeframe=bt.TimeFrame.Minutes,
        compression=1,
         
        datetime=5,
        high=1,
        low=2,
        open=0,
        close=3,
        volume=4,
        openinterest=-1
    )
    

    but it keeps giving me this error message:

    File "C:\Users\...\AppData\Local\Programs\Python\Python37\lib\site-packages\backtrader\indicators\basicops.py", line 363, in once
       for i in range(start, end):
    TypeError: 'float' object cannot be interpreted as an integer
    

    What did I miss? I have already tried with different timeframes and compressions.


  • administrators

    @vishamar said in 1-min data date error:

    I have already tried with different timeframes and compressions.

    Which clearly indicates the problem has nothing to do with the data feed and associated timeframes and compressions.

    @vishamar said in 1-min data date error:

    What did I miss?

    Showing the code that actually generates the exception.



  • Oh, sorry. Here is the code:

    class strategy1(bt.Strategy):
        def __init__(self):
    
            ohlc4=(self.data.close+self.data.high+self.data.low+self.data.open)/4
            
            params = (("scl", scl), ("length", length),)
            
            rma=bt.ind.SmoothedMovingAverage(ohlc4, period=scl)
            TR=bt.ind.TrueRange()
            atr=bt.ind.SmoothedMovingAverage(TR, period=scl)
            
    
        def next(self):
            
            ma_scl=rma
            scm_off=int(fieldValues[5])*int(fieldValues[0])*atr
            ma_scl[scl_2][np.isnan(ma_scl[scl_2])]=ohlc4
            sct =  ma_scl[scl_2]+scm_off  
            scb =  ma_scl[scl_2]-scm_off
            idxstopprice=(ohlc4[idxstopprice]<ohlc4[idxstopprice-1]).idxmax()  
            stop_price=ohlc4[idxstopprice]
            
            if self.data.close[0]>sct and self.data.close[0]<(stop_price+int(fieldValues[7])*int(fieldValues[1])) and self.data.close[0]<(stop_price-int(fieldValues[8])*int(fieldValues[2])):
                self.buy()
                self.order = self.buy()
    
    cerebro = bt.Cerebro()
    
    data = bt.feeds.GenericCSVData(
        dataname="myfile.csv",
    
        fromdate=datetime(2013,3,31,22,7),
        todate=datetime(2018,11,24,23,18),
    
        dtformat=("%Y-%m-%dT%H:%M:%S.%fZ"),
        timeframe=bt.TimeFrame.Minutes,
        compression=1,
         
        datetime=5,
        high=1,
        low=2,
        open=0,
        close=3,
        volume=4,
        openinterest=-1
    )
    
    cerebro.adddata(data)
    
    cerebro.addstrategy(strategy1)
    startcash=100000
    cerebro.run()
    cerebro.plot()
    

  • administrators

    The code must be a joke. A bad one, obviously.

    Some of your posts:

    • https://community.backtrader.com/post/7664

      You want to do things in notify_trade and yet you present code now which is like a random collection of characters. How have you managed to buy/sell anything?

    • https://community.backtrader.com/post/7632

      You backtest twice over the same period (with an extended period once) and show random results for the common leading period, which is impossible. But how can you backtest when your code is random?

    Your turn.



  • @backtrader Thank you for your kind words.
    This is a completely other task with a completely other code. The previous one is working now.
    This code is not random, it only takes many input values.