# Finding the First Order Open Price from several Open orders

376

I am trying to find the first open price from several open orders and symbols.  Below is my initial attempt.  Need help getting this to work.

```/***********************************
//Finding the First OrderPrice for a bunch of orders and symbols
***********************************/

int FindFirstOrderPrice(int magic, string Symb, int Type)
{
int orderstotal = OrdersTotal();
int orders      = 0;
int ordticket[100][2];
int j;
double oldorderopenprice=0,orderprice;
int oldticketnumber=0,ticketnumber;

for ( j = 0; j < orderstotal; j++)
{

if (OrderMagicNumber() != magic || OrderSymbol() != Symb)
continue;

int orderType = OrderType();
if (orderType != OP_BUY || orderType != OP_SELL)
continue;

if ( (orderType == OP_BUY || orderType == OP_SELL) && OrderMagicNumber() == magic && OrderSymbol()==Symb)
{
ordticket[orders][0] = OrderOpenTime();
ordticket[orders][1] = OrderTicket();
orders++;
}
}

ArrayResize(ordticket,orders);
ArraySort(ordticket);

for(j = 0; j < orders; j++)
{
OrderSelect(j, SELECT_BY_POS);
if(OrderMagicNumber() != magic || OrderSymbol()!=Symb)
continue;
ArrayResize(ordticket, ArraySize(ordticket) + 1);
ordticket[ArraySize(ordticket) - 1] = OrderTicket();
if(OrderSymbol()==pair && OrderMagicNumber()==magicNumber && OrderType()==OP_SELL)
{
ticketnumber=OrderTicket();
if(ticketnumber>oldticketnumber)
{
orderprice=OrderOpenPrice(ordticket[j][1]);
oldorderopenprice=orderprice;
oldticketnumber=ticketnumber;
}
}
}

return(orderprice);

}
//+------------------------------------------------------------------+
```
1498

Kengen:

I am trying to find the first open price from several open orders and symbols.  Below is my initial attempt.  Need help getting this to work.

I'm not sure that I understand what you want to do because your code seems far, far more complex than is necessary from your description. If you want to find the open price of the earliest order for a given symbol, type, and magic number, then the following should be sufficient:

```double FindFirstOrderPrice(int magic, string Symb, int Type)
{
double Price = 0;
datetime EarliestOrder = D'2099/12/31';

for (int i = 0; i < OrdersTotal(); i++) {
if (OrderSelect(i, SELECT_BY_POS)) {
if (OrderType() == Type && OrderSymbol() == Symb && OrderMagicNumber() == magic) {
if (EarliestOrder > OrderOpenTime()) {
EarliestOrder = OrderOpenTime();
Price = OrderOpenPrice();
}
}
}
}
return Price; // Returns 0 if no matching orders
}```

Even if I have misunderstood what you are trying to do, I still have two comments:

• You seem to be trying to return a price as an int when you should be returning it as a double
• The term "first" is ambiguous. For example, let's say that you place two orders, A and B, and then do a partial close of A. In MT4 you will then have orders B and C, where C is reported as having a later ticket number but earlier open-time than B. It's a matter of personal taste whether you consider that C is earlier than B. The above code assumes that you want to select orders by time. Your current code looks as though it picks the order with the earliest ticket.
376

I believe I did not explain my need well enough.  I am trading on a FXCM-US account which requires that trades are closed using FIFO rules (First In First Out).  I have open 5 orders each of 5 different pairs ( for a total of 25 open orders).  My goal is to close all 25 orders when an AccountProfit()  target is reached.  Under FIFO I will need to sort and order all the pairs based on the opening time and close the trades based on the oldest open time and ticket.  I was intending to find the first open price and the use a loop to close all other trades.

122

Use this library - it will do what you are asking for: https://www.mql5.com/en/code/11786