How to embed python in mql5?

 

Hello,

I have a complete python-project that calculates signals for a history of data. For simplicity lets say I have a class like that:

class PythonSignal:
        def next(self, price_data) -> int:
                signal = ... # do some calculation on my state and the specified data
                return signal

Now I would like to create an instance of this class in my OnInit and call next in OnTick.

PythonSignal instance;

int OnInit()
{
        instance = ... // instatiate PythonSignal-Class
}

void OnTick()
{
        price_data = ...
        int signal = instance.next(price_data);
        ....
}

Is there a way to do something like that (libraries / dlls maybe)? I do need to include a class, not just a function. Could you give me an example on how to do it or a link to a tutorial?


Thank you!

 
UnknownInnocent: I have a complete python-project that calculates signals for a history of data. For simplicity lets say I have a class like that: Now I would like to create an instance of this class in my OnInit and call next in OnTick. Is there a way to do something like that (libraries / dlls maybe)? I do need to include a class, not just a function. Could you give me an example on how to do it or a link to a tutorial? Thank you!

You can't embed "Pyhton" into your MQL5 code. The Python integration is for externally accessing MetaTrader 5 only.

 
Fernando Carreiro #:

You can't embed "Pyhton" into your MQL5 code. The Python integration is for externally accessing MetaTrader 5 only.

If I am informed correctly, the integration is for scripts only and you cannot build experts - there isn't even an OnTick-Function (need to build a while-true-loop and ask for data every x time-periods?) and you cannot test it with the strategy tester, which makes the usability horrible.

In C++ it is possible to embed python so I hoped it was possible in mql5 too...

Can I do something like embedding my python in a C++-Class, integrate this C++ Class in a .dll and use it through the dll?

 
UnknownInnocent #: If I am informed correctly, the integration is for scripts only and you cannot build experts - there isn't even an OnTick-Function (need to build a while-true-loop and ask for data every x time-periods?) and you cannot test it with the strategy tester, which makes the usability horrible.

As I stated, it is for external access only! Not for event driven interaction. However, even with those limitations, you can still develop automation equivalent to an EA, for strategies that are not so critical with regard to tick events and can easily work on a timed based interaction.

UnknownInnocent #: In C++ it is possible to embed python so I hoped it was possible in mql5 too... Can I do something like embedding my python in a C++-Class, integrate this C++ Class in a .dll and use it through the dll?

It makes no sense to go to so much trouble to interchange between three languages, MQL, C++, and Python. It's going to be bloated and inefficient. Take the time to do it in pure MQL5 with the added benefit of being more concise and efficient and have much less hassle.

 
Fernando Carreiro # :

As I stated, it is for external access only! Not for event driven interaction. However, even with those limitations, you can still develop automation equivalent to an EA, for strategies that are not so critical with regard to tick events and can easily work on a timed based interaction.

It makes no sense to go to so much trouble to interchange between three languages, MQL, C++, and Python. It's going to be bloated and inefficient. Take the time to do it in pure MQL5 with the added benefit of being more concise and efficient and have much less hassle.

I have read carefully, the need to invoke python functionality in MQL5 exists, because python has rare and complex functions (think of econometrics), not MQL5. There would be Alglib, but there is no decent documentation, which instead is richly explained in the python libraries. Returning to the integration, my conclusion is that it is only possible by compiling the python libraries into DLLs, and calling the DLL inside the indicators or EAs. Do you have any examples on the net using DLLs in MQL5 code?

 
Sabino Martiradonna #: I have read carefully, the need to invoke python functionality in MQL5 exists, because python has rare and complex functions (think of econometrics), not MQL5. There would be Alglib, but there is no decent documentation, which instead is richly explained in the python libraries. Returning to the integration, my conclusion is that it is only possible by compiling the python libraries into DLLs, and calling the DLL inside the indicators or EAs. Do you have any examples on the net using DLLs in MQL5 code?

I have no examples myself, but you can search the Articles section for examples of how to write and call DLLs from MQL.

However, most of them are for C/C++ or C#. I think there is also a Delphi example, but I don't believe there are any examples for Python.

Reason: