Problem with Counting pending orders with MQL5

 

Hi all

I would like to count pending orders properly by opening one pending order at a time. Unfortunately it opens many orders. I know I'm doing something wrong but I can not figure out what's wrong. Please help if you know whats the problem.

Thank You.

#property strict

#include <Trade\Trade.mqh>
#include <Trade\SymbolInfo.mqh>
#include <Trade\PositionInfo.mqh>
#include <Trade\AccountInfo.mqh>
#include <Trade\OrderInfo.mqh>

CTrade trade;
CTrade m_trade;
CPositionInfo m_position;
CSymbolInfo m_symbol;
COrderInfo m_order;

input int MagicNumber=555;
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
int NumberOfOrders()
 {
  int Count=0;
  for(int a=PositionsTotal()-1;a>=0;a--)
  {
   string symbol=PositionGetSymbol(a);
   if(Symbol()==symbol)
   {
    Count++;
   }
  }
  
  return(Count);
 }
 
int CountPending()
 {
  int Pending=0;
  for(int b=PositionsTotal()-1;b>=0;b--)
   {
    string OrderSymbol=OrderGetString(ORDER_SYMBOL);
    int Magic=(int)OrderGetInteger(ORDER_MAGIC);
    bool Select=OrderSelect(OrderGetTicket(b));
    if(OrderSymbol==Symbol()&&Magic==MagicNumber)
    {
     ENUM_ORDER_TYPE Type=(ENUM_ORDER_TYPE)OrderGetInteger(ORDER_TYPE);
     if(Type==ORDER_TYPE_BUY_STOP||Type==ORDER_TYPE_SELL_STOP)
     Pending++;
    }
   }
  return(Pending);
 } 

void Buy()
 {
  double Pips=0;
  double TickSize=SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_SIZE);
  if(TickSize==0.00001||TickSize==0.001)
  Pips=TickSize*10;
  else Pips=TickSize;
  
  double Ask=SymbolInfoDouble(Symbol(),SYMBOL_ASK);
  double Bid=SymbolInfoDouble(Symbol(),SYMBOL_BID);
  
  double CloseBar=iClose(NULL,0,1);
  double CloseBarNext=iClose(NULL,0,2);
  double OpenBar=iOpen(NULL,0,1);
  double OpenBarNext=iOpen(NULL,0,2);
  double HighBar=iHigh(NULL,0,1);
  double HighBarNext=iHigh(NULL,0,2);
  double LowBar=iLow(NULL,0,1);
  double LowBarNext=iLow(NULL,0,2);
  
  double EngulfingBuy=LowBar<LowBarNext&&HighBar>HighBarNext&&CloseBar>OpenBarNext&&OpenBar<CloseBar&&OpenBarNext>CloseBarNext;
  double HaramiBuy=OpenBarNext>CloseBarNext&&CloseBar>OpenBar&&HighBarNext>HighBar&&OpenBarNext>CloseBar&&LowBarNext<LowBar;
  
  int Support=iLowest(Symbol(),0,MODE_LOW,50,0);
  int Resistance=iHighest(Symbol(),0,MODE_HIGH,50,0);
  
  if(Ask>Resistance)
  {
  if(HaramiBuy)
  {
   trade.BuyStop(1,Ask+(5000*Pips),NULL,0,0,0,0,"JackBuda");
   //Print(GetLastError());
  }
  }
  
  if(Ask>Resistance)
  {
  if(EngulfingBuy)
  {
   trade.BuyStop(1,Ask+(5000*Pips),NULL,0,0,0,0,"JackBuda");
   //Print(GetLastError());
  } 
  }
  
 }
 
void Sell()
 {
  double Pips=0;
  double TickSize=SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_SIZE);
  if(TickSize==0.00001||TickSize==0.001)
  Pips=TickSize*10;
  else Pips=TickSize;
  
  double Ask=SymbolInfoDouble(Symbol(),SYMBOL_ASK);
  double Bid=SymbolInfoDouble(Symbol(),SYMBOL_BID);
  
  double CloseBar=iClose(NULL,0,1);
  double CloseBarNext=iClose(NULL,0,2);
  double OpenBar=iOpen(NULL,0,1);
  double OpenBarNext=iOpen(NULL,0,2);
  double HighBar=iHigh(NULL,0,1);
  double HighBarNext=iHigh(NULL,0,2);
  double LowBar=iLow(NULL,0,1);
  double LowBarNext=iLow(NULL,0,2);
  
  double EngulfingSell=LowBar<LowBarNext&&HighBar>HighBarNext&&CloseBar<OpenBarNext&&OpenBar>CloseBar&&OpenBarNext<CloseBarNext;
  double HaramiSell=OpenBarNext<CloseBarNext&&CloseBar<OpenBar&&HighBarNext<HighBar&&OpenBarNext<CloseBar&&LowBarNext>LowBar;
  
  int Support=iLowest(Symbol(),0,MODE_LOW,50,0);
  int Resistance=iHighest(Symbol(),0,MODE_HIGH,50,0);
  
  if(Bid<Support)
  {
  if(HaramiSell)
  {
   trade.SellStop(1,Bid-(5000*Pips),NULL,0,0,0,0,"JackBuda");
   //Print(GetLastError());
  }
  }
  
  if(Bid<Support)
  {
  if(EngulfingSell)
  {
   trade.SellStop(1,Bid-(5000*Pips),NULL,0,0,0,0,"JackBuda");
   //Print(GetLastError());
  }
  }
  
 } 

