How to have only one open trade at a time?

 
Hi, My expert advisor is based on such a strategy that it opens a new trade on every single bar.What should i do to my ea so that it opens a new trade only when the previous trade is closed according to the closing strategy..........Thanks alot in advance !
 
int start(){

//manage open orders here

if (OrdersTotal() > 0){
      return(0);
}

//open new orders here

}

or a with a bit more control

int start(){

//manage open orders here

if (getOpenOrders() > 0){
      return(0);
}

//open new orders here

}

int getOpenOrders(){

   int liOrders = 0;
   for (int i = 0; i < OrdersTotal(); i++){
     if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == false){
       continue;
     }
     if (OrderSymbol() != Symbol() || OrderMagicNumber() != /*your magic number*/){
       continue;
     }
     liOrders++;
   }
   return(liOrders);
}
hth
 

@ Russell.....I can't figure out where to paste this code in my ea.Therefore,i am attaching my ea here.I would be very thankful if you paste this code in my ea and also test it whether it works or not.Also,please do provide me a brief description of where you pasted this code.Thanks alot....God Bless you :)

Files:
 

innovativebug:

you should start using Magicnumbers other then 0 like:


extern int MagicNumber = 34474212;


simpy copy&paste Russells code to the end of your code like here:



int getOpenOrders(){

   int liOrders = 0;
   for (int i = 0; i < OrdersTotal(); i++){
     if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == false){
       continue;
     }
     if (OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber){
       continue;
     }
     liOrders++;
   }
   return(liOrders);
}



and you must limit all ENTRY signals with getOpenOrders() function like:



   //Buy
   if (Order == SIGNAL_BUY && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount))) && getOpenOrders()==0) {




   //Sell
   if (Order == SIGNAL_SELL && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount))) && getOpenOrders()==0) {



thats it

 
@ fx1.net......i did the same just what i was told by you....The ea works but it reacting the same way as it use to work before i.e opening and closing trade on everybar (Backtested) :(
 

innovativebug:

- do you have multiple symbols on same EA?

- did you change MagicNumber as i mentioned?

Its impossible that it open a trade without closing previous if you have done it properly.

 
@ fx1.net .... i did change the magic no as you mentioned but i also run multiple symbols on the same ea.Do u recommend not to do that i.e having multiple currency ea ?
 
Taskin Osman:

innovativebug:

- do you have multiple symbols on same EA?

- did you change MagicNumber as i mentioned?

Its impossible that it open a trade without closing previous if you have done it properly.

Mate,  I am using own EA on multiple symbols...  ex)  GBPUSD, EURUSD, USDJPY  etc..


How can I open only one trade  ??


for example,


If my ea open trade on GBPUSD,   stop open other multiple symbols  until close the previous order(GBPUSD)

 
JIHUN NAM:

Mate,  I am using own EA on multiple symbols...  ex)  GBPUSD, EURUSD, USDJPY  etc..


How can I open only one trade  ??


for example,


If my ea open trade on GBPUSD,   stop open other multiple symbols  until close the previous order(GBPUSD)

Why are you responding to a 10 year old thread?

The answer is simple. Don't open any trades when a trade of any symbol  is already open.

 

if ( PositionsTotal()<1 )

{

 Code goes here, e.g (place a buy/sell order);

}

Thanks and GOD bless, amen.

WE THANK THEE JESUS,MARY OUR GOD.

 
VICTORMARY OLAREWAJUMARIA. #: if ( PositionsTotal()<1 )

There are no positions in MT4