iCustom: calling an indicator from .NET EA

Browse CustomIndicatorCaller sample source code or download in a ZIP file

iCustom() is an MQL and NQuotes function that allows calling indicators and returning their values from an expert advisor program. It is possible to call iCustom() from .NET using NQuotes library. Indicators with a single parameter or with an empty parameter set can be called without any special code preparations.

Calling indicators with 2 or more parameters requires a trick to serialize parameters to a string and then parse this string on MQL side. On C# side you need to convert all parameters to strings and concatenate them using String.Join() to form a single string, which is passed to iCustom(). On the indicator side you need to add a parsing code to init() function, which splits the passed string, and converts each part to an appropriate type.

If you are converting an existing indicator to be callable from C#, it means that you need to remove “input” and “extern” modifiers from all your parameters, add a single input parameter “input string paramsPack”, then parse it in init() and assign values to your original parameters.

There’s a sample “CustomIndicatorCaller” that shows this technique. You can find the sample source code in the “%TERMINAL_DATA_PATH%\MQL4\Projects\nquotes\CustomIndicatorCaller” folder. Try it like this:

  1. Copy the MQL file “CustomHighLowIndicator.mq4” into “%TERMINAL_DATA_PATH%\MQL4\Indicators” folder and compile using the MetaEditor (metaeditor.exe).
  2. Build the Visual Studio project. It builds a EA DLL and copies it to “%TERMINAL_DATA_PATH%\MQL4\Experts” folder.
  3. Start the MT4 terminal (terminal.exe) and attach the “nquotes” EA to the chart. This will activate the built EA DLL, which calls the indicator and prints the value.

The expected EA output looks like this :

2014.01.01 19:30:48 2013.10.22 00:00  nquotes/nquoteslib EURUSD,M15: loaded successfully
2014.01.01 19:30:49 2013.10.22 00:00  CustomHighLowIndicator EURUSD,M15: loaded successfully
2014.01.01 19:30:49 2013.10.22 00:00  nquotes/nquoteslib EURUSD,M15: CustomHighLowIndicator value = 255.33

The printed value is a sum of parameters passed from C#: param2 + param3.