Tuesday, August 12, 2025

Streaming Live MT5 Quotes in Java Swing with MTsocketAPI

 If you’ve ever wanted to visualize MetaTrader 5 live data outside the MT5 terminal, you’re in luck. With MTsocketAPI and a bit of Java Swing, you can build a real-time quote viewer that streams and displays OHLC (Open, High, Low, Close) data in a sleek, interactive UI.

In this post, we’ll walk through how the Java Swing app works and how it connects to MT5.


The Goal

We want to:

  • Select a trading symbol (Forex, Stocks, Crypto, etc.).

  • Automatically subscribe to its OHLC updates via MTsocketAPI.

  • Display the data in a user-friendly table.

  • Save the streamed data to CSV for analysis.


Architecture Overview

The app follows a simple two-socket model:

  1. Command Socket (Port 77) – Sends commands to MTsocketAPI (e.g., subscribe to a symbol).

  2. Data Socket (Port 78) – Streams incoming OHLC updates in JSON format.

When a user changes the symbol in the dropdown, the app sends a TRACK_OHLC subscription command for that symbol and starts receiving fresh price bars in real-time.


UI Design with Java Swing

The interface is built with JTable for displaying OHLC data and JComboBox for symbol selection. Each symbol displays:

  • DESCRIPTION – Human-friendly name (e.g., Euro vs. United States Dollar).

  • PATH – The MT5 market watch hierarchy path (e.g., Forex\FX-Majors\EURUSD).

Example from the UI:

DESCRIPTION: Euro vs. United States Dollar | PATH: Forex\FX-Majors\EURUSD



Live Data Handling

Once connected:

  • The app reads JSON strings line-by-line from the data socket.

  • It parses TIME, OPEN, CLOSE, HIGH, and LOW values.

  • Swing’s invokeLater is used to safely update the table on the Event Dispatch Thread (EDT).


Saving Data

With a single click, you can export the recorded quotes to a CSV file:

Time,Open,Close,High,Low

2025-08-12 14:00,1.09543,1.09621,1.09712,1.09487

Why This Matters

  • Custom Dashboards – You’re no longer tied to MT5’s UI.

  • Data Export – Store and analyze market history your way.

  • Multi-Symbol Monitoring – Expand the logic to track multiple symbols at once.


Next Steps

This high-level example only scratches the surface. You could:

  • Add charting using JFreeChart or JavaFX Canvas.

  • Handle multiple timeframes simultaneously.

  • Build trading alerts on top of the incoming stream.

With MTsocketAPI + Java Swing, you have the freedom to extend MT5 data into any custom Java-based trading tool you can imagine.

No comments:

Post a Comment

Mastering Unit Testing in Spring Boot – Part 2: Setting Up JUnit 5 & Mockito

  Last time, we learned why testing matters and met our new best friends: JUnit 5 & Mockito. Now it’s time to invite them into our Spri...