Prevent multiple trades on the same currency

 

Good Afternoon,

I'm currently working on my first EA and working with pre existing code.   Here is the question, is there a simple relatively clean way to have the EA check open orders, and ensure that if it would not open another order on the same currency?  The only way I can think of would be lots of if statements which seems like it would be highly inefficient.

For example if the EA were to take a long position on EURUSD, I want to ensure that it doesn't open anymore positions long or short on any pairs that have either the EUR or USD in them.  I apologize in advance if this has been asked and answered.  

 
Robert Bruce:

Good Afternoon,

I'm currently working on my first EA and working with pre existing code.   Here is the question, is there a simple relatively clean way to have the EA check open orders, and ensure that if it would not open another order on the same currency?  The only way I can think of would be lots of if statements which seems like it would be highly inefficient.

For example if the EA were to take a long position on EURUSD, I want to ensure that it doesn't open anymore positions long or short on any pairs that have either the EUR or USD in them.  I apologize in advance if this has been asked and answered.  

Ahh so you dont like currency correlation...

You could make an array of open positions (of type Position or just use the integer ticket).

If there is a signal to open on EURCAD then you would loop through the array and check if any currently open positions were opened on EUR or CAD. Maybe using substrings?

If there are no positions using either of them then open the new position based on the signal and add it to the array.

 
emerson2102:

Ahh so you dont like currency correlation...

You could make an array of open positions (of type Position or just use the integer ticket).

If there is a signal to open on EURCAD then you would loop through the array and check if any currently open positions were opened on EUR or CAD. Maybe using substrings?

If there are no positions using either of them then open the new position based on the signal and add it to the array.

Thanks!  I think your right an ARRAY is the way to go, looks like I have some reading todo.  I'm just trying to limit account risk by avoiding trading on the same currency multiple times.
 
Robert Bruce:

Good Afternoon,

I'm currently working on my first EA and working with pre existing code.   Here is the question, is there a simple relatively clean way to have the EA check open orders, and ensure that if it would not open another order on the same currency?  The only way I can think of would be lots of if statements which seems like it would be highly inefficient.

For example if the EA were to take a long position on EURUSD, I want to ensure that it doesn't open anymore positions long or short on any pairs that have either the EUR or USD in them.  I apologize in advance if this has been asked and answered.  


Hello, I would like to check if you were able to code this specific array as I Couldn't achieve it as my programming knowledge is a not so high.

Reason: