Problem when replace iMA with iMAOnArray

 

original code: why the result is different?It should be the same!

for (i=bars;i>=0;i--)
   {
      ma     = iMA("NZDUSD",0,periodMA,0,0,PRICE_MEDIAN,i);//replace with iMAONArrey
for (i=bars;i>=0;i--)
   {
   double haHigh = iHigh("NZDUSD",0,i);
   double haLow  = iLow("NZDUSD",0,i);
   double haClose= iClose("NZDUSD",0,i);
   double haMEDIAN=(haHigh+haLow)/2;
   ExtMapBuffer5[i] = (haHigh+haLow)/2;
   //CloseBuffer[i] =  haClose;
   
   }
   for (i=bars;i>=0;i--)
   {
      ma     = iMAOnArray(ExtMapBuffer5,0,periodMA,0,mode1,i);//iMAOnArray(ExtMapBuffer5,0,periodMA,0,mode1,i);iMA(NULL,NULL,periodMA,0,mode1,price1,i);
      s = signal;       price = (High[i]+Low[i])/2;       if (price <ma ) s = SIGNAL_SHORT;       if (price >ma ) s = SIGNAL_LONG;       // calc min/max          if (signal != SIGNAL_NONE)       {           if (price< min) min = price;           if (price > max) max = price;       }       if (s != signal)       {           if (gTradeID >= 0)           {               gTradeMin[gTradeID] = min;               gTradeMax[gTradeID] = max;               gTradeEnd[gTradeID] = i;               gTradeCmd[gTradeID] = s;           }           gTradeID++;           gTradeOpen[gTradeID] = price;           gTradeStart[gTradeID] = i;           gTradeEnd[gTradeID] = 0;           min = 99999;           max = -9999;           profit = 0;           if (signal == SIGNAL_SHORT)           {             profit = open-price;             profit /= Point;             profit -= spread;             totalProfit += profit;                        }                        if (signal == SIGNAL_LONG)             {             profit = price-open;             profit /= Point;             profit -= spread;             totalProfit += profit;           }                        gOpenTime = Time[i];           openid = i;           signal = s;           open = price;                  }    }
 
sunyc1982:

original code: why the result is different?It should be the same!

Yes, it's the same.

There is a mistake in your program.

Try this one.

 {
//---
   int i,limit;

   if(prev_calculated<0)
      return(-1);
   if(prev_calculated==0)
      limit=rates_total-periodMA;
   else
      limit=rates_total-prev_calculated;

   for(i=limit; i>=0; i--)
     {
      Line1_Buffer[i]=iMA("NZDUSD",0,periodMA,0,MODE_SMA,PRICE_MEDIAN,i);  //replace with iMAONArrey

      double haHigh = iHigh("NZDUSD", 0, i);
      double haLow  =  iLow("NZDUSD", 0, i);

      ExtMapBuffer5[i]=(haHigh+haLow)/2;
     }

   for(i=limit; i>=0; i--)
      Line2_Buffer[i]=iMAOnArray(ExtMapBuffer5,0,periodMA,0,MODE_SMA,i);
//---
   return(rates_total);
  }
 
Naguisa Unada:

Yes, it's the same.

There is a mistake in your program.

Try this one.

Hello,you may misunderstand what I mean.In the code I want to replace "  ma     = iMA("NZDUSD",0,periodMA,0,0,PRICE_MEDIAN,i);  "  

with "   ma     = iMAOnArray(ExtMapBuffer5,0,periodMA,0,mode1,i);   ".And I make some change,But it get different result.Sorry,I hope I make myself clear this time.

How to define prev_calculated?

 

@sunyc1982 Do not edit your original code to incorporate improvements. Readers who happen to come across later will have no clue what your original problem was all about.

Instead, add the modified code in a new post and explain there why you still face problems.

 

OK,let me chang a way to ask .Here is the code.ma[i](white) and ma3[i](blue) should be the same line.I use mql4 only.

#property indicator_chart_window

#property indicator_buffers 5
#property indicator_color1 Aqua
#property indicator_color2 Yellow
#property indicator_color3 Blue
extern double times = 3.0;
extern int MaxHistory = 200;
extern int aaa = 0;

double ma[];
double ma2[];
double ma3[];
double ma4[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
SetIndexStyle(0, DRAW_LINE);
SetIndexStyle(1, DRAW_LINE);
SetIndexStyle(2, DRAW_LINE);
SetIndexStyle(3, DRAW_LINE);

SetIndexBuffer(0, ma);
SetIndexBuffer(1, ma2);
SetIndexBuffer(2, ma3);
SetIndexBuffer(3, ma4);



return(0);
}
//+----op;olikjiojiljuhhhhhhhjuiiiiiiiiiiouukkkkkjjjjjjjjjjjjjjjuopuuuuuuy8888888877--------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{

return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start(){

string basename = "DDT";
IndicatorShortName(basename);
int window = WindowFind(basename);

   int counted_bars=IndicatorCounted();
   int limit = Bars-counted_bars-1;
   if(limit>MaxHistory-1) limit=MaxHistory-1;
   for(int i=limit;i>=0;i--){
     ma[i] = iMA(NULL,0,25,0,0,PRICE_MEDIAN,i);
     
   }
   
   for( i=limit;i>=0;i--){
   double haHigh = iHigh(NULL,0,i);
   double haLow  = iLow(NULL,0,i);
   ma2[i] = (haHigh+haLow)/2;
   }
   
   for(i=limit;i>=0;i--){
     ma3[i] = iMAOnArray(ma2,0,25,0,0,i);
     
   }

   
   
   
return(0);
}

But after run it looks like.Blue and the white should be one line

What should I change in the code?thank U

 


Are you talking about the lines in this ellipse? That is because the calculation range is different.

Your program format is too old.

I show you a new one.

#property version   "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   2
//--- plot ma1
#property indicator_label1  "ma1"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrLimeGreen
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- plot ma3
#property indicator_label2  "ma3"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrMagenta
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1
//--- input parameters
input int      periodMA=25; // MA Period
//--- indicator buffers
double         ma1[];
double         ma2[];
double         ma3[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   IndicatorBuffers(3);
   SetIndexBuffer(0,ma1);
   SetIndexBuffer(1,ma3);
   SetIndexBuffer(2,ma2);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
   return(0);
  }
//+------------------------------------------------------------------+
//| 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(prev_calculated<0)
      return(-1);
   if(prev_calculated==0)
      limit=rates_total-periodMA;
   else
      limit=rates_total-prev_calculated;

   for(i=limit; i>=0; i--)
      ma1[i]=iMA(NULL,0,periodMA,0,MODE_SMA,PRICE_MEDIAN,i);

   for(i=limit; i>=0; i--)
     {
      /*
      double haHigh = iHigh(NULL, 0, i);
      double haLow  =  iLow(NULL, 0, i);

      ma2[i]=(haHigh+haLow)/2;
      */

      ma2[i]=(high[i]+low[i])/2;
     }

   for(i=limit; i>=0; i--)
      ma3[i]=iMAOnArray(ma2,0,periodMA,0,MODE_SMA,i);
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
Reason: