投资建议的来源 手把手教会你用 AI 和 Python 进行股票往来展望(齐备代码干货)
图片
写在前边的话:本文手把手教会全球使用 Python 和 AI 进行股票往来展望。首先先容了不同的展望方法,卓绝是 LSTM 处理序列展望的才调。然后提供了观点考证设施,包括装配、创建模式等,还展示代码树立,如导入库、用函数熟练测试模子,终末还评估了模子的性能。咱们探寻了多种展望股价的形式,像 Facebook 的 Prophet 等展望器用、SARIMA 模子等统计技能、多项式回首等机器学习政策,还有基于东说念主工智能的轮回神经网罗(RNN)。在广漠东说念主工智能模子与时间里,咱们发现瑕瑜时缅念念(LSTM)模子能带来最理念念的收尾。
LSTM 模子是递归神经网罗架构的一种变形,擅所长理序列展望贫瘠。它与传统的前馈神经网罗不同,具有同样缅念念的结构,能在大皆序列中保留高下文数据。这一特质使其相当合乎时候序列展望、当然言语处理以过火他依赖序列数据的任务。它通过缓解消结怨梯度爆炸问题,处分了圭表 RNN 的基本缺点,从而栽种了模子识别数据集内永久依赖联系的才调。因此,LSTM 已成为需要永劫候深远连气儿数据的复杂任务的首选。
为了考证其灵验性,咱们开拓了一个观点考证。
一、准备职责你需要在你的野心计中(或聘请使用 VSCode 会愈加便捷)装配最新版块的 Python 和 PIP。(https://code.visualstudio.com/)
创建一个带有 “main.py “文献的 Python 模式。
在模式中添加 “data”目次。
树立并激活假造环境。
trading-ai-lstm $ python3 -m venv venvtrading-ai-lstm $ source venv/.bin/activate(venv) trading-ai-lstm $
创建一个 “requirements.txt “文献。
pandasnumpyscikit-learnscipymatplotlibtensorfloweodhdpython-dotenv
确保已在假造环境中升级 PIP 并装配依赖项。
(venv) trading-ai-lstm $ pip install --upgrade pip(venv) trading-ai-lstm $ python3 -m pip install -r requirements.txt
需要在”.env “文献中加入了 EODHD API 的 API 密钥。
API_TOKEN=<YOUR_API_KEY_GOES_HERE>
一切就绪。如果你正在使用 VSCode ,并但愿使用与咱们疏导的”.vscode/settings.json “文献,请点击 Fork 本模式 GitHub 仓库(https://github.com/alexyu2013/trading-ai-lstm),以备以防巧合。
{
'python.formatting.provider': 'none', 'python.formatting.blackArgs': ['--line-length', '160'], 'python.linting.flake8Args': [ '--max-line-length=160', '--ignore=E203,E266,E501,W503,F403,F401,C901' ], 'python.analysis.diagnosticSeverityOverrides': { 'reportUnusedImport': 'information', 'reportMissingImports': 'none' }, '[python]': { 'editor.defaultFormatter': 'ms-python.black-formatter' }}二、代码构建
第一步是导入必要的库。
import osos.environ['TF_CPP_MIN_LOG_LEVEL'] = '1'import pickleimport pandas as pdimport numpy as npfrom dotenv import load_dotenvfrom sklearn.metrics import mean_squared_error, mean_absolute_errorfrom tensorflow.keras.models import Sequentialfrom tensorflow.keras.layers import LSTM, Dense, Dropoutfrom tensorflow.keras.models import load_modelfrom sklearn.preprocessing import MinMaxScalerimport matplotlib.pyplot as pltfrom eodhd import APIClient
TensorFlow 频频会自动生成诸多训诲与调试信息。而咱们更倾向于粗略明了的输出,故而对这些见知进行了铁心。这不错在导入“os”模块后,借助 os.environ 来竣事。
机器学习和东说念主工智能模子的熟练过程需要大皆的微调,主若是通过所谓的超参数(hyperparameters)进行不休。这个问题长短不一,掌持它需要不竭学习和耐性,最好超参数的聘请受到各式要素的影响。字据咱们通过 EODHD API (https://eodhd.com/)取得的圭表普尔 500 指数逐日数据,咱们首先使用了一些广为认同的树立。咱们饱读动您修改这些树立以提高收尾。当前,暴戾将序列长度保持在 20。
# Configurable hyperparametersseq_length = 20batch_size = 64lstm_units = 50epochs = 100
下一步是从咱们的”.env “文献中取得 EODHD API 的 API_TOKEN。
# Load environment variables from the .env fileload_dotenv()# Retrieve the API keyAPI_TOKEN = os.getenv('API_TOKEN')if API_TOKEN is not None: print(f'API key loaded: {API_TOKEN[:4]}********')else: raise LookupError('Failed to load API key.')
需要确保领有灵验的 EODHD API 的 API_TOKEN 才能收效造访数据。
咱们如故树立了几个可重迭使用的函数,并将不才文中详实先容它们的功能。我把这些函数进行了代码注视,以评释其操作。
def get_ohlc_data(use_cache: bool = False) -> pd.DataFrame: ohlcv_file = 'data/ohlcv.csv' if use_cache: if os.path.exists(ohlcv_file): return pd.read_csv(ohlcv_file, index_col=None) else: api = APIClient(API_TOKEN) df = api.get_historical_data( symbol='HSPX.LSE', interval='d', iso8601_start='2010-05-17', iso8601_end='2023-10-04', ) df.to_csv(ohlcv_file, index=False) return df else: api = APIClient(API_TOKEN) return api.get_historical_data( symbol='HSPX.LSE', interval='d', iso8601_start='2010-05-17', iso8601_end='2023-10-04', )def create_sequences(data, seq_length): x, y = [], [] for i in range(len(data) - seq_length): x.append(data[i : i + seq_length]) y.append(data[i + seq_length, 3]) # The prediction target 'close' is the 4th column (index 3) return np.array(x), np.array(y)def get_features(df: pd.DataFrame = None, feature_columns: list = ['open', 'high', 'low', 'close', 'volume']) -> list: return df[feature_columns].valuesdef get_target(df: pd.DataFrame = None, target_column: str = 'close') -> list: return df[target_column].valuesdef get_scaler(use_cache: bool = True) -> MinMaxScaler: scaler_file = 'data/scaler.pkl' if use_cache: if os.path.exists(scaler_file): # Load the scaler with open(scaler_file, 'rb') as f: return pickle.load(f) else: scaler = MinMaxScaler(feature_range=(0, 1)) with open(scaler_file, 'wb') as f: pickle.dump(scaler, f) return scaler else: return MinMaxScaler(feature_range=(0, 1))def scale_features(scaler: MinMaxScaler = None, features: list = []): return scaler.fit_transform(features)def get_lstm_model(use_cache: bool = False) -> Sequential: model_file = 'data/lstm_model.h5' if use_cache: if os.path.exists(model_file): # Load the model return load_model(model_file) else: # Train the LSTM model and save it model = Sequential() model.add(LSTM(units=lstm_units, activation='tanh', input_shape=(seq_length, 5))) model.add(Dropout(0.2)) model.add(Dense(units=1)) model.compile(optimizer='adam', loss='mean_squared_error') model.fit(x_train, y_train, epochs=epochs, batch_size=batch_size, validation_data=(x_test, y_test)) # Save the entire model to a HDF5 file model.save(model_file) return model else: # Train the LSTM model model = Sequential() model.add(LSTM(units=lstm_units, activation='tanh', input_shape=(seq_length, 5))) model.add(Dropout(0.2)) model.add(Dense(units=1)) model.compile(optimizer='adam', loss='mean_squared_error') model.fit(x_train, y_train, epochs=epochs, batch_size=batch_size, validation_data=(x_test, y_test)) return modeldef get_predicted_x_test_prices(x_test: np.ndarray = None): predicted = model.predict(x_test) # Create a zero-filled matrix to aid in inverse transformation zero_filled_matrix = np.zeros((predicted.shape[0], 5)) # Replace the 'close' column of zero_filled_matrix with the predicted values zero_filled_matrix[:, 3] = np.squeeze(predicted) # Perform inverse transformation return scaler.inverse_transform(zero_filled_matrix)[:, 3]def plot_x_test_actual_vs_predicted(actual_close_prices: list = [], predicted_x_test_close_prices = []) -> None: # Plotting the actual and predicted close prices plt.figure(figsize=(14, 7)) plt.plot(actual_close_prices, label='Actual Close Prices', color='blue') plt.plot(predicted_x_test_close_prices, label='Predicted Close Prices', color='red') plt.title('Actual vs Predicted Close Prices') plt.xlabel('Time') plt.ylabel('Price') plt.legend() plt.show()def predict_next_close(df: pd.DataFrame = None, scaler: MinMaxScaler = None) -> float: # Take the last X days of data and scale it last_x_days = df.iloc[-seq_length:][['open', 'high', 'low', 'close', 'volume']].values last_x_days_scaled = scaler.transform(last_x_days) # Reshape this data to be a single sequence and make the prediction last_x_days_scaled = np.reshape(last_x_days_scaled, (1, seq_length, 5)) # Predict the future close price future_close_price = model.predict(last_x_days_scaled) # Create a zero-filled matrix for the inverse transformation zero_filled_matrix = np.zeros((1, 5)) # Put the predicted value in the 'close' column (index 3) zero_filled_matrix[0, 3] = np.squeeze(future_close_price) # Perform the inverse transformation to get the future price on the original scale return scaler.inverse_transform(zero_filled_matrix)[0, 3]def evaluate_model(x_test: list = []) -> None: # Evaluate the model y_pred = model.predict(x_test) mse = mean_squared_error(y_test, y_pred) mae = mean_absolute_error(y_test, y_pred) rmse = np.sqrt(mse) print(f'Mean Squared Error: {mse}') print(f'Mean Absolute Error: {mae}') print(f'Root Mean Squared Error: {rmse}')
咱们需细心指出的是,在各类函数中增添了“use_cache”变量。此政策意在裁汰对 EODHD 应用措施接口的冗余 API 调用,防护诳骗疏导的逐日数据对模子进行重迭的重新熟练。激活“use_cache”变量会将数据存储至“data/”目次下的文献里。若数据不存在,则会创建;若已存在,则会加载。当屡次运行剧本时,此方法能显贵栽种效力。若要在每次运行时取得新数据,只需在调用函数时禁用“use_cache”选项或清空“data/”目次中的文献,就能得到疏导的收尾。
当今干与代码的中枢部分…
if __name__ == '__main__': # Retrieve 3369 days of S&P 500 data df = get_ohlc_data(use_cache=True) print(df)
首先,咱们从 EODHD API 取得 OHLCV 数据,并将其存入名为 “df “的 Pandas DataFrame。OHLCV 暗意开盘价、最高价、最廉价、收盘价和成交量,是往来烛炬图数据的圭表属性。如前所述,咱们启用了缓存以简化经过。咱们还不错聘请在屏幕上清晰这些数据。
图片
咱们将一次性先容以下代码块…
features = get_features(df) target = get_target(df) scaler = get_scaler(use_cache=True) scaled_features = scale_features(scaler, features) x, y = create_sequences(scaled_features, seq_length) train_size = int(0.8 * len(x)) # Create a train/test split of 80/20% x_train, x_test = x[:train_size], x[train_size:] y_train, y_test = y[:train_size], y[train_size:] # Re-shape input to fit lstm layer x_train = np.reshape(x_train, (x_train.shape[0], seq_length, 5)) # 5 features x_test = np.reshape(x_test, (x_test.shape[0], seq_length, 5)) # 5 features
“features” 包括咱们将用来展望标的(即 “close”)的一系列输入。
“target” 包含一个标的值列表,如 “close“。
“scaler”代表一种用于将数字圭表化的方法,使它们具有可比性。举例,咱们的数据集运转时的接近值可能是 784,终末可能是 3538。终末一瞥的数字越高,并不虞味着展望的真谛越大。归一化可确保可比性。
“scaled_features” 是缩放过程的收尾,咱们将用它来熟练东说念主工智能模子。
“x_train” and “x_test” 分裂暗意咱们将用于熟练和测试东说念主工智能模子的数据集,频频的作念法是 80/20 分派。这意味着 80% 的往来数据用于熟练,20% 用于测试模子。x “暗意这些特征或输入。
“y_train” and “y_test” 的功能同样,但只包含标的值,如 “close”。
终末,必须对数据进行重塑,以知足 LSTM 层的条目。
咱们开拓了一种功能,既能对模子进行重新熟练,又能载入之前已熟练好的模子。
model = get_lstm_model(use_cache=True)
图片
从清晰的图片中不错一窥熟练序列。你会发现,当先, “loss”和 “val_loss” 方针可能并不完全一致。不外,跟着熟练的进行,这些数据有望趋于一致,这标明熟练取得了进展。
Loss: 这是在熟练数据集上野心的均方过错(MSE)。它反馈了每个熟练期展望标签和实在标签之间的“cost” 或 “error” 。咱们的标的是通过连气儿的历时来减少这一数字。
Val_loss: 这个均方过错是在考证数据集上细主见,用于算计模子在熟练过程中未遭受的数据上的推崇。它是模子泛化到新的未见数据才调的方针。
搜检测试集的展望收盘价列表,不错使用此代码。
predicted_x_test_close_prices = get_predicted_x_test_prices(x_test) print('Predicted close prices:', predicted_x_test_close_prices)
单看这些数据,可能并不卓绝具有启发性或直不雅。不外,通过绘图实质收盘价与展望收盘价的对比图(请闪耀,这只占系数这个词数据集的 20%),咱们不错得到更显然的图像,如下图所示。
# Plot the actual and predicted close prices for the test data plot_x_test_actual_vs_predicted(df['close'].tail(len(predicted_x_test_close_prices)).values, predicted_x_test_close_prices)
图片
收尾标明,在测试阶段,该模子在展望收盘价方面推崇出色。
当今,咱们来望望最令东说念主期待的方面:咱们能细目翌日的展望收盘价吗?
# Predict the next close price predicted_next_close = predict_next_close(df, scaler) print('Predicted next close price:', predicted_next_close)Predicted next close price: 3536.906685638428
这是一个用于教养主见的基本示例,只是是一个运转。从这里运转,您不错议论加入更多的熟练数据,调养超参数,或将模子应用于不同的阛阓和时候区间。如果您念念对模子进行评估,不错将其包括在内。
# Evaluate the model evaluate_model(x_test)
在咱们的决议中的输出情况是
Mean Squared Error: 0.00021641664334765608Mean Absolute Error: 0.01157513692221611Root Mean Squared Error: 0.014711106122506767
“平均昔日过错”(mean_squared_error)和 “平均齐全过错”(mean_absolute_error)函数来自 scikit-learn 的度量模块,分裂用于野心平均昔日过错(MSE)和平均齐全过错(MAE)。均方根过错 (RMSE) 是通过对 MSE 取昔日根得出的。
这些方针为模子的准确性提供了数字化的评估,也为模子的性能进行了定量的分析,而图形化的展示则更故意于直不雅地对比展望值与实质数值,以及直不雅地相比展望值和实质值。
三、总结在本文中我详实先容了用 Python 和 AI 作念往来展望的经过。首先是各式展望办法,像 Facebook 的 Prophet、SARIMA 模子、多项式回首,还有基于东说念主工智能的轮回神经网罗(RNN),这内部我合计 LSTM 模子最锐利。LSTM 模子是种异常的递归神经网罗,能处理序列展望问题,还处分了圭表 RNN 的消结怨梯度爆炸问题,合乎时候序列展望和当然言语处理这些任务。
接下来,我给全球提供了一个观点考证的准备设施,包括装配Python和PIP、创建模式和文献、树立假造环境以及创建requirements.txt文献。还包括 VSCode的树立文献示例,以及本模式的 GitHub 代码仓库。
而在树立代码的部分,我详实评释了何如导入必要的库和调用 EODHD API’s,并先容了一系列可重用的函数,这些函数用于取得数据、创建序列、取得特征和标的值、缩放特征、取得LSTM模子、进行展望以及评估模子。此外,咱们还参谋了何如使用缓存来减少无用要的API调用和数据重迭加载。
终末,本文展示了何如使用这些函数来熟练和测试LSTM模子,并展示了何如展望下一个往来日的收盘价。通过相比实质收盘价和展望收盘价的图表,以及野心均方过错(MSE)、均方根过错(RMSE)和均齐全过错(MAE)等方针,来评估模子的性能。浅易总结起来即是底下6句话:
LSTM模子在往来展望中的后果优于其他方法,因为它大概更好地处理永久依赖问题。
使用缓存机制不错提高数据处理的效力,幸免重迭的API调用和模子熟练。
通过可视化实质和展望的收盘价,以及野心磋议的过错方针,不错直不雅地评估模子的展望准确性。
模子的熟练和测试应该使用不同的数据集,以确保模子的泛化才调。
调养超参数和使用绝顶的熟练数据不错进一步提高模子的性能。
模子的展望收尾不错当作往来决策的参考,但应严慎使用,因为展望并不老是准确的。
本文内容只是是时间探讨和学习,并不组成任何投资暴戾。
转发请注明原作家和出处。
本站仅提供存储奇迹,系数内容均由用户发布,如发现存害或侵权内容,请点击举报。