Skip to content

Getting started

1. Download the SDK

Download the package from the download page and unzip it. In this guide, <package> is the folder where you unzipped it.

native/
  win-x64/     mtsocketapi_native.dll, mtsocketapi_native.dll.lib   (Windows)
  linux-x64/   libmtsocketapi_native.so                              (Linux)
bindings/
  csharp/      MtSocketApi library + ExampleMT5 and ExampleMT4 (Visual Studio solution MtSocketApi.sln)
  python/      mtsocketapi package + example_mt5.py and example_mt4.py
  java/        com.mtsocketapi classes + example/ExampleMT5.java and example/ExampleMT4.java
  cpp/         header-only C++17 wrapper + example/mt5.cpp and example/mt4.cpp (CMake)
  rust/        mtsocketapi crate + examples/example_mt5.rs and examples/example_mt4.rs
include/
  mtsocketapi.h   C API (for other languages)

The native folder contains the compiled library; each language uses it through its binding. Every example finds it there automatically.

2. Get your broker's details

You need three things:

  • the server:
    • MetaTrader 5: the address of your broker's MetaTrader 5 web terminal (a WebSocket address that starts with wss://; this video shows how to find it),
    • MetaTrader 4: the name of your broker's server, as shown in the login window of MetaTrader 4 (for example MetaQuotes-Demo),
  • your account number,
  • your password.

The examples ask for them when they start; you do not have to edit anything. Your password is sent only to your broker.

3. Run the example

Every language has two examples: one for MetaTrader 5 (ExampleMT5, example_mt5...) and one for MetaTrader 4 (ExampleMT4, example_mt4...). Run the one of your platform. Both do the same:

  1. Ask for the server, the account number and the password (hidden in C#, Java and Python), then connect and log in. Without a license, demo accounts work in demo mode.
    • MetaTrader 5: the web terminal address. Enter keeps wss://web.metatrader.app/terminal.
    • MetaTrader 4: the name of the trade server. Enter keeps MetaQuotes-Demo.
  2. Show the connection mode (MT5) or the server (MT4), the license state, the account balance and the open positions.
  3. Subscribe to EURUSD, open a 0.01-lot market order and close it (press Enter between steps).
  4. Show the last 5 positions of the last 5 days and the number of H1 candles of the last 3 days.
  5. Show the high-impact economic calendar events of the last 24 hours.

If the connection is lost, the example tries to reconnect every 10 seconds until it works, and shows the time of every disconnection and attempt.

Use a demo account

The example sends a real order to the account you log in to. Run it with a demo account.

The commands below are for Windows PowerShell; the Linux equivalent is given when it differs.

Requirements: Visual Studio 2022 (workload ".NET desktop development"), or the .NET 8 SDK (winget install Microsoft.DotNet.SDK.8; Linux: install .NET).

Visual Studio: open bindings\csharp\MtSocketApi.sln and press F5. It runs ExampleMT5. For MetaTrader 4, right-click ExampleMT4 → Set as Startup Project, then F5.

Command line:

cd <package>\bindings\csharp
dotnet run --project ExampleMT5     # MetaTrader 5
dotnet run --project ExampleMT4     # MetaTrader 4

The MtSocketApi project copies the native library from native\ next to the program by itself.

Your own project: reference bindings\csharp\MtSocketApi\MtSocketApi.csproj (or the MtSocketApi.dll it builds) and make sure mtsocketapi_native.dll (Linux: libmtsocketapi_native.so) is in the same folder as your program.

Requirements: Python 3 (winget install Python.Python.3.12; Linux: usually installed). No other package is needed.

cd <package>\bindings\python
python example_mt5.py     # MetaTrader 5
python example_mt4.py     # MetaTrader 4

Linux: python3 example_mt5.py or python3 example_mt4.py.

Your own scripts: copy the mtsocketapi folder next to your script (or add bindings/python to PYTHONPATH) and copy the native library into the mtsocketapi folder: mtsocketapi_native.dll on Windows, libmtsocketapi_native.so on Linux (both can be there at the same time).

Requirements: a JDK (winget install EclipseAdoptium.Temurin.21.JDK; Linux: sudo apt install openjdk-21-jdk) and the JNA library (one jar, downloaded below).

cd <package>\bindings\java
mkdir lib, out -Force
Invoke-WebRequest https://repo1.maven.org/maven2/net/java/dev/jna/jna/5.14.0/jna-5.14.0.jar -OutFile lib\jna-5.14.0.jar
$files = (Get-ChildItem -Recurse src\main\java -Filter *.java).FullName
javac -encoding UTF-8 -cp lib\jna-5.14.0.jar -d out $files
java --enable-native-access=ALL-UNNAMED -cp "out;lib\jna-5.14.0.jar" com.mtsocketapi.example.ExampleMT5   # MetaTrader 5
java --enable-native-access=ALL-UNNAMED -cp "out;lib\jna-5.14.0.jar" com.mtsocketapi.example.ExampleMT4   # MetaTrader 4

Linux:

cd <package>/bindings/java
mkdir -p lib out
curl -L -o lib/jna-5.14.0.jar https://repo1.maven.org/maven2/net/java/dev/jna/jna/5.14.0/jna-5.14.0.jar
javac -encoding UTF-8 -cp lib/jna-5.14.0.jar -d out $(find src/main/java -name "*.java")
java --enable-native-access=ALL-UNNAMED -cp "out:lib/jna-5.14.0.jar" com.mtsocketapi.example.ExampleMT5   # MetaTrader 5
java --enable-native-access=ALL-UNNAMED -cp "out:lib/jna-5.14.0.jar" com.mtsocketapi.example.ExampleMT4   # MetaTrader 4

Notes:

  • After editing ExampleMT5.java or ExampleMT4.java (in src\main\java\com\mtsocketapi\example), run javac and java again.
  • Java 8 to 21: remove --enable-native-access=ALL-UNNAMED.
  • Account numbers above 2147483647 need the L suffix in Java: long login = 10013224806L;.
  • With Maven or Gradle, add the dependency net.java.dev.jna:jna:5.14.0 (see pom.xml). To find the native library from anywhere, run with -Dmtsocketapi.lib=<full path of the dll or so>.

Requirements:

  • Windows: Visual Studio 2022 with the workload "Desktop development with C++" (it includes CMake).
  • Linux: sudo apt install g++ cmake.

Windows, in the "Developer PowerShell for VS 2022" (Start menu):

cd <package>\bindings\cpp
cmake -S . -B build
cmake --build build --config Release
.\build\Release\example_mt5.exe     # MetaTrader 5
.\build\Release\example_mt4.exe     # MetaTrader 4

Linux:

cd <package>/bindings/cpp
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build
./build/example_mt5     # MetaTrader 5
./build/example_mt4     # MetaTrader 4

After editing example\mt5.cpp or example\mt4.cpp, run cmake --build build --config Release again. In Visual Studio you can also use "File → Open → Folder…" on bindings\cpp, choose example_mt5.exe or example_mt4.exe as the startup item and run it with ▶.

Your own project: add bindings\cpp\include to the include folders, link with mtsocketapi_native.dll.lib (Linux: libmtsocketapi_native.so) and put the DLL next to your program. Then #include <mtsocketapi/mtsocketapi.hpp>.

Requirements: Rust (rustup.rs). On Windows, rustup asks for the Visual Studio C++ build tools.

cd <package>\bindings\rust
cargo run --example example_mt5     # MetaTrader 5
cargo run --example example_mt4     # MetaTrader 4

Your own project: add the dependency mtsocketapi = { path = "<package>/bindings/rust" } and put the native library next to your program (or set the environment variable MTSOCKETAPI_LIB to its full path).

4. Add your license key

Without a license only demo accounts can be used, with up to 1 lot per order, 5 symbols per subscription and 30 days of history. To use a license, add your key to the options of the client, as the comment next to it in the example shows. See Licensing.

5. Choose a connection mode (optional, MetaTrader 5)

In the MT5 example, the mode variable near the top selects Direct (the default), Browser or Hybrid. Direct is the fastest and needs nothing else; see Connection modes for when the other two are useful. MetaTrader 4 always connects in Direct mode.