def initialize(context): attach_pipeline(make_pipeline(), 'my_pipeline') context.sector_wise_exposure = dict() context.sector_stocks = {} context.turnover_count = 0 if context.live_trading is False: schedule_function(stop_loss, date_rule=date_rules.month_start()) schedule_function(rebalance, date_rule=date_rules.month_start())
def before_trading_start(context, data): context.pipeline_data = pipeline_output('my_pipeline') if context.live_trading is True: try: with open('sector_list.pickle', 'rb') as handle: context.sector_stocks = pickle.load(handle) except: context.sector_stocks = {} schedule_function(stop_loss, date_rule=date_rules.month_start()) schedule_function(rebalance, date_rule=date_rules.month_start()) context.logic_run_done = False
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_testing_algo(self, context): set_commission(PerShare()) set_slippage(VolumeShareSlippage()) if self.syms is None: self.syms = self.__validate_symbols__(self.ticker_syms) self.portfolio_memory = PortfolioMemory(len(self.syms), self.config.n_history) schedule_function(self.testing_rebalance, date_rule=date_rules.month_start(), time_rule=time_rules.market_open(minutes=1))
def initialize(context): attach_pipeline(make_pipeline(), 'my_pipeline') # context.broker = BacktestBroker() context.stop_loss_list = pd.Series() context.sector_wise_exposure = dict() context.sector_stocks = {} context.turnover_count = 0 if context.live_trading is False: schedule_function(rebalance, date_rule=date_rules.month_start())
def initialize(context): # attach_pipeline(make_pipeline(), 'my_pipeline') context.turnover_count = 0 # etf stock context.shorting_on = True context.longStock = symbol('IVV') context.shortStock = symbol('SH') if context.live_trading is False: schedule_function(monthly_rebalance, date_rule=date_rules.month_start())
def initialize(context): c = context # Some Risk-On universes core_etf = symbols('QQQ', 'XLP', 'TLT', 'IEF') tact_etf = symbols('XLV', 'XLY', 'TLT', 'GLD') VAAG12 = symbols('SPY', 'IWM', 'QQQ', 'VGK', 'EWJ', 'VWO', 'VNQ', 'GSG', 'GLD', 'TLT', 'LQD', 'HYG') VAAG4 = symbols('SPY', 'VEA', 'VWO', 'BND') c.RISK = VAAG4 # Which Riskasset universe to use? alletf = symbols('QQQ', 'XLP', 'TLT', 'IEF', 'SPY', 'IWM', 'QQQ', 'VGK', 'EWJ', 'VWO', 'VNQ', 'GSG', 'GLD', 'TLT', 'LQD', 'HYG', 'XLV', 'XLY', 'GLD', 'VEA') # 'TLO' repleced by 'TLT' # Some Risk Off (Cash) universes c.CASH = symbols('SHY', 'IEF', 'LQD') schedule_function(define_weights, date_rules.month_end(Month_Offset), time_rules.market_open(minutes=64)) log.info('define_weights for last trading day of the month') schedule_function(trade_sell, date_rules.month_end(Month_Offset), time_rules.market_open(minutes=65)) log.info('trade_sell for last trading day of the month') if TplusX > 0: schedule_function( trade_buy, date_rules.month_start(max(0, TplusX - 1 - Month_Offset)), time_rules.market_open(minutes=66)) log.info('trade_buy for month day ' + str(max(0, TplusX - Month_Offset))) else: schedule_function(trade_buy, date_rules.month_end(Month_Offset), time_rules.market_open(minutes=66)) log.info('trade_buy last trading day of the month') stocks = core_etf + tact_etf context.sell = {} context.buy = {} for stock in stocks: context.sell[stock] = 0 context.buy[stock] = 0 context.difference_perc = 0.02 context.stops = {} context.stoploss = 0.30 context.stoppedout = []
def before_trading_start(context, data): # context.pipeline_data = pipeline_output('my_pipeline') if context.live_trading is True: schedule_function(monthly_rebalance, date_rule=date_rules.month_start()) context.logic_run_done = False