Beispiel #1
0
    def test_rolling_std_1obs(self):
        result = mom.rolling_std(np.array([1.0, 2.0, 3.0, 4.0, 5.0]), 1, min_periods=1)
        expected = np.zeros(5)

        assert_almost_equal(result, expected)

        result = mom.rolling_std(np.array([np.nan, np.nan, 3.0, 4.0, 5.0]), 3, min_periods=2)
        self.assertTrue(np.isnan(result[2]))
def findEvents(symbols, startday,endday,verbose=False):
  timeofday=dt.timedelta(hours=16)
  timestamps = du.getNYSEdays(startday,endday,timeofday)
  dataobj = da.DataAccess('Norgate')
  if verbose:
    print __name__ + " reading data"

  adjclose = dataobj.get_data(timestamps, symbols, closefield)
  adjclose = (adjclose.fillna()).fillna(method='backfill')

  adjcloseSPY = dataobj.get_data(timestamps, ['SPY'], closefield)
  adjcloseSPY = (adjcloseSPY.fillna()).fillna(method='backfill')
  
  if verbose:
    print __name__ + " finding events"

  # for symbol in symbols:
  # close[symbol][close[symbol]>= 1.0] = np.NAN
  # for i in range(1,len(close[symbol])):
  # if np.isnan(close[symbol][i-1]) and close[symbol][i] < 1.0 :#(i-1)th was > $1, and (i)th is <$1
  # close[symbol][i] = 1.0 #overwriting the price by the bit
  # close[symbol][close[symbol]< 1.0] = np.NAN

  #print adjclose      
  # Get the 20 day moving avg and moving stddev
  movavg = pa.rolling_mean(adjclose,20,min_periods=20)
  movavgSPY = pa.rolling_mean(adjcloseSPY,20,min_periods=20)
  movstddev = pa.rolling_std(adjclose, 20, min_periods=20)
  movstddevSPY = pa.rolling_std(adjcloseSPY, 20, min_periods=20)
  
  upperband = movavg + 2*movstddev
  upperbandSPY = movavgSPY + 2*movstddevSPY
  lowerband = movavg - 2*movstddev
  lowerbandSPY = movavgSPY - 2*movstddevSPY

  # Compute the bollinger %b indicator for all stocks
  normalizedindicator = 2*(adjclose - movavg)/(upperband - lowerband)
  #print normalizedindicator
  normalizedindicatorSPY = 2*(adjcloseSPY - movavgSPY)/(upperbandSPY - lowerbandSPY)
  #print normalizedindicatorSPY
  #bandwidth = (upperband - lowerband)/movavg
  #print bandwidth
  #print upperband
  # Compute the event matrix as follows:
  # Set periods of low volatility to 1
  # In from the period of low volatility to the period of say, 15 days, following low volatility 
  # if the stock price breaks above the upper band, there is a surge. this is a positive event. Set this event to 2
  # Finally, set all events other than 2 to NaN. Then, set all 2's to 1
  eventMatrix = adjclose.copy()
  
  for symbol in symbols:
    for row in range(len(adjclose[:][symbol])):
      eventMatrix[symbol][row] = np.NAN
      if normalizedindicator[symbol][row] - normalizedindicatorSPY['SPY'][row] >= 0.5:
        eventMatrix[symbol][row] = 1

  return eventMatrix
Beispiel #3
0
    def test_rolling_std_1obs(self):
        result = mom.rolling_std(np.array([1.,2.,3.,4.,5.]),
                                 1, min_periods=1)
        expected = np.zeros(5)

        assert_almost_equal(result, expected)

        result = mom.rolling_std(np.array([np.nan,np.nan,3.,4.,5.]),
                                 3, min_periods=2)
        self.assert_(np.isnan(result[2]))
    def forecast_mean_sd(self, returns):
        mean_fcst = pd.Panel(
            {"Forecast": rolling_mean(returns.data, self.window)})
        sd_fcst = pd.Panel(
            {"Forecast": rolling_std(returns.data, self.window)})

        return (mean_fcst, sd_fcst)
def findEvents(symbols, startday,endday,verbose=False):
	timeofday=dt.timedelta(hours=16)
	timestamps = du.getNYSEdays(startday,endday,timeofday)
	dataobj = da.DataAccess('Norgate')
	if verbose:
            print __name__ + " reading data"
	adjclose = dataobj.get_data(timestamps, symbols, closefield)
	adjclose = (adjclose.fillna()).fillna(method='backfill')
	if verbose:
            print __name__ + " finding events"
	# for symbol in symbols:
	    # close[symbol][close[symbol]>= 1.0] = np.NAN
	    # for i in range(1,len(close[symbol])):
	        # if np.isnan(close[symbol][i-1]) and close[symbol][i] < 1.0 :#(i-1)th was > $1, and (i)th is <$1
             		# close[symbol][i] = 1.0 #overwriting the price by the bit
	    # close[symbol][close[symbol]< 1.0] = np.NAN

  #print adjclose      
  # Get the 20 day moving avg and moving stddev
	movavg = pa.rolling_mean(adjclose,20,min_periods=20)
	movstddev = pa.rolling_std(adjclose, 20, min_periods=20)
  # Compute the upper and lower bollinger bands
	upperband = movavg + 2*movstddev
	lowerband = movavg - 2*movstddev
  
	#bandwidth = (upperband - lowerband)/movavg
	#print bandwidth
	#print upperband
  # Compute the event matrix as follows:
  # Set periods of low volatility to 1
  # In from the period of low volatility to the period of say, 15 days, following low volatility 
  # if the stock price breaks above the upper band, there is a surge. this is a positive event. Set this event to 2
  # Finally, set all events other than 2 to NaN. Then, set all 2's to 1
	lookaheadperiod = 15
	eventMatrix = adjclose.copy()
	for symbol in symbols:
		for row in range(len(adjclose[:][symbol])):
			eventMatrix[symbol][row] = np.NAN
			if upperband[symbol][row] > 0 and lowerband[symbol][row] > 0 and movavg[symbol][row] > 0:
				if (upperband[symbol][row] - lowerband[symbol][row])/movavg[symbol][row] < 0.10:
					eventMatrix[symbol][row] = 1
				else:
          				currow = row - 1
					numOnes = 0
          				while currow > row - lookaheadperiod and currow >= 0:
						if eventMatrix[symbol][currow] != 1:
							break

		        			if eventMatrix[symbol][currow] == 1 and adjclose[symbol][row] > upperband[symbol][row]:
              						numOnes = numOnes + 1
            					currow = currow - 1
                    
					if numOnes >= 5:
						eventMatrix[symbol][row] = 2

		eventMatrix[symbol][eventMatrix[symbol]!= 2] = np.NAN
    		eventMatrix[symbol][eventMatrix[symbol]== 2] = 1
    

	return eventMatrix
Beispiel #6
0
def bbands(s, n=20, ndev=2):
    mavg = moments.rolling_mean(s, n)
    mstd = moments.rolling_std(s, n)

    hband = mavg + ndev * mstd
    lband = mavg - ndev * mstd

    return DataFrame(dict(ma=mavg, lband=lband, hband=hband))
Beispiel #7
0
def bbands(s, n=20, ndev=2):
    mavg = moments.rolling_mean(s, n)
    mstd = moments.rolling_std(s, n)

    hband = mavg + ndev*mstd
    lband = mavg - ndev*mstd

    return DataFrame(dict(ma=mavg, lband=lband, hband=hband))
Beispiel #8
0
    def test_rolling_functions_window_non_shrinkage(self):
        # GH 7764
        s = Series(range(4))
        s_expected = Series(np.nan, index=s.index)
        df = DataFrame([[1, 5], [3, 2], [3, 9], [-1, 0]], columns=['A', 'B'])
        df_expected = DataFrame(np.nan, index=df.index, columns=df.columns)
        df_expected_panel = Panel(items=df.index,
                                  major_axis=df.columns,
                                  minor_axis=df.columns)

        functions = [
            lambda x: mom.rolling_cov(
                x, x, pairwise=False, window=10, min_periods=5),
            lambda x: mom.rolling_corr(
                x, x, pairwise=False, window=10, min_periods=5),
            lambda x: mom.rolling_max(x, window=10, min_periods=5),
            lambda x: mom.rolling_min(x, window=10, min_periods=5),
            lambda x: mom.rolling_sum(x, window=10, min_periods=5),
            lambda x: mom.rolling_mean(x, window=10, min_periods=5),
            lambda x: mom.rolling_std(x, window=10, min_periods=5),
            lambda x: mom.rolling_var(x, window=10, min_periods=5),
            lambda x: mom.rolling_skew(x, window=10, min_periods=5),
            lambda x: mom.rolling_kurt(x, window=10, min_periods=5),
            lambda x: mom.rolling_quantile(
                x, quantile=0.5, window=10, min_periods=5),
            lambda x: mom.rolling_median(x, window=10, min_periods=5),
            lambda x: mom.rolling_apply(x, func=sum, window=10, min_periods=5),
            lambda x: mom.rolling_window(
                x, win_type='boxcar', window=10, min_periods=5),
        ]
        for f in functions:
            try:
                s_result = f(s)
                assert_series_equal(s_result, s_expected)

                df_result = f(df)
                assert_frame_equal(df_result, df_expected)
            except (ImportError):

                # scipy needed for rolling_window
                continue

        functions = [
            lambda x: mom.rolling_cov(
                x, x, pairwise=True, window=10, min_periods=5),
            lambda x: mom.rolling_corr(
                x, x, pairwise=True, window=10, min_periods=5),
            # rolling_corr_pairwise is depracated, so the following line should be deleted
            # when rolling_corr_pairwise is removed.
            lambda x: mom.rolling_corr_pairwise(x, x, window=10, min_periods=5
                                                ),
        ]
        for f in functions:
            df_result_panel = f(df)
            assert_panel_equal(df_result_panel, df_expected_panel)
def before_trading_start(context, data):
    # Pulling VIX
    vix = pd.read_csv(
        'http://www.cboe.com/publish/scheduledtask/mktdata/datahouse/vixcurrent.csv'
    )
    vix.columns = ['Date', 'Open', 'High', 'Low', 'Close']
    vix = vix[1:]
    vix['Date'] = pd.to_datetime(vix['Date'])
    vix = vix.set_index(pd.DatetimeIndex(vix['Date']))

    # Getting VIX last close
    vx = float(vix[-1:].Close[0])

    # Pulling Futures Data
    vc = pd.read_csv('http://cfe.cboe.com/market-data/futures-settlements')
    vc.columns = ['Symbol', 'settle']
    vc['expiry'] = pd.to_datetime(vc['Symbol'].str.split(' ').str.get(1))
    vc['name'] = vc['Symbol'].str.split(' ').str.get(0)
    vc = vc[vc['name'] == 'VX'][['Symbol', 'settle', 'expiry']]
    vc = vc[vc['expiry'] > dt.datetime.today().date()]
    vc = vc.reset_index()

    # Getting Settlements of V1 and V2
    v1 = vc['settle'][0]
    v2 = vc['settle'][1]

    spot_wgt = maturities(context, data)
    front_wgt = 1 - spot_wgt

    context.threshold = 0.95

    # Creating ratio from weighting
    sf1_ratio = vx / v1
    f1f2_ratio = v1 / v2
    #print"Front weight: %.6f"%spot_wgt
    #print "V1: %.4f" %data.current('v1','price')
    context.last_ratio = spot_wgt * sf1_ratio + front_wgt * f1f2_ratio
    #print "Contango: %.6f"%context.last_ratio
    context.signal = (context.last_ratio <
                      context.threshold) and (vx < context.max_vix)
    # context.open_price = data.current(context.vxx,'close')

    # Pulling spy_hist
    request_data(historyData=[(context.spy, '1 day', '22 D')])
    spy_hist = data[context.spy].hist['1 day']['close']
    log_returns = np.log(spy_hist) - np.log(spy_hist.shift(1))
    daily_vol = st.rolling_std(log_returns, context.augen_period, ddof=1)
    daily_vol_dollar = (np.exp(daily_vol) - 1) * spy_hist
    context.dollar_vol = daily_vol_dollar.ix[-1]

    context.spy_last_close = data.current(context.spy, 'close')

    context.stop_loss_initializer = False
    context.stop_loss_not_triggered = True
    context.buyback_not_triggered = True
Beispiel #10
0
def before_trading_start(context, data):
    spot_wgt = maturities(context, data)
    front_wgt = 1 - spot_wgt
    print "Front weight: %.4f" % spot_wgt

    # Logging Key Notes
    settle = data.current('vf', 'settle')
    contango = data.current('vf', 'contango')
    vix_close = float(data.current('vix', 'Close'))
    print "Settlement: " + str(settle) + " Contango: " + str(
        contango) + " VIX Close: " + str(vix_close)

    # Calculating Ratio for VIX/F1
    vixf1 = vix_close / data.current('vf', 'settle')

    context.xiv_ratio = spot_wgt * vixf1 + front_wgt * contango
    context.xiv_signal = (context.xiv_ratio < context.xiv_threshold) and (
        vix_close < context.max_vix)
    log.info("Normalized Ratio: %.2f" % context.xiv_ratio)
    print "Normalized Ratio: " + str(context.xiv_ratio)

    # XIV Stop Loss Triggers Initiation
    context.stop_loss_initializer = False
    context.stop_loss_not_triggered = True
    context.buyback_not_triggered = True

    # XIV Buyback Triggers
    spy_hist = data.history(context.spy, 'close', context.augen_period + 1,
                            '1d')
    log_returns = np.log(spy_hist) - np.log(spy_hist.shift(1))
    daily_vol = st.rolling_std(log_returns, context.augen_period, ddof=1)
    daily_vol_dollar = daily_vol * spy_hist
    context.dollar_vol = daily_vol_dollar.ix[-1]

    # Calculating SPY SMA 25-220
    spy_hist = data.history(context.spy, 'close', context.spy_long, '1d')
    context.spy_long_sma = spy_hist.mean()
    context.spy_shrt_sma = spy_hist.tail(context.spy_short).mean()
    context.spy_signal = context.spy_shrt_sma > context.spy_long_sma
    context.last_spy_price = data.current(context.spy, 'close')
    log.info(
        "Last SPY Close: %.2f SPY_Long: %.2f SPY_Short: %.2f" %
        (context.last_spy_price, context.spy_long_sma, context.spy_shrt_sma))

    # Calculating TLT SMA 35-70
    tlt_hist = data.history(context.tlt, 'close', context.tlt_long, '1d')
    context.tlt_long_sma = tlt_hist.mean()
    context.tlt_shrt_sma = tlt_hist.tail(context.tlt_short).mean()
    context.tlt_signal = context.tlt_shrt_sma > context.tlt_long_sma
    last_tlt_price = data.current(context.tlt, 'close')
    log.info("Last TLT Close: %.2f TLT_Long: %.2f TLT_Short: %.2f" %
             (last_tlt_price, context.tlt_long_sma, context.tlt_shrt_sma))
Beispiel #11
0
    def test_rolling_std_neg_sqrt(self):
        # unit test from Bottleneck

        # Test move_nanstd for neg sqrt.

        a = np.array([0.0011448196318903589,
                      0.00028718669878572767,
                      0.00028718669878572767,
                      0.00028718669878572767,
                      0.00028718669878572767])
        b = mom.rolling_std(a, window=3)
        self.assertTrue(np.isfinite(b[2:]).all())

        b = mom.ewmstd(a, span=3)
        self.assertTrue(np.isfinite(b[2:]).all())
Beispiel #12
0
    def test_rolling_std_neg_sqrt(self):
        # unit test from Bottleneck

        # Test move_nanstd for neg sqrt.

        a = np.array([
            0.0011448196318903589, 0.00028718669878572767,
            0.00028718669878572767, 0.00028718669878572767,
            0.00028718669878572767
        ])
        b = mom.rolling_std(a, window=3)
        self.assert_(np.isfinite(b[2:]).all())

        b = mom.ewmstd(a, span=3)
        self.assert_(np.isfinite(b[2:]).all())
def bollinger_bands(d_data, ldt_timestamps, ls_symbols=None, lookback = 20,
                    width = 1, plot_boll=False, ls_symbols_plot=None):
    if ls_symbols == None:
        ls_symbols = list(d_data.keys())
    df_close = copy.deepcopy(d_data)
    df_close = df_close[ls_symbols]

    df_mean_bollinger = copy.deepcopy(df_close) * np.NAN
    df_std_bollinger = copy.deepcopy(df_close) *np.NAN
    df_index_bollinger = copy.deepcopy(df_close) *np.NAN

    for c_sym in ls_symbols:
        df_mean_bollinger[c_sym] = pd_stats.rolling_mean(df_close[c_sym],lookback)
        df_std_bollinger[c_sym] = width*pd_stats.rolling_std(df_close[c_sym],lookback)
        df_index_bollinger[c_sym] = (df_close[c_sym] -
                                     df_mean_bollinger[c_sym])/df_std_bollinger[c_sym]

    if plot_boll:
        if ls_symbols_plot == None:
            if len(ls_symbols) <= 5:
                ls_symbols_plot = ls_symbols
            else:
                ls_symbols_plot = ls_symbols[0:5]
        fig = []
        for c_sym in ls_symbols_plot:
            fig.append(plt.figure())
            ax = fig[-1].add_subplot(211)
            ax.plot(ldt_timestamps,df_close[c_sym],'k')
            ax.plot(ldt_timestamps,df_mean_bollinger[c_sym],'b')
            ax.plot(ldt_timestamps,df_mean_bollinger[c_sym] - df_std_bollinger[c_sym],'b--')
            ax.plot(ldt_timestamps,df_mean_bollinger[c_sym] + df_std_bollinger[c_sym],'b--')
            ax.set_xlim((ldt_timestamps[0],ldt_timestamps[-1]))
            ax.get_xaxis().set_visible(False)
            ax.set_ylabel('Adj. Close')

            ax = fig[-1].add_subplot(212)
            ax.plot(ldt_timestamps, df_index_bollinger)
            ax.plot([ldt_timestamps[0], ldt_timestamps[-1]],
                    [1, 1])
            ax.plot([ldt_timestamps[0], ldt_timestamps[-1]],
                    [-1, -1])
            ax.set_xlim((ldt_timestamps[0],ldt_timestamps[-1]))
            ax.set_xlabel('Time')
            ax.set_ylabel('Bollinger Val.')
        plt.show()

    return df_index_bollinger
Beispiel #14
0
def compute_bollinger_band(basic_portfolio,
                           period,
                           source='yahoo',
                           filename=None):
    """
    Compute the bollinger band for a list of stocks.
    @param basic_portfolio: A basic portfolio instance
    @param period:
    @param source: source to get the data
    @param filename:
    @return:
    """

    assert isinstance(basic_portfolio, BasicPortfolio)
    stock_close_prices = basic_portfolio.get_stock_close_prices(source)

    basic_portfolio.print_information()
    print 'Lookback period : ', period

    bol_mean = ts.rolling_mean(stock_close_prices, period)
    bol_std = ts.rolling_std(stock_close_prices, period)

    bollinger_band_up = bol_mean + bol_std
    bollinger_band_down = bol_mean - bol_std

    plt.clf()
    plt.plot(stock_close_prices.index, stock_close_prices.values)
    plt.plot(stock_close_prices.index, bollinger_band_up)
    plt.plot(stock_close_prices.index, bollinger_band_down)
    plt.legend(['Stock adjusted price', 'Bollinger band', 'Bollinger band'])
    plt.ylabel('Price')
    plt.xlabel('Date')
    if filename is not None:
        plt.savefig(filename, format='pdf')
    else:
        plt.show()

    bol_val = (stock_close_prices - bol_mean) / bol_std
    val = DataFrame(bol_val,
                    index=stock_close_prices.index,
                    columns=basic_portfolio.tickers)

    # print val[-5:]
    val.to_csv('result/bol.csv')

    # return the bollinger value
    return val
Beispiel #15
0
    def test_rolling_functions_window_non_shrinkage(self):
        # GH 7764
        s = Series(range(4))
        s_expected = Series(np.nan, index=s.index)
        df = DataFrame([[1,5], [3, 2], [3,9], [-1,0]], columns=['A','B'])
        df_expected = DataFrame(np.nan, index=df.index, columns=df.columns)
        df_expected_panel = Panel(items=df.index, major_axis=df.columns, minor_axis=df.columns)

        functions = [lambda x: mom.rolling_cov(x, x, pairwise=False, window=10, min_periods=5),
                     lambda x: mom.rolling_corr(x, x, pairwise=False, window=10, min_periods=5),
                     lambda x: mom.rolling_max(x, window=10, min_periods=5),
                     lambda x: mom.rolling_min(x, window=10, min_periods=5),
                     lambda x: mom.rolling_sum(x, window=10, min_periods=5),
                     lambda x: mom.rolling_mean(x, window=10, min_periods=5),
                     lambda x: mom.rolling_std(x, window=10, min_periods=5),
                     lambda x: mom.rolling_var(x, window=10, min_periods=5),
                     lambda x: mom.rolling_skew(x, window=10, min_periods=5),
                     lambda x: mom.rolling_kurt(x, window=10, min_periods=5),
                     lambda x: mom.rolling_quantile(x, quantile=0.5, window=10, min_periods=5),
                     lambda x: mom.rolling_median(x, window=10, min_periods=5),
                     lambda x: mom.rolling_apply(x, func=sum, window=10, min_periods=5),
                     lambda x: mom.rolling_window(x, win_type='boxcar', window=10, min_periods=5),
                    ]
        for f in functions:
            try:
                s_result = f(s)
                assert_series_equal(s_result, s_expected)

                df_result = f(df)
                assert_frame_equal(df_result, df_expected)
            except (ImportError):

                # scipy needed for rolling_window
                continue

        functions = [lambda x: mom.rolling_cov(x, x, pairwise=True, window=10, min_periods=5),
                     lambda x: mom.rolling_corr(x, x, pairwise=True, window=10, min_periods=5),
                     # rolling_corr_pairwise is depracated, so the following line should be deleted
                     # when rolling_corr_pairwise is removed.
                     lambda x: mom.rolling_corr_pairwise(x, x, window=10, min_periods=5),
                    ]
        for f in functions:
            df_result_panel = f(df)
            assert_panel_equal(df_result_panel, df_expected_panel)
Beispiel #16
0
def compute_bollinger_band(basic_portfolio, period, source='yahoo', filename=None):
    """
    Compute the bollinger band for a list of stocks.
    @param basic_portfolio: A basic portfolio instance
    @param period:
    @param source: source to get the data
    @param filename:
    @return:
    """

    assert isinstance(basic_portfolio, BasicPortfolio)
    stock_close_prices = basic_portfolio.get_stock_close_prices(source)
    
    basic_portfolio.print_information()
    print 'Lookback period : ', period
        
    bol_mean = ts.rolling_mean(stock_close_prices, period)
    bol_std = ts.rolling_std(stock_close_prices, period)
    
    bollinger_band_up = bol_mean + bol_std
    bollinger_band_down = bol_mean - bol_std

    plt.clf()
    plt.plot(stock_close_prices.index, stock_close_prices.values)
    plt.plot(stock_close_prices.index, bollinger_band_up)
    plt.plot(stock_close_prices.index, bollinger_band_down)
    plt.legend(['Stock adjusted price', 'Bollinger band', 'Bollinger band'])
    plt.ylabel('Price')
    plt.xlabel('Date')
    if filename is not None:
        plt.savefig(filename, format='pdf')
    else:
        plt.show()
    
    bol_val = (stock_close_prices - bol_mean)/bol_std
    val = DataFrame(bol_val, index=stock_close_prices.index,
                    columns=basic_portfolio.tickers)
    
    # print val[-5:]
    val.to_csv('result/bol.csv')
    
    # return the bollinger value
    return val
Beispiel #17
0
def before_trading_start(context, data):
    spot_wgt = maturities(context, data)
    front_wgt = 1 - spot_wgt

    context.threshold = 0.95

    # Creating ratio from weighting
    sf1_ratio = data.current('vix', 'Open') / data.current('v1', 'Open')
    f1f2_ratio = data.current('v1', 'Open') / data.current('v2', 'Open')
    print "Front weight: %.6f" % spot_wgt
    print "V1 SOD: %.4f" % data.current('v1', 'Open')
    context.last_ratio = spot_wgt * sf1_ratio + front_wgt * f1f2_ratio
    print "Contango SOD: %.6f" % context.last_ratio
    context.signal = (context.last_ratio < context.threshold) and (
        data.current('vix', 'Open') < context.max_vix)

    # Creating EOD Ratio from weighting
    sf1_ratio_eod = data.current('vix', 'price') / data.current('v1', 'price')
    f1f2_ratio_eod = data.current('v1', 'price') / data.current('v2', 'price')
    print "V1 EOD: %.4f" % data.current('v1', 'price')
    context.last_ratio_eod = spot_wgt * sf1_ratio_eod + front_wgt * f1f2_ratio_eod
    print "Contango EOD: %.6f" % context.last_ratio_eod
    context.signal_eod = (context.last_ratio_eod < context.threshold) and (
        data.current('vix', 'price') < context.max_vix)

    # context.open_price = data.current(context.vxx,'close')

    spy_hist = data.history(context.spy, 'close', context.augen_period + 1,
                            '1d')
    log_returns = np.log(spy_hist) - np.log(spy_hist.shift(1))
    daily_vol = st.rolling_std(log_returns, context.augen_period, ddof=1)
    daily_vol_dollar = (np.exp(daily_vol) - 1) * spy_hist
    context.dollar_vol = daily_vol_dollar.ix[-1]

    context.spy_last_close = data.current(context.spy, 'close')

    context.stop_loss_initializer = False
    context.stop_loss_not_triggered = True
    context.buyback_not_triggered = True
Beispiel #18
0
def ComputeBollingerBands(ls_symbols, startdate, enddate, period, filename = ''):
    
    # Get the data from local repository
    d_data = GetDataLocalYahoo(startdate, enddate, ls_symbols)
    
    print 'Symbol : ', ls_symbols
    print 'Start date : ', startdate
    print 'Start date : ', enddate
    print 'Lookback period : ', period
    
    df_close = d_data['close']
        
    bol_mean = ts.rolling_mean(df_close, period)
    bol_std = ts.rolling_std(df_close, period)    
    
    bolband_up = bol_mean + bol_std
    bolband_dw = bol_mean - bol_std
        
    # Plotting the prices with x-axis=timestamps
    if filename is not '':
        plt.clf()
        plt.plot(df_close.index, df_close.values)
        plt.plot(df_close.index, bolband_up)
        plt.plot(df_close.index, bolband_dw)
        plt.legend(['Stock adjusted price', 'Bollinger band', 'Bollinger band'])
        plt.ylabel('Price')
        plt.xlabel('Date')
        plt.savefig(filename, format='pdf')     
    
    bol_val = (df_close - bol_mean)/bol_std
    val = DataFrame(bol_val, index = df_close.index, 
                    columns = ls_symbols)
    
    # print val[-5:]
    val.to_csv('bol.csv')
    
    # return the bollinger value
    return val
Beispiel #19
0
print dt.datetime.now().time(), "Read the data"

data_obj = da.DataAccess('Yahoo')
ls_symbols = data_obj.get_symbols_from_list('sp5002012')
cmp_symbols = ['SPY']

ls_keys = 'close'
ldf_data = data_obj.get_data(ldt_timestamps, ls_symbols + cmp_symbols, ls_keys)
ldf_data = ldf_data.fillna(method='ffill')
ldf_data = ldf_data.fillna(method='bfill')
ldf_data = ldf_data.fillna(1.0)

print dt.datetime.now().time(), "Calculating Bollinger's Value"
data_mean = pd.rolling_mean(ldf_data, window=20, min_periods=1)
data_std = pd.rolling_std(ldf_data, window=20, min_periods=1)

bollinger_value = {}
for s in ls_symbols + cmp_symbols:
    bollinger_value[s] = (ldf_data[s][ldt_timestamps] - data_mean[s][ldt_timestamps]) / data_std[s][ldt_timestamps]


print dt.datetime.now().time(), "Finding Events"
total_count = 0
print ldt_timestamps
# f = open('workfile.csv', 'w')
# for s_sym in ls_symbols:
#     print "\t", s_sym
#     temp_count = 0
#     for i in range(1, len(ldt_timestamps)):  # range(19, len(ldt_timestamps) - 20):
#         if bollinger_value[s_sym][i - 1] >= -2.0 >= bollinger_value[s_sym][i] and bollinger_value['SPY'][i] >= 1.5:
Beispiel #20
0
    return ts_date[thisind].strftime('%Y-%m-%d')

####################################################
#     Bollinger Bands with Pandas                  #
####################################################

# Select close price as plotting data
close_px = aapl['Close']

# Parameters for Bollinger Bands
period = 10
std = 2

# Calculation of Bollinger Bands: SMA, Upper and Lower
mavg = pa.rolling_mean(close_px, period)
mstd = pa.rolling_std(close_px, period)
uband = mavg + 2 * mstd
lband = mavg - 2 * mstd

# Excercise: Use Matplotlib to plot stock price
#close_px.plot(label='AAPL', style='k*')
#mavg.plot(label='mavg')
#uband.plot()
#lband.plot()
#plt.legend()
#plt.show()

##################################################
#       Data for Candlestick Chart               #
#       Open, Close, High, Low                   #
##################################################
startday = dt.datetime(2009,1,1)
endday = dt.datetime(2010,1,1)
stock='IBM'
symbols = [stock]
timeofday=dt.timedelta(hours=16)
timestamps = du.getNYSEdays(startday,endday,timeofday)

dataobj = da.DataAccess('Norgate')
adjclose = dataobj.get_data(timestamps, symbols, "close")

adjclose = adjclose.fillna()
adjclose = adjclose.fillna(method='backfill')

# Get the 20 day moving avg and moving stddev
movavg = pa.rolling_mean(adjclose,20,min_periods=20)
movstddev = pa.rolling_std(adjclose, 20, min_periods=20)

# Compute the upper and lower bollinger bands
upperband = movavg + 2*movstddev
lowerband = movavg - 2*movstddev

# Plot the adjclose, movingavg, upper and lower bollinger bands
plt.clf()

plt.plot(adjclose.index,adjclose[stock].values)
plt.plot(adjclose.index,movavg[stock].values)
plt.plot(adjclose.index,upperband[stock].values)
plt.plot(adjclose.index,lowerband[stock].values)
plt.xlim(adjclose.index[0], adjclose.index[len(adjclose.index)-1])

plt.legend(['IBM','Moving Avg.','Upper Bollinger Band','Lower Bollinger Band'], loc='upper left')
Beispiel #22
0
import numpy as np
import sys


def rolling_window(a, window):
    shape = a.shape[:-1] + (a.shape[-1] - window + 1, window)
    strides = a.strides + (a.strides[-1], )
    return np.lib.stride_tricks.as_strided(a, shape=shape, strides=strides)


start = datetime.datetime(2004, 11, 21)
stop = datetime.datetime(2014, 11, 21)
broadMarket = web.DataReader(sys.argv[1], 'yahoo', start, stop)
broadMarket = broadMarket['Close']
returns = broadMarket.pct_change()

vol5 = st.rolling_std(returns, window=5) / 5.0
vol30 = st.rolling_std(returns, window=30) / 30.0
vol60 = st.rolling_std(returns, window=60) / 60.0
vol120 = st.rolling_std(returns, window=120) / 120.0
vol240 = st.rolling_std(returns, window=240) / 240.0

for i in range(240, len(vol240) - 30):
    print vol5.iloc[i], "\t", \
 vol30.iloc[i], "\t", \
 vol60.iloc[i], "\t", \
 vol120.iloc[i], "\t", \
 vol240.iloc[i], "\t", \
 vol30.iloc[i+30]
#returns = np.array([broadMarket.iloc[i,3]/broadMarket.iloc[i-1,3] - 1.0 for i in range(1, len(broadMarket))])
 def forecast_mean_sd(self, returns):
     mean_fcst = pd.Panel({"Forecast":rolling_mean(returns.data, self.window)}) 
     sd_fcst = pd.Panel({"Forecast":rolling_std(returns.data, self.window)})
     
     return (mean_fcst, sd_fcst)
import matplotlib.pyplot as plt
import pandas.util.testing as t
import pandas.stats.moments as m

t.N = 500
ts = t.makeTimeSeries()
ts[::100] = 20

s = ts.cumsum()


plt.figure(figsize=(10, 5))
plt.plot(s.index, m.ewmvol(s, span=50, min_periods=1).values, color='b')
plt.plot(s.index, m.rolling_std(s, 50, min_periods=1).values, color='r')

plt.title('Exp-weighted std with shocks')
plt.legend(('Exp-weighted', 'Equal-weighted'))

f = plt.gcf()
f.autofmt_xdate()

plt.show()
plt.close('all')
print title
ax1.set_title(title)
# ax.set_xlabel('Time (h)')
# ax.set_ylabel('RMSE')
# ax.set_yscale('log')
# ax.set_xlim(0,30)
# colors = sns.color_palette("husl", 25)
# for i in range(0,25):
# ax.scatter(time_by_seed[i], error_by_seed[i], c=cm.hsv(i/25.,1), s=[30]*len(time_by_seed[i]))
# ax.scatter(time_by_seed[i], error_by_seed[i], c=[colors[i]]*len(time_by_seed[i]), s=[30]*len(time_by_seed[i]))

ax1.set_xlabel('Time (h)')
ax1.set_ylabel('RMSE')
ax1.set_xlim(-1, 30)
y_mean = stats.rolling_mean(sorted_errors, 5)
y_std = stats.rolling_std(sorted_errors, 5)
# y_upper = y_mean + 2*y_std
y_upper = stats.rolling_max(sorted_errors, 5)
# y_lower = y_mean - 2*y_std
y_lower = stats.rolling_min(sorted_errors, 5)
sorted_data = DataFrame(data=sorted_points, columns=['time', 'binned_time', 'error', 'seed'])
# sns.jointplot("binned_time", "error", sorted_data)
# ax1.scatter(sorted_binned_time, sorted_errors)
ax1.plot(sorted_time, y_mean, color="red", label="Rolling mean")
# ax1.errorbar(sorted_binned_time, sorted_errors, marker='o', ms=8, yerr=3*y_std, ls='dotted', label="Rolling mean")
ax1.legend()
ax1.fill_between(sorted_time, y_mean, y_upper, facecolor='gray', interpolate=True, alpha=0.5)
ax1.fill_between(sorted_time, y_lower, y_mean, facecolor='gray', interpolate=True, alpha=0.5)
if not os.path.isdir("plots"):
    os.mkdir("plots")
#fig.savefig("plots/points.png", bbox_inches='tight')
import datetime
import pandas as pd 
import numpy as np
import sys

def rolling_window(a, window):
    shape = a.shape[:-1] + (a.shape[-1] - window + 1, window)
    strides = a.strides + (a.strides[-1],)
    return np.lib.stride_tricks.as_strided(a, shape=shape, strides=strides)

start = datetime.datetime(2004, 11, 21)
stop = datetime.datetime(2014, 11, 21)
broadMarket = web.DataReader(sys.argv[1], 'yahoo', start, stop)
broadMarket = broadMarket['Close']
returns = broadMarket.pct_change()

vol5 = st.rolling_std(returns, window=5)/5.0
vol30 = st.rolling_std(returns, window=30)/30.0
vol60 = st.rolling_std(returns, window=60)/60.0
vol120 = st.rolling_std(returns, window=120)/120.0
vol240 = st.rolling_std(returns, window=240)/240.0

for i in range(240, len(vol240)-30):
    print vol5.iloc[i], "\t", \
	vol30.iloc[i], "\t", \
	vol60.iloc[i], "\t", \
	vol120.iloc[i], "\t", \
	vol240.iloc[i], "\t", \
	vol30.iloc[i+30]
#returns = np.array([broadMarket.iloc[i,3]/broadMarket.iloc[i-1,3] - 1.0 for i in range(1, len(broadMarket))])
Beispiel #27
0
data_access = da.DataAccess('Yahoo')
symbols = data_access.get_symbols_from_list("sp5002012")
symbols.append(_benchmark_symbol)
print 'total symbols:', len(symbols), "starts with:", symbols[:20]

print 'getting data...'
ls_keys = ['close'] # 'actual_close'
ldf_data = data_access.get_data(index, symbols, ls_keys)
d_data = dict(zip(ls_keys, ldf_data))

print 'filling n/a with 1.0...'
for s_key in ls_keys:
    d_data[s_key] = d_data[s_key].fillna(method='ffill')
    d_data[s_key] = d_data[s_key].fillna(method='bfill')
    d_data[s_key] = d_data[s_key].fillna(1.0)

print "calculating rolling mean and std"
prices = d_data['close'].copy()
rolling_means = moments.rolling_mean(prices, _lookback, min_periods=_lookback)
rolling_stds = moments.rolling_std(prices, _lookback, min_periods=_lookback)

print "calculating Bollinger values"
bands = pd.DataFrame(0, index, symbols)
for s in symbols:
    bands[s] = (prices[s] - rolling_means[s]) / (rolling_stds[s])

print "finding Events for Bollinger data"
find_events(bands, symbols)

print "orders put to:", _orders_fname
print "done."
Beispiel #28
0
import matplotlib.pyplot as plt
import pandas.util.testing as t
import pandas.stats.moments as m

t.N = 500
ts = t.makeTimeSeries()
ts[::100] = 20

s = ts.cumsum()

plt.figure(figsize=(10, 5))
plt.plot(s.index, m.ewmvol(s, span=50, min_periods=1).values, color='b')
plt.plot(s.index, m.rolling_std(s, 50, min_periods=1).values, color='r')

plt.title('Exp-weighted std with shocks')
plt.legend(('Exp-weighted', 'Equal-weighted'))

f = plt.gcf()
f.autofmt_xdate()

plt.show()
plt.close('all')
Beispiel #29
0
dt_end = dt.datetime(year=2010, month=12, day=31)
print "symbol:", symbol, "from", dt_start, 'to', dt_end

print "reading close prices to DataFrame from Yahoo"
index = du.getNYSEdays(dt_start, dt_end, dt.timedelta(hours=16))
c_datable = da.DataAccess('Yahoo')
key_list = ['open', 'high', 'low', 'close', 'volume', 'actual_close']
data_list = c_datable.get_data(index, symbols, key_list)
d_data = dict(zip(key_list, data_list))
price = d_data['close'].copy()
price = price.fillna(method='ffill')
price = price.fillna(method='bfill')

print "calculating rolling mean and std"
rolling_mean = moments.rolling_mean(price, 20, min_periods=20)
rolling_std = moments.rolling_std(price, 20, min_periods=20)

print "generating graph"
plt.clf()
plt.plot(price.index, price[symbol].values, label=symbol)
plt.plot(price.index, rolling_mean[symbol].values)
#plt.plot(price.index, rolling_std[symbol].values)
plt.legend([symbol, 'Rolling Mean'])
plt.ylabel('Adjusted Close')
graph_file_name = symbol + ".png"
print "saving to", graph_file_name
plt.savefig(graph_file_name, format='png')

print "calculating Bollinger values"
bands = pd.DataFrame(0, index, symbols)
for s in symbols:
Beispiel #30
0
print title
ax1.set_title(title)
# ax.set_xlabel('Time (h)')
# ax.set_ylabel('RMSE')
# ax.set_yscale('log')
# ax.set_xlim(0,30)
# colors = sns.color_palette("husl", 25)
# for i in range(0,25):
# ax.scatter(time_by_seed[i], error_by_seed[i], c=cm.hsv(i/25.,1), s=[30]*len(time_by_seed[i]))
# ax.scatter(time_by_seed[i], error_by_seed[i], c=[colors[i]]*len(time_by_seed[i]), s=[30]*len(time_by_seed[i]))

ax1.set_xlabel('Time (h)')
ax1.set_ylabel('RMSE')
ax1.set_xlim(-1, 30)
y_mean = stats.rolling_mean(sorted_errors, 5)
y_std = stats.rolling_std(sorted_errors, 5)
# y_upper = y_mean + 2*y_std
y_upper = stats.rolling_max(sorted_errors, 5)
# y_lower = y_mean - 2*y_std
y_lower = stats.rolling_min(sorted_errors, 5)
sorted_data = DataFrame(data=sorted_points,
                        columns=['time', 'binned_time', 'error', 'seed'])
# sns.jointplot("binned_time", "error", sorted_data)
# ax1.scatter(sorted_binned_time, sorted_errors)
ax1.plot(sorted_time, y_mean, color="red", label="Rolling mean")
# ax1.errorbar(sorted_binned_time, sorted_errors, marker='o', ms=8, yerr=3*y_std, ls='dotted', label="Rolling mean")
ax1.legend()
ax1.fill_between(sorted_time,
                 y_mean,
                 y_upper,
                 facecolor='gray',