'PositionClose' - undeclared identifier

 

Hi all

the documentation mentions PositionClose() as part of the class CTrade.

I am directly manipulating the orders, not using class.

How can I close positions?

Current code (DOES NOT WORK):

ulong ticket;

if(PositionsTotal()>0) {
for(i = 0; i < PositionsTotal(); i++) {
ticket=PositionGetInteger(POSITION_TICKET);
PositionClose(ticket);
}
}

EDIT: I saw multiple similar questions and the answer is always to "use class" but I am not familiar with class and method coding, so I would rather do it directly the old fashioned way. Should I bite the bullet and just start using classes to learn how to?



Documentation on MQL5: Standard Library / Trade Classes / CTrade
Documentation on MQL5: Standard Library / Trade Classes / CTrade
  • www.mql5.com
CTrade - Trade Classes - Standard Library - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Nuno Costa:

Hi all

the documentation mentions PositionClose() as part of the class CTrade.

I am directly manipulating the orders, not using class.

How can I close positions?

Current code (DOES NOT WORK):


EDIT: I saw multiple similar questions and the answer is always to "use class" but I am not familiar with class and method coding, so I would rather do it directly the old fashioned way. Should I bite the bullet and just start using classes to learn how to?



////////////
/+------------------------------------------------------------------+
//|                                           SembolHepsiniKapat.mq5 |
//|                        Copyright 2018, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#include <trade/trade.mqh>
void OnStart()
  {
   CTrade trade;
   int i=PositionsTotal()-1;
   while (i>=0)
     {
       if (trade.PositionClose(PositionGetTicket(i))) Print("Kapatıldı");
      }
       i--;
     }
 
 
Mehmet Bastem #:

Thanks.

That and some other post from "@Vladimir Karputov" shown me the way. Cheers

 
Nuno Costa:

Hi all

the documentation mentions PositionClose() as part of the class CTrade.

I am directly manipulating the orders, not using class.

How can I close positions?

Current code (DOES NOT WORK):

ulong ticket;

EDIT: I saw multiple similar questions and the answer is always to "use class" but I am not familiar with class and method coding, so I would rather do it directly the old fashioned way. Should I bite the bullet and just start using classes to learn how to?



Hope this helps:

CTrade trade;
if(PositionsTotal()>0){
  for(i=0;i<PositionsTotal();i++){
    ulong iTicket=PositionGetTicket(i);
    if(!trade.PositionClose(iTicket,ULONG_MAX)){
      Print("PositionClose error ",trade.ResultRetcode());
      return;
      }
    }
  }
 
David Diez # :


Your code is wrong. Deleting positions must be carried out TO ZERO! Example:

//+------------------------------------------------------------------+
//| Close all positions                                              |
//+------------------------------------------------------------------+
void CloseAllPositions(void)
  {
   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)
           {
            if(m_position.PositionType()==POSITION_TYPE_BUY)
              {
               if(!m_trade.PositionClose(m_position.Ticket())) // close a position by the specified m_symbol
                  if(InpPrintLog)
                     Print(__FILE__," ",__FUNCTION__,", ERROR: ","BUY PositionClose ",m_position.Ticket(),", ",m_trade.ResultRetcodeDescription());
              }
            if(m_position.PositionType()==POSITION_TYPE_SELL)
              {
               if(!m_trade.PositionClose(m_position.Ticket())) // close a position by the specified m_symbol
                  if(InpPrintLog)
                     Print(__FILE__," ",__FUNCTION__,", ERROR: ","SELL PositionClose ",m_position.Ticket(),", ",m_trade.ResultRetcodeDescription());
              }
           }
  }
Reason: