Save ticket number in an array

To add comments, please log in or register
Wessam Nabil
81
Wessam Nabil  

hello

i made an expert, it works good but for a reason it need a unique number to restore every trade so i tried to save ticket number 

i searched file open, and arrays but i couldnt find what i want beside im not good in arrays

finally i found something near to what i want 

i got the code but it not give me the result

//+------------------------------------------------------------------+
//|                                                            5.mq4 |
//|                        Copyright 2018, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict


int Magic = 00100;    
int ticket;
double StartPoints = 12;
double pips;
double ticksize;
int positions = 4;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit(){
      
   ticksize = MarketInfo(Symbol(),MODE_TICKSIZE);
   if(ticksize == 0.00001 || 0.001){
      pips = ticksize * 10;
   }else{
      pips = ticksize;
   }  
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason){
  
}
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick(){
   if(OrdersTotal() < positions){
      ticket = OrderSend(Symbol(), OP_SELL, 0.01, Bid, 3, Bid+(StartPoints*pips), Bid-(StartPoints*pips), NULL, Magic, 0, Red);
   }
   int                                  // this is what i got
   intticketNumber[10],         // can i leave this empty instead of 10? to make it dynamic?
   intOrdersTotal = OrdersTotal();

        if( ArrayResize( intticketNumber, intOrdersTotal ) == intOrdersTotal ){
        for( int i = intOrdersTotal - 1; i >= 0; i-- ){
      if( OrderSelect( i, SELECT_BY_POS, MODE_TRADES ) ){
         intticketNumber[ i ] = ticket;
      }else{
         intticketNumber[ i ] = EMPTY;
      }
        Print ("here: ", intticketNumber[ i]);
   }
}
}
//+------------------------------------------------------------------+
Wessam Nabil
81
Wessam Nabil  

all i need for now to print each trade ticket number

thanks for any help in advance

Biantoro Kunarto
22107
Biantoro Kunarto  

You can use this code :

int Magic = 00100;    
int ticket;
double StartPoints = 12;
double pips;
double ticksize;
int positions = 4;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
 {
   ticksize = MarketInfo(Symbol(),MODE_TICKSIZE);
   if(ticksize == 0.00001 || 0.001)
      pips = ticksize * 10;
   else
      pips = ticksize;
   return(INIT_SUCCEEDED);
 }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
 {

 }

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
 {
   if(OrdersTotal() < positions)
      {
         ticket = OrderSend(Symbol(), OP_SELL, 0.01, Bid, 3, Bid+(StartPoints*pips), Bid-(StartPoints*pips), NULL, Magic, 0, Red);
      }
   int intticketNumber[1];
   int intOrdersTotal = OrdersTotal();
   if ( intOrdersTotal > 1 )
      ArrayResize( intticketNumber, intOrdersTotal );
   for( int i = intOrdersTotal - 1; i >= 0; i-- )
      if( OrderSelect( i, SELECT_BY_POS, MODE_TRADES ) )
         {
            intticketNumber[i] = OrderTicket();
            Print ("here: ", intticketNumber[i]);
         } 
 }
nicholi shen
2392
nicholi shen  

It's better to use a well made collection. Your best bet is to use CArrayInt for store ticket numbers. You could also subclass and make a method to formate the string how you like. Example: 


#include <arrays/arrayint.mqh>
class ArrayInt : public CArrayInt
{
   public: virtual string to_string() const {
      string res = "[";
      for(int i=0; i<this.Total(); i++) {
         res += string(this[i]);
         if(i < size - 1)
            res += ", ";
      }
      return res + "]";
   }
};

void OnStart()
{
   ArrayInt arr;
   for(int i=OrdersHistoryTotal()-1, x=0; i>=0 && x < 6; i--, x++) 
      if(OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))
         arr.Add(OrderTicket());
   Print(arr.to_string());
}

output

EURUSD,H1: [20284, 17619, 17453, 17283, 17109, 91828]
Wessam Nabil
81
Wessam Nabil  

thank you for your replaying

i found a solution also so ill put it here to anyone may be have the same problem

void OnTick(){
   int Pos_Order_Ticket[1];        
   int TotalOrders = OrdersTotal();

   if(ArrayResize(Pos_Order_Ticket, TotalOrders) == TotalOrders){
      for(int i = TotalOrders - 1; i >= 0; i--){
         if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)){
            Pos_Order_Ticket[i] = OrderTicket();
         }else{
            Pos_Order_Ticket[i] = EMPTY;
         }
         Print ("here: ", Pos_Order_Ticket[i]);
      }
   }
}
To add comments, please log in or register