Strange MT4 output...

athan
249

Hi

i ve got the following code

 

#include <stderror.mqh>
#include <stdlib.mqh>
#include <logfile.mq4>

#property indicator_level1 0
#property indicator_separate_window


// --- parameters
extern int history=180;

int init()
  {

   log_open("testground.log");
  
   return(0);
  }

int deinit()
  {
      
  log_close();
   return(0);
  }

int start()
{
   
 datetime dt,mdt,dt1,dt2; 
int  i, j, k, candles, counted_bars,per,shift,maxBars,minBars;
   
      
    counted_bars=IndicatorCounted(); // amount of bars that haven't changed since the last start() call.
   
   if (candles<0) return (0);   
         candles=Bars-counted_bars-1;                     // Index of the first uncounted

     if (candles>history-1)                             // If too many bars ..
        {
            candles=history-1;                         // ..calculate specified amount
         }
     

    dt=iTime(Symbol(),PERIOD_M1,0); //time of current bar (live candle)
   
    shift=1+MathMod( TimeMinute(dt), (Period()) );  //shift to the left of the 0 bar     
                                                       
    log("0 BAR:"+getCandleDate(dt));

    maxBars=candles*Period(); // maximum M1 bars.  Limit for j
    i=1; // bar counter. ignore zero bar. Start from 1   eg. 1..99  // for 100 candles
    
   dt=iTime(Symbol(),PERIOD_M1,  shift );   //time of the initial M1 bar. j is already shifted

   dt1=Time[i]; //search from date
   dt2=Time[i-1]-60; //to date
   
    log("Time Range, bar:"+i+"    time range: "+getCandleDate(dt1)+"  -  "+getCandleDate(dt2));    

  return(0);
}


string getCandleDate(int i)
{
   return (TimeDay(i)+"/"+TimeMonth(i)+"/"+TimeYear(i)+" "+TimeHour(i)+":"+TimeMinute(i));
}

 Now i do the following the following steps:

1. I clear the profile default folder.
2. I open the MT4 terminal and load a H1 chart. eg. USD./JPY. graph.
3. I place the indicator.

The results i m getting on the logfile the first time i m running this is 

0 BAR:0/1/1900 0:0

The second time i place the indicator sometimes works, sometimes not. Also when i m switching from H1 to M30 I m again getting a totally wrong minutes number...

Any ideas? 

William Roeder
25849
William Roeder  
// string getCandleDate(int i)
   string getCandleDate(datetime i)
athan
249
athan  
WHRoeder:


thanks for your reply i missed that one. Although this wasnt the problem. datetime is a 4byte integer same as int so i dont think it makes any difference the way i use it.

Another strange situation appears. I m following the below procedure:

 1. Clear Default
2. Load USDCHF Chart, H1 on MT4
3. Load Indicator
4. Check MT4 1M candle time manually and is 21:32

on the log i get as

0 BAR:5/2/2013 10:19 

Makes no sense!!!

When i repeat the above 1-3 steps procedure it brings up ther correct one!!!

Also following again 1-3 steps loading another chart (like XAUUSD) it brings again the  

0 BAR:0/1/1900 0:0

Any help please on this one!?? it drives me crazy!!! 

Simon Gniadkowski
17411
athanfx:


thanks for your reply i missed that one. Although this wasnt the problem. datetime is a 4byte integer same as int so i dont think it makes any difference the way i use it.

Another strange situation appears. I m following the below procedure:

 1. Clear Default
2. Load USDCHF Chart, H1 on MT4
3. Load Indicator
4. Check MT4 1M candle time manually and is 21:32

on the log i get as

0 BAR:5/2/2013 10:19 

Makes no sense!!!

When i repeat the above 1-3 steps procedure it brings up ther correct one!!!

Perhaps you do not have up to date data when you first request the information,  then when you  try again you have the data so it works as you expect,  check for error 4066,  if you get it try for the data again . . .

ERR_HISTORY_WILL_UPDATED4066Requested history data in updating state.
athan
249
athan  
RaptorUK:

Perhaps you do not have up to date data when you first request the information,  then when you  try again you have the data so it works as you expect,  check for error 4066,  if you get it try for the data again . . .

ERR_HISTORY_WILL_UPDATED4066Requested history data in updating state.

 


thanks. that could be the reason since  I ve noticed that this happens only on the first tick. aftr that it looks it works and repeats again when i m chenching TF.

does it matters that i m not using ArrayCopySeries() and i m reffering directly to M1 timeframe from a higher timeframe? Should i use ArrayCopySeries() instead?

Also will ERR_HISTORY_WILL_UPDATED work wihout ArrayCopySeries() ?

Simon Gniadkowski
17411
athanfx:


thanks. that could be the reason since  I ve noticed that this happens only on the first tick. aftr that it looks it works and repeats again when i m chenching TF.

does it matters that i m not using ArrayCopySeries() and i m reffering directly to M1 timeframe from a higher timeframe? Should i use ArrayCopySeries() instead?

Also will ERR_HISTORY_WILL_UPDATED work wihout ArrayCopySeries() ?

No, No, Yes

Before your iTime() call call GetLastEror() to clear it's variable,  then do your iTime() call then check GetLastError() if it's 4066 perform the iTime() call again,  this can be wrapped up in a while loop.  The iTime() call will initiate the data download if you don't already have it,  you may want to include a short Sleep() to give the data time to arrive.