#property strict
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots 1
#property indicator_label1 "Ma"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrRed
#property indicator_style1 STYLE_SOLID
#property indicator_width1 2
input bool AutoMatchTF = true;
extern ENUM_TIMEFRAMES TimeFrame = PERIOD_CURRENT;
input int MaPeriods = 12;
input ENUM_MA_METHOD MaMethod = MODE_EMA;
input ENUM_APPLIED_PRICE MaPrice = PRICE_CLOSE;
double MaBuffer[];
int OnInit()
{
//--- indicator buffers mapping
SetIndexBuffer(0,MaBuffer);
for(int i=0; i<1; i++)
{
SetIndexEmptyValue(i,0.0);
SetIndexDrawBegin(i,MaPeriods);
}
//--- 是否自动匹配周期
if(AutoMatchTF)
{
if(Period()==1)TimeFrame =30;
else if(Period()==5)TimeFrame =240;
else if(Period()==15||Period()==30||Period()==60)TimeFrame=PERIOD_D1;
else if(Period()==240)TimeFrame = PERIOD_W1;
else if(Period()>=1440)TimeFrame = PERIOD_MN1;
}
else
{
if(TimeFrame<=Period())TimeFrame=0;
}
string name = StringFormat("MA mtf(%s,%s,%d)",StringSubstr(EnumToString(TimeFrame),7), StringSubstr(EnumToString(MaMethod),5),MaPeriods);
IndicatorShortName(name);
IndicatorDigits(Digits);
if(MaPeriods<=0)
{
return(INIT_FAILED);
}
//---
return(INIT_SUCCEEDED);
}
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 limit=0;
if(rates_total<MaPeriods)return(0);
if(prev_calculated>0) limit = rates_total - prev_calculated +1;
else if(prev_calculated<=0)limit = rates_total;
if(TimeFrame==0)
{
for(int i=0; i<limit; i++)
{
if(i>=rates_total-1-MaPeriods-1)continue;
MaBuffer = iMA(NULL,0,MaPeriods,0,MaMethod,MaPrice,i);
}
}
else //跨周期
{
if(iBars(Symbol(),TimeFrame)<MaPeriods)return(0);
int max = iBarShift(NULL,0,iTime(NULL,TimeFrame,1));
limit = MathMax(limit,max);
for(int i=0; i<limit; i++)
{
if(i>=rates_total-1-MaPeriods-1)continue;
int shift =iBarShift(NULL,TimeFrame,time);
MaBuffer =iMA(NULL,TimeFrame,MaPeriods,0,MaMethod,MaPrice,shift);
}
}
return(rates_total);
}
//+------------------------------------------------------------------+ |