IMPROVE LSTM模型用于股票交易和加速代码执行

问题描述 投票:0回答:0
我对此有几个问题:

少量图:

随着时间的推移,资本的情节看起来很奇怪。从字面上看,这只是一条线条。我不确定为什么会发生这种情况,但我相信它可能会犯下一些统计问题。可能是由于我正在更新和绘制资本的方式?

  1. 这是第一分钟的怪异情节

这是第二分钟的怪异情节

这是第三分钟的怪异情节

这是第四分钟的怪异情节

这是第五分钟的怪异情节

这是第六分钟的怪异情节

奇怪的是,该情节似乎没有遵循“投资规则”。直观地,情节应从10000开始,并且会有很多波动,而不是单一的直线。

(更新:我看到资本在同一分钟内翻了一番,这似乎很奇怪。我知道这是一个简化的模拟,并且没有考虑到现实世界中的许多因素,但是我是仍然对这个结果感到困惑。 代表时间的图的X轴也显示了所有tick的同一标签。我相信这是因为循环运行得如此之快,以至于单个循环迭代中的所有动作都几乎得到了相同的时间戳,直到一分钟。 我的问题是:

可能导致资本在同一分钟内翻一番?

如何修改代码,以便绘图的X轴标签显示适合每个动作的正确时间?

    执行时间和操作:
  1. 我希望我的代码指定LSTM模型计算的确切时间和确切的操作(买入/出售)。我该如何实现?

逐步执行代码:

代码需要在一分钟的时间内工作才能更新分钟。但是,目前需要超过一分钟的时间。有什么方法可以优化代码以使其更快地运行?
  1. updates: 有了@furas的帮助,这是我的修订代码:

    import numpy as np import pandas as pd import matplotlib.pyplot as plt import matplotlib.dates as mdates from sklearn.preprocessing import MinMaxScaler from tensorflow.keras.models import Sequential from keras.layers import LSTM, Dense import time import requests # Define the stock symbol and your Alpha Vantage API key symbol = 'AAPL' api_key = 'MY_API' # Define the initial capital initial_capital = 10000 capital = initial_capital # Brokerage fee per stock brokerage_fee_per_stock = 0.02 # Minimum brokerage fee min_brokerage_fee = 18 # SEC tax rate sec_tax_rate = 0.0000278 # Create a dataframe to store the capital and actions over time capital_df = pd.DataFrame(columns=['time', 'capital', 'action', 'price']) capital_df.loc[0] = {'time': pd.Timestamp.now(), 'capital': capital, 'action': 'Initial', 'price': 0} # Initialize with initial capital # Define lookback period lookback = 60 # Define the LSTM model model = Sequential() model.add(LSTM(units=50, return_sequences=True, input_shape=(lookback, 1))) model.add(LSTM(units=50)) model.add(Dense(1)) # Compile the LSTM model model.compile(loss='mean_squared_error', optimizer='adam') # Create the plot before the loop plt.figure(figsize=(10, 5)) plt.title('Capital Over Time') plt.xlabel('Time') plt.ylabel('Capital') plt.xticks(rotation=45) plt.gca().xaxis.set_major_locator(plt.MaxNLocator(10)) # Show 10 ticks plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d %H:%M')) # Format the time up to minutes # Initialize a flag for the first iteration and the action first_iteration = True action = 'Initial' while True: start_time = time.time() # Start the timer # Fetch real-time data using Alpha Vantage API url = f'https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol={symbol}&interval=1min&apikey={api_key}' response = requests.get(url) data = response.json() current_price = float(data['Time Series (1min)'][list(data['Time Series (1min)'].keys())[0]]['4. close']) # Preprocess the data scaler = MinMaxScaler(feature_range=(0, 1)) price_data = np.array([current_price for _ in range(lookback)]).reshape(-1, 1) scaled_data = scaler.fit_transform(price_data) # Prepare inputs for the LSTM model inputs = scaled_data.reshape(1, lookback, 1) # Fit the LSTM model model.fit(inputs, np.array([current_price]), epochs=1, batch_size=1, verbose=2) # Make predictions using the trained LSTM model predicted_price = model.predict(inputs) predicted_price = scaler.inverse_transform(predicted_price)[0][0] # Calculate the number of stocks to buy or sell num_stocks = capital // current_price # Calculate the total brokerage fee for buying and selling total_brokerage_fee = max(brokerage_fee_per_stock * num_stocks, min_brokerage_fee) * 2 # Calculate the SEC tax sec_tax = sec_tax_rate * predicted_price * num_stocks # Update capital based on predicted price and record the action if first_iteration: first_iteration = False elif predicted_price > current_price: capital += num_stocks * current_price - total_brokerage_fee - sec_tax action = 'Buy' else: capital -= num_stocks * current_price + total_brokerage_fee + sec_tax action = 'Sell' # Append the current capital and action to the dataframe new_row = {'time': pd.Timestamp.now(), 'capital': capital, 'action': action, 'price': current_price} capital_df = pd.concat([capital_df, pd.DataFrame([new_row])], ignore_index=True) # Update the plot data without creating it all again plt.plot(capital_df['time'], capital_df['capital']) plt.gcf().autofmt_xdate() # Autoformat the time label for better display plt.draw() plt.pause(0.01) # Pause for the plot to update print(f'Current earning: {capital - initial_capital}, Action: {action} at price: {current_price}') # Calculate the elapsed time and wait for the remaining time to complete 60 seconds elapsed_time = time.time() - start_time time.sleep(max(60 - elapsed_time, 0))
  2. 现在是我的问题:

    从第二分钟开始,什么可能导致资本两倍?

如何修改代码以防止这种情况发生?

我使用了IEX Cloud的另一个API,并证明了问题与API无关:

import numpy as np import pandas as pd import matplotlib.pyplot as plt import matplotlib.dates as mdates from sklearn.preprocessing import MinMaxScaler from tensorflow.keras.models import Sequential from keras.layers import LSTM, Dense import time import requests # Define the stock symbol and your IEX Cloud API key symbol = 'AAPL' api_key = 'sk_98e272a5046941a2a4f7c3554bbecce1' # replace with your own API key # Define the initial capital initial_capital = 10000 capital = initial_capital # Brokerage fee per stock brokerage_fee_per_stock = 0.02 # Minimum brokerage fee min_brokerage_fee = 18 # SEC tax rate sec_tax_rate = 0.0000278 # Create a dataframe to store the capital and actions over time capital_df = pd.DataFrame(columns=['time', 'capital', 'action', 'price']) capital_df.loc[0] = {'time': pd.Timestamp.now(), 'capital': capital, 'action': 'Initial', 'price': 0} # Initialize with initial capital # Define lookback period lookback = 60 # Define the LSTM model model = Sequential() model.add(LSTM(units=50, return_sequences=True, input_shape=(lookback, 1))) model.add(LSTM(units=50)) model.add(Dense(1)) # Compile the LSTM model model.compile(loss='mean_squared_error', optimizer='adam') # Create the plot before the loop plt.figure(figsize=(10, 5)) plt.title('Capital Over Time') plt.xlabel('Time') plt.ylabel('Capital') plt.xticks(rotation=45) plt.gca().xaxis.set_major_locator(plt.MaxNLocator(10)) # Show 10 ticks plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d %H:%M')) # Format the time up to minutes # Initialize a flag for the first iteration and the action first_iteration = True action = 'Initial' while True: start_time = time.time() # Start the timer print(f'Start time: {start_time}') # Print the start time # Fetch real-time data using IEX Cloud API url = f'https://cloud.iexapis.com/stable/stock/{symbol}/quote?token={api_key}' response = requests.get(url) data = response.json() current_price = float(data['latestPrice']) # Preprocess the data scaler = MinMaxScaler(feature_range=(0, 1)) price_data = np.array([current_price for _ in range(lookback)]).reshape(-1, 1) scaled_data = scaler.fit_transform(price_data) # Prepare inputs for the LSTM model inputs = scaled_data.reshape(1, lookback, 1) # Fit the LSTM model model.fit(inputs, np.array([current_price]), epochs=1, batch_size=1, verbose=2) # Make predictions using the trained LSTM model predicted_price = model.predict(inputs) predicted_price = scaler.inverse_transform(predicted_price)[0][0] # Calculate the number of stocks to buy or sell num_stocks = capital // current_price # Calculate the total brokerage fee for buying and selling total_brokerage_fee = max(brokerage_fee_per_stock * num_stocks, min_brokerage_fee) * 2 # Calculate the SEC tax sec_tax = sec_tax_rate * predicted_price * num_stocks # Update capital based on predicted price and record the action if first_iteration: first_iteration = False elif predicted_price > current_price: capital += num_stocks * current_price - total_brokerage_fee - sec_tax action = 'Buy' else: capital -= num_stocks * current_price + total_brokerage_fee + sec_tax action = 'Sell' print(f'Action: {action}, Capital: {capital}') # Print the action and the updated capital # Append the current capital and action to the dataframe new_row = {'time': pd.Timestamp.now(), 'capital': capital, 'action': action, 'price': current_price} capital_df = pd.concat([capital_df, pd.DataFrame([new_row])], ignore_index=True) # Update the plot data without creating it all again plt.plot(capital_df['time'], capital_df['capital']) plt.gcf().autofmt_xdate() # Autoformat the time label for better display plt.draw() plt.pause(0.01) # Pause for the plot to update print(f'Current earning: {capital - initial_capital}, Action: {action} at price: {current_price}') # Print the current earning, action, and price # Calculate the elapsed time and wait for the remaining time to complete 60 seconds elapsed_time = time.time() - start_time print(f'Elapsed time: {elapsed_time}') # Print the elapsed time time.sleep(max(60 - elapsed_time, 0))
  1. 任何帮助将不胜感激。

    谢谢你!

  2. 是因为您的买入声明正在增加不准确的添加,这就是为什么您会随机加倍的原因。您将NUM_STOCKS变量定义为资本 /当前价格。在引用的代码中,您基本上说的是Capital + Capital(10000/stock_price * stock_price),因为您是资本,并且初始时间在同一时间序列中,它只是两倍。而且,如果我说对了,您的买入行动仍在继续做同样的事情,这基本上是对数的增加,如果您进行数学,您基本上每分钟都会获得100%的回报,这就是为什么您会得到奇怪的情节点 - 费用和税收。您需要更改更新资本功能,以防止将来。或者,您可以更新计算要购买的股票数量。

    if first_iteration: first_iteration = False elif predicted_price > current_price: capital += num_stocks * current_price - total_brokerage_fee - sec_tax action = 'Buy' else: capital -= num_stocks * current_price + total_brokerage_fee + sec_tax action = 'Sell'

python matplotlib debugging refactoring lstm
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.