def test_famafrench_test(self): self.load_data(start_date="2000-01-01") try: quandl.ApiConfig.api_key = 'Z2WYzGME3qmxnqQgBcdX' ust = quandl.get("USTREASURY/YIELD", start_date="2000-01-01") f = open('../writeup/tables/FFRegressionTables.tex', 'a') for co in TestGenericFuture.co_tickers.keys( ): #["Corn", "Brent", "Lumber", "Copper", "Gold", "Wheat"]: try: commodity = co expiries = pd.read_excel("../data/" + commodity + "ContractExpiries.xlsx") root_ticker = TestGenericFuture.co_tickers[commodity] generic_futures = [1, 2] tickers = [root_ticker + str(i) for i in generic_futures] near_future_data = self.data.loc[:, tickers[0]] far_future_data = self.data.loc[:, tickers[1]] near_future_ticker = tickers[0] far_future_ticker = tickers[1] near_future = generic_futures[0] far_future = generic_futures[1] near_future_expiries = expiries[[ "Date", str(near_future) + "C_Expiry" ]].set_index("Date") far_future_expiries = expiries[[ "Date", str(far_future) + "C_Expiry" ]].set_index("Date") near_future = GenericFuture(ticker=near_future_ticker, number=near_future, data=near_future_data) far_future = GenericFuture(ticker=far_future_ticker, number=far_future, data=far_future_data) near_future.clean_data() far_future.clean_data() near_future.set_expiries(near_future_expiries) far_future.set_expiries(far_future_expiries) fit = near_future.famafrench_test(far_future, ust["3 MO"] / 100, log=False, frequency='M') print(commodity) print(fit.summary()) f.write(fit.summary().as_latex()) f.write('''\newline''') except KeyError: print("Error in " + co) pass f.close() except Exception as e: self.fail(e)
def test_calculate_return(self): self.load_data() try: future = GenericFuture(ticker="B", number=1) tickers = self.data.columns[1:] data = self.data.iloc[:, 0] ticker = tickers[1] number = 1 future = GenericFuture(ticker=ticker, number=number, data=data) future.clean_data() future.to_monthly() future.calculate_return() future.returns().plot() plt.show() except Exception as e: self.fail()
def test_clean_data(self): self.load_data() try: future = GenericFuture(ticker="B", number=1) tickers = self.data.columns[1:] data = self.data.iloc[:, 0] ticker = tickers[1] number = 1 future = GenericFuture(ticker=ticker, number=number, data=data) future.clean_data() if future.missing_count() == 0: self.fail() print("Missing Data: " + str(future.missing_count())) future.plot() plt.show() except Exception as e: self.fail()
def test_calculate_basis(self): self.load_data(start_date="2000-01-01") try: all_basis = [] for co in TestGenericFuture.co_tickers.keys(): try: commodity = co expiries = pd.read_excel("../data/" + commodity + "ContractExpiries.xlsx") root_ticker = TestGenericFuture.co_tickers[commodity] generic_futures = [1, 2] tickers = [root_ticker + str(i) for i in generic_futures] near_future_data = self.data.loc[:, tickers[0]] far_future_data = self.data.loc[:, tickers[1]] near_future_ticker = tickers[0] far_future_ticker = tickers[1] near_future = generic_futures[0] far_future = generic_futures[1] near_future_expiries = expiries[[ "Date", str(near_future) + "C_Expiry" ]].set_index("Date") far_future_expiries = expiries[[ "Date", str(far_future) + "C_Expiry" ]].set_index("Date") near_future = GenericFuture(ticker=near_future_ticker, number=near_future, data=near_future_data) far_future = GenericFuture(ticker=far_future_ticker, number=far_future, data=far_future_data) near_future.clean_data() far_future.clean_data() near_future.set_expiries(near_future_expiries) far_future.set_expiries(far_future_expiries) basis = near_future.calculate_basis(far_future, log=True, ffill=True) all_basis.append(basis) except KeyError: pass all_basis = pd.concat(all_basis, axis=1) all_basis.to_excel("../data/AllBasisLog.xlsx") all_basis = all_basis.resample('M', convention='end').last() summary_description = all_basis.describe().round(2).to_latex() f = open('../writeup/tables/SummaryStatistics.tex', 'w') f.write(summary_description) f.close() sns.set(rc={"grid.linewidth": 0.6, "figure.figsize": (11.7, 8.27)}) sns.set_style(style='whitegrid') ax = sns.boxplot(data=all_basis) ax.set_xticklabels(ax.get_xticklabels(), rotation=45) ax.get_figure().savefig("../writeup/img/BasisBoxPlot.png") #from statsmodels.graphics.tsaplots import plot_acf, plot_pacf #plot_acf(nb.values.squeeze(), lags=40) # basis.plot() #lt.show() #plot_pacf(near_future.series().values.squeeze(), lags=40) #plt.show() except Exception as e: self.fail(e)
def main(**kwargs): data = load_data(start_date="2000-01-01") quandl.ApiConfig.api_key = 'Z2WYzGME3qmxnqQgBcdX' ust = quandl.get("USTREASURY/YIELD", start_date="2000-01-01") all_basis = list() f = open('../tex/FFRegressionTables.tex', 'a') for co in co_tickers.keys(): try: commodity = co expiries = pd.read_excel("../data/" + commodity + "ContractExpiries.xlsx") root_ticker = co_tickers[commodity] generic_futures = [1, 2] tickers = [root_ticker + str(i) for i in generic_futures] near_future_data = data.loc[:, tickers[0]] far_future_data = data.loc[:, tickers[1]] near_future_ticker = tickers[0] far_future_ticker = tickers[1] near_future = generic_futures[0] far_future = generic_futures[1] near_future_expiries = expiries[[ "Date", str(near_future) + "C_Expiry" ]].set_index("Date") far_future_expiries = expiries[[ "Date", str(far_future) + "C_Expiry" ]].set_index("Date") near_future = GenericFuture(ticker=near_future_ticker, number=near_future, data=near_future_data) far_future = GenericFuture(ticker=far_future_ticker, number=far_future, data=far_future_data) near_future.clean_data() far_future.clean_data() near_future.set_expiries(near_future_expiries) far_future.set_expiries(far_future_expiries) basis = near_future.calculate_basis(far_future) all_basis.append(basis) fit = near_future.famafrench_test(far_future, ust["3 MO"] / 100, log=False, frequency='M') print(commodity) print(fit.summary()) f.write(fit.summary().as_latex()) f.write(co) except KeyError: print("Error in " + co) pass f.close() all_basis = pd.concat(all_basis, axis=1) all_basis = all_basis.resample('M', convention='end').last() all_basis.describe().round(2).drop( index="count").to_latex("../tex/SummaryStatistics.tex") ax = sns.boxplot(data=all_basis.drop(columns=["CME_RB1", "CME_PL1"])) ax.get_figure().savefig("../tex/CYDistribution.png") return