Download MetaTrader 5

Speed of Bars function

To add comments, please log in or register
Did you like the article? Share it with your friends!
fxtrader1081
14
fxtrader1081 2015.03.24 10:21 

I am having a hard time with my ea being slow and after using the profiler, it seems the cause is the Bars function.  Now, the bars function is being called in the Function I am using t ge the time of x bar.  That is because as I understand it, I am using the CiTime class to get the time and as I understand it I must resize the buffer using the CiTime.BufferResize function unless am missing something.  Here is the code.

 

 CList timesLst;

datetime GetTime(string smbl, ENUM_TIMEFRAMES tf, int cndl)

{

   int bars = Bars(smbl,tf);

   int tot = timesLst.Total();

   datetime toRet[];

   for(int i=0;i<tot;i++)

   {

      CiTime *cit = timesLst.GetNodeAtIndex(i);

      string smblHere = cit.Symbol();

      ENUM_TIMEFRAMES tfHere = cit.Timeframe(); 

      if(smblHere == smbl && tfHere == tf)

      {         

         if(cit.BufferSize() != bars)cit.BufferResize(bars);   

         cit.GetData(cndl,1,toRet);      

         return toRet[0];

      }

   }

   

   CiTime *cit = new CiTime();

   cit.Create(smbl,tf);

   cit.BufferResize(bars);  

   timesLst.Add(cit);

   cit.GetData(cndl,1,toRet);

   return toRet[0];

fxtrader1081
14
fxtrader1081 2015.03.25 11:05  
I didn't get a response so I found a way to implement my own solution.  Here is a class that will cut down your time to get the total bars in half.  If you think about how many times you need this information in a test, this is huge. If you save the bars object for each symbol/tf then you can cut out the search time as well which might drop off another 1%.
Files:
Stuart Browne
5818
Stuart Browne 2015.03.25 11:37  
fxtrader1081:
I didn't get a response so I found a way to implement my own solution.  Here is a class that will cut down your time to get the total bars in half.  If you think about how many times you need this information in a test, this is huge. If you save the bars object for each symbol/tf then you can cut out the search time as well which might drop off another 1%.
Thanks for reporting back your results 
To add comments, please log in or register