def aroon(s, n=25): from pandas import DataFrame, Series from pandas.stats import moments s = pullStockData(s) up = 100 * moments.rolling_apply(s['High'], n + 1, lambda x: x.argmax()) / n dn = 100 * moments.rolling_apply(s['Low'], n + 1, lambda x: x.argmin()) / n return DataFrame(dict(up=up, down=dn))
def test_rolling_apply_out_of_bounds(self): # #1850 arr = np.arange(4) # it works! result = mom.rolling_apply(arr, 10, np.sum) self.assertTrue(isnull(result).all()) result = mom.rolling_apply(arr, 10, np.sum, min_periods=1) assert_almost_equal(result, result)
def test_rolling_apply_out_of_bounds(self): # #1850 arr = np.arange(4) # it works! result = mom.rolling_apply(arr, 10, np.sum) self.assert_(isnull(result).all()) result = mom.rolling_apply(arr, 10, np.sum, min_periods=1) assert_almost_equal(result, result)
def roll_mean(x, window, min_periods=None, freq=None, center=False): return mom.rolling_apply(x, window, lambda x: x[np.isfinite(x)].mean(), min_periods=min_periods, freq=freq, center=center)
def cci(s, n=20, c=0.015): if isinstance(s, DataFrame): s = s[['high', 'low', 'close']].mean(axis=1) mavg = moments.rolling_mean(s, n) mdev = moments.rolling_apply(s, n, lambda x: np.fabs(x - x.mean()).mean()) return (s - mavg) / (c * mdev)
def cci(s, n=20, c=0.015): if isinstance(s, DataFrame): s = s[['high', 'low', 'close']].mean(axis=1) mavg = moments.rolling_mean(s, n) mdev = moments.rolling_apply(s, n, lambda x: np.fabs(x - x.mean()).mean()) return (s - mavg)/(c * mdev)
def test_rolling_apply(self): ser = Series([]) assert_series_equal(ser, mom.rolling_apply(ser, 10, lambda x:x.mean())) def roll_mean(x, window, min_periods=None, freq=None): return mom.rolling_apply(x, window, lambda x: x[np.isfinite(x)].mean(), min_periods=min_periods, freq=freq) self._check_moment_func(roll_mean, np.mean)
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 safezone(s, position, nmean=10, npen=2.0, nagg=3): if isinstance(s, DataFrame): s = s.low if position == 'long' else s.high sgn = -1.0 if position == 'long' else 1.0 # Compute the average upside/downside penetration pen = moments.rolling_apply( sgn * s.diff(), nmean, lambda x: x[x > 0].mean() if (x > 0).any() else 0) stop = s + sgn * npen * pen return hhv(stop, nagg) if position == 'long' else llv(stop, nagg)
def safezone(s, position, nmean=10, npen=2.0, nagg=3): if isinstance(s, DataFrame): s = s.low if position == 'long' else s.high sgn = -1.0 if position == 'long' else 1.0 # Compute the average upside/downside penetration pen = moments.rolling_apply( sgn*s.diff(), nmean, lambda x: x[x > 0].mean() if (x > 0).any() else 0 ) stop = s + sgn*npen*pen return hhv(stop, nagg) if position == 'long' else llv(stop, nagg)
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 aroon(s, n=25): up = 100 * moments.rolling_apply(s.high, n + 1, lambda x: x.argmax()) / n dn = 100 * moments.rolling_apply(s.low, n + 1, lambda x: x.argmin()) / n return DataFrame(dict(up=up, down=dn))
def roll_mean(x, window, min_periods=None, time_rule=None): return moments.rolling_apply(x, window, lambda x: x[np.isfinite(x)].mean(), min_periods=min_periods, time_rule=time_rule)
def roll_mean(x, window, min_periods=None, time_rule=None): return mom.rolling_apply(x, window, lambda x: x[np.isfinite(x)].mean(), min_periods=min_periods, time_rule=time_rule)
def aroon(df, n=25): up = 100 * moments.rolling_apply(df['High'], n + 1, lambda x: x.argmax()) / n dn = 100 * moments.rolling_apply(df['Low'], n + 1, lambda x: x.argmin()) / n return pd.DataFrame(dict(up=up, down=dn))