MQL4设计MFI指标问题求解

 

求大神指点:

以下代码运行没有错误,但运行时不显示指标。分析发现GetMFI函数的返回值计算正确,但是没有显示出来。但是如果输出sum1和sum2的加法,减法和乘法都是显示指标的,就包含sum1/sum2的除法不显示。哎~~

//+------------------------------------------------------------------+

//|                                                          MFI.mq4 |

//|                        Copyright 2022, MetaQuotes Software Corp. |

//|                                             https://www.mql5.com |

//+------------------------------------------------------------------+

#property copyright "Copyright 2022, MetaQuotes Software Corp."

#property link      "https://www.mql5.com"

#property version   "1.00"

#property strict

#property indicator_separate_window

#property indicator_buffers 1

#property indicator_type1 DRAW_LINE

#property indicator_style1 STYLE_SOLID

#property indicator_color1 clrAliceBlue

#property indicator_width1 1



//---


//---

input int period = 14;

//---

double MFIBugger[];


//+------------------------------------------------------------------+

//| Custom indicator initialization function                         |

//+------------------------------------------------------------------+

int OnInit()

  {

//--- indicator buffers mapping

   SetIndexBuffer(0, MFIBugger);

//---

   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[])

  {

//---

   int i, limit;

//---

   if(rates_total < period)

     {

      return(0);

     }

//---

   bool as_series_high = ArrayGetAsSeries(high);

   bool as_series_low = ArrayGetAsSeries(low);

   bool as_series_close = ArrayGetAsSeries(close);

   bool as_series_tick_volume = ArrayGetAsSeries(tick_volume);

   bool as_series_MFIBugger = ArrayGetAsSeries(MFIBugger);

   if(as_series_close)

     {

      ArraySetAsSeries(close, false);

     }

   if(as_series_high)

     {

      ArraySetAsSeries(high, false);

     }

   if(as_series_low)

     {

      ArraySetAsSeries(low, false);

     }

   if(as_series_MFIBugger)

     {

      ArraySetAsSeries(MFIBugger, false);

     }

   if(as_series_tick_volume)

     {

      ArraySetAsSeries(tick_volume, false);

     }

//---

   if(prev_calculated == 0)

     {

      limit = period;

      for(i = 0; i < limit; i++)

        {

         MFIBugger[i] = 0;

        }

      for(i = limit; i < rates_total; i++)

        {

         MFIBugger[i] = GetMFI(high, low, close, tick_volume, period, i);

        }

     }

   else

     {

      limit = prev_calculated - 1;

      for(i = limit; i < rates_total; i++)

        {

         MFIBugger[i] = GetMFI(high, low, close, tick_volume, period, i);

        }

     }

//---

   if(as_series_close)

     {

      ArraySetAsSeries(close, true);

     }

   if(as_series_high)

     {

      ArraySetAsSeries(high, true);

     }

   if(as_series_low)

     {

      ArraySetAsSeries(low, true);

     }

   if(as_series_MFIBugger)

     {

      ArraySetAsSeries(MFIBugger, true);

     }

   if(as_series_tick_volume)

     {

      ArraySetAsSeries(tick_volume, true);

     }

//--- return value of prev_calculated for next call

   return(rates_total);

  }

//+------------------------------------------------------------------+

double GetMFI(const double& high[], const double& low[], const double& close[], const long& volume[], const int count, int begin)

  {

   double typ1, typ2, sum1 = 0, sum2 = 0, mfi;


   for(int i = 1; i < count; i++)

     {

      typ1 = (high[begin - i - 1] + low[begin - i - 1] + close[begin - i - 1]) / 3;

      typ2 = (high[begin - i] + low[begin - i] + close[begin - i]) / 3;

      if(typ1 > typ2)

        {

         sum1 += typ1 * volume[begin - i - 1];

        }

      else

        {

         sum2 += typ2 * volume[begin - i];

        }

     }


   mfi=100-100/(1+sum2/sum1);

   

   return(mfi);

  }

//+------------------------------------------------------------------+


//+------------------------------------------------------------------+


Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • 2022.04.13
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
 

第一次計算時sum1等於零 所以執行會出錯 除數絕對不能等於零

修改成下述就可以運行了 會影響最遠的那根K 或是最近的一根K 你自己要看著排除 

if(sum1==0)
     sum1=sum2;
     
   mfi=100-100/(1+sum2/sum1);

return(mfi);
Desk
 
非常感谢!