Помогите разобраться с цыклами - страница 2

 
berezhnuy:
Хотя просматривается смещение показаний на сам период вычислений, TarasBY
А как определил?
 

а так?

//+------------------------------------------------------------------+
//|                                                  Custom MACD.mq4 |
//|                   Copyright 2005-2014, MetaQuotes Software Corp. |
//|                                              https://www.mql4.com |
//+------------------------------------------------------------------+
#property copyright   "2005-2014, MetaQuotes Software Corp."
#property link        "https://www.mql4.com"
#property description "Moving Averages Convergence/Divergence"
#property strict

#include <MovingAverages.mqh>

//--- indicator settings
#property  indicator_separate_window
#property  indicator_buffers 1
#property  indicator_color1  Silver
//--- indicator parameters
input int InpFastEMA=12;   // Fast EMA Period
input int InpSlowEMA=26;   // Slow EMA Period
input int cperiod=70;  // Signal SMA Period
//--- indicator buffers
double    ExtMacdBuffer[];
//--- right input parameters flag
bool      ExtParameters=false;
int per;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit(void)
  {
   IndicatorDigits(0);
//--- drawing settings
   SetIndexStyle(0,DRAW_HISTOGRAM);
//--- indicator buffers mapping
   SetIndexBuffer(0,ExtMacdBuffer);
//--- name for DataWindow and indicator subwindow label
   IndicatorShortName("MACDcross("+IntegerToString(InpFastEMA)+","+IntegerToString(InpSlowEMA)+","+IntegerToString(cperiod)+")");
   SetIndexLabel(0,"MACDcross");
//--- check for input parameters
   if(InpFastEMA<=1 || InpSlowEMA<=1 || InpFastEMA>=InpSlowEMA)
     {
      Print("Wrong input parameters");
      ExtParameters=false;
      return(INIT_FAILED);
     }
   else
      ExtParameters=true;
  per=MathMax(cperiod,InpSlowEMA);
//--- initialization done

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Moving Averages Convergence/Divergence                           |
//+------------------------------------------------------------------+
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,j,zz=0;
//---
   if(!ExtParameters)
      return(0);
//--- last counted bar will be recounted
  if(prev_calculated<=0) limit=rates_total-1-per;
  else limit=1;
//--- macd counted in the 1-st buffer
   for(i=0; i<=limit; i++)
    {
      zz=0;
      for(j=0;j<=cperiod;j++)
      {
         if ((iMA(NULL,0,InpSlowEMA,0,MODE_EMA,PRICE_CLOSE,i+j)<iMA(NULL,0,InpFastEMA,0,MODE_EMA,PRICE_CLOSE,i+j) 
         && iMA(NULL,0,InpSlowEMA,0,MODE_EMA,PRICE_CLOSE,i+j+1)>iMA(NULL,0,InpFastEMA,0,MODE_EMA,PRICE_CLOSE,i+j+1)) 
         || (iMA(NULL,0,InpSlowEMA,0,MODE_EMA,PRICE_CLOSE,i+j)>iMA(NULL,0,InpFastEMA,0,MODE_EMA,PRICE_CLOSE,i+j) 
         && iMA(NULL,0,InpSlowEMA,0,MODE_EMA,PRICE_CLOSE,i+j+1)<iMA(NULL,0,InpFastEMA,0,MODE_EMA,PRICE_CLOSE,i+j+1)))
            zz++;
      ExtMacdBuffer[i]=zz;
      }
    }
   return(rates_total);
  }
//+------------------------------------------------------------------+
 

Неправильно limit вычисляется.

Может так:

   int limit=rates_total-prev_calculated;

   if(prev_calculated==0){

      limit--;

      limit-=70;

   }

   

   for(int i=limit;i>=0;i--){ 
 

Поставил к примеру за последние 9 баров, то есть не сложно посчитать вручную, и визуально данные графика немного не соответствуют действительности. 

К примеру вот на скрине пересечений явно больше чем 1, и в тоже время не показывает даже самые последние пересечения


 
Поправил вычисление limit.
 
TarasBY так вроде все правильно работает
Причина обращения: