Closing orders for specific symbols

 

I have started an EA that will place either BUY or SELL orders based on certain criteria. This EA will run on multiple charts at the same time and will place orders in many different symbols. When a certain condition appears (say price closing below the 11 EMA), I want to close all BUY orders for that symbol. If the same condition appears on other charts, I want them to close all BUY orders for their respective symbols also. When a different condition occurs (say closing above the 11 EMA), I want to close all SELL orders for that symbol. If the same condition appears on other charts, I want them to close all SELL orders for their respective symbols also.

I have read many posts on this subject and must confess to being more confused than when I started. Therefore, I have decided to ask for help without offering any code that I have been working on so far as, given a free rein, your solution will no doubt be more elegant. Can anyone help me get started with this?

Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Trade Orders in DOM
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Trade Orders in DOM
  • www.mql5.com
For equity securities, the Depth of Market window is available, where you can see the current Buy and Sell orders. Desired direction of a trade operation, required amount and requested price are specified for each order. To obtain information...
 

There are two ways to do it:

  1. way: to build a mechanism for closing positions into an existing MQL5 Expert Advisor
  2. way: write a specialized MQL5 Expert Advisor - it will be responsible for closing positions
Which way will you choose?
 
Andy Galloway:

… I want … I want … I want … I want …

I have decided to ask for help without offering any code …

Can anyone help me get started with this?

  1. Help you with what? You haven't stated a problem, you stated a want.
         How To Ask Questions The Smart Way. 2004
              Prune pointless queries.

    You have only four choices:
    1. Search for it. Do you expect us to do your research for you?

    2. Beg 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.

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

    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.04.21

  2. Do you really expect an answer? There are no mind readers here and our crystal balls are cracked.

    Always post all relevant code.
         How To Ask Questions The Smart Way. 2004
              Be precise and informative about your problem

    We can't see your broken code.

    Fix your broken code.

 

The reason I thought it best to ask for help before coding anything is because I am totally confused. Here is the code I have written to place the orders when the price closes above the upper BB or below the lower BB. 

The next thin I want to do is check if the last candle closed below the upper BB if I have open BUY orders and check if the last candle closed above the lower BB if I have open SELL orders. I am so confused that I don't even know where to start.

// Inputs
input double varTradeLots = 0.01;          //Size of trades
input int varBbPeriod = 20;                //Bollinger Band period
input int varBbBandsShift = 0;             //Bollinger Band shift 
input int varBbDeviation = 2;              //Bollinger Band deviation
input int varBbAppliedPrice = PRICE_CLOSE; //Bollinger Band applied price
input bool varTradeAlert = true;           //Alert after trade has been placed
input bool varTradeTest = false;           //Message instead of placing trade

string timeFrame;
string tradeSide;
double tradeId;

datetime lastBarCheck;
datetime checkTime;
double uband;
double lband;

/*
   +------------------------------------------------------------------+
   | Expert initialization function                                   |
   +------------------------------------------------------------------+
*/
int OnInit()
   {
//---
   
//---
   return(INIT_SUCCEEDED);
   }
/*
   +------------------------------------------------------------------+
   | Expert deinitialization function                                 |
   +------------------------------------------------------------------+
*/
void OnDeinit(const int reason)
   {
//---
   
   }
/*
   +------------------------------------------------------------------+
   | Expert tick function                                             |
   +------------------------------------------------------------------+
*/
void OnTick()
   {

/*
   +--------------------------------------------------------------------+
   | Run only once per bar                                              |
   +--------------------------------------------------------------------+
*/

//      MessageBox ("The time[0] is " + Time[0]);
   if (lastBarCheck != Time[0])
      {
      getBandValues(Close[1]);
      tradeSide = "No";
      
      if (Close[1] > uband)
         {
         tradeSide = "BUY";
         }
      else if (Close[1] < lband)
         {
         tradeSide = "SELL";
         }
      else
         {
         Alert("NO TRADE");
         }
      placeOrder(tradeSide);
      
      lastBarCheck = Time[0];
      }
   };
   
int getBandValues (double close1)
      {
      uband=iBands(NULL,0,varBbPeriod,varBbDeviation,varBbBandsShift,varBbAppliedPrice,MODE_UPPER,0);
      lband=iBands(NULL,0,varBbPeriod,varBbDeviation,varBbBandsShift,varBbAppliedPrice,MODE_LOWER,0);
      return(0);
      };
   
int placeOrder(string tradeDirection)
   {
   if (tradeDirection != "No")
      {
      if (tradeDirection == "BUY")
         {
         if (varTradeTest == true)
            {
            Alert("");
            Alert("Symbol = ", Symbol(), " ", timeFrame, " | Direction = ", tradeDirection, " | Volume = ", varTradeLots, " lots");
            Alert("TRADE ALERT:");
            }
         else
            {
            tradeId = OrderSend(Symbol(),OP_BUY,varTradeLots,Ask,0,0,0);
            if (varTradeAlert == true)
               {
               Alert("");
               Alert("Symbol = ", Symbol(), " ", timeFrame, " | Direction = ", tradeDirection, " | Volume = ", varTradeLots, " lots");
               Alert("TRADE PLACED:");
               }
            }
         }
      else if (tradeDirection == "SELL")
         {
         if (varTradeTest == true)
            {
            Alert("");
            Alert("Symbol = ", Symbol(), " ", timeFrame, " | Direction = ", tradeDirection, " | Volume = ", varTradeLots, " lots");
            Alert("TRADE ALERT:");
            }
         else
            {
            tradeId = OrderSend(Symbol(),OP_SELL,varTradeLots,Bid,0,0,0);
            if (varTradeAlert == true)
               {
               Alert("");
               Alert("Symbol = ", Symbol(), " ", timeFrame, " | Direction = ", tradeDirection, " | Volume = ", varTradeLots, " lots");
               Alert("TRADE PLACED:");
               }
            }
         }
      }
   return (0);
   }

//  +------------------------------------------------------------------+
//  | Tester function                                                  |
//  +------------------------------------------------------------------+
double OnTester()
   {
//---
   double ret=0.0;
//---

//---
   return(ret);
   }
//  +------------------------------------------------------------------+
//  | ChartEvent function                                              |
//  +------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
   {
//---
   
   }
//  +------------------------------------------------------------------+
 

Study the attached file source code , it points out some things .

Enjoy [_]p

Files: