EA will not open sell order (sell stop) & opens too many orders

 

Hi all

My EA will not open sell stop orders & opens too many orders instead of 1. If you know the problem please help.

#property copyright "Copyright 2022, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

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

CTrade trade;
CPositionInfo m_position;
CSymbolInfo m_symbol;
COrderInfo m_order;
CDealInfo  m_deal;
CAccountInfo m_account;

input int MagicNumber=11;
input double TakeProfit=20.5;
input double StopLoss=49.5;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
 {
  ResetLastError();
  
  if(!m_symbol.Name(Symbol()))
  {
   return(INIT_FAILED);
  }
  
  trade.SetExpertMagicNumber(MagicNumber);
  trade.SetMarginMode();
  trade.SetTypeFillingBySymbol(m_symbol.Name());
  
  return(INIT_SUCCEEDED);
 }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
int OrdersPending()
 {
  int Pending=0;
  for(int a=OrdersTotal()-1;a>=0;a--)
  {
   bool Select=OrderSelect(OrderGetTicket(a));
   string OrderSymbol=OrderGetString(ORDER_SYMBOL);
   int Magic=(int)OrderGetInteger(ORDER_MAGIC);
   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 TakeProfitBuy=Ask+TakeProfit*Pips;
  double StopLossBuy=Ask-StopLoss*Pips;
  
  datetime Expire=TimeCurrent()+(60*15);
  
  double Close[];
  ArraySetAsSeries(Close,true);
  CopyClose(Symbol(),0,0,Bars(Symbol(),Period()),Close);
  
  double Lowest=iLow(Symbol(),0,iLowest(Symbol(),0,MODE_LOW,10,1));
  double Lowest2=iLow(Symbol(),0,iLowest(Symbol(),0,MODE_LOW,10,2));
  
  if(OrdersTotal()==0)
  {
   if(Close[1]>Lowest&&Lowest2<Close[1])
   {
    trade.BuyStop(0.01,Ask+0.5*Pips,NULL,StopLossBuy,TakeProfitBuy,ORDER_TIME_SPECIFIED,Expire,"JackBuda");
   }
  }
  
 }
 
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 TakeProfitSell=Bid-TakeProfit*Pips;
  double StopLossSell=Bid+StopLoss*Pips;
  
  datetime Expire=TimeCurrent()+(60*15);
  
  double Close[];
  ArraySetAsSeries(Close,true);
  CopyClose(Symbol(),0,0,Bars(Symbol(),Period()),Close);
  
  double Highest=iHigh(Symbol(),0,iHighest(Symbol(),0,MODE_HIGH,10,1));
  double Highest2=iHigh(Symbol(),0,iHighest(Symbol(),0,MODE_HIGH,10,2));

  if(OrdersTotal()==0)
  {
   if(Close[1]<Highest&&Close[1]>Highest2)
   {
    trade.SellStop(0.01,Bid-0.5*Pips,NULL,StopLossSell,TakeProfitSell,ORDER_TIME_SPECIFIED,Expire,"JackBuda");
   }
  }
  
 }  

void OnTick()
 {
  int Spread=(int)SymbolInfoInteger(Symbol(),SYMBOL_SPREAD);
  double MaxLot=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MAX);
  int Leverage=(int)AccountInfoInteger(ACCOUNT_LEVERAGE);
  
  Comment("Spread is "+DoubleToString(Spread/10,1)+
  "\nMax Lot For Broker is "+DoubleToString(MaxLot/1,1)+
  "\nLeverage For Broker is "+DoubleToString(Leverage/1,1));
  
  double AccBalance=AccountInfoDouble(ACCOUNT_BALANCE);
  
  int Figures=0;
  
  if(AccBalance>10)
  {
   Figures=2;
  }
  
  switch(Figures)
  {
   case 2:
   if(OrdersPending()<1){Buy();}
   
   if(OrdersPending()<1){Sell();}
  }
   
 }
 

Figured out why sell wont open.

if(Close[1]>Lowest&&Lowest2<Close[1])

To

if(Close[1]>Lowest&&Close[1]<Lowest2)

Still trying to figure out why it opens too many trades.

 
Jack Buda #: Still trying to figure out why it opens too many trades.

You open a pending order if there are no other pending orders. You don't check for open positions.

 
William Roeder #:

You open a pending order if there are no other pending orders. You don't check for open positions.

Sorry what I meant was too many "pending" orders & not positions
 
Jack Buda #: Sorry what I meant was too many "pending" orders & not positions

No, what you said was "why it opens too many trades."

When a pending order opens, it is no longer a pending order. So your code opens another pending order.

 
William Roeder #:

No, what you said was "why it opens too many trades."

When a pending order opens, it is no longer a pending order. So your code opens another pending order.

Is it possible not to open another besides using the following:

if(MaxTrades!=1)
  {
   if(Close[1]>Lowest&&Lowest2<Close[1])
   {
    MaxTrades=1;
    trade.BuyStop(0.01,Ask+0.5*Pips,NULL,StopLossBuy,TakeProfitBuy,ORDER_TIME_SPECIFIED,Expire,"JackBuda");
   }
  }
Reason: