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").
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.
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#:
public override int start()
{
OnTick();
return 0;
}
using System.Drawing;
).Build the project, it should happen without any problems.
In order to debug you will need to have an executable program to run. Let's create a project that helps with debugging support.
NQuotes.DebugHost.Server.Start(args);
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):
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:
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.