(註:本站文案除原創外,其餘訊息来自互联网收集和整理,请自行参酌。)
2016/09/23 (五)
EA鍽程之MQL4語言:"自定義指標 " 也不難?!
自訂指標:
技術指標是一種用來輔助判斷行情的程式,按照特定的演算法經過對市場資料計算後的值在螢幕上用線條、箭頭等標注出來。
MQL4規定在同一個圖示中最多只能畫8種類型的線條或者符號,為了方便理解,我們在此稱為8個圖層。如下圖:
自訂指標又分為兩種類型,一個是在主圖中顯示,如移動平均線,一個是在副圖中顯示,如MACD。指標的原始程式碼,在这里通過理解原始程式碼 MACD.mq4 比任何論述都有效。參酌如下:
//+------------------------------------------------------------------+
//|
Custom MACD.mq4 |
//| Copyright 2005-2014,
MetaQuotes Software Corp. |
//|
http://www.mql4.com |
//+------------------------------------------------------------------+
#property copyright "2005-2014, MetaQuotes Software
Corp."
#property link "http://www.mql4.com"
#property description "Moving Averages
Convergence/Divergence"
#property strict
#include <MovingAverages.mqh>
//--- indicator settings
#property
indicator_separate_window
#property
indicator_buffers 2
#property
indicator_color1 Silver
#property
indicator_color2 Red
#property
indicator_width1 2
//--- indicator parameters
input int InpFastEMA=12; // Fast EMA Period
input int InpSlowEMA=26; // Slow EMA Period
input int InpSignalSMA=9; // Signal SMA Period
//--- indicator buffers
double
ExtMacdBuffer[];
double
ExtSignalBuffer[];
//--- right input parameters flag
bool
ExtParameters=false;
//+------------------------------------------------------------------+
//| Custom indicator initialization
function |
//+------------------------------------------------------------------+
int OnInit(void)
{
IndicatorDigits(Digits+1);
//--- drawing settings
SetIndexStyle(0,DRAW_HISTOGRAM);
SetIndexStyle(1,DRAW_LINE);
SetIndexDrawBegin(1,InpSignalSMA);
//--- indicator buffers mapping
SetIndexBuffer(0,ExtMacdBuffer);
SetIndexBuffer(1,ExtSignalBuffer);
//--- name for DataWindow and indicator
subwindow label
IndicatorShortName("MACD("+IntegerToString(InpFastEMA)+","+IntegerToString(InpSlowEMA)+","+IntegerToString(InpSignalSMA)+")");
SetIndexLabel(0,"MACD");
SetIndexLabel(1,"Signal");
//--- check for input parameters
if(InpFastEMA<=1 || InpSlowEMA<=1 || InpSignalSMA<=1 ||
InpFastEMA>=InpSlowEMA)
{
Print("Wrong input parameters");
ExtParameters=false;
return(INIT_FAILED);
}
else
ExtParameters=true;
//--- initialization done
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Moving Averages Convergence/Divergence |
//+------------------------------------------------------------------+
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 i,limit;
//---
if(rates_total<=InpSignalSMA || !ExtParameters)
return(0);
//--- last counted bar will be recounted
limit=rates_total-prev_calculated;
if(prev_calculated>0)
limit++;
//--- macd counted in the 1-st buffer
for(i=0; i<limit; i++)
ExtMacdBuffer[i]=iMA(NULL,0,InpFastEMA,0,MODE_EMA,PRICE_CLOSE,i)-
iMA(NULL,0,InpSlowEMA,0,MODE_EMA,PRICE_CLOSE,i);
//--- signal line counted in the 2-nd
buffer
SimpleMAOnBuffer(rates_total,prev_calculated,0,InpSignalSMA,ExtMacdBuffer,ExtSignalBuffer);
//--- done
return(rates_total);
}
//+------------------------------------------------------------------+