Set an EA to only one symbol

Kl JJ  

Hello,

I would like to use my EA on different symbols on the same MT5 account but I can't because in some parts of the code I used PositionsTotal, or a double openprice but they are focusing on all the symbols on the account.


   //count open position
   
   buy_count = 0;   sell_count = 0; 
   
   
    for(int i = PositionsTotal() - 1 ; i >= 0 ; i--)   
     {
     PositionGetTicket(i);                           
     
      if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY) 
        {
         buy_count++;
        }
      if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL) 
        {
         sell_count++;
        }

     } 



double OpenPrice;

      for(int i =0;i<PositionsTotal();i++){
      
      ulong ticket =PositionGetTicket(i);
      
      if(PositionSelectByTicket(ticket)){
      
       OpenPrice= PositionGetDouble(POSITION_PRICE_OPEN);
      
      }


   }

I would like that this code only work on the current chart but I can't find a way to fix this, can you help me ? Thanks

Documentation on MQL5: Trade Functions / PositionsTotal
Documentation on MQL5: Trade Functions / PositionsTotal
  • www.mql5.com
PositionsTotal - Trade Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
Kl JJ  

Hi, i tried this but It doesn't work.

for(int i = PositionsTotal() - 1 ; i >= 0 ; i--)   
{
PositionGetTicket(i); 
     
                      
     
      if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY ) 
        {
        if(PositionGetString(POSITION_SYMBOL) == _Symbol)
        
          { 
            buy_count++;
          }
         
        }
      if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL ) 
        {
          if(PositionGetString(POSITION_SYMBOL) == _Symbol)
           { 
            sell_count++;
           }
        }
} 

Anton Ohurtsov  
Kl JJ #:

Hi, i tried this but It doesn't work.

You should select position by PositionSelectByTicket()
Kl JJ  
Anton Ohurtsov #:
You should select position by PositionSelectByTicket()

thanks for the help but i'm sorry it's still doesn't work


  if(PositionsTotal() > 0)
      for(int i = 0; i < PositionsTotal(); i++)
        {
         ulong ticket = PositionGetTicket(i);
         if(PositionSelectByTicket(ticket))
            if(PositionGetInteger(POSITION_MAGIC) == InMagicnumber)
               if(PositionGetString(POSITION_SYMBOL) == _Symbol)
                  if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
                     {
                        buy_count++;
                     }
                  if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)
                     {
                        sell_count++;
                     }
 
         }
Anton Ohurtsov  
Kl JJ #:

thanks for the help but i'm sorry it's still doesn't work


Also try type casting (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE) == ...
Anton Ohurtsov  
Are your positions opened with InMagicnumber?
Kl JJ  
Anton Ohurtsov #:
Are your positions opened with InMagicnumber?

Yes I was wrong because I opened positions manually so it works now ! thanks


and for the openprice, to only have the price on the current chart, I should do the same ?

Anton Ohurtsov  
Kl JJ #:

Yes I was wrong because I opened positions manually so it works now ! thanks


and for the openprice, to only have the price on the current chart, I should do the same ?

If open price of position, then you should do the same.
Kl JJ  

Okay it works !


for those who want the code : 

double OpenPrice;
   
   for(int v = 0; v< total_open_positions; v++)
     {
       ulong positionticket = PositionGetTicket(v);
       ulong  magic=PositionGetInteger(POSITION_MAGIC);
      
      if(PositionSelectByTicket(positionticket))
        {
         if(magic ==InMagicnumber )
           {
            
            OpenPrice= PositionGetDouble(POSITION_PRICE_OPEN);
            
           }
        }
        
         }