如何计算最后交叉的移动平均线数量?

时间:2016-02-14 08:15:32

标签: mql4 metatrader4

我正在编写一个MQL4自定义指标,它可以告诉特定的一组移动平均线在多少个柱前交叉。

具体来说,我希望输出显示

  

“过去10个小节的20个句点MA( .. PRICE_OPEN )低于MA( .. PRICE_CLOSE )”。

int 号码的形式。

double ma1 = iMA( Symbol(), 10080, 20, 0, 0, PRICE_OPEN,  0 );
double ma2 = iMA( Symbol(), 10080, 20, 0, 0, PRICE_CLOSE, 0 );

我想知道ma1高于或低于ma2表示自当前柱数以来有多少柱。

1 个答案:

答案 0 :(得分:1)

TLDR;

MQL4 自定义指标设计有点精神分裂任务,有2个部分

一旦决定设计自定义指标,就必须注意硬币的两面。

MQL4代码对于呼叫者侧具有非平凡的签名(通常为 EA交易脚本类型的MQL4代码)


自定义指标,在它自己的基础上,以特殊模式运行,计算&将其值存储到一个或多个名为 IndexBuffer -s的数组中。

  

Phase I.: 设计来电方界面(我们需要设置/接收的是什么?)
   Phase II.: 设计自定义指标实施方

这样,给定指标

  • 设置只有一个参数:period ... 20
  • 接收只有一个int天的值{ +:above | 0: xoss | -: under}

Phase I. :设计来电者方界面

鉴于上述参数化和自定义指标构造的MQL4概念,以下通用模板(建议在CustomIndicator - 文件中常规维护为注释部分 - 文件)反映了调用者端接口所需的详细信息(只有复制粘贴机制才能获得这些细节,而CustomIndicator维护者负责实现完整性责任:

// CALL-er SIDE INTERFACE |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
                                                // 
                                                // #define sIndicatorPathNAME             "#AliceInTheWonderlands_msMOD_0.00"
                                                // 
                                                // //_____________________________________INPUT(s)
                                                // // iMA_PERIOD:
                                                // extern int   iMA_PERIOD    = 20;
                                                // 
                                                // //_____________________________________OUTPUT(s):
                                                // #define iOutputDoubleUpOrDnBUFFER     0
                                                // 
                                                // //_____________________________________CALL-SIGNATURE:
                                                // 
                                                //                            double  iCustom(  _Symbol,                      // string       symbol,           // symbol:                                                 Symbol name on the data of which the indicator will be calculated. NULL means the current symbol.
                                                //                                              PERIOD_W1,                    // int          timeframe,        // timeframe
                                                //                                              sIndicatorPathNAME,           // string       name,             // path/name of the custom indicator compiled program:     Custom indicator compiled program name, relative to the root indicators directory (MQL4/Indicators/). If the indicator is located in subdirectory, for example, in MQL4/Indicators/Examples, its name must be specified as "Examples\\indicator_name" (double backslash "\\"must be specified as separator instead of a single one).
                                                //                                              iMA_PERIOD,                   // ...[1]       ...,              // custom indicator [1]-st input parameter
                                                //                                              <<N/A>>,                      // ...[2+]                        // custom indicator further input parameters (if necessary)
                                                //                                              iOutputDoubleUpOrDnBUFFER,    // int          mode,             // line index:                                             Line index. Can be from 0 to 7 and must correspond with the index, specified in call of the SetIndexBuffer() function.
                                                //                                              i                             // int          bar_shift         // shift
                                                //                                              );
                                                // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

典型的 Expert Advisor 调用如下所示:

int anIndicatorVALUE = iCustom( _Symbol,
                                PERIOD_W1,
                                sIndicatorPathNAME,
                                iMA_PERIOD,
                                iOutputDoubleUpOrDnBUFFER,
                                aCurrentBarPTR
                                );
if ( 0 > anIndicatorVALUE ) // on "line" under for <anIndicatorVALUE> [PERIOD]-s
{ ...
}

Phase II. :设计自定义指标实施方

预设整体容量

#property indicator_buffers 1            // compile time directive

为所需的IndexBuffer分配内存

IndicatorBuffers( 1 );                   // upper limit of 512 is "far" enough

声明 IndexBuffer

double UpOrDnBUFFER[];                   // mandatory to be a double

关联 IndexBuffer索引

SetIndexBuffer( 0, UpOrDnBUFFER );       // index 0: UpOrDnBUFFER[]
ArraySetAsSeries(  UpOrDnBUFFER, True ); // reverse AFTER association

任何自定义指标的核心逻辑都在 OnCalculate() 功能中,您可以在其中 - 实施期望的微积分
和 - 存储结果值到 UpOrDnBUFFER[]; 的各个单元格中

根据请求编写完全成熟的代码不是StackOverflow的主要目标,但是让我草拟一些关于此的注释,因为自定义指标实现方需要一些练习:

  • 因为自定义指标 OnCalculate()逐步运行&#34;所以请设计您的计算策略&amp;请记住&#34; block&#34;之间的状态。前进的计算块。

  • 如同给定的那样,十字架是三态系统,因此请观察案件的问题,在这些情况下,决定是在&#34; live&#34;吧,状态{above | cross |在条形演变过程中,under}可能会发生很多次变化。