How should i check if I have an open buy order?

 

Hey Buddies

I'm not a computer programmer and my question seems simple. But I'm confused about it. 

I have read MQL4 book and function reference, but i can't find the solution.

I just want to check if there is an open buy position or not?

I tried to use OrdersTotal() but it returns buy and sell number, while I want to check if there is just Buy position.

A way is using OrderType() and OrderSelect() but I cant understand their logic. What is ticket number when my program even doesn't know is there any open order or not?

I should say, I want to check orders for each symbol separately and there will be no pending order. Just open market orders.


My problem in two sentences: << How should i check if I have an open buy order? what is the code I need? >>

if you give the code, it's good and enough. and code description is welcomed.

Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
  • www.mql5.com
Predefined Macro Substitutions - Named Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
s115rz:
Apologize for wrong place for topic.

@ s115rz

ddd


//+------------------------------------------------------------------+
//|                                                           SS.mq4 |
//|                                 Copyright 2019, Haskaya Software |
//|                                   https://www.haskayayazilim.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, Haskaya Software"
#property link      "https://www.haskayayazilim.net"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
    int OrderCountBuy=OrderFind(0);
    int OrderCountSell=OrderFind(1);
    Print("Buy Ordet Count "+string(OrderCountBuy)+" Sell Order Count "+string(OrderCountSell));
    
  }
//+------------------------------------------------------------------+
int OrderFind(int Durum)
{
  int sonuc=0;
  int total = OrdersTotal();
   for(int i=total-1;i>=0;i--)
    {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)        break;
     
       if (OrderType()==Durum) sonuc=sonuc+1;     
    }
 
   
    return(sonuc);
}
 
Mehmet Bastem:

@ s115rz



Thanks Mehmet for code.

As i want to check symbol by symbol, I have written this code. Will it work?



Bool BPos = 0;
Bool SPos = 0;

for( int i = 0 ; i < OrdersTotal() ; i++ )
   {
   OrderSelect( i, SELECT_BY_POS, MODE_TRADES ); 
   if (OrderSymbol() == Symbol() && OrderType() == OP_BUY)
      {
      BPos = 1;
      }
   if (OrderSymbol() == Symbol() && OrderType() == OP_SELL)
      {
      SPos = 1;
      }
   }
 
Mehmet Bastem:
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)        break;

Why do you exit the loop if the OrderSelect() fails?

 
s115rz:

Thanks Mehmet for code.

As i want to check symbol by symbol, I have written this code. Will it work?

Your code is ok, but it is bool, not Bool.

You should also get into the habit of using false or true instead of 0 and 1 for bools. Makes it easier to follow your code.

   bool BPos = false;
   bool SPos = false;

   for( int i = 0 ; i < OrdersTotal() ; i++ )
      {
      if(OrderSelect( i, SELECT_BY_POS, MODE_TRADES) && OrderSymbol() == Symbol())
         if (OrderType() == OP_BUY)
            {
            BPos = true;
            }
         else if (OrderType() == OP_SELL)
            {
            SPos = true;
            }
      }
 
Keith Watford:

Your code is ok, but it is bool, not Bool.

You should also get into the habit of using false or true instead of 0 and 1 for bools. Makes it easier to follow your code.


Thank you so much.

Good advice.

Reason: