def test_candlestick_chart(data): df = data[100:300] ind = indicator.Indicate(data) sma = ind.smooth_moving_average(14)['close_sma_14'][100:300] rsi = ind.relative_strength_index() rsi_overlay = observe.trend_relative_strength_index(rsi)[100:300] candlestick_chart(df, sma, rsi['rsi'][100:300], rsi_overlay, 'AUD_JPY')
def test_indicate_init_np_columns(data): ts = data.to_numpy() columns = np.split(ts, ts.shape[1], axis=1) del ts ts = np.asarray([np.concatenate(col) for col in columns]) indicator.Indicate(data=ts, labels=['open', 'high', 'low'], orient='columns')
def test_sma(data): arr = data["close"].to_numpy(copy=True).astype(float) ti_sma = np.round(np.append([np.nan for i in range(9)], ti.sma(arr, period=10)), decimals=3) sma = indicator.Indicate(data["close"])\ .smooth_moving_average(10)['close_sma_10']\ .astype(float) assert np.allclose(ti_sma, sma, atol=1e-03, equal_nan=True)
def test_indicate_init_np_rows_integrity(data): df_og = data.astype(float).copy() ts = data.to_numpy() d = indicator.Indicate(data=ts, labels=['open', 'high', 'low', 'close'], orient='rows') df_proc = pd.DataFrame(d.data) df_og.reset_index(drop=True, inplace=True) pd.testing.assert_frame_equal(df_og, df_proc)
def test_adx(data): """Spot check against ti seems accurate, slight differences (1e-02) with Oanda display""" arr_high = data["high"].to_numpy().astype(float) arr_low = data["low"].to_numpy().astype(float) arr_close = data["close"].to_numpy().astype(float) ti_adx = np.append([np.nan for i in range(26)], ti.adx(arr_high, arr_low, arr_close, period=14)) adx = indicator.Indicate(data, exp=6)\ .average_directional_movement()['adx']\ .astype(float) assert np.allclose(ti_adx[-10:], adx[-10:], atol=1e-03, equal_nan=True)
def test_atr(data): arr_high = data["high"].to_numpy().astype(float) arr_low = data["low"].to_numpy().astype(float) arr_close = data["close"].to_numpy().astype(float) ti_atr = np.round(np.append([np.nan for i in range(13)], ti.atr(arr_high, arr_low, arr_close, period=14)), decimals=3) atr = indicator.Indicate(data)\ .average_true_range()['atr']\ .astype(float) assert np.allclose(ti_atr, atr, atol=1e-03, equal_nan=True)
def test_indicate_init_np_columns_integrity(data): df_og = data.astype(float).copy() ts = data.to_numpy() columns = np.split(ts, ts.shape[1], axis=1) del ts ts = np.asarray([np.concatenate(col) for col in columns]) d = indicator.Indicate(data=ts, labels=['open', 'high', 'low', 'close'], orient='columns') df_proc = pd.DataFrame(d.data) df_og.reset_index(drop=True, inplace=True) pd.testing.assert_frame_equal(df_og, df_proc)
def test_stoch(data): arr_high = data["high"].to_numpy().astype(float) arr_low = data["low"].to_numpy().astype(float) arr_close = data["close"].to_numpy().astype(float) ti_percK, ti_percD = ti.stoch(arr_high, arr_low, arr_close, 14, 1, 3) stoch = indicator.Indicate(data).stochastic() percK = stoch['percK'].astype(float) percD = stoch['percD'].astype(float) assert np.allclose(ti_percK[-250:], percK[-250:], atol=1e-03, equal_nan=True) assert np.allclose(ti_percD[-250:], percD[-250:], atol=1e-03, equal_nan=True)
def test_macd(data): """Values match approx Oanda via spot check but not tulipy, hence marked to fail""" arr = data["close"].to_numpy(copy=True).astype(float) ti_macd, ti_signal, ti_histogram = ti.macd(arr, 12, 26, 9) s = indicator.Indicate(data["close"], exp=6)\ .moving_average_convergence_divergence() assert np.allclose(ti_macd[-250:], s['macd'][-250:].astype(float), atol=1e-05, equal_nan=True) assert np.allclose(ti_signal[-250:], s['signal'][-250:].astype(float), atol=1e-05, equal_nan=True) assert np.allclose(ti_histogram[-250:], s['histogram'][-250:].astype(float), atol=1e-05, equal_nan=True)
def test_candles_indicators_relationship(save_to_table, dbsession): """Test indicators table functionality, relationship with candles table and data integrity.""" data = save_to_table[1].drop('batch_id', axis=1) ikh = indicator.Indicate(data, exp=6).ichimoku_kinko_hyo() ikh['senkou_A'] = ikh['senkou_A'][:len(data)] ikh['senkou_B'] = ikh['senkou_B'][:len(data)] df = pd.DataFrame(ikh, index=data.timestamp) df.reset_index(inplace=True) df.rename(columns={'index': 'timestamp'}, inplace=True) df['batch_id'] = save_to_table[0] rows = df.to_dict('records') for entry in rows: entry['timestamp'] = entry['timestamp'].to_pydatetime() dbsession.bulk_insert_mappings(Indicators, rows) assert len( dbsession.query(Indicators).filter_by( batch_id=save_to_table[0]).all()) == len(df) tenkan = [] kijun = [] chikou = [] senkou_A = [] senkou_B = [] for row in dbsession.query(Candles).filter_by(batch_id=save_to_table[0])\ .all(): tenkan.append(row.indicators.tenkan) kijun.append(row.indicators.kijun) chikou.append(row.indicators.chikou) senkou_A.append(row.indicators.senkou_A) senkou_B.append(row.indicators.senkou_B) np_ikh = np.stack((tenkan, kijun, chikou, senkou_A, senkou_B), axis=1) df_ikh = pd.DataFrame( np_ikh, columns=['tenkan', 'kijun', 'chikou', 'senkou_A', 'senkou_B']) # print(df_ikh.tail()) df.drop(['timestamp', 'batch_id'], inplace=True, axis=1) pd.testing.assert_frame_equal(df, df_ikh)
def indicate(mid): return indicator.Indicate(mid)
def test_rsi(data): arr = data["close"].to_numpy(copy=True).astype(float) ti_rsi = np.round(ti.rsi(arr, 14), decimals=3) rsi = indicator.Indicate(data["close"]).relative_strength_index()['rsi']\ .astype(float) assert np.allclose(ti_rsi[-250:], rsi[-250:], atol=1e-03, equal_nan=True)
def test_indicate_init_df_integrity(data): df_og = data.astype(float).copy() d = indicator.Indicate(data=data) df_proc = pd.DataFrame(d.data) df_og.reset_index(drop=True, inplace=True) pd.testing.assert_frame_equal(df_og, df_proc)
def test_indicate_init_np_rows(data): ts = data.to_numpy() indicator.Indicate(data=ts, labels=['open', 'high', 'low'], orient='rows')
def test_indicate_init(ts, labels, orient): d = indicator.Indicate(data=ts, labels=labels, orient=orient) assert isinstance(d.data, dict)
def test_indicate_init_none(): indicator.Indicate()