How Do i Use PositionClose()

To add comments, please log in or register
mohsen bahrami
344
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;
  }
  
To add comments, please log in or register