How should I selectively close orders opened by different strategies in one EA? Help please!

 

Hi, I am having some technical issues regarding selective order closing in MT4, and I would appreciate the help!

This is what I try to achieve:

I have two different strategies on buy orders and two different strategies on sell orders, and I would set up specific order closing conditions for orders opened by their specific strategies without letting the order closing conditions interfering with each other. 

Here is how I attempted to solve this issue:

I set up two Buy commands, two Sell commands and two Close Buy commands and two Close Sell commands, namely OPBUY, OPBUY1, OPSELL, OPSELL1, CloseBuy, CloseBuy1, CloseSell, CloseSell1 in the codes I provide below as well as two different order magic numbers, which are MagicNumber(123) and Magic(321) in the codes. Among them, OPBUY, CloseBuy, OPSELL,CloseSell use MagicNumber whilst OPBUY1, CloseBuy1, OPSELL1,CloseSell1 use Magic.

However, it didn't work out the way I intended. It works fine when I use OPBUY, CloseBuy, OPSELL,CloseSell and MagicNumber for strategy 1, the EA can also open orders by OPBUY1, OPSELL1 and Magic for strategy 2, but it just won't trigger CloseBuy1 and CloseSell1 with Magic which puzzles the hell out of me and leave the open orders of strategy 2 hanging. 

Is there anything that I did wrong here? What should I do to circumvent or solve this issue and make my two strategies compatible with each other in one EA? 

Below is my codes, and thanks for your help!

//+------------------------------------------------------------------+
//|                                                         H4-1.mq4 |
//|                                                          Bernard |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Bernard"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
input bool   OpenBUY=True;
input bool   OpenSELL=True;
input bool   CloseBySignal=True;

input double StopLoss=0;

input double TakeProfit=0;
input double TrailingStop=0;

input int    RSIperiod=14;
input double BuyLevel=30;
input double SellLevel=70;
input bool   AutoLot=True;
input double Risk=1;
input double ManualLots=0.1;
input int    MagicNumber=123;
input int    Magic=321; 
input string Koment="RSIea";
input int    Slippage=10;


//---- buffers


//---
int OrderBuy,OrderSell;
int ticket;
int LotDigits;
int BarsCount = 0;
double Trail,iTrailingStop;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int deinit()
  {
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
 {
   double stoplevel=MarketInfo(Symbol(),MODE_STOPLEVEL);
   OrderBuy=0;
   OrderSell=0;
   for(int cnt=0; cnt<OrdersTotal(); cnt++)
     {
      if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber && OrderComment()==Koment)
           {
            if(OrderType()==OP_BUY) OrderBuy++;
            if(OrderType()==OP_SELL) OrderSell++;
            if(TrailingStop>0)
              {
               iTrailingStop=TrailingStop;
               if(TrailingStop<stoplevel) iTrailingStop=stoplevel;
               Trail=iTrailingStop*Point;
               double tsbuy=NormalizeDouble(Bid-Trail,Digits);
               double tssell=NormalizeDouble(Ask+Trail,Digits);
               if(OrderType()==OP_BUY && Bid-OrderOpenPrice()>Trail*Point && Bid-OrderStopLoss()>Trail*Point)
                 {
                  ticket=OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+Trail*Point,OrderTakeProfit(),0,Blue);
                 }
               if(OrderType()==OP_SELL && OrderOpenPrice()-Ask>Trail*Point && (OrderStopLoss()-Ask>Trail*Point || OrderStopLoss()==0))
                 {
                  ticket=OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-Trail*Point,OrderTakeProfit(),0,Blue);
                 }
              }
           }
     }


