Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
Your topic has been moved to the section: Expert Advisors and Automated Trading
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
It sounds like a situation where you're not establishing a unique magic number in the different expert advisors. I think you're also just overcomplicating the code
if(PositionsTotal() == 0 && magicNumber == Magic){
// open one position if no positions are active in *this* EA
}
It sounds like a situation where you're not establishing a unique magic number in the different expert advisors. I think you're also just overcomplicating the code
struct MqlTradeRequest |
magic | Expert Advisor ID. It allows organizing analytical processing of trade orders. Each Expert Advisor can set its own unique ID when sending a trade request. |
Before executing the order placement operation, check if there are any open positions. Loop through their magic numbers, and if one matches the EA's input magic number, you'll know that the EA already has a position open and you should skip the process.
I agree with you that this doesn't seem to work and I'm also at a loss here
I'm using the trade library, I set the magic number in OnInit:
trade.SetExpertMagicNumber(expert_magic);
then before I execute the trade, I try to request the magic number using RequestMagic() function
https://www.mql5.com/en/docs/standardlibrary/tradeclasses/ctrade/ctraderequestmagic
if(allow_buy && bearTrend[0] == 0 && trade.RequestMagic() == expert_magic){ }
but then it won't execute any position
Sorry, I don't know

- www.mql5.com
I just saw now that the magic number doesn't become active until the first position is opened, which is because magic number is sent with the order I guess, but ideally it should work different in the framework (in my opinion).
So you will have to make a function like this:
int GetOpenPositionCount( int cnt) { for(int i = PositionsTotal() - 1; i >= 0; i--) { if(PositionGetSymbol(i) == _Symbol && PositionSelect(_Symbol) && PositionGetInteger(POSITION_MAGIC) == MagicNumber) { cnt++; magicNumberActive = true; break; } } return cnt; }
with a global bool variable "magicNumberActive" initialized as false
then execute your positions in such a way:
if(magicNumberActive && trade.RequestMagic == magicNumber){ // execute trades } else{ // execute the first position without magic number }

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi everyone,
am currently facing an issue that I can't seem to overcome. I am running multiple bots on a single VPS. The problem is that, in my trading system, the expert advisor can only have one open position at a time.
At first, I used PositionsTotal , but I noticed that it counts the number of active positions across the entire account. As a result, when one bot opened a trade, all the other bots were unable to trade because one of the conditions for entering a position is that PositionsTotal must be equal to 0.
For now, here is the solution I have found:
This seemed to solve the problem, but I occasionally noticed that the bot could take two positions at the same time. After some thought, I realized that this might be due to the fact that MyPosition resets to 0 on every tick. With market stress or latency, it sometimes happened that the loop didn't have enough time to verify itself, and MyPosition remained at 0 long enough to allow the bot to open another position.
That’s why I considered a second solution:
However, after testing, I noticed that the bot takes multiple positions each time the conditions allow it.
I really don’t understand how I can run multiple bots simultaneously on different symbols while integrating a system that calculates the number of positions specific to each bot, without encountering issues related to stress or delays.
Thank you very much in advance for your help.
Schmitt Alexis