def stochastic_oscillator(company, window=14, begin="2000-01-03", end=datetime.date.today()): closes = dfr.get_data([company], begin, end) lows = dfr.get_data([company], begin, end, "low") highs = dfr.get_data([company], begin, end, "high") oscillators = closes for index in range(len(closes[company])): high = closes[company][index] low = closes[company][index] for item in lows["low"][(index - window):index]: if item < low: low = item for item in highs["high"][(index - window):index]: if item > high: high = item total = 100 * (closes[company][index] - low) / (high - low) if index > window: oscillators[company][index] = total else: oscillators[company][index] = None oscillators.columns = ["Stochastic Oscillator"] return oscillators
def comulative_returns(): dates = pd.date_range('2012-01-01', '2012-12-31') df = get_data([SPY, XOM], dates) # plot_data(df) # print df cr = compute_comulative_returns(df) plot_data(cr, title="Comulative returns")
def inputs(company, begin="2000-01-03", end=datetime.date.today()): df = dataframe.get_data([company]) boll = indicators.bollinger_differential(company) rsi = indicators.RSI(company) oscillator = indicators.stochastic_oscillator(company) input_set = df.join(boll.join(rsi)) return input_set[20:]
def daily_returns(): dates = pd.date_range('2012-07-01', '2012-07-31') df = get_data([SPY, XOM], dates) # plot_data(df) # print df daily_returns = compute_daily_returns(df) plot_data(daily_returns, title="Daily returns")
def bollinger_differential(company, window=20, begin="2000-01-03", end=datetime.date.today()): df = dfr.get_data([company], begin, end) mean = pd.rolling_mean(df[company], window=window) stdev = pd.rolling_std(df[company], window=window) boll = ((mean - df[company]) / stdev).to_frame() boll.columns = ["Bollinger Differential"] return boll
def test_run(): dates = pd.date_range('2012-01-01', '2012-12-31') df = get_data([SPY], dates) ax = df[SPY].plot(title="SPY rolling mean", label=SPY) # Plot SPY data, retain matplotlib axis object rm_SPY = pd.rolling_mean(df[SPY], window=20) rm_SPY.plot(label="Rolling mean", ax=ax) ax.set_xlabel("Date") ax.set_ylabel("Date") ax.legend(loc="upper left") plt.show()
def test_run(): dates = pd.date_range('2010-01-01', '2012-12-31') symbols = [consts.SPY, consts.XOM, consts.GOOG, consts.GLD] df = dataframe.get_data(symbols, dates) dataframe.plot_data(df) # Mean is the average of set of values # Median value in the middle when they are all sorted # Standard deviation (std) square root of variants # It is a measure of deviation of central value which is mean. # Higher number means that stock has varied over # time more then other stocks. # Global Statistics: mean, median, std, sum, prod, mode, etc.... print df.mean()
def compute_bollinger_bands(): dates = pd.date_range('2012-01-01', '2012-12-31') df = get_data([SPY], dates) rm_spy = get_rolling_mean(df[SPY]) rstd_spy = get_rolling_std(df[SPY]) upper_band, lower_band = get_bollinger_bands(rm_spy, rstd_spy) ax = df[SPY].plot(title="SPY rolling mean", label=SPY) rm_spy.plot(label="Rolling mean", ax=ax) upper_band.plot(label="upper band", ax=ax) lower_band.plot(label="lower band", ax=ax) ax.set_xlabel("Date") ax.set_ylabel("Date") ax.legend(loc="upper left") plt.show()
def RSI(company, window=14, begin="2000-01-03", end=datetime.date.today()): df = dfr.get_data([company], begin, end) change = df[1:] - df[:-1].values rsi = df for item in range(len(df[company])): gains = 0 losses = 0 for index in change[company][item - window:item]: if index > 0: gains += item elif index < 0: losses += item if losses == 0: rsi[company][item] = None else: rsi[company][item] = 100 - (100 / (1 + (gains / losses))) rsi.columns = ["RSI"] return rsi
def test_run(): dates = pd.date_range("2009-01-01", "2012-12-31") df = get_data([SPY, XOM, GLD], dates) daily_returns = compute_daily_returns(df) spy_daily_returns = daily_returns[SPY] xom_daily_returns = daily_returns[XOM] daily_returns.plot(kind="scatter", x=SPY, y=XOM) beta_XOM, alpha_XOM = np.polyfit(spy_daily_returns, xom_daily_returns, 1) plt.plot(spy_daily_returns, beta_XOM * spy_daily_returns + alpha_XOM, "-", color="r") plt.show() gld_daily_returns = daily_returns[GLD] daily_returns.plot(kind="scatter", x=SPY, y=GLD) beta_GLD, alpha_GLD = np.polyfit(spy_daily_returns, gld_daily_returns, 1) plt.plot(spy_daily_returns, beta_GLD * spy_daily_returns + alpha_GLD, "-", color="r") plt.show() print daily_returns.corr(method="pearson")
def test_run(): dates = pd.date_range("2009-01-01", "2012-12-31") df = get_data([SPY, XOM], dates) daily_returns = compute_daily_returns(df) # plot_data(daily_returns, title="Daily returns") daily_returns[SPY].hist(bins=20, label=SPY) daily_returns[XOM].hist(bins=20, label=XOM) plt.legend(loc="upper right") mean = daily_returns[SPY].mean() print "mean= ", mean std = daily_returns[SPY].std() print "std= ", std # plt.axvline(mean, color="w", linestyle="dashed", linewidth=2) # plt.axvline(std, color="r", linestyle="dashed", linewidth=2) # plt.axvline(-std, color="r", linestyle="dashed", linewidth=2) plt.show() print daily_returns.kurtosis()
def tomorrow_close(company, begin="2000-01-03", end=datetime.date.today()): df = dataframe.get_data([company]) df.columns = ["Tomorrow's Close"] return df.shift(-1)[20:]