How do I add code for Maximum Trades Open At A Time?

 

Hi,

I cant seem to get the Max Orders to work.

(Max Orders being maximum ammount of trades open at a time)

A little help please and thanks!

Here is the whole EA code

[Very simple 2 moving average EA]

when the lines cross it trades.

 

#property copyright "Reilly"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

//////////////////////////////////////////////////////////////////////
//INPUT//
//////////////////////////////////////////////////////////////////////
extern double takeprofit=0.005;
extern double stoploss=0.0025;
extern int fastma=5;
extern int fastmashift=0;
//^ ma shift is offset of linegraph on chart
extern int fastmamethod=0;
//^ma method is simple/exponential moving average etc
extern int fastmaappliedto=0;
//^applied to is basing the moving average on the closing price of the bar or the opening price etc
extern int slowma=21;
extern int slowmashift=0;
extern int slowmamethod=0;
extern int slowmaappliedto=0;
extern double lotsize=0.01;
extern int magicnumber = 1337;
double pips;
int ticket1;
int ticket2;
int orders=OrdersTotal();
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()

  {
//---
/*
   double ticksize = MarketInfo(Symbol(),MODE_TICKSIZE);
   if (ticksize = 0.00001 || 0.001)
   double pips = (ticksize*10);
   else pips = ticksize;
   */

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


double previousfast = iMA(NULL,0,fastma,fastmashift,fastmamethod,fastmaappliedto,2);
double currentfast = iMA(NULL,0,fastma,fastmashift,fastmamethod,fastmaappliedto,1);
//^(NULL=moving average on the current currency pair. 0=the current timeframe. fastma= the moving average period.
double previousslow = iMA(NULL,0,slowma,slowmashift,slowmamethod,slowmaappliedto,2);
double currentslow = iMA(NULL,0,slowma,slowmashift,slowmamethod,slowmaappliedto,1);


   static datetime BarTime=0;
   datetime now_bar=Time[0];
   if(BarTime!=now_bar)
     {
      BarTime=now_bar;
  
    


if(previousfast<previousslow && currentfast>currentslow && orders<1)

{



if(OrderSend(Symbol(),OP_BUY,lotsize,Ask,3,Ask-(stoploss),Ask+(takeprofit),"EA Jim Dandy MA Trade",magicnumber,0,Green)==-1)
Print(GetLastError());
  

}

  if (previousfast>previousslow && currentfast<currentslow && orders<1)

{

  
      
if (OrderSend(Symbol(),OP_SELL,lotsize,Bid,3,Bid+(stoploss),Bid-(takeprofit),"EA Jim Dandy MA Trade",magicnumber,0,Red)==-1)
Print(GetLastError());
}
}
  }

//+------------------------------------------------------------------+
 
You need to update "orders" each time. Your code never updates the value.

If you don't want your EA to do anything when there is already an order, just do the check right at the start. That will avoid a lot of unnecessary processing:

void OnTick()
  {
   if(OrdersTotal()>0) return;
 
honest_knave:
You need to update "orders" each time. Your code never updates the value.

If you don't want your EA to do anything when there is already an order, just do the check right at the start. That will avoid a lot of unnecessary processing:

void OnTick()
  {
   if(OrdersTotal()>0) return;
 
see
Files:
 
Orders variable should be updated every tick, you are only assigning it once at the start of the EA
 
honest_knave: If you don't want your EA to do anything when there is already an order, just do the check right at the start. That will avoid a lot of unnecessary processing:
void OnTick(){
   if(OrdersTotal()>0) return;
Using OrdersTotal directly and/or no Magic number filtering on your OrderSelect 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 forum
 
whroeder1:
Using OrdersTotal directly and/or no Magic number filtering on your OrderSelect 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 forum
The OP will need to decide if that is a problem for them. There is no need to add additional code if they don't need it.
 
honest_knave: There is no need to add additional code if they don't need it.
And they don't know if they need it, unless a warning is given about their current limitations. And again you have to try to get the last word in.
Reason: