Orderselect last order open

To add comments, please log in or register
jordarro
102
jordarro  

Hi guys,

I have a problem, i want to select the last open order and know the OrderOpenPrice, so i can do:

OrderSelect(0,SELECT_BY_POS);

double last_price = OrderOpenPrice


It works fine but when i have 2 operations this function always returns the first operation OrderOpenPrice, I want to know the second operation (the last one) order price and forget my first price, what can i do?

Thanks in advance

Alexander
2441
Alexander  
OrderSelect(OrdersTotal()-1,SELECT_BY_POS);

double last_price = OrderOpenPrice;
jordarro
102
jordarro  
Roger:


Thanks Roger, you are the best!

If someone need the code this is the solution that worked for me perfectly.

int new_var = OrdersTotal()-1

OrderSelect(new_var,SELECT_BY_POS);

double last_price = OrderOpenPrice;

RJo
94
RJo  
int new_var = OrdersTotal()-1

OrderSelect(new_var,SELECT_BY_POS);

double last_price = OrderOpenPrice();
Don't forget the ()'s after OrderOpenPrice.
David Diez
1405
David Diez  
What if need to select OpenPrice from last buy or sell order?
Lorentzos Roussos
18914
Lorentzos Roussos  
David Diez:
What if need to select OpenPrice from last buy or sell order?

Try these :

struct rop_data
{
bool valid;
int order_type;
double recent_open_price;
bool has_sl;
bool has_tp;
double sl;
double tp;
double lots;
int ticket;
int magic;
datetime opened;
};

rop_data recent_order_price(bool filter_symbol,
                            string symbol,
                            bool filter_magic_number,
                            int magic_number,
                            bool filter_order_type,
                            ENUM_ORDER_TYPE type,
                            bool filter_time_window,
                            datetime from,
                            datetime to,
                            bool filter_lot_size,
                            double min_lot,
                            double max_lot)
{
rop_data result;
result.valid=false;

//total orders 
  int tot=OrdersTotal();
  if(tot>0)
  {
    //found
    int found_ticket=-1;
    for(int o=tot-1;o>=0;o--)
    {
    bool selector=OrderSelect(o,SELECT_BY_POS,MODE_TRADES);
    //if order selected 
    if(selector)
      {
      //valids list
      bool valid_magic=true;
      bool valid_date=true;
      bool valid_type=true;
      bool valid_symbol=true;
      bool valid_lot=true;
      //symbol
      if(filter_symbol==true&&OrderSymbol()!=symbol) valid_symbol=false;
      //type
      if(filter_order_type==true&&OrderType()!=type) valid_type=false;
      //magic
      if(filter_magic_number==true&&OrderMagicNumber()!=magic_number) valid_magic=false;
      //lot
      if(filter_lot_size==true&&(OrderLots()<min_lot||OrderLots()>max_lot)) valid_lot=false;
      //date
      if(filter_time_window==true&&(OrderOpenTime()<from||OrderOpenTime()>to)) valid_date=false;
      //if all are valid - thats our order 
        if((valid_date&&valid_lot&&valid_magic&&valid_symbol&&valid_type)==true)
          {
          found_ticket=OrderTicket();
          //fill up data 
          result.valid=true;
          result.ticket=found_ticket;
          result.has_sl=false;
          if(OrderStopLoss()!=0) result.has_sl=true;
          result.has_tp=false;
          if(OrderTakeProfit()!=0) result.has_tp=true;
          result.sl=OrderStopLoss();
          result.tp=OrderTakeProfit();
          result.lots=OrderLots();
          result.magic=OrderMagicNumber();
          result.order_type=OrderType();
          result.recent_open_price=OrderOpenPrice();
          result.opened=OrderOpenTime();
          break;
          }
      }
    //if order selected ends her e
    }
    
  }
//total orders 

return(result);
}
William Roeder
20214
William Roeder  
RJo:
int new_var = OrdersTotal()-1

OrderSelect(new_var,SELECT_BY_POS);

double last_price = OrderOpenPrice();
Using OrdersTotal directly and/or no Magic number filtering on your OrderSelect loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
          Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 and MetaTrader 4 - MQL4 programming forum
          MagicNumber: "Magic" Identifier of the Order - MQL4 Articles
To add comments, please log in or register