How to prevent the opening of duplicate positions on a specific symbol?

 

Hello dear friends

I want to check in EA if a position of a specific currency is open or not!but i dont know how should i do that!

 For example, I want only one position to be opened at the same time on currencies that have USD on one side, for example,

if a position is already open on the EURUSD currency pair, do not open another position on GBP/USD or NZD/USD or any currency pair that has the USD symbol on one side.

so i need the code of that.

Thanks...

 
Mr. Vp: I want to check in EA if a position of a specific currency is open or not!but i dont know how should i do that!  For example, I want only one position to be opened at the same time on currencies that have USD on one side, for example, if a position is already open on the EURUSD currency pair, do not open another position on GBP/USD or NZD/USD or any currency pair that has the USD symbol on one side. so i need the code of that.

You can use PositionSelect. It will true if any position is open for the selected symbol. It will also select the position for further processing, but read the documentation about the differences between "netting" and "hedging" types of accounts.

EDIT: My answer was for MQL5 instead of MQL4. My apologies for the confusion.

 
Fernando Carreiro #:
You can use PositionSelect. It will true if any position is open for the selected symbol. It will also select the position for further processing, but read the documentation about the differences between "netting" and "hedging" types of accounts.

thanks

Can you show me a code example about this Because I can't get what I want by just knowing a function!

 
Mr. Vp #: Can you show me a code example about this Because I can't get what I want by just knowing a function!
My apologies! I answered your original question with information about MQL5 instead of MQL4.
 
Mr. Vp: I want to check in EA if a position of a specific currency is open or not!but i dont know how should i do that!  For example, I want only one position to be opened at the same time on currencies that have USD on one side, for example, if a position is already open on the EURUSD currency pair, do not open another position on GBP/USD or NZD/USD or any currency pair that has the USD symbol on one side. so i need the code of that. Thanks...

In MQL4 you have to scan all the current orders in search of market orders for a specific symbol. It can't be done by a single function as in the case for MQL5.

So, I suggest taking some time to analyse some examples of MQL4 EAs in the CodeBase so that you can learn how to manage orders and use the trade functions in MQL4.

Trade Functions - MQL4 Reference
Trade Functions - MQL4 Reference
  • docs.mql4.com
Trade Functions - MQL4 Reference
 
Fernando Carreiro #:

In MQL4 you have to scan all the current orders in search of market orders for a specific symbol. It can't be done by a single function as in the case for MQL5.

So, I suggest taking some time to analyse some examples of MQL4 EAs in the CodeBase so that you can learn how to manage orders and use the trade functions in MQL4.

I have been working on my expert for more than a year

I got really good results, but the only problem I still couldn't solve is the same question I asked at the beginning of this page!

Thank you very much for guiding me. I will try to find the answer to my question through your guidance

, but I ask other friends who read this post that if you know the answer of my question, please send the code in the form of a Small project!

Thank u very much 

 
Mr. Vp #: the only problem I still couldn't solve is the same question I asked at the beginning of this page!

You were told what to do, an OrderSelect loop and see if you have one for the current symbol. Show us your attempt (using the CODE button) and state the nature of your difficulty.
          No free help (2017)

Or pay someone. Top of every page is the link Freelance.
          Hiring to write script - General - MQL5 programming forum (2018)

We're not going to code it for you (although it could happen if you are lucky or the issue is interesting).
          No free help (2017)

 
Mr. Vp #:

I have been working on my expert for more than a year

I got really good results, but the only problem I still couldn't solve is the same question I asked at the beginning of this page!

Thank you very much for guiding me. I will try to find the answer to my question through your guidance

, but I ask other friends who read this post that if you know the answer of my question, please send the code in the form of a Small project!

Thank u very much 

I would loop through all opened trades and break all pairs into half… like USDJPY I would break it in USD and JPY…put all into an array, and then do the same for the current chart symbol… then loop through all items in that array and compare with the current chart symbol… just an idea … there is more to do to make it compatible with all prefixes or suffixes that some brokers may have … or maybe you want to avoid buying USD in one trade, and selling USD in another trade … there is more to do 
 
Daniel Cioca #:
I would loop through all opened trades and break all pairs into half… like USDJPY I would break it in USD and JPY…put all into an array, and then do the same for the current chart symbol… then loop through all items in that array and compare with the current chart symbol… just an idea … there is more to do to make it compatible with all prefixes or suffixes that some brokers may have … or maybe you want to avoid buying USD in one trade, and selling USD in another trade … there is more to do 

thanks dear

Reason: