Automated Trading using MetaTrader

Introduction

In this article, we will look at how to do automated trading using MetaTrader. We will show how to connect MetaTrader to our AutoTrader software & trade on any Indian trading platforms like Upstox, Zerodha Kite, NOW, NEST, ODIN etc.

You do NOT need to purchase any API from your stock broker.

Prerequisite

You need to install AutoTrader software.
You need to install MetaTrader software (free version is sufficient).

Integration

Part I

  1. All functions provided by AutoTrader are defined in algotrader-util.mqh header file. This file is available at:
    1. MetaTrader 4: C:\autotrader\scripts\mt4\Include
    2. MetaTrader 5: C:\autotrader\scripts\mt5\Include
  2. Open MetaTrader, press F4 to open MetaEditor. Copy above file in the include folder as seen in below picture:

Part II

AutoTrader communicates with other software using csv files. The default path for this is: C:\autotrader\data\order. But MetaTrader does not all access to folders outside its working directory. Hence we need to change this path to Common working directory. It can be changed in AutoTrader settings:

Example: A path on our test machine looks like below. Note that you must create a folder called autotrader in your MetaTrader common files folder. Details of this common path can found here.

C:\Users\PRITESH\AppData\Roaming\MetaQuotes\Terminal\Common\Files\autotrader

IPC_FILES_PATH

Here is how to change the setting in AutoTrader:

  1. Click on AutoTrader menu (Settings -> AutoTrader)
  2. Find setting IPC_FILES_PATH & click on it
  3. Click Edit button
  4. Put new path in Value field
  5. Click Update
IPC_FILES_CHARSET

Sometimes the default charset does not work. As per our tests, we have tested with following IPC_FILES_CHARSET setting to value:

  • MetaTrader 4: UTF-8
  • MetaTrader 5: UTF-16LE

Part III

Now you are all set to use AutoTrader functions in your MQL strategy code. All you have to do is include this algotrader-util.mqh file into your strategy code:

#include <algotrader-util.mqh>

Here are some examples of the functions in use:

Example 1:

string id = placeOrderAdvanced(
NSE, EQ, “SBIN”, BUY, INTRADAY, LIMIT, 10,
299.2, 199.4, 5, defaultOptionType(), defaultStrikePrice(),
defaultExpiry(), “CLIENT001”, defaultValidity(), defaultTraderType(),
defaultMarketProtectionPct(), -1, “comments”);

Print(“Order Id: “, id);

Example 2:

string id = placeBracketOrder(NSE, “SBIN”, BUY, 2, 280.5, 1.5, 2,0, true);

Print(“Order Id: “, id);

Example 3:

string id = placeCoverOrder(NSE, “SBIN”, BUY, 2, 280.5, 1.5, 2,0, true);

Print(“Order Id: “, id);

Example 4:

PlaceOrderMultiClientResult result;
string clientsFile = “autotrader\\clients.csv”;
placeOrderAdvancedMultiClient(result, clientsFile,
NSE, EQ, “SBIN”, BUY, INTRADAY, LIMIT, 10,
299.2, 199.4, 5,
defaultOptionType(), defaultStrikePrice(), defaultExpiry());

Print(“Result = “, result.result);
Print(“Order Ids:”);

Example 5:

string id = placeOrderUsingParams(AT_EXCHANGE, AT_SYMBOL, BUY,
AT_ORDER_TYPE, AT_QUANTITY, 274.3, 0, 1);
Print(“Order Id: “, id);

// If the order is remains open till 11:30 am then cancel it automatically
cancelOrderAtTime(id, 11, 30, 0);

Example 6:

Print(“Equity Margin Available = “, getMarginAvailable());
Print(“Portfolio M2M = “, getPortfolioM2M());
Print(“Buy Price = “, getPositionBuyPrice(“SBIN”));
Print(“PnL = “, getPositionPNL(“SBIN”));
Print(“Order Status = “, getOrderStatus(“1544192677-29397”));

Note: We do not have MetaTrader experts & hence we cannot provide any sample strategies.

References

For a list of available functions, please see AutoTrader Functions.

Conclusion

Integrating with MetaTrader is just a matter couple of settings changes & you are good to do automated trading from MetaTrader on supported trading terminals from Indian brokers.

After spending more than 12 years developing Financial Software, I took a break from my career to start my own algorithmic trading setup. This new journey started well and along with my own proprietary trading systems, I also started building automation systems for retail traders, fund managers and HNIs. I was employed with firms like Morgan Stanley, HSBC & SunGard. I work on various technologies, but my strengths are building server side cloud based algorithmic trading systems built primarily using Java programming language. Apart from all of this, I am a big fan of Formula 1 racing :-)

29 thoughts on “Automated Trading using MetaTrader”
  • Narayanan says:

    after installing both autotrader and metatrader 5 demo, and doing the necessary file changes and path changes ……i am wondering why the settings list still shows amibroker instead of metadata…

    January 20, 2019 at 6:55 am
    • Admin says:

      Settings has some hard coded properties for amibroker, but they wont cause you any harm.

      January 22, 2019 at 3:17 am
  • Gokul says:

    Do we get RTD in MT4 or MT5 from autotrader as well?

    March 27, 2019 at 1:42 pm
    • Admin says:

      Nope

      March 28, 2019 at 3:25 pm
  • Im using MT platform. I did setup as per your guidelines. I am getting order details filled up in Orders.csv file. But order not created in Upstox trading platform. Any idea why?

    May 13, 2019 at 6:02 pm
  • Dillip Choudhury says:

    Does this support MT5 only or MT4 also? Can I use the inbuilt trailing stoploss feature of MT4 for trading Nifty options in my Upstox account?

    May 26, 2019 at 5:25 pm
    • Admin says:

      It supports MT4 as well, follow the same procedure & if you face any issues then send an email to us. We provide a set of additional functions for automation, you will have to read a bit more about AutoTrader and then see if it works with built-in features of MT.

      May 27, 2019 at 5:25 am
  • surendra says:

    hi i have cont configurs this seting

    June 26, 2019 at 2:53 pm
    • Admin says:

      Refer to help sub-menu on AutoTrader menu.

      July 2, 2019 at 6:14 am
  • ADESH KUMAR says:

    For MT4 autotrading, Do I need to buy separate Data feed or can MT4 use KITE Data?

    July 16, 2019 at 9:06 pm
    • Admin says:

      Separate datafeed

      July 29, 2019 at 4:38 am
  • Sunil says:

    Does this support Odin Diet ?

    July 21, 2019 at 4:53 am
    • Admin says:

      Yes

      July 29, 2019 at 4:36 am
  • Vishal says:

    does it need to have a broker tie up or whether we can use our existing broker

    July 25, 2019 at 5:14 am
    • Admin says:

      You can use the brokers/trading platforms which are supported by autotrader. Please refer to product home page.

      July 29, 2019 at 4:36 am
  • Naval says:

    can we apply change our strategy as per requirement , also can we use expert advisors in mt4 platform for indian indices trading

    July 25, 2019 at 5:16 am
    • Admin says:

      Yes, you can do all of that.

      July 29, 2019 at 4:35 am
  • sateshchand says:

    i want to use your auto trader for ninjatrader. i am hiring a russian ninjatrader coder for writting indicator.i want only place order feature.what i want is excatly write in csv text file .example place order (nfo,instrument name,buy or sell,intrday market order like this. dont tell me to watch help files and excell file those are confusing. i have to explain to my coders,latter i can not change it.kindly give one line answer

    December 17, 2019 at 6:10 am
  • vinay says:

    sir plzz uplode vedio of intergating b/w mt4 and autotrade

    December 31, 2019 at 11:21 am
    • Pritesh says:

      We will have it soon…

      January 2, 2020 at 5:58 am
  • Sundar soni says:

    Sir I have my own expert advisor and indicators that are still doing auto trading in mt4 and mt5 in fx and giving popup alert for entry and exit in indian market symbols .And then I trade manually according to the alert .Now I want to ask that is there any modifications needed in my expert/indicators mql5 code to make integration with nest trader? If yes then what or can you assist for that..please

    January 10, 2020 at 2:07 am
    • Pritesh says:

      Please go through above article, everything you need is already mentioned there. Is there anything you didn’t understand?

      January 10, 2020 at 3:39 am
  • Gokulnath Panneerselvam says:

    Hi, can I post the PlaceBracketorder function in the orders.csv directly.
    placeBracketOrder(exchange, “symbol”, tradetype, qty, price, SL, TGT, Valdation); just with this can I place order through MT4. I have designed like, if I click a buy button or sell button from MT4, it will send the placebracket order to the orders.csv. Please help me whether it is okay and it will work as am not using Include file. Directly writing the values to csv. Please confirm it. Waiting for your valuable answer.

    February 10, 2020 at 12:55 pm
    • Pritesh says:

      Yes, but you need to write as per the expected format. Please see this post for more details.

      February 10, 2020 at 4:30 pm
  • Anuraag says:

    Is it correct that AT does not support Live and Real-Time Data on MT5? If yes, then is there a road map on when this feature can be expected? Also, what is the recommended way to get Live and Real-Time Datafeed on MT5? Any recommended sources?

    August 27, 2020 at 12:07 am
    • Pritesh says:

      We are not data vendors & there is no plan to follow the data route. There are many authorized data vendors & you need to approach them to find out whether they support MT5. We do not have much information about these vendors.

      August 27, 2020 at 9:55 pm
  • Ansari says:

    Hi ,
    How can i set up MetaTrader.

    September 18, 2020 at 11:10 pm
  • Ansari says:

    Hi,
    Good I have done .. I would like to know if you have any function to cancel all active trade in one click ?

    September 23, 2020 at 6:10 pm
Leave A Comment