_old: How to connect MetaTrader4 Platform (script)

Send signals from MT4 to Collective2 using MT4 scripts

As we wrote, the simplest way to send your MetaTrader trades into a Collective2 system is to use our C2MT4Connector Expert Advisor. C2MT4Connector watches changes in your MT4 trading account and sends those changes as trading signals to a Collective2 trading system. If you need more precise control over the type of signals sent to Collective2, you can use specific commands in your own MT4 script.

Examples

Here is an example of how to use our library in your MT4 scripts. The example is based on Moving Average sample provided by MT4.

Asynchronous nature of C2MT4Connector

Note that C2OrderXYZ functions are asynchronous. Returned values are returned almost immediately from your computer . Functions do not wait for Collective2 operations. This means that your MetaTrader Terminal (and therefore your trading ) are not blocked by Collective2 operations or by slow network traffic. Collective2 responses are returned by the C2NextResponse() function.

Thread safety

C2MT4Connector is not thread-safe. Do not use this DLL in two or more EAs in the same MT4 instance.

Available methods

This section describes the methods available to you in our C2MT4Connector.dll

Common parameters

  • string email - your Collective2 logon e-mail
  • C2Password Your C2 Data Services password This is not the password you use to log in to Collective2. Learn about your C2 Data Services Password.
  • string systemid - your Collective2 system id

Returned string

Almost all functions return a string. This string can be "OK" or "Error". This value (OK or ERROR) is the result of the operation performed in IN YOUR COMPUTER. It is not Collective2's response.

Methods

Initialization

string C2Init2(string c2InterfaceHost, string email, string password, string systemid, double c2LotMultiplier, string eaVersion, string terminal_data_path, string terminal_commondata_path, string reserve)

  • string c2InterfaceHost: IP address or a host name of Collective2 endpoint
  • string email: Collective2 login
  • string password: C2 Data Service password. (Not your Collective2 login password!)
  • string systemid: Collective2 system identifier (number)
  • double c2LotMultiplier: a value to multiply the lot size traded within MetaTrader prior to sending to Collective2. For example, if MetaTrader is trading in lots 0.10, a multiplier of 10 would tell Collective2 to trade 1 lot. A lot at Collective2 needs to be at least 1 and a whole number (i.e. no fractional or decimal portion).
  • string eaVersion: version of your MetaTrader script (arbitrary text)
  • string terminal_data_path: a folder in which terminal data are stored. When calling, provide the TerminalInfoString(TERMINAL_DATA_PATH) expression.
  • string terminal_commondata_path: a common path for all of the terminals installed on a computer. When calling, provide the TerminalInfoString(TERMINAL_COMMONDATA_PATH) expression.
  • string reserve: reserve

Deinitialization

string C2DeInit()

Function returns "OK" or an error text.

Send opening signal

string C2OrderOpen(string email, string password, string systemid, int orderid, string symbol, int cmd, double volume, double price, double stoploss, double takeprofit, string timeStamp, string mt4OrderComment);

  • email, password, systemid - see above
  • int orderid: MetaTrader's order number
  • string symbol: Traded symbol
  • int cmd: See MetaTrader Help: Standard constants -> Trade operations
  • double volume: Traded amount. It must to be an integer number.
  • double price: A price for Limit/Stop orders.
  • double stoploss: A stop loss price.
  • double takeprofit: A profit target price.
  • string timeStamp: Optional parameter which can help Collective2 to track your signals. Please use MT4 command: TimeToStr(OrderOpenTime(),TIME_DATE|TIME_SECONDS);
  • string mt4OrderComment: Short comment which will be shown on Collective2.

Send closing signal

string C2OrderClose(string email, string password, string systemid, int orderid, string symbol, int cmd, double volume, double closePrice, string reserve);

  • email, password, systemid - see above
  • int orderid: MetaTrader's order number
  • string symbol: Traded symbol int cmd: See MetaTrader Help: Standard constants -> Trade operations This is the command from the original opening signal (the signal we are closing). Collective2 will determine appropriate closing command.
  • double volume: Traded amount. It must to be an integer number.
  • double closePrice: A price for Limit/Stop orders.
  • string reserve: A string for the future use. Not used currently.

Cancel signal

string C2OrderCancel(string email,string password, string systemid, int orderid, string reserve);

  • email, password, systemid - see above
  • int orderid: MetaTrader's order number
  • string reserve: A string for the future use. Not used currently.

Modify signal

string C2OrderModify(string email,string password, string systemid, int orderid, int cmd, double oldPrice, double oldStopLoss, double oldTakeProfit, double newPrice, double newStopLoss, double newTakeProfit, string reserve);

  • email, password, systemid - see above
  • int orderid: MetaTrader's order number. The order we are going to modify.
  • int cmd: See MetaTrader Help: Standard constants -> Trade operations This is the MT4 command id from the original signal (the signal we are going to modify).
  • double oldPrice: A price of the original order (for pending Limit/Stop orders).
  • double oldStopLoss: A stop loss price of the original order.
  • double oldTakeProfit: A profit target price of the original order..
  • double newPrice: A new price. You can change the price of pending Limit/Stop orders.
  • double newStopLoss: A new stop loss price. If newStopLoss=0.0 then the original stop loss signal will be cancelled.
  • double newTakeProfit: A new profit target price. If newTakeProfit=0.0 then the original profit target signal will be cancelled.
  • string reserve: A string for the future use. Not used currently.

Request a C2 responses count

int C2ReadResponses();

Function returns a number of available Collective2 responses.

Read a C2 response

string C2NextResponse()

Function returns one of available Collective2 responses. Use this function in the cycle like this: 

int ReadCollective2Responses()
{
   Sleep(4000); // Wait 4 seconds (let C2 finish its work before reading responses).
   int numOfResponses = C2ReadResponses();
   if (numOfResponses > 0)
   {
      for  (int i=1; i<=numOfResponses; i++) 
{ string c2Result = C2NextResponse(); if (c2Result != "")
{ Print(" <- from C2: ",c2Result); } } } return (0); }

Note

If you want blocking operations in your MT4 script, wait until C2ReadResponses() > 0. But be careful: do not block forever or your trading will be blocked too...

Have more questions?