def main(): app = algo.Algo() con = msu.get_my_sql_connection() date_now = cu.get_doubledate() datetime_now = dt.datetime.now() report_date = exp.doubledate_shift_bus_days() ticker_head_list = ['HO', 'LC', 'FC', 'NQ'] # avoid HO on wednesdays for now!! data_list = [gfp.get_futures_price_preloaded(ticker_head=x, settle_date=report_date) for x in ticker_head_list] ticker_frame = pd.concat(data_list) ticker_frame.sort(['ticker_head','volume'], ascending=[True, False], inplace=True) ticker_frame.drop_duplicates(subset=['ticker_head'], take_last=False, inplace=True) ticker_list = list(ticker_frame['ticker']) theme_name_list = set([x + '_long' for x in ticker_list]).union(set([x + '_short' for x in ticker_list])) alias_portfolio = aup.portfolio(ticker_list=theme_name_list) latest_trade_entry_datetime = dt.datetime(datetime_now.year, datetime_now.month, datetime_now.day, 12, 0, 0) latest_livestock_exit_datetime = dt.datetime(datetime_now.year, datetime_now.month, datetime_now.day, 13, 55, 0) latest_macro_exit_datetime = dt.datetime(datetime_now.year, datetime_now.month, datetime_now.day, 15, 55, 0) daily_sd_dictionary = {} for i in range(len(ticker_list)): daily_data_frame = gfp.get_futures_price_preloaded(ticker=ticker_list[i], settle_date_to=report_date) daily_data_frame['close_diff'] = daily_data_frame['close_price'].diff() daily_sd_dictionary[ticker_list[i]] = np.std(daily_data_frame['close_diff'].iloc[-40:]) db_alias = 'itf_' + datetime_now.strftime('%b_%y') strategy_frame = ts.get_open_strategies(as_of_date=int(datetime_now.strftime('%Y%m%d')), con=con) if db_alias not in strategy_frame.values: db_strategy_output = ts.generate_db_strategy_from_alias(alias=db_alias, description_string='strategy_class=itf',con=con) db_alias = db_strategy_output['alias'] app.ticker_list = ticker_list app.db_alias = db_alias app.tick_size_dictionary = {x:cmi.tick_size[cmi.get_contract_specs(x)['ticker_head']] for x in ticker_list} app.daily_sd_dictionary = daily_sd_dictionary app.contract_multiplier_dictionary = {x:cmi.contract_multiplier[cmi.get_contract_specs(x)['ticker_head']] for x in ticker_list} app.ticker_head_dictionary = {x:cmi.get_contract_specs(x)['ticker_head'] for x in ticker_list} app.latest_trade_entry_datetime = latest_trade_entry_datetime app.latest_trade_exit_datetime_dictionary = {'HO': latest_macro_exit_datetime, 'LC': latest_livestock_exit_datetime, 'FC': latest_livestock_exit_datetime, 'NQ': latest_macro_exit_datetime} app.alias_portfolio = alias_portfolio app.output_dir = ts.create_strategy_output_dir(strategy_class='itf', report_date=report_date) app.log = lg.get_logger(file_identifier='ib_itf',log_level='INFO') app.con = con app.connect(client_id=4) app.run()
def get_continuous_bar_data(**kwargs): date_to = kwargs['date_to'] num_days_back = kwargs['num_days_back'] ticker = kwargs['ticker'] if 'boto_client' in kwargs.keys(): boto_client = kwargs['boto_client'] else: boto_client = get_boto_client() date_list = [exp.doubledate_shift_bus_days(double_date=date_to, shift_in_days=x) for x in reversed(range(1, num_days_back))] date_list.append(date_to) bar_data_list = [get_book_snapshot(ticker=ticker,utc_doubledate=x,boto_client=boto_client) for x in date_list] bar_data = pd.concat(bar_data_list) contract_specs_output = cmi.get_contract_specs(ticker) ticker_class = contract_specs_output['ticker_class'] if ticker_class == 'Livestock': data_out = bar_data[(bar_data['hour_minute'] >= 830) & (bar_data['hour_minute'] < 1305)] elif ticker_class == 'Ag': data_out = bar_data[((bar_data['hour_minute'] >= 1900) & (bar_data['hour_minute'] <= 2359)) | (bar_data['hour_minute'] < 745) | ((bar_data['hour_minute'] >= 830) & (bar_data['hour_minute'] < 1320))] elif ticker_class in ['Energy', 'STIR', 'Index', 'FX', 'Treasury', 'Metal']: data_out = bar_data[(bar_data['hour_minute'] < 1600) | (bar_data['hour_minute'] >= 1700)] return data_out
def get_ocs_position(**kwargs): net_position = ts.get_net_position_4strategy_alias(**kwargs) empty_position_q = False correct_position_q = False scale = 0 if len(net_position.index) == 0: empty_position_q = True return { 'empty_position_q': empty_position_q, 'correct_position_q': correct_position_q, 'scale': scale, 'sorted_position': net_position } net_position['cont_indx'] = [ cmi.get_contract_specs(x)['cont_indx'] for x in net_position['ticker'] ] net_position.sort_values('cont_indx', ascending=True, inplace=True) if (len(net_position.index) == 2) & (net_position['qty'].sum() == 0): correct_position_q = True scale = net_position['qty'].iloc[0] return { 'empty_position_q': empty_position_q, 'correct_position_q': correct_position_q, 'scale': scale, 'sorted_position': net_position }
def get_futures_price_preloaded(**kwargs): if 'ticker_head' in kwargs.keys(): ticker_head = kwargs['ticker_head'] else: ticker = kwargs['ticker'] ticker_head = cmi.get_contract_specs(ticker)['ticker_head'] if 'futures_data_dictionary' in kwargs.keys(): data_out = kwargs['futures_data_dictionary'][ticker_head] else: presaved_futures_data_folder = dn.get_directory_name(ext='presaved_futures_data') if os.path.isfile(presaved_futures_data_folder + '/' + ticker_head + '.pkl'): data_out = pd.read_pickle(presaved_futures_data_folder + '/' + ticker_head + '.pkl') else: data_out = pd.DataFrame() return data_out if 'settle_date' in kwargs.keys(): settle_date = kwargs['settle_date'] if isinstance(settle_date,int): data_out = data_out[data_out['settle_date'] == cu.convert_doubledate_2datetime(settle_date)] elif isinstance(settle_date,dt.datetime): data_out = data_out[data_out['settle_date'] == settle_date] if 'settle_date_from_exclusive' in kwargs.keys(): data_out = data_out[data_out['settle_date']>cu.convert_doubledate_2datetime(kwargs['settle_date_from_exclusive'])] if 'ticker' in kwargs.keys(): data_out = data_out[data_out['ticker']==ticker] return data_out
def calc_realized_vol_4options_ticker(**kwargs): ticker = kwargs['ticker'] contract_specs_output = cmi.get_contract_specs(ticker) if contract_specs_output['ticker_class'] in ['Index', 'FX', 'Metal']: use_proxy_contract = True else: use_proxy_contract = False if use_proxy_contract: if 'futures_data_dictionary' in kwargs.keys(): futures_data_input = { 'ticker_head': contract_specs_output['ticker_head'], 'settle_date': kwargs['settle_date'] } futures_data_input['futures_data_dictionary'] = kwargs[ 'futures_data_dictionary'] data_out = gfp.get_futures_price_preloaded(**futures_data_input) data_out = data_out.reset_index() kwargs['ticker'] = data_out['ticker'].loc[ data_out['volume'].idxmax()] else: kwargs['ticker'] = omu.get_option_underlying(**kwargs) return calc_realized_vol_4futures_ticker(**kwargs)
def get_days2_roll(**kwargs): ticker = kwargs['ticker'] date_to = kwargs['date_to'] contract_specs = cmf.get_contract_specs(ticker) ticker_head = contract_specs['ticker_head'] ticker_class = cmf.ticker_class[ticker_head] ticker_year = contract_specs['ticker_year'] ticker_month_num = contract_specs['ticker_month_num'] datetime_to = cu.convert_doubledate_2datetime(date_to) bday_us = CustomBusinessDay(calendar=get_calendar_4ticker_head(ticker_head)) if ticker_class == 'Metal': if ticker_month_num == 1: ticker_year_roll = ticker_year-1 ticker_month_roll = 12 else: ticker_year_roll = ticker_year ticker_month_roll = ticker_month_num-1 roll_datetime = dt.date(ticker_year_roll,ticker_month_roll,25) else: expiration_datetime = get_expiration_from_db(**kwargs) roll_datetime = expiration_datetime-dt.timedelta(days=5) if roll_datetime > datetime_to.date(): dts = pd.date_range(start=datetime_to, end=roll_datetime, freq=bday_us) tr_days_2roll = len(dts)-1 else: dts = pd.date_range(start=roll_datetime, end=datetime_to, freq=bday_us) tr_days_2roll = -(len(dts)-1) return {'roll_datetime': roll_datetime, 'cal_days_2roll': (roll_datetime-datetime_to.date()).days, 'tr_days_2roll': tr_days_2roll}
def execDetailsEnd(self, reqId: int): super().execDetailsEnd(reqId) print("ExecDetailsEnd. ", reqId) trade_frame = self.trade_frame trade_frame['real_tradeQ'] = True trade_frame['ticker_head'] = [cmi.get_contract_specs(x)['ticker_head'] for x in trade_frame['ticker']] if pth.exists(self.trade_file): order_frame = pd.read_csv(self.trade_file,names=['order_id','ticker','urgency','strategy_class']) print(order_frame['order_id']) print(trade_frame['order_id']) for i in range(len(trade_frame.index)): selected_frame = order_frame[order_frame['order_id'] == trade_frame['order_id'].iloc[i]] if len(selected_frame.index)>0: if selected_frame['strategy_class'].iloc[0]=='delta_hedge': trade_frame.loc[i,'alias'] = self.delta_alias elif self.trade_assignment_by_tickerhead: for key, value in self.trade_assignment_by_tickerhead.items(): trade_frame.loc[trade_frame['ticker_head'] == key,'alias'] = value else: trade_frame['alias'] = 'delta_Oct18_2' trade_frame = trade_frame[pd.notnull(trade_frame['alias'])] trade_frame.reset_index(inplace=True, drop=True) ts.load_trades_2strategy(trade_frame=trade_frame, con=self.con)
def get_tickers_from_agg_method_and_contracts_back(**kwargs): ticker = kwargs['ticker'] aggregation_method = kwargs['aggregation_method'] contracts_back = kwargs['contracts_back'] contact_specs_out = cmi.get_contract_specs(ticker) ref_date = 10000 * contact_specs_out[ 'ticker_year'] + 100 * contact_specs_out['ticker_month_num'] + 1 ref_datetime = cu.convert_doubledate_2datetime(ref_date) if aggregation_method == 12: cal_date_list = [ ref_datetime - relativedelta(years=x) for x in range(1, contracts_back + 1) ] elif aggregation_method == 1: cal_date_list = [ ref_datetime - relativedelta(months=x) for x in range(1, contracts_back + 1) ] ticker_list = [ contact_specs_out['ticker_head'] + cmi.full_letter_month_list[x.month - 1] + str(x.year) for x in cal_date_list ] return ticker_list
def convert_ticker_from_db2tt(db_ticker): if '-' in db_ticker: spreadQ = True ticker_list = db_ticker.split('-') else: spreadQ = False ticker_list = [db_ticker] contract_specs_list = [cmi.get_contract_specs(x) for x in ticker_list] ticker_head_list = [x['ticker_head'] for x in contract_specs_list] exchange_traded = cmi.get_exchange_traded(ticker_head_list[0]) if exchange_traded == 'CME': exchange_string = 'CME' elif exchange_traded == 'ICE': exchange_string = 'ICE_IPE' tt_ticker_head = su.get_key_in_dictionary(dictionary_input=conversion_from_tt_ticker_head, value=ticker_head_list[0]) maturity_string_list = [dt.date(x['ticker_year'],x['ticker_month_num'],1).strftime('%b%y') for x in contract_specs_list] if spreadQ: if exchange_traded == 'ICE': tt_ticker = exchange_string + ' ' + tt_ticker_head + ' Spread ' + maturity_string_list[0] + '-' + maturity_string_list[1] elif exchange_traded == 'CME': tt_ticker = exchange_string + ' Calendar- 1x' + tt_ticker_head + ' ' + maturity_string_list[0] + '--1x' + maturity_string_list[1] else: tt_ticker = exchange_string + ' ' + tt_ticker_head + ' ' + maturity_string_list[0] return tt_ticker
def get_futures_price_preloaded(**kwargs): if 'ticker_head' in kwargs.keys(): ticker_head = kwargs['ticker_head'] else: ticker = kwargs['ticker'] contract_specs_output = cmi.get_contract_specs(ticker) ticker_head = contract_specs_output['ticker_head'] file_ticker = cmi.mini_contract_dictionary.get( ticker_head, ticker_head) + contract_specs_output['ticker_month_str'] + str( contract_specs_output['ticker_year']) if 'futures_data_dictionary' in kwargs.keys(): data_out = kwargs['futures_data_dictionary'][ticker_head] else: presaved_futures_data_folder = dn.get_directory_name( ext='presaved_futures_data') file_ticker_head = cmi.mini_contract_dictionary.get( ticker_head, ticker_head) if os.path.isfile(presaved_futures_data_folder + '/' + file_ticker_head + '.pkl'): data_out = pd.read_pickle(presaved_futures_data_folder + '/' + file_ticker_head + '.pkl') else: data_out = pd.DataFrame() return data_out if 'settle_date' in kwargs.keys(): settle_date = kwargs['settle_date'] if isinstance(settle_date, int): data_out = data_out[data_out['settle_date'] == cu.convert_doubledate_2datetime(settle_date)] elif isinstance(settle_date, dt.datetime): data_out = data_out[data_out['settle_date'] == settle_date] if 'settle_date_from_exclusive' in kwargs.keys(): data_out = data_out[ data_out['settle_date'] > cu.convert_doubledate_2datetime( kwargs['settle_date_from_exclusive'])] if 'settle_date_from' in kwargs.keys(): data_out = data_out[ data_out['settle_date'] >= cu.convert_doubledate_2datetime( kwargs['settle_date_from'])] if 'settle_date_to' in kwargs.keys(): settle_date_to = kwargs['settle_date_to'] if isinstance(settle_date_to, int): data_out = data_out[ data_out['settle_date'] <= cu.convert_doubledate_2datetime( kwargs['settle_date_to'])] elif isinstance(settle_date_to, dt.datetime): data_out = data_out[data_out['settle_date'] <= settle_date_to] if 'ticker' in kwargs.keys(): data_out = data_out[data_out['ticker'] == file_ticker] return data_out
def request_spread_market_data(self): spread_ticker_list = self.price_request_dictionary['spread'] self.nonfinished_bid_price_list.extend(spread_ticker_list) self.nonfinished_ask_price_list.extend(spread_ticker_list) self.nonfinished_bid_quantity_list.extend(spread_ticker_list) self.nonfinished_ask_quantity_list.extend(spread_ticker_list) for i in range(len(spread_ticker_list)): self.bid_price_dictionary[spread_ticker_list[i]] = np.nan self.ask_price_dictionary[spread_ticker_list[i]] = np.nan self.bid_quantity_dictionary[spread_ticker_list[i]] = np.nan self.ask_quantity_dictionary[spread_ticker_list[i]] = np.nan self.fair_price_dictionary[spread_ticker_list[i]] = np.nan split_out = spread_ticker_list[i].split("-") ticker1 = split_out[0] ticker2 = split_out[1] contract_specs_output = cmi.get_contract_specs(ticker1) ticker_head = contract_specs_output['ticker_head'] exchange = cmi.get_ib_exchange_name(ticker_head) ib_ticker_head = su.get_key_in_dictionary( dictionary_input=tfl.conversion_from_tt_ticker_head, value=contract_specs_output['ticker_head']) spread_contract = Contract() spread_contract.symbol = ib_ticker_head spread_contract.secType = "BAG" spread_contract.currency = "USD" spread_contract.exchange = exchange leg1 = ComboLeg() leg1.conId = self.contractIDDictionary[ticker1] leg1.ratio = 1 leg1.action = "BUY" leg1.exchange = exchange leg2 = ComboLeg() leg2.conId = self.contractIDDictionary[ticker2] leg2.ratio = 1 leg2.action = "SELL" leg2.exchange = exchange spread_contract.comboLegs = [] spread_contract.comboLegs.append(leg1) spread_contract.comboLegs.append(leg2) self.market_data_ReqId_dictionary[ self.next_val_id] = spread_ticker_list[i] self.log.info('req id: ' + str(self.next_val_id) + ', spread_ticker:' + str(spread_ticker_list[i])) self.reqMktData(self.next_valid_id(), spread_contract, "", False, False, []) self.spread_contract_dictionary[ spread_ticker_list[i]] = spread_contract
def option_model_wrapper(**kwargs): # enter underlying, strike, (option_price or implied_vol) # calculation_date, exercise_type, option_type con = msu.get_my_sql_connection(**kwargs) ticker = kwargs['ticker'] calculation_date = kwargs['calculation_date'] if 'interest_rate_date' in kwargs.keys(): interest_rate_date = kwargs['interest_rate_date'] else: interest_rate_date = calculation_date #print(ticker) #print(kwargs['exercise_type']) contract_specs_output = cmi.get_contract_specs(ticker) ticker_head = contract_specs_output['ticker_head'] contract_multiplier = cmi.contract_multiplier[ticker_head] expiration_datetime = exp.get_expiration_from_db(ticker=ticker, instrument='options', con=con) expiration_date = int(expiration_datetime.strftime('%Y%m%d')) if 'con' not in kwargs.keys(): con.close() interest_rate = grfs.get_simple_rate( as_of_date=interest_rate_date, date_to=expiration_date)['rate_output'] if np.isnan(interest_rate): option_greeks = { 'implied_vol': np.NaN, 'delta': np.NaN, 'vega': np.NaN, 'dollar_vega': np.NaN, 'theta': np.NaN, 'dollar_theta': np.NaN, 'gamma': np.NaN, 'dollar_gamma': np.NaN, 'interest_rate': np.NaN, 'cal_dte': np.NaN } else: option_greeks = qom.get_option_greeks(risk_free_rate=interest_rate, expiration_date=expiration_date, **kwargs) option_greeks['implied_vol'] = 100 * option_greeks['implied_vol'] option_greeks[ 'dollar_vega'] = option_greeks['vega'] * contract_multiplier / 100 option_greeks[ 'dollar_theta'] = option_greeks['theta'] * contract_multiplier option_greeks[ 'dollar_gamma'] = option_greeks['gamma'] * contract_multiplier option_greeks['interest_rate'] = interest_rate return option_greeks
def get_greeks_4strategy_4date(**kwargs): con = msu.get_my_sql_connection(**kwargs) alias = kwargs['alias'] if 'as_of_date' in kwargs.keys(): as_of_date = kwargs['as_of_date'] else: as_of_date = exp.doubledate_shift_bus_days() position_frame = tas.get_net_position_4strategy_alias(alias=alias,as_of_date=as_of_date,con=con) options_frame = position_frame[position_frame['instrument'] == 'O'] if options_frame.empty: if 'con' not in kwargs.keys(): con.close() return {'ticker_portfolio': pd.DataFrame(columns=['total_oev', 'theta','dollar_vega','ticker']), 'strike_portfolio': pd.DataFrame()} unique_ticker_list = options_frame['ticker'].unique() result_list = [] contract_specs_output_list = [cmi.get_contract_specs(x) for x in unique_ticker_list] contract_multiplier_list = [cmi.contract_multiplier[x['ticker_head']] for x in contract_specs_output_list] for i in range(len(unique_ticker_list)): skew_output = gop.get_options_price_from_db(ticker=unique_ticker_list[i], settle_date=as_of_date, column_names=['option_type', 'strike', 'theta', 'vega', 'delta']) skew_output.reset_index(drop=True,inplace=True) skew_output['delta_diff'] = abs(skew_output['delta']-0.5) atm_point = skew_output.loc[skew_output['delta_diff'].idxmin()] skew_output.rename(columns={'strike': 'strike_price'}, inplace=True) merged_data = pd.merge(options_frame[options_frame['ticker'] == unique_ticker_list[i]], skew_output, how='left', on=['option_type','strike_price']) merged_data['oev'] = merged_data['vega']/atm_point['vega'] merged_data['total_oev'] = merged_data['oev']*merged_data['qty'] merged_data['dollar_theta'] = merged_data['theta']*merged_data['qty']*contract_multiplier_list[i] merged_data['dollar_vega'] = merged_data['vega']*merged_data['qty']*contract_multiplier_list[i]/100 result_list.append(merged_data) strike_portfolio = pd.concat(result_list) grouped = strike_portfolio.groupby('ticker') ticker_portfolio = pd.DataFrame() ticker_portfolio['ticker'] = (grouped['ticker'].first()).values ticker_portfolio['total_oev'] = (grouped['total_oev'].sum()).values ticker_portfolio['theta'] = (grouped['dollar_theta'].sum()).values ticker_portfolio['dollar_vega'] = (grouped['dollar_vega'].sum()).values if 'con' not in kwargs.keys(): con.close() return {'ticker_portfolio': ticker_portfolio, 'strike_portfolio': strike_portfolio }
def main(): app = Algo() ticker_list = ['HOK2018'] app.tick_size_dictionary = {x: cmi.tick_size[cmi.get_contract_specs(x)['ticker_head']] for x in ticker_list} app.log = lg.get_logger(file_identifier='ib_itf', log_level='INFO') app.ticker_list = ticker_list app.connect(client_id=6) app.run()
def get_clean_intraday_data(**kwargs): ticker = kwargs['ticker'] date_to = kwargs['date_to'] #print(ticker) if 'num_days_back' in kwargs.keys(): num_days_back = kwargs['num_days_back'] else: num_days_back = 10 if 'freq_str' in kwargs.keys(): freq_str = kwargs['freq_str'] else: freq_str = 'T' ticker_list = ticker.split('-') ticker_head = cmi.get_contract_specs(ticker_list[0])['ticker_head'] ticker_class = cmi.ticker_class[ticker_head] date_list = [exp.doubledate_shift_bus_days(double_date=date_to,shift_in_days=x) for x in reversed(range(1,num_days_back))] date_list.append(date_to) intraday_data = get_aligned_futures_data_intraday(contract_list=[ticker], date_list=date_list,freq_str=freq_str) if intraday_data.empty: return pd.DataFrame() intraday_data['time_stamp'] = [x.to_datetime() for x in intraday_data.index] intraday_data['hour_minute'] = [100*x.hour+x.minute for x in intraday_data['time_stamp']] intraday_data['settle_date'] = [x.date() for x in intraday_data['time_stamp']] end_datetime = cmi.last_trade_hour_minute[ticker_head] start_datetime = cmi.first_trade_hour_minute[ticker_head] end_hour_minute = 100*end_datetime.hour + end_datetime.minute start_hour_minute = 100*start_datetime.hour + start_datetime.minute if ticker_class in ['Ag']: start_hour_minute1 = 45 end_hour_minute1 = 745 selected_data = intraday_data[((intraday_data['hour_minute']< end_hour_minute1)&(intraday_data['hour_minute'] >= start_hour_minute1))| ((intraday_data['hour_minute'] < end_hour_minute)&(intraday_data['hour_minute'] >= start_hour_minute))] else: selected_data = intraday_data[(intraday_data['hour_minute'] < end_hour_minute)&(intraday_data['hour_minute'] >= start_hour_minute)] selected_data['mid_p'] = (selected_data['c1']['best_bid_p']+selected_data['c1']['best_ask_p'])/2 selected_data['total_traded_q'] = selected_data['c1']['total_traded_q'] return selected_data
def get_futures_days2_expiration(expiration_input): ticker = expiration_input['ticker'] date_to = expiration_input['date_to'] contract_specs_output = cmf.get_contract_specs(ticker) bday_us = CustomBusinessDay(calendar=get_calendar_4ticker_head(contract_specs_output['ticker_head'])) expiration_date = get_futures_expiration(ticker) dts = pd.date_range(start=cu.convert_doubledate_2datetime(date_to), end=expiration_date, freq=bday_us) return len(dts)-1
def get_futures_days2_expiration(expiration_input): ticker = expiration_input['ticker'] date_to = expiration_input['date_to'] contract_specs_output = cmf.get_contract_specs(ticker) bday_us = CustomBusinessDay(calendar=get_calendar_4ticker_head(contract_specs_output['ticker_head'])) expiration_date = get_futures_expiration(ticker) dts = pd.date_range(start=cu.convert_doubledate_2datetime(date_to), end=expiration_date, freq=bday_us) return len(dts)-1
def cal_greeks_4option_maturity(**kwargs): option_prices = gop.get_options_price_from_db(**kwargs) if option_prices.empty: return pd.DataFrame() option_prices = option_prices[option_prices['strike'] > 0] option_prices.reset_index(drop=True, inplace=True) contract_specs_out = cmi.get_contract_specs(kwargs['ticker']) exercise_type = cmi.get_option_exercise_type(**contract_specs_out) underlying_ticker = oput.get_option_underlying(**kwargs) futures_price_output = gfp.get_futures_price_preloaded( ticker=underlying_ticker, settle_date=kwargs['settle_date']) if futures_price_output.empty: return pd.DataFrame() underlying_price = futures_price_output['close_price'].iloc[0] expiration_datetime = exp.get_expiration_from_db(instrument='options', **kwargs) expiration_date = int(expiration_datetime.strftime('%Y%m%d')) interest_rate = grfs.get_simple_rate( as_of_date=kwargs['settle_date'], date_to=expiration_date)['rate_output'] option_greeks = [ qom.get_option_greeks(underlying=underlying_price, option_price=float( option_prices['close_price'].iloc[x]), strike=float(option_prices['strike'].iloc[x]), risk_free_rate=interest_rate, expiration_date=expiration_date, calculation_date=kwargs['settle_date'], option_type=option_prices['option_type'].iloc[x], exercise_type=exercise_type) for x in range(len(option_prices.index)) ] greek_frame = pd.DataFrame(option_greeks) return pd.concat([ greek_frame[['delta', 'gamma', 'implied_vol', 'theta', 'vega']], option_prices ], axis=1)
def get_data4datelist(**kwargs): ticker_list = kwargs['ticker_list'] date_list = kwargs['date_list'] ticker_head_list = [cmi.get_contract_specs(x)['ticker_head'] for x in ticker_list] if 'spread_weights' in kwargs.keys(): spread_weights = kwargs['spread_weights'] else: weights_output = sutil.get_spread_weights_4contract_list(ticker_head_list=ticker_head_list) spread_weights = weights_output['spread_weights'] num_contracts = len(ticker_list) ticker_class_list = [cmi.ticker_class[x] for x in ticker_head_list] intraday_data = opUtil.get_aligned_futures_data_intraday(contract_list=ticker_list, date_list=date_list) intraday_data['time_stamp'] = [x.to_datetime() for x in intraday_data.index] intraday_data['settle_date'] = intraday_data['time_stamp'].apply(lambda x: x.date()) intraday_data['hour_minute'] = [100*x.hour+x.minute for x in intraday_data['time_stamp']] end_hour = min([cmi.last_trade_hour_minute[x] for x in ticker_head_list]) start_hour = max([cmi.first_trade_hour_minute[x] for x in ticker_head_list]) if 'Ag' in ticker_class_list: start_hour1 = dt.time(0, 45, 0, 0) end_hour1 = dt.time(7, 45, 0, 0) selection_indx = [x for x in range(len(intraday_data.index)) if ((intraday_data['time_stamp'].iloc[x].time() < end_hour1) and(intraday_data['time_stamp'].iloc[x].time() >= start_hour1)) or ((intraday_data['time_stamp'].iloc[x].time() < end_hour) and(intraday_data['time_stamp'].iloc[x].time() >= start_hour))] else: selection_indx = [x for x in range(len(intraday_data.index)) if (intraday_data.index[x].to_datetime().time() < end_hour) and(intraday_data.index[x].to_datetime().time() >= start_hour)] intraday_data = intraday_data.iloc[selection_indx] intraday_data['spread'] = 0 for i in range(num_contracts): intraday_data['c' + str(i+1), 'mid_p'] = (intraday_data['c' + str(i+1)]['best_bid_p'] + intraday_data['c' + str(i+1)]['best_ask_p'])/2 intraday_data['spread'] = intraday_data['spread']+intraday_data['c' + str(i+1)]['mid_p']*spread_weights[i] return intraday_data
def generate_vcs_alias(**kwargs): vcs_row = kwargs['vcs_row'] ticker1 = vcs_row['ticker1'] ticker2 = vcs_row['ticker2'] ticker_head = vcs_row['tickerHead'] Q = vcs_row['Q'] contract_contract_specs1 = cmi.get_contract_specs(ticker1) contract_contract_specs2 = cmi.get_contract_specs(ticker2) ticker_year_short1 = contract_contract_specs1['ticker_year'] % 100 ticker_year_short2 = contract_contract_specs2['ticker_year'] % 100 alias = '' if Q < 50: alias = ticker_head + contract_contract_specs1['ticker_month_str'] + str(ticker_year_short1) + \ contract_contract_specs2['ticker_month_str'] + str(ticker_year_short2) + 'VCS' elif Q > 50: alias = ticker_head + contract_contract_specs2['ticker_month_str'] + str(ticker_year_short2) + \ contract_contract_specs1['ticker_month_str'] + str(ticker_year_short1) + 'VCS' return alias
def get_intraday_vcs(**kwargs): if 'report_date' in kwargs.keys(): report_date = kwargs['report_date'] else: report_date = exp.doubledate_shift_bus_days() atm_vol_ratio = kwargs['atm_vol_ratio'] vcs_output = vcs.generate_vcs_sheet_4date(date_to=report_date) vcs_pairs = vcs_output['vcs_pairs'] if 'id' in kwargs.keys(): id = kwargs['id'] ticker1 = vcs_pairs['ticker1'].iloc[id] ticker2 = vcs_pairs['ticker2'].iloc[id] else: ticker1 = kwargs['ticker1'] ticker2 = kwargs['ticker2'] ticker_head = cmi.get_contract_specs(ticker1)['ticker_head'] ticker_class = cmi.ticker_class[ticker_head] vcs_output = ops.get_vcs_signals(ticker_list=[ticker1, ticker2], settle_date=report_date, atm_vol_ratio=atm_vol_ratio) q = vcs_output['q'] q1 = vcs_output['q1'] filter_out = of.get_vcs_filters(data_frame_input=pd.DataFrame.from_dict({ 'tickerHead': [ticker_head], 'tickerClass': [ticker_class], 'Q': [q], 'Q1': [q1] }), filter_list=['long2', 'short2']) if filter_out['selected_frame'].empty: validQ = False else: validQ = True return { 'ticker1': ticker1, 'ticker2': ticker2, 'Q': q, 'Q1': q1, 'validQ': validQ }
def get_days2_expiration(**kwargs): date_to = kwargs['date_to'] datetime_to = cu.convert_doubledate_2datetime(date_to) expiration_datetime = get_expiration_from_db(**kwargs) contract_specs_output = cmf.get_contract_specs(kwargs['ticker']) bday_us = CustomBusinessDay(calendar=get_calendar_4ticker_head(contract_specs_output['ticker_head'])) dts = pd.date_range(start=datetime_to, end=expiration_datetime, freq=bday_us) return {'expiration_datetime': expiration_datetime, 'cal_dte': (expiration_datetime-datetime_to.date()).days, 'tr_dte': len(dts)-1}
def get_days2_expiration(**kwargs): date_to = kwargs['date_to'] datetime_to = cu.convert_doubledate_2datetime(date_to) expiration_datetime = get_expiration_from_db(**kwargs) contract_specs_output = cmf.get_contract_specs(kwargs['ticker']) bday_us = CustomBusinessDay(calendar=get_calendar_4ticker_head(contract_specs_output['ticker_head'])) dts = pd.date_range(start=datetime_to, end=expiration_datetime, freq=bday_us) return {'expiration_datetime': expiration_datetime, 'cal_dte': (expiration_datetime-datetime_to.date()).days, 'tr_dte': len(dts)-1}
def calc_intrday_pnl_from_prices(**kwargs): if 'as_of_date' in kwargs.keys(): as_of_date = kwargs['as_of_date'] else: as_of_date = exp.doubledate_shift_bus_days() net_position_frame = tas.get_net_position_4strategy_alias(alias=kwargs['alias'], as_of_date=as_of_date) net_position_frame['ticker_head'] = [cmi.get_contract_specs(x)['ticker_head'] for x in net_position_frame['ticker']] net_position_frame['contract_multiplier'] = [cmi.contract_multiplier[x] for x in net_position_frame['ticker_head']] con = msu.get_my_sql_connection(**kwargs) option_frame = net_position_frame[net_position_frame['instrument'] == 'O'] #option_frame = option_frame[(option_frame['strike_price'] == 54)|(option_frame['strike_price'] == 60)] #option_frame = option_frame[option_frame['strike_price'] == 112] #option_frame['qty'].loc[option_frame['ticker']=='LNV2016'] = -20 option_frame['close_price'] = [gop.get_options_price_from_db(ticker=option_frame['ticker'].iloc[x], strike=option_frame['strike_price'].iloc[x], option_type=option_frame['option_type'].iloc[x], con=con,settle_date=as_of_date)['close_price'][0] for x in range(len(option_frame.index))] structure_quantity = abs(option_frame['qty']).unique()[0] structure_multiplier = option_frame['contract_multiplier'].unique()[0] structure_price = sum(option_frame['close_price']*option_frame['qty'])/structure_quantity if structure_price<0: structure_quantity = -structure_quantity structure_price = -structure_price structure_pnl = structure_quantity*structure_multiplier*(kwargs['structure_price']-structure_price) futures_frame = net_position_frame[net_position_frame['instrument'] == 'F'] futures_frame['close_price'] = [gfp.get_futures_price_preloaded(ticker=x, settle_date=as_of_date)['close_price'].iloc[0] for x in futures_frame['ticker']] futures_frame['intraday_price'] = [kwargs[x] for x in futures_frame['ticker']] futures_frame['intraday_pnl'] = (futures_frame['intraday_price']-futures_frame['close_price'])*futures_frame['qty']*futures_frame['contract_multiplier'] if 'con' not in kwargs.keys(): con.close() return {'structure_pnl': structure_pnl, 'futures_pnl': futures_frame['intraday_pnl'].sum(),'structure_settle': structure_price}
def get_formatted_manual_entry_fills(**kwargs): fill_frame = pd.read_csv(dna.get_directory_name(ext='daily') + '/' + manual_trade_entry_file_name) formatted_frame = fill_frame formatted_frame.rename(columns={'optionType': 'option_type', 'strikePrice': 'strike_price', 'tradePrice': 'trade_price', 'quantity': 'trade_quantity'}, inplace=True) formatted_frame['strike_price'] = formatted_frame['strike_price'].astype('float64') formatted_frame['PQ'] = formatted_frame['trade_price']*formatted_frame['trade_quantity'] formatted_frame['instrument'] = 'O' formatted_frame.loc[formatted_frame['option_type'].isnull(),'instrument'] = 'F' formatted_frame.loc[[cmi.is_stockQ(x) for x in formatted_frame['ticker']], 'instrument'] = 'S' option_type = formatted_frame['option_type'] formatted_frame['option_type']= option_type.where(pd.notnull(option_type),None) option_indx = formatted_frame['instrument'] == 'O' formatted_frame['generalized_ticker'] = formatted_frame['ticker'] formatted_frame['generalized_ticker'][option_indx] = formatted_frame['ticker'][option_indx] + '-' + \ formatted_frame['option_type'][option_indx] + '-' + \ formatted_frame['strike_price'][option_indx].astype(str) formatted_frame['side'] = np.sign(formatted_frame['trade_quantity']) formatted_frame['ticker_head'] = [cmi.get_contract_specs(x)['ticker_head'] for x in formatted_frame['ticker']] grouped = formatted_frame.groupby(['generalized_ticker', 'side']) aggregate_trades = pd.DataFrame() aggregate_trades['trade_price'] = grouped['PQ'].sum()/grouped['trade_quantity'].sum() aggregate_trades['trade_quantity'] = grouped['trade_quantity'].sum() aggregate_trades['ticker'] = grouped['ticker'].first() aggregate_trades['ticker_head'] = grouped['ticker_head'].first() aggregate_trades['instrument'] = grouped['instrument'].first() aggregate_trades['option_type'] = grouped['option_type'].first() aggregate_trades['strike_price'] = grouped['strike_price'].first() aggregate_trades['real_tradeQ'] = True return {'raw_trades': fill_frame, 'aggregate_trades': aggregate_trades }
def calc_intraday_structure_pnl_from_prices(**kwargs): if 'as_of_date' in kwargs.keys(): as_of_date = kwargs['as_of_date'] else: as_of_date = exp.doubledate_shift_bus_days() con = msu.get_my_sql_connection(**kwargs) structure_type = kwargs['structure_type'] structure_price = kwargs['structure_price'] ticker_list = kwargs['ticker_list'] strike_list = kwargs['strike_list'] underlying_price_list = kwargs['underlying_price_list'] qty = kwargs['qty'] if structure_type == 'straddle_spread': option_frame = pd.DataFrame.from_items([('ticker', [ticker_list[0], ticker_list[0], ticker_list[1], ticker_list[1]]), ('option_type', ['C', 'P', 'C', 'P']), ('strike_price', [strike_list[0], strike_list[0], strike_list[1],strike_list[1]]), ('qty', [-1, -1, 1, 1])]) option_price_output = [gop.get_options_price_from_db(ticker=option_frame['ticker'].iloc[x], strike=option_frame['strike_price'].iloc[x], option_type=option_frame['option_type'].iloc[x], con=con,settle_date=as_of_date,column_names=['close_price','delta']) for x in range(len(option_frame.index))] option_frame['delta'] = [option_price_output[x]['delta'][0] for x in range(len(option_frame.index))] option_frame['close_price'] = [option_price_output[x]['close_price'][0] for x in range(len(option_frame.index))] option_frame['PQ'] = option_frame['close_price']*option_frame['qty'] option_frame['signed_delta'] = option_frame['delta']*option_frame['qty'] delta_list = [option_frame[option_frame['ticker'] == x]['signed_delta'].sum() for x in ticker_list] ticker_head = cmi.get_contract_specs(ticker_list[0])['ticker_head'] contract_multiplier = cmi.contract_multiplier[ticker_head] structure_price_yesterday = option_frame['PQ'].sum() structure_pnl = qty*(structure_price-structure_price_yesterday)*contract_multiplier underlying_ticker_list = [oputil.get_option_underlying(ticker=x) for x in ticker_list] underlying_price_list_yesterday = [gfp.get_futures_price_preloaded(ticker=x, settle_date=as_of_date)['close_price'].iloc[0] for x in underlying_ticker_list] delta_pnl = contract_multiplier*sum([-delta_list[x]*qty*(underlying_price_list[x]-underlying_price_list_yesterday[x]) for x in range(len(delta_list))]) return {'total_pnl': structure_pnl+delta_pnl, 'structure_pnl': structure_pnl, 'delta_pnl': delta_pnl, 'structure_price_yesterday': structure_price_yesterday }
def get_ib_contract_from_db_ticker(**kwargs): sec_type = kwargs['sec_type'] ticker = kwargs['ticker'] contract_out = Contract() contract_specs_output = cmi.get_contract_specs(ticker) ticker_head = contract_specs_output['ticker_head'] if sec_type in ['F', 'OF']: secType = "FUT" currency = 'USD' ib_ticker_head = su.get_key_in_dictionary(dictionary_input=conversion_from_ib_ticker_head, value=contract_specs_output['ticker_head']) ib_contract_month = str(contract_specs_output['ticker_year']*100 + contract_specs_output['ticker_month_num']) exchange = cmi.get_ib_exchange_name(contract_specs_output['ticker_head']) contract_out.secType = secType contract_out.symbol = ib_ticker_head contract_out.exchange = exchange contract_out.currency = currency contract_out.lastTradeDateOrContractMonth = ib_contract_month if sec_type=='OF': contract_out.secType = "FOP" if 'option_type' in kwargs.keys(): contract_out.right = kwargs['option_type'] if 'strike' in kwargs.keys(): contract_out.strike = str(round(kwargs['strike'],2)) contract_out.tradingClass = ib_option_trading_class_dictionary[ticker_head] contract_out.multiplier = ib_multiplier_dictionary.get(ticker_head, 1) if sec_type=='S': contract_out.secType = 'STK' contract_out.symbol = ticker contract_out.currency = 'USD' contract_out.exchange = smi.get_ib_exchange_name(ticker) return contract_out
def get_ttapi_filename(**kwargs): ticker = kwargs['ticker'] contract_specs_output = cmi.get_contract_specs(ticker) ticker_head = contract_specs_output['ticker_head'] exchange_traded = cmi.get_exchange_traded(ticker_head) ttapi_ticker_head = su.get_key_in_dictionary(dictionary_input=tfl.conversion_from_tt_ticker_head, value=ticker_head) if exchange_traded == 'CME': exchange_string = 'CME' elif exchange_traded == 'ICE': exchange_string = 'ICE_IPE' maturity_string = dt.date(contract_specs_output['ticker_year'],contract_specs_output['ticker_month_num'],1).strftime('%b%y') return exchange_string + ' ' + ttapi_ticker_head + ' ' + maturity_string + '.csv'
def get_formatted_manual_entry_fills(**kwargs): fill_frame = pd.read_csv(dn.get_directory_name(ext='daily') + '/' + manual_trade_entry_file_name) formatted_frame = fill_frame formatted_frame.rename(columns={'optionType': 'option_type', 'strikePrice': 'strike_price', 'tradePrice': 'trade_price', 'quantity': 'trade_quantity'}, inplace=True) formatted_frame['strike_price'] = formatted_frame['strike_price'].astype('float64') formatted_frame['PQ'] = formatted_frame['trade_price']*formatted_frame['trade_quantity'] formatted_frame['instrument'] = 'O' formatted_frame.loc[formatted_frame['option_type'].isnull(),'instrument'] = 'F' option_type = formatted_frame['option_type'] formatted_frame['option_type']= option_type.where(pd.notnull(option_type),None) option_indx = formatted_frame['instrument'] == 'O' formatted_frame['generalized_ticker'] = formatted_frame['ticker'] formatted_frame['generalized_ticker'][option_indx] = formatted_frame['ticker'][option_indx] + '-' + \ formatted_frame['option_type'][option_indx] + '-' + \ formatted_frame['strike_price'][option_indx].astype(str) formatted_frame['side'] = np.sign(formatted_frame['trade_quantity']) formatted_frame['ticker_head'] = [cmi.get_contract_specs(x)['ticker_head'] for x in formatted_frame['ticker']] grouped = formatted_frame.groupby(['generalized_ticker', 'side']) aggregate_trades = pd.DataFrame() aggregate_trades['trade_price'] = grouped['PQ'].sum()/grouped['trade_quantity'].sum() aggregate_trades['trade_quantity'] = grouped['trade_quantity'].sum() aggregate_trades['ticker'] = grouped['ticker'].first() aggregate_trades['ticker_head'] = grouped['ticker_head'].first() aggregate_trades['instrument'] = grouped['instrument'].first() aggregate_trades['option_type'] = grouped['option_type'].first() aggregate_trades['strike_price'] = grouped['strike_price'].first() aggregate_trades['real_tradeQ'] = True return {'raw_trades': fill_frame, 'aggregate_trades': aggregate_trades }
def get_option_underlying(**kwargs): ticker = kwargs['ticker'] contract_specs_output = cmi.get_contract_specs(ticker) ticker_head = contract_specs_output['ticker_head'] ticker_month_num = contract_specs_output['ticker_month_num'] ticker_year = contract_specs_output['ticker_year'] if ticker_head == 'E0': ticker_head = 'ED' ticker_year = ticker_year + 1 elif ticker_head == 'E2': ticker_head = 'ED' ticker_year = ticker_year + 2 elif ticker_head == 'E3': ticker_head = 'ED' ticker_year = ticker_year + 3 elif ticker_head == 'E4': ticker_head = 'ED' ticker_year = ticker_year + 4 elif ticker_head == 'E5': ticker_head = 'ED' ticker_year = ticker_year + 5 futures_contract_months = cmi.futures_contract_months[ticker_head] futures_contract_month_numbers = [ cmi.letter_month_string.find(x) + 1 for x in futures_contract_months ] leading_months = [ x for x in futures_contract_month_numbers if x >= ticker_month_num ] if len(leading_months) > 0: underlying_month_num = leading_months[0] underlying_month_year = ticker_year else: underlying_month_num = futures_contract_month_numbers[0] underlying_month_year = ticker_year + 1 return ticker_head + cmi.letter_month_string[ underlying_month_num - 1] + str(underlying_month_year)
def get_tickers_from_agg_method_and_contracts_back(**kwargs): ticker = kwargs['ticker'] aggregation_method = kwargs['aggregation_method'] contracts_back = kwargs['contracts_back'] contact_specs_out = cmi.get_contract_specs(ticker) ref_date = 10000*contact_specs_out['ticker_year']+100*contact_specs_out['ticker_month_num']+1 ref_datetime = cu.convert_doubledate_2datetime(ref_date) if aggregation_method == 12: cal_date_list = [ref_datetime - relativedelta(years=x) for x in range(1, contracts_back+1)] elif aggregation_method == 1: cal_date_list = [ref_datetime - relativedelta(months=x) for x in range(1, contracts_back+1)] ticker_list = [contact_specs_out['ticker_head'] + cmi.full_letter_month_list[x.month-1] + str(x.year) for x in cal_date_list] return ticker_list
def calc_realized_vol_4options_ticker(**kwargs): ticker = kwargs['ticker'] contract_specs_output = cmi.get_contract_specs(ticker) if contract_specs_output['ticker_class'] in ['Index', 'FX', 'Metal']: use_proxy_contract = True else: use_proxy_contract = False if use_proxy_contract: if 'futures_data_dictionary' in kwargs.keys(): futures_data_input = {'ticker_head': contract_specs_output['ticker_head'],'settle_date': kwargs['settle_date']} futures_data_input['futures_data_dictionary'] = kwargs['futures_data_dictionary'] data_out = gfp.get_futures_price_preloaded(**futures_data_input) data_out = data_out.reset_index() kwargs['ticker'] = data_out['ticker'].loc[data_out['volume'].idxmax()] else: kwargs['ticker'] = omu.get_option_underlying(**kwargs) return calc_realized_vol_4futures_ticker(**kwargs)
def get_tick_data(**kwargs): ticker = kwargs['ticker'] contract_specs_output = cmi.get_contract_specs(ticker) ticker_head = tickerhead_dict_from_db_2qg.get(contract_specs_output['ticker_head'],contract_specs_output['ticker_head']) output_file = 'D:/Research/test_file.csv.gz' try: os.remove(output_file) except OSError: pass if 'boto_client' in kwargs.keys(): boto_client = kwargs['boto_client'] else: boto_client = get_boto_client() utc_doubledate = kwargs['utc_doubledate'] utc_year = m.floor(utc_doubledate/10000) key_aux = ticker_head + '/' + ticker_head + contract_specs_output['ticker_month_str'] + str(contract_specs_output['ticker_year']%10) + '.csv.gz' if utc_year in [2017, 2018]: bucket = 'us-futures-taq-' + str(utc_year) key = str(utc_doubledate) + '/' + key_aux else: bucket = 'us-futures-taq' key = str(utc_year) + '/' + str(utc_doubledate) + '/' + key_aux try: boto_client.download_file(bucket, key, output_file,{'RequestPayer': 'requester'}) data_out = pd.read_table(output_file, compression='gzip', sep=',') except Exception as e: print(ticker + ', ' + str(utc_doubledate)) print(e) data_out = pd.DataFrame() return data_out
def get_intraday_vcs(**kwargs): if 'report_date' in kwargs.keys(): report_date = kwargs['report_date'] else: report_date = exp.doubledate_shift_bus_days() id = kwargs['id'] atm_vol_ratio = kwargs['atm_vol_ratio'] vcs_output = vcs.generate_vcs_sheet_4date(date_to=report_date) vcs_pairs = vcs_output['vcs_pairs'] ticker1 = vcs_pairs['ticker1'].iloc[id] ticker2 = vcs_pairs['ticker2'].iloc[id] ticker_head = cmi.get_contract_specs(ticker1)['ticker_head'] ticker_class = cmi.ticker_class[ticker_head] vcs_output = ops.get_vcs_signals(ticker_list=[ticker1, ticker2],settle_date=report_date,atm_vol_ratio=atm_vol_ratio) q = vcs_output['q'] q1 = vcs_output['q1'] filter_out = of.get_vcs_filters(data_frame_input=pd.DataFrame.from_items([('tickerHead', [ticker_head]), ('tickerClass', [ticker_class]), ('Q', [q]), ('Q1', [q1])]), filter_list=['long2', 'short2']) if filter_out['selected_frame'].empty: validQ = False else: validQ = True print(ticker1) print(ticker2) print('Q: ' + str(q)) print('Q1: ' + str(q1)) print('Valid?: ' + str(validQ))
def cal_greeks_4option_maturity(**kwargs): option_prices = gop.get_options_price_from_db(**kwargs) if option_prices.empty: return pd.DataFrame() contract_specs_out = cmi.get_contract_specs(kwargs['ticker']) exercise_type = cmi.get_option_exercise_type(**contract_specs_out) underlying_ticker = oput.get_option_underlying(**kwargs) futures_price_output = gfp.get_futures_price_preloaded(ticker=underlying_ticker, settle_date=kwargs['settle_date']) if futures_price_output.empty: return pd.DataFrame() underlying_price = futures_price_output['close_price'].iloc[0] expiration_datetime = exp.get_expiration_from_db(instrument='options', **kwargs) expiration_date = int(expiration_datetime.strftime('%Y%m%d')) interest_rate = grfs.get_simple_rate(as_of_date=kwargs['settle_date'], date_to=expiration_date)['rate_output'] #print(kwargs['settle_date']) #print(expiration_date) option_greeks = [qom.get_option_greeks(underlying=underlying_price, option_price=float(option_prices['close_price'].iloc[x]), strike=float(option_prices['strike'].iloc[x]), risk_free_rate=interest_rate, expiration_date=expiration_date, calculation_date=kwargs['settle_date'], option_type=option_prices['option_type'].iloc[x], exercise_type=exercise_type) for x in range(len(option_prices.index))] greek_frame = pd.DataFrame(option_greeks) return pd.concat([greek_frame[['delta', 'gamma', 'implied_vol', 'theta', 'vega']], option_prices], axis=1)
def load_price_data_4ticker(load_price_data_input): ticker = load_price_data_input['ticker'] expiration_date = load_price_data_input['expiration_date'] data_vendor_id = load_price_data_input['data_vendor_id'] symbol_id = load_price_data_input['symbol_id'] quandl_input = {'ticker': ticker} if 'date_to' in load_price_data_input.keys(): quandl_input['date_to'] = load_price_data_input['date_to'] if 'date_from' in load_price_data_input.keys() and load_price_data_input['date_from'] is not None: quandl_input['date_from'] = load_price_data_input['date_from'] quandl_out = gdq.get_daily_historic_data_quandl(**quandl_input) if not quandl_out['success']: return price_data = quandl_out['data_out'] if price_data.empty: print('Empty Results For ' + ticker) return contract_specs_output = cmi.get_contract_specs(ticker) if contract_specs_output['ticker_head'] == 'JY': price_multiplier = 10 else: price_multiplier = 1 bday_us = CustomBusinessDay(calendar=exp.get_calendar_4ticker_head(contract_specs_output['ticker_head'])) dts = pd.date_range(start=price_data.index[0], end=expiration_date, freq=bday_us) dts = [x.date() for x in dts] now = datetime.datetime.now() price_data['price_date'] = pd.Series(price_data.index, index=price_data.index) column_names = price_data.columns.tolist() open_indx = column_names.index('Open') high_indx = column_names.index('High') low_indx = column_names.index('Low') settle_indx = column_names.index('Settle') volume_indx = column_names.index('Volume') interest_indx = column_names.index('Open Interest') date_indx = column_names.index('price_date') tuples = [tuple([data_vendor_id, symbol_id, contract_specs_output['ticker_head'], contract_specs_output['ticker_month_num'], x[date_indx].to_datetime().date(), (expiration_date-x[date_indx].to_datetime().date()).days, len([y for y in dts if y > x[date_indx].to_datetime().date()]), now, now, None if np.isnan(x[open_indx]) else price_multiplier*x[open_indx], None if np.isnan(x[high_indx]) else price_multiplier*x[high_indx], None if np.isnan(x[low_indx]) else price_multiplier*x[low_indx], None if np.isnan(x[settle_indx]) else price_multiplier*x[settle_indx], None if np.isnan(x[volume_indx]) else x[volume_indx], None if np.isnan(x[interest_indx]) else x[interest_indx]]) for x in price_data.values] column_str = "data_vendor_id, symbol_id, ticker_head, ticker_month, price_date,cal_dte, tr_dte, created_date,last_updated_date, open_price, high_price, low_price, close_price, volume, open_interest" insert_str = ("%s, " * 15)[:-2] final_str = "REPLACE INTO daily_price (%s) VALUES (%s)" % (column_str, insert_str) con = msu.get_my_sql_connection(**load_price_data_input) msu.sql_execute_many_wrapper(final_str=final_str, tuples=tuples, con=con) if 'con' not in load_price_data_input.keys(): con.close()
def get_intraday_spread_signals(**kwargs): ticker_list = kwargs['ticker_list'] date_to = kwargs['date_to'] ticker_list = [x for x in ticker_list if x is not None] ticker_head_list = [cmi.get_contract_specs(x)['ticker_head'] for x in ticker_list] ticker_class_list = [cmi.ticker_class[x] for x in ticker_head_list] print('-'.join(ticker_list)) if 'tr_dte_list' in kwargs.keys(): tr_dte_list = kwargs['tr_dte_list'] else: tr_dte_list = [exp.get_days2_expiration(ticker=x,date_to=date_to, instrument='futures')['tr_dte'] for x in ticker_list] weights_output = sutil.get_spread_weights_4contract_list(ticker_head_list=ticker_head_list) if 'aggregation_method' in kwargs.keys() and 'contracts_back' in kwargs.keys(): aggregation_method = kwargs['aggregation_method'] contracts_back = kwargs['contracts_back'] else: amcb_output = [opUtil.get_aggregation_method_contracts_back(cmi.get_contract_specs(x)) for x in ticker_list] aggregation_method = max([x['aggregation_method'] for x in amcb_output]) contracts_back = min([x['contracts_back'] for x in amcb_output]) if 'futures_data_dictionary' in kwargs.keys(): futures_data_dictionary = kwargs['futures_data_dictionary'] else: futures_data_dictionary = {x: gfp.get_futures_price_preloaded(ticker_head=x) for x in list(set(ticker_head_list))} if 'use_last_as_current' in kwargs.keys(): use_last_as_current = kwargs['use_last_as_current'] else: use_last_as_current = True if 'datetime5_years_ago' in kwargs.keys(): datetime5_years_ago = kwargs['datetime5_years_ago'] else: date5_years_ago = cu.doubledate_shift(date_to,5*365) datetime5_years_ago = cu.convert_doubledate_2datetime(date5_years_ago) if 'num_days_back_4intraday' in kwargs.keys(): num_days_back_4intraday = kwargs['num_days_back_4intraday'] else: num_days_back_4intraday = 5 contract_multiplier_list = [cmi.contract_multiplier[x] for x in ticker_head_list] aligned_output = opUtil.get_aligned_futures_data(contract_list=ticker_list, tr_dte_list=tr_dte_list, aggregation_method=aggregation_method, contracts_back=contracts_back, date_to=date_to, futures_data_dictionary=futures_data_dictionary, use_last_as_current=use_last_as_current) aligned_data = aligned_output['aligned_data'] current_data = aligned_output['current_data'] spread_weights = weights_output['spread_weights'] portfolio_weights = weights_output['portfolio_weights'] aligned_data['spread'] = 0 aligned_data['spread_pnl_1'] = 0 aligned_data['spread_pnl1'] = 0 spread_settle = 0 last5_years_indx = aligned_data['settle_date']>=datetime5_years_ago num_contracts = len(ticker_list) for i in range(num_contracts): aligned_data['spread'] = aligned_data['spread']+aligned_data['c' + str(i+1)]['close_price']*spread_weights[i] spread_settle = spread_settle + current_data['c' + str(i+1)]['close_price']*spread_weights[i] aligned_data['spread_pnl_1'] = aligned_data['spread_pnl_1']+aligned_data['c' + str(i+1)]['change_1']*portfolio_weights[i]*contract_multiplier_list[i] aligned_data['spread_pnl1'] = aligned_data['spread_pnl1']+aligned_data['c' + str(i+1)]['change1_instant']*portfolio_weights[i]*contract_multiplier_list[i] aligned_data['spread_normalized'] = aligned_data['spread']/aligned_data['c1']['close_price'] data_last5_years = aligned_data[last5_years_indx] percentile_vector = stats.get_number_from_quantile(y=data_last5_years['spread_pnl_1'].values, quantile_list=[1, 15, 85, 99], clean_num_obs=max(100, round(3*len(data_last5_years.index)/4))) downside = (percentile_vector[0]+percentile_vector[1])/2 upside = (percentile_vector[2]+percentile_vector[3])/2 date_list = [exp.doubledate_shift_bus_days(double_date=date_to,shift_in_days=x) for x in reversed(range(1,num_days_back_4intraday))] date_list.append(date_to) intraday_data = opUtil.get_aligned_futures_data_intraday(contract_list=ticker_list, date_list=date_list) intraday_data['time_stamp'] = [x.to_datetime() for x in intraday_data.index] intraday_data['settle_date'] = intraday_data['time_stamp'].apply(lambda x: x.date()) end_hour = min([cmi.last_trade_hour_minute[x] for x in ticker_head_list]) start_hour = max([cmi.first_trade_hour_minute[x] for x in ticker_head_list]) trade_start_hour = dt.time(9, 30, 0, 0) if 'Ag' in ticker_class_list: start_hour1 = dt.time(0, 45, 0, 0) end_hour1 = dt.time(7, 45, 0, 0) selection_indx = [x for x in range(len(intraday_data.index)) if ((intraday_data['time_stamp'].iloc[x].time() < end_hour1) and(intraday_data['time_stamp'].iloc[x].time() >= start_hour1)) or ((intraday_data['time_stamp'].iloc[x].time() < end_hour) and(intraday_data['time_stamp'].iloc[x].time() >= start_hour))] else: selection_indx = [x for x in range(len(intraday_data.index)) if (intraday_data.index[x].to_datetime().time() < end_hour) and(intraday_data.index[x].to_datetime().time() >= start_hour)] intraday_data = intraday_data.iloc[selection_indx] intraday_data['spread'] = 0 for i in range(num_contracts): intraday_data['c' + str(i+1), 'mid_p'] = (intraday_data['c' + str(i+1)]['best_bid_p'] + intraday_data['c' + str(i+1)]['best_ask_p'])/2 intraday_data['spread'] = intraday_data['spread']+intraday_data['c' + str(i+1)]['mid_p']*spread_weights[i] unique_settle_dates = intraday_data['settle_date'].unique() intraday_data['spread1'] = np.nan for i in range(len(unique_settle_dates)-1): if (intraday_data['settle_date'] == unique_settle_dates[i]).sum() == \ (intraday_data['settle_date'] == unique_settle_dates[i+1]).sum(): intraday_data.loc[intraday_data['settle_date'] == unique_settle_dates[i],'spread1'] = \ intraday_data['spread'][intraday_data['settle_date'] == unique_settle_dates[i+1]].values intraday_data = intraday_data[intraday_data['settle_date'].notnull()] intraday_mean = intraday_data['spread'].mean() intraday_std = intraday_data['spread'].std() intraday_data_last2days = intraday_data[intraday_data['settle_date'] >= cu.convert_doubledate_2datetime(date_list[-2]).date()] intraday_data_yesterday = intraday_data[intraday_data['settle_date'] == cu.convert_doubledate_2datetime(date_list[-1]).date()] intraday_mean2 = intraday_data_last2days['spread'].mean() intraday_std2 = intraday_data_last2days['spread'].std() intraday_mean1 = intraday_data_yesterday['spread'].mean() intraday_std1 = intraday_data_yesterday['spread'].std() intraday_z = (spread_settle-intraday_mean)/intraday_std num_obs_intraday = len(intraday_data.index) num_obs_intraday_half = round(num_obs_intraday/2) intraday_tail = intraday_data.tail(num_obs_intraday_half) num_positives = sum(intraday_tail['spread'] > intraday_data['spread'].mean()) num_negatives = sum(intraday_tail['spread'] < intraday_data['spread'].mean()) recent_trend = 100*(num_positives-num_negatives)/(num_positives+num_negatives) pnl_frame = ifs.get_pnl_4_date_range(date_to=date_to, num_bus_days_back=20, ticker_list=ticker_list) if (len(pnl_frame.index)>15)&(pnl_frame['total_pnl'].std() != 0): historical_sharp = (250**(0.5))*pnl_frame['total_pnl'].mean()/pnl_frame['total_pnl'].std() else: historical_sharp = np.nan return {'downside': downside, 'upside': upside,'intraday_data': intraday_data, 'z': intraday_z,'recent_trend': recent_trend, 'intraday_mean': intraday_mean, 'intraday_std': intraday_std, 'intraday_mean2': intraday_mean2, 'intraday_std2': intraday_std2, 'intraday_mean1': intraday_mean1, 'intraday_std1': intraday_std1, 'aligned_output': aligned_output, 'spread_settle': spread_settle, 'data_last5_years': data_last5_years,'historical_sharp':historical_sharp}
def get_book_snapshot_4ticker(**kwargs): if 'folder_date' in kwargs.keys(): folder_date = kwargs['folder_date'] else: folder_date = exp.doubledate_shift_bus_days() if 'freq_str' in kwargs.keys(): freq_str = kwargs['freq_str'] else: freq_str = 'T' ticker = kwargs['ticker'] data_dir = dn.get_dated_directory_extension(ext='intraday_ttapi_data_fixed_interval', folder_date=folder_date) file_name = data_dir + '/' + ticker + '_' + freq_str + '.pkl' if os.path.isfile(file_name): book_snapshot = pd.read_pickle(file_name) return book_snapshot data_frame_out = load_csv_file_4ticker(**kwargs) if data_frame_out.empty: return pd.DataFrame(columns=['best_bid_p','best_bid_q','best_ask_p','best_ask_q']) start_datetime = dt.datetime.utcfromtimestamp(data_frame_out['time'].values[0].tolist()/1e9).replace(microsecond=0, second=0) end_datetime = dt.datetime.utcfromtimestamp(data_frame_out['time'].values[-1].tolist()/1e9).replace(microsecond=0, second=0) merged_index = pd.date_range(start=start_datetime,end=end_datetime,freq=freq_str) data_frame_out.set_index('time', inplace=True, drop=True) best_bid_p = data_frame_out[data_frame_out['field'] == 'BestBidPrice'] best_bid_p = best_bid_p.groupby(best_bid_p.index).last() best_bid_p = best_bid_p.reindex(merged_index,method='pad') best_bid_q = data_frame_out[data_frame_out['field'] == 'BestBidQuantity'] best_bid_q = best_bid_q.groupby(best_bid_q.index).last() best_bid_q = best_bid_q.reindex(merged_index,method='pad') best_ask_p = data_frame_out[data_frame_out['field'] == 'BestAskPrice'] best_ask_p = best_ask_p.groupby(best_ask_p.index).last() best_ask_p = best_ask_p.reindex(merged_index,method='pad') best_ask_q = data_frame_out[data_frame_out['field'] == 'BestAskQuantity'] best_ask_q = best_ask_q.groupby(best_ask_q.index).last() best_ask_q = best_ask_q.reindex(merged_index,method='pad') book_snapshot = pd.DataFrame(index=merged_index) book_snapshot['best_bid_p'] = best_bid_p['value'].astype('float64') book_snapshot['best_bid_q'] = best_bid_q['value'] book_snapshot['best_ask_p'] = best_ask_p['value'].astype('float64') book_snapshot['best_ask_q'] = best_ask_q['value'] ticker_head = cmi.get_contract_specs(kwargs['ticker'])['ticker_head'] book_snapshot['best_bid_p'] = [tfl.convert_trade_price_from_tt(price=x,ticker_head=ticker_head) for x in book_snapshot['best_bid_p']] book_snapshot['best_ask_p'] = [tfl.convert_trade_price_from_tt(price=x,ticker_head=ticker_head) for x in book_snapshot['best_ask_p']] book_snapshot.to_pickle(file_name) return book_snapshot
def get_intraday_trend_signals(**kwargs): ticker = kwargs['ticker'] date_to = kwargs['date_to'] datetime_to = cu.convert_doubledate_2datetime(date_to) breakout_method = 2 #print(ticker) ticker_head = cmi.get_contract_specs(ticker)['ticker_head'] contract_multiplier = cmi.contract_multiplier[ticker_head] ticker_class = cmi.ticker_class[ticker_head] daily_settles = gfp.get_futures_price_preloaded(ticker=ticker) daily_settles = daily_settles[daily_settles['settle_date'] <= datetime_to] daily_settles['ewma10'] = pd.ewma(daily_settles['close_price'], span=10) daily_settles['ewma50'] = pd.ewma(daily_settles['close_price'], span=50) if daily_settles['ewma10'].iloc[-1] > daily_settles['ewma50'].iloc[-1]: long_term_trend = 1 else: long_term_trend = -1 date_list = [exp.doubledate_shift_bus_days(double_date=date_to,shift_in_days=1)] date_list.append(date_to) intraday_data = opUtil.get_aligned_futures_data_intraday(contract_list=[ticker], date_list=date_list) intraday_data['time_stamp'] = [x.to_datetime() for x in intraday_data.index] intraday_data['hour_minute'] = [100*x.hour+x.minute for x in intraday_data['time_stamp']] end_hour = cmi.last_trade_hour_minute[ticker_head] start_hour = cmi.first_trade_hour_minute[ticker_head] if ticker_class in ['Ag']: start_hour1 = dt.time(0, 45, 0, 0) end_hour1 = dt.time(7, 45, 0, 0) selection_indx = [x for x in range(len(intraday_data.index)) if ((intraday_data['time_stamp'].iloc[x].time() < end_hour1) and(intraday_data['time_stamp'].iloc[x].time() >= start_hour1)) or ((intraday_data['time_stamp'].iloc[x].time() < end_hour) and(intraday_data['time_stamp'].iloc[x].time() >= start_hour))] else: selection_indx = [x for x in range(len(intraday_data.index)) if (intraday_data.index[x].to_datetime().time() < end_hour) and(intraday_data.index[x].to_datetime().time() >= start_hour)] selected_data = intraday_data.iloc[selection_indx] selected_data['mid_p'] = (selected_data['c1']['best_bid_p']+selected_data['c1']['best_ask_p'])/2 selected_data['ewma100'] = pd.ewma(selected_data['mid_p'], span=100) selected_data['ewma25'] = pd.ewma(selected_data['mid_p'], span=25) selected_data.reset_index(inplace=True,drop=True) datetime_to = cu.convert_doubledate_2datetime(date_to) range_start = dt.datetime.combine(datetime_to,dt.time(8,30,0,0)) range_end = dt.datetime.combine(datetime_to,dt.time(9,0,0,0)) first_30_minutes = selected_data[(selected_data['time_stamp'] >= range_start)& (selected_data['time_stamp'] <= range_end)] trading_data = selected_data[selected_data['time_stamp'] > range_end] trading_data_shifted = trading_data.shift(5) range_min = first_30_minutes['mid_p'].min() range_max = first_30_minutes['mid_p'].max() initial_range = range_max-range_min if breakout_method == 1: bullish_breakout = trading_data[(trading_data['mid_p'] > range_max)& (trading_data['mid_p'] < range_max+0.5*initial_range)& (trading_data_shifted['mid_p']<range_max)& (trading_data['ewma25'] > range_max)& (trading_data['mid_p'] > trading_data['ewma100'])] bearish_breakout = trading_data[(trading_data['mid_p'] < range_min)& (trading_data['mid_p'] > range_min-0.5*initial_range)& (trading_data_shifted['mid_p']>range_min)& (trading_data['ewma25'] < range_min)& (trading_data['mid_p'] < trading_data['ewma100'])] elif breakout_method == 2: bullish_breakout = pd.DataFrame() bearish_breakout = pd.DataFrame() if long_term_trend > 0: bullish_breakout = trading_data[(trading_data['mid_p'] > range_max)& (trading_data_shifted['mid_p']<range_max)& (long_term_trend == 1)& (trading_data['mid_p'] > trading_data['ewma100'])] elif long_term_trend < 0: bearish_breakout = trading_data[(trading_data['mid_p'] < range_min)& (trading_data_shifted['mid_p']>range_min)& (long_term_trend == -1)& (trading_data['mid_p'] < trading_data['ewma100'])] bullish_cross = trading_data[trading_data['mid_p'] > trading_data['ewma100']] bearish_cross = trading_data[trading_data['mid_p'] < trading_data['ewma100']] end_of_day_price = trading_data['mid_p'].iloc[-1] end_of_day_time_stamp = trading_data['time_stamp'].iloc[-1] valid_bearish_breakoutQ = False valid_bullish_breakoutQ = False bearish_breakout_price_entry = np.NaN bullish_breakout_price_entry = np.NaN if not bearish_breakout.empty: if bearish_breakout.index[0]+1 < trading_data.index[-1]: if trading_data['mid_p'].loc[bearish_breakout.index[0]+1]>range_min-0.5*initial_range: valid_bearish_breakoutQ = True bearish_breakout_price_entry = trading_data['mid_p'].loc[bearish_breakout.index[0]+1] bearish_breakout_time_stamp = trading_data['time_stamp'].loc[bearish_breakout.index[0]+1] if not bullish_breakout.empty: if bullish_breakout.index[0]+1<trading_data.index[-1]: if trading_data['mid_p'].loc[bullish_breakout.index[0]+1]<range_max+0.5*initial_range: valid_bullish_breakoutQ = True bullish_breakout_price_entry = trading_data['mid_p'].loc[bullish_breakout.index[0]+1] bullish_breakout_time_stamp = trading_data['time_stamp'].loc[bullish_breakout.index[0]+1] stop_loss = (range_max-range_min)*contract_multiplier pnl_list = [] direction_list = [] entry_time_list = [] exit_time_list = [] entry_price_list = [] exit_price_list = [] daily_trade_no_list = [] exit_type_list = [] ticker_list = [] if valid_bearish_breakoutQ: direction_list.append(-1) entry_time_list.append(bearish_breakout_time_stamp) entry_price_list.append(bearish_breakout_price_entry) daily_pnl = bearish_breakout_price_entry-end_of_day_price exit_price = end_of_day_price exit_type = 'eod' exit_time = end_of_day_time_stamp daily_trade_no = 1 #bullish_cross_stop_frame = bullish_cross[(bullish_cross['time_stamp'] > bearish_breakout_time_stamp)] #if (not bullish_cross_stop_frame.empty) and (bullish_cross_stop_frame.index[0]+1<trading_data.index[-1]): # daily_pnl = bearish_breakout_price_entry-trading_data['mid_p'].loc[bullish_cross_stop_frame.index[0]+1] # exit_price = trading_data['mid_p'].loc[bullish_cross_stop_frame.index[0]+1] # exit_type = 'oso' # exit_time = trading_data['time_stamp'].loc[bullish_cross_stop_frame.index[0]+1] #if valid_bullish_breakoutQ: # if bullish_breakout_time_stamp>bearish_breakout_time_stamp: # if bullish_breakout_time_stamp<exit_time: # daily_pnl = bearish_breakout_price_entry-bullish_breakout_price_entry # exit_price = bullish_breakout_price_entry # exit_type = 'fso' # exit_time = bullish_breakout_time_stamp # else: # daily_trade_no = 2 exit_time_list.append(exit_time) exit_type_list.append(exit_type) daily_trade_no_list.append(daily_trade_no) pnl_list.append(daily_pnl) exit_price_list.append(exit_price) ticker_list.append(ticker) if valid_bullish_breakoutQ: direction_list.append(1) entry_time_list.append(bullish_breakout_time_stamp) entry_price_list.append(bullish_breakout_price_entry) daily_pnl = end_of_day_price-bullish_breakout_price_entry exit_price = end_of_day_price exit_type = 'eod' exit_time = end_of_day_time_stamp daily_trade_no = 1 bearish_cross_stop_frame = bearish_cross[(bearish_cross['time_stamp'] > bullish_breakout_time_stamp)] #if (not bearish_cross_stop_frame.empty) and (bearish_cross_stop_frame.index[0]+1 < trading_data.index[-1]): # daily_pnl = trading_data['mid_p'].loc[bearish_cross_stop_frame.index[0]+1]-bullish_breakout_price_entry # exit_price = trading_data['mid_p'].loc[bearish_cross_stop_frame.index[0]+1] # exit_type = 'oso' # exit_time = trading_data['time_stamp'].loc[bearish_cross_stop_frame.index[0]+1] #if valid_bearish_breakoutQ: # if bearish_breakout_time_stamp>bullish_breakout_time_stamp: # if bearish_breakout_time_stamp<exit_time: # daily_pnl = bearish_breakout_price_entry-bullish_breakout_price_entry # exit_price = bearish_breakout_price_entry # exit_type = 'fso' # exit_time = bearish_breakout_time_stamp # else: # daily_trade_no = 2 exit_time_list.append(exit_time) exit_type_list.append(exit_type) daily_trade_no_list.append(daily_trade_no) pnl_list.append(daily_pnl) exit_price_list.append(exit_price) ticker_list.append(ticker) pnl_frame = pd.DataFrame.from_items([('ticker', ticker_list), ('ticker_head',ticker_head), ('direction', direction_list), ('entry_price', entry_price_list), ('exit_price', exit_price_list), ('pnl', pnl_list), ('entry_time', entry_time_list), ('exit_time', exit_time_list), ('exit_type', exit_type_list), ('daily_trade_no', daily_trade_no_list)]) pnl_frame['pnl'] = pnl_frame['pnl']*contract_multiplier return {'intraday_data': selected_data, 'range_min': range_min, 'range_max':range_max,'pnl_frame':pnl_frame,'stop_loss':stop_loss}
def update_options_price_database_from_cme_files_4ticker(**kwargs): ticker = kwargs['ticker'] contract_specs_output = cmi.get_contract_specs(ticker) ticker_head = contract_specs_output['ticker_head'] ticker_month_num = contract_specs_output['ticker_month_num'] ticker_year = contract_specs_output['ticker_year'] if 'settle_date' in kwargs.keys(): settle_date = kwargs['settle_date'] kwargs['report_date'] = settle_date else: settle_date = int(time.strftime('%Y%m%d')) kwargs['settle_date'] = settle_date kwargs['report_date'] = settle_date if not exp.is_business_day(double_date=settle_date, reference_tickerhead=ticker_head): return if 'expiration_date' in kwargs.keys(): expiration_date = kwargs['expiration_date'] else: expiration_date = exp.get_options_expiration(ticker) expiration_date = expiration_date.date() settle_datetime = cu.convert_doubledate_2datetime(settle_date) if 'cal_dte' in kwargs.keys(): cal_dte = kwargs['cal_dte'] else: cal_dte = (expiration_date - settle_datetime.date()).days if 'tr_dte' in kwargs.keys(): tr_dte = kwargs['tr_dte'] else: bday_us = CustomBusinessDay( calendar=exp.get_calendar_4ticker_head(ticker_head)) dts = pd.date_range(start=settle_datetime, end=expiration_date, freq=bday_us) tr_dte = len( [x for x in dts if x.to_pydatetime().date() < expiration_date]) data_vendor_id = 2 now = dt.datetime.now() con = msu.get_my_sql_connection(**kwargs) process_output = pco.process_cme_options_4ticker(**kwargs) if process_output['success']: settle_frame = process_output['settle_frame'] else: if 'con' not in kwargs.keys(): con.close() return column_names = settle_frame.columns.tolist() option_type_indx = column_names.index('option_type') strike_indx = column_names.index('strike') settle_indx = column_names.index('settle') volume_indx = column_names.index('volume') interest_indx = column_names.index('interest') tuples = [ tuple([ data_vendor_id, ticker_head, ticker_month_num, ticker_year, ticker, x[option_type_indx], x[strike_indx], settle_datetime.date(), cal_dte, tr_dte, now, now, None if np.isnan(x[settle_indx]) else x[settle_indx], None if np.isnan(x[volume_indx]) else x[volume_indx], None if np.isnan(x[interest_indx]) else x[interest_indx] ]) for x in settle_frame.values ] column_str = "data_vendor_id, ticker_head, ticker_month, ticker_year, ticker, " \ " option_type, strike, price_date, cal_dte, tr_dte, " \ " created_date,last_updated_date, close_price, volume, open_interest" insert_str = ("%s, " * len(column_str.split(',')))[:-2] final_str = "REPLACE INTO daily_option_price (%s) VALUES (%s)" % ( column_str, insert_str) msu.sql_execute_many_wrapper(final_str=final_str, tuples=tuples, con=con) if 'con' not in kwargs.keys(): con.close()
def get_scv_signals(**kwargs): ticker = kwargs['ticker'] date_to = kwargs['date_to'] con = msu.get_my_sql_connection(**kwargs) if 'futures_data_dictionary' in kwargs.keys(): futures_data_dictionary = kwargs['futures_data_dictionary'] else: futures_data_dictionary = { x: gfp.get_futures_price_preloaded(ticker_head=x) for x in [cmi.get_contract_specs(ticker)['ticker_head']] } aligned_indicators_output = ops.get_aligned_option_indicators( ticker_list=[ticker], settle_date=date_to, con=con) if not aligned_indicators_output['success']: return { 'downside': np.NaN, 'upside': np.NaN, 'theta': np.NaN, 'realized_vol_forecast': np.NaN, 'real_vol20_current': np.NaN, 'imp_vol': np.NaN, 'imp_vol_premium': np.NaN, 'q': np.NaN } hist = aligned_indicators_output['hist'] current = aligned_indicators_output['current'] vcs_output = vcs.generate_vcs_sheet_4date(date_to=date_to, con=con) if 'con' not in kwargs.keys(): con.close() clean_indx = hist['c1']['profit5'].notnull() clean_data = hist[clean_indx] if clean_data.empty: downside = np.NaN upside = np.NaN else: last_available_align_date = clean_data.index[-1] clean_data = clean_data[clean_data.index >= last_available_align_date - dt.timedelta(5 * 365)] profit5 = clean_data['c1']['profit5'] percentile_vector = stats.get_number_from_quantile( y=profit5.values, quantile_list=[1, 15, 85, 99], clean_num_obs=max(100, round(3 * len(profit5.values) / 4))) downside = (percentile_vector[0] + percentile_vector[1]) / 2 upside = (percentile_vector[2] + percentile_vector[3]) / 2 realized_vol_output = rvue.forecast_realized_vol_until_expiration( ticker=ticker, futures_data_dictionary=futures_data_dictionary, date_to=date_to) realized_vol_forecast = realized_vol_output['realized_vol_forecast'] real_vol20_current = realized_vol_output['real_vol20_current'] imp_vol = current['imp_vol'][0] imp_vol_premium = 100 * (imp_vol - realized_vol_forecast) / imp_vol q = np.NaN if vcs_output['success']: vcs_pairs = vcs_output['vcs_pairs'] selected_pairs = vcs_pairs[vcs_pairs['ticker2'] == ticker] if not selected_pairs.empty: q = 100 - selected_pairs['Q'].mean() return { 'downside': downside, 'upside': upside, 'theta': current['theta'][0], 'realized_vol_forecast': realized_vol_forecast, 'real_vol20_current': real_vol20_current, 'imp_vol': imp_vol, 'imp_vol_premium': imp_vol_premium, 'q': q }
def update_options_price_database_from_cme_files_4ticker(**kwargs): ticker = kwargs['ticker'] contract_specs_output = cmi.get_contract_specs(ticker) ticker_head = contract_specs_output['ticker_head'] ticker_month_num = contract_specs_output['ticker_month_num'] ticker_year = contract_specs_output['ticker_year'] if 'settle_date' in kwargs.keys(): settle_date = kwargs['settle_date'] kwargs['report_date'] = settle_date else: settle_date = int(time.strftime('%Y%m%d')) kwargs['settle_date'] = settle_date kwargs['report_date'] = settle_date if not exp.is_business_day(double_date=settle_date, reference_tickerhead=ticker_head): return if 'expiration_date' in kwargs.keys(): expiration_date = kwargs['expiration_date'] else: expiration_date = exp.get_options_expiration(ticker) expiration_date = expiration_date.date() settle_datetime = cu.convert_doubledate_2datetime(settle_date) if 'cal_dte' in kwargs.keys(): cal_dte = kwargs['cal_dte'] else: cal_dte = (expiration_date-settle_datetime.date()).days if 'tr_dte' in kwargs.keys(): tr_dte = kwargs['tr_dte'] else: bday_us = CustomBusinessDay(calendar=exp.get_calendar_4ticker_head(ticker_head)) dts = pd.date_range(start=settle_datetime, end=expiration_date, freq=bday_us) tr_dte = len([x for x in dts if x.to_datetime().date() < expiration_date]) data_vendor_id = 2 now = dt.datetime.now() con = msu.get_my_sql_connection(**kwargs) process_output = pco.process_cme_options_4ticker(**kwargs) if process_output['success']: settle_frame = process_output['settle_frame'] else: if 'con' not in kwargs.keys(): con.close() return column_names = settle_frame.columns.tolist() option_type_indx = column_names.index('option_type') strike_indx = column_names.index('strike') settle_indx = column_names.index('settle') volume_indx = column_names.index('volume') interest_indx = column_names.index('interest') tuples = [tuple([data_vendor_id, ticker_head, ticker_month_num, ticker_year, ticker, x[option_type_indx],x[strike_indx],settle_datetime.date(), cal_dte, tr_dte, now, now, None if np.isnan(x[settle_indx]) else x[settle_indx], None if np.isnan(x[volume_indx]) else x[volume_indx], None if np.isnan(x[interest_indx]) else x[interest_indx]]) for x in settle_frame.values] column_str = "data_vendor_id, ticker_head, ticker_month, ticker_year, ticker, " \ " option_type, strike, price_date, cal_dte, tr_dte, " \ " created_date,last_updated_date, close_price, volume, open_interest" insert_str = ("%s, " * len(column_str.split(',')))[:-2] final_str = "REPLACE INTO daily_option_price (%s) VALUES (%s)" % (column_str, insert_str) msu.sql_execute_many_wrapper(final_str=final_str, tuples=tuples, con=con) if 'con' not in kwargs.keys(): con.close()
def get_strategy_pnl_4day(**kwargs): alias = kwargs['alias'] pnl_date = kwargs['pnl_date'] if 'shift_in_days' in kwargs.keys(): shift_in_days = kwargs['shift_in_days'] else: shift_in_days = 1 if 'broker' in kwargs.keys(): broker = kwargs['broker'] else: broker = 'abn' #print(pnl_date) pnl_datetime = cu.convert_doubledate_2datetime(pnl_date) con = msu.get_my_sql_connection(**kwargs) if 'trades_frame' in kwargs.keys(): trades_frame = kwargs['trades_frame'] ticker_head_list = [ cmi.get_contract_specs(x)['ticker_head'] for x in trades_frame['ticker'] ] else: trades_frame = ts.get_trades_4strategy_alias(alias=alias, con=con) ticker_head_list = [ cmi.get_contract_specs(x)['ticker_head'] for x in trades_frame['ticker'] ] trades_frame['contract_multiplier'] = [ cmi.contract_multiplier[x] for x in ticker_head_list ] trades_frame['t_cost'] = [ cmi.get_t_cost(ticker_head=x, broker=broker) for x in ticker_head_list ] trades_frame['ticker_head'] = ticker_head_list option_indx = trades_frame['instrument'] == 'O' trades_frame['generalized_ticker'] = trades_frame['ticker'] trades_frame['generalized_ticker'][option_indx] = trades_frame['ticker'][option_indx] + '-' + \ trades_frame['option_type'][option_indx] + '-' + \ trades_frame['strike_price'][option_indx].astype(str) position_frame_aux = trades_frame[ trades_frame['trade_date'] < pnl_datetime] intraday_frame_aux = trades_frame[trades_frame['trade_date'] == pnl_datetime] grouped = position_frame_aux.groupby(['generalized_ticker']) net_position = pd.DataFrame() net_position['qty'] = grouped['trade_quantity'].sum() net_position['generalized_ticker'] = grouped['generalized_ticker'].first() net_position = net_position[abs(net_position['qty']) > 0.1] useful_generalized_ticker_list = list( set(net_position['generalized_ticker'].values) | set(intraday_frame_aux['generalized_ticker'].unique())) trades_frame = trades_frame[trades_frame['generalized_ticker'].isin( useful_generalized_ticker_list)] if 'futures_data_dictionary' in kwargs.keys(): futures_data_dictionary = kwargs['futures_data_dictionary'] else: unique_ticker_head_list = list(set(ticker_head_list)) futures_data_dictionary = { x: gfp.get_futures_price_preloaded(ticker_head=x) for x in unique_ticker_head_list } pnl_date_1 = exp.doubledate_shift_bus_days(double_date=pnl_date, shift_in_days=shift_in_days) underlying_frame = trades_frame[trades_frame['instrument'] == 'F'] option_frame = trades_frame[trades_frame['instrument'] == 'O'] futures_price_out_1 = [ gfp.get_futures_price_preloaded( ticker=x, futures_data_dictionary=futures_data_dictionary, settle_date=pnl_date_1) for x in underlying_frame['ticker'] ] futures_price_out = [ gfp.get_futures_price_preloaded( ticker=x, futures_data_dictionary=futures_data_dictionary, settle_date=pnl_date) for x in underlying_frame['ticker'] ] underlying_frame['price_1'] = [ np.NaN if x.empty else x['close_price'].values[0] for x in futures_price_out_1 ] underlying_frame['price'] = [ np.NaN if x.empty else x['close_price'].values[0] for x in futures_price_out ] #underlying_frame['price_1'] = [gfp.get_futures_price_preloaded(ticker=x, # futures_data_dictionary=futures_data_dictionary, # settle_date=pnl_date_1)['close_price'].values[0] for x in underlying_frame['ticker']] #underlying_frame['price'] = [gfp.get_futures_price_preloaded(ticker=x, # futures_data_dictionary=futures_data_dictionary, # settle_date=pnl_date)['close_price'].values[0] for x in underlying_frame['ticker']] option_frame['price_1'] = [ gop.get_options_price_from_db( ticker=option_frame['ticker'].iloc[x], strike=option_frame['strike_price'].iloc[x], option_type=option_frame['option_type'].iloc[x], con=con, settle_date=pnl_date_1, return_nan_if_emptyQ=True)['close_price'].values[0] for x in range(len(option_frame.index)) ] option_frame['price'] = [ gop.get_options_price_from_db( ticker=option_frame['ticker'].iloc[x], strike=option_frame['strike_price'].iloc[x], option_type=option_frame['option_type'].iloc[x], con=con, settle_date=pnl_date, return_nan_if_emptyQ=True)['close_price'].values[0] for x in range(len(option_frame.index)) ] trades_frame = pd.concat([option_frame, underlying_frame]) position_frame = trades_frame[trades_frame['trade_date'] < pnl_datetime] nan_price_q = position_frame['price'].isnull().values.any() intraday_frame = trades_frame[trades_frame['trade_date'] == pnl_datetime] position_pnl_per_ticker = pd.DataFrame(columns=['ticker', 'pnl_position']) intraday_pnl_per_ticker = pd.DataFrame(columns=['ticker', 'pnl_intraday']) position_pnl_per_tickerhead = pd.DataFrame( columns=['ticker_head', 'pnl_position']) intraday_pnl_per_tickerhead = pd.DataFrame( columns=['ticker_head', 'pnl_intraday']) if len(position_frame) == 0: position_pnl = 0 else: position_frame['pnl'] = position_frame['contract_multiplier']*\ position_frame['trade_quantity']*\ (position_frame['price']-position_frame['price_1']) position_pnl = position_frame['pnl'].sum() position_grouped_per_ticker = position_frame.groupby('ticker') position_grouped_per_tickerhead = position_frame.groupby('ticker_head') position_pnl_per_ticker['pnl_position'] = ( position_grouped_per_ticker['pnl'].sum()).values position_pnl_per_ticker['ticker'] = ( position_grouped_per_ticker['ticker'].first()).values position_pnl_per_tickerhead['pnl_position'] = ( position_grouped_per_tickerhead['pnl'].sum()).values position_pnl_per_tickerhead['ticker_head'] = ( position_grouped_per_tickerhead['ticker_head'].first()).values if len(intraday_frame) == 0: intraday_pnl = 0 t_cost = 0 else: intraday_frame['pnl'] = intraday_frame['contract_multiplier']*\ intraday_frame['trade_quantity']*\ (intraday_frame['price']-intraday_frame['trade_price']) intraday_frame['pnl_wtcost'] = intraday_frame['pnl'] - abs( intraday_frame['trade_quantity'] * intraday_frame['t_cost']) intraday_pnl = intraday_frame['pnl'].sum() t_cost = (abs(intraday_frame['trade_quantity'] * intraday_frame['t_cost'])).sum() intraday_grouped_per_ticker = intraday_frame.groupby('ticker') intraday_grouped_per_tickerhead = intraday_frame.groupby('ticker_head') intraday_pnl_per_ticker['pnl_intraday'] = ( intraday_grouped_per_ticker['pnl_wtcost'].sum()).values intraday_pnl_per_ticker['ticker'] = ( intraday_grouped_per_ticker['ticker'].first()).values intraday_pnl_per_tickerhead['pnl_intraday'] = ( intraday_grouped_per_tickerhead['pnl_wtcost'].sum()).values intraday_pnl_per_tickerhead['ticker_head'] = ( intraday_grouped_per_tickerhead['ticker_head'].first()).values pnl_per_ticker = pd.merge(position_pnl_per_ticker, intraday_pnl_per_ticker, how='outer', on='ticker') intraday_zero_indx = [ x not in intraday_pnl_per_ticker['ticker'].values for x in pnl_per_ticker['ticker'] ] position_zero_indx = [ x not in position_pnl_per_ticker['ticker'].values for x in pnl_per_ticker['ticker'] ] pnl_per_ticker['pnl_position'][position_zero_indx] = 0 pnl_per_ticker['pnl_intraday'][intraday_zero_indx] = 0 pnl_per_ticker['pnl_total'] = pnl_per_ticker[ 'pnl_position'] + pnl_per_ticker['pnl_intraday'] pnl_per_ticker.set_index('ticker', drop=True, inplace=True) pnl_per_tickerhead = pd.merge(position_pnl_per_tickerhead, intraday_pnl_per_tickerhead, how='outer', on='ticker_head') intraday_zero_indx = [ x not in intraday_pnl_per_tickerhead['ticker_head'].values for x in pnl_per_tickerhead['ticker_head'] ] position_zero_indx = [ x not in position_pnl_per_tickerhead['ticker_head'].values for x in pnl_per_tickerhead['ticker_head'] ] pnl_per_tickerhead['pnl_position'][position_zero_indx] = 0 pnl_per_tickerhead['pnl_intraday'][intraday_zero_indx] = 0 pnl_per_tickerhead['pnl_total'] = pnl_per_tickerhead[ 'pnl_position'] + pnl_per_tickerhead['pnl_intraday'] pnl_per_tickerhead.set_index('ticker_head', drop=True, inplace=True) if 'con' not in kwargs.keys(): con.close() return { 'total_pnl': int(position_pnl + intraday_pnl - t_cost), 'position_pnl': int(position_pnl), 'intraday_pnl': int(intraday_pnl), 't_cost': int(t_cost), 'nan_price_q': nan_price_q, 'pnl_per_ticker': pnl_per_ticker, 'pnl_per_tickerhead': pnl_per_tickerhead }
def get_strategy_pnl_4day(**kwargs): alias = kwargs['alias'] pnl_date = kwargs['pnl_date'] #print(pnl_date) pnl_datetime = cu.convert_doubledate_2datetime(pnl_date) con = msu.get_my_sql_connection(**kwargs) if 'trades_frame' in kwargs.keys(): trades_frame = kwargs['trades_frame'] ticker_head_list = [cmi.get_contract_specs(x)['ticker_head'] for x in trades_frame['ticker']] else: trades_frame = ts.get_trades_4strategy_alias(alias=alias,con=con) ticker_head_list = [cmi.get_contract_specs(x)['ticker_head'] for x in trades_frame['ticker']] trades_frame['contract_multiplier'] = [cmi.contract_multiplier[x] for x in ticker_head_list] trades_frame['t_cost'] = [cmi.t_cost[x] for x in ticker_head_list] trades_frame['ticker_head'] = ticker_head_list if 'futures_data_dictionary' in kwargs.keys(): futures_data_dictionary = kwargs['futures_data_dictionary'] else: unique_ticker_head_list = list(set(ticker_head_list)) futures_data_dictionary = {x: gfp.get_futures_price_preloaded(ticker_head=x) for x in unique_ticker_head_list} pnl_date_1 = exp.doubledate_shift_bus_days(double_date=pnl_date) underlying_frame = trades_frame[trades_frame['instrument'] == 'F'] option_frame = trades_frame[trades_frame['instrument'] == 'O'] futures_price_out_1 = [gfp.get_futures_price_preloaded(ticker=x, futures_data_dictionary=futures_data_dictionary, settle_date=pnl_date_1) for x in underlying_frame['ticker']] futures_price_out = [gfp.get_futures_price_preloaded(ticker=x, futures_data_dictionary=futures_data_dictionary, settle_date=pnl_date) for x in underlying_frame['ticker']] underlying_frame['price_1'] = [np.NaN if x.empty else x['close_price'].values[0] for x in futures_price_out_1] underlying_frame['price'] = [np.NaN if x.empty else x['close_price'].values[0] for x in futures_price_out] #underlying_frame['price_1'] = [gfp.get_futures_price_preloaded(ticker=x, # futures_data_dictionary=futures_data_dictionary, # settle_date=pnl_date_1)['close_price'].values[0] for x in underlying_frame['ticker']] #underlying_frame['price'] = [gfp.get_futures_price_preloaded(ticker=x, # futures_data_dictionary=futures_data_dictionary, # settle_date=pnl_date)['close_price'].values[0] for x in underlying_frame['ticker']] option_frame['price_1'] = [gop.get_options_price_from_db(ticker=option_frame['ticker'].iloc[x], strike=option_frame['strike_price'].iloc[x], option_type=option_frame['option_type'].iloc[x], con=con,settle_date=pnl_date_1, return_nan_if_emptyQ = True)['close_price'].values[0] for x in range(len(option_frame.index))] option_frame['price'] = [gop.get_options_price_from_db(ticker=option_frame['ticker'].iloc[x], strike=option_frame['strike_price'].iloc[x], option_type=option_frame['option_type'].iloc[x], con=con,settle_date=pnl_date, return_nan_if_emptyQ = True)['close_price'].values[0] for x in range(len(option_frame.index))] trades_frame = pd.concat([option_frame, underlying_frame]) position_frame = trades_frame[trades_frame['trade_date'] < pnl_datetime] intraday_frame = trades_frame[trades_frame['trade_date'] == pnl_datetime] position_pnl_per_ticker = pd.DataFrame(columns=['ticker','pnl_position']) intraday_pnl_per_ticker = pd.DataFrame(columns=['ticker','pnl_intraday']) position_pnl_per_tickerhead = pd.DataFrame(columns=['ticker_head','pnl_position']) intraday_pnl_per_tickerhead = pd.DataFrame(columns=['ticker_head','pnl_intraday']) if len(position_frame) == 0: position_pnl = 0 else: position_frame['pnl'] = position_frame['contract_multiplier']*\ position_frame['trade_quantity']*\ (position_frame['price']-position_frame['price_1']) position_pnl = position_frame['pnl'].sum() position_grouped_per_ticker = position_frame.groupby('ticker') position_grouped_per_tickerhead = position_frame.groupby('ticker_head') position_pnl_per_ticker['pnl_position'] = (position_grouped_per_ticker['pnl'].sum()).values position_pnl_per_ticker['ticker'] = (position_grouped_per_ticker['ticker'].first()).values position_pnl_per_tickerhead['pnl_position'] = (position_grouped_per_tickerhead['pnl'].sum()).values position_pnl_per_tickerhead['ticker_head'] = (position_grouped_per_tickerhead['ticker_head'].first()).values if len(intraday_frame) == 0: intraday_pnl = 0 t_cost = 0 else: intraday_frame['pnl'] = intraday_frame['contract_multiplier']*\ intraday_frame['trade_quantity']*\ (intraday_frame['price']-intraday_frame['trade_price']) intraday_frame['pnl_wtcost'] = intraday_frame['pnl']-abs(intraday_frame['trade_quantity']*intraday_frame['t_cost']) intraday_pnl = intraday_frame['pnl'].sum() t_cost = (abs(intraday_frame['trade_quantity']*intraday_frame['t_cost'])).sum() intraday_grouped_per_ticker = intraday_frame.groupby('ticker') intraday_grouped_per_tickerhead = intraday_frame.groupby('ticker_head') intraday_pnl_per_ticker['pnl_intraday'] = (intraday_grouped_per_ticker['pnl_wtcost'].sum()).values intraday_pnl_per_ticker['ticker'] = (intraday_grouped_per_ticker['ticker'].first()).values intraday_pnl_per_tickerhead['pnl_intraday'] = (intraday_grouped_per_tickerhead['pnl_wtcost'].sum()).values intraday_pnl_per_tickerhead['ticker_head'] = (intraday_grouped_per_tickerhead['ticker_head'].first()).values pnl_per_ticker = pd.merge(position_pnl_per_ticker,intraday_pnl_per_ticker,how='outer',on='ticker') intraday_zero_indx = [x not in intraday_pnl_per_ticker['ticker'].values for x in pnl_per_ticker['ticker']] position_zero_indx = [x not in position_pnl_per_ticker['ticker'].values for x in pnl_per_ticker['ticker']] pnl_per_ticker['pnl_position'][position_zero_indx] = 0 pnl_per_ticker['pnl_intraday'][intraday_zero_indx] = 0 pnl_per_ticker['pnl_total'] = pnl_per_ticker['pnl_position']+pnl_per_ticker['pnl_intraday'] pnl_per_ticker.set_index('ticker', drop=True, inplace=True) pnl_per_tickerhead = pd.merge(position_pnl_per_tickerhead,intraday_pnl_per_tickerhead,how='outer',on='ticker_head') intraday_zero_indx = [x not in intraday_pnl_per_tickerhead['ticker_head'].values for x in pnl_per_tickerhead['ticker_head']] position_zero_indx = [x not in position_pnl_per_tickerhead['ticker_head'].values for x in pnl_per_tickerhead['ticker_head']] pnl_per_tickerhead['pnl_position'][position_zero_indx] = 0 pnl_per_tickerhead['pnl_intraday'][intraday_zero_indx] = 0 pnl_per_tickerhead['pnl_total'] = pnl_per_tickerhead['pnl_position']+pnl_per_tickerhead['pnl_intraday'] pnl_per_tickerhead.set_index('ticker_head', drop=True, inplace=True) if 'con' not in kwargs.keys(): con.close() return {'total_pnl': int(position_pnl+intraday_pnl - t_cost), 'position_pnl': int(position_pnl), 'intraday_pnl': int(intraday_pnl), 't_cost': int(t_cost), 'pnl_per_ticker': pnl_per_ticker, 'pnl_per_tickerhead':pnl_per_tickerhead}
def get_scv_signals(**kwargs): ticker = kwargs['ticker'] date_to = kwargs['date_to'] con = msu.get_my_sql_connection(**kwargs) if 'futures_data_dictionary' in kwargs.keys(): futures_data_dictionary = kwargs['futures_data_dictionary'] else: futures_data_dictionary = {x: gfp.get_futures_price_preloaded(ticker_head=x) for x in [cmi.get_contract_specs(ticker)['ticker_head']]} aligned_indicators_output = ops.get_aligned_option_indicators(ticker_list=[ticker], settle_date=date_to, con=con) if not aligned_indicators_output['success']: return {'downside': np.NaN, 'upside': np.NaN, 'theta': np.NaN, 'realized_vol_forecast': np.NaN, 'real_vol20_current': np.NaN, 'imp_vol': np.NaN, 'imp_vol_premium': np.NaN, 'q': np.NaN} hist = aligned_indicators_output['hist'] current = aligned_indicators_output['current'] vcs_output = vcs.generate_vcs_sheet_4date(date_to=date_to,con=con) if 'con' not in kwargs.keys(): con.close() clean_indx = hist['c1']['profit5'].notnull() clean_data = hist[clean_indx] if clean_data.empty: downside = np.NaN upside = np.NaN else: last_available_align_date = clean_data.index[-1] clean_data = clean_data[clean_data.index >= last_available_align_date-dt.timedelta(5*365)] profit5 = clean_data['c1']['profit5'] percentile_vector = stats.get_number_from_quantile(y=profit5.values, quantile_list=[1, 15, 85, 99], clean_num_obs=max(100, round(3*len(profit5.values)/4))) downside = (percentile_vector[0]+percentile_vector[1])/2 upside = (percentile_vector[2]+percentile_vector[3])/2 realized_vol_output = rvue.forecast_realized_vol_until_expiration(ticker=ticker, futures_data_dictionary=futures_data_dictionary, date_to=date_to) realized_vol_forecast = realized_vol_output['realized_vol_forecast'] real_vol20_current = realized_vol_output['real_vol20_current'] imp_vol = current['imp_vol'][0] imp_vol_premium = 100*(imp_vol-realized_vol_forecast)/imp_vol q = np.NaN if vcs_output['success']: vcs_pairs = vcs_output['vcs_pairs'] selected_pairs = vcs_pairs[vcs_pairs['ticker2'] == ticker] if not selected_pairs.empty: q = 100-selected_pairs['Q'].mean() return {'downside': downside, 'upside': upside, 'theta': current['theta'][0], 'realized_vol_forecast': realized_vol_forecast, 'real_vol20_current': real_vol20_current, 'imp_vol': imp_vol, 'imp_vol_premium': imp_vol_premium, 'q': q}
def get_hedge_4strategy(**kwargs): con = msu.get_my_sql_connection(**kwargs) current_date = cu.get_doubledate() settle_price_date = exp.doubledate_shift_bus_days(double_date=current_date, shift_in_days=1) position_frame = tas.get_net_position_4strategy_alias(alias=kwargs['alias'],as_of_date=current_date,con=con) intraday_price_frame = gip.get_cme_direct_prices() intraday_price_frame.rename(columns={'ticker': 'underlying_ticker'},inplace=True) intraday_price_frame['ticker_head'] = [cmi.get_contract_specs(x)['ticker_head'] for x in intraday_price_frame['underlying_ticker']] intraday_price_frame['mid_price'] = (intraday_price_frame['bid_price'] + intraday_price_frame['ask_price'])/2 intraday_price_frame['mid_price'] = [tfl.convert_trade_price_from_cme_direct(ticker_head=intraday_price_frame['ticker_head'].iloc[x], price=intraday_price_frame['mid_price'].iloc[x]) for x in range(len(intraday_price_frame.index))] options_frame = position_frame[position_frame['instrument'] == 'O'] futures_frame = position_frame[position_frame['instrument'] == 'F'] if options_frame.empty: futures_frame.rename(columns={'ticker': 'underlying_ticker', 'qty': 'underlying_delta'},inplace=True) futures_frame = futures_frame[['underlying_ticker', 'underlying_delta']] net_position = pd.merge(futures_frame, intraday_price_frame, how='left', on='underlying_ticker') net_position['hedge_price'] = net_position['mid_price'] net_position['hedge'] = -net_position['underlying_delta'] return net_position imp_vol_list = [gop.get_options_price_from_db(ticker=options_frame['ticker'].iloc[x], settle_date=settle_price_date, strike=options_frame['strike_price'].iloc[x], column_names=['imp_vol'], con=con)['imp_vol'] for x in range(len(options_frame.index))] options_frame['imp_vol'] = [imp_vol_list[x][1] if (np.isnan(imp_vol_list[x][0]) and len(imp_vol_list[x]) > 1) else imp_vol_list[x][0] for x in range(len(options_frame.index))] options_frame['underlying_ticker'] = [omu.get_option_underlying(ticker=x) for x in options_frame['ticker']] options_frame = pd.merge(options_frame, intraday_price_frame, how='left', on='underlying_ticker') options_frame['ticker_head'] = [cmi.get_contract_specs(x)['ticker_head'] for x in options_frame['ticker']] options_frame['exercise_type'] = [cmi.get_option_exercise_type(ticker_head=x) for x in options_frame['ticker_head']] options_frame['strike_price'] = options_frame['strike_price'].astype('float64') options_frame['delta'] = [omu.option_model_wrapper(ticker=options_frame['ticker'].iloc[x], calculation_date=current_date, interest_rate_date=settle_price_date, underlying=options_frame['mid_price'].iloc[x], strike=options_frame['strike_price'].iloc[x], implied_vol=options_frame['imp_vol'].iloc[x], option_type=options_frame['option_type'].iloc[x], exercise_type=options_frame['exercise_type'].iloc[x], con=con)['delta'] for x in range(len(options_frame.index))] options_frame['total_delta'] = options_frame['qty']*options_frame['delta'] grouped = options_frame.groupby('underlying_ticker') net_position = pd.DataFrame() net_position['underlying_ticker'] = (grouped['underlying_ticker'].first()).values net_position['hedge_price'] = (grouped['mid_price'].first()).values net_position['option_delta'] = (grouped['total_delta'].sum()).values net_position['option_delta'] = net_position['option_delta'].round(2) if futures_frame.empty: net_position['total_delta'] = net_position['option_delta'] else: futures_frame.rename(columns={'ticker': 'underlying_ticker', 'qty': 'underlying_delta'},inplace=True) futures_frame = futures_frame[['underlying_ticker', 'underlying_delta']] isinOptions = futures_frame['underlying_ticker'].isin(net_position['underlying_ticker']) futures_frame_w_options = futures_frame[isinOptions] futures_frame_wo_options = futures_frame[~isinOptions] if futures_frame_w_options.empty: net_position['underlying_delta'] = 0 net_position['total_delta'] = net_position['option_delta'] else: net_position = pd.merge(net_position, futures_frame_w_options, how='outer', on='underlying_ticker') net_position['total_delta'] = net_position['option_delta']+net_position['underlying_delta'] if not futures_frame_wo_options.empty: net_position_futures = pd.merge(futures_frame_wo_options, intraday_price_frame, how='left', on='underlying_ticker') net_position_futures['hedge_price'] = net_position_futures['mid_price'] net_position_futures['option_delta'] = 0 net_position_futures['total_delta'] = net_position_futures['underlying_delta'] net_position = pd.concat([net_position,net_position_futures[['underlying_ticker','hedge_price','option_delta','underlying_delta','total_delta']]]) net_position['hedge'] = -net_position['total_delta'] if 'con' not in kwargs.keys(): con.close() return net_position
def get_quandl_database_4ticker(ticker): ticker_head = cmi.get_contract_specs(ticker)["ticker_head"] return quandl_database[ticker_head]
def get_strategy_pnl(**kwargs): alias = kwargs['alias'] con = msu.get_my_sql_connection(**kwargs) #print(alias) strategy_info = ts.get_strategy_info_from_alias(alias=alias, con=con) if 'as_of_date' in kwargs.keys(): as_of_date = kwargs['as_of_date'] else: as_of_date = exp.doubledate_shift_bus_days() open_date = int(strategy_info['open_date'].strftime('%Y%m%d')) close_date = int(strategy_info['close_date'].strftime('%Y%m%d')) if close_date>as_of_date: close_date = as_of_date bus_day_list = exp.get_bus_day_list(date_from=open_date,date_to=close_date) trades_frame = ts.get_trades_4strategy_alias(alias=alias,con=con) ticker_head_list = [cmi.get_contract_specs(x)['ticker_head'] for x in trades_frame['ticker']] unique_ticker_head_list = list(set(ticker_head_list)) if 'futures_data_dictionary' in kwargs.keys(): futures_data_dictionary = kwargs['futures_data_dictionary'] else: futures_data_dictionary = {x: gfp.get_futures_price_preloaded(ticker_head=x) for x in unique_ticker_head_list} trades_frame['contract_multiplier'] = [cmi.contract_multiplier[x] for x in ticker_head_list] trades_frame['t_cost'] = [cmi.t_cost[x] for x in ticker_head_list] pnl_path = [get_strategy_pnl_4day(alias=alias,pnl_date=x,con=con, trades_frame=trades_frame, futures_data_dictionary=futures_data_dictionary) for x in bus_day_list] pnl_per_tickerhead_list = [x['pnl_per_tickerhead'] for x in pnl_path] pnl_per_tickerhead = pd.concat(pnl_per_tickerhead_list, axis=1) pnl_per_tickerhead = pnl_per_tickerhead[['pnl_total']] pnl_per_tickerhead = pnl_per_tickerhead.transpose() if len(unique_ticker_head_list)>1: zero_indx = [[x not in y.index for y in pnl_per_tickerhead_list] for x in pnl_per_tickerhead.columns] for i in range(len(pnl_per_tickerhead.columns)): pnl_per_tickerhead.iloc[:, i][zero_indx[i]] = 0 pnl_per_tickerhead['settle_date'] = bus_day_list pnl_per_tickerhead.reset_index(inplace=True,drop=True) pnl_frame = pd.DataFrame(pnl_path) pnl_frame['settle_date'] = bus_day_list if 'con' not in kwargs.keys(): con.close() return {'pnl_frame': pnl_frame[['settle_date','position_pnl','intraday_pnl','t_cost','total_pnl']], 'pnl_per_tickerhead': pnl_per_tickerhead, 'daily_pnl': pnl_frame['total_pnl'].values[-1], 'total_pnl': pnl_frame['total_pnl'].sum()}
def get_results_4strategy(**kwargs): signal_input = dict() if 'futures_data_dictionary' in kwargs.keys(): signal_input['futures_data_dictionary'] = kwargs['futures_data_dictionary'] if 'date_to' in kwargs.keys(): date_to = kwargs['date_to'] else: date_to = exp.doubledate_shift_bus_days() if 'datetime5_years_ago' in kwargs.keys(): signal_input['datetime5_years_ago'] = kwargs['datetime5_years_ago'] if 'strategy_info_output' in kwargs.keys(): strategy_info_output = kwargs['strategy_info_output'] else: strategy_info_output = ts.get_strategy_info_from_alias(**kwargs) con = msu.get_my_sql_connection(**kwargs) strategy_info_dict = sc.convert_from_string_to_dictionary(string_input=strategy_info_output['description_string']) #print(kwargs['alias']) strategy_class = strategy_info_dict['strategy_class'] pnl_frame = tpm.get_daily_pnl_snapshot(as_of_date=date_to) pnl_frame = pnl_frame[pnl_frame['alias']==kwargs['alias']] strategy_position = ts.get_net_position_4strategy_alias(alias=kwargs['alias'],as_of_date=date_to) if strategy_class == 'futures_butterfly': ticker_head = cmi.get_contract_specs(strategy_info_dict['ticker1'])['ticker_head'] if not strategy_position.empty: total_contracts2trade = strategy_position['qty'].abs().sum() t_cost = cmi.t_cost[ticker_head] QF_initial = float(strategy_info_dict['QF']) z1_initial = float(strategy_info_dict['z1']) bf_signals_output = fs.get_futures_butterfly_signals(ticker_list=[strategy_info_dict['ticker1'], strategy_info_dict['ticker2'], strategy_info_dict['ticker3']], aggregation_method=int(strategy_info_dict['agg']), contracts_back=int(strategy_info_dict['cBack']), date_to=date_to,**signal_input) aligned_output = bf_signals_output['aligned_output'] current_data = aligned_output['current_data'] holding_tr_dte = int(strategy_info_dict['trDte1'])-current_data['c1']['tr_dte'] if strategy_position.empty: recommendation = 'CLOSE' elif (z1_initial>0)&(holding_tr_dte > 5) &\ (bf_signals_output['qf']<QF_initial-20)&\ (pnl_frame['total_pnl'].iloc[0] > 3*t_cost*total_contracts2trade): recommendation = 'STOP' elif (z1_initial<0)&(holding_tr_dte > 5) &\ (bf_signals_output['qf']>QF_initial+20)&\ (pnl_frame['total_pnl'].iloc[0] > 3*t_cost*total_contracts2trade): recommendation = 'STOP' elif (current_data['c1']['tr_dte'] < 35)&\ (pnl_frame['total_pnl'].iloc[0] > 3*t_cost*total_contracts2trade): recommendation = 'STOP' elif (current_data['c1']['tr_dte'] < 35)&\ (pnl_frame['total_pnl'].iloc[0] < 3*t_cost*total_contracts2trade): recommendation = 'WINDDOWN' else: recommendation = 'HOLD' result_output = {'success': True,'ticker_head': ticker_head, 'QF_initial':QF_initial,'z1_initial': z1_initial, 'QF': bf_signals_output['qf'],'z1': bf_signals_output['zscore1'], 'short_tr_dte': current_data['c1']['tr_dte'], 'holding_tr_dte': holding_tr_dte, 'second_spread_weight': bf_signals_output['second_spread_weight_1'],'recommendation': recommendation} elif strategy_class == 'spread_carry': trades4_strategy = ts.get_trades_4strategy_alias(**kwargs) grouped = trades4_strategy.groupby('ticker') net_position = pd.DataFrame() net_position['ticker'] = (grouped['ticker'].first()).values net_position['qty'] = (grouped['trade_quantity'].sum()).values net_position = net_position[net_position['qty'] != 0] net_position['ticker_head'] = [cmi.get_contract_specs(x)['ticker_head'] for x in net_position['ticker']] price_output = [gfp.get_futures_price_preloaded(ticker=x, settle_date=date_to) for x in net_position['ticker']] net_position['tr_dte'] = [x['tr_dte'].values[0] for x in price_output] results_frame = pd.DataFrame() unique_tickerhead_list = net_position['ticker_head'].unique() results_frame['tickerHead'] = unique_tickerhead_list results_frame['ticker1'] = [None]*len(unique_tickerhead_list) results_frame['ticker2'] = [None]*len(unique_tickerhead_list) results_frame['qty'] = [None]*len(unique_tickerhead_list) results_frame['pnl'] = [None]*len(unique_tickerhead_list) results_frame['downside'] = [None]*len(unique_tickerhead_list) results_frame['indicator'] = [None]*len(unique_tickerhead_list) results_frame['timeHeld'] = [None]*len(unique_tickerhead_list) results_frame['recommendation'] = [None]*len(unique_tickerhead_list) spread_carry_output = osc.generate_spread_carry_sheet_4date(report_date=date_to) spread_report = spread_carry_output['spread_report'] pnl_output = tpnl.get_strategy_pnl(**kwargs) pnl_per_tickerhead = pnl_output['pnl_per_tickerhead'] for i in range(len(unique_tickerhead_list)): net_position_per_tickerhead = net_position[net_position['ticker_head'] == unique_tickerhead_list[i]] net_position_per_tickerhead.sort('tr_dte',ascending=True,inplace=True) selected_spread = spread_report[(spread_report['ticker1'] == net_position_per_tickerhead['ticker'].values[0]) & (spread_report['ticker2'] == net_position_per_tickerhead['ticker'].values[1])] results_frame['ticker1'][i] = selected_spread['ticker1'].values[0] results_frame['ticker2'][i] = selected_spread['ticker2'].values[0] results_frame['qty'][i] = net_position_per_tickerhead['qty'].values[0] selected_trades = trades4_strategy[trades4_strategy['ticker'] == results_frame['ticker1'].values[i]] price_output = gfp.get_futures_price_preloaded(ticker=results_frame['ticker1'].values[i], settle_date=pd.to_datetime(selected_trades['trade_date'].values[0])) results_frame['timeHeld'][i] = price_output['tr_dte'].values[0]-net_position_per_tickerhead['tr_dte'].values[0] results_frame['pnl'][i] = pnl_per_tickerhead[unique_tickerhead_list[i]].sum() if unique_tickerhead_list[i] in ['CL', 'B', 'ED']: results_frame['indicator'][i] = selected_spread['reward_risk'].values[0] if results_frame['qty'][i] > 0: results_frame['recommendation'][i] = 'STOP' elif results_frame['qty'][i] < 0: if results_frame['indicator'][i] > -0.06: results_frame['recommendation'][i] = 'STOP' else: results_frame['recommendation'][i] = 'HOLD' else: results_frame['indicator'][i] = selected_spread['q_carry'].values[0] if results_frame['qty'][i] > 0: if results_frame['indicator'][i] < 19: results_frame['recommendation'][i] = 'STOP' else: results_frame['recommendation'][i] = 'HOLD' elif results_frame['qty'][i] < 0: if results_frame['indicator'][i] > -9: results_frame['recommendation'][i] = 'STOP' else: results_frame['recommendation'][i] = 'HOLD' if results_frame['qty'][i] > 0: results_frame['downside'][i] = selected_spread['downside'].values[0]*results_frame['qty'][i] else: results_frame['downside'][i] = selected_spread['upside'].values[0]*results_frame['qty'][i] return {'success': True, 'results_frame': results_frame} elif strategy_class == 'vcs': greeks_out = sg.get_greeks_4strategy_4date(alias=kwargs['alias'], as_of_date=date_to) ticker_portfolio = greeks_out['ticker_portfolio'] if ticker_portfolio.empty: min_tr_dte = np.NaN result_output = {'success': False, 'net_oev': np.NaN, 'net_theta': np.NaN, 'long_short_ratio': np.NaN, 'recommendation': 'EMPTY', 'last_adjustment_days_ago': np.NaN, 'min_tr_dte': np.NaN, 'long_oev': np.NaN, 'short_oev': np.NaN, 'favQMove': np.NaN} else: min_tr_dte = min([exp.get_days2_expiration(ticker=x,date_to=date_to,instrument='options',con=con)['tr_dte'] for x in ticker_portfolio['ticker']]) net_oev = ticker_portfolio['total_oev'].sum() net_theta = ticker_portfolio['theta'].sum() long_portfolio = ticker_portfolio[ticker_portfolio['total_oev'] > 0] short_portfolio = ticker_portfolio[ticker_portfolio['total_oev'] < 0] short_portfolio['total_oev']=abs(short_portfolio['total_oev']) long_oev = long_portfolio['total_oev'].sum() short_oev = short_portfolio['total_oev'].sum() if (not short_portfolio.empty) & (not long_portfolio.empty): long_short_ratio = 100*long_oev/short_oev long_portfolio.sort('total_oev', ascending=False, inplace=True) short_portfolio.sort('total_oev', ascending=False, inplace=True) long_ticker = long_portfolio['ticker'].iloc[0] short_ticker = short_portfolio['ticker'].iloc[0] long_contract_specs = cmi.get_contract_specs(long_ticker) short_contract_specs = cmi.get_contract_specs(short_ticker) if 12*long_contract_specs['ticker_year']+long_contract_specs['ticker_month_num'] < \ 12*short_contract_specs['ticker_year']+short_contract_specs['ticker_month_num']: front_ticker = long_ticker back_ticker = short_ticker direction = 'long' else: front_ticker = short_ticker back_ticker = long_ticker direction = 'short' if 'vcs_output' in kwargs.keys(): vcs_output = kwargs['vcs_output'] else: vcs_output = ovcs.generate_vcs_sheet_4date(date_to=date_to) vcs_pairs = vcs_output['vcs_pairs'] selected_result = vcs_pairs[(vcs_pairs['ticker1'] == front_ticker) & (vcs_pairs['ticker2'] == back_ticker)] if selected_result.empty: favQMove = np.NaN else: current_Q = selected_result['Q'].iloc[0] q_limit = of.get_vcs_filter_values(product_group=long_contract_specs['ticker_head'], filter_type='tickerHead',direction=direction,indicator='Q') if direction == 'long': favQMove = current_Q-q_limit elif direction == 'short': favQMove = q_limit-current_Q else: long_short_ratio = np.NaN favQMove = np.NaN trades_frame = ts.get_trades_4strategy_alias(**kwargs) trades_frame_options = trades_frame[trades_frame['instrument'] == 'O'] last_adjustment_days_ago = len(exp.get_bus_day_list(date_to=date_to,datetime_from=max(trades_frame_options['trade_date']).to_datetime())) if favQMove >= 10 and last_adjustment_days_ago > 10: recommendation = 'STOP-ratio normalized' elif min_tr_dte<25: recommendation = 'STOP-close to expiration' elif np.isnan(long_short_ratio): recommendation = 'STOP-not a proper calendar' else: if long_short_ratio < 80: if favQMove < 0: recommendation = 'buy_options_to_grow' else: recommendation = 'buy_options_to_shrink' elif long_short_ratio > 120: if favQMove < 0: recommendation = 'sell_options_to_grow' else: recommendation = 'sell_options_to_shrink' else: recommendation = 'HOLD' result_output = {'success': True, 'net_oev': net_oev, 'net_theta': net_theta, 'long_short_ratio': long_short_ratio, 'recommendation': recommendation, 'last_adjustment_days_ago': last_adjustment_days_ago, 'min_tr_dte': min_tr_dte, 'long_oev': long_oev, 'short_oev': short_oev, 'favQMove': favQMove} else: result_output = {'success': False} if 'con' not in kwargs.keys(): con.close() return result_output
def get_vcs_pairs_4date_legacy(**kwargs): settle_date = kwargs['settle_date'] settle_datetime = cu.convert_doubledate_2datetime(settle_date) con = msu.get_my_sql_connection(**kwargs) liquid_options_frame = cl.generate_liquid_options_list_dataframe(settle_date=settle_date,con=con) contract_specs_output = [cmi.get_contract_specs(x) for x in liquid_options_frame['ticker']] liquid_options_frame['ticker_head'] = [x['ticker_head'] for x in contract_specs_output] liquid_options_frame['ticker_month'] = [x['ticker_month_num'] for x in contract_specs_output] liquid_options_frame['ticker_class'] = [x['ticker_class'] for x in contract_specs_output] liquid_options_frame['cal_dte'] = [(x-settle_datetime.date()).days for x in liquid_options_frame['expiration_date']] liquid_options_frame = liquid_options_frame[((liquid_options_frame['ticker_head'] == 'LN')&(liquid_options_frame['cal_dte'] <= 360))| ((liquid_options_frame['ticker_head']=='LC')&(liquid_options_frame['cal_dte']<=360)& (liquid_options_frame['ticker_month']%2==0))| ((liquid_options_frame['ticker_head']=='LC')&(liquid_options_frame['cal_dte']<=40)& (liquid_options_frame['ticker_month']%2==1))| ((liquid_options_frame['ticker_head']=='ES')&(liquid_options_frame['cal_dte']<=270))| ((liquid_options_frame['ticker_class']=='FX')&(liquid_options_frame['cal_dte']<=270)& (liquid_options_frame['ticker_month']%3==0))| ((liquid_options_frame['ticker_class']=='FX')&(liquid_options_frame['cal_dte']<=70)& (liquid_options_frame['ticker_month']%3!=0))| ((liquid_options_frame['ticker_head']=='GC')&(liquid_options_frame['cal_dte']<=360)& (liquid_options_frame['ticker_month'].isin([6,12])))| ((liquid_options_frame['ticker_head']=='GC')&(liquid_options_frame['cal_dte']<=270)& (liquid_options_frame['ticker_month'].isin([2,4,8,10])))| ((liquid_options_frame['ticker_head']=='GC')&(liquid_options_frame['cal_dte']<=70))| ((liquid_options_frame['ticker_head']=='SI')&(liquid_options_frame['cal_dte']<=360)& (liquid_options_frame['ticker_month'].isin([7,12])))| ((liquid_options_frame['ticker_head']=='SI')&(liquid_options_frame['cal_dte']<=270)& (liquid_options_frame['ticker_month'].isin([1,3,5,9])))| ((liquid_options_frame['ticker_head']=='SI')&(liquid_options_frame['cal_dte']<=70))| ((liquid_options_frame['ticker_class']=='Treasury')&(liquid_options_frame['cal_dte']<=180)& (liquid_options_frame['ticker_month']%3==0))| ((liquid_options_frame['ticker_class']=='Treasury')&(liquid_options_frame['cal_dte']<=70)& (liquid_options_frame['ticker_month']%3!=0))| ((liquid_options_frame['ticker_head']=='C')&(liquid_options_frame['cal_dte']<=540)& (liquid_options_frame['ticker_month']==12))| ((liquid_options_frame['ticker_head']=='C')&(liquid_options_frame['cal_dte']<=360)& (liquid_options_frame['ticker_month']==7))| ((liquid_options_frame['ticker_head']=='C')&(liquid_options_frame['cal_dte']<=270)& (liquid_options_frame['ticker_month'].isin([3,5,9])))| ((liquid_options_frame['ticker_head']=='C')&(liquid_options_frame['cal_dte']<=40))| ((liquid_options_frame['ticker_head']=='S')&(liquid_options_frame['cal_dte']<=540)& (liquid_options_frame['ticker_month']==11))| ((liquid_options_frame['ticker_head']=='S')&(liquid_options_frame['cal_dte']<=360)& (liquid_options_frame['ticker_month']==7))| ((liquid_options_frame['ticker_head']=='S')&(liquid_options_frame['cal_dte']<=270)& (liquid_options_frame['ticker_month'].isin([1,3,5,8,9])))| ((liquid_options_frame['ticker_head']=='S')&(liquid_options_frame['cal_dte']<=40))| ((liquid_options_frame['ticker_head']=='SM')&(liquid_options_frame['cal_dte']<=360)& (liquid_options_frame['ticker_month']==12))| ((liquid_options_frame['ticker_head']=='SM')&(liquid_options_frame['cal_dte']<=270)& (liquid_options_frame['ticker_month']==7))| ((liquid_options_frame['ticker_head']=='SM')&(liquid_options_frame['cal_dte']<=180)& (liquid_options_frame['ticker_month'].isin([1,3,5,8,9,10])))| ((liquid_options_frame['ticker_head']=='SM')&(liquid_options_frame['cal_dte']<=40))| ((liquid_options_frame['ticker_head']=='BO')&(liquid_options_frame['cal_dte']<=360)& (liquid_options_frame['ticker_month']==12))| ((liquid_options_frame['ticker_head']=='BO')&(liquid_options_frame['cal_dte']<=270)& (liquid_options_frame['ticker_month']==7))| ((liquid_options_frame['ticker_head']=='BO')&(liquid_options_frame['cal_dte']<=180)& (liquid_options_frame['ticker_month'].isin([1,3,5,8,9,10])))| ((liquid_options_frame['ticker_head']=='BO')&(liquid_options_frame['cal_dte']<=40))| ((liquid_options_frame['ticker_head']=='W')&(liquid_options_frame['cal_dte']<=360)& (liquid_options_frame['ticker_month']==12))| ((liquid_options_frame['ticker_head']=='W')&(liquid_options_frame['cal_dte']<=270)& (liquid_options_frame['ticker_month']==7))| ((liquid_options_frame['ticker_head']=='W')&(liquid_options_frame['cal_dte']<=180)& (liquid_options_frame['ticker_month'].isin([3,5,9])))| ((liquid_options_frame['ticker_head']=='W')&(liquid_options_frame['cal_dte']<=40))| ((liquid_options_frame['ticker_head']=='CL')&(liquid_options_frame['cal_dte']<=720)& (liquid_options_frame['ticker_month']==12))| ((liquid_options_frame['ticker_head']=='CL')&(liquid_options_frame['cal_dte']<=540)& (liquid_options_frame['ticker_month']==6))| ((liquid_options_frame['ticker_head']=='CL')&(liquid_options_frame['cal_dte']<=180))| ((liquid_options_frame['ticker_head']=='NG')&(liquid_options_frame['cal_dte']<=360))] liquid_options_frame.sort(['ticker_head','cal_dte'],ascending=[True,True],inplace=True) liquid_options_frame['tr_dte'] = [exp.get_days2_expiration(date_to=settle_date,con=con,instrument='options',ticker=x)['tr_dte'] for x in liquid_options_frame['ticker']] if 'con' not in kwargs.keys(): con.close() option_frame = liquid_options_frame[liquid_options_frame['tr_dte'] >= 35] option_frame.reset_index(drop=True,inplace=True) unique_ticker_heads = option_frame['ticker_head'].unique() tuples = [] for ticker_head_i in unique_ticker_heads: ticker_head_data = option_frame[option_frame['ticker_head'] == ticker_head_i] ticker_head_data.sort('cal_dte', ascending=True, inplace=True) if len(ticker_head_data.index) >= 2: for i in range(len(ticker_head_data.index)-1): for j in range(i+1,len(ticker_head_data.index)): tuples = tuples + [(ticker_head_data.index[i], ticker_head_data.index[j])] return pd.DataFrame([(option_frame['ticker'][indx[0]], option_frame['ticker'][indx[1]], option_frame['ticker_head'][indx[0]], option_frame['ticker_class'][indx[0]], option_frame['tr_dte'][indx[0]], option_frame['tr_dte'][indx[1]]) for indx in tuples],columns=['ticker1','ticker2','tickerHead','tickerClass','trDte1','trDte2'])
def get_historical_risk_4strategy(**kwargs): con = msu.get_my_sql_connection(**kwargs) alias = kwargs['alias'] #print(alias) if 'as_of_date' in kwargs.keys(): as_of_date = kwargs['as_of_date'] else: as_of_date = exp.doubledate_shift_bus_days() if 'datetime5_years_ago' in kwargs.keys(): datetime5_years_ago = kwargs['datetime5_years_ago'] else: date5_years_ago = cu.doubledate_shift(as_of_date,5*365) datetime5_years_ago = cu.convert_doubledate_2datetime(date5_years_ago) net_position = ts.get_net_position_4strategy_alias(alias=alias,con=con) net_position = net_position[net_position['instrument'] != 'O'] if 'con' not in kwargs.keys(): con.close() if net_position.empty: return {'downside': 0, 'pnl_5_change': []} amcb_output = [opUtil.get_aggregation_method_contracts_back(cmi.get_contract_specs(x)) for x in net_position['ticker']] aggregation_method = pd.DataFrame(amcb_output)['aggregation_method'].max() if aggregation_method == 12: contracts_back = const.annualContractsBack elif aggregation_method == 3: contracts_back = const.quarterlyContractsBack elif aggregation_method == 1: contracts_back = const.monthlyContractsBack aligned_output = opUtil.get_aligned_futures_data(contract_list=net_position['ticker'].values, aggregation_method=aggregation_method, contracts_back=contracts_back,date_to=as_of_date,**kwargs) aligned_data = aligned_output['aligned_data'] last5_years_indx = aligned_data['settle_date'] >= datetime5_years_ago data_last5_years = aligned_data[last5_years_indx] ticker_head_list = [cmi.get_contract_specs(x)['ticker_head'] for x in net_position['ticker']] contract_multiplier_list = [cmi.contract_multiplier[x] for x in ticker_head_list] pnl_5_change_list = [contract_multiplier_list[x]* net_position['qty'].iloc[x]* data_last5_years['c' + str(x+1)]['change_5'] for x in range(len(net_position.index))] pnl_5_change = sum(pnl_5_change_list) percentile_vector = stats.get_number_from_quantile(y=pnl_5_change.values, quantile_list=[1, 15], clean_num_obs=max(100, round(3*len(pnl_5_change.values)/4))) downside = (percentile_vector[0]+percentile_vector[1])/2 unique_ticker_head_list = list(set(ticker_head_list)) ticker_head_based_pnl_5_change = {x: sum([pnl_5_change_list[y] for y in range(len(ticker_head_list)) if ticker_head_list[y] == x]) for x in unique_ticker_head_list} return {'downside': downside, 'pnl_5_change': pnl_5_change,'ticker_head_based_pnl_5_change':ticker_head_based_pnl_5_change}
def get_futures_expiration(ticker): contract_specs = cmf.get_contract_specs(ticker) ticker_head = contract_specs['ticker_head'] ticker_class = cmf.ticker_class[ticker_head] ticker_year = contract_specs['ticker_year'] ticker_month_num = contract_specs['ticker_month_num'] bday_us = CustomBusinessDay(calendar=get_calendar_4ticker_head(ticker_head)) if ticker_class == 'Ag': dts = pd.date_range(start=pd.datetime(ticker_year,ticker_month_num,10), end=pd.datetime(ticker_year, ticker_month_num, 15), freq=bday_us) if pd.datetime(ticker_year,ticker_month_num,15) in dts: exp_indx = -2 else: exp_indx = -1 elif ticker_head == 'LC': dts = pd.date_range(pd.datetime(ticker_year,ticker_month_num, 1), periods=32, freq=bday_us) dts = dts[dts.month==ticker_month_num] exp_indx = -1 elif ticker_head == 'LN': dts = pd.date_range(pd.datetime(ticker_year,ticker_month_num, 1), periods=15, freq=bday_us) exp_indx = 9 elif ticker_head == 'FC': prev_output = get_prev_ticker_year_month(ticker_year,ticker_month_num) ticker_year_prev = prev_output['ticker_year_prev'] ticker_month_num_prev = prev_output['ticker_month_num_prev'] dts = pd.date_range(pd.datetime(ticker_year_prev,ticker_month_num_prev, 1), periods=62, freq=bday_us) dts = dts[dts.month <= ticker_month_num] thursday_list = [] thursday_no = [] num_thursdays = 0 holiday_requirement_dummy = [] for i in range(len(dts)): if dts.dayofweek[i]==3: thursday_list.append(dts[i]) if dts.month[i]==ticker_month_num: num_thursdays += 1 thursday_no.append(num_thursdays) else: thursday_no.append(-1) if(dts.dayofweek[i-1]==2)and(dts.dayofweek[i-2]==1)and(dts.dayofweek[i-3]==0)and(dts.dayofweek[i-4]==4): holiday_requirement_dummy.append(True) else: holiday_requirement_dummy.append(False) if ticker_month_num is not 11: dts = [thursday_list[i] for i in range(len(thursday_list)) if holiday_requirement_dummy[i]] else: dts = [thursday_list[i] for i in range(len(thursday_list)) if holiday_requirement_dummy[i] and thursday_no[i] < 4] exp_indx = -1 elif ticker_head == 'CL': prev_output = get_prev_ticker_year_month(ticker_year,ticker_month_num) ticker_year_prev = prev_output['ticker_year_prev'] ticker_month_num_prev = prev_output['ticker_month_num_prev'] dts = pd.date_range(start=pd.datetime(ticker_year_prev,ticker_month_num_prev,1), end=pd.datetime(ticker_year_prev, ticker_month_num_prev, 25), freq=bday_us) exp_indx = -4 elif ticker_head == 'HO' or ticker_head == 'RB': prev_output = get_prev_ticker_year_month(ticker_year,ticker_month_num) ticker_year_prev = prev_output['ticker_year_prev'] ticker_month_num_prev = prev_output['ticker_month_num_prev'] dts = pd.date_range(pd.datetime(ticker_year_prev,ticker_month_num_prev, 1), periods=32, freq=bday_us) dts = dts[dts.month == ticker_month_num_prev] exp_indx = -1 elif ticker_head == 'NG': prev_output = get_prev_ticker_year_month(ticker_year,ticker_month_num) ticker_year_prev = prev_output['ticker_year_prev'] ticker_month_num_prev = prev_output['ticker_month_num_prev'] dts = pd.date_range(start=pd.datetime(ticker_year_prev, ticker_month_num_prev, 1), end=pd.datetime(ticker_year, ticker_month_num, 1), freq=bday_us) if pd.datetime(ticker_year,ticker_month_num,1) in dts: exp_indx = -4 else: exp_indx = -3 elif ticker_head in ['ES', 'NQ']: dts = pd.date_range(pd.datetime(ticker_year, ticker_month_num, 1), periods=32) dts = dts[dts.dayofweek == 4] exp_indx = 2 elif ticker_class == 'FX': dts = pd.date_range(pd.datetime(ticker_year, ticker_month_num, 1), periods=32) wednesdays = dts[dts.dayofweek == 2] dts = pd.date_range(start=pd.datetime(ticker_year, ticker_month_num, 1), end=wednesdays[2], freq=bday_us) exp_indx = -3 elif ticker_head in ['TY', 'US']: dts = pd.date_range(pd.datetime(ticker_year, ticker_month_num, 1), periods=32, freq=bday_us) dts = dts[dts.month == ticker_month_num] exp_indx = -8 elif ticker_head in ['FV', 'TU']: dts = pd.date_range(pd.datetime(ticker_year, ticker_month_num, 1), periods=32, freq=bday_us) dts = dts[dts.month == ticker_month_num] exp_indx = -1 elif ticker_head == 'ED': dts = pd.date_range(pd.datetime(ticker_year,ticker_month_num, 1), periods=32) dts = dts[dts.month == ticker_month_num] wednesday_list = dts[dts.dayofweek == 2] dts = pd.date_range(start=pd.datetime(ticker_year,ticker_month_num, 1), end=wednesday_list[2], freq=bday_us) exp_indx = -3 elif ticker_class == 'Metal': dts = pd.date_range(pd.datetime(ticker_year, ticker_month_num, 1), periods=32, freq=bday_us) dts = dts[dts.month == ticker_month_num] exp_indx = -3 elif ticker_head == 'SB': prev_output = get_prev_ticker_year_month(ticker_year,ticker_month_num) ticker_year_prev = prev_output['ticker_year_prev'] ticker_month_num_prev = prev_output['ticker_month_num_prev'] dts = pd.date_range(pd.datetime(ticker_year_prev, ticker_month_num_prev, 1), periods=32, freq=bday_us) dts = dts[dts.month == ticker_month_num_prev] exp_indx = -1 elif ticker_head == 'B': if 100*ticker_year+ticker_month_num <= 201602: prev_output = get_prev_ticker_year_month(ticker_year,ticker_month_num) ticker_year_prev = prev_output['ticker_year_prev'] ticker_month_num_prev = prev_output['ticker_month_num_prev'] dts = pd.date_range(start=pd.datetime(ticker_year_prev, ticker_month_num_prev, 1), end=pd.datetime(ticker_year, ticker_month_num, 1)) dts = pd.date_range(start=pd.datetime(ticker_year_prev, ticker_month_num_prev, 1), end=dts[-16], freq=bday_us) exp_indx = -2 else: prev_output = get_prev_ticker_year_month(ticker_year,ticker_month_num) prev_output2 = get_prev_ticker_year_month(prev_output['ticker_year_prev'], prev_output['ticker_month_num_prev']) ticker_year_prev2 = prev_output2['ticker_year_prev'] ticker_month_num_prev2 = prev_output2['ticker_month_num_prev'] dts = pd.date_range(pd.datetime(ticker_year_prev2, ticker_month_num_prev2, 1), periods=32, freq=bday_us) if ticker_month_num_prev2 == 12: dts_week = pd.bdate_range(pd.datetime(ticker_year_prev2, ticker_month_num_prev2, 1), periods=32) special_days = set(dts_week).difference(set(dts)) bus_days_before_special_days = [dts_week[x] for x in range(len(dts_week)-1) if dts_week[x+1] in special_days] dts = pd.DatetimeIndex([dts[x] for x in range(len(dts)) if dts[x] not in bus_days_before_special_days]) dts = dts[dts.month==ticker_month_num_prev2] exp_indx = -1 elif ticker_head == 'KC': dts = pd.date_range(pd.datetime(ticker_year,ticker_month_num, 1), periods=32, freq=bday_us) dts = dts[dts.month==ticker_month_num] exp_indx = -9 elif ticker_head == 'CC': dts = pd.date_range(pd.datetime(ticker_year,ticker_month_num, 1), periods=32, freq=bday_us) dts = dts[dts.month==ticker_month_num] exp_indx = -12 elif ticker_head == 'CT': dts = pd.date_range(pd.datetime(ticker_year,ticker_month_num, 1), periods=32, freq=bday_us) dts = dts[dts.month==ticker_month_num] exp_indx = -17 elif ticker_head == 'OJ': dts = pd.date_range(pd.datetime(ticker_year,ticker_month_num, 1), periods=32, freq=bday_us) dts = dts[dts.month == ticker_month_num] exp_indx = -15 return dts[exp_indx].to_datetime()
def get_strategy_pnl(**kwargs): alias = kwargs['alias'] con = msu.get_my_sql_connection(**kwargs) strategy_info = ts.get_strategy_info_from_alias(alias=alias, con=con) if 'as_of_date' in kwargs.keys(): as_of_date = kwargs['as_of_date'] else: as_of_date = exp.doubledate_shift_bus_days() if 'broker' in kwargs.keys(): broker = kwargs['broker'] else: broker = 'abn' open_date = int(strategy_info['open_date'].strftime('%Y%m%d')) #open_date = 20160920 close_date = int(strategy_info['close_date'].strftime('%Y%m%d')) if close_date > as_of_date: close_date = as_of_date bus_day_list = exp.get_bus_day_list(date_from=open_date, date_to=close_date) trades_frame = ts.get_trades_4strategy_alias(alias=alias, con=con) if sum(trades_frame['instrument'] == 'S') > 0: stock_strategy_Q = True else: stock_strategy_Q = False if stock_strategy_Q: return { 'pnl_frame': pd.DataFrame(), 'daily_pnl': np.nan, 'total_pnl': np.nan } unique_ticker_list = trades_frame['ticker'].unique() stock_data_dictionary = { x: gsp.get_stock_price_preloaded(ticker=x) for x in unique_ticker_list } trades_frame['t_cost'] = [ smi.get_ib_t_cost(price=trades_frame['trade_price'].iloc[x], quantity=trades_frame['trade_quantity'].iloc[x]) for x in range(len(trades_frame.index)) ] pnl_path = [ get_stock_strategy_pnl_4day( alias=alias, pnl_date=x, con=con, trades_frame=trades_frame, stock_data_dictionary=stock_data_dictionary) for x in bus_day_list ] nan_price_q_list = [x['nan_price_q'] for x in pnl_path] good_price_q_list = [not i for i in nan_price_q_list] bus_day_after_nan_list = [ bus_day_list[x + 1] for x in range(len(bus_day_list) - 1) if nan_price_q_list[x] ] pnl_path = [ pnl_path[x] for x in range(len(pnl_path)) if good_price_q_list[x] ] bus_day_list = [ bus_day_list[x] for x in range(len(bus_day_list)) if good_price_q_list[x] ] # print(bus_day_list) # print(bus_day_after_nan_list) if len(bus_day_after_nan_list) > 0: pnl_path_after_nan = [ get_stock_strategy_pnl_4day( alias=alias, pnl_date=x, con=con, trades_frame=trades_frame, broker=broker, shift_in_days=2, stock_data_dictionary=stock_data_dictionary) for x in bus_day_after_nan_list ] for i in range(len(bus_day_after_nan_list)): if bus_day_after_nan_list[i] in bus_day_list: index_val = bus_day_list.index(bus_day_after_nan_list[i]) pnl_path[index_val] = pnl_path_after_nan[i] pnl_frame = pd.DataFrame(pnl_path) pnl_frame['settle_date'] = bus_day_list output_dictionary = { 'pnl_frame': pnl_frame[[ 'settle_date', 'position_pnl', 'intraday_pnl', 't_cost', 'total_pnl' ]], 'daily_pnl': pnl_frame['total_pnl'].values[-1], 'total_pnl': pnl_frame['total_pnl'].sum() } else: ticker_head_list = [ cmi.get_contract_specs(x)['ticker_head'] for x in trades_frame['ticker'] ] unique_ticker_head_list = list(set(ticker_head_list)) if 'futures_data_dictionary' in kwargs.keys(): futures_data_dictionary = kwargs['futures_data_dictionary'] else: futures_data_dictionary = { x: gfp.get_futures_price_preloaded(ticker_head=x) for x in unique_ticker_head_list } trades_frame['contract_multiplier'] = [ cmi.contract_multiplier[x] for x in ticker_head_list ] trades_frame['t_cost'] = [ cmi.get_t_cost(ticker_head=x, broker=broker) for x in ticker_head_list ] pnl_path = [ get_strategy_pnl_4day( alias=alias, pnl_date=x, con=con, trades_frame=trades_frame, broker=broker, futures_data_dictionary=futures_data_dictionary) for x in bus_day_list ] nan_price_q_list = [x['nan_price_q'] for x in pnl_path] good_price_q_list = [not i for i in nan_price_q_list] bus_day_after_nan_list = [ bus_day_list[x + 1] for x in range(len(bus_day_list) - 1) if nan_price_q_list[x] ] pnl_path = [ pnl_path[x] for x in range(len(pnl_path)) if good_price_q_list[x] ] bus_day_list = [ bus_day_list[x] for x in range(len(bus_day_list)) if good_price_q_list[x] ] #print(bus_day_list) #print(bus_day_after_nan_list) if len(bus_day_after_nan_list) > 0: pnl_path_after_nan = [ get_strategy_pnl_4day( alias=alias, pnl_date=x, con=con, trades_frame=trades_frame, broker=broker, shift_in_days=2, futures_data_dictionary=futures_data_dictionary) for x in bus_day_after_nan_list ] for i in range(len(bus_day_after_nan_list)): index_val = bus_day_list.index(bus_day_after_nan_list[i]) pnl_path[index_val] = pnl_path_after_nan[i] pnl_per_tickerhead_list = [x['pnl_per_tickerhead'] for x in pnl_path] pnl_per_tickerhead = pd.concat(pnl_per_tickerhead_list, axis=1, sort=True) pnl_per_tickerhead = pnl_per_tickerhead[['pnl_total']] pnl_per_tickerhead = pnl_per_tickerhead.transpose() if len(unique_ticker_head_list) > 1: zero_indx = [[x not in y.index for y in pnl_per_tickerhead_list] for x in pnl_per_tickerhead.columns] for i in range(len(pnl_per_tickerhead.columns)): pnl_per_tickerhead.iloc[:, i][zero_indx[i]] = 0 pnl_per_tickerhead['settle_date'] = bus_day_list pnl_per_tickerhead.reset_index(inplace=True, drop=True) pnl_frame = pd.DataFrame(pnl_path) pnl_frame['settle_date'] = bus_day_list daily_index = pnl_frame['settle_date'] == as_of_date if sum(daily_index) == 0: daily_pnl = np.nan else: daily_pnl = pnl_frame['total_pnl'].values[-1] output_dictionary = { 'pnl_frame': pnl_frame[[ 'settle_date', 'position_pnl', 'intraday_pnl', 't_cost', 'total_pnl' ]], 'pnl_per_tickerhead': pnl_per_tickerhead, 'daily_pnl': daily_pnl, 'total_pnl': pnl_frame['total_pnl'].sum() } if 'con' not in kwargs.keys(): con.close() return output_dictionary
def get_options_expiration(ticker): contract_specs = cmf.get_contract_specs(ticker) ticker_head = contract_specs['ticker_head'] ticker_class = cmf.ticker_class[ticker_head] ticker_year = contract_specs['ticker_year'] ticker_month_num = contract_specs['ticker_month_num'] bday_us = CustomBusinessDay(calendar=get_calendar_4ticker_head(ticker_head)) if ticker_class == 'Ag' or ticker_class == 'Treasury': prev_output = get_prev_ticker_year_month(ticker_year, ticker_month_num) ticker_year_prev = prev_output['ticker_year_prev'] ticker_month_num_prev = prev_output['ticker_month_num_prev'] dts = pd.date_range(pd.datetime(ticker_year_prev, ticker_month_num_prev, 1), periods=32) dts = dts[dts.month == ticker_month_num_prev] bu_dts = pd.date_range(pd.datetime(ticker_year_prev, ticker_month_num_prev, 1), periods=32, freq=bday_us) bu_dts = bu_dts[bu_dts.month == ticker_month_num_prev] fridays = dts[dts.dayofweek == 4] selected_fridays = fridays[fridays <= bu_dts[-3]] if selected_fridays[-1] in bu_dts: dts = selected_fridays else: dts = bu_dts[bu_dts < selected_fridays[-1]] exp_indx = -1 elif ticker_head == 'LC': bu_dts = pd.date_range(pd.datetime(ticker_year, ticker_month_num, 1), periods=32, freq=bday_us) fridays = bu_dts[bu_dts.dayofweek == 4] dts = fridays exp_indx = 0 elif ticker_head == 'LN': dts = pd.date_range(pd.datetime(ticker_year,ticker_month_num, 1), periods=15, freq=bday_us) exp_indx = 9 elif ticker_head == 'CL': prev_output = get_prev_ticker_year_month(ticker_year, ticker_month_num) ticker_year_prev = prev_output['ticker_year_prev'] ticker_month_num_prev = prev_output['ticker_month_num_prev'] dts = pd.date_range(start=pd.datetime(ticker_year_prev,ticker_month_num_prev,1), end=pd.datetime(ticker_year_prev, ticker_month_num_prev, 25), freq=bday_us) exp_indx = -7 elif ticker_head == 'NG': prev_output = get_prev_ticker_year_month(ticker_year,ticker_month_num) ticker_year_prev = prev_output['ticker_year_prev'] ticker_month_num_prev = prev_output['ticker_month_num_prev'] dts = pd.date_range(start=pd.datetime(ticker_year_prev, ticker_month_num_prev, 1), end=pd.datetime(ticker_year, ticker_month_num, 1), freq=bday_us) if pd.datetime(ticker_year,ticker_month_num,1) in dts: exp_indx = -5 else: exp_indx = -4 elif ticker_head in ['ES', 'NQ']: dts = pd.date_range(pd.datetime(ticker_year, ticker_month_num, 1), periods=32) dts = dts[dts.dayofweek == 4] exp_indx = 2 elif ticker_class == 'FX': dts = pd.date_range(pd.datetime(ticker_year, ticker_month_num, 1), periods=32) fridays = dts[dts.dayofweek == 4] wednesdays = dts[dts.dayofweek == 2] dts = fridays[fridays < wednesdays[2]] exp_indx = -2 elif ticker_class == 'STIR': if (ticker_month_num % 3) or ticker_head in ['E0', 'E2', 'E3', 'E4', 'E5']: dts = pd.date_range(pd.datetime(ticker_year, ticker_month_num, 1), periods=32) fridays = dts[dts.dayofweek == 4] wednesdays = dts[dts.dayofweek == 2] dts = fridays[fridays < wednesdays[2]] exp_indx = -1 else: dts = pd.date_range(pd.datetime(ticker_year, ticker_month_num, 1), periods=32) dts = dts[dts.month == ticker_month_num] wednesday_list = dts[dts.dayofweek == 2] dts = pd.date_range(start=pd.datetime(ticker_year, ticker_month_num, 1), end=wednesday_list[2], freq=bday_us) exp_indx = -3 elif ticker_class == 'Metal': prev_output = get_prev_ticker_year_month(ticker_year, ticker_month_num) ticker_year_prev = prev_output['ticker_year_prev'] ticker_month_num_prev = prev_output['ticker_month_num_prev'] dts = pd.date_range(pd.datetime(ticker_year_prev, ticker_month_num_prev, 1), periods=32) dts = dts[dts.month == ticker_month_num_prev] bu_dts = pd.date_range(pd.datetime(ticker_year_prev, ticker_month_num_prev, 1), periods=32, freq=bday_us) bu_dts = bu_dts[bu_dts.month == ticker_month_num_prev] cal_days_after = dts[bu_dts[-4] < dts] #return {'dts': dts, 'bu_dts': bu_dts} if cal_days_after[0] in bu_dts: dts = bu_dts exp_indx = -4 else: cal_days_after = dts[bu_dts[-5] < dts] if cal_days_after[0] in bu_dts: dts = bu_dts exp_indx = -5 else: dts = bu_dts exp_indx = -6 return dts[exp_indx].to_datetime()
def backtest_scalping_4date(**kwargs): ticker = kwargs['ticker'] trade_date = kwargs['trade_date'] profit_target = kwargs['profit_target'] stop_loss = kwargs['stop_loss'] ticker_head = cmi.get_contract_specs(ticker)['ticker_head'] tick_size = cmi.tick_size[ticker_head] data_frame_out = rid.load_csv_file_4ticker(ticker=ticker, folder_date=trade_date) best_bid_p = data_frame_out[data_frame_out['field'] == 'BestBidPrice'] best_ask_p = data_frame_out[data_frame_out['field'] == 'BestAskPrice'] best_bid_p['value'] = best_bid_p['value'].astype('float64') best_ask_p['value'] = best_ask_p['value'].astype('float64') best_bid_p['value'] = [ tfl.convert_trade_price_from_tt(price=x, ticker_head=ticker_head) for x in best_bid_p['value'] ] best_ask_p['value'] = [ tfl.convert_trade_price_from_tt(price=x, ticker_head=ticker_head) for x in best_ask_p['value'] ] snapshot_data = get_order_book_signals_4date(ticker=ticker, trade_date=trade_date) position = 0 entry_list = [] exit_list = [] entry_time = [] exit_time = [] for i in range(len(snapshot_data.index) - 1): #print(i) ask_updates = best_ask_p[ (best_ask_p['time'] >= snapshot_data.index[i]) & (best_ask_p['time'] < snapshot_data.index[i + 1])] bid_updates = best_bid_p[ (best_bid_p['time'] >= snapshot_data.index[i]) & (best_bid_p['time'] < snapshot_data.index[i + 1])] if (position == 0) and snapshot_data['bias'].iloc[i]: if ask_updates.empty: continue ask_market_cross = ask_updates[ ask_updates['value'] <= snapshot_data['best_bid_p'].iloc[i]] if ask_market_cross.empty: continue entry_list.append(ask_market_cross['value'].iloc[0]) entry_time.append(ask_market_cross['time'].iloc[0]) stop_point = ask_market_cross['value'].iloc[ 0] - stop_loss * tick_size exit_point = ask_market_cross['value'].iloc[ 0] + profit_target * tick_size position = 1 elif (i == len(snapshot_data.index) - 2) and (position == 1): exit_list.append(snapshot_data['best_bid_p'].iloc[i + 1]) exit_time.append(snapshot_data.index[i + 1]) position = 0 elif position == 1: if bid_updates.empty: continue long_stop = bid_updates[bid_updates['value'] <= stop_point] if not long_stop.empty: exit_list.append(long_stop['value'].iloc[0]) exit_time.append(long_stop['time'].iloc[0]) position = 0 continue long_exit = bid_updates[bid_updates['value'] >= exit_point] if not long_exit.empty: exit_list.append(long_exit['value'].iloc[0]) exit_time.append(long_exit['time'].iloc[0]) position = 0 data_frame = pd.DataFrame.from_items([('entry_p', entry_list), ('exit_p', exit_list), ('entry_time', entry_time), ('exit_time', exit_time)]) data_frame['pnl'] = data_frame['exit_p'] - data_frame['entry_p'] return data_frame
def process_cme_options_4ticker(**kwargs): ticker = kwargs['ticker'] report_date = kwargs['report_date'] contract_specs_output = cmi.get_contract_specs(ticker) ticker_class = contract_specs_output['ticker_class'] ticker_head = contract_specs_output['ticker_head'] name_type_output = cmeu.get_file_name_type_from_tickerclass(ticker_class, 'options') file_name = name_type_output['file_name'] file_type = name_type_output['file_type'] if file_type == 'txt': if 'data_read_out' in kwargs.keys(): data_read_out = kwargs['data_read_out'][file_name] else: data_read_out = rcf.read_cme_settle_txt_files(file_name=file_name, report_date=report_date) title_frame = data_read_out['title_frame'] settle_list = data_read_out['settle_list'] month_strike_list = data_read_out['month_strike_list'] volume_filtered_list = data_read_out['volume_filtered_list'] interest_filtered_list = data_read_out['interest_filtered_list'] selected_frame = title_frame[(title_frame['asset_type'] == 'options') & (title_frame['ticker_head'] == ticker_head)] datetime_conversion = [dt.datetime.strptime(x.replace('JLY', 'JUL'), '%b%y') for x in selected_frame['maturity_string']] selected_frame['ticker_year'] = [x.year for x in datetime_conversion] selected_frame['ticker_month'] = [x.month for x in datetime_conversion] selected_frame['ticker'] = [ticker_head + cmi.full_letter_month_list[selected_frame.loc[x, 'ticker_month']-1] + str(selected_frame.loc[x, 'ticker_year']) for x in selected_frame.index] selected_frame_call = selected_frame[(selected_frame['ticker'] == ticker)&(selected_frame['option_type'] == 'C')] selected_frame_put = selected_frame[(selected_frame['ticker'] == ticker)&(selected_frame['option_type'] == 'P')] if selected_frame_call.empty or selected_frame_put.empty: return {'success': False, 'settle_frame': pd.DataFrame()} selected_call_indx = selected_frame_call.index[0] selected_put_indx = selected_frame_put.index[0] call_dataframe = pd.DataFrame.from_items([('strike', month_strike_list[selected_call_indx]), ('settle', settle_list[selected_call_indx]), ('volume', volume_filtered_list[selected_call_indx]), ('interest', interest_filtered_list[selected_call_indx])]) put_dataframe = pd.DataFrame.from_items([('strike', month_strike_list[selected_put_indx]), ('settle', settle_list[selected_put_indx]), ('volume', volume_filtered_list[selected_put_indx]), ('interest', interest_filtered_list[selected_put_indx])]) call_dataframe['option_type'] = 'C' put_dataframe['option_type'] = 'P' settle_frame = pd.concat([call_dataframe, put_dataframe]) elif file_type == 'csv': data_read_out = rcf.read_cme_option_settle_csv_files(file_name=file_name, report_date=report_date) settle_frame = pd.DataFrame() selected_frame = data_read_out[data_read_out['ticker_head'] == ticker_head] selected_frame['ticker_month'] = selected_frame['CONTRACT MONTH'].astype('int') selected_frame['ticker'] = [ticker_head + cmi.full_letter_month_list[selected_frame.loc[x, 'ticker_month']-1] + str(selected_frame.loc[x, 'CONTRACT YEAR']) for x in selected_frame.index] selected_frame = selected_frame[selected_frame['ticker'] == ticker] if selected_frame.empty: return {'success': False, 'settle_frame': pd.DataFrame()} settle_frame['ticker'] = selected_frame['ticker'] settle_frame['option_type'] = selected_frame['PUT/CALL'] settle_frame['strike'] = selected_frame['STRIKE'] settle_frame['settle'] = selected_frame['SETTLE'] settle_frame['volume'] = selected_frame['EST. VOL'] settle_frame['interest'] = selected_frame['PRIOR INT'] settle_frame.reset_index(inplace=True, drop=True) settle_frame['strike'] = settle_frame['strike'].astype('float64') if ticker_head in ['C', 'S', 'W', 'KW']: splited_strings = [x.split("'") for x in settle_frame['settle']] settle_frame['settle'] = [(0 if x[0] == '' else int(x[0])) + int(x[1])*0.125 if len(x) == 2 else np.NaN for x in splited_strings] settle_frame['strike'] = settle_frame['strike']/10 elif ticker_head in ['ED', 'E0', 'E2', 'E3', 'E4', 'E5']: settle_frame['settle'] = settle_frame['settle'].replace('CAB', cmi.option_cabinet_values[ticker_head]) settle_frame['settle'] = settle_frame['settle'].astype('float64') half_quarter_indx = settle_frame['strike'] % 25 == 12 settle_frame['strike'] = settle_frame['strike']/100 settle_frame['strike'][half_quarter_indx] = settle_frame['strike'][half_quarter_indx]+0.005 elif ticker_head in ['SM']: settle_frame['settle'] = settle_frame['settle'].astype('float64') settle_frame['strike'] = settle_frame['strike']/100 elif ticker_head in ['BO']: settle_frame['settle'] = settle_frame['settle'].astype('float64') settle_frame['strike'] = settle_frame['strike']/1000 elif ticker_head in ['LC', 'LN', 'ES', 'NQ']: settle_frame['settle'] = settle_frame['settle'].replace('CAB', cmi.option_cabinet_values[ticker_head]) settle_frame['settle'] = settle_frame['settle'].astype('float64') elif ticker_head in ['FC']: settle_frame['settle'] = settle_frame['settle'].replace('CAB', cmi.option_cabinet_values[ticker_head]) settle_frame['settle'] = settle_frame['settle'].astype('float64') settle_frame['strike'] = settle_frame['strike']/100 elif ticker_head in ['AD', 'CD']: settle_frame['settle'] = settle_frame['settle'].replace('CAB', 100*cmi.option_cabinet_values[ticker_head]) settle_frame['settle'] = settle_frame['settle'].astype('float64')/100 settle_frame['strike'] = settle_frame['strike']/10000 elif ticker_head in ['EC']: settle_frame['settle'] = settle_frame['settle'].replace('CAB', cmi.option_cabinet_values[ticker_head]) settle_frame['settle'] = settle_frame['settle'].astype('float64') settle_frame['strike'] = settle_frame['strike']/1000 elif ticker_head in ['JY']: settle_frame['settle'] = settle_frame['settle'].replace('CAB', cmi.option_cabinet_values[ticker_head]/1000) settle_frame['settle'] = settle_frame['settle'].astype('float64')*1000 settle_frame['strike'] = settle_frame['strike']*10 elif ticker_head in ['BP']: settle_frame['settle'] = settle_frame['settle'].replace('CAB', 100*cmi.option_cabinet_values[ticker_head]) settle_frame['settle'] = settle_frame['settle'].astype('float64')/100 settle_frame['strike'] = settle_frame['strike']/1000 elif ticker_head in ['TU', 'FV']: half_quarter_indx = settle_frame['strike'] % 25 == 12 settle_frame['strike'] = settle_frame['strike']/100 settle_frame['strike'][half_quarter_indx] = settle_frame['strike'][half_quarter_indx]+0.005 splited_strings = [x.split("'") for x in settle_frame['settle']] settle_frame['settle'] = [(0 if x[0] == '' else int(x[0])) + int(x[1])/640 if len(x) == 2 else (cmi.option_cabinet_values[ticker_head] if x[0] == 'CAB' else np.NaN) for x in splited_strings] elif ticker_head in ['TY','US']: settle_frame['strike'] = settle_frame['strike']/100 splited_strings = [x.split("'") for x in settle_frame['settle']] settle_frame['settle'] = [(0 if x[0] == '' else int(x[0])) + int(x[1])/64 if len(x) == 2 else (cmi.option_cabinet_values[ticker_head] if x[0] == 'CAB' else np.NaN) for x in splited_strings] elif ticker_head in ['GC', 'SI', 'CL', 'NG', 'RB', 'HO']: settle_frame['settle'] = settle_frame['settle'].astype('float64') settle_frame['volume'] = settle_frame['volume'].replace('', 0) settle_frame['volume'] = settle_frame['volume'].astype('int') settle_frame['interest'] = settle_frame['interest'].replace('', 0) settle_frame['interest'] = settle_frame['interest'].astype('int') return {'success': True, 'settle_frame': settle_frame}
def get_intraday_spread_signals(**kwargs): ticker_list = kwargs['ticker_list'] date_to = kwargs['date_to'] #print(ticker_list) ticker_list = [x for x in ticker_list if x is not None] ticker_head_list = [ cmi.get_contract_specs(x)['ticker_head'] for x in ticker_list ] ticker_class_list = [cmi.ticker_class[x] for x in ticker_head_list] #print('-'.join(ticker_list)) if 'tr_dte_list' in kwargs.keys(): tr_dte_list = kwargs['tr_dte_list'] else: tr_dte_list = [ exp.get_days2_expiration(ticker=x, date_to=date_to, instrument='futures')['tr_dte'] for x in ticker_list ] if 'aggregation_method' in kwargs.keys( ) and 'contracts_back' in kwargs.keys(): aggregation_method = kwargs['aggregation_method'] contracts_back = kwargs['contracts_back'] else: amcb_output = [ opUtil.get_aggregation_method_contracts_back( cmi.get_contract_specs(x)) for x in ticker_list ] aggregation_method = max( [x['aggregation_method'] for x in amcb_output]) contracts_back = min([x['contracts_back'] for x in amcb_output]) if 'futures_data_dictionary' in kwargs.keys(): futures_data_dictionary = kwargs['futures_data_dictionary'] else: futures_data_dictionary = { x: gfp.get_futures_price_preloaded(ticker_head=x) for x in list(set(ticker_head_list)) } if 'use_last_as_current' in kwargs.keys(): use_last_as_current = kwargs['use_last_as_current'] else: use_last_as_current = True if 'datetime5_years_ago' in kwargs.keys(): datetime5_years_ago = kwargs['datetime5_years_ago'] else: date5_years_ago = cu.doubledate_shift(date_to, 5 * 365) datetime5_years_ago = cu.convert_doubledate_2datetime(date5_years_ago) if 'num_days_back_4intraday' in kwargs.keys(): num_days_back_4intraday = kwargs['num_days_back_4intraday'] else: num_days_back_4intraday = 10 contract_multiplier_list = [ cmi.contract_multiplier[x] for x in ticker_head_list ] aligned_output = opUtil.get_aligned_futures_data( contract_list=ticker_list, tr_dte_list=tr_dte_list, aggregation_method=aggregation_method, contracts_back=contracts_back, date_to=date_to, futures_data_dictionary=futures_data_dictionary, use_last_as_current=use_last_as_current) aligned_data = aligned_output['aligned_data'] current_data = aligned_output['current_data'] if ticker_head_list in fixed_weight_future_spread_list: weights_output = sutil.get_spread_weights_4contract_list( ticker_head_list=ticker_head_list) spread_weights = weights_output['spread_weights'] portfolio_weights = weights_output['portfolio_weights'] else: regress_output = stats.get_regression_results({ 'x': aligned_data['c2']['change_1'][-60:], 'y': aligned_data['c1']['change_1'][-60:] }) spread_weights = [1, -regress_output['beta']] portfolio_weights = [ 1, -regress_output['beta'] * contract_multiplier_list[0] / contract_multiplier_list[1] ] aligned_data['spread'] = 0 aligned_data['spread_pnl_1'] = 0 aligned_data['spread_pnl1'] = 0 spread_settle = 0 last5_years_indx = aligned_data['settle_date'] >= datetime5_years_ago num_contracts = len(ticker_list) for i in range(num_contracts): aligned_data['spread'] = aligned_data['spread'] + aligned_data[ 'c' + str(i + 1)]['close_price'] * spread_weights[i] spread_settle = spread_settle + current_data[ 'c' + str(i + 1)]['close_price'] * spread_weights[i] aligned_data[ 'spread_pnl_1'] = aligned_data['spread_pnl_1'] + aligned_data[ 'c' + str(i + 1)]['change_1'] * portfolio_weights[ i] * contract_multiplier_list[i] aligned_data[ 'spread_pnl1'] = aligned_data['spread_pnl1'] + aligned_data[ 'c' + str(i + 1)]['change1_instant'] * portfolio_weights[ i] * contract_multiplier_list[i] aligned_data['spread_normalized'] = aligned_data['spread'] / aligned_data[ 'c1']['close_price'] data_last5_years = aligned_data[last5_years_indx] percentile_vector = stats.get_number_from_quantile( y=data_last5_years['spread_pnl_1'].values, quantile_list=[1, 15, 85, 99], clean_num_obs=max(100, round(3 * len(data_last5_years.index) / 4))) downside = (percentile_vector[0] + percentile_vector[1]) / 2 upside = (percentile_vector[2] + percentile_vector[3]) / 2 date_list = [ exp.doubledate_shift_bus_days(double_date=date_to, shift_in_days=x) for x in reversed(range(1, num_days_back_4intraday)) ] date_list.append(date_to) intraday_data = opUtil.get_aligned_futures_data_intraday( contract_list=ticker_list, date_list=date_list) if len(intraday_data.index) == 0: return { 'downside': downside, 'upside': upside, 'intraday_data': intraday_data, 'trading_data': intraday_data, 'spread_weight': spread_weights[1], 'portfolio_weight': portfolio_weights[1], 'z': np.nan, 'recent_trend': np.nan, 'intraday_mean10': np.nan, 'intraday_std10': np.nan, 'intraday_mean5': np.nan, 'intraday_std5': np.nan, 'intraday_mean2': np.nan, 'intraday_std2': np.nan, 'intraday_mean1': np.nan, 'intraday_std1': np.nan, 'aligned_output': aligned_output, 'spread_settle': spread_settle, 'data_last5_years': data_last5_years, 'ma_spread_lowL': np.nan, 'ma_spread_highL': np.nan, 'ma_spread_low': np.nan, 'ma_spread_high': np.nan, 'intraday_sharp': np.nan } intraday_data['time_stamp'] = [ x.to_datetime() for x in intraday_data.index ] intraday_data['settle_date'] = intraday_data['time_stamp'].apply( lambda x: x.date()) end_hour = min([cmi.last_trade_hour_minute[x] for x in ticker_head_list]) start_hour = max( [cmi.first_trade_hour_minute[x] for x in ticker_head_list]) trade_start_hour = dt.time(9, 30, 0, 0) if 'Ag' in ticker_class_list: start_hour1 = dt.time(0, 45, 0, 0) end_hour1 = dt.time(7, 45, 0, 0) selection_indx = [ x for x in range(len(intraday_data.index)) if ((intraday_data['time_stamp'].iloc[x].time() < end_hour1) and (intraday_data['time_stamp'].iloc[x].time() >= start_hour1)) or ((intraday_data['time_stamp'].iloc[x].time() < end_hour) and (intraday_data['time_stamp'].iloc[x].time() >= start_hour)) ] else: selection_indx = [ x for x in range(len(intraday_data.index)) if (intraday_data.index[x].to_datetime().time() < end_hour) and ( intraday_data.index[x].to_datetime().time() >= start_hour) ] intraday_data = intraday_data.iloc[selection_indx] intraday_data['spread'] = 0 for i in range(num_contracts): intraday_data[ 'c' + str(i + 1), 'mid_p'] = (intraday_data['c' + str(i + 1)]['best_bid_p'] + intraday_data['c' + str(i + 1)]['best_ask_p']) / 2 intraday_data['spread'] = intraday_data['spread'] + intraday_data[ 'c' + str(i + 1)]['mid_p'] * spread_weights[i] unique_settle_dates = intraday_data['settle_date'].unique() intraday_data['spread1'] = np.nan for i in range(len(unique_settle_dates) - 1): if (intraday_data['settle_date'] == unique_settle_dates[i]).sum() == \ (intraday_data['settle_date'] == unique_settle_dates[i+1]).sum(): intraday_data.loc[intraday_data['settle_date'] == unique_settle_dates[i],'spread1'] = \ intraday_data['spread'][intraday_data['settle_date'] == unique_settle_dates[i+1]].values intraday_data = intraday_data[intraday_data['settle_date'].notnull()] intraday_mean10 = intraday_data['spread'].mean() intraday_std10 = intraday_data['spread'].std() intraday_data_last5days = intraday_data[ intraday_data['settle_date'] >= cu.convert_doubledate_2datetime( date_list[-5]).date()] intraday_data_last2days = intraday_data[ intraday_data['settle_date'] >= cu.convert_doubledate_2datetime( date_list[-2]).date()] intraday_data_yesterday = intraday_data[intraday_data['settle_date'] == cu.convert_doubledate_2datetime( date_list[-1]).date()] intraday_mean5 = intraday_data_last5days['spread'].mean() intraday_std5 = intraday_data_last5days['spread'].std() intraday_mean2 = intraday_data_last2days['spread'].mean() intraday_std2 = intraday_data_last2days['spread'].std() intraday_mean1 = intraday_data_yesterday['spread'].mean() intraday_std1 = intraday_data_yesterday['spread'].std() intraday_z = (spread_settle - intraday_mean5) / intraday_std5 num_obs_intraday = len(intraday_data.index) num_obs_intraday_half = round(num_obs_intraday / 2) intraday_tail = intraday_data.tail(num_obs_intraday_half) num_positives = sum( intraday_tail['spread'] > intraday_data['spread'].mean()) num_negatives = sum( intraday_tail['spread'] < intraday_data['spread'].mean()) if num_positives + num_negatives != 0: recent_trend = 100 * (num_positives - num_negatives) / (num_positives + num_negatives) else: recent_trend = np.nan intraday_data_shifted = intraday_data.groupby('settle_date').shift(-60) intraday_data['spread_shifted'] = intraday_data_shifted['spread'] intraday_data[ 'delta60'] = intraday_data['spread_shifted'] - intraday_data['spread'] intraday_data['ewma10'] = pd.ewma(intraday_data['spread'], span=10) intraday_data['ewma50'] = pd.ewma(intraday_data['spread'], span=50) intraday_data['ewma200'] = pd.ewma(intraday_data['spread'], span=200) intraday_data['ma40'] = pd.rolling_mean(intraday_data['spread'], 40) intraday_data[ 'ewma50_spread'] = intraday_data['spread'] - intraday_data['ewma50'] intraday_data[ 'ma40_spread'] = intraday_data['spread'] - intraday_data['ma40'] selection_indx = [ x for x in range(len(intraday_data.index)) if (intraday_data['time_stamp'].iloc[x].time() > trade_start_hour) ] selected_data = intraday_data.iloc[selection_indx] selected_data['delta60Net'] = (contract_multiplier_list[0] * selected_data['delta60'] / spread_weights[0]) selected_data.reset_index(drop=True, inplace=True) selected_data['proxy_pnl'] = 0 t_cost = cmi.t_cost[ticker_head_list[0]] ma_spread_low = np.nan ma_spread_high = np.nan ma_spread_lowL = np.nan ma_spread_highL = np.nan intraday_sharp = np.nan if sum(selected_data['ma40_spread'].notnull()) > 30: quantile_list = selected_data['ma40_spread'].quantile([0.1, 0.9]) down_indx = selected_data['ma40_spread'] < quantile_list[0.1] up_indx = selected_data['ma40_spread'] > quantile_list[0.9] up_data = selected_data[up_indx] down_data = selected_data[down_indx] ma_spread_lowL = quantile_list[0.1] ma_spread_highL = quantile_list[0.9] #return {'selected_data':selected_data,'up_data':up_data,'up_indx':up_indx} selected_data.loc[up_indx, 'proxy_pnl'] = (-up_data['delta60Net'] - 2 * num_contracts * t_cost).values selected_data.loc[down_indx, 'proxy_pnl'] = (down_data['delta60Net'] - 2 * num_contracts * t_cost).values short_term_data = selected_data[ selected_data['settle_date'] >= cu.convert_doubledate_2datetime( date_list[-5]).date()] if sum(short_term_data['ma40_spread'].notnull()) > 30: quantile_list = short_term_data['ma40_spread'].quantile([0.1, 0.9]) ma_spread_low = quantile_list[0.1] ma_spread_high = quantile_list[0.9] if selected_data['proxy_pnl'].std() != 0: intraday_sharp = selected_data['proxy_pnl'].mean( ) / selected_data['proxy_pnl'].std() return { 'downside': downside, 'upside': upside, 'intraday_data': intraday_data, 'trading_data': selected_data, 'spread_weight': spread_weights[1], 'portfolio_weight': portfolio_weights[1], 'z': intraday_z, 'recent_trend': recent_trend, 'intraday_mean10': intraday_mean10, 'intraday_std10': intraday_std10, 'intraday_mean5': intraday_mean5, 'intraday_std5': intraday_std5, 'intraday_mean2': intraday_mean2, 'intraday_std2': intraday_std2, 'intraday_mean1': intraday_mean1, 'intraday_std1': intraday_std1, 'aligned_output': aligned_output, 'spread_settle': spread_settle, 'data_last5_years': data_last5_years, 'ma_spread_lowL': ma_spread_lowL, 'ma_spread_highL': ma_spread_highL, 'ma_spread_low': ma_spread_low, 'ma_spread_high': ma_spread_high, 'intraday_sharp': intraday_sharp }
def get_aligned_option_indicators(**kwargs): ticker_list = kwargs['ticker_list'] settle_datetime = cu.convert_doubledate_2datetime(kwargs['settle_date']) #print(ticker_list) if 'num_cal_days_back' in kwargs.keys(): num_cal_days_back = kwargs['num_cal_days_back'] else: num_cal_days_back = 20*365 settle_datetime_from = settle_datetime-dt.timedelta(num_cal_days_back) contract_specs_output_list = [cmi.get_contract_specs(x) for x in ticker_list] ticker_head_list = [x['ticker_head'] for x in contract_specs_output_list] contract_multiplier_list = [cmi.contract_multiplier[x['ticker_head']] for x in contract_specs_output_list] cont_indx_list = [x['ticker_year']*100+x['ticker_month_num'] for x in contract_specs_output_list] month_seperation_list = [cmi.get_month_seperation_from_cont_indx(x,cont_indx_list[0]) for x in cont_indx_list] if 'option_ticker_indicator_dictionary' in kwargs.keys(): option_ticker_indicator_dictionary = kwargs['option_ticker_indicator_dictionary'] else: con = msu.get_my_sql_connection(**kwargs) unique_ticker_heads = list(set(ticker_head_list)) option_ticker_indicator_dictionary = {x: get_option_ticker_indicators(ticker_head=x, settle_date_to=kwargs['settle_date'], num_cal_days_back=num_cal_days_back, con=con) for x in unique_ticker_heads} if 'con' not in kwargs.keys(): con.close() option_ticker_indicator_dictionary_final = {ticker_list[x]: option_ticker_indicator_dictionary[ticker_head_list[x]] for x in range(len(ticker_list))} max_available_settle_list = [] tr_dte_list = [] cal_dte_list = [] imp_vol_list = [] theta_list = [] close2close_vol20_list = [] volume_list = [] open_interest_list = [] for x in range(len(ticker_list)): ticker_data = option_ticker_indicator_dictionary_final[ticker_list[x]] ticker_data = ticker_data[ticker_data['settle_date'] <= settle_datetime] option_ticker_indicator_dictionary_final[ticker_list[x]] = ticker_data ticker_data = ticker_data[ticker_data['ticker'] == ticker_list[x]] max_available_settle_list.append(ticker_data['settle_date'].iloc[-1]) last_available_settle = min(max_available_settle_list) for x in range(len(ticker_list)): ticker_data = option_ticker_indicator_dictionary_final[ticker_list[x]] ticker_data = ticker_data[(ticker_data['ticker'] == ticker_list[x]) & (ticker_data['settle_date'] == last_available_settle)] tr_dte_list.append(ticker_data['tr_dte'].iloc[0]) cal_dte_list.append(ticker_data['cal_dte'].iloc[0]) imp_vol_list.append(ticker_data['imp_vol'].iloc[0]) theta_list.append(ticker_data['theta'].iloc[0]*contract_multiplier_list[x]) close2close_vol20_list.append(ticker_data['close2close_vol20'].iloc[0]) volume_list.append(ticker_data['volume'].iloc[0]) open_interest_list.append(ticker_data['open_interest'].iloc[0]) current_data = pd.DataFrame.from_items([('ticker',ticker_list), ('tr_dte', tr_dte_list), ('cal_dte', cal_dte_list), ('imp_vol', imp_vol_list), ('theta', theta_list), ('close2close_vol20', close2close_vol20_list), ('volume', volume_list), ('open_interest', open_interest_list)]) current_data['settle_date'] = last_available_settle current_data.set_index('ticker', drop=True, inplace=True) current_data = current_data[['settle_date', 'tr_dte', 'cal_dte', 'imp_vol', 'close2close_vol20', 'theta', 'volume', 'open_interest']] aggregation_method = max([ocu.get_aggregation_method_contracts_back({'ticker_class': x['ticker_class'], 'ticker_head': x['ticker_head']})['aggregation_method'] for x in contract_specs_output_list]) if (current_data['tr_dte'].min() >= 80) and (aggregation_method == 1): aggregation_method = 3 tr_days_half_band_width_selected = ocu.tr_days_half_band_with[aggregation_method] data_frame_list = [] ref_tr_dte_list_list = [] for x in range(len(ticker_list)): ticker_data = option_ticker_indicator_dictionary_final[ticker_list[x]] if ticker_head_list[x] in ['ED', 'E0', 'E2', 'E3', 'E4', 'E5']: model = 'OU' else: model = 'BS' tr_dte_upper_band = current_data['tr_dte'].loc[ticker_list[x]]+tr_days_half_band_width_selected tr_dte_lower_band = current_data['tr_dte'].loc[ticker_list[x]]-tr_days_half_band_width_selected ref_tr_dte_list = [y for y in cmi.aligned_data_tr_dte_list if y <= tr_dte_upper_band and y>=tr_dte_lower_band] if len(ref_tr_dte_list) == 0: return {'hist': [], 'current': [], 'success': False} if aggregation_method == 12: aligned_data = [gop.load_aligend_options_data_file(ticker_head=cmi.aligned_data_tickerhead[ticker_head_list[x]], tr_dte_center=y, contract_month_letter=contract_specs_output_list[x]['ticker_month_str'], model=model) for y in ref_tr_dte_list] ticker_data = ticker_data[ticker_data['ticker_month'] == contract_specs_output_list[x]['ticker_month_num']] else: aligned_data = [gop.load_aligend_options_data_file(ticker_head=cmi.aligned_data_tickerhead[ticker_head_list[x]], tr_dte_center=y, model=model) for y in ref_tr_dte_list] aligned_data = [y[(y['trDTE'] >= current_data['tr_dte'].loc[ticker_list[x]]-tr_days_half_band_width_selected)& (y['trDTE'] <= current_data['tr_dte'].loc[ticker_list[x]]+tr_days_half_band_width_selected)] for y in aligned_data] aligned_data = pd.concat(aligned_data) aligned_data['settle_date'] = pd.to_datetime(aligned_data['settleDates'].astype('str'), format='%Y%m%d') aligned_data.rename(columns={'TickerYear': 'ticker_year', 'TickerMonth': 'ticker_month', 'trDTE': 'tr_dte', 'calDTE': 'cal_dte', 'impVol': 'imp_vol', 'close2CloseVol20': 'close2close_vol20'}, inplace=True) aligned_data.sort(['settle_date', 'ticker_year', 'ticker_month'], ascending=[True,True,True],inplace=True) aligned_data.drop_duplicates(['settle_date','ticker_year','ticker_month'],inplace=True) aligned_data['old_aligned'] = True aligned_data = aligned_data[['settle_date','ticker_month', 'ticker_year', 'cal_dte', 'tr_dte', 'imp_vol', 'close2close_vol20', 'profit5', 'old_aligned']] tr_dte_selection = (ticker_data['tr_dte'] >= current_data['tr_dte'].loc[ticker_list[x]]-tr_days_half_band_width_selected)&\ (ticker_data['tr_dte'] <= current_data['tr_dte'].loc[ticker_list[x]]+tr_days_half_band_width_selected) ticker_data = ticker_data[tr_dte_selection] ticker_data['old_aligned'] = False ticker_data['profit5'] = np.NaN ticker_data = pd.concat([aligned_data, ticker_data[['settle_date', 'ticker_month', 'ticker_year', 'cal_dte', 'tr_dte', 'imp_vol', 'close2close_vol20', 'profit5', 'old_aligned']]]) ticker_data = ticker_data[(ticker_data['settle_date'] <= settle_datetime)&(ticker_data['settle_date'] >= settle_datetime_from)] ticker_data['cont_indx'] = 100*ticker_data['ticker_year']+ticker_data['ticker_month'] ticker_data['cont_indx_adj'] = [cmi.get_cont_indx_from_month_seperation(y,-month_seperation_list[x]) for y in ticker_data['cont_indx']] data_frame_list.append(ticker_data) ref_tr_dte_list_list.append(ref_tr_dte_list) for x in range(len(ticker_list)): data_frame_list[x].set_index(['settle_date','cont_indx_adj'], inplace=True,drop=False) data_frame_list[x]['imp_vol'] = data_frame_list[x]['imp_vol'].astype('float64') merged_dataframe = pd.concat(data_frame_list, axis=1, join='inner',keys=['c'+ str(x+1) for x in range(len(ticker_list))]) merged_dataframe['abs_tr_dte_diff'] = abs(merged_dataframe['c1']['tr_dte']-tr_dte_list[0]) merged_dataframe['settle_date'] = merged_dataframe['c1']['settle_date'] merged_dataframe.sort(['settle_date', 'abs_tr_dte_diff'], ascending=[True,True], inplace=False) merged_dataframe.drop_duplicates('settle_date', inplace=True, take_last=False) merged_dataframe.index = merged_dataframe.index.droplevel(1) return {'hist': merged_dataframe, 'current': current_data, 'success': True}
def get_results_4ticker(**kwargs): ticker = kwargs['ticker'] date_to = kwargs['date_to'] ticker_frame = gfp.get_futures_price_preloaded(ticker=ticker, settle_date_to=date_to) ticker_frame['close_diff'] = ticker_frame['close_price'].diff() daily_sd = np.std(ticker_frame['close_diff'].iloc[-41:-1]) ticker_frame['ma50'] = ticker_frame['close_price'].rolling( window=50, center=False).mean() ticker_frame['ma200'] = ticker_frame['close_price'].rolling( window=200, center=False).mean() if ticker_frame['close_price'].iloc[-2] > ticker_frame['ma50'].iloc[-2]: trend1 = 1 else: trend1 = -1 if ticker_frame['ma50'].iloc[-2] > ticker_frame['ma200'].iloc[-2]: trend2 = 1 else: trend2 = -1 candle_frame = qd.get_continuous_bar_data(ticker=ticker, date_to=date_to, num_days_back=20) ticker_head = cmi.get_contract_specs(ticker)['ticker_head'] candle_frame['ewma300'] = candle_frame['close'].ewm( span=300, min_periods=250, adjust=True, ignore_na=False).mean() candle_frame['ewma50'] = candle_frame['close'].ewm(span=50, min_periods=40, adjust=True, ignore_na=False).mean() candle_frame['ma5'] = candle_frame['close'].rolling(window=5, center=False).mean() candle_frame['ewma300D'] = candle_frame['ewma300'] - candle_frame[ 'ewma300'].shift(60) candle_frame['ewma300DN'] = candle_frame['ewma300D'] / daily_sd candle_frame[ 'ewma50D'] = candle_frame['ewma50'] - candle_frame['ewma50'].shift(10) candle_frame['min14'] = candle_frame['low'].rolling(window=14, center=False).min() candle_frame['max14'] = candle_frame['high'].rolling(window=14, center=False).max() candle_frame['william'] = -100 * ( candle_frame['max14'] - candle_frame['close']) / ( candle_frame['max14'] - candle_frame['min14']) candle_frame['datetime'] = [ x.replace(hour=0, second=0, minute=0) for x in candle_frame.index ] candle_frame['obs_no'] = range(len(candle_frame.index)) candle_frame['bullishW'] = candle_frame['william'] > -20 candle_frame['bearishW'] = candle_frame['william'] < -80 candle_frame['bullishW'] = candle_frame['bullishW'].astype(int) candle_frame['bearishW'] = candle_frame['bearishW'].astype(int) candle_frame = candle_frame[np.isfinite(candle_frame['ewma300D'])] candle_frame['ewma300DS'] = np.sign(candle_frame['ewma300D']) candle_frame['ewma300DSDiff'] = abs(candle_frame['ewma300DS'].diff()) turning_points = candle_frame[candle_frame['ewma300DSDiff'] != 0] turning_points['turning_points'] = turning_points['obs_no'] merged_frame = pd.concat([candle_frame, turning_points['turning_points']], axis=1) merged_frame['turning_points'].iloc[0] = 0 merged_frame['turning_points'] = merged_frame['turning_points'].fillna( method='ffill') merged_frame[ 'trend_age'] = merged_frame['obs_no'] - merged_frame['turning_points'] merged_frame['bullishWCumsum'] = merged_frame.groupby( 'turning_points')['bullishW'].transform(pd.Series.cumsum) merged_frame['bearishWCumsum'] = merged_frame.groupby( 'turning_points')['bearishW'].transform(pd.Series.cumsum) candle_frame = merged_frame candle_frame = candle_frame.dropna().reset_index(drop=True, inplace=False) date_timeto = cu.convert_doubledate_2datetime(date_to) daily_frame = candle_frame[candle_frame['datetime'] == date_timeto] daily_frame.reset_index(drop=True, inplace=True) tick_size = cmi.tick_size[ticker_head] latest_trade_exit_hour_minute = get_latest_trade_exit_hour_minute( ticker_head) current_position = 0 direction_list = [] entry_price_list = [] exit_price_list = [] entry_index_list = [] exit_index_list = [] entry_hour_minute_list = [] stop_price_list = [] target_price_list = [] ewma300DN_list = [] trend_age_list = [] bullishWCumsum_list = [] bearishWCumsum_list = [] stop_adjustment_possible_Q = False long_trade_possible_Q = False short_trade_possible_Q = False breakout_price = np.nan for i in range(2, len(daily_frame.index)): if (daily_frame['ewma300D'].iloc[i] < 0) | (daily_frame['william'].iloc[i] <= -80): long_trade_possible_Q = False if (daily_frame['ewma300D'].iloc[i] > 0) | (daily_frame['william'].iloc[i] >= -20): short_trade_possible_Q = False if long_trade_possible_Q and (daily_frame['high'].iloc[i] > breakout_price + tick_size) \ and (daily_frame['hour_minute'].iloc[i] <= 1100): long_trade_possible_Q = False current_position = 1 direction_list.append(current_position) ewma300DN_list.append(daily_frame['ewma300DN'].iloc[i - 1]) trend_age_list.append(daily_frame['trend_age'].iloc[i - 1]) bullishWCumsum_list.append(daily_frame['bullishWCumsum'].iloc[i - 1]) bearishWCumsum_list.append(daily_frame['bearishWCumsum'].iloc[i - 1]) entry_index_list.append(i) entry_hour_minute_list.append(daily_frame['hour_minute'].iloc[i]) entry_price = breakout_price + 1.5 * tick_size entry_price_list.append(entry_price) stop_price_list.append(swing_low - tick_size) target_price_list.append(2 * entry_price - swing_low) breakout_price = np.nan continue if (current_position == 0) and (daily_frame['ewma300D'].iloc[i] > 0) and (daily_frame['william'].iloc[i - 1] <= -80) \ and (daily_frame['william'].iloc[i] > -80) and (daily_frame['hour_minute'].iloc[i] <= 1100) and (daily_frame['hour_minute'].iloc[i] >= 830): long_trade_possible_Q = True breakout_price = daily_frame['high'].iloc[i] for j in range(len(daily_frame.index)): if j == 0: swing_low = daily_frame['low'].iloc[i - 1] elif daily_frame['low'].iloc[i - 1 - j] <= swing_low: swing_low = daily_frame['low'].iloc[i - 1 - j] elif daily_frame['low'].iloc[i - 1 - j] > swing_low: break if short_trade_possible_Q and (daily_frame['low'].iloc[i] < breakout_price - tick_size) \ and (daily_frame['hour_minute'].iloc[i] <= 1100): short_trade_possible_Q = False current_position = -1 direction_list.append(current_position) ewma300DN_list.append(daily_frame['ewma300DN'].iloc[i - 1]) trend_age_list.append(daily_frame['trend_age'].iloc[i - 1]) bullishWCumsum_list.append(daily_frame['bullishWCumsum'].iloc[i - 1]) bearishWCumsum_list.append(daily_frame['bearishWCumsum'].iloc[i - 1]) entry_index_list.append(i) entry_hour_minute_list.append(daily_frame['hour_minute'].iloc[i]) entry_price = breakout_price - 1.5 * tick_size entry_price_list.append(entry_price) stop_price_list.append(swing_high + tick_size) target_price_list.append(2 * entry_price - swing_high) breakout_price = np.nan continue if (current_position == 0) and (daily_frame['ewma300D'].iloc[i] < 0) and (daily_frame['william'].iloc[i - 1] >= -20) \ and (daily_frame['william'].iloc[i] < -20) and (daily_frame['hour_minute'].iloc[i] <= 1100) and (daily_frame['hour_minute'].iloc[i] >= 830): short_trade_possible_Q = True breakout_price = daily_frame['high'].iloc[i] for j in range(len(daily_frame.index)): if j == 0: swing_high = daily_frame['high'].iloc[i - 1] elif daily_frame['high'].iloc[i - 1 - j] >= swing_high: swing_high = daily_frame['high'].iloc[i - 1 - j] elif daily_frame['high'].iloc[i - 1 - j] < swing_high: break if (current_position > 0) and stop_adjustment_possible_Q and ( daily_frame['close'].iloc[i-2] < daily_frame['ma5'].iloc[i-2]) \ and (daily_frame['close'].iloc[i-1] < daily_frame['ma5'].iloc[i-1]): stop_price_list[-1] = min(daily_frame['low'].iloc[i - 2], daily_frame['low'].iloc[i - 1]) if (current_position < 0) and stop_adjustment_possible_Q and ( daily_frame['close'].iloc[i-2] > daily_frame['ma5'].iloc[i-2]) \ and (daily_frame['close'].iloc[i-1] > daily_frame['ma5'].iloc[i-1]): stop_price_list[-1] = max(daily_frame['high'].iloc[i - 2], daily_frame['high'].iloc[i - 1]) if (current_position > 0) and ((daily_frame['hour_minute'].iloc[i] >= latest_trade_exit_hour_minute) | (i == len(daily_frame.index) - 1)): current_position = 0 exit_price_list.append(daily_frame['open'].iloc[i] - 0.5 * tick_size) exit_index_list.append(i) stop_adjustment_possible_Q = False if (current_position < 0) and ((daily_frame['hour_minute'].iloc[i] >= latest_trade_exit_hour_minute) | (i == len(daily_frame.index) - 1)): current_position = 0 exit_price_list.append(daily_frame['open'].iloc[i] + 0.5 * tick_size) exit_index_list.append(i) stop_adjustment_possible_Q = False if (current_position > 0) and (daily_frame['low'].iloc[i] <= stop_price_list[-1]): current_position = 0 exit_price_list.append(stop_price_list[-1] - 0.5 * tick_size) exit_index_list.append(i) stop_adjustment_possible_Q = False if (current_position < 0) and (daily_frame['high'].iloc[i] >= stop_price_list[-1]): current_position = 0 exit_price_list.append(stop_price_list[-1] + 0.5 * tick_size) exit_index_list.append(i) stop_adjustment_possible_Q = False if (current_position > 0) and (daily_frame['high'].iloc[i] >= target_price_list[-1]): stop_adjustment_possible_Q = True if (current_position < 0) and (daily_frame['low'].iloc[i] <= target_price_list[-1]): stop_adjustment_possible_Q = True trades_frame = pd.DataFrame.from_items([ ('direction', direction_list), ('entry_price', entry_price_list), ('exit_price', exit_price_list), ('entry_index', entry_index_list), ('exit_index', exit_index_list), ('entry_hour_minute', entry_hour_minute_list), ('target_price', target_price_list), ('ewma300DN', ewma300DN_list), ('trend_age', trend_age_list), ('bullishWCumsum', bullishWCumsum_list), ('bearishWCumsum', bearishWCumsum_list) ]) trades_frame['pnl'] = ( trades_frame['exit_price'] - trades_frame['entry_price']) * trades_frame['direction'] trades_frame['pnl_dollar'] = cmi.contract_multiplier[ ticker_head] * trades_frame['pnl'] trades_frame['stop_loss'] = abs(trades_frame['entry_price'] - trades_frame['target_price']) trades_frame['daily_sd'] = daily_sd trades_frame['normalized_stop_loss'] = trades_frame['stop_loss'] / daily_sd trades_frame['pnl_normalized'] = trades_frame['pnl'] / daily_sd trades_frame['ticker_head'] = ticker_head trades_frame['ticker'] = ticker trades_frame['trend1'] = trend1 trades_frame['trend2'] = trend2 trades_frame['trade_date'] = date_to return {'trades_frame': trades_frame, 'daily_frame': daily_frame}
def get_aligned_option_indicators_legacy(**kwargs): ticker_list = kwargs['ticker_list'] tr_dte_list = kwargs['tr_dte_list'] settle_datetime = cu.convert_doubledate_2datetime(kwargs['settle_date']) if 'num_cal_days_back' in kwargs.keys(): num_cal_days_back = kwargs['num_cal_days_back'] else: num_cal_days_back = 20*365 settle_datetime_from = settle_datetime-dt.timedelta(num_cal_days_back) contract_specs_output_list = [cmi.get_contract_specs(x) for x in ticker_list] ticker_head_list = [x['ticker_head'] for x in contract_specs_output_list] cont_indx_list = [x['ticker_year']*100+x['ticker_month_num'] for x in contract_specs_output_list] month_seperation_list = [cmi.get_month_seperation_from_cont_indx(x,cont_indx_list[0]) for x in cont_indx_list] aggregation_method = max([ocu.get_aggregation_method_contracts_back({'ticker_class': x['ticker_class'], 'ticker_head': x['ticker_head']})['aggregation_method'] for x in contract_specs_output_list]) if (min(tr_dte_list) >= 80) and (aggregation_method == 1): aggregation_method = 3 tr_days_half_band_width_selected = ocu.tr_days_half_band_with[aggregation_method] data_frame_list = [] for x in range(len(ticker_list)): if ticker_head_list[x] in ['ED', 'E0', 'E2', 'E3', 'E4', 'E5']: model = 'OU' else: model = 'BS' tr_dte_upper_band = tr_dte_list[x]+tr_days_half_band_width_selected tr_dte_lower_band = tr_dte_list[x]-tr_days_half_band_width_selected ref_tr_dte_list = [y for y in cmi.aligned_data_tr_dte_list if y <= tr_dte_upper_band and y>=tr_dte_lower_band] if len(ref_tr_dte_list) == 0: return {'hist': [], 'current': [], 'success': False} if aggregation_method == 12: aligned_data = [gop.load_aligend_options_data_file(ticker_head=cmi.aligned_data_tickerhead[ticker_head_list[x]], tr_dte_center=y, contract_month_letter=contract_specs_output_list[x]['ticker_month_str'], model=model) for y in ref_tr_dte_list] else: aligned_data = [gop.load_aligend_options_data_file(ticker_head=cmi.aligned_data_tickerhead[ticker_head_list[x]], tr_dte_center=y, model=model) for y in ref_tr_dte_list] aligned_data = [y[(y['trDTE'] >= tr_dte_lower_band)&(y['trDTE'] <= tr_dte_upper_band)] for y in aligned_data] aligned_data = pd.concat(aligned_data) aligned_data.drop('theta', axis=1, inplace=True) aligned_data['settle_date'] = pd.to_datetime(aligned_data['settleDates'].astype('str'), format='%Y%m%d') aligned_data = aligned_data[(aligned_data['settle_date'] <= settle_datetime)&(aligned_data['settle_date'] >= settle_datetime_from)] aligned_data.rename(columns={'TickerYear': 'ticker_year', 'TickerMonth': 'ticker_month', 'trDTE': 'tr_dte', 'calDTE': 'cal_dte', 'impVol': 'imp_vol', 'close2CloseVol20': 'close2close_vol20', 'dollarTheta': 'theta'}, inplace=True) aligned_data.sort(['settle_date', 'ticker_year', 'ticker_month'], ascending=[True,True,True],inplace=True) aligned_data.drop_duplicates(['settle_date','ticker_year','ticker_month'],inplace=True) aligned_data = aligned_data[['settle_date','ticker_month', 'ticker_year', 'cal_dte', 'tr_dte', 'imp_vol', 'theta', 'close2close_vol20', 'profit5']] aligned_data['cont_indx'] = 100*aligned_data['ticker_year']+aligned_data['ticker_month'] aligned_data['cont_indx_adj'] = [cmi.get_cont_indx_from_month_seperation(y,-month_seperation_list[x]) for y in aligned_data['cont_indx']] data_frame_list.append(aligned_data) for x in range(len(ticker_list)): data_frame_list[x].set_index(['settle_date','cont_indx_adj'], inplace=True,drop=False) merged_dataframe = pd.concat(data_frame_list, axis=1, join='inner',keys=['c'+ str(x+1) for x in range(len(ticker_list))]) merged_dataframe['abs_tr_dte_diff'] = abs(merged_dataframe['c1']['tr_dte']-tr_dte_list[0]) merged_dataframe['settle_date'] = merged_dataframe['c1']['settle_date'] merged_dataframe.sort(['settle_date', 'abs_tr_dte_diff'], ascending=[True,True], inplace=False) merged_dataframe.drop_duplicates('settle_date', inplace=True, take_last=False) merged_dataframe.index = merged_dataframe.index.droplevel(1) tr_dte_list = [] cal_dte_list = [] imp_vol_list = [] theta_list = [] close2close_vol20_list = [] for x in range(len(ticker_list)): selected_data = merged_dataframe['c' + str(x+1)] if settle_datetime in selected_data.index: selected_data = selected_data.loc[settle_datetime] else: return {'hist': [], 'current': [], 'success': False} if selected_data['cont_indx'] != cont_indx_list[x]: return {'hist': [], 'current': [], 'success': False} tr_dte_list.append(selected_data['tr_dte']) cal_dte_list.append(selected_data['cal_dte']) imp_vol_list.append(selected_data['imp_vol']) theta_list.append(selected_data['theta']) close2close_vol20_list.append(selected_data['close2close_vol20']) current_data = pd.DataFrame.from_items([('ticker',ticker_list), ('tr_dte', tr_dte_list), ('cal_dte', cal_dte_list), ('imp_vol', imp_vol_list), ('theta', theta_list), ('close2close_vol20', close2close_vol20_list)]) current_data['settle_date'] = settle_datetime current_data.set_index('ticker', drop=True, inplace=True) return {'hist': merged_dataframe, 'current': current_data, 'success': True}