打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
看MT4代码,学编程 一

1。指标代码解释

//例1

//+------------------------------------------------------------------+

//双些线后是单行注释,用于注解,自用说明。/*和*/包起来实现多行注释,记录自己的说明介绍,编程使用记录等

//MQL4语言基本服从C语言的规则-----------注意目前MetaEditor处理不好多字节代码,所以不要在代码中使用中文和中文空格-------------+

//每个指标文件只是至少包括三个部分(1)property 和参数,数组声明,(2)初始化函数nit(), (3)主函数start()

//property 是各种说明信息

//最重要必须的是这三种,(1)说明指标将画在价格窗口还是独立的窗口

//(2)有多少个(1~7)储存指标数据的数组,(3)说明对应将画指标的绘画颜色,编号1~7

#property indicator_chart_window

#property indicator_buffers 1

#property indicator_color1 Red

//---- 可设置的参数,可根据需要,由使用者设置

extern int MA_Period=13;

extern int MA_Shift=0;

extern int MA_Method=2;

extern int MA_Price = 6;

/* MA_Method =

MODE_SMA 0 Simple moving average,

MODE_EMA 1 Exponential moving average,

MODE_SMMA 2 Smoothed moving average,

MODE_LWMA 3 Linear weighted moving average.

*/

/* MA_Price =

PRICE_CLOSE 0 Close price.

PRICE_OPEN 1 Open price.

PRICE_HIGH 2 High price.

PRICE_LOW 3 Low price.

PRICE_MEDIAN 4 Median price, (high+low)/2.

PRICE_TYPICAL 5 Typical price, (high+low+close)/3.

PRICE_WEIGHTED 6 Weighted close price, (high+low+close+close)/4.

*/

//数组,储存指标数据

double Buffer0[];

//----

//+------------------------------------------------------------------+

//| 初始化准备函数,装入时调用一次               |

//+------------------------------------------------------------------+

int init()

{

//-设置编号为0的线的线形等参数, 0~6,对应indicator_color1~7

SetIndexStyle(0,DRAW_LINE);

//---- 设置编号为0的线 与数组的对应关系, 0~6

SetIndexBuffer(0,Buffer0);

return(0);

}

//+------------------------------------------------------------------+

//|                                             |

int start()   //指标计算主函数,每次计算调用

{

ma();

return(0);

}

//+------------------------------------------------------------------+

//|自定义函数,这里只是直接使用库函数实现MA, 若你自己计算,可设计任何指标                           |

//+------------------------------------------------------------------+

void ma()

{

int pos=Bars;   //Bars = Number of bars in the current chart.当前窗口中的蜡烛数

while(pos>=0)

{

Buffer0[pos]=iMA(NULL,0,MA_Period,MA_Shift,MA_Method,MA_Price,pos);

pos--;

}

}

//

///----------------------------------------------------------------------

复制代码

例2

//+------------------------------------------------------------------+

//双些线后是单行注释,用于注解,自用说明。/*和*/包起来实现多行注释,记录自己的说明介绍,编程使用记录等

//MQL4语言基本服从C语言的规则-----------注意目前MetaEditor处理不好多字节代码,所以不要在代码中使用中文和中文空格-------------+

//每个指标文件只是至少包括三个部分(1)property 和参数,数组声明,(2)初始化函数nit(), (3)主函数start()

//property 是各种说明信息

//最重要必须的是这三种,(1)说明指标将画在价格窗口还是独立的窗口

//(2)有多少个(1~7)储存指标数据的数组,(3)说明对应将画指标的绘画颜色,编号1~7

#property indicator_separate_window

#property indicator_buffers 7

#property indicator_color1 Red

#property indicator_color2 Yellow

#property indicator_color3 Blue

#property indicator_color4 Green

#property indicator_color5 Gray

#property indicator_color6 SkyBlue

#property indicator_color7 Tan

//---- 可设置的参数,可根据需要,由使用者设置

extern int MA_Period=13;

extern int MA_Shift=0;

extern int MA_Method=2;

extern int MA_Price = 6;

//数组,储存指标数据

double Buffer0[];

double Buffer1[];

double Buffer2[];

double Buffer3[];

double Buffer4[];

double Buffer5[];

double Buffer6[];

//----

//+------------------------------------------------------------------+

//| 初始化准备函数,装入时调用一次               |

//+------------------------------------------------------------------+

int init()

{

//-设置编号为0的线的线形等参数, 0~6,对应indicator_color1~7

SetIndexStyle(0,DRAW_LINE);

SetIndexStyle(1,DRAW_LINE);

SetIndexStyle(3,DRAW_LINE);

SetIndexStyle(4,DRAW_LINE);

SetIndexStyle(5,DRAW_LINE);

SetIndexStyle(6,DRAW_LINE);

//---- 设置编号为0的线 与数组的对应关系, 0~6

SetIndexBuffer(0,Buffer0);

SetIndexBuffer(1,Buffer1);

SetIndexBuffer(2,Buffer2);

SetIndexBuffer(3,Buffer3);

SetIndexBuffer(4,Buffer4);

SetIndexBuffer(5,Buffer5);

SetIndexBuffer(6,Buffer6);

return(0);

}

//+------------------------------------------------------------------+

//|                                             |

int start()   //指标计算主函数,每次计算调用

{

ma();

ma1();

return(0);

}

//+------------------------------------------------------------------+

//|自定义函数,这里只是直接使用库函数实现MA, 若你自己计算,可设计任何指标                           |

//+------------------------------------------------------------------+

void ma()

{

int pos=Bars;   //Bars = Number of bars in the current chart.当前窗口中的蜡烛数

while(pos>=0)

{

Buffer0[pos]=iMA(NULL,0,MA_Period,MA_Shift,MA_Method,MA_Price,pos);

Buffer1[pos]=iMA(NULL,0,MA_Period*2,MA_Shift,MA_Method,MA_Price,pos);

Buffer2[pos]=iMA(NULL,0,MA_Period*3,MA_Shift,MA_Method,MA_Price,pos);

pos--;

}

}

void ma1()

{

int pos=Bars;   //Bars = Number of bars in the current chart.当前窗口中的蜡烛数

while(pos>=0)

{

Buffer3[pos]=iMA(NULL,0,MA_Period*4,MA_Shift,MA_Method,MA_Price,pos);

Buffer4[pos]=iMA(NULL,0,MA_Period*5,MA_Shift,MA_Method,MA_Price,pos);

Buffer5[pos]=iMA(NULL,0,MA_Period*6,MA_Shift,MA_Method,MA_Price,pos);

Buffer6[pos]=iMA(NULL,0,MA_Period*7,MA_Shift,MA_Method,MA_Price,pos);

pos--;

}

}

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
会变色的均线
MQL4课程11
双线MACD指标算法原理
MT4编程实例:在欧元图上显示英磅的RSI指标
MT4编程初级手册(10):一条指标线的实例
《MQL4实用编程》读书笔记(8)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服