def generate_signals(context, data): try: pipeline_results = pipeline_output('strategy_pipeline') except NoFurtherDataError: context.long_securities = [] context.short_securities = [] return p = context.params['percentile'] momentum = pipeline_results long_candidates = momentum[momentum > 0].dropna().sort_values('momentum') short_candidates = momentum[momentum < 0].dropna().sort_values('momentum') n_long = len(long_candidates) n_short = len(short_candidates) n = int(min(n_long, n_short) * p) if n == 0: print("{}, no signals".format(get_datetime())) context.long_securities = [] context.short_securities = [] context.long_securities = long_candidates.index[-n:] context.short_securities = short_candidates.index[:n]
def generate_signals(context, data): try: pipeline_results = pipeline_output('strategy_pipeline') except NoFurtherDataError: context.long_securities = [] context.short_securities = [] return p = context.params['percentile'] pipeline_results = pipeline_results[pipeline_results.vol > 0].dropna() vol_threshold_hi = pipeline_results.vol.quantile(0.75) vol_threshold_lo = pipeline_results.vol.quantile(0.25) high_vol = pipeline_results[pipeline_results.vol > vol_threshold_hi] low_vol = pipeline_results[pipeline_results.vol < vol_threshold_lo] long_candidates = high_vol.sort_values('skew', ascending=True) short_candidates = low_vol.sort_values('skew', ascending=True) available = min(len(long_candidates), len(short_candidates)) n = int(available * p) print('total candidates {}'.format(n)) if n == 0: print("{}, no signals".format(get_datetime())) context.long_securities = [] context.short_securities = [] context.long_securities = long_candidates.index[:n] context.short_securities = short_candidates.index[:n]
def analyze(context, perf): """ Called at the end of strategy run. """ # current simulation date-time print('{} {}'.format(get_datetime().date(), 30 * '#')) # accessing portfolio details portfolio_value = context.portfolio.portfolio_value cash = context.portfolio.cash positions = context.portfolio.positions print('portfolio_value {}, cash {}'.format(portfolio_value, cash)) for asset, position in positions.items(): print('Symbol {}, amount {}'.format(asset.symbol, position.quantity)) # accessing account details print('leverage {}'.format(context.account.leverage)) print('net leverage {}'.format(context.account.net_leverage)) print('available cash {}'.format(context.account.available_funds)) print('total positions exposure {}'.format( context.account.total_positions_exposure)) print('performance data columns...') print(perf.columns)
def initialize(context): print("{}:inside initialize".format(get_datetime())) schedule_function(rebalance, date_rule=date_rules.month_start(), time_rule=time_rules.market_open()) context.frequency = 120 context.loop_count = 0
def update_pnl_history(self): dt = get_datetime() perfs = dict((key, 0.0) for key in self.advisors_keys) for advisor in self.advisors: perfs[advisor.name] = advisor.perf perfs = pd.DataFrame(perfs, index=[dt]) if self.perfs.empty: self.perfs = perfs else: self.perfs = self.perfs.append(perfs)
def generate_signals(context, data): try: results = pipeline_output('strategy_pipeline') except: return # use other columns to print other indicators scanning results results = results.sort_values('rsi', ascending=True) print('{}{}'.format(get_datetime(), '-' * 30)) print(results.head()) print(results.tail())
def update_weights(self): ''' Called to update the weighing scheme. It can be scheduled to be called at a lower frequency than signal computation. ''' dt = get_datetime() self.current_weights = self.weighing_function() weight = pd.DataFrame(self.current_weights, index=[dt]) if self.weights.empty: self.weights = weight else: self.weights = self.weights.append(weight)
def handle_data(context, data): """ A function to define things to do at every bar """ if check_stop_loss(context, data): print('{} got stopped out'.format(get_datetime())) return context.bar_count = context.bar_count + 1 if context.bar_count < context.params['trade_freq']: return # time to trade, call the strategy function context.bar_count = 0 run_strategy(context, data)
def generate_signals(context, data): try: pipeline_results = pipeline_output('strategy_pipeline') except NoFurtherDataError: context.long_securities = [] context.short_securities = [] return p = context.params['percentile'] momentum = pipeline_results.dropna().sort_values('momentum') n = int(len(momentum) * p) if n == 0: print("{}, no signals".format(get_datetime())) context.long_securities = [] context.short_securities = [] context.long_securities = momentum.index[-n:] context.short_securities = momentum.index[:n]
def generate_signals(context, data): try: pipeline_results = pipeline_output('strategy_pipeline') except: context.long_securities = [] context.short_securities = [] return p = context.params['percentile'] rsi_factor = pipeline_results candidates = rsi_factor[rsi_factor > 0].dropna().sort_values('rsi') n = int(len(candidates) * p) if n == 0: print("{}, no signals".format(get_datetime())) context.long_securities = [] context.short_securities = [] context.long_securities = candidates.index[-n:] context.short_securities = candidates.index[:n]
def handle_data(context, data): if time_to_run(context): print("{}:inside handle_data".format(get_datetime()))
def before_trading_start(context, data): context.loop_count = 0 print("{}:inside before_trading_start".format(get_datetime()))
def rebalance(context, data): print("{}:inside rebalance".format(get_datetime()))