//+------------------------------------------------------------------+ //| TestCopyBuffer1.mq5 | //| Copyright 2009, MetaQuotes Software Corp. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "2009, MetaQuotes Software Corp." #property link "https://www.mql5.com" #property version "1.00" #property indicator_separate_window #property indicator_buffers 1 #property indicator_plots 1 //---- 图 MA #property indicator_label1 "MA" #property indicator_type1 DRAW_LINE #property indicator_color1 clrRed #property indicator_style1 STYLE_SOLID #property indicator_width1 1 //--- 输入参量 input bool AsSeries=true; input int period=15; input ENUM_MA_METHOD smootMode=MODE_EMA; input ENUM_APPLIED_PRICE price=PRICE_CLOSE; input int shift=0; //--- 指标缓冲 double MABuffer[]; int ma_handle; //+------------------------------------------------------------------+ //| 自定义指标初始化函数 | //+------------------------------------------------------------------+ int OnInit() { //--- 指标缓冲绘图 if(AsSeries) ArraySetAsSeries(MABuffer,true); Print("Indicator buffer is timeseries = ",ArrayGetAsSeries(MABuffer)); SetIndexBuffer(0,MABuffer,INDICATOR_DATA); Print("Indicator buffer after SetIndexBuffer() is timeseries = ", ArrayGetAsSeries(MABuffer)); //--- 更待指标缓冲访问单元命令 ArraySetAsSeries(MABuffer,AsSeries); IndicatorSetString(INDICATOR_SHORTNAME,"MA("+period+")"+AsSeries); //--- ma_handle=iMA(Symbol(),0,period,shift,smootMode,price); 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[]) { //--- 复制缓冲区MABuffer中的移动平均值 int copied=CopyBuffer(ma_handle,0,0,rates_total,MABuffer); Print("MABuffer[0] = ",MABuffer[0]);// 取决于AsSeries值 // 将收到旧值 // 或者为当前未完成的字节 //--- 为下一次调用返回prev_calculated值 return(rates_total); } //+------------------------------------------------------------------+ |