Problem with pending orders

 

Hello everyone,

This is my first EA and also first post on this forum :-)

So,  my EA based on Moving Averages Indicator these are basic conditions:

1.       The first candle after crossing the lines is the candle, where I’m placing the pending order with price 0,00015pips above ‘high price’ or 0,00015pips under ‘low price’ of signaled candle.

2.       So pending order is waiting for appropriate price to open itself, let’s say as a SELL order, but sometimes that pending order doesn’t reach the right price to change itself to SELL order(or BUY order) and then the opposite signal appears to place BUYSTOP, ok and the main problem is when BUYSTOP opens as a BUY order, and at that point I want to delete pending SELL order. (or pending BUY order if SELLSTOP opens as SELL order).

3.       And then let’s say after next 5 candles we get signal to SELLSTOP and if pending order opens as a SELL order I want to close BUY order.

First I’ve only added just the functionality of pending orders without deleting them such as, placing pending orders on every signal with appropriate price mentioned in point 1 and etc. Then, I’ve tried to add second point from the above’s list, but then I lost control on my EA’s, I don’t know what exactly is going on and what’s worse I don’t know how to fix is. About expiration of pending orders…I’ve set 1hr, because I’m testing it on 5M period and I didn’t think much about setting the expiration time, because if EA would delete pending orders like I want it to do, the time could be infinity, as I don’t want to close pending orders because of expiring time, sometimes it lasts 2hr sometimes 6hr, that depends on period time.

Like I said it’s my first EA and first experience with mql4, so I am asking for your understanding. Maybe it’s just a problem with some line of code or some conditions or what maybe worse my wrong understanding.
 I know there is quite a bit lines of code, but I believe, that for some more experienced users won’t be a problem. I’ve tried to name the functions and variables as to what they do, but if somehow my code would be
 not clear to understand, I offer my help.

 

Thank you in advance for your help.

P.S. Here is the main part of code, the whole code is placed as a file under the post.

extern double PadAmount=1.5;
extern double PipsValue=34.73;

extern bool UseTraillingStop=true;

extern double RewardRatio=2;;
extern double RiskPercent=3;

extern int FirstMA=5;
extern int FirstMaShift=0;
extern int FirstMaMethod=1;
extern int FirstMaAppliedTo=0;

extern int SecondMA=21;
extern int SecondMaShift=0;
extern int SecondMaMethod=1;
extern int SecondMaAppliedTo=0;

extern int MagicNumber=1234;
double pips;
double pipsToBsl;
double pipsToSsl;

double high,low;
//+------------------------------------------------------------------+
int OnInit()
  {
   double tickSize=MarketInfo(Symbol(),MODE_TICKSIZE);
   if(tickSize==0.00001 || tickSize==0.001)
      pips=tickSize*10;
   else pips=tickSize;

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {

  }
//+------------------------------------------------------------------+
void OnTick()
  {
   if(UseTraillingStop)
      traillingStop();
   if(isNewCandle())
      checkForMaTrade();

//BUY conditions
   if(Ask>(high)
     {
      for(int i=OrdersTotal()-1; i>=0; i--)
        {
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
           {
            int cmd=OrderType();
            if(cmd!=OP_BUY && cmd!=OP_SELL && cmd==OP_SELLSTOP)
              {
               closeSellOrder();
               bool result=OrderDelete(OrderTicket());
               if(result!=true) Print("LastError of BUY's deleting order= ",GetLastError());
               break;
              }
           }
         else { Print("Error when order select ",GetLastError()); break; }
        }
     }
//SELL conditions
   if(Bid<low)
     {
      for(int i=OrdersTotal()-1; i>=0; i--)
        {
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
           {
            int cmd=OrderType();
            if(cmd!=OP_BUY && cmd!=OP_SELL && cmd==OP_BUYSTOP)
              {
               closeBuyOrder();
               bool result=OrderDelete(OrderTicket());
               if(result!=true) Print("LastError of SELL's deleting order= ",GetLastError());
               break;
              }
           }
         else { Print("Error when order select ",GetLastError()); break; }
        }
     }
  }
//+------------------------------------------------------------------+
void checkForMaTrade()
  {
   static int countCandle=0;
   double currentFirstMA=iMA(NULL,0,FirstMA,FirstMaShift,FirstMaMethod,FirstMaAppliedTo,1);
   double currentSecondMA = iMA(NULL,0,SecondMA,SecondMaShift,SecondMaMethod,SecondMaAppliedTo,1);
   double previousFirstMA = iMA(NULL,0,FirstMA,FirstMaShift,FirstMaMethod,FirstMaAppliedTo,2);
   double previousSecondMA= iMA(NULL,0,SecondMA,SecondMaShift,SecondMaMethod,SecondMaAppliedTo,2);

   countCandle++;
//BUY comditions
   if(currentFirstMA>currentSecondMA && previousFirstMA<previousSecondMA)
     {
      high=High[1]+1.5*pips;
      SendNotification("Zarejestrowano przecięcie średnich - sygnał BUY");
      orderEntry(0,countCandle,high);
      countCandle=0;
     }
//SELL conditions
   if(currentFirstMA<currentSecondMA && previousFirstMA>previousSecondMA)
     {
      low=Low[1]-1.5*pips;
      SendNotification("Zarejestrowano przecięcie średnich - sygnał SELL");
      orderEntry(1,countCandle,low);
      countCandle=0;
     }
  }
//+------------------------------------------------------------------+
bool isNewCandle()
  {
   static int barsOnChart=0;
   if(Bars==barsOnChart)
     {
      return (false);
     }
   barsOnChart=Bars;
   return (true);
  }
//+------------------------------------------------------------------+
void orderEntry(int direction,int candlesBack,double price)
  {
   int err=0;
   double lotSize= 0;
   double equity = AccountEquity();
   double riskedAmount=equity*RiskPercent*0.01;

   int buyStopCandle=iLowest(NULL,0,1,candlesBack,1);
   double buyStopPrice=Low[buyStopCandle];
   pipsToBsl=Ask-buyStopPrice;
   double buyTakeprofitPrice=Ask+pipsToBsl*RewardRatio;

   int sellStopCandle=iHighest(NULL,0,2,candlesBack,1);
   double sellStopPrice=High[sellStopCandle];
   pipsToSsl=sellStopPrice-Bid;
   double sellTakeprofitPrice=Bid-pipsToSsl*RewardRatio;

//BUY order
   if(direction==0)
     {
      double bsl=buyStopPrice;
      double btp=buyTakeprofitPrice;
      lotSize=riskedAmount/((pipsToBsl/pips)*PipsValue);
      int orderBuy=OrderSend(Symbol(),OP_BUYSTOP,lotSize,price,2,0,0,NULL,MagicNumber,TimeCurrent()+60*60,Red);
      if(orderBuy<0)
        {
         err=GetLastError();
         Print("Error order BUY errorNumber= ",err);
        }
      if(orderBuy>0)
        {
         bool orderBuyModify=OrderModify(orderBuy,OrderOpenPrice(),bsl,btp,0,Red);
         if(orderBuyModify!=true)
           {
            err=GetLastError();
            Print("Error order open-modify SELL errorNumber= ",err);
           }
        }
     }
//SELL order
   if(direction==1)
     {
      double ssl=sellStopPrice;
      double stp=sellTakeprofitPrice;
      lotSize=riskedAmount/((pipsToSsl/pips)*PipsValue);
      int orderSell=OrderSend(Symbol(),OP_SELLSTOP,lotSize,price,3,0,0,NULL,MagicNumber,TimeCurrent()+60*60,Green);
      if(orderSell<0)
        {
         err=GetLastError();
         Print("Error order SELL errorNumber= ",err);
        }
      if(orderSell>0)
        {
         bool orderSellModify=OrderModify(orderSell,OrderOpenPrice(),ssl,stp,0,Green);
         if(orderSellModify!=true)
           {
            err=GetLastError();
            Print("Error order open-modify SELL errorNumber= ",err);
           }
        }
     }
  }
//+------------------------------------------------------------------+
 
Pilar12:

Hello everyone,

This is my first EA and also first post on this forum :-)

So,  my EA based on Moving Averages Indicator these are basic conditions:

1.       The first candle after crossing the lines is the candle, where I’m placing the pending order with price 0,00015pips above ‘high price’ or 0,00015pips under ‘low price’ of signaled candle.

2.       So pending order is waiting for appropriate price to open itself, let’s say as a SELL order, but sometimes that pending order doesn’t reach the right price to change itself to SELL order(or BUY order) and then the opposite signal appears to place BUYSTOP, ok and the main problem is when BUYSTOP opens as a BUY order, and at that point I want to delete pending SELL order. (or pending BUY order if SELLSTOP opens as SELL order).

3.       And then let’s say after next 5 candles we get signal to SELLSTOP and if pending order opens as a SELL order I want to close BUY order.

First I’ve only added just the functionality of pending orders without deleting them such as, placing pending orders on every signal with appropriate price mentioned in point 1 and etc. Then, I’ve tried to add second point from the above’s list, but then I lost control on my EA’s, I don’t know what exactly is going on and what’s worse I don’t know how to fix is. About expiration of pending orders…I’ve set 1hr, because I’m testing it on 5M period and I didn’t think much about setting the expiration time, because if EA would delete pending orders like I want it to do, the time could be infinity, as I don’t want to close pending orders because of expiring time, sometimes it lasts 2hr sometimes 6hr, that depends on period time.

 

Thank you in advance for your help.

P.S. Here is the main part of code, the whole code is placed as a file under the post.

I was willing to help but the code you provided (mq4 file) doesn't even compile so, it's not very serious to ask for help in such condition.

Reason: