its not closing only specific directions , please help what is missing here in my code ??

Pratyush Sharma  

i want to close only specific directions , and rest will be open, 

below i created a function , it take 1 parameter, ( buy, sell, all )


steps to get my view: 

step 1: run the EA

step 2: open manual trade : 3 buy and 3 sell

step 3: this EA should close only sell directions, because you written "sell" in the functions

but unfortunately it closes all postions ??? why.. what i'm missing here?? 


please help

#include <Trade\Trade.mqh>
CTrade trade;

void OnTick()
  {
    // open manually trade , and this EA will close all desired positions
    
    close_positions( "sell" );  
    
  } // on tick



void close_positions( string buy_sell_all = "all" )
{
    for ( int i = PositionsTotal()-1    ;   i >= 0  ;   i-- )
    {
          
        ulong direction = PositionGetInteger(POSITION_TYPE);
        string symbol = PositionGetSymbol(i);
        
        // close buy positions
        if ( buy_sell_all == "buy" && 
             symbol == _Symbol && 
             direction == POSITION_TYPE_BUY ){
             
             ulong ticket_no = PositionGetTicket(i);
             trade.PositionClose( ticket_no, -1 );
            } // if

        // close sell positions
        if ( buy_sell_all == "sell" && 
             symbol == _Symbol && 
             direction == POSITION_TYPE_SELL ){
                
             ulong ticket_no = PositionGetTicket(i);
             trade.PositionClose( ticket_no, -1 );
            } // if

        // close all positions
        if ( buy_sell_all == "all" && 
             symbol == _Symbol ){
             
             ulong ticket_no = PositionGetTicket(i);
             trade.PositionClose( ticket_no, -1 );
           } // if                  

    } // for

} // close all positions

Carl Schreiber  

Please read this article here to understand how to deal with open position: https://www.mql5.com/en/articles/7981.

Searching here is a lot faster than waiting here for other that might be interested...

Quick Manual Trading Toolkit: Working with open positions and pending orders
Quick Manual Trading Toolkit: Working with open positions and pending orders
  • www.mql5.com
In this article, we will expand the capabilities of the toolkit: we will add the ability to close trade positions upon specific conditions and will create tables for controlling market and pending orders, with the ability to edit these orders.
Pratyush Sharma  
Carl Schreiber #:

Please read this article here to understand how to deal with open position: https://www.mql5.com/en/articles/7981.

Searching here is a lot faster than waiting here for other that might be interested...

please help me to fix it.. it hard for me to understand oop written program 

William Roeder  
    for ( int i = PositionsTotal()-1    ;   i >= 0  ;   i-- )
    {
          
        ulong direction = PositionGetInteger(POSITION_TYPE);
        string symbol = PositionGetSymbol(i);


MT5: first select a position via CPositionInfo, directly, or by 'MT4Orders' library (2016)

Pratyush Sharma  
William Roeder #:


MT5: first select a position via CPositionInfo, directly, or by 'MT4Orders' library (2016)

William Roeder #:


MT5: first select a position via CPositionInfo, directly, or by 'MT4Orders' library (2016)

for ( int i = PositionsTotal()-1    ;   i >= 0  ;   i-- )
    {
        ulong ticket_no = PositionGetTicket(i);  
        ulong direction = PositionGetInteger(POSITION_TYPE);
        string symbol = PositionGetSymbol(i);
        

PERFECT .. NOW ITS WORKING.. 

Reason: