Skip to content

Using code examples#

Example code structure#

The working code examples are located in our github repositories:

The examples in all languages are structured in the same way.

  • mt5 folder contains examples for MetaTrader 5 platform
  • mt4 folder contains examples for MetaTrader 4 platform

Please note that the example code for MT4 and MT5 is nearly identical and differs only in code which uploads MT broker server address to MetaApi.

Inside above folders, there are metaApiRpcExample and metaApiSynchronizationExample. First demonstrates RPC API which is good for developing trade monitoring applications similar to myfxbook. Second example demonstrates API which is good for developing trade applications such as trade copiers or trading robots.

Configuring examples#

  1. First you need to install packages. You can do so by issuing following commands:

    • node.js: npm i
    • python: pip install -r requirements.txt
  2. Next, you need to provide the MetaTrader file containing broker server address.

    • there is a short video demonstrating how to capture the file for MetaTrader 5
    • there is a short video demonstrating how to capture the file for MetaTrader 4
  3. Place the file you captured from your MetaTrader platform into the same folder example executable is located at. Rename it to servers.dat for MT5 or broker.srv for MT4.

  4. Specify file location in example code. For node.js fill in the following lines of the code

    • MetaTrader 5

      let serverDatFile = process.env.PATH_TO_SERVERS_DAT || './servers.dat';

    • MetaTrader 4

      let brokerSrvFile = process.env.PATH_TO_BROKER_SRV || './broker.srv';

    Code changes for Python are similar.

  5. Next, you need to supply data for your trading accounts. For node.js fill in the follwing lines of the code:

    let login = process.env.LOGIN || '<put in your MT login here>';
    let password = process.env.PASSWORD || '<put in your MT password here>';
    let serverName = process.env.SERVER || '<put in your MT server name here>';

    You have to replace the text inside quotes with your MT login, MT password and MT server name. Please note that you can use an investor password as well for read-only access.

    Changes you need to make for python examples are similar.

  6. Finally, supply your MetaApi token. To do so first obtain your API token on https://app.metaapi.cloud/token page. Then edit the following line in order to suppoly your API token to the example code:

    let token = process.env.TOKEN || '<put in your token here>';

    You have to replace the text inside quotes with an actual value of your API token.

    Changes you need to make for python examples are similar.

Launching the examples#

In order to launch the examples, use the following commands:

  • node.js: node metaApiRpcExample or node metaApiSynchronizationExample
  • python: python metaApiRpcExample.py or python metaApiSynchronizationExample.py

Resolving problems#

If you messed up with example code and want to restart we recommend you to go to https://app.metaapi.cloud and first delete your test API server and test provisioning profile. Then re-execute the steps above.

If you still have issues, feel free to contact support via online chat.