π§° Using CSharpMT4 via CLI (No GUI)
This guide shows how to run MetaRPC MT4 (C#) entirely from the terminal β no GUI required. Perfect for developers, ops, and anyone who prefers commandβline control.
π§ Requirements
| Tool/Lib | Purpose |
|---|---|
| .NET 8 SDK | Build & run the console app |
| MetaTrader 4 | Terminal with MetaRPC MT4 plugin enabled |
appsettings.json |
MT4 login, server/host, and default symbol configuration |
| Terminal/PowerShell | Execute all commands from the shell |
β Exactly one of
ServerNameorHostmust be set inappsettings.json.
π Project Structure
CSharpMT4/
βββ docs/
β βββ Account/
β βββ Market Info/
β βββ Orders/
β βββ Streaming/
β βββ index.md
β βββ cli_usage.md # β you are here
β
βββ appsettings.json # MT4Options: credentials & defaults
βββ MetaRPC.CSharpMT4.csproj # Project file
βββ MetaRPC.CSharpMT4.sln # Solution file
βββ Mt4account.cs # MT4Account: connectivity & lowβlevel API
βββ Mt4service.cs # MT4Service: friendly wrappers & demos
βββ Program.cs # Entry point with toggles
β
βββ .github/
βββ bin/
βββ obj/
βββ .gitignore
π§© Example appsettings.json
{
"MT4Options": {
"User": 501401178,
"Password": "***",
"ServerName": "RoboForex-Demo", // OR set "Host": "mt4.mrpc.pro"
"Host": null,
"Port": 443,
"DefaultSymbol": "EURUSD"
}
}
Notes
- Set either
ServerNameorHost; leave the othernull/empty. DefaultSymbolis used as the base chart for the terminal session.
π Running the App
From the repository root:
# Build
dotnet build
# Run
dotnet run --project MetaRPC.CSharpMT4.csproj
If connection succeeds youβll see logs like:
π Connecting to MT4...
β
Connected to MT4 server
=== Account Summary ===
Balance: ..., Equity: ..., Currency: ...
Press Ctrl+C to stop; the app handles graceful cancellation.
π§ͺ Available Functions (by category)
π§Ύ Account
ShowAccountSummary()β prints balance, equity, and currency.
π Market Info
ShowQuote(symbol)β latest bid/ask snapshot.ShowQuotesMany(symbols, timeoutSecondsPerSymbol=5)β first tick per symbol.ShowQuoteHistory(symbol)β last 5 days of H1 OHLC data.ShowAllSymbols()β list all instruments with indices.ShowSymbolParams(symbol)β full trading parameters (digits, volumes, modes, currencies).ShowSymbolInfo(symbol)β lightweight info (digits, spread flag, bid).ShowTickValues(symbols[])β TickValue, TickSize, ContractSize for sizing math.
π¦ Orders
ShowOpenedOrders()β dump currently opened orders.ShowOpenedOrderTickets()β only open order ticket IDs.ShowOrdersHistory()β closed orders (last 7 days, closeβtime DESC).ShowOrderSendExample(symbol)β opens a real order (demo parameters). β οΈCloseOrderExample(ticket)β close/delete by ticket. β οΈCloseByOrderExample(ticket, oppositeTicket)β close with opposite order. β οΈ
π Streaming
ShowRealTimeQuotes(symbol, timeoutSeconds=5)β first arriving tick or timeout.StreamQuotesForSymbolsAsync(symbols[], durationSeconds=10)β live ticks for a fixed period.StreamTradeUpdates()β trade activity stream (demo breaks after first event).StreamOpenedOrderProfits()β floating P/L per open order (interval=1000 ms in demo).StreamOpenedOrderTickets()β current open tickets (interval=1000 ms in demo).
Methods marked β οΈ can place/modify/close trades β even on demo. Use carefully.
π» Enabling/Disabling Examples
Program.cs contains simple toggles:
// β οΈ Real trading operations (leave false unless you know what youβre doing)
private static readonly bool EnableTradingExamples = false;
// Streaming demos (ticks/profits/tickets)
private const bool EnableStreams = true;
You can also comment/uncomment individual calls near the "Market / Symbols" and "Streaming" sections inside Run(...).
π Quick CLI Session
await _service.ShowAccountSummary();
await _service.ShowQuote("EURUSD");
await _service.ShowAllSymbols();
await _service.ShowQuotesMany(new[] { "EURUSD", "GBPUSD", "USDJPY" });
// Live tick for 5 seconds max
await _service.ShowRealTimeQuotes("EURUSD", timeoutSeconds: 5);
Expected tick output format:
Tick: EURUSD 1.09876/1.09889 @ 2025-08-21 21:23:05
π§ Tips
- Timeouts & cancellation: streaming demos intentionally end with
OperationCanceledExceptionwhen the token fires β thatβs normal and handled by logs. - Base chart:
ConnectByServerNameAsync(..., baseChartSymbol: "EURUSD")ensures the terminal opens a chart that can emit ticks immediately. - Logging: uses
Microsoft.Extensions.Logging.Console. Adjust verbosity via filters if needed. - Culture/formatting: keep raw doubles for math; format only for display.
β Troubleshooting
MSB1009: The project file does not existβ run from repo root or pass the correct path to.csproj.Charts ids not definedafter a short tick stream β the service closes the temporary chart when cancelling; itβs safe. Avoid reusing the same stream instance after cancellation; start a new call.not connected/ connection retries β check credentials,ServerName/Host, and network reachability to the MT4 gateway.
This console app is a minimal, fast, and scriptable way to interact with MT4: discover instruments, read quotes, place/close orders, and subscribe to live streams β all from your terminal.