Expert creation tutorial

The tutorial is aimed to help understanding the differences between C# and MQL, and guides you through the process of creating your first expert advisor using C# and NQuotes. This tutorial is based on the "MACD Sample.mq4" sample, which is included in the MetaTrader installation.

If you want to see a quick result, you can take the tutorial source code and skip to the EA debugging section. The source code for the tutorial is installed in "%TERMINAL_DATA_PATH%\MQL4\Projects\nquotes\MovingAverageExpert" folder (typically "c:\Program Files\MetaTrader 4\MQL4\Projects\nquotes\MovingAverageExpert").

It's important to know the TERMINAL_DATA_PATH of your terminal, or the "terminal data folder". You can read about it here: Data Structure in MetaTrader 4. This tutorial assumes that you are running your terminal in "portable" mode, meaning that your TERMINAL_DATA_PATH is the same as the terminal installation path (for example, "c:\Program Files\MetaTrader 4").

Step 1: Create a project

For this project you'll need a C# development environment like Visual Studio. There's a free version of Visual Studio, which is called "Visual Studio Community", and can be downloaded from Visual Studio Community page.

Create a Visual C# "Class Library" project in Visual Studio.

NQuotes project

Step 2: Create an expert advisor class

Add a reference to "nquotes.dll", which can be found in the "%TERMINAL_DATA_PATH%\MQL4\Libraries\nquotes" folder (typically "c:\Program Files\MetaTrader 4\MQL4\Libraries\nquotes\nquotes.dll").

Delete "Class1.cs" file and create a public class called "MACDExpert", inherit this class from "NQuotes.MqlApi". It should look like this:

public class MACDExpert : NQuotes.MqlApi { ... }

Find the "MACD Sample.mq4" sample in "%TERMINAL_DATA_PATH%\MQL4\Experts" (typically "c:\Program Files\MetaTrader 4\MQL4\Experts\MACD Sample.mq4").

Open the sample in MetaEditor, copy the whole text and paste it right inside the MACDExpert class (in the class body between the curly braces).

Now you need to make some adjustments to the code to migrate from MQL to C#:

  1. Remove lines starting with #property.
  2. Change "input" keyword to "public" for external input variables.
  3. Replace NULL with null.
  4. Remove "void" in OnTick(void) method signature.
  5. Add a method start() and call OnTick() like this: public override int start() { OnTick(); return 0; }
  6. In OrderSend() and OrderModify() function calls change the "expiration" DateTime parameter from "0" to "DateTime.MinValue".
  7. In OrderSend(), OrderModify() and OrderClose() function calls change the "Green", "Violet" and "Red" colors to "Color.Green", "Color.Violet" and "Color.Red" respectively (you'll need to reference the standard "System.Drawing.dll" and import it with using System.Drawing;).

Build the project, it should happen without any problems.

Step 2.1: Add a debugger project

In order to debug you will need to have an executable program to run. Let's create a project that helps with debugging support.

  1. Add a new "Console Application" to the solution. Name it "DebugHost".
  2. Add a reference from DebugHost to "MACDSample" subproject.
  3. Add a reference from DebugHost to "nquotes.dll" (see step 2).
  4. Inside the generated Program class "Main" method add this line of code: NQuotes.DebugHost.Server.Start(args);
  5. Right click on the "DebugHost" project and choose "Set as StartUp Project". This activates the "DebugHost" project for running.
  6. Build the solution.

Step 3: Run the debugger project

Put a breakpoint on the first line inside the start() method, and then press "Start Debugging" (F5) in Visual Studio.

Started "DebugHost.exe" is waiting for the MetaTrader terminal.
Note: The breakpoint will not become fully red immediately. It will show a warning sign with a message "The breakpoint will not currently be hit. No symbols have been loaded for this document.". This is normal, because the expert advisor DLL (MACDSample.dll) is not yet loaded.

Now start the MetaTrader terminal and login with your demo account.

Open the strategy tester (View - Strategy tester, or Ctrl+R). Change the settings as specified here (see the screenshot below):

  • Expert Advisor: nquotes.ex4
  • Symbol: EURUSD
  • Model: Open prices only
  • Period: M15
  • Use date: FROM one week before today TO today

NQuotes EA strategy tester settings

Press the "Start" button in the strategy tester. As soon as you do it, a breakpoint will be hit, and you could follow the execution in the debugger:
Debugging

For now just disable the breakpoint and continue execution by pressing "F5".

The execution will proceed and in the end you will see the report and the graph of the profit of your expert advisor in the terminal strategy tester window.