Help in coding.

18

Hi everyone.

Could anyone one please teach me how to control the number of the opened trades in my EA ? (please have a look on my code below)

It is a 2 moving averages (not crossover) (it is above/below) using candle number 0, so when the fast MA is above the slow MA it opens a buy order straight away, and the opposite for sell.

I know it might open a lot of trades as I'm using candle number 0, but I'm only trying to learn how to control the number of opened trades that's all.

The problem is that the EA open trades on every tick,,,,,
but what I would like is for the EA to open only 1 trade for every time the conditions for buy/sell orders are met.

For example:
then the conditions are met for a sell trade then the EA should open 1 sell trade,

I would appreciate any help.

```void OnTick()
{
double fastMA = iMA (NULL,60,5,0,0,0,0);
double slowMA = iMA (NULL,60,30,0,0,0,0);

if (fastMA>slowMA)
if (Orderscnt()==0) //for first buy order

if (fastMA<slowMA)
if (Orderscnt()==0) // for first sell order
int ticket_2 = OrderSend (Symbol(),OP_SELL,lot,Bid,30,0,0,NULL,magicno,0,clrRed);

if (fastMA>slowMA)
if (Orderscnt()>0) // for any future buy order

if (fastMA<slowMA)
if (Orderscnt()>0) // for any future sell order
int ticket_2 = OrderSend (Symbol(),OP_SELL,lot,Bid,30,0,0,NULL,magicno,0,clrRed);
}
//+------------------------------------------------------------------+
int Orderscnt(int type=-1)
{
int cnt=0;
for(int i=0;i<OrdersTotal();i++)
{
{
if(OrderSymbol()==Symbol()&&OrderMagicNumber()==magicno)
{
if(OrderType()==type||type==-1)
{
cnt++;
}
}
}
}
return(cnt);
}  ```
25092

tt.aa: Could anyone one please teach me how to control the number of the opened trades in my EA ? (please have a look on my code below)
1. Why did you post your MT4 question in the Root / MT5 EA 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. Your first if/if says fast above slow and no open orders, open a buy. The third if/if says fast above slow and at least one open order, open another buy. Stop opening orders if there is already one open. Delete the third and fourth.
206

Hi

here is the probleme with your code

if (Orderscnt ()> 0 )// for any future buy order

that s mean  the expert after checking conditions for buy or sell, will opens many, many others orders (maximum orders that broker allows)

Solution is simple :

change it to

if (fastMA>slowMA)
if (Orderscnt()==1)// for any future buy order

if (fastMA<slowMA)
if (Orderscnt()==1)// for any future sell order
int ticket_2 = OrderSend (Symbol(),OP_SELL,lot,Bid,30,0,0,NULL,magicno,0,clrRed);
}

In this way, Expert will open Just second Order and stop opening other orders.

good luck.