Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    Resampling data using 1minute freq

    Indicators/Strategies/Analyzers
    2
    4
    47
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • C
      coder1111 last edited by

      Hi Team

      I am getting error while I am using "1min" frequency in the resampling function rest is I use other timeframe like "2min","15min","60min" it works well.

      Also my data frequency is "1min".
      Also I have tried to comment the resampling code while passing directly "1min" data and I am getting error.

      from __future__ import (absolute_import, division, print_function,
                              unicode_literals)
      import datetime
      import pandas as pd
      from collections import OrderedDict
      import backtrader as bt
      import backtrader.indicators as btind
      import backtrader.analyzers as btanalyzers
      import argparse
      import backtrader.feeds as btfeeds
      import datetime
      
      def runstrat():
          args = parse_args()
          datapath_bn = 'data/BANKNIFTY_FUT.csv'
          datapath_nf = 'data/NIFTY_FUT.csv'
          timeframe_resample = '1min'
          # Create a cerebro entity
          cerebro = bt.Cerebro()
      
          # Add a strategy
          cerebro.addstrategy(PairTrading)
          dataframe_bn = getData(datapath_bn)
          dataframe_nf = getData(datapath_nf)
          #Data for specific Market Time
          dataframe_nf = dataframe_nf.between_time('09:00', '15:30', axis=0, include_start=True, include_end=True)
          dataframe_bn = dataframe_bn.between_time('09:00', '15:30', axis=0, include_start=True, include_end=True)
      
          #Resample Here
          dataframe_bn = resample_df(dataframe_bn,timeframe_resample)
          dataframe_nf = resample_df(dataframe_nf,timeframe_resample)
          dataframe_nf['symbol'] = 'nifty'
          dataframe_bn['symbol'] = 'banknifty'
          dataframe_bn = dataframe_bn.dropna()
          dataframe_nf = dataframe_nf.dropna()
          # skiprows = 1 if args.noheaders else 0
          # header = None if args.noheaders else 0
          
          data = bt.feeds.PandasData(dataname=dataframe_bn)
          print(dataframe_bn)
          data1 = bt.feeds.PandasData(dataname=dataframe_nf)
          print(dataframe_nf)
      
          #cerebro.addwriter(bt.WriterFile, csv=True)
      
          # Add the Data Feed to Cerebro
          cerebro.adddata(data,name='banknifty')
          cerebro.adddata(data1,name='nifty')
      
          # Set our desired cash start
          cerebro.broker.setcash(10000000.0)
      
          # Add the analyzers we are interested in
          cerebro.addanalyzer(bt.analyzers.TradeAnalyzer, _name="ta")
          cerebro.addanalyzer(bt.analyzers.SQN, _name="sqn")
      
          # Print out the starting conditions
          print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
          #cerebro.addanalyzer(btanalyzers.PyFolio.get_analysis(), _name='mysharpe')
          # Run over everything
          strategies= cerebro.run(tradehistory = True)
          # Print out the final result
          print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
          # Plot the result
          #cerebro.plot(style='candlestick')
      
          firstStrat = strategies[0]
          # print the analyzers
          printTradeAnalysis(firstStrat.analyzers.ta.get_analysis())
          printSQN(firstStrat.analyzers.sqn.get_analysis())
      

      And the error I get is :-

      Traceback (most recent call last):
        File "D:/Python/Pycharm/Strategies /Trading/t_v1.py", line 367, in <module>
          runstrat()
        File "D:/Python/Pycharm/Strategies /Trading/t_v1.py", line 337, in runstrat
          strategies= cerebro.run(tradehistory = True)
        File "D:\Python\Pycharm\Strategies\venv\lib\site-packages\backtrader\cerebro.py", line 1127, in run
          runstrat = self.runstrategies(iterstrat)
        File "D:\Python\Pycharm\Strategies\venv\lib\site-packages\backtrader\cerebro.py", line 1293, in runstrategies
          self._runonce(runstrats)
        File "D:\Python\Pycharm\Strategies\venv\lib\site-packages\backtrader\cerebro.py", line 1652, in _runonce
          strat._once()
        File "D:\Python\Pycharm\Strategies\venv\lib\site-packages\backtrader\lineiterator.py", line 297, in _once
          indicator._once()
        File "D:\Python\Pycharm\Strategies\venv\lib\site-packages\backtrader\lineiterator.py", line 297, in _once
          indicator._once()
        File "D:\Python\Pycharm\Strategies\venv\lib\site-packages\backtrader\lineiterator.py", line 318, in _once
          self.once(self._minperiod, self.buflen())
        File "D:\Python\Pycharm\Strategies\venv\lib\site-packages\backtrader\indicator.py", line 136, in once_via_next
          self.next()
        File "D:\Python\Pycharm\Strategies\venv\lib\site-packages\backtrader\indicators\ols.py", line 54, in next
          intercept, slope = sm.OLS(p0, p1).fit().params
      ValueError: not enough values to unpack (expected 2, got 1)
      
      Process finished with exit code 1
      
      

      Also I think might be its for the indicator I am using:

      self.transform = btind.OLS_TransformationN(self.data1, self.data, period=self.p.period)
      self.zscore = self.transform.zscore
      

      Please can anyone let me know how to fix it?

      1 Reply Last reply Reply Quote 0
      • run-out
        run-out last edited by

        @coder1111 said in Resampling data using 1minute freq:

        data = bt.feeds.PandasData(dataname=dataframe_bn)
        print(dataframe_bn)
        data1 = bt.feeds.PandasData(dataname=dataframe_nf)
        print(dataframe_nf)

        It's a bit hard to tell without seeing the data going in. Could you include a few rows from each dataframe?

        In the mean time, try adding in the time frequency to tell backtrader what size your data is.

        data = bt.feeds.PandasData(dataname=dataframe_bn, timeframe = bt.TimeFrame.Minutes, compression = 1)
        
        C 1 Reply Last reply Reply Quote 2
        • C
          coder1111 @run-out last edited by

          @run-out I just tried to add the changes but still the same problem. I think might be, it is because of btind.OLS_TransformationN.

          run-out 1 Reply Last reply Reply Quote 0
          • run-out
            run-out @coder1111 last edited by

            @coder1111 I just tested the following:

            self.ols = bt.ind.OLS_TransformationN(period=10)
            self.ols = bt.ind.OLS_TransformationN(self.datas[0], self.datas[1], period=10)
            self.ols = bt.ind.OLS_TransformationN(self.datas[0].close, self.datas[1].close, period=10)
            

            And they are all working fine. Perhaps as I suggested in my earlier post you could share your data, and while you are at it, share the rest of your code altogether, and we can help you find your problem. As it stands we don't have enough information.

            1 Reply Last reply Reply Quote 1
            • 1 / 1
            • First post
              Last post
            Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
            $(document).ready(function () { app.coldLoad(); }); }