How to create a queue using MQL?

 

I'd need to create a queue structure with the below interface but the size can increase at any time:

double bucket[];

Enqueue(double val) // add as last item in the bucket

double Dequeue() // remove the first item from the bucket

double ReadFirst() // read the value of the first item in the bucket

 
Pooya Khamooshi:

I'd need to create a queue structure with the below interface but the size can increase at any time:

MQL5 has this:

https://www.mql5.com/en/docs/standardlibrary/generic/cqueue

It doesn't look like MQL4 has this, but you could always roll your own.

 
Anthony Garot:

MQL5 has this:

https://www.mql5.com/en/docs/standardlibrary/generic/cqueue

It doesn't look like MQL4 has this, but you could always roll your own.

Thanks! I must plan upgrading to MQL5!

But for now with MQL4, I only have the Array object and few helper methods it has like ArraySize, ArrayResize! Not sure.

 

I wrote this for MQL4 which looks stupid but works! anyone can improve it?

Assumptions:

1)

There is a known max size for the queue

2)

If an array item is 0 it means it's empty.


class DoubleQueue

{    

   public:

      double Result[];

      DoubleQueue(int size);

      void Enqueue(double item);

      double Dequeue();

      double ReadFirst();

      int Count();      

};


 // Implementations

 DoubleQueue::DoubleQueue(int maxSize) {

   ArrayResize(Result, maxSize);

   for(int i = 0; i < maxSize; i++) {

      Result[i] = 0;    

   }

 } 

  

  void DoubleQueue::Enqueue(double item) {

   int arraySize = ArraySize(Result);

   for(int i = arraySize - 1; i >= 0; i--) {

      if (Result[i] == 0) {

         Result[i] = item;

         break;

      }         

   } 

  }

  

  double DoubleQueue::Dequeue() {

   int arraySize = ArraySize(Result);

   double found = 0;

   for(int i = 0; i < arraySize; i++) {

      if (Result[i] != 0) {

         found = Result[i];

         Result[i] = 0;

         break;

      }         

   } 

   return found;

  }

  

  int DoubleQueue::Count() {

   int arraySize = ArraySize(Result);

   int count = 0;

   for(int i = arraySize - 1; i >= 0; i--) {

      if (Result[i] != 0) {

         count += 1;

         continue;

      }

      break;         

   } 

   return count;

  }

  

  double DoubleQueue::ReadFirst() {

   int arraySize = ArraySize(Result);

   double found = 0;

   for(int i = 0; i < arraySize; i++) {

      if (Result[i] != 0) {

         found = Result[i];

         break;

      }         

   } 

   return found;

  }

 
Pooya Khamooshi:

I wrote this for MQL4 which looks stupid but works! anyone can improve it?

I would go with a Linked List implementation. Consider this for C++

https://www.geeksforgeeks.org/queue-set-1introduction-and-array-implementation/

 

That's the same implementation, first in first out.

Given I had only ArrayResize method, had to make it like that!

Any better idea with MQL4?

 

You can copy the entire generic directory from MQL5 and use it in MQL4. This example was compiled and tested in MT4

#include <generic/queue.mqh>

void OnStart()
{
   int start_vals[] = {1, 2, 3};
   CQueue<int> q(start_vals);
   q.Enqueue(-1);
   while (q.Count()) {
      Print(q.Dequeue());
   }
}
https://www.mql5.com/en/docs/standardlibrary/generic/cqueue
Documentation on MQL5: Standard Library / Generic Data Collections / CQueue
Documentation on MQL5: Standard Library / Generic Data Collections / CQueue
  • www.mql5.com
The CQueue class is a dynamic collection of T type data, which is organized as a queue that operates on the FIFO (first in, first out) principle.
 
Great, thank you very much!
 

Hey there! So I'm having a problem with this. Where exacgly should this code go while coding an EA. Because I get an error that says 'for' loops cannot be in globals and when used with void OnStart() and also having errors when trying to put it elsewhere.

nicholish en #:

You can copy the entire generic directory from MQL5 and use it in MQL4. This example was compiled and tested in MT4

https://www.mql5.com/en/docs/standardlibrary/generic/cqueue
Reason: