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.
Warning
This demo was built with AutoTrader, which is now restricted to NOW, NEST & ODIN platforms only.
For trading with Upstox, Zerodha, AliceBlue or Finvasia; please use the next generation AutoTrader-Web. Read more about it’s MetaTrader Library.
Prerequisite
You need to install AutoTrader software.
You need to install MetaTrader software (free version is sufficient).
Integration
Part I
- All functions provided by AutoTrader are defined in algotrader-util.mqh header file. This file is available at:
- MetaTrader 4: C:\autotrader\scripts\mt4\Include
- MetaTrader 5: C:\autotrader\scripts\mt5\Include
- 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:
- Click on AutoTrader menu (Settings -> AutoTrader)
- Find setting IPC_FILES_PATH & click on it
- Click Edit button
- Put new path in Value field
- 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 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 amSettings has some hard coded properties for amibroker, but they wont cause you any harm.
January 22, 2019 at 3:17 amDo we get RTD in MT4 or MT5 from autotrader as well?
March 27, 2019 at 1:42 pmNope
March 28, 2019 at 3:25 pmIm 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 pmDoes 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 pmIt 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 amhi i have cont configurs this seting
June 26, 2019 at 2:53 pmRefer to help sub-menu on AutoTrader menu.
July 2, 2019 at 6:14 amFor MT4 autotrading, Do I need to buy separate Data feed or can MT4 use KITE Data?
July 16, 2019 at 9:06 pmSeparate datafeed
July 29, 2019 at 4:38 amDoes this support Odin Diet ?
July 21, 2019 at 4:53 amYes
July 29, 2019 at 4:36 amdoes it need to have a broker tie up or whether we can use our existing broker
July 25, 2019 at 5:14 amYou can use the brokers/trading platforms which are supported by autotrader. Please refer to product home page.
July 29, 2019 at 4:36 amcan 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 amYes, you can do all of that.
July 29, 2019 at 4:35 ami 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 amsir plzz uplode vedio of intergating b/w mt4 and autotrade
December 31, 2019 at 11:21 amWe will have it soon…
January 2, 2020 at 5:58 amSir 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 amPlease go through above article, everything you need is already mentioned there. Is there anything you didn’t understand?
January 10, 2020 at 3:39 amHi, can I post the PlaceBracketorder function in the orders.csv directly.
February 10, 2020 at 12:55 pmplaceBracketOrder(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.
Yes, but you need to write as per the expected format. Please see this post for more details.
February 10, 2020 at 4:30 pmIs 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 amWe 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 pmHi ,
September 18, 2020 at 11:10 pmHow can i set up MetaTrader.
https://stocksdeveloper.in/documentation/client-setup/metatrader-library/
September 19, 2020 at 11:14 amHi,
September 23, 2020 at 6:10 pmGood I have done .. I would like to know if you have any function to cancel all active trade in one click ?
Hi, I’m reading these pages after I saw the Amibroker interfacing. With Amibroker, we do not need to buy RTD as the price is included in subscription for AT (as I understand). I’ve understood how would we route the Data & signals from MT4 to AT. But, without RTD for MT4, how much will AT subscription cost for 6 month package? I have accounts in both Zerodha and Upstocks.
October 31, 2020 at 1:34 pmWe do not provide RTD the product you are looking at will soon be taken out of service for certain platforms. Please use the new AutoTrader Web product.
November 2, 2020 at 10:30 amHow can i use trailing bracket order in c# and meta trader
November 6, 2020 at 2:38 pmhttps://stocksdeveloper.in/documentation/api/place-bracket-order/
November 6, 2020 at 3:05 pmPritesh, not received email after registration, Could you help.
Cheers
December 16, 2020 at 9:56 pmGK
Kindly contact support on help@stocksdeveloper.in
December 17, 2020 at 10:30 am