MetaTrader and Python integration

Browse PyMovingAverage sample source code or download in a ZIP file

NQuotes is designed to integrate MetaTrader with .NET platform. This platform is language independent, which gives you as a developer a larger choice of languages than a single MQL. Python is one of the most popular languages, so why not try it?

NQuotes contains a sample of an expert advisor for MT4 in Python, or more specifically - in IronPython, which is an advanced Python implementation for .NET platform. It has some peculiarities, but most of the time you can expect that your Python code works the same way. The Python standard library and a lot of 3rd party libraries are available to use as well.

How to try the sample?


After you install NQuotes, the PyMovingAverage sample is located in "%TERMINAL_DATA_PATH%\MQL4\Projects\nquotes\PyMovingAverage" folder.

  1. Open "PyMovingAverage.sln" and build it. It assumes that IronPython is installed at "c:\Program Files\IronPython 2.7", so if you have a different installation path, please fix the DLL references. The required DLLs are located in "Platforms\Net40" subdirectory of the installation folder.
  2. Start debugging (F5).
  3. Open MT4 terminal and run "nquotes" expert advisor. You can run it in the Strategy Tester with "Open prices only" model and a date period.

Description of the Python and MetaTrader integration sample

IronPython can't build "CLS-compliant" assemblies so far, so it needs to be hosted in a C# application. The hosting code is a "PyMovingAverage.cs" class. This class has several tasks:

The expert advisor is a "" class, which is converted to Python from a MT4 sample "Moving Average.mq4" (see "%TERMINAL_DATA_PATH%\MQL4\Experts"). This code is very close to what you would normally have if you used C# to create a EA. It's very similar to the MQL counterpart with a few differences. The main difference is scoping. We don't want to pollute the global scope, so we always to have to specify the scope (which is a common thing to do in Python):

Tips for using IronPython with NQuotes

Building your EA as a DLL and obfuscation

There's a tool "" in "Tools/Scripts" folder that can compile Python files into DLLs. Using this tool you can build your EA module as a DLL, which must be added to your C# code using a call to _engine.Runtime.LoadAssembly(). Later on you can use obfuscation software to obfuscate it providing additional security for your solution.

If everything is built from sources, you don't need to have IronPython installed on the machine, you just need all DLLs with their dependencies to be in ExpertsDirPath.

Standard library

IronPython contains a standard library implementation, but it needs to be located, otherwise you will get some "ImportError: No module named x" errors. The simplest option is to add it to sys.path like this:
import sys
sys.path.append(r'c:\Program Files\IronPython 2.7\Lib')

Another option is to set it in the IRONPYTHONPATH environment variable.

This works fine for development. For release builds you might not want to ask clients to install IronPython, therefore you'll need to copy all your dependencies into the build output folder or build them as a DLL (see for example IronPython: EXE compiled using cannot import module "os").