Accessing data from other timeframes problem

 

Hi Experts,

I have written a fairly simple indicator which, when included on a chart will get data from 2 custom indicators on 3 different timeframes for the particular currency pair that is on. I then display this in a small table as shown:

The code for this is :

string chart[] = {"H1", "H4", "D1"};
int i,ydist;

int init()
  {
//---- indicators
   
    ydist = 10;  
    //set up Trade headers 
    for(i=0;i<=2;i++)
    {
    ObjectCreate("txt"+i,OBJ_LABEL,0,0,0,0);
    ObjectSet("txt"+i, OBJPROP_XDISTANCE,750);
    ObjectSet("txt"+i, OBJPROP_YDISTANCE,ydist);
    ObjectSetText("txt"+i,"Trade"+" "+chart[i],12, "Times New Roman Bold", Yellow);
    ydist+= 20;
    }
    
    ydist = 10;
    //set up label area for arrows
    for(i=0;i<=2;i++)
    {
    ObjectCreate("arrow"+i,OBJ_LABEL,0,0,0,0);
    ObjectSet("arrow"+i, OBJPROP_XDISTANCE,825);
    ObjectSet("arrow"+i, OBJPROP_YDISTANCE,ydist);
    ydist+= 20;
    }
     
    ydist = 10;
    //set up label area for signal text
    for(i=0;i<=2;i++)
    {
    ObjectCreate("sign"+i,OBJ_LABEL,0,0,0);
    ObjectSet("sign"+i,OBJPROP_XDISTANCE,875);
    ObjectSet("sign"+i,OBJPROP_YDISTANCE,ydist);
    ObjectSetText("sign"+i,"Signal: ",12, "Times New Roman Bold", Yellow);
    ydist+=20;
    }
   
    //label areas for signal values   
    ydist=10;
    for(i=0;i<=2;i++)
    {
    ObjectCreate("val"+i,OBJ_LABEL,0,0,0);
    ObjectSet("val"+i,OBJPROP_XDISTANCE,925);
    ObjectSet("val"+i,OBJPROP_YDISTANCE,ydist);
    ydist+=20;
    }
   indicators();
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+

int start()
  {
   
   indicators(); 
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+

   void indicators()
   {
   int period;
   string arrow;
   double value, signal;
   double sma_chart;
   
   for(i=0;i<=2;i++)
     {
      switch(i)
        {
         case 0 :period = PERIOD_H1;break;
         case 1 :period = PERIOD_H4;break;
         case 2 :period = PERIOD_D1;break;
        }
  
      double price;
      price = MarketInfo(Symbol(),MODE_BID);
      
      //gets the value of all custom indicators at 'close' of current candle
      value = iCustom(NULL,period,"custom1",0,0)
      signal = iCustom(NULL,period, "custom2",2,5,3,1,0);
      sma_chart = iMA(NULL,period,60,0,MODE_SMA,PRICE_CLOSE,0);
   
      //determine trade conditions & display arrows
     if(price > sma_chart && price > value && signal > 0)ObjectSetText("arrow"+i,CharToStr(217),12,"Wingdings",Green);
         else if(price < sma_chart && price < value && signal < 0){ObjectSetText("arrow"+i,CharToStr(218),12,"Wingdings",Red);
                    }else{ObjectSetText("arrow"+i,CharToStr(216),12,"Wingdings",Orange);}
                    
      //displays signal values from custom2 indicator 
      ObjectSetText("val"+i,DoubleToStr(signal,4),12, "Times New Roman Bold", Yellow);                     
      } 
     WindowRedraw(); 
     return;
This all works fine..except it takes ages to load!

So if I have this indicator dropped on to >10 charts and open MT4 it takes close to 2 minutes to open. When I change timeframes on a currency pair the chart takes more than 5 secs to load.

Can anybody offer reasons why this happens? Is it a memory allocation problem?

 

Your code doesn't compile . . .

Can you provide the custom1 and custom2 Indicators . .

 
RaptorUK:

Your code doesn't compile . . .

Can you provide the custom1 and custom2 Indicators . .


these are proprietary indicators so i don't want to put them out there. If I have them simply loaded on charts with no code running i.e no data/price access from other timeframes, there is no problem and switching between timeframes is quick. As soon as I load the full code and get it to calculate the time to change between charts increases dramatically. This is pretty frustrating for such simple code!! These indicators obviously go through the usual deinit(), init(), process when changing between timeframes and I just can't fathom out why it takes so long to them read/access the data from 3 timeframes.....
 
sd59:

these are proprietary indicators so i don't want to put them out there.

Fair enough . . . little hard to help in that case.

When it's loading take a look at Task manager and see what your Terminal is doing in terms of CPU usage. Bear in mind if you have a multi core CPU MT4 will only use one core . . . and if you have Hyperthreading enabled that will complicate things further.

 
RaptorUK:

Fair enough . . . little hard to help in that case.

When it's loading take a look at Task manager and see what your Terminal is doing in terms of CPU usage. Bear in mind if you have a multi core CPU MT4 will only use one core . . . and if you have Hyperthreading enabled that will complicate things further.

thanks for your message...yeh the CPU is at 100% when changing between timeframes but comes back to normal pretty quickly. It's just that the time lapse before loading the new timeframe is a few seconds which is not much but a pain in the a*se! I tried to make it quicker by putting the indicator calc in the init() block but doesn't seem to make any difference. Can't really understand why a simple calc like this slows things up. Actually what i really want is to put a much bigger table onto a chart which reads from >10 currency pairs across 3 timeframes - I got this to work but the load time is ridiculous!! I don't know if I should implement ArrayCopy Rates() function in case there are problems reading historical data.

I've isolated this time lag to the code that reads the data from different timeframes.....without this everything is fine so I don't think anything is wrong with the custom indicators....weird!

 

have you try running this as EA calling from function?

since and if it does not require to iterate and make calc on every bar, why even compile as indicator in the first place?

i once coded my own custom indic, and it does delay. The answer I got is bec. the mql loader will have to load icustom on every bar, which is why the use of indicatorcounted() function for indicators only.

try also, execute as a script and see how long one instance of calling takes, maybe the prb is known.



 

I haven't tried running it as an EA but I don't see how that would speed it up. I do need it to calc on every tick as I am monitoring the price relative to the indicators - I can set up alerts when certain conditions are met.

The cutom indicators are written with the indicatorcounted() function and as I mentioned if I remove the calc part of the code there is no problem i.e no delay experienced when changing between timeframes. All the calc is doing is 'reading' the value of the indicators at every tick. When it is running stand alone on a particular timeframe of a currency there is no problem, it's only when I change between timeframes there is a delay!

Do you think it has something to do with historical data access problems considering it has to always deinit(), init()...etc?

 
sd59:

I haven't tried running it as an EA but I don't see how that would speed it up. I do need it to calc on every tick as I am monitoring the price relative to the indicators - I can set up alerts when certain conditions are met.

The cutom indicators are written with the indicatorcounted() function and as I mentioned if I remove the calc part of the code there is no problem i.e no delay experienced when changing between timeframes. All the calc is doing is 'reading' the value of the indicators at every tick. When it is running stand alone on a particular timeframe of a currency there is no problem, it's only when I change between timeframes there is a delay!

Do you think it has something to do with historical data access problems considering it has to always deinit(), init()...etc?

1) historical data access prb, unless only on test mode.

2) deinit(), init(), they just run once in their "lifetime" on the charts.

Having said, your deinit() is doing nothing at all, when it should: objectsdeleteAll(), since you output objects.

 
diostar:
2) deinit(), init(), they just run once in their "lifetime" on the charts.
Every indicator and EA on the chart will call deinit/init for EVERY change of pair, timeframe, etc. But only loaded once, statics and globals are not reset.
 
WHRoeder:
Every indicator and EA on the chart will call deinit/init for EVERY change of pair, timeframe, refresh, etc. But only loaded once, statics and globals are not reset.

on refresh?

 
diostar:

on refresh?

that's exactly correct - if you look at the experts tabs every time you change a timeframe with a custom indicator you'll see it being uninit() & reloaded. Again as I mentioned there is NO time delay if ONLY the custom (drawn) indicators are being loaded. If the rest of the code is run it slows everything down!
Reason: