PositionClose is not working

 

Hİ all

Both close function is not working for me 

the code is below,

do you see the reason for it?


trade.PositionClose(PositionGetSymbol(i));
               trade.PositionClose(ticket);
    for(int i=PositionsTotal()-1; i>=0; i--)
        {
         ulong ticket = PositionGetTicket(i);
         PositionSelectByTicket(ticket);
         ENUM_POSITION_TYPE type=(ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);

         if(PositionGetSymbol(ticket)==_Symbol) //Sadece kendi pairindeki semboller üstünde işlem yap
           {
            if(type==POSITION_TYPE_BUY)
              {
               trade.PositionClose(PositionGetSymbol(i));
               trade.PositionClose(ticket);
              }
           }
        }
 

The correct example is:

//+------------------------------------------------------------------+
//|                               Close Positions Current Symbol.mq5 |
//|                              Copyright © 2020, Vladimir Karputov |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2020, Vladimir Karputov"
#property version   "1.000"
#include <Trade\PositionInfo.mqh>
#include <Trade\Trade.mqh>
CPositionInfo  m_position;                   // trade position object
CTrade         m_trade;                      // trading object
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   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()==Symbol())
           {
            if(!m_trade.PositionClose(m_position.Ticket())) // close a position by the specified m_symbol
               Print(__FILE__," ",__FUNCTION__,", ERROR: ","CTrade.PositionClose ",m_position.Ticket());
           }
  }
//+------------------------------------------------------------------+
 
Vladimir Karputov:

The correct example is:

but I just want to close one type of positions buy or sell

how can I do it before it

 
MEHMET FATIH BARUT:

but I just want to close one type of positions buy or sell

how can I do it before it

Code (very simplified code) :

//+------------------------------------------------------------------+
//| Close positions                                                  |
//+------------------------------------------------------------------+
void ClosePositions(const ENUM_POSITION_TYPE pos_type)
  {
   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()==Symbol())
            if(m_position.PositionType()==pos_type)
              {
               if(m_position.PositionType()==POSITION_TYPE_BUY)
                  if(!m_trade.PositionClose(m_position.Ticket())) // close a position by the specified m_symbol
                     Print(__FILE__," ",__FUNCTION__,", ERROR: ","CTrade.PositionClose ",m_position.Ticket());
               if(m_position.PositionType()==POSITION_TYPE_SELL)
                  if(!m_trade.PositionClose(m_position.Ticket())) // close a position by the specified m_symbol
                     Print(__FILE__," ",__FUNCTION__,", ERROR: ","CTrade.PositionClose ",m_position.Ticket());
              }
  }
 

you are great man, you made me rich

yeeey.

 
one last u
Vladimir Karputov:

Code (very simplified code) :

one last question, how can I modify the position for further continuing. How can I modify the position's SL and TP

I meant how can I take some little risk to Trail my takeprofit little bit further.

 
MEHMET FATIH BARUT:
one last u

one last question, how can I modify the position for further continuing. How can I modify the position's SL and TP

I meant how can I take some little risk to Trail my takeprofit little bit further.

Maybe you should try to read the documentation ?

Have you understood why your initial code what not working properly ?

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

Maybe you should try to read the documentation ?

Have you understood why your initial code what not working properly ?

Only difference is seemed to me as PositionInfo.mqh

 
MEHMET FATIH BARUT :

Only difference is seemed to me as  PositionInfo.mqh

CPositionInfo shopping class allows you to write simple and understandable code.

Let's step by step this code:

1.   for(int i=PositionsTotal()-1; i>=0; i--) // returns the number of current positions
2.      if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties
3.         if(m_position.Symbol()==Symbol())
           {
4.            if(!m_trade.PositionClose(m_position.Ticket())) // close a position by the specified m_symbol
               Print(__FILE__," ",__FUNCTION__,", ERROR: ","CTrade.PositionClose ",m_position.Ticket());
           }

1 -> passage through the list of all positions TO ZERO

2. -> SelectByIndex (Selects the position by index for further access to its properties)

3. -> compare position symbol and current symbol

4. -> PositionClose (closes a position with the specified ticket)

Documentation on MQL5: Standard Library / Trade Classes / CPositionInfo / SelectByIndex
Documentation on MQL5: Standard Library / Trade Classes / CPositionInfo / SelectByIndex
  • www.mql5.com
Standard Library / Trade Classes / CPositionInfo / SelectByIndex - Reference on algorithmic/automated trading language for MetaTrader 5
Reason: