def get_contract_list_4year_range(**kwargs): now = datetime.datetime.utcnow() start_year = kwargs['start_year'] end_year = kwargs['end_year'] if 'tickerhead_list' in kwargs.keys(): tickerhead_list = kwargs['tickerhead_list'] else: tickerhead_list = cmi.cme_futures_tickerhead_list futures_contract_months = cmi.futures_contract_months contract_name_dict = cmi.contract_name ticker_class_dict = cmi.ticker_class year_list = range(start_year,end_year) ticker_list = [] for i in tickerhead_list: contract_months = futures_contract_months[i] for j in contract_months: for k in year_list: ticker = i+j+str(k) ticker_list.append((ticker,i,k,cmi.letter_month_string.find(j)+1,exp.get_futures_expiration(ticker), 'futures',contract_name_dict[i],ticker_class_dict[i],'USD',now,now)) return ticker_list
def get_contract_list_4year_range(**kwargs): now = datetime.datetime.utcnow() start_year = kwargs["start_year"] end_year = kwargs["end_year"] if "tickerhead_list" in kwargs.keys(): tickerhead_list = kwargs["tickerhead_list"] else: tickerhead_list = cmi.cme_futures_tickerhead_list futures_contract_months = cmi.futures_contract_months contract_name_dict = cmi.contract_name ticker_class_dict = cmi.ticker_class year_list = range(start_year, end_year) ticker_list = [] for i in tickerhead_list: contract_months = futures_contract_months[i] for j in contract_months: for k in year_list: ticker = i + j + str(k) ticker_list.append( ( ticker, i, k, cmi.letter_month_string.find(j) + 1, exp.get_futures_expiration(ticker), "futures", contract_name_dict[i], ticker_class_dict[i], "USD", now, now, ) ) return ticker_list
def get_simple_rate(**kwargs): as_of_date = kwargs['as_of_date'] date_to = kwargs['date_to'] if 'date_from' in kwargs.keys(): date_from = kwargs['date_from'] else: date_from = as_of_date if 'ticker_head' in kwargs.keys(): ticker_head = kwargs['ticker_head'] else: ticker_head = 'ED' ta_output_dir = dn.get_dated_directory_extension(folder_date=as_of_date, ext='ta') file_name = ta_output_dir + '/' + ticker_head + '_interest_curve.pkl' #print('as_of_date: ' + str(as_of_date) + ', date_to: ' + str(date_to)) if os.path.isfile(file_name): price_frame = pd.read_pickle(file_name) if (not os.path.isfile(file_name)) or price_frame.empty: price_frame = gfp.get_futures_price_preloaded(ticker_head=ticker_head, settle_date=as_of_date) price_frame = price_frame[price_frame['close_price'].notnull()] price_frame.sort_values('tr_dte', ascending=True, inplace=True) price_frame['exp_date'] = [ exp.get_futures_expiration(x) for x in price_frame['ticker'] ] price_frame['implied_rate'] = 100 - price_frame['close_price'] price_frame.to_pickle(file_name) if price_frame.empty: return { 'rate_output': np.NaN, 'price_frame': pd.DataFrame( columns=['ticker', 'cal_dte', 'exp_date', 'implied_rate']) } datetime_to = cu.convert_doubledate_2datetime(date_to) datetime_from = cu.convert_doubledate_2datetime(date_from) price_frame_first = price_frame[price_frame['exp_date'] <= datetime_from] price_frame_middle = price_frame[(price_frame['exp_date'] > datetime_from) & (price_frame['exp_date'] < datetime_to)] if price_frame_middle.empty: if not price_frame_first.empty: rate_output = price_frame_first['implied_rate'].iloc[-1] / 100 else: rate_output = price_frame['implied_rate'].iloc[0] / 100 return { 'rate_output': rate_output, 'price_frame': price_frame[['ticker', 'cal_dte', 'exp_date', 'implied_rate']] } if price_frame_first.empty: first_rate = price_frame_middle['implied_rate'].iloc[0] first_period = ( price_frame_middle['exp_date'].iloc[0].to_pydatetime() - datetime_from).days else: first_rate = price_frame_first['implied_rate'].iloc[-1] first_period = ( price_frame_middle['exp_date'].iloc[0].to_pydatetime() - datetime_from).days last_rate = price_frame_middle['implied_rate'].iloc[-1] last_period = ( datetime_to - price_frame_middle['exp_date'].iloc[-1].to_pydatetime()).days middle_discount = [ 1 + (price_frame_middle['implied_rate'].iloc[x] * (price_frame_middle['cal_dte'].iloc[x + 1] - price_frame_middle['cal_dte'].iloc[x]) / 36500) for x in range(len(price_frame_middle.index) - 1) ] total_discount = np.prod( np.array(middle_discount)) * (1 + (first_rate * first_period / 36500)) * ( 1 + (last_rate * last_period / 36500)) total_period = ( price_frame_middle['cal_dte'].iloc[-1] - price_frame_middle['cal_dte'].iloc[0]) + first_period + last_period rate_output = (total_discount - 1) * 365 / total_period return { 'rate_output': rate_output, 'price_frame': price_frame[['ticker', 'cal_dte', 'exp_date', 'implied_rate']] }
def get_simple_rate(**kwargs): as_of_date = kwargs["as_of_date"] date_to = kwargs["date_to"] if "date_from" in kwargs.keys(): date_from = kwargs["date_from"] else: date_from = as_of_date if "ticker_head" in kwargs.keys(): ticker_head = kwargs["ticker_head"] else: ticker_head = "ED" ta_output_dir = dn.get_dated_directory_extension(folder_date=as_of_date, ext="ta") file_name = ta_output_dir + "/" + ticker_head + "_interest_curve.pkl" # print('as_of_date: ' + str(as_of_date) + ', date_to: ' + str(date_to)) if os.path.isfile(file_name): price_frame = pd.read_pickle(file_name) if (not os.path.isfile(file_name)) or price_frame.empty: price_frame = gfp.get_futures_price_preloaded(ticker_head=ticker_head, settle_date=as_of_date) price_frame = price_frame[price_frame["close_price"].notnull()] price_frame.sort("tr_dte", ascending=True, inplace=True) price_frame["exp_date"] = [exp.get_futures_expiration(x) for x in price_frame["ticker"]] price_frame["implied_rate"] = 100 - price_frame["close_price"] price_frame.to_pickle(file_name) if price_frame.empty: return { "rate_output": np.NaN, "price_frame": pd.DataFrame(columns=["ticker", "cal_dte", "exp_date", "implied_rate"]), } datetime_to = cu.convert_doubledate_2datetime(date_to) datetime_from = cu.convert_doubledate_2datetime(date_from) price_frame_first = price_frame[price_frame["exp_date"] <= datetime_from] price_frame_middle = price_frame[ (price_frame["exp_date"] > datetime_from) & (price_frame["exp_date"] < datetime_to) ] if price_frame_middle.empty: if not price_frame_first.empty: rate_output = price_frame_first["implied_rate"].iloc[-1] / 100 else: rate_output = price_frame["implied_rate"].iloc[0] / 100 return { "rate_output": rate_output, "price_frame": price_frame[["ticker", "cal_dte", "exp_date", "implied_rate"]], } if price_frame_first.empty: first_rate = price_frame_middle["implied_rate"].iloc[0] first_period = (price_frame_middle["exp_date"].iloc[0].to_datetime() - datetime_from).days else: first_rate = price_frame_first["implied_rate"].iloc[-1] first_period = (price_frame_middle["exp_date"].iloc[0].to_datetime() - datetime_from).days last_rate = price_frame_middle["implied_rate"].iloc[-1] last_period = (datetime_to - price_frame_middle["exp_date"].iloc[-1].to_datetime()).days middle_discount = [ 1 + ( price_frame_middle["implied_rate"].iloc[x] * (price_frame_middle["cal_dte"].iloc[x + 1] - price_frame_middle["cal_dte"].iloc[x]) / 36500 ) for x in range(len(price_frame_middle.index) - 1) ] total_discount = ( np.prod(np.array(middle_discount)) * (1 + (first_rate * first_period / 36500)) * (1 + (last_rate * last_period / 36500)) ) total_period = ( (price_frame_middle["cal_dte"].iloc[-1] - price_frame_middle["cal_dte"].iloc[0]) + first_period + last_period ) rate_output = (total_discount - 1) * 365 / total_period return {"rate_output": rate_output, "price_frame": price_frame[["ticker", "cal_dte", "exp_date", "implied_rate"]]}
('SIJ2016', pd.datetime(2016, 3, 28)), ('SIK2016', pd.datetime(2016, 4, 26)), ('SIM2016', pd.datetime(2016, 5, 25)), ('SIN2016', pd.datetime(2016, 6, 27)), ('SIQ2016', pd.datetime(2016, 7, 26)), ('SIU2016', pd.datetime(2016, 8, 25)), ('SIZ2016', pd.datetime(2016, 11, 22)), ('SIN2017', pd.datetime(2017, 6, 27)), ('SIZ2017', pd.datetime(2017, 11, 27)), ('SIN2018', pd.datetime(2018, 6, 26)), ('SIZ2018', pd.datetime(2018, 11, 27)), ('SIN2019', pd.datetime(2019, 6, 25)), ('SIZ2019', pd.datetime(2019, 11, 25)), ('SIN2020', pd.datetime(2020, 6, 25)), ('SIZ2020', pd.datetime(2020, 11, 24))) success_indx = [exp.get_futures_expiration(expiration_test[i][0]) == expiration_test[i][1] for i in range(len(expiration_test))] #success_indx = [exp.get_options_expiration(option_expiration_test[i][0]) == option_expiration_test[i][1] for i in range(len(option_expiration_test))] failed = [expiration_test[i] for i in range(len(expiration_test)) if not success_indx[i]] #failed = [option_expiration_test[i] for i in range(len(option_expiration_test)) if not success_indx[i]] if not failed: print('TEST PASSED!') else: print('TEST FAILED:') for i in failed: print(i)