找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
首页论坛EAHub 外汇论坛
查看: 10|回复: 0

跨周期指标写法(以均线为例子)

[复制链接]

4万

主题

54

回帖

4万

积分

管理员

积分
45954
发表于 2026-8-26 15:13:00 | 显示全部楼层 |阅读模式
#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);

}

//+------------------------------------------------------------------+
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|EAHub外汇论坛

GMT+8, 2026-10-11 01:18 , Processed in 0.107578 second(s), 18 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表