Skip to content

🧰 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 ServerName or Host must be set in appsettings.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 ServerName or Host; leave the other null/empty.
  • DefaultSymbol is 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 OperationCanceledException when 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 defined after 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.