Latest Position type

 

The code below correctly identifies the position type when there is only one trade opened, however if there are more than one position opened, the code does not return the latest position

type. How does i modify it to identify the position type of the LATEST opened trade if there are more than one trade? 


double POSITTYPE()
  {

   double POSX=0;
   
   string   symbol;
   symbol=PositionGetSymbol(POSITION_SYMBOL);

    for(int i=PositionsTotal()-1;i>=0;i--)
     {
      if(PositionSelectByTicket(PositionGetTicket(i)) && 
            PositionGetString(POSITION_SYMBOL)==_Symbol && 
            PositionGetInteger(POSITION_MAGIC)==MagicNumber)
           {
            if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
               POSX=1;
            if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)
               POSX=2;
           
        }
     }
   return( POSX);
  }

 
It is necessary to take into account the position time and store it in a variable. Implementation example: Last position type:
 
Vladimir Karputov:
It is necessary to take into account the position time and store it in a variable. Implementation example: Last position type:


double POSITTYPE()
  {

   datetime lastTime  = 0;

 long position_time = PositionGetInteger(POSITION_TIME);
   double POSX=0;
   
   string   symbol;
   symbol=PositionGetSymbol(POSITION_SYMBOL);

    for(int i=PositionsTotal()-1;i>=0;i--)
     {
      if(PositionSelectByTicket(PositionGetTicket(i)) && 
            PositionGetString(POSITION_SYMBOL)==_Symbol && 
            PositionGetInteger(POSITION_MAGIC)==MagicNumber&& position_time  > lastTime)
           {
            if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
               POSX=1;
            if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)
               POSX=2;
           
        }
     }
   return( POSX);
  }
  

The code has been amended as above but sill does not work

 
Peter Kaiza:

The code has been amended as above but sill does not work

1. POSITION_TIME returns a datetime type.
2. You are assigning that value before selecting your position
3. You never assign last_time a value so you're always comparing against 0
 
Peter Kaiza:

The code has been amended as above but sill does not work

Forum on trading, automated trading systems and testing trading strategies

Latest Position type

Vladimir Karputov, 2020.10.29 15:03

It is necessary to take into account the position time and store it in a variable. Implementation example: Last position type:

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   ENUM_POSITION_TYPE   last_pos_type  = WRONG_VALUE;
   ulong                last_pos_time  = 0;            // "0" -> D'1970.01.01 00:00';
   string               text           = "";
//---
   for(int i=PositionsTotal()-1; i>=0; i--) // returns the number of current positions
      if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties
         if(m_position.Symbol()==m_symbol.Name() && m_position.Magic()==InpMagic)
           {
            ulong pos_time=m_position.TimeMsc();
            if(pos_time>last_pos_time)
              {
               last_pos_type=m_position.PositionType();
               last_pos_time=pos_time;
               //---
               text=EnumToString(last_pos_type)+" | "+m_position.Symbol()+" | "+
                    IntegerToString(m_position.Ticket())+" | "+TimeToString(m_position.Time(),TIME_DATE|TIME_SECONDS)+" | "+
                    DoubleToString(m_position.Volume(),2);
              }
           }
   Comment(text);
  }
 
Vladimir Karputov:

How do  implement this?


           ulong pos_time=m_position.TimeMsc();
            if(pos_time>last_pos_time)
              {
               last_pos_type=m_position.PositionType();
               last_pos_time=pos_time;
 
Peter Kaiza :

How do  implement this?


I already gave a link to a complete example:

Forum on trading, automated trading systems and testing trading strategies

Latest Position type

Vladimir Karputov, 2020.10.29 15:03

It is necessary to take into account the position time and store it in a variable. Implementation example: Last position type: