Forex Factory (https://www.forexfactory.com/)
-   Platform Tech (https://www.forexfactory.com/forum/69-platform-tech)
-   -   Metatrader using 40% of the CPU all the time when using EA (https://www.forexfactory.com/thread/383657-metatrader-using-40-of-the-cpu-all-the)

Jasus Sep 11, 2012 8:07pm | Post# 1

Metatrader using 40% of the CPU all the time when using EA
 
The EA only runs real code every 15 minutes, between them it only runs this code (which is on start() function):

if ( iTime(NULL, PERIOD_M15, 1) == last_start_time )
return(0);

which makes the EA stop until the next tick.

I put the EA on 15 pairs, and terminal.exe is constantly using 35-40% of the CPU (my processor is Intel Quad core Q6600 @ 2.40GHz). Is it normal?

pip_seeker Sep 11, 2012 10:12pm | Post# 2

The EA only runs real code every 15 minutes, between them it only runs this code (which is on start() function):

if ( iTime(NULL, PERIOD_M15, 1) == last_start_time )
return(0);

which makes the EA stop until the next tick.

I put the EA on 15 pairs, and terminal.exe is constantly using 35-40% of the CPU (my processor is Intel Quad core Q6600 @ 2.40GHz). Is it normal?

Nope, sounds like a memory leak possibly.

When you start up the EA snap a pic of your windows task manager on performance tab... with the market open wait a few hours and then snap another picture of the windows task manager.

Compare the two pics. Are you using more memory in the last pic than the first? If you are using more memory in the second pic than the first then that's your problem. (Right now I am only guessing)

nubcake Sep 12, 2012 1:56am | Post# 3

The EA only runs real code every 15 minutes, between them it only runs this code (which is on start() function):

if ( iTime(NULL, PERIOD_M15, 1) == last_start_time )
return(0);

which makes the EA stop until the next tick.

I put the EA on 15 pairs, and terminal.exe is constantly using 35-40% of the CPU (my processor is Intel Quad core Q6600 @ 2.40GHz). Is it normal?
i wouldn't be testing against iTime each tick. shouldn't make a grand difference, but still i wouldn't do that. you are better-off within your code block calculating the next specific time to always be testing against... then you can just if (time[0] >= whatevertime) { do some shit, and calc next whatevertime }

Inserted Code
//+------------------------------------------------------------------+
//|                                        testingotherscodeindi.mq4 |
//|                                                          nubcake |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "nubcake"
#property link      ""

#property indicator_chart_window

#define cycles 1000000

int deinit() { Comment(""); return(0); }

int init() { return(0); }

int start() {
  int c;
  int tc, start1, start2, end1, end2, dummytick;
  datetime dummytime;
  
  start1 = GetTickCount();
  for (c = 0; c < cycles; c++) if (Time[1] != Time[0]) dummytick = GetTickCount();
  end1 = GetTickCount() - tc;  
  Comment(dummytick);
  
  start2 = GetTickCount();
  for (c = 0; c < cycles; c++) if (iTime(NULL, PERIOD_M15, 1) != Time[0]) dummytick = GetTickCount();
  end2 = GetTickCount() - tc;  
  Comment(dummytick);
  
  Comment("Time[]:\n", start1, " ", end1, " (", end1-start1, "ms)\niTime[]:\n", start2, " ", end2, " (", end2-start2, "ms)");
  
  return(0);
}
this bit of junk code shows on my platform that iTime does cost more cycles than a simple Time[] test. on my system the cost is insignificant, but perhaps on your platform something hokey is going on. perhaps you have some other piece of code running with a similar iSomeFunc calls that are the problem. if you were happy-enough to use the code you have shown here you are probably happily doing something somewhere else really inefficiently and slowing your terminal down without realizing it.

something isn't right and more poking and prodding of your setup is required to isolate the problem...

Jasus Sep 12, 2012 6:25am | Post# 4

Thanks for the answers, but today I noticed that happens for 7-8 minutes and then the CPU use goes to 5-10% until the next M15 bar, which is the time to run all the code (a lot of loops). I guess there's no way to lower the CPU use right?

Edit: I just noticed that the EA only runs all the code in those 15 minutes on the first pair I put the EA in, on the others it doesn't run all the code until the next bar appears. I don't know what to do now... Run a terminal.exe for each pair?

nubcake Sep 12, 2012 7:05am | Post# 5

Thanks for the answers, but today I noticed that happens for 7-8 minutes and then the CPU use goes to 5-10% until the next M15 bar, which is the time to run all the code (a lot of loops). I guess there's no way to lower the CPU use right?

Edit: I just noticed that the EA only runs all the code in those 15 minutes on the first pair I put the EA in, on the others it doesn't run all the code until the next bar appears. I don't know what to do now... Run a terminal.exe for each pair?
your code sounds all kinds of busted. it sounds like it's triggering, getting caught in unnecessary loops and hanging your terminal to some degree or another, and then you aren't updating your vals to have it trigger on the next period. i think you need to do an overhaul and attack whatever it is you are trying to do from a different, more efficient, angle.

not much else can really be said with just those few lines of code.

pip_seeker Sep 12, 2012 7:10am | Post# 6

1 Attachment(s)
I agree with Nubcake... something is not right.

Here's a pic of my task manager running 2 instances of meta trader with 1 ea on 1 pair on one and on the other running on more than 10 pairs with a piece of code that runs every tick and more than 2000 lines of code.

FYI, Reason why I suspect memory leak is I've seen that issue before on my own rig and it's not instantaneous it builds over time. A memory leak in and of itself will cause cpu overloads as well as more and more memory usage as time goes on. If you restart the computer / EA then it starts all over again... that's why they call it a "leak".

You will only get to the bottom of it by poking and prodding as Nubcake suggested.
Name:  task_mngr.png
Views: 972
Size:  44 KB

Jasus Sep 17, 2012 11:38am | Post# 7

Thanks for the help, I changed my code to be (way) more efficient. As for the 5%-10% CPU use, it's because of a set of indicators I need to use with my EA.


© Forex Factory