编程逆袭仔 发表于 2013-8-13 21:17:58

MQL4自定义指标编写相关函数(一)

本组函数用于设计和编写自定义指标。这些函数不能用在智能交易和脚本中。
[*]IndicatorBuffers() – 指标数据缓冲区
[*]IndicatorCounted() – 指标计数
[*]IndicatorDigits() – 设置指标精度
[*]IndicatorShortName() – 设置指标简称
[*]SetIndexArrow() – 设置指标箭头符号
[*]SetIndexBuffer() – 设置指标缓冲区位置
[*]SetIndexDrawBegin() – 设置指标线起始位置
[*]SetIndexEmptyValue() – 设置图表画线空值
[*]SetIndexLabel() – 设置指标线标签
[*]SetIndexShift() – 设置指标线偏移值
[*]SetIndexStyle() – 设置指标线样式
[*]SetLevelStyle() – 设置水平线样式
[*]SetLevelValue() – 设置水平线值

IndicatorBuffers() – 指标数据缓冲区
1
void IndicatorBuffers(int count)




为指标缓冲区分配内存,用于自定义指标计算。缓冲区的个数不能超过8个或者是小于在自定义缓冲区属性中所给出的值。如果自定义指标要求额外的缓冲区用于统计,那么这个函数必须使用指定的总缓冲区数。参数:count - 分配缓冲区的总数。缓冲区数应该在 indicator_buffers常量值 和8之间。 示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#propertyindicator_separate_window
#propertyindicator_buffers 1
#propertyindicator_color1Silver
//---- 指标参数
extern int FastEMA=12;
extern int SlowEMA=26;
extern int SignalSMA=9;
//---- 指标缓冲区
double   ind_buffer1[];
double   ind_buffer2[];
double   ind_buffer3[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
//---- 使用2个额外的缓冲区用于计算。
   IndicatorBuffers(3);
//---- 画线设置
   SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID,3);
   SetIndexDrawBegin(0,SignalSMA);
   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS)+2);
//---- 设置 3 个指标缓冲区位置
   SetIndexBuffer(0,ind_buffer1);
   SetIndexBuffer(1,ind_buffer2);
   SetIndexBuffer(2,ind_buffer3);
//----数据窗口和自定义窗口的指标标签名称
   IndicatorShortName("OsMA("+FastEMA+","+SlowEMA+","+SignalSMA+")");
//---- 初始化结束
   return(0);
}




Back to Top
IndicatorCounted() – 指标计数
1
int IndicatorCounted()




在自定义指标上次计算运行之后,本函数返回已经计算好的的柱体(K线)数量。曾计算过的柱体(K线)不必重新计算。大多数情况下,相同的指标值不需要重算,因此本函数用于优化计算。注:最新的柱体(K线)不必考虑重算,在多数情况下,这个柱子有必要重算,然而,有时会遇到边界情况,也就是在新柱体的第一个价位时从智能交易调用自定义指标。可能先前柱子的最后一个价位还没有处理完(因为在这一跳进入时上一跳还没有处理完),因为这个原因,自定义指标将不会被调用和计算。为了避免指标计算出错,IndicatorCounted()函数将返回前一个柱数。示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int start()
{
   int limit;
   int counted_bars=IndicatorCounted();
//---- 检验可能出现错误
   if(counted_bars<0) return(-1);
//---- 最后没有计算过的柱将被重算
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
//---- 主循环
   for(int i=0; i<limit; i++)
   {
      //---- ma_shift set to 0 because SetIndexShift called abowe
      ExtBlueBuffer=iMA(NULL,0,JawsPeriod,0,MODE_SMMA,PRICE_MEDIAN,i);
      ExtRedBuffer=iMA(NULL,0,TeethPeriod,0,MODE_SMMA,PRICE_MEDIAN,i);
      ExtLimeBuffer=iMA(NULL,0,LipsPeriod,0,MODE_SMMA,PRICE_MEDIAN,i);
   }
//---- 完成
   return(0);
}




Back to Top
IndicatorDigits() – 设置指标精度
1
void IndicatorDigits(int digits)




设置指标精度(小数点后位数)使其值显示直观化。货币对价格精度采用默认值,指标会添加到图表上。参数:digits - 精度要求(小数点后位数)。 示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int init()
{
//---- 使用2个额外的缓冲区用于计算。
   IndicatorBuffers(3);
//---- 画出参数设置
   SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID,3);
   SetIndexDrawBegin(0,SignalSMA);
   IndicatorDigits(Digits+2); //设置指标精度
//----设置 3 个指标缓冲区位置
   SetIndexBuffer(0,ind_buffer1);
   SetIndexBuffer(1,ind_buffer2);
   SetIndexBuffer(2,ind_buffer3);
//---- 数据窗口和自定义窗口的指标标签名称
   IndicatorShortName("OsMA("+FastEMA+","+SlowEMA+","+SignalSMA+")");
//---- 初始化完成
   return(0);
}




Back to Top
IndicatorShortName() – 设置指标简称
1
void IndicatorShortName(string name)




设置显示在数据窗口和子窗口中自定义指标的“简称”。参数:name - 设置的名称。 示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int init()
{
//----使用2个额外的缓冲区用于计算
   IndicatorBuffers(3);
//---- 画出参数设置
   SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID,3);
   SetIndexDrawBegin(0,SignalSMA);
   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS)+2);
//---- 设置 3 个指标缓冲区位置
   SetIndexBuffer(0,ind_buffer1);
   SetIndexBuffer(1,ind_buffer2);
   SetIndexBuffer(2,ind_buffer3);
//---- 数据窗口和自定义窗口的指标标签名称
   IndicatorShortName("OsMA("+FastEMA+","+SlowEMA+","+SignalSMA+")");
//---- 初始化完成
   return(0);
}




Back to Top
SetIndexArrow() – 设置指标箭头符号
1
void SetIndexArrow(int index, int code)




为 DRAW_ARROW类型 的指标线设置一个箭头符号。箭头代码 范围限于33到255之间,超过无效。参数:index - 指标线。必须是 0 - 7 之间code - 来自 Wingdings字体 或 箭头代码 的符号代码示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int init()
{
//---- 2 个指标缓冲区
    SetIndexBuffer(0,ExtUppperBuffer);
    SetIndexBuffer(1,ExtLowerBuffer);
//---- 画线参数设置
    SetIndexStyle(0,DRAW_ARROW);
    SetIndexArrow(0,217);
    SetIndexStyle(1,DRAW_ARROW);
    SetIndexArrow(1,218);
//---- 显示在DataWindow窗口
    SetIndexLabel(0,"Fractal Up");
    SetIndexLabel(1,"Fractal Down");
//---- 初始化完成
   return(0);
}




Back to Top
SetIndexBuffer() – 设置指标缓冲区位置
1
void SetIndexBuffer(int index, double array[])




将自定义指标预定义的缓冲区绑定到全局数组。需要计算指标缓冲区的个数由 IndicatorBuffers() 函数设定并且不能超过8个。如果成功,返回TRUE,否则,将返回FALSE。如果想获得更详细的信息,可以调用 GetLastError()函数。参数:index - 指标线。必须是 0 - 7 之间array[] - 存储计算指标值的数组。示例:
1
2
3
4
5
6
double ExtBufferSilver[];
int init()
{
    SetIndexBuffer(0, ExtBufferSilver); // 第1个指标线指标值的缓冲区
    // ...
}





竹乐居士 发表于 2013-12-30 22:00:20

张光铣 发表于 2014-12-24 23:20:53

赚钱,赚积分。顶....

refuse 发表于 2014-12-25 00:29:40

我靠

怀秋 发表于 2015-3-3 00:01:37

nice!!!!!!!!!!!!

陈陈美妆1 发表于 2015-3-3 07:23:46

xiexiefenxiang

傻子般的爱情 发表于 2015-3-3 07:23:51

我是大哥 发表于 2020-10-15 14:29:15

感谢楼主分享
页: [1]
查看完整版本: MQL4自定义指标编写相关函数(一)