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实现方面需要一些练习: