Close all open positions and orders if stoploss is hit on one position

 

Hello Team -

I am new to MT5 and i have been trying to develop an EA. I opened an initial trade and pending orders for 3 additional trades. 

Using the order delete function deletes the pending orders when i run the EA before the market order even opens. I will appreciate if i can get help here on how to cancel the pending orders once the initial trade hits breakeven or stoploss. 

Please see my code below:

if(PositionsTotal() ==0 && RSIValue < 20) 

      {

         trade.Buy(0.1,NULL,Ask,(Ask - 300*_Point),Ask + 1500 * _Point)

         trade.BuyStop(1,Ask + 400*_Point, NULL, Ask + 200 * _Point,Ask + 1500 * _Point, 0);

         trade.BuyStop(1,Ask + 800*_Point, NULL, Ask + 600 * _Point,Ask + 1500 * _Point, 0);

         trade.BuyStop(1,Ask + 1200*_Point, NULL, Ask + 1000 * _Point,Ask + 1500 * _Point, 0);         

      }

i want to be able to delete the pending orders only when one of the trades closes before the other in that sequence.

Appreciate your attention.

 
  1. Help you with what? You haven't stated a problem, you stated a want. Show us your attempt (using the CODE button) and state the nature of your problem.
              No free help (2017)

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

    We're not going to code it for you (although it could happen if you are lucky or the problem is interesting).
              No free help (2017)


  2. There is no need to create pending orders in code.
    1. The pending has the slight advantage, A) you are closer to the top of the queue (filled quicker), B) there's no round trip network delay (filled quicker.)
    2. Don't worry about it unless you're scalping M1 or trading news.
    3. Humans can't watch the screen 24/7, so they use pending orders; EAs can, so no need for pending orders, have it wait until the market reaches the trigger price and just open an order.
 
William Roeder #:

Help you with what? You haven't stated a problem, you stated a want. Show us your attempt (using the CODE button) and state the nature of your problem.
          No free help (2017)

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

We're not going to code it for you (although it could happen if you are lucky or the problem is interesting).
          No free help (2017)

Thanks a lot for the response William. I hope i get lucky though. Please see my code below. I need help with a modification of the code to cancel all the orders when any of the open positions hits hits stop loss.
#include <Trade\Trade.mqh>
#define MA_MAGIC 123
CTrade trade;


int OnInit()
  {
   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {
   
  }
void OnTick()
  {
   double Ask = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits); 
   double Bid = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits)
   double RSIArray[];
   
   ArraySetAsSeries(RSIArray, true);
   
   int RSIDefinition = iRSI(_Symbol,_Period,14,PRICE_CLOSE);
   CopyBuffer(RSIDefinition,0,0,3,RSIArray)
   double RSIValue = NormalizeDouble(RSIArray[0],20);
   int positions = 0;
   
   if(PositionsTotal() ==0 && RSIValue < 20) 
      {
         trade.Buy(0.1,NULL,Ask,(Ask - 300*_Point),Ask + 1500 * _Point); 
         trade.BuyStop(1,Ask + 400*_Point, NULL, Ask + 200 * _Point,Ask + 1500 * _Point, 0);
         trade.BuyStop(1,Ask + 800*_Point, NULL, Ask + 600 * _Point,Ask + 1500 * _Point, 0);
         trade.BuyStop(1,Ask + 1200*_Point, NULL, Ask + 1000 * _Point,Ask + 1500 * _Point, 0);
               
         if (PositionsTotal() ==0) Cancelpendingorders();
      }
 
Tee Salem # :
Thanks a lot for the response William. I hope i get lucky though. Please see my code below. I need help with a modification of the code to cancel all the orders when any of the open positions hits hits stop loss.

The biggest mistake is that you create an indicator handle at each tick:

void OnTick()
  {
***
   int RSIDefinition = iRSI(_Symbol,_Period,14,PRICE_CLOSE);
***

The MQL5 style implies that the indicator handle must be created ONCE and must be done in OnInit !!!

Reason: