iCustom
iCustom
函数返回指定自定义指标的处理器。
int iCustom(
string symbol, // 交易品种名称
ENUM_TIMEFRAMES period, // 周期
string name // 文件夹/自定义指标_名称
... // 指标输入参量列表
);参量
- symbol
[in] 证券交易品种名称,数据用来计算指标。 NULL 值代表当前交易品种。
- period
[in] 周期值可以是 ENUM_TIMEFRAMES 值中的一个,0代表当前时间表。
- name
[in] 如果在自定义指标名称前指出反斜杠符号’\’,则相对于MQL5根文件夹搜索EX5指标文件。因此,对于调用iCustom(Symbol()、Period()、 “\FirstIndicator”…),该指标将被加载为MQL5\FirstIndicator.ex5。如果在此路径中找不到此文件,则返回错误error 4802 (ERR_INDICATOR_CANNOT_CREATE)。
如果路径不是以’\‘开始,则会根据以下方式搜索和下载指标:
- 首先,在调用程序的EX5文件所在的文件夹中搜索指标EX5文件。例如,CrossMA.EX5 EA位于MQL5\Experts\MyExperts,并包含iCustom调用(Symbol()、Period()、“SecondIndicator”…)。在这种情况下,将在MQL5\Experts\MyExperts\SecondIndicator.ex5中搜索指标。
- 如果在同目录下没有找到指标,则相对于MQL5\Indicators指标根目录执行搜索。换句话说,将执行对MQL5\Indicators\SecondIndicator.ex5文件的搜索。如果找不到指标,则该函数返回INVALID_HANDLE并触发错误4802(ERR_INDICATOR_CANNOT_CREATE)。
: 如果在子目录(例如MyIndicators\ThirdIndicator)中设置指标路径,则首先在MQL5\Experts\MyExperts\MyIndicators\ThirdIndicator.ex5的调用程序文件夹(EA交易位于MQL5\Experts\MyExperts)中执行搜索。如果未成功,则执行对MQL5\Indicators\MyIndicators\ThirdIndicator.ex5文件的搜索。确保在路径中使用双反斜杠’\\‘作为分隔符,例如iCustom(Symbol()、Period()、 “MyIndicators\\ThirdIndicator”…)
返回值
返回特殊技术指标处理器,失败返回 INVALID_HANDLE. 计算机内存从不使用的指标中释放,使用指标处理程序传递到的函数 IndicatorRelease() 。
注释
必须编辑自定义指标(扩展EX5)并且在可获端或子目录中保存在MQL5/Indicators 目录中。
需要测试的指标是指自动从iCustom()函数调用,如果相应的参数是通过一个通用的常量字符串集合。对于其它情况下(使用IndicatorCreate() 函数或一个非常量字符串设定指标名称)该属性#property tester_indicator是必需的:
#property tester_indicator "indicator_name.ex5"如果在指标中使用 第一调用方式,然后在自定义指标中可以在"Parameters"标签中自动形成为指示数据的计算,如果"Apply to"参量不是明确指定的,默认计算将以"Close"价格值为主。

当从MQL5程序中调用自定义指标时,Applied_Price参量或者另外的指标处理器应该最后传递,在所有自定义指标的输入变量之后。
另见
程序属性, 时间序列和指标访问, IndicatorCreate(), IndicatorRelease()
示例:
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots 1
//---- 标签图1
#property indicator_label1 "Label1"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrRed
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
//--- 输入参量
input int MA_Period=21;
input int MA_Shift=0;
input ENUM_MA_METHOD MA_Method=MODE_SMA;
//--- 指标缓冲区
double Label1Buffer[];
//--- 自定义移动平均数处理程序。mq5自定义指标
int MA_handle;
//+------------------------------------------------------------------+
//| 自定义指标初始化函数 |
//+------------------------------------------------------------------+
int OnInit()
{
//--- 指标缓冲区绘图
SetIndexBuffer(0,Label1Buffer,INDICATOR_DATA);
ResetLastError();
MA_handle=iCustom(NULL,0,"Examples\\Custom Moving Average",
MA_Period,
MA_Shift,
MA_Method,
PRICE_CLOSE // 使用收盘价
);
Print("MA_handle = ",MA_handle," error = ",GetLastError());
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| 自定义指标重复函数 |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
const int prev_calculated,
const datetime &time[],
const double &open[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
const int &spread[])
{
//--- 复制指标自定义移动平均值到指标缓冲区
int copy=CopyBuffer(MA_handle,0,0,rates_total,Label1Buffer);
Print("copy = ",copy," rates_total = ",rates_total);
//--- 如果尝试失败-这里报道
if(copy<=0)
Print("An attempt to get the values if Custom Moving Average has failed");
//--- 为下次调用返回prev_calculated值
return(rates_total);
}
//+------------------------------------------------------------------+