В этом прогоне я использовал классификацию sklearn.svm.SVC, чтобы увидеть, оказывают ли технические индикаторы какое-либо прогнозирующее влияние на цену следующего периода.
Вот используемые функции: rsi, adx, закрытие, максимум, минимум изменения цен.
x_rsi = rsi(X['close'])
x_adx = adx(X['high'], X['low'], X['close'])
x_close_change = np.log( X['close']).diff()
x_high_change = np.log(X['high']).diff()
x_low_change = np.log(X['low']). diff()
x_close_change50 = np.log(X['close']).diff(50)
Оценка перекрестной проверки ([0,48493068, 0,48733414, 0,48325792]). Предпочел бы, чтобы это значение было выше 0,5, чтобы эта модель была точной.
''' Technical analysis to classify data. By [email protected] 23 May 2018 ''' import numpy as np import pandas as pd import matplotlib.pyplot as plt from numpy.linalg import norm from sklearn.cluster import KMeans from sklearn.linear_model import LinearRegression pickle_path = '../data_bank/XBTUSD_BSTP_Curncy_2017-11-06_to_2018-05-22_interval_1.pkl' X = pd.read_pickle(pickle_path).head(10000) X_test = pd.read_pickle(pickle_path).tail(10000)['close'].values Y = [np.sign(np.diff(np.log(X)))] def rsi(close, n=14, fillna=False): """Relative Strength Index (RSI) Compares the magnitude of recent gains and losses over a specified time period to measure speed and change of price movements of a security. It is primarily used to attempt to identify overbought or oversold conditions in the trading of an asset. https://www.investopedia.com/terms/r/rsi.asp Args: close(pandas.Series): dataset 'Close' column. n(int): n period. fillna(bool): if True, fill nan values. Returns: pandas.Series: New feature generated. """ diff = close.diff() which_dn = diff < 0 up, dn = diff, diff*0 up[which_dn], dn[which_dn] = 0, -up[which_dn] emaup = up.ewm(n).mean() emadn = dn.ewm(n).mean() rsi = 100 * emaup/(emaup + emadn) if fillna: rsi = rsi.fillna(50) return pd.Series(rsi, name='rsi') def adx(high, low, close, n=14, fillna=False): """Average Directional Movement Index (ADX) The Plus Directional Indicator (+DI) and Minus Directional Indicator (-DI) are derived from smoothed averages of these differences, and measure trend direction over time. These two indicators are often referred to collectively as the Directional Movement Indicator (DMI). The Average Directional Index (ADX) is in turn derived from the smoothed averages of the difference between +DI and -DI, and measures the strength of the trend (regardless of direction) over time. Using these three indicators together, chartists can determine both the direction and strength of the trend. http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:average_directional_index_adx Args: high(pandas.Series): dataset 'High' column. low(pandas.Series): dataset 'Low' column. close(pandas.Series): dataset 'Close' column. n(int): n period. fillna(bool): if True, fill nan values. Returns: pandas.Series: New feature generated. """ cs = close.shift(1) tr = high.combine(cs, max) - low.combine(cs, min) trs = tr.rolling(n).sum() up = high - high.shift(1) dn = low.shift(1) - low pos = ((up > dn) & (up > 0)) * up neg = ((dn > up) & (dn > 0)) * dn dip = 100 * pos.rolling(n).sum() / trs din = 100 * neg.rolling(n).sum() / trs dx = 100 * np.abs((dip - din)/(dip + din)) adx = dx.ewm(n).mean() if fillna: adx = adx.fillna(40) return pd.Series(adx, name='adx') x_rsi = rsi(X['close']) x_adx = adx(X['high'], X['low'], X['close']) x_close_change = np.log(X['close']).diff() x_high_change = np.log(X['high']).diff() x_low_change = np.log(X['low']).diff() x_close_change50 = np.log(X['close']).diff(50) x = list(zip(x_rsi, x_adx, x_close_change, x_high_change, x_low_change, x_close_change50)) x_a = x[50:][:-1] y = np.sign(x_close_change.shift(-1).fillna(0)) y_a = y[50:][:-1] from sklearn import svm from sklearn.model_selection import cross_val_score clf = svm.SVC() scores = cross_val_score(clf, x_a, y_a)