Daiyukai 发表于 2024-9-10 10:08:44

找副图趋势指标


bigwin 发表于 2024-9-10 14:08:50

//+------------------------------------------------------------------
#property copyright   "kevin"
#property link      "mrsun@163.com"
#property link      "https://www.mql5.com"
#property description "separate_window trend"
//+------------------------------------------------------------------
#property indicator_separate_window
#property indicator_buffers 14
#property indicator_plots   3
#property indicator_label1"趋势美国线"
#property indicator_type1   DRAW_COLOR_BARS
#property indicator_color1clrDarkGray,clrDeepSkyBlue,clrSandyBrown
#property indicator_label2"趋势蜡烛线"
#property indicator_type2   DRAW_COLOR_CANDLES
#property indicator_color2clrDarkGray,clrDeepSkyBlue,clrSandyBrown
#property indicator_label3"趋势线"
#property indicator_type3   DRAW_COLOR_LINE
#property indicator_color3clrDarkGray,clrDeepSkyBlue,clrSandyBrown
#property indicator_width32
//
//--- 输入参数
//
enum enDisplayStyle
{
   dis_automatic, // 默认形式
   dis_line,      // 趋势线
   dis_bars,      // 趋势美国线
   dis_candles    // 趋势蜡烛线
};
input int                inpPeriod       = 20;            // 趋势周期
input ENUM_APPLIED_PRICE inpPrice      = PRICE_CLOSE;   // 价格源值
input enDisplayStyle   inpDisplayStyle = dis_automatic; // 趋势显示风格

                                                          //
//--- 缓冲区
//
double canh[],canl[],cano[],canc[],cancl[],baro[],barh[],barl[],barc[],barcl[],line[],linecl[],hull[],hullcl[];
//+------------------------------------------------------------------+
//|            指标初始化函数                         |
//+------------------------------------------------------------------+
int OnInit()
{
//--- indicator buffers mapping
   SetIndexBuffer(0,baro,INDICATOR_DATA);
   SetIndexBuffer(1,barh,INDICATOR_DATA);
   SetIndexBuffer(2,barl,INDICATOR_DATA);
   SetIndexBuffer(3,barc,INDICATOR_DATA);
   SetIndexBuffer(4,barcl,INDICATOR_COLOR_INDEX);
   SetIndexBuffer(5,cano,INDICATOR_DATA);
   SetIndexBuffer(6,canh,INDICATOR_DATA);
   SetIndexBuffer(7,canl,INDICATOR_DATA);
   SetIndexBuffer(8,canc,INDICATOR_DATA);
   SetIndexBuffer(9,cancl,INDICATOR_COLOR_INDEX);
   SetIndexBuffer(10,line,INDICATOR_DATA);
   SetIndexBuffer(11,linecl,INDICATOR_COLOR_INDEX);
   SetIndexBuffer(12,hull,INDICATOR_CALCULATIONS);
   SetIndexBuffer(13,hullcl,INDICATOR_CALCULATIONS);
//---
   IndicatorSetString(INDICATOR_SHORTNAME,"Hull trend ("+(string)inpPeriod+")");
   return (INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//|   去初始化函数                  |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
}
//+------------------------------------------------------------------+
//| 自定义指标选项                              |
//+------------------------------------------------------------------+
#define displayLine   0
#define displayBars   1
#define displayCandle 2
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
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(Bars(_Symbol,_Period)<rates_total) return(prev_calculated);
   int limit=prev_calculated-1;
   static int prevDisplayType = -1;
   int currDisplayType = -1;
   switch(inpDisplayStyle)
   {
      case dis_line :      currDisplayType = CHART_LINE;    break;
      case dis_bars :      currDisplayType = CHART_BARS;    break;
      case dis_candles :   currDisplayType = CHART_CANDLES; break;
      case dis_automatic : currDisplayType = (int)ChartGetInteger(0,CHART_MODE);
   }
   if(currDisplayType!=prevDisplayType)
   {
      limit=0; prevDisplayType=currDisplayType;
   }
   int i=(int)MathMax(limit,0); for(; i<rates_total && !_StopFlag; i++)
   {
      hull   = iHull(getPrice(inpPrice,open,close,high,low,i,rates_total),inpPeriod,i,rates_total);
      hullcl = (i>0) ? (hull>hull) ? 1 : (hull<hull) ? 2 : hullcl : 0;
      baro = barh = barl = barc = EMPTY_VALUE;
      cano = canh = canl = canc = EMPTY_VALUE;
      line = EMPTY_VALUE;
      switch(currDisplayType)
      {
         case CHART_BARS :
            barh= high;
            barl= low;
            barc= close;
            baro= open;
            barcl = hullcl;
            break;
         case CHART_CANDLES :
            canh= high;
            canl= low;
            canc= close;
            cano= open;
            cancl = hullcl;
            break;
         case CHART_LINE :
            line=hull;
            linecl=hullcl;
      }
   }
   return (i);
}
//+------------------------------------------------------------------+
//|   自定义函数                                                |
//+------------------------------------------------------------------+
double workHull[];
//
//---
//
double iHull(double price,double period,int r,int bars,int instanceNo=0)
{
   if(ArrayRange(workHull,0)!=bars) ArrayResize(workHull,bars);
   instanceNo*=2; workHull=price;
   if(period<=1) return(price);
//
//---
//
   int HmaPeriod= (int)MathMax(period,2);
   int HalfPeriod = (int)MathFloor(HmaPeriod/2);
   int HullPeriod = (int)MathFloor(MathSqrt(HmaPeriod));
   double hma,hmw,weight;
   hmw=HalfPeriod; hma=hmw*price;
   for(int k=1; k<HalfPeriod && (r-k)>=0; k++)
   {
      weight = HalfPeriod-k;
      hmw   += weight;
      hma   += weight*workHull;
   }
   workHull=2.0*hma/hmw;
   hmw=HmaPeriod; hma=hmw*price;
   for(int k=1; k<period && (r-k)>=0; k++)
   {
      weight = HmaPeriod-k;
      hmw   += weight;
      hma   += weight*workHull;
   }
   workHull-=hma/hmw;
   hmw=HullPeriod; hma=hmw*workHull;
   for(int k=1; k<HullPeriod && (r-k)>=0; k++)
   {
      weight = HullPeriod-k;
      hmw   += weight;
      hma   += weight*workHull;
   }
   return(hma/hmw);
}
//
//---
//
double getPrice(ENUM_APPLIED_PRICE tprice,const double &open[],const double &close[],const double &high[],const double &low[],int i,int _bars)
{
   switch(tprice)
   {
      case PRICE_CLOSE:   return(close);
      case PRICE_OPEN:      return(open);
      case PRICE_HIGH:      return(high);
      case PRICE_LOW:       return(low);
      case PRICE_MEDIAN:    return((high+low)/2.0);
      case PRICE_TYPICAL:   return((high+low+close)/3.0);
      case PRICE_WEIGHTED:return((high+low+close+close)/4.0);
   }
   return(0);
}
//+------------------------------------------------------------------+


页: [1]
查看完整版本: 找副图趋势指标