MQL4 函数指针/函数回调解决方案

Joe*_*Box 4 algorithmic-trading forex mql4

据我所知,MQL4 中不存在函数指针。

作为一种解决方法,我使用:

// included for both caller as callee side
class Callback{
   public: virtual void callback(){ return; }
}
Run Code Online (Sandbox Code Playgroud)

然后在传递回调的源中:

class mycb : Callback{
   public: virtual void callback(){
     // call to whatever function needs to be called back in this source
   }mcbi;
Run Code Online (Sandbox Code Playgroud)

现在 mcbi 可以通过如下方式传递:

 afunction(){
    fie_to_receive_callback((Callback *)mycbi);
 }      
Run Code Online (Sandbox Code Playgroud)

接收者可以回调为:

 fie_to_receive_callback(mycb *mcbi){
    mcbi.callback(); // call the callback function
  }
Run Code Online (Sandbox Code Playgroud)

有没有更简单的方法在 mql4 中传递函数回调?

Inv*_* TS 5

实际上有一种方法,在 MQL4 中使用函数指针。下面是一个例子:

typedef int(*MyFuncType)(int,int);

int addition (int a, int b)
{ return (a+b); }

int subtraction (int a, int b)
{ return (a-b); }

int operation (int x, int y, MyFuncType myfunc)
{
   int g;
   g = myfunc(x,y);
   return (g);
}

int OnInit()
{
   int m,n;
   m = operation (7, 5, addition);
   n = operation (20, m, subtraction);
   Print(n);
   return(INIT_FAILED);  //just to close the expert
}
Run Code Online (Sandbox Code Playgroud)


use*_*197 0

不,幸运的是没有。(. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

MQL4运行时执行引擎 (MT4) 具有相当脆弱的进程/线程处理,并且添加更多(且更智能)的构造(超出基本的{ OnTimer() | OnTick() | OnCalculate() }事件绑定回调)对主要 MT4 职责中已经无法保证的实时执行构成相当大的威胁。虽然“新”-MQL4.56789 可能会提供这样做的技巧,但可能有更安全的卸载策略来进行分布式,让 MT4 传统处理程序从外部处理集群接收“预烘焙”结果,而不是尝试一棵已经有一年了的可怜的圣诞树上挂着越来越多的闪烁的小玩意。

要认识到这种避免危险的方式是多么残酷,只需注意原始OnTimer()使用的 1 秒分辨率(是的,世界上有 1.000.000.000ns步,其中流提供者以纳秒为单位标记事件......)

* ):是的,自从"new"-MQL4引入以来,原始语言中有许多隐身模式的变化MQL4。每次更新后,建议您查看“新”帮助文件,因为可能会有新选项和令人讨厌的惊喜。MQL4花费数百人*年维护一个代码库,这确实是一次非常毁灭性的经历。