Using Deep Learning to Decode Candlestick Patterns

·

In today’s stock market, investors rely on three primary analysis methods: technical, fundamental, and sentiment (or "chip") analysis. Each approach has its own logic and dedicated followers, and none can be definitively labeled as superior. The choice often depends on an individual trader’s psychology, personality, and discipline. For me, successful investing isn’t just about buying low and selling high—it’s also about timing. A high internal rate of return (IRR) matters as much as overall profit, because returns diminish over time.

Given this focus on timing, I’ve become deeply interested in the daily candlestick chart—a rich source of temporal data. Candlestick pattern analysis examines five key elements: open, close, high, low, and volume. These values reflect the psychological tug-of-war between bulls and bears. Inspired by Japanese rice trader Homma Munehisa, the Sakata Method laid early groundwork for modern candlestick theory. However, markets evolve, and so do patterns. Can deep learning help us decode today’s complex, fast-changing charts?

How LSTM Neural Networks Interpret Market Trends

Long Short-Term Memory (LSTM) networks—a type of recurrent neural network (RNN)—are exceptionally good at processing sequential data. Think of how humans understand context in language:

“I grew up in France. I speak fluent ???”

Even without finishing the sentence, you’d likely guess “French.” That’s because your brain retains relevant past information (France) to predict future outcomes (language). LSTMs mimic this ability by learning which pieces of historical data are important and which can be forgotten.

Candlestick charts work similarly. Price movements are responses to time-based signals:

👉 Discover how AI models analyze financial patterns using real-time data.

The goal of an LSTM model is to identify such meaningful sequences in historical price data and learn how they correlate with future movements.

Building a Stock Prediction Model with LSTM

To test this idea, we’ll use daily stock data from Hon Hai Precision (Foxconn, ticker: 2317) from 2013 to 2017. The dataset includes open, high, low, close prices, and trading volume.

Step 1: Load and Clean the Data

We begin by loading the data into a Pandas DataFrame and removing any rows with missing values:

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

Step 2: Normalize the Data

Since neural networks perform better with uniformly scaled inputs, we apply Min-Max normalization:

from sklearn import preprocessing

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

foxconndf_norm = normalize(foxconndf)

Step 3: Prepare Training and Testing Sets

We define a time window (e.g., 20 days) and structure the data so each input contains 20 consecutive days of normalized data, with the 21st day’s closing price as the target:

import numpy as np

def data_helper(df, time_frame):
    number_features = len(df.columns)
    datavalue = df.values
    result = []

    for index in range(len(datavalue) - (time_frame + 1)):
        result.append(datavalue[index: index + (time_frame + 1)])

    result = np.array(result)
    train_size = int(0.9 * result.shape[0])

    x_train = result[:train_size, :-1]
    y_train = result[:train_size, -1, -1]

    x_test = result[train_size:, :-1]
    y_test = result[train_size:, -1, -1]

    x_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1], number_features))
    x_test = np.reshape(x_test, (x_test.shape[0], x_test.shape[1], number_features))

    return [x_train, y_train, x_test, y_test]

X_train, y_train, X_test, y_test = data_helper(foxconndf_norm, 20)

Step 4: Build the LSTM Model

Using Keras, we construct a model with two LSTM layers (256 units each), dropout for regularization, and dense layers for final output:

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

def build_model(input_length, input_dim):
    model = Sequential()
    model.add(LSTM(256, input_shape=(input_length, input_dim), return_sequences=True))
    model.add(Dropout(0.3))
    model.add(LSTM(256, return_sequences=False))
    model.add(Dropout(0.3))
    model.add(Dense(16, activation='relu'))
    model.add(Dense(1, activation='linear'))
    model.compile(loss='mse', optimizer='adam', metrics=['accuracy'])
    return model

model = build_model(20, 5)

Step 5: Train and Predict

We train the model over 50 epochs with a batch size of 128:

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

After training, we make predictions and reverse the normalization to compare actual vs predicted prices:

def denormalize(df, norm_value):
    original_value = df['close'].values.reshape(-1,1)
    min_max_scaler = preprocessing.MinMaxScaler()
    min_max_scaler.fit_transform(original_value)
    return min_max_scaler.inverse_transform(norm_value.reshape(-1,1))

pred = model.predict(X_test)
denorm_pred = denormalize(foxconndf, pred)
denorm_ytest = denormalize(foxconndf, y_test)

Evaluating the Results

When plotting the predicted (red) versus actual (blue) prices:

import matplotlib.pyplot as plt
plt.plot(denorm_pred, color='red', label='Prediction')
plt.plot(denorm_ytest, color='blue', label='Actual')
plt.legend()
plt.show()

We observe that while the overall trend is somewhat captured, the predictions often lag behind real movements by several days—a critical flaw for trading strategies.

👉 See how advanced algorithms improve predictive accuracy in volatile markets.

Improving Model Performance

To close this gap, consider tuning:

Even small changes can significantly boost performance.

Frequently Asked Questions

Q: Can LSTM accurately predict stock prices?
A: LSTMs can capture trends and patterns but cannot guarantee precise predictions due to market randomness and external factors.

Q: Is deep learning suitable for day trading?
A: While possible, latency and overfitting risks make it challenging. It's better suited for medium-term forecasting.

Q: What other models work well with financial time series?
A: GRUs (Gated Recurrent Units), Transformers, and hybrid models combining CNNs with LSTMs show promise.

Q: How much historical data is needed?
A: At least several years of daily data are recommended to capture different market cycles.

Q: Are there risks in relying solely on AI for trading?
A: Yes—models may fail during black swan events or structural market shifts. Always combine with risk management.

👉 Explore tools that integrate machine learning with real-world trading execution.

Core Keywords

By leveraging deep learning techniques like LSTM, traders can move beyond traditional chart reading and tap into powerful pattern recognition tools—ushering in a new era of data-driven investing.