//--- input parameters input int InpFastMa = 5; // Fast ma perid input int InpSlowMa = 10; // Slow ma perid input int InpLongMa = 20; // Long ma perid input ENUM_APPLIED_PRICE InpAppliedPrice = PRICE_CLOSE; // Applied price //--- Ma handles int ExtFastMaHandle; int ExtSlowMaHandle; int ExtLongMaHandle;
新人MQL5学习中,代码每次运行的结果 都不一样,
画了三条线, 有时间一条线, 有时间两条线 有时没得线..
//#property indicator_separate_window // 单独绘图窗口
#property indicator_chart_window
//#property indicator_minimum -100 // 窗口最小轴
//#property indicator_maximum 100 // 窗口最大轴
#property indicator_buffers 6 // 一共有几个缓存数组
#property indicator_plots 3 // 几种图形
//--- plot Line
#property indicator_label1 "Line"
#property indicator_type1 DRAW_COLOR_LINE
#property indicator_color1 clrWhite
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
double LineBuffer[]; // 绘制线的缓存区
double LineColorsBuffer[];
//--- plot lineTwo
#property indicator_label2 "LineTwo"
#property indicator_type2 DRAW_COLOR_LINE
#property indicator_color2 clrWhite
#property indicator_style2 STYLE_SOLID
#property indicator_width2 1
double LineTwoBuffer[];
double LineTwoColorsBuffer[];
//--- plot lineThe
#property indicator_label3 "LineThe"
#property indicator_type3 DRAW_COLOR_LINE
#property indicator_color3 clrWhite
#property indicator_style3 STYLE_SOLID
#property indicator_width3 1
double LineTheBuffer[];
double LineTheColorBuffer[];
//--- input parameters
input int InpFastMa = 5; // Fast ma perid
input int InpSlowMa = 10; // Slow ma perid
input int InpLongMa = 20; // Long ma perid
input ENUM_APPLIED_PRICE InpAppliedPrice = PRICE_CLOSE; // Applied price
//--- Ma handles
int ExtFastMaHandle;
int ExtSlowMaHandle;
int ExtLongMaHandle;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- indicator buffers mapping
SetIndexBuffer( 0, LineBuffer, INDICATOR_DATA );
SetIndexBuffer( 1, LineColorsBuffer, INDICATOR_COLOR_INDEX );
SetIndexBuffer( 2, LineTwoBuffer, INDICATOR_DATA );
SetIndexBuffer( 3, LineTwoColorsBuffer, INDICATOR_COLOR_INDEX );
SetIndexBuffer( 4, LineTheBuffer, INDICATOR_DATA );
SetIndexBuffer( 5, LineTheColorBuffer, INDICATOR_COLOR_INDEX );
ExtFastMaHandle = iMA( NULL, 0, InpFastMa, 0, MODE_EMA, InpAppliedPrice );
ExtSlowMaHandle = iMA( NULL, 0, InpSlowMa, 0, MODE_EMA, InpAppliedPrice );
ExtLongMaHandle = iMA( NULL, 0, InpLongMa, 0, MODE_EMA, InpAppliedPrice );
//-- 返回访问数组时间[] - 像时间序列一样
//ArraySetAsSeries( LineBuffer, true );
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
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[])
{
//--- check for data
if( InpFastMa > rates_total ){
return( 0 );
}
int _calculated = 0;
_calculated = BarsCalculated( ExtFastMaHandle );
if( _calculated <= 0 ){
Print("Not all data of ExtFastMaHandle is calculated (",_calculated,"bars ). Error",GetLastError());
return(0);
}
_calculated = BarsCalculated( ExtSlowMaHandle );
if( _calculated <= 0 ){
Print("Not all data of ExtSlowMaHandle is calculated (",_calculated,"bars ). Error",GetLastError());
return(0);
}
_calculated = BarsCalculated( ExtLongMaHandle );
if( _calculated <= 0 ){
Print("Not all data of ExtLongMaHandle is calculated (",_calculated,"bars ). Error",GetLastError());
return(0);
}
//--- we can copy net all data
PrintFormat( "prev_calculated == %d, rates_total == %d ", prev_calculated, rates_total );
int _to_copy;
if( prev_calculated > rates_total || prev_calculated < 0 ){
_to_copy = rates_total;
} else {
_to_copy = rates_total - prev_calculated;
if( prev_calculated > 0 ){
_to_copy++;
}
}
Print( "_to_copy === ", _to_copy );
//--- get Fast EMA buffer
if( IsStopped() ){
Print( "Check is Stopped " + __FILE__ + __LINE__ );
return(0);
}
if( CopyBuffer( ExtFastMaHandle, 0, 0, rates_total, LineBuffer ) < 0 ){
Print("Getting FAST EMA is failed! Error", GetLastError());
return(0);
}
if( CopyBuffer( ExtSlowMaHandle, 0, 0, rates_total, LineTwoBuffer ) < 0 ){
Print("Getting Slow EMA is failed! Error", GetLastError());
return(0);
}
if( CopyBuffer( ExtLongMaHandle, 0, 0, rates_total, LineTheBuffer ) < 0 ){
Print("Getting Long EMA is failed! Error", GetLastError());
return(0);
}
//---
int _limit =( prev_calculated > 0 ) ? 1 : ( prev_calculated - 1 );
Print( " calu _limit scope /'_limit == /' %d ", _limit );
//--- return value of prev_calculated for next call
return(rates_total);
}