help~how to get current open position info only?[MQL5]

 
void CheckOrder()
{
 BuyNum=0;
 ArraySetAsSeries(BuyOPrice,true);
 ArraySetAsSeries(buy_ticketnum,true);
 
 if(PositionsTotal()>0)
 {
   for(int i=0;i<=BuyNum;i++)
   {
    ulong takeOrder=PositionGetTicket(i);
    if(PositionSelect(Symbol())==true)
    {
     if(PositionGetSymbol(i)==Symbol() && PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
     {
      if(PositionGetString(POSITION_COMMENT)=="Buy_A"+string(i+1)) //if I add comment get, it includes closed order
      {
        ArrayResize(BuyOPrice,i+1,0);
        BuyNum++;
        ArrayResize(buy_ticketnum,i+1,0);
        buy_ticketnum[i]=PositionGetInteger(POSITION_TICKET);
        BuyOPrice[i]=PositionGetDouble(POSITION_PRICE_OPEN);
      }
     }
    }
   }  
 }
}

Hi everyone, how can I get current open position with specific comment? when I use positionGetstring(position_comment), it includes closed order which i dont want. please help me, thank you!

Documentation on MQL5: Trade Functions / PositionGetString
Documentation on MQL5: Trade Functions / PositionGetString
  • www.mql5.com
PositionGetString - Trade Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Wai Shing Ng:

Hi everyone, how can I get current open position with specific comment? when I use positionGetstring(position_comment), it includes closed order which i dont want. please help me, thank you!

In MQL5 you can't compare strings by '==' you have to use; https://www.mql5.com/en/docs/strings/stringcompare

Documentation on MQL5: String Functions / StringCompare
Documentation on MQL5: String Functions / StringCompare
  • www.mql5.com
StringCompare - String Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 

I recommend learning the basics - so as not to confuse "order", "deal" and "position": Basic Principles

You didn't read the documentation well and you are using the functions for the NETTING account.


Here's how to do it:

//+------------------------------------------------------------------+
//|                                                      Test_en.mq5 |
//|                              Copyright © 2022, Vladimir Karputov |
//|                      https://www.mql5.com/en/users/barabashkakvn |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2022, Vladimir Karputov"
#property link      "https://www.mql5.com/en/users/barabashkakvn"
#property version   "1.000"
#property script_show_inputs
//---
#include <Trade\PositionInfo.mqh>
CPositionInfo  m_position;                   // trade position object
//---
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
  }
//+------------------------------------------------------------------+
//| Check Positions                                                  |
//+------------------------------------------------------------------+
void CheckPositions()
  {
   for(int i=PositionsTotal()-1; i>=0; i--)
      if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties
         if(m_position.Symbol()==Symbol() /*&& m_position.Magic()==m_magic*/)
           {
            string comment=m_position.Commemt();
            ulong ticket=m_position.Ticket();
            if(m_position.PositionType()==POSITION_TYPE_BUY)
              {
               ...
              }
            else
              {
               ...
              }
           }
  }
//+------------------------------------------------------------------+
 
Carl Schreiber #:

In MQL5 you can't compare strings by '==' you have to use; https://www.mql5.com/en/docs/strings/stringcompare

thank you very much for your help

 
Vladimir Karputov #:

I recommend learning the basics - so as not to confuse "order", "deal" and "position": Basic Principles

You didn't read the documentation well and you are using the functions for the NETTING account.


Here's how to do it:

thank you very much for your help and information

 
    ulong takeOrder=PositionGetTicket(i);
    if(PositionSelect(Symbol())==true)

You get a ticket from a random position (any chart) but then select a position on the current chart.