Only have one trade open per currency pair at a time

 

Hi Guys,

I am trying to add some code to my EA so that it only has one trade open per currency pair at a time.

I found this code online and tried it but I'm now getting the following errors:

Errors 

Could anyone possibly give me advice on how to resolve this issue?

void OnTick()  
        
   {  
   int total,ord,i;
   string symbol;
   total = OrdersTotal();
  
   for(i=0;i<total;i++)

   {
   OrderSelect(i,SELECT_BY_POS);
   if(OrderSymbol() = Symbol())ord++;
   }
  
   if(ord>0); //Abort! A Position For This Pair is Already Open

//the rest of my program code
}

Thanks in advance!

Anton
 

 
== and not =
 
The code I posted in this thread may also help.
 
Thank you guys, I appreciate the help!
 
Alain Verleyen:
== and not =

Hi Alain,

I have been extremely busy with work so haven't been able to spend much on my code last week. Even when I add what you recommended, the code still opens multiple trades per currency pair.

Do you maybe have an idea why it does this?

Thank you,

Anton 

 
This does not stop your code. It will just roll right on past it, whatever the outcome of the "ord" test.
void OnTick()  
        
   {  
   int total,ord,i;
   string symbol;
   total = OrdersTotal();
  
   for(i=0;i<total;i++)

   {
   OrderSelect(i,SELECT_BY_POS);
   if(OrderSymbol() == Symbol())ord++;
   }
  
   if(ord>0); //Abort! A Position For This Pair is Already Open

//the rest of my program code
}

If you don't want you code to do anything else if a position is already open (so you don't want it to manage any open positions etc) then change this to:

void OnTick()  
        
   {  
   int total,ord,i;
   string symbol;
   total = OrdersTotal();
  
   for(i=0;i<total;i++)

   {
   OrderSelect(i,SELECT_BY_POS);
   if(OrderSymbol() == Symbol())ord++;
   }
  
   if(ord>0) return; //Abort! A Position For This Pair is Already Open

//the rest of my program code
}


 

 
honest_knave:
This does not stop your code. It will just roll right on past it, whatever the outcome of the "ord" test.
void OnTick()  
        
   {  
   int total,ord,i;
   string symbol;
   total = OrdersTotal();
  
   for(i=0;i<total;i++)

   {
   OrderSelect(i,SELECT_BY_POS);
   if(OrderSymbol() == Symbol())ord++;
   }
  
   if(ord>0); //Abort! A Position For This Pair is Already Open

//the rest of my program code
}

If you don't want you code to do anything else if a position is already open (so you don't want it to manage any open positions etc) then change this to:

void OnTick()  
        
   {  
   int total,ord,i;
   string symbol;
   total = OrdersTotal();
  
   for(i=0;i<total;i++)

   {
   OrderSelect(i,SELECT_BY_POS);
   if(OrderSymbol() == Symbol())ord++;
   }
  
   if(ord>0) return; //Abort! A Position For This Pair is Already Open

//the rest of my program code
}


 

Thanks a ton Honest!!

If I add the return; and link my code to multiple charts, for example: EURUSD and GBPUSD, then will the code trade the charts completely independent of each other? provided of course there is enough in my account balance to fund both?

 
C0mput3r:

Thanks a ton Honest!!

If I add the return; and link my code to multiple charts, for example: EURUSD and GBPUSD, then will the code trade the charts completely independent of each other? provided of course there is enough in my account balance to fund both?

Yes, each instance of the EA will be working independently unless you code it otherwise.

If you have one EURUSD order open:

  • ord==1 on EURUSD chart so no more trades allowed on that chart.
  • ord==0 on GBPUSD chart so trade allowed on that chart.

This is because you are testing for Symbol.

The only caveat: if you have 2 charts of the same symbol open (e.g. 2 x EURUSD charts) then they will only trade 1 order combined... in theory. You may find some oddities through processing delays if they are both are trading the same logic at the same time. This is where Magic Numbers could be useful.

If you want to trade multiple symbols from the same chart e.g. have only the EURUSD chart open but trade EURUSD and GBPUSD, you would need to make some changes.
 
honest_knave:

Yes, each instance of the EA will be working independently unless you code it otherwise.

If you have one EURUSD order open:

  • ord==1 on EURUSD chart so no more trades allowed on that chart.
  • ord==0 on GBPUSD chart so trade allowed on that chart.

This is because you are testing for Symbol.

The only caveat: if you have 2 charts of the same symbol open (e.g. 2 x EURUSD charts) then they will only trade 1 order combined... in theory. You may find some oddities through processing delays if they are both are trading the same logic at the same time. This is where Magic Numbers could be useful.

If you want to trade multiple symbols from the same chart e.g. have only the EURUSD chart open but trade EURUSD and GBPUSD, you would need to make some changes.
Thanks honest! I really appreciate it! I have divided my lot sizes in 2 now and linked my code to EURUSD and GBPUSD.
double AccountBalance = AccountInfoDouble(ACCOUNT_BALANCE);
double Leverage = AccountInfoInteger(ACCOUNT_LEVERAGE);
double LotSize = ((((AccountBalance/Ask)*Leverage) - 1)/100000)/2;
So hopefully it will start trading on both Symbols independently without affecting each others orders.
 
 
if(OrderSymbol() == Symbol())ord++;
Do not trade multiple currencies in one EA
Reason: