How to make an EA place a single order per signal, and keep placing another order once there is a new signal even if the previous orders are still in the market

 


Hello my people.

I am currently building an EA that place order once 5 & 20 exponential moving averages crosses each other, the issue is once the fast moving average crosses the slow moving avearge to the upside, the bot will immediately place multiple buy order and stop out will occur.

So i managed to stop that behavior by checking if(ordersTotal() == 0){ ordersend() }, but i want the bot to be able to place multiple other even there are still active market other in the market, but it should place only one single order per signal.

Since i did not use stop loss in designing the bot, so it is possible to have a buy signal while there is a sell signal in loss.

For example if there is a buy signal place one buy order, if there is a sell signal place one sell order, either the initial buy order hit take profit or not keep placing another other once there is a crossover. 

See my code below, it is not working. if there is a sell order in the market, if buy signal occur it will not place any trade.

Please show me a better way to do this.


string maCrossOverSignal()
  {
   string signal;

   double fastEmaBar0 = iMA(NULL,0,fastEma_Period,fastEma_ma_Shift,fastEma_ma_Method,fastEma_applied_Price,fastEma_shift_CandleIndex);
   double slowEmaBar0 = iMA(NULL,0,slowEma_Period,slowEma_ma_Shift,slowEma_ma_Method,slowEma_applied_Price,slowEma_shift_CandleIndex);

   double fastEmaBar1 = iMA(NULL,0,fastEma_Period,fastEma_ma_Shift,fastEma_ma_Method,fastEma_applied_Price,fastEma_shift_CandleIndex + 1);
   double slowEmaBar1 = iMA(NULL,0,slowEma_Period,slowEma_ma_Shift,slowEma_ma_Method,slowEma_applied_Price,slowEma_shift_CandleIndex + 1);

   if((fastEmaBar1 < slowEmaBar1) && (fastEmaBar0 > slowEmaBar0))
     {
      signal = "Buy";
     }
   else
      if((fastEmaBar1 > slowEmaBar1) && (fastEmaBar0 < slowEmaBar0))
        {
         signal = "Sell";
        }
   return signal;
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void enterMarket()
  {
   if(maCrossOverSignal() == "Buy")
     {
      // if there is a buy signal & there is no active or pending trade in the market
      if(OrdersTotal() == 0)
        {
         Print("time to Buy ");
         maBt = orderSend(NULL,OP_BUY,lotSize,Ask,100,0,0,"maBt");
         if(maBt > 0)
           {
            if(OrderSelect(maBt,SELECT_BY_TICKET))
              {
               modify(maBt,OrderOpenPrice(),/*OrderOpenPrice() - 500 * Point*/0,OrderOpenPrice() + 100 * Point,0);
              }
           }
        }
      else // if there is a buy signal while the last placed other is a buy order & still in the market & the order is in profit dont place another other
         if(OrderSelect(OrdersTotal()-1,SELECT_BY_TICKET))
           {
            if(OrderType() == OP_BUY && OrderProfit() >= 0)
              {
               return;
              }
            else //if there is a buy signal & there is already a sell order in the market, i still want to buy
               if(OrderType() == OP_SELL)
                 {
                  Print("time to Buy ");
                  maBt = OrderSend(NULL,OP_BUY,lotSize,Ask,100,0,0,"maBt");
                  if(maBt > 0)
                    {
                     if(OrderSelect(maBt,SELECT_BY_TICKET))
                       {
                        modify(maBt,OrderOpenPrice(),/*OrderOpenPrice() - 500 * Point*/0,OrderOpenPrice() + 100 * Point,0);
                       }
                    }
                 }
           }
      //if(){}
     }
   else
      if(maCrossOverSignal() == "Sell")
        {
         // if there is a sell signal & there is no active or pending trade in the market
         if(OrdersTotal() == 0)
           {
            Print("time to Sell ");
            maSt = OrderSend(NULL,OP_SELL,lotSize,Bid,100,0,0,"maSt");
            if(maSt > 0)
              {
               if(OrderSelect(maSt,SELECT_BY_TICKET))
                 {
                  modify(maSt,OrderOpenPrice(),/*OrderOpenPrice() + 500 * Point*/0,OrderOpenPrice() - 100 * Point,0);
                 }
              }

           }
         else  // if there is a sell signal while the last placed other is a sell order & still in the market & the order is in profit dont place another other
            if(OrderSelect(OrdersTotal()-1,SELECT_BY_POS))
              {
               if(OrderType() == OP_SELL && OrderProfit() >= 0)
                 {
                  return;
                 }
               else //if there is a sell signal & there is already a buy order in the market, i still want to sell
                  if(OrderType() == OP_BUY)
                    {
                     Print("time to Sell ");
                     maSt = OrderSend(NULL,OP_SELL,lotSize,Bid,100,0,0,"maSt");
                     if(maSt > 0)
                       {
                        if(OrderSelect(maSt,SELECT_BY_TICKET))
                          {
                           modify(maSt,OrderOpenPrice(),/*OrderOpenPrice() + 500 * Point*/0,OrderOpenPrice() - 100 * Point,0);
                          }
                       }
                    }
              }
        }
  }
 
  1. Only check for a cross on a new bar

    For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart), volume is unreliable (miss ticks), Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.
              New candle - MQL4 programming forum #3 2014.04.04

    I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
              Running EA once at the start of each bar - MQL4 programming forum 2011.05.06

  2. or, check for a change in signal.
              MQL4 (in Strategy Tester) - double testing of entry conditions - MQL5 programming forum #1 2017.12.12

Reason: