def olsfit(ticker, bench, begdate, enddate): tickerdata = ts.get_k_data(ticker, start=begdate, end=enddate) benchdata = ts.get_k_data(bench, start=begdate, end=enddate) y = DP.closetoret(tickerdata['close']) x = DP.closetoret(benchdata['close']) x = sm.add_constant(x) model = sm.OLS(y, x) results = model.fit() return results.summary()
def rollingbeta(ticker, bench, begdate, enddate, window=252): tickerdata = ts.get_k_data(ticker, start=begdate, end=enddate) benchdata = ts.get_k_data(bench, start=begdate, end=enddate) y0 = DP.closetoret(tickerdata['close']) x0 = DP.closetoret(benchdata['close']) y0 = pd.Series(y0) x0 = pd.Series(x0) model = pd.ols(y=y0, x=x0, window=window) model.beta.plot() plt.show()
def adftest(stockcode, begdate, enddate): ''' example result (0.049177575166452235, 0.96241494632563063, 1, 3771, {’1%’: -3.4320852842548395, ’10%’: -2.5671781529820348, ’5%’: -2.8623067530084247}, 19576.116041473877) Since the calculated value of the test statistic is larger than any of the critical values at the 1, 5 or 10 percent levels, we cannot reject the null hypothesis of = 0 and thus we are unlikely to have found a mean reverting time series. This is in line with our tuition as most equities behave akin to Geometric Brownian Motion (GBM), i.e. a random walk. :param stockcode: :return: ''' data = ts.get_k_data(stockcode, start=begdate, end=enddate) ret = DP.closetoret(data['close']) resultret = stattools.adfuller(ret) resultclose = stattools.adfuller(data['close']) print('return', resultret) print('close', resultclose) return resultret, resultclose
def Var(ticker, begdate, enddate, nshares, ndays): x = ts.get_k_data(ticker, start=begdate, end=enddate) ret = DP.closetoret(x) position = nshares * x['close'][-1] VaR = position * std(ret) * sqrt(ndays) print("Holding=", position, "VaR=", round(VaR, 4), "in ", ndays, "Days") return VaR
def AminudIlliq(closes, volume): p = closes dollar_vol = np.array(volume * p) ret = np.array(DP.closetoret(closes)) illiq = np.mean(np.divide(abs(ret), dollar_vol[1:])) return illiq