def initialize(context): # Get continuous futures for Light Sweet Crude Oil... context.crude_oil = continuous_future('CL', roll='calendar') # ... and RBOB Gasoline context.gasoline = continuous_future('RB', roll='calendar') # If Zipline has trouble pulling the default benchmark, try setting the # benchmark to something already in your bundle set_benchmark(context.crude_oil) # Ignore commissions and slippage for now set_commission(us_futures=commission.PerTrade(cost=0)) set_slippage(us_futures=slippage.FixedSlippage(spread=0.0)) # Long and short moving average window lengths context.long_ma = 65 context.short_ma = 5 # True if we currently hold a long position on the spread context.currently_long_the_spread = False # True if we currently hold a short position on the spread context.currently_short_the_spread = False # Rebalance pairs every day, 30 minutes after market open schedule_function(func=rebalance_pairs, date_rule=date_rules.every_day(), time_rule=time_rules.market_open(minutes=30)) # Record Crude Oil and Gasoline Futures prices everyday schedule_function(record_price, date_rules.every_day(), time_rules.market_open())
def initialize(context): context.fut = continuous_future('ES', roll='calendar') # Ignore commissions and slippage for now set_commission(us_futures=commission.PerTrade(cost=0)) set_slippage(us_futures=slippage.FixedSlippage(spread=0.0)) context.i = 0 context.invested = False
def initialize(context): """Set initialization params for a backtest""" # trading pair context.asset = symbol(trading_pair) # trading signals, dict, timestamp:amount context.trades = trades # transaction cost if commission_is_per_share: context.set_commission( commission.PerShare(cost=commission_cost, min_trade_cost=0)) else: context.set_commission(commission.PerTrade(cost=commission_cost))
def initialize(context): # these must ALWAYS be present! context.transforms = [] context.algo_rules = [] context.max_lookback = 63 context.outstanding = {} # orders which span multiple days context.raw_data = {} ############################################################# # set the following parameters as required context.show_positions = True # select records to show in algo.show_records() context.show_records = True # replace cash_proxy with risk_free if cantec.allow_cash_proxY_replacement is True # and cash_proxy price is <= average cash_proxy price over last context.cash_proxy_lookback days context.allow_cash_proxy_replacement = False context.cash_proxy_lookback = 43 # must be <= context.max_lookback context.update_metrics = False # to calculate Sharpe ratio context.calculate_SR = False context.SR_lookback = 63 # must be <= context.max_lookback context.SD_factor = 0 # position only changed if percentage change > threshold context.threshold = 0.01 # the following can be changed context.market_proxy = symbols('VFINX')[0] context.risk_free = symbols('VFISX')[0] # context.market_proxy = symbols('SPY')[0] # context.risk_free = symbols('SHY')[0] set_commission(commission.PerTrade(cost=10.0)) context.leverage = 1.0 ################################################################# # configure strategies context.rebalance_interval = 3 # set interval to n = no of periods (default: months) # if you want to change default period, change schedule reallocate below # Strategy 1 rs0003 = StrategyParameters( context, name='rs0003', portfolios=[symbols('NHMAX', 'FAGIX', 'VFIIX')], # portfolios=[symbols('HYD','HYD','MBB')], portfolio_allocation_modes=['EW'], portfolio_allocation_kwargs=[{}], security_weights=[None], portfolio_allocation_formulas=[None], scoring_methods=['RS'], scoring_factors=[{ '+momentum': 1.0 }], n_tops=[1], protection_modes=['BY_RULE'], protection_rules=['smma_rule'], protection_formulas=[None], cash_proxies=[symbol('VFISX')], # cash_proxies=[symbol('SHY')], strategy_allocation_mode='FIXED', portfolio_weights=[1.0], strategy_allocation_formula=None, strategy_allocation_rule=None) Configurator(context, define_transforms, define_rules, strategies=[rs0003]) ############################ # configure algorithm algo = Algo(context, strategies=[rs0003.strategy], allocation_model=AllocationModel(context, mode='EW', weights=None, formula=None), regime=None) ########################################################################################################### # generate algo data every day at close schedule_function(algo.update_data, date_rules.every_day(), time_rules.market_close()) # daily functions to handle GTC orders schedule_function(algo.check_for_unfilled_orders, date_rules.every_day(), time_rules.market_close()) schedule_function(algo.fill_outstanding_orders, date_rules.every_day(), time_rules.market_open()) if context.update_metrics: # calculate metrics every day schedule_function(algo.update_metrics, date_rules.every_day(), time_rules.market_close()) if context.show_positions: schedule_function(algo.show_positions, date_rules.month_start(days_offset=0), time_rules.market_open()) if context.show_records: # show records every day # edit the show_records function to include records required schedule_function(algo.show_records, date_rules.every_day(), time_rules.market_close()) schedule_function(algo.rebalance, date_rules.month_end(days_offset=2), time_rules.market_open())