GetTickCount() to measure efficient code

 
Hi everyone I want to know if my code is actually efficient when it runs, I looked and ask on another forum and people told me to use GetTickValue() to test it. I looked into the documentation and implement it on my code and return a value around 950 to 1050. How can I interpret this value and what the tick value benchmark for efficient code?
 
  1. Don't worry about performance, until you have a problem. Then, and only then, use the profiler, find the problem, fix the problem, and remeasure.

  2. There is no such function defined. List of MQL4 Functions - MQL4 Reference

 
William Roeder #:
  1. Don't worry about performance, until you have a problem. Then, and only then, use the profiler, find the problem, fix the problem, and remeasure.

  2. There is no such function defined. List of MQL4 Functions - MQL4 Reference

My bad the function is GetTickCount() and not GetTickValue().

In my backtest there's a strategy that displayed fast and there's a strategy that displayed slow even though at the max speed and I thought maybe the performance of my code that makes it slow. Does performance isn't equal to the speed of our order get sent to market?
 
  1. Luandre Ezra return a value around 950 to 1050. How can I interpret this value

    It is not the value, it is the difference between two calls. 950-1050 difference means it took once second.


  2. Luandre Ezra #: Does performance isn't equal to the speed of our order get sent to market?

    Speed English; that is nonsensical. Use the automatic translation tool if needed. Use simple language structure when using mechanical translation. (2013)

  3. Always post all relevant code (using Code button) or attach the source file.
         How To Ask Questions The Smart Way. (2004)
              Be precise and informative about your problem

    We can't see your broken code.

 

my code looks like this,

void OnTick()
  {
//Print("MAGIC NUMBER: ", MagicNumber);

   if(IsNewCandle())
     {
      uint  Start = GetTickCount();
      resetCounterVar();
      CloseOrders(); 

      if(CheckPendOrder()==0)
         OpenOrders();
      uint time = GetTickCount()-Start;
      Print("time: ", time);
     }
  }

So the whole process is taking around a second for MT4 to finish execute the code. Does one second is quite normal or it is slow?


Luandre Ezra #: Does performance isn't equal to the speed of our order get sent to market?

What I meant is that performance should be equal to how fast MT4 read and execute code and sent market order (from mt4 client to mt4 server to ECN) so why you say that performance should not be worried (I do scalping btw)?

 
Luandre Ezra #: my code looks like this, So the whole process is taking around a second for MT4 to finish execute the code. Does one second is quite normal or it is slow?

There is no way for us to know if that is fast, slow or normal, because we have no ideia what your code does or under what conditions.

Also, your measurement is not only considering your code execution but also the network latency. So, your code may be fast, but your network response slow, or you code may be slow and your network fast. You can't tell from the way you are measuring things.

 
Luandre Ezra #:

my code looks like this,

So the whole process is taking around a second for MT4 to finish execute the code. Does one second is quite normal or it is slow?

void OnTick()
  {
//Print("MAGIC NUMBER: ", MagicNumber);

   if(IsNewCandle())
     {
      uint  Start = GetTickCount();
      resetCounterVar();
      CloseOrders(); 

      if(CheckPendOrder()==0)
         OpenOrders();
      uint time = GetTickCount()-Start;
      Print("time: ", time);
     }
  }


You are calling 4 different functions. As we have no idea what those functions do we cannot guess whether it may be slow or not.

I will edit your title so that it shows  GetTickCount()  instead of Tick value.

 
Keith Watford #:

You are calling 4 different functions. As we have no idea what those functions do we cannot guess whether it may be slow or not.

I will edit your title so that it shows  GetTickCount()  instead of Tick value.

int CheckOpenPosition(int shift=0)
  {
   int result=0;
   if(buySignal)
      result=1;
   if(sellSignal)
      result=2;
   return result;
  }
//
int CheckClosePosition(int shift=0)
  {
   int result=0;
   if(sellSignal)
      result=1;
   if(buySignal)
      result=2;
   return result;
  }
//
void OpenOrders()
  {
   if(CheckOpenPosition()==1)
     {
      MarketBuyOrder();
     }

   if(CheckOpenPosition()==2)
     {
      MarketSellOrder();
     }
  }

//+------------------------------------------------------------------+
void MarketBuyOrder()
  {
   RefreshRates();
   openPriceBuy         = Ask;

   stoplossBuy          = iLow(Symbol(),PERIOD_CURRENT,iLowest(Symbol(),PERIOD_CURRENT,MODE_LOW,BarShift,0));;
   takeProfitBuy        = (openPriceBuy - stoplossBuy)*RiskReward+openPriceBuy;

   if(Take_profit == 0)
      takeProfitBuy       = 0;

   if(LotSizeType       == Fixed)
      lotSizeBuy        = FixedLotSize;
   if(LotSizeType       == Dynamic)
      lotSizeBuy        = CalculateLotSize(Risk_Per_Trade/100,stoplossBuy)/lotType;
   if(lotSizeBuy < MarketInfo(Symbol(),MODE_MINLOT))
      lotSizeBuy        = MarketInfo(Symbol(),MODE_MINLOT);

   ticketBuy            = OrderSend(Symbol(),OP_BUY,lotSizeBuy,openPriceBuy,slippage,stoplossBuy,takeProfitBuy,"BUY",MagicNumber);
  }

//
void MarketSellOrder()
  {
   RefreshRates();
   openPriceSell        = Bid;

   stoplossSell         = iHigh(Symbol(),PERIOD_CURRENT,iHighest(Symbol(),PERIOD_CURRENT,MODE_HIGH,BarShift,0));;
   takeProfitSell       = openPriceSell - (stoplossSell - openPriceSell)*RiskReward;

   if(Take_profit == 0)
      takeProfitSell       = 0;

   if(LotSizeType       == Fixed)
      lotSizeSell        = FixedLotSize;
   if(LotSizeType       == Dynamic)
      lotSizeSell       = CalculateLotSize(Risk_Per_Trade/100,stoplossSell)/lotType;
   if(lotSizeSell < MarketInfo(Symbol(),MODE_MINLOT))
      lotSizeSell       = MarketInfo(Symbol(),MODE_MINLOT);

   ticketSell           = OrderSend(Symbol(),OP_SELL,lotSizeSell,openPriceSell,slippage,stoplossSell,takeProfitSell,"SELL",MagicNumber);
  }
//
void CloseOrders()
  {
   RefreshRates();
   if(sellSignal)
     {
      for(int i = OrdersTotal() - 1; i >= 0; i--)
        {
         if(OrderSelect(i,SELECT_BY_POS)                 &&
            OrderSymbol() == Symbol()                    &&
            OrderMagicNumber() == MagicNumber)
           {
            if(OrderType() == OP_BUY)
              {
               bool res = OrderClose(OrderTicket(),OrderLots(),Bid,0);
               if(!res)
                  Print("Order failed to CLOSE OPEN ORDER for ticket # ",OrderTicket()," with error: ",GetLastError());
              }
           }
        }
     }
   if(BuySignal)
     {
      for(int i = OrdersTotal() - 1; i >= 0; i--)
        {
         if(OrderSelect(i,SELECT_BY_POS)                 &&
            OrderSymbol() == Symbol()                    &&
            OrderMagicNumber() == MagicNumber)
           {
            if(OrderType() == OP_SELL)
              {
               bool res = OrderClose(OrderTicket(),OrderLots(),Ask,0);
               if(!res)
                  Print("Order failed to CLOSE OPEN ORDER for ticket # ",OrderTicket()," with error: ",GetLastError());
              }
           }
        }
     }
  }

//NEW OnTick()

void OnTick()
  {
   if(IsNewCandle())
     {
      uint  Start = GetTickCount();
      CloseOrders(); 

      if(OrdersTotal()<MaxOpenTrade)
         OpenOrders();
      uint time = GetTickCount()-Start;
      Print("time: ", time);
     }
  }

here's all the code and function except the logic after I clean all unnecessary variable and function.


Fernando Carreiro : There is no way for us to know if that is fast, slow or normal, because we have no ideia what your code does or under what conditions.

So even though we know about the tick count value we still can't determine if it's fast or not? In this case there's no standard that can be used as a benchmark, right?
 
Luandre Ezra #: here's all the code and function except the logic after I clean all unnecessary variable and function. So even though we know about the tick count value we still can't determine if it's fast or not? In this case there's no standard that can be used as a benchmark, right?

Correct! There is no benchmark, nothing to compare your results with. The only thing you can do with it is compare it to other code to determine which one is the faster of the two.

For example, you could time different EAs from the codebase and compare your code execution speed to those, to see if at least your code is more efficient than those. Or another example, is for you to streamline your own code as best you can, and see how much you have saved.

However, usually this method of timing is done in a loop over many iterations to measure the real execution speed, because sometimes a single iteration is too quick to get a proper measurement.

Also, remember my warning, you are currently measuring not only your code execution but also the delays for the order processing, which are dependant on network latency and broker processing time, so it is not accurately giving you the metrics of your code alone. Separate the two, so that you can see your code execution time, and the order processing time.

 
Luandre Ezra #:

here's all the code and function except the logic after I clean all unnecessary variable and function.


So even though we know about the tick count value we still can't determine if it's fast or not? In this case there's no standard that can be used as a benchmark, right?

You are focusing on the wrong stuff....

The bulk of your execution time based on code shown will be in the processing of a trade request, so measure that first because there is little you can do about it with code and it provides a base line.

You code is full of duplicate calls and unnecessary functions, clean them up, do things once.

You say your are a scalper but you seem to be only executing on a new bar - so one second does not seem a lot to worry about in context of elapsed time.

Use the code profiler

 
Luandre Ezra #:

here's all the code and function except the logic after I clean all unnecessary variable and function.


So even though we know about the tick count value we still can't determine if it's fast or not? In this case there's no standard that can be used as a benchmark, right?
If I understand your code correctly, you only execute once on a new bar, and your code seems to have a runtime of 1000 ms, this does not seem critical in any way.

If you insist, do a search on the forum, there have been in depth discussions about measuring code and the pitfalls that come with it. - In short: it is everything but trivial.

EDIT:

The profiler in MQL is often misleading, and won't give you a good picture on what is actually going on. - I've tried my best to get smart with that stuff, but failed miserable... I went to write my own performance metrics library. (Can be found on CodeBase)
Reason: