Pair Trading with minutes data



  • Hi,

    Since I upgrade my BT at last version, hours, minutes or secondes are not processes any more.
    It's look like BT is blocked at only day level.

    FYI : I setup timeframe and compression but it's still bloking to the first date to the first day "2016-11-29T23:59:59.999989".
    cerebro.resampledata(data0,timeframe=tframes[args.timeframe], compression=args.compression)
    cerebro.resampledata(data1,timeframe=tframes[args.timeframe], compression=args.compression)

    Could you provide a version of pair-trading contribution working with minutes data ?

    Regards,


  • administrators

    To properly provide an answer one would need some pieces of the puzzle.

    A long shot: you are not loading the data feed with the proper timeframe/compression information.



  • :Here a part of the source code with the timeframe/compression setting

    def runstrategy():
        args = parse_args()
    
        # Create a cerebro
        cerebro = bt.Cerebro()
    
        # Get the dates from the args
        fromdate = datetime.datetime(2016, 11, 25) # datetime.datetime.strptime(args.fromdate, '%Y-%m-%d')
        todate = datetime.datetime(2016, 12, 02) # datetime.datetime.strptime(args.todate, '%Y-%m-%d')
    
        # Create the 1st data
        tframes = dict(
            secondes=bt.TimeFrame.Seconds,
            minutes=bt.TimeFrame.Minutes,
            days=bt.TimeFrame.Days,
            weeks=bt.TimeFrame.Weeks,
            months=bt.TimeFrame.Months,
            years=bt.TimeFrame.Years)
    
        # Create the 1st data
        """
        data0 = btfeeds.YahooFinanceCSVData(
            dataname=args.data0,
            fromdate=fromdate,
            todate=todate)
        """
        data0 = btfeeds.GenericCSVData(
            dataname=args.data0,
    
            fromdate=fromdate,
            todate=todate,
    
            nullvalue=0.0,
    
            dtformat=('%Y-%m-%d %H:%M:%S'),
            datetime=0,
            high=2,
            low=3,
            open=1,
            close=4,
            volume=5,
            openinterest=-1,
        )
    
        # Add the 1st data to cerebro
        cerebro.adddata(data0)
    
        # Create the 2nd data
        """
        data1 = btfeeds.YahooFinanceCSVData(
            dataname=args.data1,
            fromdate=fromdate,
            todate=todate)
        """
        data1 = btfeeds.GenericCSVData(
            dataname=args.data1,
    
            fromdate=fromdate,
            todate=todate,
    
            nullvalue=0.0,
    
            dtformat=('%Y-%m-%d %H:%M:%S'),
            datetime=0,
            high=2,
            low=3,
            open=1,
            close=4,
            volume=5,
            openinterest=-1,
        )
    
        # Add the 2nd data to cerebro
        cerebro.adddata(data1)
    
    
        cerebro.resampledata(data0,timeframe=tframes[args.timeframe], compression=args.compression)
        # Create the 2nd data
        # Add the 2nd data to cerebro
        #cerebro.adddata(data1)
        cerebro.resampledata(data1,timeframe=tframes[args.timeframe], compression=args.compression)
    
        # Add the strategy
        cerebro.addstrategy(PairTradingStrategy,
                            period=args.period,
                            stake=args.stake)
    
        # Add the commission - only stocks like a for each operation
        cerebro.broker.setcash(args.cash)
    
        # Add the commission - only stocks like a for each operation
        cerebro.broker.setcommission(commission=args.commperc)
    
        # And run it
        cerebro.run(runonce=not args.runnext,
                    preload=not args.nopreload,
                    oldsync=args.oldsync)
    
        # Plot if requested
        if args.plot:
            cerebro.plot(numfigs=args.numfigs, volume=False, zdown=False)
    
    
    def parse_args():
        parser = argparse.ArgumentParser(description='MultiData Strategy')
    
        parser.add_argument('--data0', '-d0',
                            default='intra-DDD.csv',
                            help='1st data into the system')
    
        parser.add_argument('--data1', '-d1',
                            default='intra-SSYS.csv',
                            help='2nd data into the system')
    
        parser.add_argument('--fromdate', '-f',
                            default='1997-01-01',
                            help='Starting date in YYYY-MM-DD format')
    
        parser.add_argument('--todate', '-t',
                            default='1998-06-01',
                            help='Starting date in YYYY-MM-DD format')
    
        group = parser.add_mutually_exclusive_group()
        group.add_argument('--tframe', default='years', required=False,
                           choices=['days', 'weeks', 'months', 'years'],
                           help='TimeFrame for the returns/Sharpe calculations')
    
        group.add_argument('--legacyannual', action='store_true',
                           help='Use legacy annual return analyzer')
    
    
        pgroup = parser.add_mutually_exclusive_group(required=False)
    
        pgroup.add_argument('--replay',
                            required=False, action='store_true',
                            help='replay to chosen timeframe')
    
        pgroup.add_argument('--resample', default = True,
                            required=False, action='store_true',
                            help='resample to chosen timeframe')
    
        parser.add_argument('--timeframe', default='minutes',
                            choices=bt.TimeFrame.Names,
                            required=False, action='store',
                            help='TimeFrame for Resample/Replay')
    
        parser.add_argument('--compression', default=1, type=int,
                            required=False, action='store',
                            help='Compression for Resample/Replay')
    
        parser.add_argument('--timeframe1', default=True,
                            choices=bt.TimeFrame.Names,
                            required=False, action='store',
                            help='TimeFrame for Resample/Replay - Data1')
    
        parser.add_argument('--compression1', default=None, type=int,
                            required=False, action='store',
                            help='Compression for Resample/Replay - Data1')
    
        parser.add_argument('--period', default=10, type=int,
                            help='Period to apply to the Simple Moving Average')
    
        parser.add_argument('--cash', default=100000, type=int,
                            help='Starting Cash')
    
        parser.add_argument('--runnext', action='store_true',
                            help='Use next by next instead of runonce')
    
        parser.add_argument('--nopreload', action='store_true',
                            help='Do not preload the data')
    
        parser.add_argument('--oldsync', action='store_true',
                            help='Use old data synchronization method')
    
        parser.add_argument('--commperc', default=0.005, type=float,
                            help='Percentage commission (0.005 is 0.5%%')
    
        parser.add_argument('--stake', default=10, type=int,
                            help='Stake to apply in each operation')
    
        parser.add_argument('--plot', '-p', default=True, action='store_true',
                            help='Plot the read data')
    
        parser.add_argument('--numfigs', '-n', default=1,
                            help='Plot using numfigs figures')
    
        return parser.parse_args()
    
    
    if __name__ == '__main__':
        runstrategy()
    

  • administrators

    @GwadaMan said in Pair Trading with minutes data:

    data0 = btfeeds.GenericCSVData(
        dataname=args.data0,
    
        fromdate=fromdate,
        todate=todate,
    
        nullvalue=0.0,
    
        dtformat=('%Y-%m-%d %H:%M:%S'),
        datetime=0,
        high=2,
        low=3,
        open=1,
        close=4,
        volume=5,
        openinterest=-1,
    )
    

    timeframe? compression?



  • @backtrader the code is splited. Does it miss something ? I didn't undestand your question.

    cerebro.resampledata(data0,timeframe=tframes[args.timeframe], compression=args.compression)
    cerebro.resampledata(data1,timeframe=tframes[args.timeframe], compression=args.compression)

    parser.add_argument('--timeframe', default='minutes',
    choices=bt.TimeFrame.Names,
    required=False, action='store',
    help='TimeFrame for Resample/Replay')

    parser.add_argument('--compression', default=1, type=int,
    required=False, action='store',
    help='Compression for Resample/Replay')


  • administrators

    The code is not formatted because you have not added 3-tick marks (see the top of the page) before and after the code.

    You are instantiating the data feed without any timeframe / compression indication. Same issue today: https://community.backtrader.com/topic/290/is-this-a-valid-way-of-analyzing-multiple-timeframes



  • @backtrader thx I fixed it with your help !!!


Log in to reply
 

Looks like your connection to Backtrader Community was lost, please wait while we try to reconnect.