Help with multiple orders

Sakkull  
//+------------------------------------------------------------------+
//|                                                MAAAACROSSING.mq4 |
//|                        Copyright 2018, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
extern int TakeProfit=250;
extern int StopLoss=200;
extern double LotSize=0.01;
extern int MagicNumber=1234;
double pips;
double point;
int ticket;



//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   double ticksize = MarketInfo(Symbol(), MODE_TICKSIZE);
   if (ticksize == 0.00001 || point == 0.001)
   double pips = ticksize*10;
   else pips = ticksize;
//---

   
   return(INIT_SUCCEEDED);
  }
  
  void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   double CurrentGreen = iCustom(NULL,0,"TDI Red Green",4,1);
   double CurrentRed = iCustom(NULL,0,"TDI Red Green",5,1);
   
   double PreviousGreen = iCustom(NULL,0,"TDI Red Green",4,2);
   double PreviousRed = iCustom(NULL,0,"TDI Red Green",5,2);
   
   double CurrentYellow = iCustom(NULL,0,"TDI Red Green",2,1);
   
   double CandleHigh = iHigh(NULL,0,1);
   double CandleLow = iLow(NULL,0,1);
   
   double CurrentBlue = iMA(NULL,0,10,0,1,0,1);
   double PreviousBlue = iMA(NULL,0,10,0,1,0,2);
   double CurrentPink = iMA(NULL,0,200,0,1,0,1);
   
   if(OrdersTotal()==1 && CandleHigh>CurrentPink) CloseAllSellOrders();
   if(OrdersTotal()==1 && CandleHigh<CurrentPink) CloseAllBuyOrders();
   if(OrdersTotal()==1 && CandleHigh>CurrentBlue && CandleLow>CurrentBlue) CloseAllSellOrders();
   if(OrdersTotal()==1 && CandleHigh<CurrentBlue && CandleLow<CurrentBlue) CloseAllBuyOrders();
   
   datetime time = TimeCurrent();
   string HoursAndMinutes = TimeToString(time,TIME_MINUTES);
   

   
   ////--------------------------------------------------------------
   
   ///BUY

   if(CandleHigh>CurrentPink && CandleHigh>CurrentBlue && CandleLow>CurrentBlue && CurrentGreen>CurrentYellow && CurrentRed>CurrentYellow && PreviousGreen<PreviousRed && CurrentGreen>CurrentRed)
   if(OrdersTotal()==0)
      int sellticket = OrderSend
       (
       Symbol(),
       OP_BUY,
       0.01,
       Ask,
       3,
       Ask-200*Point,
       Ask+500*Point,
       NULL,
       MagicNumber,
       0,
       Blue
       );
   
  
   ///SELL
   
   if(CandleLow<CurrentPink && CandleLow<CurrentBlue && CandleHigh<CurrentBlue && CurrentGreen<CurrentYellow && CurrentRed<CurrentYellow && PreviousGreen>PreviousRed && CurrentGreen<CurrentRed)
   if(OrdersTotal()==0)
       int sellticket = OrderSend
       (
       Symbol(),
       OP_SELL,
       0.01,
       Bid,
       3,
       Bid+200*Point,
       Bid-500*Point,
       NULL,
       MagicNumber,
       0,
       Red
       );
       

   }
   
   
   
/////////////////////////////////////////////////////////////////////////////////////////////////////
   
      void CloseAllSellOrders()
  {
  int i, iTotalOrders;
   
   iTotalOrders=OrdersTotal()-1; // Rosh line
  
   for (i=iTotalOrders; i>=0; i--) // Rosh line     
   
   { 
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
      { 
         if (OrderMagicNumber()==MagicNumber)
         { 
            if (OrderType()==OP_SELL) OrderClose(OrderTicket(),OrderLots(),Ask,5,Violet);
            if (OrderType()==OP_SELLSTOP) OrderDelete(OrderTicket());
            if (OrderType()==OP_SELLLIMIT) OrderDelete(OrderTicket());
         }
      }
   }
}




      void CloseAllBuyOrders()
  {
  int i, iTotalOrders;
  
   iTotalOrders=OrdersTotal()-1; // Rosh line
  
   for (i=iTotalOrders; i>=0; i--) // Rosh line      
   
   
   { 
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
      { 
         if (OrderMagicNumber()==MagicNumber)
         { 
            if (OrderType()==OP_BUY) OrderClose(OrderTicket(),OrderLots(),Bid,5,Violet);
            if (OrderType()==OP_BUYSTOP) OrderDelete(OrderTicket());
            if (OrderType()==OP_BUYLIMIT) OrderDelete(OrderTicket());
            
         }
      }
   }
}

Hello, can somebody explain what do I do wrong that during backtesting, at some point (GBPUSD - 2017.03.06), my EA opens hundreds of sell orders

Marco vd Heijden  

Code it to prevent that.

Always set a safety fuse for ORDERS_MAX to prevent runaway situations.

Sakkull  
Sorry to say that but I am a noob, could you please elaborate how to do that since there is no ORDERS_MAX function?



Marco vd Heijden  
Sakkull:
Sorry to say that but I am a noob,

In that case please go here: https://www.mql5.com/en/job

You have to create the ORDERS_MAX function yourself, so that it checks the number of open orders and if it is allowed to open more.

Trading applications for MetaTrader 5 to order
Trading applications for MetaTrader 5 to order
  • www.mql5.com
Hello developers, need your help. Object: MT4 EA Automated Forex Trading with trading signals Strategy: indicators for triggering a buy or sell signal. Closed position by reverse signal or Stop Loss. Code of indicators available. General attitude: Work in all Symbols ( Pairs, index, commod) Money management Trailing Stop (not visible for...