0

我正在编写一个 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多少柱。

4

1 回答 1

1
TLDR;

MQL4 Custom Indicator设计是一个有点精神分裂的任务,有 2 个部分

一旦决定设计一个Custom Indicator,就必须注意硬币的两面。

MQL4代码对调用方
(通常是一种 Expert Advisor或一种Script类型的 MQL4 代码)有一个重要的签名,
并且
Custom Indicator它自己
在一种特殊模式下运行,在这种模式下它计算并将其值存储到一个或多个称为IndexBuffer-s 的数组中。

Phase I.:设计调用端接口(我们需要设置/接收什么?)
Phase II.:设计Custom Indicator实现端

这样,给定的指标应

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

Phase I. :设计调用方接口

鉴于上述参数化和构造MQL4概念Custom Indicator,以下通用模板(建议作为 -file 中的注释部分定期维护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. :设计Custom Indicator实施端

预设总容量

#property indicator_buffers 1            // compile time directive

IndexBuffer为需要的(s)分配内存

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

any 的核心逻辑在函数Custom Indicator内部,您可以在其中 -实现所需的微积分-结果值存储到OnCalculate()


UpOrDnBUFFER[];

根据要求编写完整的代码不是 StackOverflow 的主要目标,但让我在这方面草拟一些注释,因为 Custom Indicator实现方面需要一些练习:

  • 因为Custom Indicator OnCalculate()“渐进式”运行,所以请设计您的计算策略并记住前向渐进式计算块的“块”之间的无状态性。

  • 如给定的那样,十字架是三态系统,因此请注意案件问题,其中决定是在“实时”栏上做出的,其中状态{ above| cross| under} 在条形演化过程中可能会发生多次变化。

于 2016-02-14T12:40:29.697 回答