Close all position

25 May 2017, 08:02
Jatin Patel
0
129

define                                  ID_OWN_MAG_NUM 9999999         // ALWAYS:  use MagicNumber of the expert, to avoid MIX
//+----------------------
//| Closing all positions, MATCHING this ID_OWN_MAG_NUM ( avoid deleting others ...
//+----------------------
void OnStart()
{                                                                       // .DEF + .INIT the trade request and result of trade request
   MqlTradeRequest request;
   MqlTradeResult  result;

   int             total = PositionsTotal();                                        // .GET  number of open positions
   for (  int i = total - 1; i >= 0; i-- )                              // .ITER  over all open positions
   {                                                                    //        .GET  params of the order:
          ulong  position_ticket  = PositionGetTicket(       i );                               //  - ticket of the position
          string position_symbol  = PositionGetString(       POSITION_SYMBOL );                 //  - symbol 
          int    digits           = (int) SymbolInfoInteger( position_symbol,       
                                                             SYMBOL_DIGITS      
                                                             );                                 //  - number of decimal places
          ulong  magic            = PositionGetInteger(      POSITION_MAGIC );                  //  - MagicNumber of the position
          double volume           = PositionGetDouble(       POSITION_VOLUME );                 //  - volume of the position
          ENUM_POSITION_TYPE type = (ENUM_POSITION_TYPE) PositionGetInteger( POSITION_TYPE );   //  - type of the position

          PrintFormat( "Tkt[#%I64u] %s  %s  %.2f  %s MagNUM[%I64d]",    // .GUI:    print details about the position    
                       position_ticket,
                       position_symbol,
                       EnumToString(type),
                       volume,
                       DoubleToString( PositionGetDouble( POSITION_PRICE_OPEN ), digits ),
                       magic
                       );

          if (  magic == ID_OWN_MAG_NUM )                               // .IF MATCH:
          {     ZeroMemory( request );                                  //     .CLR data
                ZeroMemory( result  );                                  //     .CLR data
                                                                        //     .SET:
                request.action    = TRADE_ACTION_DEAL;                  //          - type of trade operation
                request.position  = position_ticket;                    //          - ticket of the position
                request.symbol    = position_symbol;                    //          - symbol 
                request.volume    = volume;                             //          - volume of the position
                request.deviation = 5;                                  //          - allowed deviation from the price
                request.magic     = EXPERT_MAGIC;                       //          - MagicNumber of the position

                if (  type == POSITION_TYPE_BUY )
                {     request.price = SymbolInfoDouble( position_symbol, SYMBOL_BID );
                      request.type  = ORDER_TYPE_SELL;
                      }
                else
                {
                      request.price = SymbolInfoDouble( position_symbol, SYMBOL_ASK );
                      request.type  = ORDER_TYPE_BUY;
                      }

                PrintFormat(       "WILL TRY: Close Tkt[#%I64d] %s %s",                      position_ticket,
                                                                                             position_symbol,
                                                                                             EnumToString( type )
                                                                                             );
                if ( !OrderSend( request,result ) )
                      PrintFormat( "INF:  OrderSend(Tkt[#%I64d], ... ) call ret'd error %d", position_ticket,
                                                                                             GetLastError()
                                                                                             );
                PrintFormat(       "INF:            Tkt[#%I64d] retcode=%u  deal=%I64u  order=%I64u", position_ticket,
                                                                                                      result.retcode,
                                                                                                      result.deal,
                                                                                                      result.order
                                                                                                      );
                }
          }
  }

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------

POSITION_MAGIC is used in current open Position selection process.

PositionSelect() example:

int CurPosLots() // total lots in current symbol position
{
   int total=PositionsTotal();
   double lots=0;
   for (int cnt=0; cnt<total; cnt++) 
      {
         if(PositionSelect(Sysbol()) )
            {
               if(PositionGetInteger(POSITION_MAGIC)==MagicNumber)
                  {
                     lots=PositionGetDouble(POSITION_VOLUME); 
                  }
            }
      }

    return(lots);
}

Share it with friends: