Your first MT4 console workflow.
Connect. Read. Open a demo trade. Close it. Disconnect.
Step-by-step transcript
- 0:00
Your first MT4 console workflow.
This tutorial shows how to connect to MT4, read your account, open and close a demo trade, and disconnect.
- 0:10
Open the MT4 console.
Open M T four dot M R P C dot pro. The console opens automatically. Getting started gives you a guided setup, a searchable list of methods, tutorials, and developer resources. For this walkthrough, select ConnectEx in the sidebar.
- 0:30
Choose your API key.
The API key identifies your access to Meta R P C. Use TRIAL for a trial session, or enter your own API key in the header. Trial capacity and limits apply. Keep your personal key private. It is different from the terminal identifier returned after connecting.
- 0:51
Connect your demo account.
In ConnectEx, enter the demo account login, its trading password, and the exact broker server name. This example uses Fresh Forex Demo. Leave the optional id field blank. Then click Execute Request and wait for the connection result. The account details shown in this recording are hidden.
- 1:13
Keep the terminal identifier.
Read the live response. HTTP Status Code two hundred means the API returned a response. Check error first. A successful connection returns data dot terminal Instance Guid. The console keeps this terminal identifier in the header and uses it as the id header on subsequent requests. Do not replace your API key with it.
- 1:39
Read the account.
Select AccountSummary and click Execute Request. Read the returned account data, including balance, equity, currency, and trade mode. This recording uses a demo account with trading enabled. Confirm that you are working with your intended account before sending any order.
- 1:59
Prepare a small demo trade.
Select OrderSend. Enter a symbol available on your broker account. Here we use B T C U S D, choose O C O P Buy, and set volume to zero point zero one lots. The symbol and minimum volume can differ between brokers. Use the symbol properties to check what your account supports.
- 2:21
Send once. Read the result.
Click Execute Request once. In this successful MT4 response, data dot ticket identifies the new trade. Data dot volume is zero point zero one, and data dot price shows the opening price. Save the ticket for the next steps. If an error is returned, read the error details before deciding what to do next.
- 2:45
Inspect the open position.
Select OpenedOrders and execute the request. Find the ticket returned by OrderSend in data dot order Infos. Check its symbol, order type, lots, and opening price. In this example there is one B T C U S D buy position. The live position table also lets you inspect open positions.
- 3:08
Close the recorded position.
To close this trade through the API, select OrderCloseDelete. Enter the ticket from the successful OrderSend response and set lots to zero point zero one, the full volume of this position. Leave closing price blank to use the current quote. Check the ticket and volume, then click Execute Request.
- 3:31
Check the close result.
The response reports no error, with mode O C D Market Order. This is the market-order close result. Now check OpenedOrders again to see whether any volume remains. This is especially useful when you request a partial close. Here we requested the full volume.
- 3:51
Verify cleanup.
Run OpenedOrders again. Our response contains an empty order Infos list, so there are no open positions or pending orders left on this demo account. An empty list is a valid successful result. The trade we created has been closed.
- 4:10
Disconnect when finished.
Finally, select Disconnect and execute the request with your current terminal id. Check that no error is returned. The terminal is now disconnected. Disconnecting alone does not close positions, which is why we closed the trade and checked OpenedOrders first.
- 4:30
Use the same steps in your code.
You have completed the MT4 console workflow. Use the code snippets as a starting point for your application, keep your API key private, and read the result of every request. For troubleshooting, open the method's result guide or watch the MT4 error-handling tutorial.