building trading system - a unified smart solution for simultaneous trading with different strategies - page 2

 
Stanislav Korotky #:

It's better to use OOP to prevent a mess in code. A lot of trading frameworks (with different complexity and capabilities) are already published on this site.

ohh, I guess I misunderstood the point of Your comment, I guess i just got the idea now...

Did you mean that the function void Check_Positions() could also defined only once not inside each strategy ?

I haven't thought about it myself as I've been testing and learning mql5 trading logic, but it's probably a good idea as the task of the function serves the same purpose in every strategy... I'll deal with it as soon as I have time.

 

I've now had a few hours to think about this topic. For easier management, it is of course a good idea to put such a function in its own responsible class so that we don't have to search for its location later, since we can do it with only a very small additional cost. At the same time let the variables of the each strategy be left processing to the strategy itself. Because each strategy is inherently very different. There is no point in bringing them higher, it would only create unjustified additional costs and additional confusion.

There is no point in making functions like this virtual or implementing it in any other way with deeper OOP principles. In this particular case, it would do more harm than good. I believe that the best thing to do to make a specific situation more comfortable would look something like this:

class C_Virtual_Strategy
{
public:
   virtual ~C_Virtual_Strategy() {} 
  
   virtual void     Set_Deal_Data(double price, datetime time, ulong ticket) = 0;
   
   virtual short    Get_Execute()     = 0; // Must return -1 (sell), 1 (buy), 0 (none)
   virtual short    Get_ID()          = 0;
   virtual short    Get_Magic()       = 0;
   virtual string   Get_Comment()     = 0;
   virtual double   Get_SL()          = 0;
   virtual double   Get_TP()          = 0;
   
   virtual short    Get_Pos_Counter() = 0;
   virtual long     Get_Pos_Id(int i) = 0;
   
   void Add_Position(long &m_pos_id[], short &pos_counter, ulong ticket) 
   {
      ArrayResize(m_pos_id, pos_counter + 1, 4);
      m_pos_id[pos_counter++] = ticket;
   }

   void Manage_Positions(long &m_pos_id[], short &pos_counter) 
   {
      for(int i = pos_counter - 1; i >= 0; i--) 
      {
         if(!PositionSelectByTicket(m_pos_id[i])) 
         {
            if(i != pos_counter - 1) 
               m_pos_id[i] = m_pos_id[pos_counter - 1];
            pos_counter--;
         }
      }
      ArrayResize(m_pos_id, pos_counter, 4);
   }
};

but my original question was much easier, whether it is still a good solution to use the PositionSelectByTicket() function to track active positions and whether this function can fail in certain situations? mql5 gurus know this for sure...