MetaTrader 5 Python User Group - how to use Python in Metatrader - page 22

 
Vladimir Karputov:
Moved now to this thread.
Thnx
 
Alexey Volchanskiy:

I am also studying Python, but only as part of mastering neural networks, machine learning and other fancy stuff and packages. I do not think it is possible to work with this super-brainer, it is 200 times slower than MQL5, I cited the results of some simple tests.

I still haven't understood, is it possible to connect Python modules and call functions from MQL5? Or just Python->MQL5?

With all due respect you just don't know how to prepare it. Reworked your example:

In Python:

#!/usr/bin/env python3

from numba import njit, prange
import time


@njit(parallel=True, fastmath=True, cache=False)
def calc_pi(num_steps):
    x, sum1 = 0.0, 0.0
    step1 = 1.0 / num_steps
    for i in prange(num_steps):
        x = (i + 0.5) * step1
        sum1 = sum1 + 4.0 / (1.0 + x * x)
    return sum1 * step1


calc_pi(1)  # minus jit compilation time
start_time = time.time()
pi = calc_pi(1000000000)
msvcr = time.time() - start_time

print(f"--- {msvcr} seconds --- The value of PI is {pi}")


For comparison in C:
#include <stdio.h>
#include <time.h>

int main()
{
    int num_steps = 1000000000;
    double step;

    clock_t start;
    clock_t stop;
    start = clock();
    double x, pi, sum=0.0;
    step = 1.0/(double)num_steps;
    for (int i = 0; i<num_steps; i++)
    {
        x = (i + .5) * step;
        sum = sum + 4.0 / (1.0 + x * x);
    }
    pi = sum*step;
    stop = clock();

    printf("The value of PI is %1.12f \n", pi);
    printf("The time to calculate PI was %5.3f seconds \n", ((double)(stop - start)/CLOCKS_PER_SEC));
    return 0;
}


This is all on an ancient AMD Athlon(tm) 64 X2 Dual Core Processor 4200+ calculator, I think on MQL5 the value will be around 12 sec.

(I don't know how to run MQL code on linux.)

So Python with jit compiler beats C (mql) by 2 times! If I run it on modern 8 Core CPU it will beat 8 times, simply because it's multi-threaded.

But even in one thread Python result is 9.6 seconds - so it's still faster. And you can also run it on a video card with CUDA.

For example I can't write multi-threaded analogues in C or MQL - I won't be able to write loky, races and crashes, plus the code will be much larger.

Question to MetaTrader developers: are you going to add call of trading functions from Python?

I would also like a function in Python to subscribe to tick events from the terminal, so when a tick comes the message will be sent to Python.

I know that there are third-party developments of these functions, but I would like to have them out of the box.

You can also try to write multi-threaded code in GoLang for fun, in one thread it is comparable in speed to C.
 

mql5

i7 on virtual machine, allocated 2 cores, 4 threads


 

What kind of processor do you have? Run my code in C to compare it with.


If it's an i7 it's probably 25 times faster than my AMD, if not more. I need a test in the same languages for the report point.

 
Lyuk:
What kind of processor do you have? Run my code in C to compare it with.

C

i7 onvirtual machine, allocated 2 cores, 4 threads


 
So it turns out that your MQL is even slightly slower than C, so my python code with jit compiler will be much faster than theirs. I predict python to be about 1.5 sec on your CPU in 4 threads, although there are only 2 real threads.
 

Py

i7 on Linux, 4 cores, 8 threads


 

Great result ! And quickly written. Anyone who says python is slow can be sent to see the test results here.

Run more python code in one thread on your processor (parallel = False)


For a full test.


Sad to say, number crunchers have only gone up by about 12 times in the last 15 years, I thought modern CPUs were faster.
 

(parallel = False)


 
Lyuk:

Great result ! And quickly written. Anyone who says python is slow can be sent to see the test results here.

Run more python code in one thread on your processor (parallel = False)


For a complete test.

They were posted a long time ago but they prefer to compare it with pure python which makes no sense ) pure python is designed for other purposes

Reason: