Array's Element hasn't value (MT4)

 

Hi, guys .

I'm trying to convert Indicator to Expert Advisor , but my code isn't working.

In the 2nd Loop , ExtMapBuffer Array's Element has value. ( Ex  : ExtMapBuffer [x] = 111.1223)

In the 3rd Loop, although ExtMapBuffer ArraySize is  still not changed,

but ExtMapBuffer Array's Element hasn't value. Ex  : ExtMapBuffer [x] = 0.0)


Here is my code.

Hope you can help me.

//+------------------------------------------------------------------+
//|                                                         Demo.mq4 |
//|                   Copyright 2005-2014, MetaQuotes Software Corp. |
//|                                              http://www.mql4.com |
//+------------------------------------------------------------------+
#property copyright   "2005-2014, MetaQuotes Software Corp."
#property link        "http://www.mql4.com"

double Lots          =0.1;

//---- input parameters 
int period=21; // period
int method=3; // MODE_LWMA 
int price=0; // PRICE_CLOSE 

//---- buffers 
double Uptrend[];
double Dntrend[];
double ExtMapBuffer[]; 


int init() 
{ 


       ArraySetAsSeries(Uptrend, false);
       ArraySetAsSeries(Dntrend, false);
       ArraySetAsSeries(ExtMapBuffer, false);
       //----  
       ArrayResize(Uptrend, Bars); 
       ArrayResize(Dntrend, Bars); 
       ArrayResize(ExtMapBuffer, Bars); 
       //----
       ArraySetAsSeries(Uptrend, true);
       ArraySetAsSeries(Dntrend, true);
       ArraySetAsSeries(ExtMapBuffer, true); 
   return(0); 
} 


int deinit() 
{ 
   return(0); 
} 

int start()
{
   int counted_bars = iBars("USDJPY", PERIOD_M5);
   if(counted_bars < 0) 
      return(-1); 
   
   int x = 0; 
   int periodSprt = MathSqrt(period); 
   int e = Bars - counted_bars + period + 1; 
   if(e > Bars) 
      e = Bars;  
   
   
   double vect[], trend[]; 
   ArrayResize(vect, e); 
   ArraySetAsSeries(vect, true);
   ArrayResize(trend, e); 
   ArraySetAsSeries(trend, true); 

   
   for(x = e-1; x >= 0; x--) 
      vect[x] = iMA(NULL, 0, period/2, 0, MODE_LWMA, PRICE_CLOSE, x)*2 - iMA(NULL, 0, period, 0, MODE_LWMA, PRICE_CLOSE, x); 

   for(x = 0; x < e-period; x++)
      ExtMapBuffer[x] = iMAOnArray(vect, 0, periodSprt, 0, MODE_LWMA, x);
   
   for(x = e-period; x >= 0; x--)
   { 
      trend[x] = trend[x+1];
      if (ExtMapBuffer[x]> ExtMapBuffer[x+1]) trend[x] =1;
      if (ExtMapBuffer[x]< ExtMapBuffer[x+1]) trend[x] =-1;
      
      if (trend[x]>0) {
         Uptrend[x] = ExtMapBuffer[x]; 
         if (trend[x+1]<0) {
            Uptrend[x+1]=ExtMapBuffer[x+1];
         }
         Dntrend[x] = EMPTY_VALUE;
      }
      
      else if (trend[x]<0) { 
         Dntrend[x] = ExtMapBuffer[x]; 
         if (trend[x+1]>0) {
            Dntrend[x+1]=ExtMapBuffer[x+1];

         }
         Uptrend[x] = EMPTY_VALUE;
      }
   }

   return(0); 
}
 
  1. awlex redc: I'm trying to convert Indicator to Expert Advisor , but my code isn't working.
    Don't try to do that. There are no buffers, no IndicatorCounted() or prev_calculated. No way to know if older bars have changed or been added (history update.)
    Just get the value(s) of the indicator(s) into the EA (using iCustom) and do what you want with it.
    You should encapsulate your iCustom calls to make your code self-documenting.
              Detailed explanation of iCustom - MQL4 programming forum

  2.    int counted_bars = iBars("USDJPY", PERIOD_M5);
    On MT4: Unless the current chart is that specific symbol(s)/TF(s) referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
              Download history in MQL4 EA - Forex Calendar - MQL4 programming forum - Page 3 #26 № 4

    The function linked to, opens a hidden chart for the symbol/TF in question (if not already open,) thus updating history, and temporarily placing the symbol on Market Watch (if not already there,) so SymbolInfoDouble(symbol, SYMBOL_BID) or MarketInfo(symbol, MODE_BID) don't also return zero on the first call.

  3.    int counted_bars = iBars("USDJPY", PERIOD_M5);
       int e = Bars - counted_bars + period + 1; 
          vect[x] = iMA(NULL, 0, period/2, 0, MODE_LWMA, PRICE_CLOSE, x)*2
    You are mixing apples and oranges.

  4. Why did you post your MT4 question in the Root / MT5 General section instead of the MQL4 section, (bottom of the Root page?)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
    Next time post in the correct place. The moderators will likely move this thread there soon.