List or Array of a Custom Object

 

Hello, I'm really struggling with the way MQL5 implements lists, I wish it was simple as C#'s List class!


I have this custom class:

class SDB_TradeAlert : public CObject
{

   private:
   public:
                              SDB_TradeAlert();
                             ~SDB_TradeAlert();
                    
      string                  symbol;                 
      string                  alertName;
      string                  direction;    
      string                  instructions;      
      double                  triggerPrice; 
      double                  stopLoss;   
      bool                    deleteOnNewBar;                 
};


SDB_TradeAlert::SDB_TradeAlert()
{
}

SDB_TradeAlert::~SDB_TradeAlert()
{
}



What is the correct way to work with a collection of this object, using CList or CArrayObj? I just need to:

- Create objects and add to the collection

- Iterate the collection and work with each item

- Delete specific items from the collection during this iteration, if they match my criteria


I tried many variations but I always end up with some kind of problem.


//Declaring the collection
CList   *alertList  = new CList; 


// Creating and adding to the list
SDB_TradeAlert alert;
...
// do some working with the item, then add to the list
alertList.Add(alert);


// Iterate the list and delete items when match my criteria
for (int i = 0; i < alertList.Total(); i++)
{
   SDB_TradeAlert alert = alertList.GetNodeAtIndex(i);  // create an instance to work in the loop
   
   if ( matches my criteria)
      alertList.Delete(i);
}


What's the simplest and most effective way to work this? It can be either CList, CArrayObj or something else, I dont really care as long as it works!


Many thanks in advance!

Documentation on MQL5: Constants, Enumerations and Structures / Objects Constants / Object Types
Documentation on MQL5: Constants, Enumerations and Structures / Objects Constants / Object Types
  • www.mql5.com
Object Types - Objects Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Eduardo Fernando Teixeira:

Hello, I'm really struggling with the way MQL5 implements lists, I wish it was simple as C#'s List class!
!

When an item is deleted, the Total method returns the correct count of elements, but the index i refers to the next element which has become current element, so it remains untouched. when you want to delete an item inside a loop, an approach is a recursive function for deletion.
 
Mohammad Hossein Sadeghi:
When an item is deleted, the Total method returns the correct count of elements, but the index i refers to the next element which has become current element, so it remains untouched. when you want to delete an item inside a loop, an approach is a recursive function for deletion.

Thank you Mohammad.


What about the methods to copy the current item in the iteration to a new instance, do you have any thoughts?

 
Eduardo Fernando Teixeira:

Thank you Mohammad.


What about the methods to copy the current item in the iteration to a new instance, do you have any thoughts?

No, take it easy, you can iterate from last to first, this is another approach for item deletion if the order of items is not important.

 
Mohammad Hossein Sadeghi:

No, take it easy, you can iterate from last to first, this is another approach for item deletion if the order of items is not important.

Thank you, will try that!