Assigning Ticket number to Array

 

Hi Folks


I am writing a program to keep a record of the Order Ticket numbers in an array.
There is an issue that I cant seem to resolve. 

The code is attached. When I print out the orderticket directly from the the OrderTicket() function
or from the order_ticket variable, it is correct. However, when I print out the value stored in the
array orders{j], it always shows '0'. 

Both order_ticket and orders[] are declared as ulong.

Thanks in advance for the help!

   for(j=0;j<orders_count;j++)
   {
      for(i=0;i<OrdersTotal();i++)
      {
        if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
        
        if(OrderSymbol()==Symbol() && OrderMagicNumber()==magicnumber[j])
        {
            prev_big_order_profit = OrderProfit();
            big_order_type = OrderType(); 
            running_profit += prev_big_order_profit;
            order_ticket = OrderTicket();
            orders[j] = order_ticket;
            Print("OrderTicket: ", OrderTicket());
            Print("order_ticket: ", order_ticket);
            Print("Orders[" + j + "]: ", orders[j]);
            Print("Prev Big Order Profit: ", prev_big_order_profit); 
            Print("Running Profit: ", running_profit);                  
        }   
      }           
   } 
 
since j is not i
 
Marco vd Heijden:
since j is not i

Hi Marco


Sorry didnt understand your reply.

In my code, you can see that I assign OrderTicket() to both a variable and an element in the array.
When I try to do the Print statement in the same loop, only the variable seems to hold the value,
while the array element prints as '0'. 

The value of 'j' is not significant here as I am assigning and printing with the same j value in the loop.

 
Your magic number is in an array?
 
The type of value returned by OrderTicket () is an int. Try changing order_ticket and orders [] to int.
 

Anytime you have a multitude of arrays with individual types all pointed at the same data/index should be a huge red flag that you're using the wrong datatype. In this case you really need to group your data into an array of structs or objects that represent your collection. Also, storing the ticket number and then looping the pool kind of defeats the purpose of having the ticket saved. Just so I don't leave you hanging on that here's a snippet of what I mean... 

Here is an example of how you could group your data and encapsulate your ticket logic.

#include <Arrays\List.mqh>
#define foreach_ticket(I,L) for(Ticket*I = L.GetFirstNode(); CheckPointer(I); I=I.Next())
//+------------------------------------------------------------------+
class Ticket : public CObject
{
public:
   int               ticket;
   double            high_profit;
   bool select(){ 
      return OrderSelect(SELECT_BY_TICKET,ticket);
   }
   bool is_open(){
      if(this.select() && OrderCloseTime()==0)
         return true;
      return false;
   }
   bool refresh(){
      if(this.is_open()){
         if(OrderProfit() > this.high_profit)
            this.high_profit = OrderProfit();
         return true;
      }
      return false;
   }
};

CList tickets;
//+------------------------------------------------------------------+

... and here's an example of how you could cleanly call it in your code. 


void OnStart()
{
   foreach_ticket(ticket, tickets){
      if(ticket.refresh()){
         OrderPrint(); //ticket is selected so this will print correctly
         Print("Hightest profit = ", ticket.high_profit);
      }
   }
}
 
raviragas:

Hi Marco


Sorry didnt understand your reply.

In my code, you can see that I assign OrderTicket() to both a variable and an element in the array.
When I try to do the Print statement in the same loop, only the variable seems to hold the value,
while the array element prints as '0'. 

The value of 'j' is not significant here as I am assigning and printing with the same j value in the loop.

You can print it and it can look correctly, but on the next pass in loop i you overwrite the value of [j] with the value of the last order because you are still on j.

try printing in the j loop in stead of the i loop.

 
raviragas:

Hi Folks


I am writing a program to keep a record of the Order Ticket numbers in an array.
There is an issue that I cant seem to resolve. 

The code is attached. When I print out the orderticket directly from the the OrderTicket() function
or from the order_ticket variable, it is correct. However, when I print out the value stored in the
array orders{j], it always shows '0'. 

Both order_ticket and orders[] are declared as ulong.

Thanks in advance for the help!

I do not understand many other offline. I'm new here.
 
   for(j=0;j<orders_count;j++)
   {
      for(i=0;i<OrdersTotal();i++)
      {
        if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;
        if(OrderSymbol()!=Symbol()) continue;
        if(OrderMagicNumber()!=magicnumber[j]) continue;

            prev_big_order_profit = OrderProfit();
            big_order_type = OrderType(); 
            running_profit += prev_big_order_profit;
            order_ticket = OrderTicket();
            //-----
            int n = ArrayBsearch(orders, order_ticket);
            if( orders[n] != order_ticket )
            {
               n = ArraySize(orders);
               if( ArrayResize(orders, n+1) == n+1 )
                  orders[n] = order_ticket;
            }
            //-----
            Print("OrderTicket: ", OrderTicket());
            Print("order_ticket: ", order_ticket);
            Print("Orders[" + n + "]: ", orders[n]);
            Print("Prev Big Order Profit: ", prev_big_order_profit); 
            Print("Running Profit: ", running_profit);
      }
   }

Something like this. Did not check, but it seems logical.

Reason: