Using mqh file or using purely - which one is fastest

[Deleted]  

Hello, 


Using 


#include <Trade\PositionInfo.mqh>
#include <Trade\Trade.mqh>
CPositionInfo  m_position;                   // trade position object
CTrade         m_trade;                      // trading object


Will make my EA slow? or should i use purely 


for example CTrade m_trade;  or directly MqlTradeRequest?

 
It's the same. And you should always prefer to use CTrade.
 
Shanmugi:

Hello, 


Using 



Will make my EA slow? or should i use purely 


for example CTrade m_trade;  or directly MqlTradeRequest?

Well CTrade run on MqlTradeRequest. But MqlTradeRequest should be faster at compile time OOP Classes don’t compile fast. During runtime don’t know how the compiler works. But yes. Imagine this you have a GUI panel and you click Long Using CTrade or MqlTradeRequest it stops the panel for some milliseconds because it’s waiting for server response, but with MqlTradeRequest and OrderSendAsync you can keep clicking and bot won’t stop to push trade. It doesn’t wait for return code. Don’t know if CTrade can do this. So yeah. Technically MqlTradeRequest is ehh ideally faster. 
 
I use CTrade
 
Arinze Michael Ejike #:
But MqlTradeRequest should be faster at compile time OOP Classes don’t compile fast.

What makes you think this?

Arinze Michael Ejike #:
During runtime don’t know how the compiler works.

The compiler does not work at runtime.

Arinze Michael Ejike #:
It doesn’t wait for return code. Don’t know if CTrade can do this.

CTrade can do this, of course: https://www.mql5.com/en/docs/standardlibrary/tradeclasses/ctrade/ctradesetasyncmode

 

Machine translation used, may contain inaccuracies.

Интересное мнение про ООП

Renat Fatkhullin, 2021.02.01 19:23

If we’re talking about MQL5, you can forget about “optimization” techniques from 10–20–30 years ago. You should write code that is as readable as possible. That’s the true measure of quality—not hacker tricks and pure showboating.

Why?

Because the compiler will run 5–10 passes of code reordering, very cleanly and compactly resolve the ends of the graphs, not to mention applying dozens of optimization patterns.

The MQL5 compiler laughs at human attempts to squeeze out “+2% speed” with fancy hacks.

If you’re interested, check out how eight square roots in different expressions were reordered and computed with four assembly instructions.

Ошибки, баги, вопросы

Renat Fatkhullin, 2018.03.13 22:59

A check showed that:

  1. SQRT is mapped to direct CPU instructions.

  2. SQRT + math operations run without branching, and in a single instruction (128-bit data) two square roots are computed at once.

    So this code turns into the following SSE assembly:
             D1=sqrt((X1-X)*(X1-X)+(Y1-Y)*(Y1-Y));
             D2=sqrt((X2-X)*(X2-X)+(Y2-Y)*(Y2-Y));
             D3=sqrt((X3-X)*(X3-X)+(Y3-Y)*(Y3-Y));
             D4=sqrt((X4-X)*(X4-X)+(Y4-Y)*(Y4-Y));
             D5=sqrt((X5-X)*(X5-X)+(Y5-Y)*(Y5-Y));
             D6=sqrt((X6-X)*(X6-X)+(Y6-Y)*(Y6-Y));
             D7=sqrt((X7-X)*(X7-X)+(Y7-Y)*(Y7-Y));
             D8=sqrt((X8-X)*(X8-X)+(Y8-Y)*(Y8-Y));
            ...
            sqrtsd  xmm1, xmm1
            unpcklpd        xmm4, xmm4
            movapd  xmm3, xmmword ptr [rsp + 432]
            unpcklpd        xmm3, xmmword ptr [rsp + 384]
            subpd   xmm3, xmm4
            mulpd   xmm3, xmm3
            unpcklpd        xmm0, xmm0
            movapd  xmm5, xmmword ptr [rsp + 416]
            unpcklpd        xmm5, xmmword ptr [rsp + 400]
            subpd   xmm5, xmm0
            mulpd   xmm5, xmm5
            addpd   xmm5, xmm3
            sqrtpd  xmm8, xmm5
            movapd  xmm5, xmmword ptr [rsp + 464]
            subpd   xmm5, xmm4
            mulpd   xmm5, xmm5
            movapd  xmm7, xmm9
            subpd   xmm7, xmm0
            mulpd   xmm7, xmm7
            addpd   xmm7, xmm5
            movapd  xmm6, xmm10
            unpcklpd        xmm6, xmm11
            subpd   xmm6, xmm4
            movapd  xmm3, xmmword ptr [rsp + 368]
            unpcklpd        xmm3, xmmword ptr [rsp + 352]
            subpd   xmm3, xmm0
            movapd  xmm4, xmm8
            shufpd  xmm4, xmm4, 1
            sqrtpd  xmm5, xmm7
            mulpd   xmm6, xmm6
            mulpd   xmm3, xmm3
            addpd   xmm3, xmm6
            sqrtpd  xmm15, xmm3
            movapd  xmm0, xmm14
            unpcklpd        xmm0, xmmword ptr [rsp + 336]
            subpd   xmm0, xmm2
            mulpd   xmm0, xmm0
            movapd  xmm2, xmm0
            shufpd  xmm2, xmm2, 1
            addsd   xmm2, xmm0
            movapd  xmm0, xmm15
            shufpd  xmm0, xmm0, 1
            sqrtsd  xmm12, xmm2
    This is a piece of art, actually. Eight square roots were computed with four calls to the assembly instruction. Two double values were computed per one call.

  3. When doing operations through an array, everything runs “normally”—with checks, branches, and overhead from converting double → integer index.

  4. When working with arrays in this example there is constant mixing of FPU/ALU, which affects performance very badly.

  5. Optimization of access to a dynamic array is excellent—top notch. But mixing FPU/ALU operations + double → integer conversion + branching takes time.

Overall conclusion: math in MQL5 wins thanks to near-perfect optimization. It’s not that arrays lose here—it’s that math wins.