How Do i Use PositionClose()

mohsen bahrami  

Hi.

How can i get the ticket of an position and close a the position?

whats wrong in Code ?


InThe Code, I Open a position and immediately (after 10 seconds) i want to close it.



//+------------------------------------------------------------------+
//|                                           test PositionClose.mq5 |
//+------------------------------------------------------------------+

#include <Expert\Expert.mqh>
#include <Trade\AccountInfo.mqh>
#include <Trade\PositionInfo.mqh>
#include <Trade\Trade.mqh>
#include <Strings\String.mqh>
#include <Statistics.mqh>
CTrade CT;
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
{
//---
ulong Ticket;
MqlTradeRequest request;
MqlTradeResult result;
ZeroMemory(request);
ZeroMemory(result);

request.action= TRADE_ACTION_DEAL;
request.magic = 22;
request.symbol= _Symbol;
request.volume= 0.05;
request.price = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
request.deviation=4;
request.action=TRADE_ACTION_DEAL;
request.type_filling= ORDER_FILLING_FOK;
request.type=ORDER_TYPE_BUY;

if(OrderSend(request,result))//|| result.deal==0)
  {
    Print("Do_Order 1 : result.deal: " ,result.deal);
    Ticket=GetLastOrderTicket();
    Print(" Ticket:",Ticket);
  }
Print("Do_Order 3 : result.deal: ",result.deal);

Sleep (10000);

if(     0
    || CT.PositionClose( Ticket,-1)
    || CT.PositionClose(_Symbol,-1)
  )
   { 
     Print("PositionClose :",_Symbol,"       Ticket Open:", Ticket );
   } 
   else
     Print("PositionClose 6  not close");
     
     }

//+------------------------------------------------------------------+
//| Returns the last order ticket in history or -1                   |
//+------------------------------------------------------------------+
ulong GetLastOrderTicket()
  {
//--- request history for the last 10 days
   if(!GetTradeHistory(20))
     {
      //--- notify on unsuccessful call and return -1
      Print(__FUNCTION__," HistorySelect() returned false");
      return -1;
     }
//--- 
   ulong first_order,last_order,orders=HistoryOrdersTotal();
//--- work with orders if there are any
   if(orders>0)
     {
     //  Print("Orders = ",orders);
      first_order=HistoryOrderGetTicket(0);
     //  PrintFormat("first_order = %d",first_order);
      if(orders>1)
        {
         last_order=HistoryOrderGetTicket((int)orders-1);
     //     PrintFormat("last_order = %d",last_order);
         return last_order;
        }
      return first_order;
     }
//--- no order found, return -1
   return -1;
  }
//+--------------------------------------------------------------------------+
//| Requests history for the last days and returns false in case of failure  |Trade Functions
//+--------------------------------------------------------------------------+
bool GetTradeHistory(int days)
  {
//--- set a week period to request trade history
   datetime to=TimeCurrent();
   datetime from=to-days*PeriodSeconds(PERIOD_D1);
   ResetLastError();
//--- make a request and check the result
   if(!HistorySelect(from,to))
     {
      Print(__FUNCTION__," HistorySelect=false. Error code=",GetLastError());
      return false;
     }
//--- history received successfully
   return true;
  }
  
rrocchi  

First you have to select a position to do something with it.


use m_position.SelectByIndex() function


Example1:

CTrade                  m_trade;      
CPositionInfo           m_position;  
 

 if ( PositionsTotal() > 0) {

      for (int i=0; i < PositionsTotal() ; i++) { 
         if(m_position.SelectByIndex(i)) {
            if (m_position.Symbol()==Symbol() && m_position.Magic()==m_magic) {
               if (m_position.PositionType() == POSITION_TYPE_SELL || m_position.PositionType() == POSITION_TYPE_BUY ) {
        
                  m_trade.PositionClose(m_position.Ticket());  // Close the selected position 

               }
            }
         }
      }
 }




Another example:


//+------------------------------------------------------------------+
//| Close all positions                                              |
//+------------------------------------------------------------------+
void CloseAllPositions()
  {
   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()==m_magic)
            m_trade.PositionClose(m_position.Ticket()); // close a position by the specified symbol
  }
mohsen bahrami  
rrocchi:

First you have to select a position to do something with it.


use m_position.SelectByIndex() function


Example1:



Another example:


Hi Dear friend.

Thanks alot.

mahdi karimi  

how can i close sell position and open buy position?

example:

/ 0 - MAIN_LINE, 1 - SIGNAL_LINE.

   double ma_ema[],ma_lwma[],wpr[];

   ArraySetAsSeries(ma_ema,true);

   ArraySetAsSeries(ma_lwma,true);

   ArraySetAsSeries(wpr,true);


   int buffer=0,start_pos=0,count=3;

   if(!iGetArray(handle_iMA_EMA,0,start_pos,count,ma_ema) || 

      !iGetArray(handle_iMA_LWMA,0,start_pos,count,ma_lwma) || 

      !iGetArray(handle_iWPR,0,start_pos,count,wpr))

     {

      PrevBars=0; return;

     }

   if(ma_ema[0]<ma_lwma[0] && ma_ema[1]>ma_lwma[1] &&wpr[1]<wpr[0]>=50.0) //Buy

     {

     m_need_open_buy=true;

I want to close sell position by openiing this position

Keith Watford  
mahdi karimi:

how can i close sell position and open buy position?

I want to close sell position by openiing this position

Please edityour post and use the code button (Alt+S) to post your code.

Reason: