赞
踩
昨天还是有点高估自己了,MACD相对较难一点,改学MA的编写,首先明确MA的计算,假如有4个值,p=[1,2, 3, 4], period=3,
则v[0]=p[0], v[1]=p[1],v[2]=(p[0]+p[1]+p[2])/3=2,
v[3]=(v[2]*3+p[3]-p[0])/3,
然后将v[3]的计算公式化简单就得到v[3]=v[2] + (p[3]-p[0])/3,把period,初始值i代进来,注意i从period开始:
v[i]=v[i-1] + (p[i]-p[i-period])/period
一句话解释即为第i个平均值的计算为i-1的位置的平均值加上一个计算值,这个计算值由第i位置的价格减去步长前的价格再除于步长period得到。
然后即可以来观察一下移动平均线的代码了:
- //+------------------------------------------------------------------+
- //| Custom Moving Average.mq5 |
- //| Copyright 2000-2024, MetaQuotes Ltd. |
- //| https://www.mql5.com |
- //+------------------------------------------------------------------+
- #property copyright "Copyright 2000-2024, MetaQuotes Ltd."
- #property link "https://www.mql5.com"
-
- //--- indicator settings
- #property indicator_chart_window
- #property indicator_buffers 2
- #property indicator_plots 2
- #property indicator_type1 DRAW_LINE
- #property indicator_color1 Red
- #property indicator_type2 DRAW_LINE
- #property indicator_color2 Silver
- //--- input parameters
- input int InpMAPeriod1=20;
- input int InpMAPeriod2=60;
- //--- indicator buffer
- double ExtLineBuffer1[];
- double ExtLineBuffer2[];
- //+------------------------------------------------------------------+
- //| Custom indicator initialization function |
- //+------------------------------------------------------------------+
- void OnInit()
- {
- //--- indicator buffers mapping
- SetIndexBuffer(0,ExtLineBuffer1,INDICATOR_DATA);
- SetIndexBuffer(1,ExtLineBuffer2,INDICATOR_DATA);
- //--- set accuracy
- IndicatorSetInteger(INDICATOR_DIGITS,_Digits+1);
- //--- set first bar from what index will be drawn
- PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,InpMAPeriod1);
- PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,InpMAPeriod2);
- //--- name for DataWindow
- string short_name ="SMA";
- IndicatorSetString(INDICATOR_SHORTNAME,short_name+"("+string(InpMAPeriod1)+")");
- IndicatorSetString(INDICATOR_SHORTNAME,short_name+"("+string(InpMAPeriod2)+")");
- //--- set drawing line empty value
- PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);
- PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,0.0);
- }
- //+------------------------------------------------------------------+
- //| Moving Average |
- //+------------------------------------------------------------------+
- int OnCalculate(const int rates_total,
- const int prev_calculated,
- const int begin,
- const double &price[])
- {
- if(rates_total<InpMAPeriod1-1+begin)
- return(0);
- //--- first calculation or number of bars was changed
- if(prev_calculated==0)
- {
- ArrayInitialize(ExtLineBuffer1,0);
- PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,InpMAPeriod1-1+begin);
- PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,InpMAPeriod2-1+begin);
- }
- //--- calculation
-
- CalculateSimpleMA(rates_total,prev_calculated,begin,price,ExtLineBuffer1, InpMAPeriod1);
- if(rates_total<InpMAPeriod2-1+begin)
- return(rates_total);
- else {
- CalculateSimpleMA(rates_total,prev_calculated,begin,price,ExtLineBuffer2, InpMAPeriod2);
- }
- //--- return value of prev_calculated for next call
- return(rates_total);
- }
- //+------------------------------------------------------------------+
- //| simple moving average |
- //+------------------------------------------------------------------+
- void CalculateSimpleMA(int rates_total,int prev_calculated,int begin,const double &price[], double &buffer[], int period)
- {
- int i,start;
- //--- first calculation or number of bars was changed
- if(prev_calculated==0)
- {
- start=period+begin;
- //--- set empty value for first start bars
- for(i=0; i<start-1; i++)
- buffer[i]=0.0;
- //--- calculate first visible value
- double first_value=0;
- for(i=begin; i<start; i++)
- first_value+=price[i];
- first_value/=period;
- buffer[start-1]=first_value;
- }
- else
- start=prev_calculated-1;
- //--- main loop
- for(i=start; i<rates_total && !IsStopped(); i++)
- buffer[i]=buffer[i-1]+(price[i]-price[i-period])/period;
- }
#property indicator_chart_window 这一行表达为主图显示,若想显示到副图,可改为#property indicator_separate_window
这一行表达为需要画两个变量:#property indicator_plots 2
这一行表达第二个画出来的变量用灰色:#property indicator_color2 Silver
程序:
input int InpMAPeriod1=20;
input int InpMAPeriod2=60;表示输入两个参数,一个为20,一个为60
初始化时需要针对变量0和变量1分别初始化:
SetIndexBuffer(0,ExtLineBuffer1,INDICATOR_DATA);
SetIndexBuffer(1,ExtLineBuffer2,INDICATOR_DATA);
代码CalculateSimpleMA中计算MA的核心代码为:
- for(i=start; i<rates_total && !IsStopped(); i++)
- buffer[i]=buffer[i-1]+(price[i]-price[i-period])/period;
- }
然后我们运行代码,会发现弹出参数,刚好是前面代码中定义的变量
运行出来的20均,60均的图形如下:
然后与通达信上的相比较,发现是一致的,说明计算无误
从目前实践的看起来,MQL5难度要显著高于通达信或是北极星什么的,唯一的好处是可以做极其复杂的计算,无限制的变量,类什么的,并可以做文件存储,网络传输,几乎能想到的一切,比如后面对接实盘时,官方对接实盘是收费的,如果你有程序化交易的程序接口,完全可在算法中直接http协议完成实盘交易。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。