void OnTick()
 {
  if(ACCOUNT_BALANCE>10&&CountPending()<1){Buy();}
 
  if(ACCOUNT_BALANCE>10&&CountPending()<1){Sell();}

  for(int Loop=PositionsTotal()-1;Loop>=0;Loop--)
  {
   if(m_position.SelectByIndex(Loop))
   if(m_position.Symbol()==Symbol())
   {
    double ProfitPips=PositionGetDouble(POSITION_VOLUME)*100;
    double Profit2=PositionGetDouble(POSITION_PROFIT);
    int Type=(int)OrderGetInteger(ORDER_TYPE);
    switch(Type)
    {
     case ORDER_TYPE_BUY:if(Profit2>ProfitPips) m_trade.PositionClose(m_position.Ticket());
     case ORDER_TYPE_SELL:if(Profit2>ProfitPips) m_trade.PositionClose(m_position.Ticket());   
    }
  
   }
  }
   
 }
 
Jack Buda:

Hi all

I would like to count pending orders properly by opening one pending order at a time. Unfortunately it opens many orders. I know I'm doing something wrong but I can not figure out what's wrong. Please help if you know whats the problem.

Thank You.

Pending orders will be found in OrdersTotal(), I believe
 
Navdeep Singh:
Pending orders will be found in OrdersTotal(), I believe
I tried using that too, still doesn't work
 
Jack Buda:
I tried using that too, still doesn't work
And how is ask or bid greater than support and resistance. Those functions(iHighest() & iLowest()) only return the shift for highest and lowest bar respectively. You need to retrieve the value for those bars

And ACCOUNT_BALANCE is an enumeration. Use the proper function 👇🏻
 
Navdeep Singh:
And how is ask or bid greater than support and resistance. Those functions(iHighest() & iLowest()) only return the shift for highest and lowest bar respectively. You need to retrieve the value for those bars

And ACCOUNT_BALANCE is an enumeration. Use the proper function 👇🏻

As for the ACCOUNT_BALANCE, i have changed it.

void OnTick()
 {
  double AccountBalance=AccountInfoDouble(ACCOUNT_BALANCE);
  
  if(AccountBalance>10&&CountPending()<1){Buy();}
 
  if(AccountBalance>10&&CountPending()<1){Sell();}

  for(int Loop=PositionsTotal()-1;Loop>=0;Loop--)
  {
   if(m_position.SelectByIndex(Loop))
   if(m_position.Symbol()==Symbol())
   {
    double ProfitPips=PositionGetDouble(POSITION_VOLUME)*100;
    double Profit2=PositionGetDouble(POSITION_PROFIT);
    int Type=(int)OrderGetInteger(ORDER_TYPE);
    switch(Type)
    {
     case ORDER_TYPE_BUY:if(Profit2>ProfitPips) m_trade.PositionClose(m_position.Ticket());
     case ORDER_TYPE_SELL:if(Profit2>ProfitPips) m_trade.PositionClose(m_position.Ticket());   
    }
  
   }
  }
   
 }

For Support & Resistance should i switch to as follows? :

if(Ask>Support)
  {
  if(HaramiBuy)
  {
   trade.BuyStop(1,Ask+(5000*Pips),NULL,0,0,0,0,"JackBuda");
   //Print(GetLastError());
  }
  }
  
  if(Ask>Support)
  {
  if(EngulfingBuy)
  {
   trade.BuyStop(1,Ask+(5000*Pips),NULL,0,0,0,0,"JackBuda");
   //Print(GetLastError());
  } 
  }

  if(Bid<Resistance)
  {
  if(HaramiSell)
  {
   trade.SellStop(1,Bid-(5000*Pips),NULL,0,0,0,0,"JackBuda");
   //Print(GetLastError());
  }
  }
  
  if(Bid<Resistance)
  {
  if(EngulfingSell)
  {
   trade.SellStop(1,Bid-(5000*Pips),NULL,0,0,0,0,"JackBuda");
   //Print(GetLastError());
  }
  }


& for Count pending orders, it opens many orders instead of 1 pending order

int CountPending()
 {
  int Pending=0;
  for(int b=OrdersTotal()-1;b>=0;b--)
  //for(int b=PositionsTotal()-1;b>=0;b--)
   {
    string OrderSymbol=OrderGetString(ORDER_SYMBOL);
    int Magic=(int)OrderGetInteger(ORDER_MAGIC);
    bool Select=OrderSelect(OrderGetTicket(b));
    if(OrderSymbol==Symbol()&&Magic==MagicNumber)
    {
     ENUM_ORDER_TYPE Type=(ENUM_ORDER_TYPE)OrderGetInteger(ORDER_TYPE);
     if(Type==ORDER_TYPE_BUY_STOP||Type==ORDER_TYPE_SELL_STOP)
     Pending++;
    }
   }
  return(Pending);
 } 
 
Jack Buda:

As for the ACCOUNT_BALANCE, i have changed it.

For Support & Resistance should i switch to as follows? :


& for Count pending orders, it opens many orders instead of 1 pending order

For support and resistance you need to get value

Example 

 double support = iHigh(_Symbol,_Period,iHighest(_Symbol,_Period,MODE_HIGH,50,0));

For counting orders order selection should be done prior to getting order information

Use the OrdersInfo class you have included to perform checks instead of declaring new variable. 

Example

m_order.Symbol()==_Symbol;
 
Navdeep Singh:

For support and resistance you need to get value

Example 

For counting orders order selection should be done prior to getting order information

Use the OrdersInfo class you have included to perform checks instead of declaring new variable. 

Example

Thank You Navdeep, everything works perfectly fine now
Reason: