Close Position

 
Waves everyone....

May i ask what is the fast way of learning that a Position in a specific Symbol has been Closed? Both Manually and with TP/SL.
Will really appreciate it. 

Sincerely. 
 
Kourosh Emami: May i ask what is the fast way of learning that a Position in a specific Symbol has been Closed? Both Manually and with TP/SL. Will really appreciate it. 

I'm not absolutely sure about this, but I believe that using PositionSelectByTicket(), it should return false, if it is no longer open. 

You will have to test this to see if it is the case. Please report back here after you have tested it.

Forum on trading, automated trading systems and testing trading strategies

MQL5 - How to check if Position/Order closed in EA

Alain Verleyen, 2018.10.20 15:04

If you are talking about mql4 OrderCloseTime(), I suppose you are knowing the ticket ? Then you just have to use PositionSelectByTicket(ticket) which return a bool, true if the position is still open.

No need to check deals just to know if a position is open, as if it is open there is no "out" deal(s) (with exception of partial closing).

 
Fernando Carreiro #:

I'm not absolutely sure about this, but I believe that using PositionSelectByTicket(), it should return false, if it is no longer open. 

You will have to test this to see if it is the case. Please report back here after you have tested it.

First of all thank you for your generosity that your solutions have been so practical and easy to follow.

i tried that expression but unfortunately after compiling it says it has no effects.

here is the following code, in this code i'm just trying to make an argument named " candleBreak " to 0 after a Position in a specific Symbol is closed cause it's gonna run on multiply Symbols and just let the program to open one position per Symbol...

#include <Trade\Trade.mqh>

input ENUM_TIMEFRAMES   i_eTimeframe   = PERIOD_M15;
input double            i_dbVolume     = 0.1;
int candleBreak = 0;
int posNumber = 0;
ulong posTicket= 0;

CTrade oTrade;

int OnInit() {
   return INIT_SUCCEEDED;
};

void OnTick() {
   static datetime dtBarCurrent  = WRONG_VALUE;
          datetime dtBarPrevious = dtBarCurrent;
                   dtBarCurrent  = iTime( _Symbol, i_eTimeframe, 0 );
          bool     bNewBarEvent  = ( dtBarCurrent != dtBarPrevious );
   static bool     bNewBarTrade  = false;
   static double   dbHigh = WRONG_VALUE,
                   dbLow  = WRONG_VALUE;
                   
                   
              for ( posNumber = 0; posNumber <= PositionsTotal() - 1; posNumber++ ) {
                         posTicket = PositionGetTicket(posNumber);
                         if (PositionSelectByTicket(posTicket) == false) {
                     candleBreak == 0;          
                   }
                  }
                   
                   
   if( bNewBarEvent ) {
         dbHigh       = iHigh( _Symbol, i_eTimeframe, 1 );
         dbLow        = iLow(  _Symbol, i_eTimeframe, 1 );
         bNewBarTrade = false;
   };
   if( !bNewBarTrade ) {
      double
         dbAsk  = SymbolInfoDouble( _Symbol, SYMBOL_BID ),
         dbBid  = SymbolInfoDouble( _Symbol, SYMBOL_BID );
      if( ( dbBid > dbHigh ) && ( candleBreak <= 0 ) ) {
      candleBreak = 1;
         bNewBarTrade = oTrade.Buy(  i_dbVolume, _Symbol, NULL, NULL , dbBid + 100*_Point );
      }
      else if( ( dbBid < dbLow ) && ( candleBreak >= 0 ) ) {
      candleBreak = -1;
         bNewBarTrade = oTrade.Sell( i_dbVolume, _Symbol, NULL, NULL, dbBid - 100*_Point );
         }
         }
         }
Files:
 
Kourosh Emami #: First of all thank you for your generosity that your solutions have been so practical and easy to follow. i tried that expression but unfortunately after compiling it says it has no effects. here is the following code, in this code i'm just trying to make an argument named " candleBreak " to 0 after a Position in a specific Symbol is closed cause it's gonna run on multiply Symbols and just let the program to open one position per Symbol...

Unfortunately, your code will NEVER be able to detect a closed position, because you are running it against positions which are open.

By looping over all open positions "PositionsTotal()", they will always report a valid selection by ticket.

for ( posNumber = 0; posNumber <= PositionsTotal() - 1; posNumber++ ) {
   posTicket = PositionGetTicket(posNumber);
   if ( PositionSelectByTicket(posTicket) == false ) { // function will always return true
      candleBreak == 0;          
   }
}

You will have to save a ticket number for a trade and then later check if it is still selectable or not. Your current code does not do that.

In fact, your current code does not even need to know if a trade is closed or not.

 
Fernando Carreiro #:

Unfortunately, your code will NEVER be able to detect a closed position, because you are running it against positions which are open.

By looping over all open positions "PositionsTotal()", they will always report a valid selection by ticket.

You will have to save a ticket number for a trade and then later check if it is still selectable or not. Your current code does not do that.

In fact, your current code does not even need to know if a trade is closed or not.

Maaaaan I love you..... I actually did it with these two simple codes....

ulong  posTicket = PositionGetInteger(POSITION_TICKET);
if ( PositionSelectByTicket( posTicket ) == false ) candleBreak = 0;

as soon as the position is closed it will reset it to zero...

but do you think it will run individually for each Symbol ?

the market is closed and i can't test it right now on multiply Symbols so i would like to know your opinion... 

 
Kourosh Emami #: Maaaaan I love you..... I actually did it with these two simple codes.... as soon as the position is closed it will reset it to zero... but do you think it will run individually for each Symbol ? the market is closed and i can't test it right now on multiply Symbols so i would like to know your opinion... 
A ticket number is unique, irrespective of symbol or magic number or other positions.