: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()