MetaTrader and Python integration
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?Prerequisites:
After you install NQuotes, the PyMovingAverage sample is located in "%TERMINAL_DATA_PATH%\MQL4\Projects\nquotes\PyMovingAverage" folder.
- 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.
- Start debugging (F5).
- 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:
- finds the .py module file (it adds the DLL directory to sys.path so that the module is found)
- creates an expert advisor object and wires it with the API calls
- adds required DLL references so that you don't have to deal with it
- handles exceptions
The expert advisor is a "MovingAverage.py" 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):
- all code is inside a class, therefore methods and properties must be accessed through "self." prefix
- static constants declared in the class must be accessed through "MovingAverage." prefix (the class name)
- MQL functions must be called with "mql." prefix (which is an alias to "self.api.")
- MQL constants must be accessed through "C." prefix
Tips for using IronPython with NQuotes
Building your EA as a DLL and obfuscation
There's a tool "pyc.py" 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.
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:
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 pyc.py cannot import module "os").