自定义指标 FirstStrike.mq4-MT4指标-峰汇在线
诚信为本
量力而为
当前位置:峰汇在线 > MetaTrader 4 > MT4指标 > 正文

自定义指标 FirstStrike.mq4

FirstStrike 指标标记特定的K线高点和低点。

图表:FirstStrike 指标

源码:

//+------------------------------------------------------------------+
//|                                                  FirstStrike.mq4 |
//|                                                             Itso |
//+------------------------------------------------------------------+
#property copyright "Itso"
#property link      "http://www.125808047.com/"

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Red
#property indicator_color2 Blue

double BuffMin[];
double BuffMax[];

int Periods[] = {
   PERIOD_M1,
   PERIOD_M5,
   PERIOD_M15,
   PERIOD_M30,
   PERIOD_H1,
   PERIOD_H4,
   PERIOD_D1,
   PERIOD_W1,
   PERIOD_MN1
};
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
  if (Period()==PERIOD_M1)
  {
   Print("This indicator doesn\'t work on M1 timeframe!");
   return(0);
  }
   SetIndexStyle(0,DRAW_ARROW);
   SetIndexArrow(0,217);
   SetIndexBuffer(0,BuffMin);
   SetIndexEmptyValue(0,0.0);
   SetIndexStyle(1,DRAW_ARROW);
   SetIndexArrow(1,218);
   SetIndexBuffer(1,BuffMax);
   SetIndexEmptyValue(1,0.0);
   Comment("www.125808047.com");
   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[])
  {
   if (Period()==PERIOD_M1) return(0);
   int    counted_bars=IndicatorCounted();
   if(counted_bars>0) counted_bars--;
   int limit=Bars-counted_bars;
   double Value;
   for (int i=0; i<limit; i++)
   {
      switch(GetFirstStrike(Period(),i,Value))
      {
         case -1: 
            {
               BuffMin[i]=Value;
               break;
            }
         case  1: 
            {
               BuffMax[i]=Value;
               break;
            }
         case  0:
            {
               BuffMin[i]=Value;
               BuffMax[i]=Value;
            }
      }
   }
   return(rates_total);
  }

int GetFirstStrike(int ParentPeriod, int ParentBar, double& Value)
{
   int CurrPeriod;
   datetime StartTime, EndTime;
   int ChildFirstBar, ChildLastBar;
   int idxMin, idxMax;
   double ValMin, ValMax;
   
   for(int i=0;i<ArrayRange(Periods,0);i++)   
   {
      if(Periods[i]==ParentPeriod)
      {
         CurrPeriod=Periods[i-1];
         break;
      }
   }
   if (ParentBar==0)
   {
      StartTime=TimeCurrent();
      ChildFirstBar=0;
   }
   else
   {
      StartTime=iTime(Symbol(),ParentPeriod,ParentBar-1);
      ChildFirstBar=iBarShift(Symbol(),CurrPeriod,StartTime);
      ChildFirstBar++;
   }
   EndTime=iTime(Symbol(),ParentPeriod,ParentBar);
   ChildLastBar=iBarShift(Symbol(),CurrPeriod,EndTime);
   
   ValMin=2*Bid;
   ValMax=0;
   for(i=ChildLastBar;i>=ChildFirstBar;i--)
   {
      double CurrLow=iLow(Symbol(),CurrPeriod,i);
      double CurrHigh=iHigh(Symbol(),CurrPeriod,i);
      if(CurrLow<ValMin)
      {
         ValMin=CurrLow;
         idxMin=i;
      }
      if(CurrHigh>ValMax)
      {
         ValMax=CurrHigh;
         idxMax=i;
      }
   }
   
   if (idxMin>idxMax)
   {
      Value=ValMin;
      return(-1);
   }
   else if(idxMin<idxMax)
   {
      Value=ValMax;
      return(1);
   }
   else
   {
      if(CurrPeriod==PERIOD_M1)
      {
         Value=ValMax;
         return(0);
      }
      else
      {
         return(GetFirstStrike(CurrPeriod,idxMax,Value));
      }
   }
}
打赏
版权所有转载请注明标题及链接:峰汇在线 » 自定义指标 FirstStrike.mq4
上一篇:
下一篇:
分享到: 更多 (0)

如您觉得此文不错请扫码打赏!

支付宝扫一扫打赏

微信扫一扫打赏