MT5, calculate open positions

 

Hi,


Im having trouble with an EA. I am trying to calculate open positions for this EA (with a certain magic number)

For testing, I am adding one buy position with a different magic number so I can see if this is working or not.

In backtest I always get both position when I count, but they have different magic numbers.

Why is that? What am I doing wrong here?


Im used to MT4 where you put some code to identify a position first, I cant find that function for MT5. 

if(OrderSelect(i,SELECT_BY_POS)==true)

I guess its something like that I just cant find and that is missing.


Code for counting:

double checkOpen()
  {
   int total=PositionsTotal();
   double total1=0;

   for(int cnt=0; cnt<total; cnt++)
     {
      string symbol=PositionGetSymbol(cnt);
      long magica=PositionGetInteger(POSITION_MAGIC);
      if(Symbol()==symbol && magica==magic1)
        {
         total1+=1;
        }
     }

   return(total1);
  }


some code for first buy:

trade.SetExpertMagicNumber(magic1);
trade.Buy(lots,NULL,Ask,Ask-StopLoss*_Point,Ask+TakeProfit*_Point,NULL);

Some code for second buy:

         trade.SetExpertMagicNumber(2222);
         trade.Buy(lots,NULL,Ask,Ask-StopLoss*_Point,Ask+TakeProfit*_Point,NULL);


Why does it not just calculate the first order and not the second? Why does it calculate both?


Thanks for reply!

Testing trading strategies on real ticks
Testing trading strategies on real ticks
  • www.mql5.com
The article provides the results of testing a simple trading strategy in three modes: "1 minute OHLC", "Every tick" and "Every tick based on real ticks" using actual historical data.
 
How to start with MQL5
How to start with MQL5
  • 2020.12.19
  • www.mql5.com
This thread discusses MQL5 code examples. There will be examples of how to get data from indicators, how to program advisors...
 

Thank you! It solved my problems! 

Reason: