Check out my source code in an indicator that shows positions on charts and therefore, loops through open positions (and orders).
Note that while magic number is referenced in the scanning portion of the indicator code, magic number is not drawn on the chart. This should be fine for your purposes because all that you're trying to do is scan your positions by magic number.
Show Positions on Custom Chart (or standard chart) for MT5
Ryan L Johnson, 2025.06.02 14:02
This indicator is a utility that shows labelled trade levels on any chart. If you want to replace your native trade levels on a native chart, then turn off "Show trade levels" in your F8 Chart Properties and attach this indicator. If you want to show trade levels on a Custom Chart (where native trade levels cannot be shown), then simply attach this indicator. BaseSymbol - specify the Symbol from which the trade level data will be pulled--handy for unique Custom Symbols. TextBarsBack - specify the number of bars back in history from the current bar where the level labels will be drawn. Note: Although magic number is referenced in the code, it is not presently included in the trade lines. If you're running multiple EA's on the same Symbol, you can edit the code to show magic numbers and then run multiple instances of the indicator on one chart. You can also edit the font sizes and text spacing in the object properties as needed for different display resolutions.Check out my source code in an indicator that shows positions on charts and therefore, loops through open positions (and orders).
Note that while magic number is referenced in the scanning portion of the indicator code, magic number is not drawn on the chart. This should be fine for your purposes because all that you're trying to do is scan your positions by magic number.
int positions = PositionsTotal(); for(int i=0; i<positions; i++) { ResetLastError(); string symbol = PositionGetSymbol(i); { long pos_magic = PositionGetInteger(POSITION_MAGIC); if(pos_magic == MagicNumber && _Symbol==symbol) { positions++; } } } if((emaFast[2] >= emaSlow[2] && emaFast[0] < emaSlow[0]) && (maTrend[1] < 50 && maTrend[1] > 30) && high <= emaFast[1]) {signal ="sell";} if(signal =="sell" && positions <=0 ){ trade.Sell.... }
Assuming that all of your not shown variables are declared properly, I would only change one thing.
There's really no need for a counter like positions++ in a positions loop. Just put your trading conditions inside the positions loop to work with the zero positions status and your MagicNumber.
There i something wrong in your code. The positions counter begins from total number of positions. Eg if positionstotal are 3 then this is incremented by 1 every time the next position with same magic is found. So we have 4, 5 and 6, etc which is far too much. Let me correct here.
int positions_with_same_magic = 0; for(int i = PositionsTotal()-1; i>=0; i--){ if(PositionSelectByIndex(i)){ int magic = PositionGetInteger(POSITION_MAGIC); string symbol = PositionGetString(POSITION_SYMBOL); if(magic == ExpertMagic && symbol == _Symbol) positions_with_same_magic+=1; // or positions_with_same_magic++; } }
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hay, I am new to programming and this is my first EA.
I used PositionsTotal()<=0 to count all positions, now I want to use magic numbers to count positions.
I would like to ask if there are any other solutions or methods.