How to know if all open positions are same?

 
I would really appreciate if one could advice how to write a code that executes, if "All Open trades are OP_BUY" or if "All Open trades are OP_SELL", then no OrderSend.
 
  1. MT5 doesn't have a OP_BUY.

    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?
    Next time post in the correct place. The moderators will likely move this thread there soon.

  2. Write a function that counts the number of orders of a type. Get the buy count, the sell count.
    bool isOkToSend =  bc * sc != 0  // Both counts non-zero = not all one type.
                    || bc + sc == 0; // Both counts zero = no open orders.
    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.04.21

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

 
William Roeder:
  1. MT5 doesn't have a OP_BUY.

    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?
    Next time post in the correct place. The moderators will likely move this thread there soon.

  2. Write a function that counts the number of orders of a type. Get the buy count, the sell count. 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.04.21

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

Dear William,

I understand your anger man. 

I am a very newbie and started coding just 2 weeks back. I don't even know what to ask. I tried some attempts and everything seemed very irrelevant.
About paying a freelancer, I am not trying to build a code and become reach. I am trying to learn, paying someone else to code for me actually doesn't help. 
And thanks a lot for your help even though you're mad, I think I understood how you explained. the code seems working.

And finally why MQL5? I am a newbie.
 

 
  1. No anger here.
  2. Post in the correct place.
 
MD Hafijul:
I would really appreciate if one could advice how to write a code that executes, if "All Open trades are OP_BUY" or if "All Open trades are OP_SELL", then no OrderSend.

Hello and welcome .

Its way more extremely easier than it sounds . 

A.Getting the total amount of open orders (we subtract one because we will loop from the amount to zero ,aka dive into the orders) 

int ot=OrdersTotal()-1;

B.To work with an order we need to tell the terminal to select it , selecting it means it fills up all fields with the information for that order.(this will make more sense once you understand there are certain things that are done they way they are because of the structure of the application)

bool was_selected=OrderSelect(...);
if(was_selected)
{
//self explanatory 
}

C.Each order has an ID and a position identifier in the orders stack . Imagine a line in Starbucks of people waiting to buy coffee . Each person has a number that identifies them in that line (the first ,the second etc) but if you ask the fifth person to tell you their name they wont say "Fifth person" but "Mary" .That means the position in the line changes ,but the name does not.
The id that rarely changes is called the Ticket .
The position in the orders stack (coffee line) is called , position
So , if i want to select the first order on the order stack i send the request :

bool was_selected=OrderSelect(0,SELECT_BY_POS,MODE_TRADES);

 We are specifying MODE_TRADES because we are interested in trades that are still open.We dont need to specify MODE_TRADES if we were to select by ticket because we would want that position no matter where it was .(Ex : if you selected Mary ,you would get Mary regardless of where she was whereas selecting the first person in the coffee line needs coffee line to be specified)

D.Each order ,after its selected ,provides us with millions and millions of info pieces (15 to be exact).
You only need 3

OrderSymbol()
OrderType()
OrderMagicNumber()

So you will loop into all open orders and look for :

Trades of the symbol you specified
with the magic number you specified
with the type being OP_BUY or OP_SELL

These will represent the group of orders you want to check for similarity .

The Function that performs this check : 

//custom data structure 
struct trilean
{
bool HasBuys,HasSells,HasAnyTrades;
//constructor or reset state upon creation of your data structure
//this means if you create a trilean it will reset its "members"
trilean(void){HasBuys=false;HasSells=false;HasAnyTrades=false;}
/*
ex: you create a boolean by typing bool something;
    you create a trilean by typing trilean something;
    and its default values for something.HasBuys ,and the other 2 members will be reset to false
*/
};
//We can use the new structure on a function ,as the data type our function returns
//Data type the function returns is trilean
  trilean CheckOrderTypes(bool by_symbol,string which_symbol,
                          bool by_magic,int which_magic){
  //the function returns a trilean ,so we assign a result variable to it
  trilean result;//no need to reset ,its done in the trilean constructor 
  //orders total -1 
  int ot=OrdersTotal()-1;
  //loop into orders
    for(int o=ot;o>=0;o--)
    {
    //select the order by its position
      bool was_selected=OrderSelect(o,SELECT_BY_POS,MODE_TRADES);
      if(was_selected)
      {
      //if it matches the symbol we specified or we dont filter by symbols 
        if(which_symbol==OrderSymbol()||!by_symbol)
        {
        //if it matches the magic number we specified or we dont filter by magic number 
          if(which_magic==OrderMagicNumber()||!by_magic)
          {
          //if its a buy
            if(OrderType()==OP_BUY)
            {
            //set any trades to true in our result variable 
              result.HasAnyTrades=true;
            //set buy trades to true in our result variable 
              result.HasBuys=true;
            }
          //if its a buy ends here 
          //if its a sell
            if(OrderType()==OP_SELL)
            {
            //set any trades to true in our result variable 
              result.HasAnyTrades=true;
            //set buy trades to true in our result variable 
              result.HasSells=true;
            }
          //if its a sell ends here 
          }
        //if it matches the magic number we specified or we dont filter by magic number ends here 
        }
      //if it matches the symbol we specified or we dont filter by symbols ends here 
      }
    //select the order by its position ends here 
    }
  //loop into ordres ends here

  //return result
  return(result);
  }

 Now , when you want to check if all trades of EURUSD for instance are buys you run this check : 

 //set a boolean that controls if trades are allowed for this type of filter 
   bool TypeFilterAllowsTrades=true;
   //run the check we just built 
   trilean check=CheckOrderTypes(true,"EURUSD",false,0);
   //reasons to disable trade switch above
   //all are buys ,and trades exist for the parameters we specified (EURUSD) 
     // EURUSD HAS TRADES  but EURUSD HAS NO SELLS 
     if(check.HasAnyTrades==true && check.HasSells==false)
     {
     //disable trading allowance for this filter 
       TypeFilterAllowsTrades=false;
     }
     // EURUSD HAS TRADES  but EURUSD HAS NO BUYS 
     if(check.HasAnyTrades==true && check.HasBuys==false)
     {
     //disable trading allowance for this filter 
       TypeFilterAllowsTrades=false;     
     }

 Enjoy 

 
Lorentzos Roussos:

Hello and welcome .

Its way more extremely easier than it sounds . 

A.Getting the total amount of open orders (we subtract one because we will loop from the amount to zero ,aka dive into the orders) 

B.To work with an order we need to tell the terminal to select it , selecting it means it fills up all fields with the information for that order.(this will make more sense once you understand there are certain things that are done they way they are because of the structure of the application)

C.Each order has an ID and a position identifier in the orders stack . Imagine a line in Starbucks of people waiting to buy coffee . Each person has a number that identifies them in that line (the first ,the second etc) but if you ask the fifth person to tell you their name they wont say "Fifth person" but "Mary" .That means the position in the line changes ,but the name does not.
The id that rarely changes is called the Ticket .
The position in the orders stack (coffee line) is called , position
So , if i want to select the first order on the order stack i send the request :

 We are specifying MODE_TRADES because we are interested in trades that are still open.We dont need to specify MODE_TRADES if we were to select by ticket because we would want that position no matter where it was .(Ex : if you selected Mary ,you would get Mary regardless of where she was whereas selecting the first person in the coffee line needs coffee line to be specified)

D.Each order ,after its selected ,provides us with millions and millions of info pieces (15 to be exact).
You only need 3

So you will loop into all open orders and look for :

Trades of the symbol you specified
with the magic number you specified
with the type being OP_BUY or OP_SELL

These will represent the group of orders you want to check for similarity .

The Function that performs this check : 

 Now , when you want to check if all trades of EURUSD for instance are buys you run this check : 

 Enjoy 

Hey man! thank you soo much for taking your time. and such a generous long explanation. I really appreciate it. 🙏🙏🙏

Reason: