def initialize(context): attach_pipeline(make_pipeline(), 'pipeline') #Schedule Functions if not IS_LIVE: schedule_function( trade, #date_rules.every_day(), #date_rules.week_end(days_offset=1),#0=Fri 1= Thurs date_rules.month_end(days_offset=3), time_rules.market_close(minutes=30) ) schedule_function(record_vars, date_rules.every_day(), time_rules.market_close()) schedule_function(cancel_open_orders, date_rules.week_end(days_offset=2), time_rules.market_close()) context.spy = symbol('SPY') #sid(8554) #SPY context.TF_filter = False #context.TF_lookback = 60 #Set number of securities to buy and bonds fund (when we are out of stocks) context.Target_securities_to_buy = 15 #10 #15 #2 #1 #5 #10 #5 context.bonds = symbol('IEF') #sid(23870) #IEF context.relative_momentum_lookback = 44 #66 #22 #4 #22 #22 #22 #126 #Momentum lookback context.momentum_skip_days = 1 context.top_n_relative_momentum_to_buy = 10 #15 #10 #15 #1 #5 #5 #10 #5 #Number to buy context.stock_weights = pd.Series() context.bond_weights = pd.Series() context.auto_close = {} #Initialize portfolio auto_close list. context.TRACK_ORDERS_ON = False
def initialize(context): schedule_function(func=trade, date_rule=date_rules.every_day(), time_rule=time_rules.market_open(), half_days=True) schedule_function(func=cancel, time_rule=time_rules.market_close(minutes=5), date_rule=date_rules.every_day(), half_days=True) schedule_function(func=reorder, time_rule=time_rules.market_open(minutes=5), date_rule=date_rules.every_day(), half_days=True) context.asserts = symbols('SPY') context.bonds = symbol('SHY') context.rebalance_date = 0 context.fired = False context.rebalance_inteval = 'D' #'Q', #'D', #'M' #'Q' #'Y' context.top_n_by_momentum = pd.Series() #Choose X stocks out of portfolio of Y stocks- how many stocks to hold - top X by momentum context.stocks = 1 #Lookback for momentum calculation context.momentum_days = 60 #set at less than 1 to ensure no leverage context.leverage_buffer = 0.99 #Set to 0 to reject any stocks with negative momentum, set to -1 to accept stocks with negative momentum context.trend = 0.0 context.reorder_dict = {}
def get_date_rules(freq): if freq == "day": start_rule = date_rules.every_day() end_rule = date_rules.every_day() elif freq == "week": start_rule = date_rules.week_start() end_rule = date_rules.week_end() elif freq == "month": start_rule = date_rules.month_start() end_rule = date_rules.month_end() else: start_rule = date_rules.every_day() end_rule = date_rules.every_day() date_rule = {"start": start_rule, "end": end_rule} time_rule = { "start": time_rules.market_open(), "end": time_rules.market_close() } return date_rule, time_rule
def initialize(context): """ Called once at the start of the algorithm. """ c = context c.etf_universe = StaticAssets( symbols('XLY', 'XLP', 'XLE', 'XLF', 'XLV', 'XLI', 'XLB', 'XLK', 'XLU')) c.alphas = pd.DataFrame() # Rebalance every day, 1 hour after market open. schedule_function( rebalance, date_rules.every_day(), time_rules.market_open(hours=1), ) # Record tracking variables at the end of each day. schedule_function( record_vars, date_rules.every_day(), time_rules.market_close(), ) # Create our dynamic stock selector. attach_pipeline(make_pipeline(context), 'pipeline') attach_pipeline(make_pipeinit(context), 'pipeinit') c.first_trading_day = True c.factor_name_list = make_factor().keys()
def initialize(context): ws.send(msg_placeholder % "Simulation Start") context.security = symbol(ticker) context.model = RandomForestRegressor() context.trained = False context.lookback = 3 context.history_range = 400 schedule_function(create_model, date_rules.week_end(), time_rules.market_close(minutes=10)) schedule_function(trade, date_rules.every_day(), time_rules.market_open(minutes=minutes)) ws.send(msg_placeholder % "Execution of Training and Trading functions scheduled")
def initialize(context): """ Called once at the start of the algorithm. """ # Rebalance every day, 1 hour after market open. schedule_function(rebalance, date_rules.month_end(), time_rules.market_open(hours=1)) # Record tracking variables at the end of each day. schedule_function( record_vars, date_rules.every_day(), time_rules.market_close(), ) # Create our dynamic stock selector. print('ATTACH PIPELINE') attach_pipeline(make_pipeline(), 'pipeline') print('PIPELINE ATTACHED')
def initialize(context): context.universe = StaticAssets(symbols( 'XLY', # Select SPDR U.S. Consumer Discretionary 'XLP', # Select SPDR U.S. Consumer Staples 'XLE', # Select SPDR U.S. Energy 'XLF', # Select SPDR U.S. Financials 'XLV', # Select SPDR U.S. Healthcare 'XLI', # Select SPDR U.S. Industrials 'XLB', # Select SPDR U.S. Materials 'XLK', # Select SPDR U.S. Technology 'XLU', # Select SPDR U.S. Utilities )) # my_pipe = Pipeline() my_pipe = make_pipeline(context) # context.universe = StaticAssets(etfs) attach_pipeline(my_pipe, 'my_pipeline') schedule_function(func=rebalance, date_rule=date_rules.month_end(days_offset=2), # date_rule=date_rules.every_day(), time_rule=time_rules.market_close(minutes=30))