EA opens Many Trades, How to Open 1 buy & 1 sell?

 

Hi there all

If you can please help, i would like to open 1 buy & 1 sell. For some reason my EA opens many trades, I know there's a way to open 1 buy & 1 sell. By using "if (OrdersTotal()<1)" i don't want to use that because i want to use the same code on a different pair. If there's way please assist & I know I'm doing something wrong tried change it no luck. :(

Thank You

extern double StopLoss=100;
extern int MagicNumber=900;
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
int Count=0;

int CountOrder()
 {
  int b=1;
  for(int Loop3=0;Loop3<b;Loop3++)
  {
   if(OrderSymbol()==Symbol()==OrderMagicNumber()==MagicNumber)
   Count++;
  }
  return(Count);
 }

void OpenOrders()
 {
  
   int Loop=-1;
   if(OrderSelect(Loop,SELECT_BY_POS,MODE_TRADES))
   if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
   
   double StopLossAsk,StopLossBid;
   
   StopLossAsk=Ask-StopLoss*Point;
   StopLossBid=Bid+StopLoss*Point;
   
    int Buy=OrderSend(Symbol(),OP_BUY,0.1,Ask,0,0,0,"Test",0,0,clrOrange);
    if(Buy>0)
    {
     bool ModBuy=OrderModify(Buy,OrderOpenPrice(),OrderOpenPrice()+StopLossAsk,0,0,clrNONE);
    }

    int Sell=OrderSend(Symbol(),OP_SELL,0.1,Bid,0,0,0,"Test",0,0,clrPowderBlue);
    if(Sell>0)
    {
     bool ModSell=OrderModify(Sell,OrderOpenPrice(),OrderOpenPrice()+StopLossBid,0,0,clrNONE);
    }

   RefreshRates();
   
 }
//+------------------------------------------------------------------+
int CloseProfit()
 {
  double Profit=1;
  for(int a=OrdersTotal()-1;a>=0;a--)
  {
   if(OrderSelect(a,SELECT_BY_POS,MODE_TRADES))
   if(OrderSymbol()==Symbol()==OrderMagicNumber()==MagicNumber)
   bool CloseOrder=false;
   int Type=OrderType();
    {
     switch(Type)
     {
      case OP_BUY:if(OrderProfit()>=Profit) CloseOrder=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),0,clrNavy);break;
      
      case OP_SELL:if(OrderProfit()>=Profit) CloseOrder=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),0,clrNavy);break;
     }
    }
  }
  return(0);
 }

void OnTick()
 {
   CountOrder();
   OpenOrders();
   CloseProfit();
 }
 

You are selecting orders BY_POS using a variable not based on OrdersTotal(), this is totally wrong, you will miss a lot of trades in your cycle.

If you don't want to use OrdersTotal()<1 for checking if your EA can do trades, make your own OrdersCount() function, that only counts order of Symbol() and with your magic number: in this way any instance of the EA can handle its own orders.

int OrdersCount() {
   int counter = 0;
   for( int i=OrdersTotal()-1; i>=0; i-- ) {
       if( !OrderSelect(i,SELECT_BY_POS) || OrderSymbol()!=Symbol() || OrderMagicNumber()!=MagicNumber ) continue;   
       counter++;
   }
   return(counter);
}
 
void OnTick()
 {
   CountOrder();
   OpenOrders();
   CloseProfit();
 }

here on Every tick it will Count Orders, 
I dont know why but 

int CountOrder()
 {
  int b=1;
  for(int Loop3=0;Loop3<b;Loop3++)
  {
   if(OrderSymbol()==Symbol()==OrderMagicNumber()==MagicNumber)
   Count++;
  }
  return(Count);
 }

This will give wrong number cause you must first select an order before you use Ordersymbol 
delete the CountOrder and use default function 

OrdersTotal()


after that it will open a buy and a sell Nothing will stop it. 

if(OrdersTotal()<maxNumber)
OpenOrders()

still thats poor way of limiting orders cause you will have 2 Orders a buy and a sell if maxNumber >1 

if(OrderSelect(a,SELECT_BY_POS,MODE_TRADES))
   if(OrderSymbol()==Symbol()==OrderMagicNumber()==MagicNumber)
   bool CloseOrder=false;
   int Type=OrderType();
    {

this will not compile 



Here was showing you places to polish up, and analyse and giving ideas 

NB I wasnt solving you problem

 
Fabio Cavalloni:


Has helped you on the OrdersCount.


Here is Fabio's code in another way 

int OrdersCount() {
   int counter = 0;
   for( int i=OrdersTotal()-1; i>=0; i-- ) {
       if( OrderSelect(i,SELECT_BY_POS) && OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber )    
       counter++;
   }
   return(counter);
}
 
Jack Buda:

Sorry, but I will not be helping you with this.

Did you learn nothing from your other topic?

https://www.mql5.com/en/forum/328726

OrdersTotal() work on 2 or more Charts by opening trades
OrdersTotal() work on 2 or more Charts by opening trades
  • 2019.12.18
  • www.mql5.com
Hi all I wonder if its possible to have OrdersTotal() on 2 different charts...
 
Exactly. Using OrdersTotal/OrdersHistoryTotal (MT4) or PositionsTotal (MT5), directly and/or no Magic number filtering on your OrderSelect/Position select loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
          Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 programming forum
          MagicNumber: "Magic" Identifier of the Order - MQL4 Articles
 
Keith Watford:

Sorry, but I will not be helping you with this.

Did you learn nothing from your other topic?

https://www.mql5.com/en/forum/328726

Mr. J. Buda wrote that post as well, 

Mr buda it has been 3 weeks what are you using to study your mql

 
Fabio Cavalloni:

You are selecting orders BY_POS using a variable not based on OrdersTotal(), this is totally wrong, you will miss a lot of trades in your cycle.

If you don't want to use OrdersTotal()<1 for checking if your EA can do trades, make your own OrdersCount() function, that only counts order of Symbol() and with your magic number: in this way any instance of the EA can handle its own orders.

Thank You Fabio, I made a mistake by not adding OrderSelect.
 
Jefferson Metha:

Mr. J. Buda wrote that post as well, 

Mr buda it has been 3 weeks what are you using to study your mql

I'm using Jimdany1958, MQL4Tutorial & Bilal Header on YouTube.

For a book its Andrew R. Young-Expert Advisor Programming_ Creating Automated Trading Systems in MQL for MetaTrader 4.

I use MQL4 for error's that i have encountered or something new i want to learn

 
Jack Buda:

I'm using Jimdany1958, MQL4Tutorial & Bilal Header on YouTube.

For a book its Andrew R. Young-Expert Advisor Programming_ Creating Automated Trading Systems in MQL for MetaTrader 4.

I use MQL4 for error's that i have encountered or something new i want to learn

imagine you are eating, and you order a lot of food from different places of earth, you have italian dishes, chinese, mexian, Russian, Indian, canaadian, SouthAfrican, Brazilian and many more all these are at ur table. 

Now cause your stomach is small you cant eat all of it, 

if you taste u wont get filled 
you have to select 1 and eat it, when done then later on u can try another meal.

Just like when studing MQL. take 1 course do it to the end, then do another. 

I watched Jimdandy videos from lesson 1 to lesson 26 and jumped to other lessons cause I understood, however Jim dandy doesnt do basics and the simplest thing will confuse you, then I read MQL documentation Oh my that was horrid went to Andrew young he talks about the basics before he goes into simple EA. also in his MT5 book he follows same pattern and topics so that will be pretty good. 

How ever Jim explains some principles in a very nice clear way. 

watch with your MetaEditor open following, 
read with your MetaEditor open following. 

 
Jefferson Metha:

imagine you are eating, and you order a lot of food from different places of earth, you have italian dishes, chinese, mexian, Russian, Indian, canaadian, SouthAfrican, Brazilian and many more all these are at ur table. 

Now cause your stomach is small you cant eat all of it, 

if you taste u wont get filled 
you have to select 1 and eat it, when done then later on u can try another meal.

Just like when studing MQL. take 1 course do it to the end, then do another. 

I watched Jimdandy videos from lesson 1 to lesson 26 and jumped to other lessons cause I understood, however Jim dandy doesnt do basics and the simplest thing will confuse you, then I read MQL documentation Oh my that was horrid went to Andrew young he talks about the basics before he goes into simple EA. also in his MT5 book he follows same pattern and topics so that will be pretty good. 

How ever Jim explains some principles in a very nice clear way. 

watch with your MetaEditor open following, 
read with your MetaEditor open following. 

Thanks for advice /tip i appreciate it a lot.

As i said in my previous post, that i started last January to program. Some functions were easy & simple, some were complex & difficult to understand. I know as i made this post i was at fault & i learn from it.

Thank You