How to Mandatory Close if Breakeven Price Level (BEPx) is Met ? (MT4)

 

Hi coders, my EA has 4 arrays in the Exit function :-

string bSym[];
double bSymBEPx[];

string sSym[];
double sSymBEPx[];

My mission is to close a bundle of open trades (BUY and/or SELL) for a symbol if emergency conditions are met.

If a symbol with only 1 single open trade, the exit can close nicely if the BEPx Level's is met. However, if the symbol (BUY and/or SELL) has > 0 open trades, the EA cannot always bundle close the symbol's open trades instantly and completely because the moment EA closes 1 open trade, the BEPx level will change immediately, resulting always remains most of the bundle open orders yet open.

I need your codes so that I can implement the "Mandatory Bundle Close" (but not the Basket Close for the whole account) for the relevant symbol with multiple open trades.

Exit Emergency Conditions (To Close BUY OpOrd #)

if(Conditions && Symbol()==bSym && BID >= bSymBEPx)

Exit Emergency Conditions (To Close SELL OpOrd #)

if(Conditions && Symbol()==sSym && ASK <= sSymBEPx)
** Kindly have a look at my attached picture for better understanding, thanks.
MQL5.community - User Memo
MQL5.community - User Memo
  • www.mql5.com
You have just registered and most likely you have questions such as, "How do I insert a picture to my a message?" "How do I format my MQL5 source code?" "Where are my personal messages kept?" You may have many other questions. In this article, we have prepared some hands-on tips that will help you get accustomed in MQL5.community and take full advantage of its available features.
Files:
 
  1. Why did you post your MT4 question in the MT5 General section instead of the MQL4 section, (bottom of the Root page)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum? (2017)
    Next time, post in the correct place. The moderators will likely move this thread there soon.

  2. Chin Min Wan: I need your codes

    You have only four choices:

    1. Search for it (CodeBase or Market). Do you expect us to do your research for you?

    2. Try asking at:

    3. MT4: Learn to code it.
      MT5: Begin learning to code it.

      If you don't learn MQL4/5, there is no common language for us to communicate. If we tell you what you need, you can't code it. If we give you the code, you don't know how to integrate it into your code.
                I need HEEEELP, please, it's URGENT...really ! - General - MQL5 programming forum (2017)

    4. Or pay (Freelance) someone to code it. Top of every page is the link Freelance.
                Hiring to write script - General - MQL5 programming forum (2019)

    We're not going to code it for you (although it could happen if you are lucky or the problem is interesting.) We are willing to help you when you post your attempt (using CODE button) and state the nature of your problem.
              No free help (2017)

 

Hi

I don’t know if I understand you correctly but it is a simple solution to this, if you want to close only some trades from one symbol, you just need to code a loop for closing and choose only trades from correct symbol to close, like that:

void closeOrders(string symbol) 
{
   bool close;
   for(int i=OrdersTotal()-1; i>=0; i--) 
   {
      if(OrderSelect(i,SELECT_BY_POS)) 
      {
            if(OrderSymbol()==symbol) 
            {
                  RefreshRates();
                  if(OrderType()==OP_BUY) 
                  {
                     close = OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Bid,Digits),100);
                  }
                  if(OrderType()==OP_BUYSTOP) 
                  {
                     close = OrderDelete(OrderTicket());
                  }
                  if(OrderType()==OP_BUYLIMIT) 
                  {
                     close = OrderDelete(OrderTicket());
                  }
                  if(OrderType()==OP_SELL) 
                  { 
                     close = OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Ask,Digits),100);
                  }
                  if(OrderType()==OP_SELLSTOP) 
                  {
                     close = OrderDelete(OrderTicket());
                  }
                  if(OrderType()==OP_SELLLIMIT) 
                  {
                     close = OrderDelete(OrderTicket());
                  }
         }
      }
   }
}

So in one “tick” you can close all of them – you don’t need to repeat this on new signal every time.  

Best Regards

 
Marzena Maria Szmit #:

Hi

I don’t know if I understand you correctly but it is a simple solution to this, if you want to close only some trades from one symbol, you just need to code a loop for closing and choose only trades from correct symbol to close, like that:

So in one “tick” you can close all of them – you don’t need to repeat this on new signal every time.  

Best Regards

Thanks Marzena. No, instead, I want to close "ALL" the open trades of the same type for a symbol if the Breakeven Price (BEPx) level is achieved. The problems that I'm facing now is if the symbol (BUY and/or SELL) has > 0 open trades, the EA cannot always bundle close the symbol’s open trades concurrently and completely because the moment EA closes the first open trade, the BEPx level will change immediately, always resulting most of the bundle open trades are yet open.

1) Kindly look at my picture, the CADJPY has both 5 BUY and 2 SELL open orders. If there is an exit signal (BID >= bSymBEPx) for BUY, the codes should only close all the 5 BUY open trades simultaneously and should not touch the 2 SELL open trades.

2) My EA's exit function based on OrderSelect(i,SELECT_BY_POS,MODE_TRADES) to provide symbols with open trades. In the exit function, it uses "symbol" to represent all the symbols with open trades. If my EA is running 30 symbols, 5 symbols with open trades and total number of open trades is 18 (including both BUY and SELL open trades), if I write the following codes :-

   Print(symbol+" High Price "+DoubleToStr(iHigh(symbol,60,1),5)+" Low Price "+DoubleToStr(iLow(symbol,60,1),5));
there would be 18 different sets of output (5 symbols) to be printed out.


For your codes and from my understanding, I find that it does not deal with immediate changes of BEPx level after one open order is closed, I always face my EA does not close completely of the same type for a symbol, always remain open. I need your codes so that I can implement the “Mandatory Bundle Close” (but not the Basket Close for the whole account) for the relevant symbol with multiple open trades, please help me the correct coding way to bundle close completely. Thanks.


FYI, my EA only have OP_BUY and OP_SELL, and do not have STOP and LIMIT orders.


 
Marzena Maria Szmit #:

Hi

I don’t know if I understand you correctly but it is a simple solution to this, if you want to close only some trades from one symbol, you just need to code a loop for closing and choose only trades from correct symbol to close, like that:

So in one “tick” you can close all of them – you don’t need to repeat this on new signal every time.  

Best Regards

Let me give you a scenario for my demo test account : USDCHF

When the ASK price is <= 0.91156 (sBEPx level), most likely the EA will only close the Order4 first, at the moment Order4 is closed by my EA, the sBEPx level will be changed and move up/down immediately (depending on the lotsize), resulting the Order3, Order2 and Order1 left opened.



 

Hi

I might not understand it properly - but  I assumed that when you get the signal from BEP  - so when you detect that ASK price is <= 0.91156 (sBEPx level) - then you  can simply activate the function i send you only for Sell trades. (You can add the order type as parameter and
you can also remove "if" statements for pending orders from the function if you don't use them). It might look like this: 

void closeOrders(string symbol , ENUM_ORDER_TYPE type) 
{
   bool close;
   for(int i=OrdersTotal()-1; i>=0; i--) 
   {
      if(OrderSelect(i,SELECT_BY_POS)) 
      {
            if(OrderSymbol()==symbol && OrderType()==type) 
            {
                  RefreshRates();
                  if(OrderType()==OP_BUY) 
                  {
                     close = OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Bid,Digits),100);
                  }
                  else 
                  if(OrderType()==OP_SELL) 
                  { 
                     close = OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Ask,Digits),100);
                  }
                  
         }
      }
   }
}
Simply the algorithm would be like this:
1. Check if there are any Sell trades
2. Calculate BEPx level for Sells
3. Check if Ask price is below BEPx for Sells
4. If 3 is true - run closeOrders(symbol, OP_SELL) --> so you will close at this moment all sell trades only.
That's all - you don't need to check BEP again after closing single trades - just check BEP and close all at this detected signal.

Best Regards
 
Marzena Maria Szmit #:

Hi

I might not understand it properly - but  I assumed that when you get the signal from BEP  - so when you detect that ASK price is <= 0.91156 (sBEPx level) - then you  can simply activate the function i send you only for Sell trades. (You can add the order type as parameter and
you can also remove "if" statements for pending orders from the function if you don't use them). It might look like this: 

Simply the algorithm would be like this:
1. Check if there are any Sell trades
2. Calculate BEPx level for Sells
3. Check if Ask price is below BEPx for Sells
4. If 3 is true - run closeOrders(symbol, OP_SELL) --> so you will close at this moment all sell trades only.
That's all - you don't need to check BEP again after closing single trades - just check BEP and close all at this detected signal.

Best Regards
Thanks so much Marzena for your input, let me think further about it as I'm learning coding now.
Reason: