Example #1
0
 def summary_stats(self, start, end):
     selection = da.selection(self.df['Adj_Close'], start, end)
     selection_spy = da.selection(self.df_spy['Adj_Close'], start, end)
     returns = selection.pct_change()
     returns_spy = selection_spy.pct_change()
     beta = returns.cov(returns_spy)/returns_spy.var()
     return {'mean': returns.mean(), 'stdev': returns.std(), 'beta': beta}
Example #2
0
 def summary_stats(self, start, end):
     selection = da.selection(self.df['Adj_Close'], start, end)
     selection_spy = da.selection(self.df_spy['Adj_Close'], start, end)
     returns = selection.pct_change()
     returns_spy = selection_spy.pct_change()
     beta = returns.cov(returns_spy) / returns_spy.var()
     return {'mean': returns.mean(), 'stdev': returns.std(), 'beta': beta}
Example #3
0
    def cointegration_test(self, start, end):
        x = da.selection(self.df1["Adj_Close"], start, end)
        y = da.selection(self.df2["Adj_Close"], start, end)
        ols_result = sm.OLS(x, y).fit()
        resid = ols_result.resid

        # compute regression residuals
        adfuller_results = ts.adfuller(resid)
        print "cointegration: " + str(adfuller_results[1])
        bound = pd.Series(movvar(resid, 30)) ** 0.5
        resid_rolling_average = da.exp_moving_average(resid, 0.1)
        resid_with_bounds = np.array([resid_rolling_average, bound, -bound]).T
        columns = ["Residuals", "Upper", "Lower"]
        resid_with_bounds_df = pd.DataFrame(resid_with_bounds, index=resid.index, columns=columns)
        return {"csv": da.csv(resid_with_bounds_df, True)}
Example #4
0
    def cointegration_test(self, start, end):
        x = da.selection(self.df1['Adj_Close'], start, end)
        y = da.selection(self.df2['Adj_Close'], start, end)
        ols_result = sm.OLS(x, y).fit()
        resid = ols_result.resid

        # compute regression residuals
        adfuller_results = ts.adfuller(resid)
        print "cointegration: " + str(adfuller_results[1])
        bound = pd.Series(movvar(resid, 30))**0.5
        resid_rolling_average = da.exp_moving_average(resid, 0.1)
        resid_with_bounds = np.array([resid_rolling_average, bound, -bound]).T
        columns = ['Residuals', 'Upper', 'Lower']
        resid_with_bounds_df = pd.DataFrame(resid_with_bounds,
                                            index=resid.index,
                                            columns=columns)
        return {'csv': da.csv(resid_with_bounds_df, True)}
Example #5
0
    def __init__(self, strategy, ticker, start, end):
        prices = da.dataframe(ticker)['Adj_Close']
        prices = da.selection(prices, start, end)
        self.signals = strategy(prices)
        self.pnl = pd.Series(np.zeros(len(prices)), index=prices.index)

        shares = 0
        for i in xrange(len(self.pnl)):
            self.pnl[i] = (prices[i] - prices[i - 1]) * shares
            shares += self.signals[i]
Example #6
0
 def __init__(self, strategy, ticker, start, end):
     prices = da.dataframe(ticker)['Adj_Close']
     prices = da.selection(prices, start, end)
     self.signals = strategy(prices)
     self.pnl = pd.Series(np.zeros(len(prices)), index=prices.index)
     
     shares = 0
     for i in xrange(len(self.pnl)):
         self.pnl[i] = (prices[i]-prices[i-1])*shares
         shares += self.signals[i]