Using Deep Learning to Analyze Candlestick Charts for Stock Market Predictions

·

In today's stock market, there are three primary analytical methods: technical analysis, fundamental analysis, and market sentiment analysis. Each approach offers unique strategies for stock selection, with none being inherently superior—the choice depends on an investor's mindset, personality, and discipline. Personally, I believe that successful investing hinges not only on buying wisely (high ROI) but also on selling promptly (high IRR), as returns diminish over time.

For those who prioritize the impact of time on stock prices, the secrets hidden within daily candlestick charts are worth exploring. Candlestick pattern analysis delves into five key elements of each candlestick: opening price, closing price, high, low, and trading volume, which reflect the psychological battle between bulls and bears.


Long Short-Term Memory (LSTM) Neural Networks

LSTM networks, a type of Recurrent Neural Network (RNN), excel at processing and predicting time-series data. Consider this sentence:

"I grew up in France and speak fluent ??."

From the context, LSTM infers "French" by linking long-term memory ("France") and short-term cues ("speak"). Similarly, stock prices react to key signals over time:

LSTM identifies these patterns within a timeframe and learns subsequent price movements.


Implementing LSTM for Stock Price Prediction

Dataset: Daily OHLCV (Open-High-Low-Close-Volume) data for Hon Hai Precision (2317) from 2013–2017.

Step 1: Data Loading and Cleaning

import pandas as pd
foxconndf = pd.read_csv('./foxconn_2013-2017.csv', index_col=0)
foxconndf.dropna(how='any', inplace=True)

Step 2: Normalization

from sklearn import preprocessing
def normalize(df):
    newdf = df.copy()
    min_max_scaler = preprocessing.MinMaxScaler()
    for col in ['open', 'high', 'low', 'close', 'volume']:
        newdf[col] = min_max_scaler.fit_transform(df[col].values.reshape(-1,1))
    return newdf
foxconndf_norm = normalize(foxconndf)

Step 3: Train-Test Split with Time-Frame

def data_helper(df, time_frame=20):
    datavalue = df.as_matrix()
    result = []
    for index in range(len(datavalue) - (time_frame+1)):
        result.append(datavalue[index: index + (time_frame+1)])
    result = np.array(result)
    split_idx = round(0.9 * result.shape[0])
    x_train = result[:split_idx, :-1]
    y_train = result[:split_idx, -1][:,-1]
    x_test = result[split_idx:, :-1]
    y_test = result[split_idx:, -1][:,-1]
    return x_train, y_train, x_test, y_test
X_train, y_train, X_test, y_test = data_helper(foxconndf_norm)

Step 4: LSTM Model Architecture

from keras.models import Sequential
from keras.layers import LSTM, Dense, Dropout
model = Sequential()
model.add(LSTM(256, input_shape=(20, 5), return_sequences=True))
model.add(Dropout(0.3))
model.add(LSTM(256, return_sequences=False))
model.add(Dense(16, activation='relu'))
model.add(Dense(1, activation='linear'))
model.compile(loss='mse', optimizer='adam')

Step 5: Training and Prediction

model.fit(X_train, y_train, batch_size=128, epochs=50, validation_split=0.1)
pred = model.predict(X_test)

Step 6: Denormalization and Visualization

denorm_pred = denormalize(foxconndf, pred)
plt.plot(denorm_pred, color='red', label='Prediction')
plt.plot(denorm_ytest, color='blue', label='Actual')
plt.legend(); plt.show()

Initial Result: Predicted prices (red) lagged behind actual prices (blue). Tweaking parameters (time-frame, batch size, epochs) improved accuracy.

👉 Master LSTM for trading with this advanced guide


FAQs

Q1: Can LSTM predict sudden market crashes?
A: While LSTM detects patterns, black swan events remain unpredictable due to their outlier nature.

Q2: How much historical data is ideal?
A: 3–5 years of daily data balances relevance and computational efficiency.

Q3: Does volume data improve predictions?
A: Yes, volume confirms trend strength—critical for validating breakout signals.


Key Takeaways

👉 Explore real-world LSTM applications in finance