昨天還是有點高估自己了,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 mappingSetIndexBuffer(0,ExtLineBuffer1,INDICATOR_DATA);SetIndexBuffer(1,ExtLineBuffer2,INDICATOR_DATA);
//--- set accuracyIndicatorSetInteger(INDICATOR_DIGITS,_Digits+1);
//--- set first bar from what index will be drawnPlotIndexSetInteger(0,PLOT_DRAW_BEGIN,InpMAPeriod1);PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,InpMAPeriod2);
//--- name for DataWindowstring short_name ="SMA";IndicatorSetString(INDICATOR_SHORTNAME,short_name+"("+string(InpMAPeriod1)+")");IndicatorSetString(INDICATOR_SHORTNAME,short_name+"("+string(InpMAPeriod2)+")");
//--- set drawing line empty valuePlotIndexSetDouble(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 changedif(prev_calculated==0){ArrayInitialize(ExtLineBuffer1,0);PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,InpMAPeriod1-1+begin);PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,InpMAPeriod2-1+begin);}
//--- calculationCalculateSimpleMA(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 callreturn(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 changedif(prev_calculated==0){start=period+begin;//--- set empty value for first start barsfor(i=0; i<start-1; i++)buffer[i]=0.0;//--- calculate first visible valuedouble first_value=0;for(i=begin; i<start; i++)first_value+=price[i];first_value/=period;buffer[start-1]=first_value;}elsestart=prev_calculated-1;
//--- main loopfor(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協議完成實盤交易。