1

我正在从你们的财务处下载股票数据。我正在尝试使用 ANN 生成股票交易信号。我得到的确定系数接近-1,预测似乎是镜像。有人可以建议。

我与其他 ML 获得了类似的确定系数。

这似乎很奇怪。

import numpy as np
import yfinance as yf
import talib as ta

from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error, r2_score, accuracy_score
from sklearn.metrics import classification_report

from sklearn.preprocessing import StandardScaler

from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout

# Ignore warnings
import warnings
warnings.filterwarnings('ignore')

import random
random.seed(42)

# YYYY-MM-DD
start_date = '2010-01-01'   
end_date = '2020-08-03'

#df = yf.download(tickers="^NSEI", start=start_date, end=end_date, interval="1d", progress=False)
df = yf.download("SBIN.NS", start=start_date, end=end_date, interval="1d", progress=False)

def create_trading_condition(df):
    df['RSI'] = ta.RSI(df['Adj Close'].values, timeperiod = 9)
    df['MACD'] = ta.MACD(df['Adj Close'].values, fastperiod=12, slowperiod=26, signalperiod=9)[0]
    df['Williams %R'] = ta.WILLR(df['High'].values, df['Low'].values, df['Adj Close'].values, 7)
    df['C-O'] = df['Adj Close'] - df['Open']
    df['H-L'] = df['High'] - df['Low']
    df['STDEV']= df['Adj Close'].rolling(5).std()
    df['TARGET'] = np.where(df['Adj Close'].shift(-1) > df['Adj Close'], 1, 0)
    df = df.dropna()
    X = df[['RSI', 'MACD', 'Williams %R', 'C-O', 'H-L', 'STDEV']]
    Y = df['TARGET']
    #print(df)
    return (df, X, Y)

df_new, X, Y = create_trading_condition(df)

X_train,X_test,Y_train,Y_test = train_test_split(X, Y, shuffle=False, train_size=0.8)

sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)

clf = Sequential()
clf.add(Dense(units = 128, kernel_initializer = 'uniform', activation= 'relu', input_dim = X.shape[1]))
clf.add(Dense(units = 128, kernel_initializer = 'uniform', activation= 'relu'))
clf.add(Dense(units = 1, kernel_initializer = 'uniform', activation= 'sigmoid'))
clf.compile(optimizer = 'adam', loss = 'mean_squared_error', metrics= ['accuracy'])
clf.fit(X_train, Y_train, batch_size = 10, epochs = 100)

Y_pred = clf.predict(X_test)
#print(Y_pred)
Y_pred = (Y_pred > 0.5)

df_new['Y_pred'] = np.NaN
df_new.iloc[(len(df_new) - len(Y_pred)):,-1:] = Y_pred
trade_df = df_new.dropna()

trade_df['Tomorrows Returns'] = 0.
trade_df['Tomorrows Returns'] = np.log(trade_df['Adj Close']/trade_df['Adj Close'].shift(1))
trade_df['Tomorrows Returns'] = trade_df['Tomorrows Returns'].shift(-1)

# Y_pred = true for long position otherwise short position
trade_df['Strategy Returns'] = 0.
trade_df['Strategy Returns'] = np.where(trade_df['Y_pred'] == True, trade_df['Tomorrows Returns'], -trade_df['Tomorrows Returns'])

trade_df['Cumulative Market Returns'] = np.cumsum(trade_df['Tomorrows Returns'])
trade_df['Cumulative Strategy Returns'] = np.cumsum(trade_df['Strategy Returns'])

#accuracy_test = accuracy_score(Y_test, clf.predict(X_test))

# mean squared error
mse_test = mean_squared_error(Y_test, clf.predict(X_test))

# Coefficient of determination
#r2_test = r2_score(Y_test, clf.predict(X_test))
r2_test = r2_score(Y_test, Y_pred)

#report_test = classification_report(Y_test, clf.predict(X_test))

#print("Test accuracy score: %.2f" % accuracy_test)
print("Test mean squared error: %.2f" % mse_test)
print('Test R-square: %.2f\n' % r2_test)

#print(report_test)

import matplotlib.pyplot as plt
plt.figure(figsize=(10,5))
plt.plot(trade_df['Cumulative Market Returns'], color='r', label='Market Returns')
plt.plot(trade_df['Cumulative Strategy Returns'], color='g', label='Strategy Returns')
plt.legend()
plt.show()
4

0 回答 0