//--- open position
if(Bars > BarsCount)
   {
   if((OpenBUY && OrderBuy<1 && ##Insert Buying Strategy 1 here##))OPBUY(); 
   if((OpenBUY && OrderBuy<1 && ##Insert Buying Strategy 2 here## ) )OPBUY1();    

   if(OpenSELL && OrderSell<1 && ##Insert Selling Strategy 1 here##)OPSELL(); 
   if((OpenSELL && OrderSell<1 && ##Insert Selling Strategy 2 here## ))OPSELL1();     
   BarsCount = Bars;
   }

//--- close position by signal      
   if(CloseBySignal)
     {
      if(OrderSell>0 && ##Close orders opened by Selling strategy 1##) CloseSell();

      if(OrderSell>0 && ##Close orders opened by Selling strategy 2## ) CloseSell1();   
    
      if(OrderBuy>0  &&( ##Close orders opened by Buying strategy 1##
      ))CloseBuy();
      
      if(OrderBuy>0  &&( ##Close orders opened by Buying strategy 2##
      ))CloseBuy1();      
     }
//---
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OPBUY()
  {
   double StopLossLevel;
   double TakeProfitLevel;
   if(BUYStopLoss>0) StopLossLevel=Bid-StopLoss*Point; else StopLossLevel=0.0;
   if(TakeProfit>0) TakeProfitLevel=Ask+TakeProfit*Point; else TakeProfitLevel=0.0;

   ticket=OrderSend(Symbol(),OP_BUY,LOT(),Ask,Slippage,StopLossLevel,TakeProfitLevel,Koment,MagicNumber,0,DodgerBlue);
  }
  
void OPBUY1()
  {
   double StopLossLevel;
   double TakeProfitLevel;
   if(BUYStopLoss>0) StopLossLevel=Bid-StopLoss*Point; else StopLossLevel=0.0;
   if(TakeProfit>0) TakeProfitLevel=Ask+TakeProfit*Point; else TakeProfitLevel=0.0;

   ticket=OrderSend(Symbol(),OP_BUY,LOT(),Ask,Slippage,StopLossLevel,TakeProfitLevel,Koment,Magic,0,clrYellow);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OPSELL()
  {
   double StopLossLevel;
   double TakeProfitLevel;
   if(SELLStopLoss>0) StopLossLevel=Ask+StopLoss*Point; else StopLossLevel=0.0;
   if(TakeProfit>0) TakeProfitLevel=Bid-TakeProfit*Point; else TakeProfitLevel=0.0;
//---
   ticket=OrderSend(Symbol(),OP_SELL,LOT(),Bid,Slippage,StopLossLevel,TakeProfitLevel,Koment,MagicNumber,0,DeepPink);
  }
  
void OPSELL1()
  {
   double StopLossLevel;
   double TakeProfitLevel;
   if(SELLStopLoss>0) SELLStopLossLevel=Ask+StopLoss*Point; else StopLossLevel=0.0;
   if(TakeProfit>0) TakeProfitLevel=Bid-TakeProfit*Point; else TakeProfitLevel=0.0;
//---
   ticket=OrderSend(Symbol(),OP_SELL,LOT(),Bid,Slippage,StopLossLevel,TakeProfitLevel,Koment,Magic,0,clrYellow);
  }
//+------------------------------------------------------------------+
                                                               
//+------------------------------------------------------------------+
void CloseSell()
  {
   int  total=OrdersTotal();
   for(int y=OrdersTotal()-1; y>=0; y--)
     {
      if(OrderSelect(y,SELECT_BY_POS,MODE_TRADES))
         if(OrderSymbol()==Symbol() && OrderType()==OP_SELL && OrderMagicNumber()==MagicNumber)
           {
            ticket=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),5,Black);
           }
     }
  }

//+------------------------------------------------------------------+

void CloseSell1()
  {
   int  total=OrdersTotal();
   for(int y=OrdersTotal()-1; y>=0; y--)
     {
      if(OrderSelect(y,SELECT_BY_POS,MODE_TRADES))
         if(OrderSymbol()==Symbol() && OrderType()==OP_SELL && OrderMagicNumber()==Magic)
           {
            ticket=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),5,Black);
           }
     }
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CloseBuy()
  {
   int  total=OrdersTotal();
   for(int y=OrdersTotal()-1; y>=0; y--)
     {
      if(OrderSelect(y,SELECT_BY_POS,MODE_TRADES))
         if(OrderSymbol()==Symbol() && OrderType()==OP_BUY && OrderMagicNumber()==MagicNumber)
           {
            ticket=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),5,Black);
           }
     }
  }
//+------------------------------------------------------------------+
void CloseBuy1()
  {
   int  total=OrdersTotal();
   for(int y=OrdersTotal()-1; y>=0; y--)
     {
      if(OrderSelect(y,SELECT_BY_POS,MODE_TRADES))
         if(OrderSymbol()==Symbol() && OrderType()==OP_BUY && OrderMagicNumber()==Magic)
           {
            ticket=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),5,Black);
           }
     }
  }
                                                     
//+------------------------------------------------------------------+
double LOT()
  {
   double lotsi;
   double ilot_max =MarketInfo(Symbol(),MODE_MAXLOT);
   double ilot_min =MarketInfo(Symbol(),MODE_MINLOT);
   double tick=MarketInfo(Symbol(),MODE_TICKVALUE);
//---
   double  myAccount=AccountBalance();
//---
   if(ilot_min==0.01) LotDigits=2;
   if(ilot_min==0.1) LotDigits=1;
   if(ilot_min==1) LotDigits=0;
//---
   if(AutoLot)
     {
      lotsi=NormalizeDouble((myAccount*Risk)/10000,LotDigits);
        } else { lotsi=ManualLots;
     }
//---
   if(lotsi>=ilot_max) { lotsi=ilot_max; }
//---
   return(lotsi);
  }
//+------------------------------------------------------------------+
  
Documentation on MQL5: Trade Functions / OrderSelect
Documentation on MQL5: Trade Functions / OrderSelect
  • www.mql5.com
Selects an order to work with. Returns true if the function has been successfully completed. Returns false if the function completion has failed. For more information about an error call GetLastError(). Do not confuse current pending orders with positions, which are also displayed on the "Trade" tab of the "Toolbox" of the client terminal...
 
You are counting OrderBuy and OrderSell only by one MagicNumber , make separate parameters for the 2 strategies like OrderBuy , OrderSell , and OrderBuy1 , OrderSell1 , counting each by its magic number .
 
Catalin Zachiu:
You are counting OrderBuy and OrderSell only by one MagicNumber , make separate parameters for the 2 strategies like OrderBuy , OrderSell , and OrderBuy1 , OrderSell1 , counting each by its magic number .

Thanks, I kinda get your idea, but I am not quite sure how I should modify it in my codes. Could you elaborate a little? Thanks!

 
Catalin Zachiu:
You are counting OrderBuy and OrderSell only by one MagicNumber , make separate parameters for the 2 strategies like OrderBuy , OrderSell , and OrderBuy1 , OrderSell1 , counting each by its magic number .

Hi, I added OrderBuy1 and OrderSell1 as you suggested and changed the codes to 

if(Bars > BarsCount)
   {
   if((OpenBUY && OrderBuy<1 && ##Insert Buying Strategy 1 here##))OPBUY(); 
   if((OpenBUY && OrderBuy1<1 && ##Insert Buying Strategy 2 here## ) )OPBUY1();    

   if(OpenSELL && OrderSell<1 && ##Insert Selling Strategy 1 here##)OPSELL(); 
   if((OpenSELL && OrderSell1<1 && ##Insert Selling Strategy 2 here## ))OPSELL1();     
   BarsCount = Bars;
   }

//--- close position by signal      
   if(CloseBySignal)
     {
      if(OrderSell>0 && ##Close orders opened by Selling strategy 1##) CloseSell();

      if(OrderSell1>0 && ##Close orders opened by Selling strategy 2## ) CloseSell1();   
    
      if(OrderBuy>0  &&( ##Close orders opened by Buying strategy 1##
      ))CloseBuy();
      
      if(OrderBuy1>0  &&( ##Close orders opened by Buying strategy 2##
      ))CloseBuy1();      
     }

I am not sure if this is how you meant it, but the issue persists and that I still can't get it to close my open orders for strategy 2. What am I missing here? Should I modify the codes in CloseBuy1 and CloseSell1? Thanks

 

@William Roeder  @Keith Watford

Sorry to bother you, I know you guys are experts, this should be a piece of cake for you, so I took the liberty to ask you guys, I wonder if you can help me, much appreciated! Apologies if you feel disturbed.

William Roeder
William Roeder
  • www.mql5.com
Trader's profile
Reason: