KazaTrade MT5 library: function reference
The KazaTrade MT5 library (mt5api.dll) lets your .NET code log in to a MetaTrader 5 account, read quotes, place and manage orders, and read history. Everything starts with one class, MT5API, in the namespace mtapi.mt5.
This reference lists the public functions, properties and events. It is short on purpose. Each entry has the signature, what it does, the parameters, the return value, the errors it throws, and an example where one helps.
Install
- Download
mt5api.dllfrom your Developer page (/app/dll). Pick the build for your runtime:net48for .NET Framework 4.8 (Windows)netstandard2.0for librariesnet8.0for .NET 8 (Linux and Windows)
- Add a reference to the DLL in your project:
<ItemGroup>
<Reference Include="mt5api">
<HintPath>libs\mt5api.dll</HintPath>
</Reference>
</ItemGroup>- Add
using mtapi.mt5;to your code.
Download the DLL from your Developer page (/app/dll). The page shows each file's SHA-256: check that your download matches it before you use the file (for example sha256sum mt5api.dll or Get-FileHash mt5api.dll).
The DLL has no key inside. Copy your key from your Developer page and set api.ApiKey before Connect. Keep the key secret, like a password. Without it, Connect fails with KazaTrade: no ApiKey set.
First call
using System;
using mtapi.mt5;
var api = new MT5API(12345678, "password", "broker.example.com", 443);
api.ApiKey = "your-key-from-the-developer-page";
api.Connect();
Console.WriteLine($"Balance: {api.Account.Balance} {api.AccountCurrency}");
Quote q = api.GetQuote("EURUSD", 5000);
Console.WriteLine($"EURUSD {q.Bid} / {q.Ask}");
api.Disconnect();The same call with a broker server name instead of host and port:
var api = new MT5API(12345678, "password", "Broker-Server");
Good to know
- Lots are
double(0.10is one tenth of a lot). The library sends them to the server with eight decimals. - Most calls come in two forms: a blocking one (
Connect) and an async one (ConnectAsync,OrderSendAsyncTask). Use the async form in services. - Events run on a worker thread. Do not block inside a handler.
- Times on
OrderandQuoteare broker server time.ServerTimegives the current server time. - Keep one
MT5APIobject per account. Reuse it. Do not log in again for every request.
Index
| Page | What is in it |
|---|---|
| connect.md | Constructors, Connect, Disconnect, settings, proxy, server and cluster info |
| account.md | Account, balance, equity, margin, trading permissions, mail, demo account |
| symbols-and-quotes.md | Symbols, subscribe, quotes, market watch, tick value, bars, sessions |
| orders.md | OrderSend, OrderClose, OrderModify, async forms, enums |
| positions-and-history.md | Open positions, closed orders, order history, equity history |
| events.md | All events and the types they carry |
| errors.md | Exception types, ServerException.Code, common messages |