some questions about OrderSelect()

 

i code a script to study OrderSelect() :

//+------------------------------------------------------------------+
//|                                               test_typeorder.mq4 |
//|                       Copyright ?2010, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright ?2010, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"
//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start()
  {
//----
int total= OrdersTotal();
for(int i=total-1;i>=0;i++)
  {  
    if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
     Alert("orderstotal=",OrdersTotal(),",i=",i,",ticket3=",OrderTicket()," ,Symbol=",OrderSymbol(),",Opentime=",TimeToStr(OrderOpenTime()));
   }

//----
   return(0);
  }
//+------------------------------------------------------------------+

the output of alert as below: it only shows i=3

why not show i=0,i=1,=2 ?

 

another question about SELECT_BY_POS

https://www.mql5.com/en/forum/126125

 
sergery:

another question about SELECT_BY_POS

https://www.mql5.com/en/forum/126125


Answers:

1. Please write i-- instead of i++

2. The order of the tickets maybe arbitrary, I'm using my own array to keep track on the tickets according to the order I issued them


Cheers.

Dror

 
drormeir wrote >>


Answers:

1. Please write i-- instead of i++

2. The order of the tickets maybe arbitrary, I'm using my own array to keep track on the tickets according to the order I issued them


Cheers.

Dror




YES, i--

Using array to save ticket numbers is safe way to track orders

Thank you !

 
sergery:

Using array to save ticket numbers is safe way to track orders

As long as you have a persistence layer (or if the information in the array is not vital in case of Terminal restart).
 
gordon wrote >>
As long as you have a persistence layer (or if the information in the array is not vital in case of Terminal restart).


sorry,what is a persistence layer ?

===GOOGLE:

What is a persistence layer?

The persistence layer is an architectural layer whose job is to provide an abstract interface to information storage mechanism(s). An API to such an interface should be abstract, and independent of storage technologies and vendors. It would typically have features such as the following:

  • store / retrieve of whole object networks by key
  • logical database cursor abstraction for accessing some / all instances of a given type
  • transaction support - open, commit, abort
  • session management
  • some basic querying support, e.g. how many instances of a given type exist etc

Usually such layers are built from at least two internal layers of software: the first being the abstract interface, the second being a set of bindings, one for each target database. In practice, there may be three layers since there may be an internal division between the logic for object and relational (and other) storage mechanisms.

 

https://www.mql5.com/en/articles/1404

i found an example using array to save the information of EA orders temporarily

// the variable that will store the amount of all orders of the expert:
int _ExpertOrdersTotal = 0;

// arrays where the order characteristics will be stored:
int _OrderTicket[], _OrderType[];
double _OrderLots[], _OrderOpenPrice[], _OrderStopLoss[], 
_OrderTakeProfit[];
double _OrderProfit[], _OrderSwap[], _OrderCommission[];
datetime _OrderOpenTime[], _OrderExpiration[];
string _OrderComment[];

void AllOrdersInit( int magic )
{
    int _GetLastError = 0, _OrdersTotal = OrdersTotal();

    // change array sizes according to the current amount of 
    // positions
    // (if _OrdersTotal = 0, change the arrays size for 1)
    int temp_value = MathMax( _OrdersTotal, 1 );
    ArrayResize( _OrderTicket,     temp_value );
    ArrayResize( _OrderType,       temp_value );
    ArrayResize( _OrderLots,       temp_value );
    ArrayResize( _OrderOpenPrice,  temp_value );
    ArrayResize( _OrderStopLoss,   temp_value );
    ArrayResize( _OrderTakeProfit, temp_value );
    ArrayResize( _OrderOpenTime,   temp_value );
    ArrayResize( _OrderProfit,     temp_value );
    ArrayResize( _OrderSwap,       temp_value );
    ArrayResize( _OrderCommission, temp_value );
    ArrayResize( _OrderComment,    temp_value );
    ArrayResize( _OrderExpiration, temp_value );

    // zeroize the arrays
    ArrayInitialize( _OrderTicket,     0 );
    ArrayInitialize( _OrderType,       0 );
    ArrayInitialize( _OrderLots,       0 );
    ArrayInitialize( _OrderOpenPrice,  0 );
    ArrayInitialize( _OrderStopLoss,   0 );
    ArrayInitialize( _OrderTakeProfit, 0 );
    ArrayInitialize( _OrderOpenTime,   0 );
    ArrayInitialize( _OrderProfit,     0 );
    ArrayInitialize( _OrderSwap,       0 );
    ArrayInitialize( _OrderCommission, 0 );
    ArrayInitialize( _OrderExpiration, 0 );
   

    _ExpertOrdersTotal = 0;
    for ( int z = _OrdersTotal - 1; z >= 0; z -- )
    {
        if ( !OrderSelect( z, SELECT_BY_POS ) )
        {
          _GetLastError = GetLastError();
          Print("OrderSelect(", z, ",SELECT_BY_POS) - Error #", 
                _GetLastError );
          continue;
        }
        if ( OrderMagicNumber() == magic && OrderSymbol() == 
             Symbol() )
        {
            // fill the arrays
            _OrderTicket[_ExpertOrdersTotal] = OrderTicket();
            _OrderType[_ExpertOrdersTotal] = OrderType();
            _OrderLots[_ExpertOrdersTotal] = 
            NormalizeDouble( OrderLots(), 1 );
            _OrderOpenPrice[_ExpertOrdersTotal] = 
            NormalizeDouble( OrderOpenPrice(), Digits );
            _OrderStopLoss[_ExpertOrdersTotal] = 
            NormalizeDouble( OrderStopLoss(), Digits );
            _OrderTakeProfit[_ExpertOrdersTotal] = 
            NormalizeDouble( OrderTakeProfit(), Digits );
            _OrderOpenTime[_ExpertOrdersTotal] = OrderOpenTime();
            _OrderProfit[_ExpertOrdersTotal] = 
            NormalizeDouble( OrderProfit(), 2 );
            _OrderSwap[_ExpertOrdersTotal] = 
            NormalizeDouble( OrderSwap(), 2 );
            _OrderCommission[_ExpertOrdersTotal] = 
            NormalizeDouble( OrderCommission(), 2 );
            _OrderComment[_ExpertOrdersTotal] = OrderComment();
            _OrderExpiration[_ExpertOrdersTotal] = 
            OrderExpiration();
            _ExpertOrdersTotal++;
        }
    }

    // change the arrays size according to the amount of 
    // positions that belong to the expert
    // (if _ExpertOrdersTotal = 0, change the arrays size for 1)
    temp_value = MathMax( _ExpertOrdersTotal, 1 );
    ArrayResize( _OrderTicket,     temp_value );
    ArrayResize( _OrderType,       temp_value );
    ArrayResize( _OrderLots,       temp_value );
    ArrayResize( _OrderOpenPrice,  temp_value );
    ArrayResize( _OrderStopLoss,   temp_value );
    ArrayResize( _OrderTakeProfit, temp_value );
    ArrayResize( _OrderOpenTime,   temp_value );
    ArrayResize( _OrderProfit,     temp_value );
    ArrayResize( _OrderSwap,       temp_value );
    ArrayResize( _OrderCommission, temp_value );
    ArrayResize( _OrderComment,    temp_value );
    ArrayResize( _OrderExpiration, temp_value );
}
 
sergery:

sorry,what is a persistence layer ?

Seems like you answered your own question - both with a definition and an example. There's a good discussion about it here -> https://www.mql5.com/en/forum/119716/.

Reason: