Candlestick-Based Trend Prediction Strategy

πŸ“ˆ Trading Strategy & Plan

This project proposes a visual-based trend prediction trading strategy using Japanese candlestick patterns and a convolutional neural network (CNN). The approach is inspired by the methodology of Mersal et al. (2024), with key adaptations to suit our dataset and simplified implementation.

πŸ” Overview of the Strategy

We use a sliding window approach to generate candlestick chart segments (5 candles per window). For each window:

  • Identify patterns using ta-lib
  • Use CNN to classify pattern as bullish or bearish
  • Based on the predicted trend, enter a trade on the next candle

🚦 Trade Walkthrough: A Single Trade

Step 1: Entry Signal

  • A 5-candle sliding window is fed into the trained CNN model.
  • CNN outputs: Uptrend (probability: 0.91)
  • Entry Condition:
    • Predicted trend = β€œUptrend”
    • The last candle in the window closes above the 20-period Simple Moving Average (SMA20)

βœ… Enter a Long Position (Buy) at the opening of the next candle

Step 2: Exit Signal

  • Exit after 3 candles (fixed holding period), OR:
  • If CNN prediction flips to Downtrend in an overlapping future window

Step 3: Stop Loss / Risk Rule

  • Place a stop loss at 1.5 Γ— ATR(14) below the entry price
  • No more than 2% of portfolio risked per trade

⛓️ Additional Rules

  • Only one active trade at a time
  • No trades during low-volume periods (overnight hours or holidays)

πŸ“… Backtest Setup

  • Data: EUR/USD 15-min OHLC from 2020–2024
  • Technical Indicators: SMA, ATR
  • Pattern Recognition: ta-lib 61-patterns
  • Model: 3-layer CNN with image input of size 150Γ—150

πŸ“Š Data Sample

Here is a preview of the candlestick-based dataset used for training the model.

import pandas as pd
from IPython.display import display, HTML

# Sample candlestick data
data = pd.DataFrame({
    "Open": [1.0854, 1.0861, 1.0870, 1.0878, 1.0884],
    "High": [1.0862, 1.0875, 1.0889, 1.0894, 1.0897],
    "Low": [1.0845, 1.0852, 1.0859, 1.0865, 1.0871],
    "Close": [1.0860, 1.0872, 1.0885, 1.0891, 1.0893],
    "Pattern": ["Hammer", "Doji", "Bullish Engulfing", "Morning Star", "Shooting Star"]
})

# Pretty HTML table
html_table = data.to_html(index=False, border=1, justify="center", classes="table table-striped")
display(HTML(html_table))
Open High Low Close Pattern
1.0854 1.0862 1.0845 1.0860 Hammer
1.0861 1.0875 1.0852 1.0872 Doji
1.0870 1.0889 1.0859 1.0885 Bullish Engulfing
1.0878 1.0894 1.0865 1.0891 Morning Star
1.0884 1.0897 1.0871 1.0893 Shooting Star