Order ticket array " Array is too large "

 

hi

im using MQL5 and i want delete the tp of the position and to know what was the tp before [virtual_tp], and then to use is at the future 

1. i tried to set a array for the ticket but the number of the ticket is too large , the max is 7 digits and the ticket digits in 8+

its work for me ok because the ticket num was just 7 digits, but when i give it to my friend he had  ticket num of 9 digits and get error "array is out of range " (i cant make a array more then that )

  double orderticket[100];
  int ordertype[100];
  double orderTP[100];
  double orderSL[100];
  double virtual_TP[1000000];

for(int i=0 ; i <PositionsTotal(); i++)
   {
         if(PositionSelectByTicket(PositionGetTicket(i))==true )
         {
                orderticket[i]=PositionGetInteger(POSITION_TICKET);
                ordertype[i]=PositionGetInteger(POSITION_TYPE);
                orderTP[i]=PositionGetDouble(POSITION_TP);
                orderSL[i]=PositionGetDouble(POSITION_SL);


                if(orderTP[i]>0)
                        {
                        virtual_TP[orderticket[i]]=orderTP[i];
                        }
                
                if(PositionGetDouble(POSITION_TP)>0)  
                        {
                        trade.PositionModify(orderticket[i],orderSL[i],0);
                        }

                
        }
}


2. i try the next way but when i have more then 1 trade and i close one of trade at the next time that the loop  run the place that  virtual_tp store is not update beacuze the posion place was changed in the loop 

  int ticket_num;
  double orderticket[100];
  int ordertype[100];
  double orderTP[100];
  double orderSL[100];
  double virtual_TP[100];

for(int i=0 ; i <PositionsTotal(); i++)
   {
         if(PositionSelectByTicket(PositionGetTicket(i))==true )
         {
                orderticket[i]=PositionGetInteger(POSITION_TICKET);
                ordertype[i]=PositionGetInteger(POSITION_TYPE);
                orderTP[i]=PositionGetDouble(POSITION_TP);
                orderSL[i]=PositionGetDouble(POSITION_SL);


                if(orderTP[i]>0)
                        {
                        virtual_TP[ticket_num]=orderTP[i];
                        }
                
                if(PositionGetDouble(POSITION_TP)>0)  
                        {
                        trade.PositionModify(orderticket[i],orderSL[i],0);
                        }

                ticket_num++    
        }
}

please any idea out to fix that ? 

 
  • Use an array of structs, and resize it for every new position. You'll have to use a for loop to look-up a certain ticket.
  • Or, use a hash map. 

The struct approach looks like this:

struct SPosition
 {
   int type;
   ulong ticket;
   double orderSL,orderTP;
   double virtualTP;
 };
SPosition Positions[];
int ticket_num=0;

void GetPositions()
 {
   for(int i=PositionsTotal()-1; i>=0; i--)
    {
      ulong ticket=PositionGetTicket(i);
      if(ticket>0)
       {
         int pos;
         for(pos=ArraySize(Positions)-1; pos>=0; pos--)
          {
            if(Positions[pos].ticket==ticket) break;
          }
         if(pos<0)
          {
           pos=ArraySize(Positions);
           ArrayResize(Positions,pos+1);
           ZeroMemory(Positions[pos]);
           Positions[pos].ticket=ticket;
          }
         Positions[pos].type=(int)PositionGetInteger(POSITION_TYPE);
         Positions[pos].orderTP=PositionGetDouble(POSITION_TP);
         Positions[pos].orderSL=PositionGetDouble(POSITION_SL);

         if(Positions[pos].orderTP>0)
          {
            Positions[pos].virtualTP=Positions[pos].orderTP;
            trade.PositionModify(ticket,Positions[pos].orderSL,0);
          }
       }
    }
   ticket_num=ArraySize(Positions);
 }
 
lippmaje:
  • Use an array of structs, and resize it for every new position. You'll have to use a for loop to look-up a certain ticket.
  • Or, use a hash map. 

The struct approach looks like this:

work perfect !!!

thank you bro :]