Download MetaTrader 5

Shifting arrays & Filling arrays - page 2

To add comments, please log in or register
nicholishen
945
nicholishen  

Here's a quick example of how to make a simple, sortable list of symbol objects.

#property strict
#include <Arrays\List.mqh>
#define SORT_SPREAD 1
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
class MySymbol : public CObject
{
public:
   string   symbol;
   MqlTick  tick;
   MySymbol(string symb):symbol(symb){Refresh();}
   void Refresh()
   {
      SymbolInfoTick(symbol,tick);
   }
   int Spread() const
   {
      return int((tick.ask - tick.bid)/SymbolInfoDouble(symbol,SYMBOL_POINT));
   }
   string ToString()
   {
      return symbol+" Spread = "+string(Spread());
   }
   int Compare(const CObject *node,const int mode=0)const
   {
      MySymbol *other = (MySymbol*)node;
      if(mode==SORT_SPREAD)
      {
         if(this.Spread()>other.Spread()) return 1;
         if(this.Spread()<other.Spread()) return -1;
      }
      return 0;
   }
};
//+------------------------------------------------------------------+.
//+------------------------------------------------------------------+
class MyList : public CList
{
public:
   void ToLog()
   {
      for(MySymbol *sym=GetFirstNode();sym!=NULL;sym = sym.Next())
         Print(sym.ToString());
   }
   void SortBySpread()
   {
      Sort(SORT_SPREAD);
   }
};
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
MyList list;
void OnStart()
{
   for(int i=0;i<SymbolsTotal(false)&&i<10;i++)
      list.Add(new MySymbol(SymbolName(i,false)));
 
   list.SortBySpread();
   list.ToLog();
}
//+------------------------------------------------------------------+
12
To add comments, please log in or register