示例#1
0
    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)
示例#2
0
    def test_fill_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)

            future.fill_data(data=data)
            future.plot()
            plt.show()

        except Exception as e:
            self.fail()
示例#3
0
    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()
示例#4
0
    def test_add_future(self):
        try:
            self.load_data()
            tickers = self.data.columns
            futures = [GenericFuture(ticker=t, number=int(t.split("_B")[1]), data=self.data[t]) for t in tickers]
            brent_curve = CommodityCurve(num_instruments=len(futures))

            for f in futures:
                brent_curve.add_future(f, f.contract_number())

        except Exception as e:
            self.fail(e)
示例#5
0
    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()
示例#6
0
    def test_append_future(self):
        try:
            self.load_data()
            tickers = self.data.columns
            futures = [GenericFuture(ticker=t, number=int(t.split("_B")[1]), data=self.data[t]) for t in tickers]
            brent_curve = CommodityCurve()

            for f in futures:
                brent_curve.append_future(f)
            brent_curve.create_curve()

        except Exception as e:
            self.fail(e)
示例#7
0
    def test_plot_latest(self):
        try:
            self.load_data()
            tickers = self.data.columns
            futures = [GenericFuture(ticker=t, number=int(t.split("_B")[1]), data=self.data[t]) for t in tickers]
            futures.sort()
            brent_curve = CommodityCurve(num_instruments=len(futures))

            for f in futures:
                brent_curve.add_future(f, f.contract_number())

            brent_curve.create_curve()
            brent_curve.plot_latest()
            plt.show()
        except Exception as e:
            raise e
示例#8
0
    #f2 = quandl.get(second_future, start_date='2000-01-01', end_date='2020-12-31')
    #f3 = quandl.get(third_future, start_date='2000-01-01', end_date='2020-12-31')
    if "Settle" in f1.columns:
        price_col = "Settle"
    else:
        price_col = "Close"

    try:
        header = ticker.split(sep="_")[1]
    except IndexError:
        header = ticker.split(sep="_")[0]

    #first_f = pd.concat([first_f, f1[price_col].rename(header)], axis=1)
    #second_f = pd.concat([second_f, f2[price_col].rename(header)], axis=1)
    futures[header] = GenericFuture(ticker=header,
                                    number="1",
                                    data=f1[price_col].rename(header))

print(futures)

from statsmodels.tsa.stattools import adfuller
from numpy import log
from statsmodels.graphics.tsaplots import plot_acf, plot_pacf
from pmdarima.arima.utils import ndiffs
from statsmodels.tsa.arima.model import ARIMA

gasoline = futures['RB']
gasoline.clean_data()
gasoline_monthly = gasoline.series().resample('M').last()

adfuller(gasoline_monthly.diff().dropna(), regression="ct")
示例#9
0
    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)
示例#10
0
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
示例#11
0
    "Gas": "CME_NG",
    "Hogs": "CME_LN",
    "Coffee": "ICE_KC",
    "Cotton": "ICE_CT",
    "Cocoa": "ICE_CC",
    "Sugar": "ICE_SB",
    "HeatOil": "CME_HO",
    "Gasoline": "CME_RB"
}

filename = "../data/TestData_Brent.xlsx"
data = pd.read_excel(filename, index_col="Date")

tickers = data.columns
futures = [
    GenericFuture(ticker=t, number=int(t.split("_B")[1]), data=data[t])
    for t in tickers
]
futures.sort()
brent_curve = CommodityCurve(num_instruments=len(futures))

for f in futures:
    brent_curve.add_future(f, f.contract_number())

brent_curve.create_curve()
brent_curve.to_monthly()
brent_curve.plot_latest()
plt.show()

from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler