MTsocketAPI SDK
The MTsocketAPI SDK is a native library for MetaTrader 4 and MetaTrader 5. Your program connects directly to your broker's MT4 or MT5 server, so there is no terminal to install, update or keep open.
It is available for C#, Python, Java, C++ and Rust, on Windows and Linux (64-bit).
Check your broker first
The SDK works with the MetaTrader 5 brokers that offer the MetaTrader 5 web terminal, and with the MetaTrader 4 servers available in the MetaTrader 4 web terminal of MetaQuotes. Before you buy, try it with a demo account of your broker: demo accounts work without a license.
- MetaTrader 5: you will need the address of your broker's web terminal (it starts with
wss://): this video shows how to find it. - MetaTrader 4: you will need the name of your broker's server, as shown in MetaTrader 4 (for example
MetaQuotes-Demo). You can check that it is available in the MetaTrader 4 web terminal.
If your broker is not available, use the MTsocketAPI EA.
MetaTrader 4 and MetaTrader 5
One library and one license for both platforms, with the same methods:
| MetaTrader 5 | MetaTrader 4 | |
|---|---|---|
| Client | MT5WebClient(url, options) |
MT4WebClient(server, options) |
| You connect with | the address of the web terminal (wss://...) |
the name of the server (MetaQuotes-Demo) |
| Options | MT5WebClientOptions |
MT4WebClientOptions |
| Connection modes | Direct, Browser and Hybrid | Direct |
| Market depth | Yes | No (MT4 has none) |
| Pending orders | limit, stop and stop-limit | limit and stop |
Code that works with both platforms can use the common base class, MTWebClient.
What you can do
- No terminal. Your program connects straight to your broker's MetaTrader 4 or 5 server. Nothing to install, update or keep open.
- Many accounts, one program. Open as many connections as you need in the same process: copy trading, hedging, monitoring, account managers.
- Windows and Linux. The same code runs on your PC, on a Linux VPS or inside a Docker container.
- Typed in 5 languages. C#, Python, Java, C++ and Rust, with typed models and auto-completion in your IDE. No JSON to parse.
- Real-time prices. Ticks (and market depth in MT5) pushed to your code as events, with very low latency and low CPU usage.
- Orders. Market, limit and stop orders, modify SL/TP, partial and full close, with the broker's result code.
- History and candles. Deals, orders and positions history, and OHLC candles in any timeframe from M1 to MN1.
- Economic calendar. The MQL5 economic calendar with actual, forecast and previous values, in UTC.
Connection modes
By default the SDK speaks the protocol of the web terminal itself (Direct mode: the fastest, with every feature). With MetaTrader 5 it can also run the official web terminal in a hidden browser (Browser and Hybrid modes): slower, but they can keep working if MetaQuotes changes the protocol. You choose the mode with one option; see Connection modes. MetaTrader 4 connects in Direct mode.
EA or SDK?
Every plan includes both products. They are two different ways to reach MetaTrader from your code:
| MTsocketAPI EA | MTsocketAPI SDK | |
|---|---|---|
| MetaTrader platform | MT4 and MT5 | MT4 and MT5 |
| Brokers | Any MT4/MT5 broker | MT5 brokers that offer the MetaTrader 5 web terminal, and MT4 servers available in the MetaTrader 4 web terminal. Try it first with a demo account |
| MetaTrader terminal needed | Yes, the EA runs inside it (Windows, or Wine) | No |
| How your code talks to it | REST API and WebSockets (TCP port 81) | Native library called from your code |
| Languages | Any language that speaks HTTP | C#, Python, Java, C++ and Rust (typed) |
| Where your program runs | Any system: on the same computer as the terminal, or from Linux, macOS, Android... through the EA's IP (remote connections: Professional plan) | Windows and Linux (64-bit): PC, VPS, Docker |
| Several accounts | One terminal per account. Need many accounts? Contact us | Many accounts in one program (Unlimited plan) |
| Remote access | Yes (Professional and Unlimited) | Not needed: your program connects from anywhere |
| Indicators (SMA, ATR, custom...) | Yes, from the terminal | No: compute them from the candles |
| Economic calendar | Yes | Yes |
| Best for | Existing terminals, any language, indicators | Servers, VPS, Docker, multi-account, low resources |
A first look
using MtSocketApi;
var client = new MT5WebClient("wss://web.metatrader.app/terminal",
new MT5WebClientOptions { LicenseKey = "YOUR-LICENSE-KEY" });
await client.ConnectAsync();
await client.LoginAsync(111111, "mypass");
Account account = await client.AccountInfoAsync();
Console.WriteLine($"Balance: {account.Balance} {account.Currency}");
var symbols = await client.LoadSymbolsAsync();
SymbolEntry eurusd = symbols.Find(s => s.Name == "EURUSD")!;
client.TickReceived += (_, t) => Console.WriteLine($"EURUSD {t.Bid} / {t.Ask}");
await client.SubscribeAsync(eurusd);
TradeResult r = await client.OpenMarketOrderAsync(eurusd, Side.Buy, 0.01);
Console.WriteLine($"{r.Retcode} {r.RetcodeText} at {r.Price}");
from mtsocketapi import MT5WebClient, MT5WebClientOptions, Side
client = MT5WebClient("wss://web.metatrader.app/terminal",
MT5WebClientOptions(license_key="YOUR-LICENSE-KEY"))
client.connect()
client.login(111111, "mypass")
account = client.account_info()
print(f"Balance: {account.balance} {account.currency}")
eurusd = next(s for s in client.load_symbols() if s.name == "EURUSD")
client.on_tick = lambda t: print("EURUSD", t.bid, t.ask)
client.subscribe([eurusd])
r = client.open_market_order(eurusd, Side.BUY, 0.01)
print(r.retcode, r.retcode_text, "at", r.price)
import com.mtsocketapi.*;
MT5WebClient client = new MT5WebClient("wss://web.metatrader.app/terminal",
new MT5WebClientOptions().licenseKey("YOUR-LICENSE-KEY"));
client.connect();
client.login(111111L, "mypass");
Account account = client.accountInfo();
System.out.println("Balance: " + account.getBalance() + " " + account.getCurrency());
SymbolEntry eurusd = client.loadSymbols().stream()
.filter(s -> s.getName().equals("EURUSD")).findFirst().get();
client.setOnTick(t -> System.out.println("EURUSD " + t.getBid() + " / " + t.getAsk()));
client.subscribe(eurusd);
TradeResult r = client.openMarketOrder(eurusd, Side.BUY, 0.01);
System.out.println(r.getRetcode() + " " + r.getRetcodeText() + " at " + r.getPrice());
#include <mtsocketapi/mtsocketapi.hpp>
#include <iostream>
using namespace mtsocketapi;
int main() {
MT5WebClientOptions options;
options.license_key = "YOUR-LICENSE-KEY";
MT5WebClient client("wss://web.metatrader.app/terminal", options);
client.connect();
client.login(111111, "mypass");
Account account = client.account_info();
std::cout << "Balance: " << account.balance << " " << account.currency << "\n";
for (const SymbolEntry& s : client.load_symbols())
if (s.name == "EURUSD") {
client.subscribe({s});
TradeResult r = client.open_market_order(s, Side::Buy, 0.01);
std::cout << r.retcode << " " << r.retcode_text << " at " << r.price << "\n";
}
}
use mtsocketapi::*;
fn main() -> Result<()> {
let options = MT5WebClientOptions { license_key: Some("YOUR-LICENSE-KEY".into()), ..Default::default() };
let client = MT5WebClient::new("wss://web.metatrader.app/terminal", options)?;
client.connect()?;
client.login(111111, "mypass")?;
let account = client.account_info()?;
println!("Balance: {} {}", account.balance, account.currency);
let eurusd = client.load_symbols()?.into_iter().find(|s| s.name == "EURUSD").unwrap();
client.on_tick(|t| println!("EURUSD {:?} / {:?}", t.bid, t.ask));
client.subscribe(std::slice::from_ref(&eurusd))?;
let r = client.open_market_order(&eurusd, Side::Buy, 0.01, None, None, "", None)?;
println!("{} {} at {}", r.retcode, r.retcode_text, r.price);
Ok(())
}
For MetaTrader 4, create an MT4WebClient with the name of the server instead: see Examples.