how to close a position

 

Hello,

how to close an opened position?

For example, I opened a position in this manner:

request.type=ORDER_TYPE_SELL;
request.price=SymbolInfoDouble(Symbol(),SYMBOL_BID);   
request.symbol=Symbol();
request.volume=0.1;
request.type_filling=ORDER_FILLING_FOK;
request.sl=NormalizeDouble(stop_loss,Digits());
request.tp=NormalizeDouble(take_profit,Digits());
OrderCheck(request,resultCheck);
OrderSend(request,result);

How to close it?

I'm using metatrader 5.


Thanks

 
Documentation on MQL5: Standard Constants, Enumerations and Structures / Trade Constants / Trade Operation Types
Documentation on MQL5: Standard Constants, Enumerations and Structures / Trade Constants / Trade Operation Types
  • www.mql5.com
Standard Constants, Enumerations and Structures / Trade Constants / Trade Operation Types - Reference on algorithmic/automated trading language for MetaTrader 5
 
managertop40 :

Hello,

how to close an opened position?

For example, I opened a position in this manner:

How to close it?

I'm using metatrader 5.


Thanks


Use the trade class CTrade - it's very simple: 

PositionClose

Closes a position for the specified symbol

PositionClosePartial

Partially closes a position on a specified symbol or having a specified ticket

 

My problem is to get control of the opened position.

If I have just one, I use this code but doesn't work:

       ZeroMemory(request);
       ZeroMemory(result);
       request.position=PositionGetTicket(1);
       request.position_by=PositionGetInteger(POSITION_TICKET);
       request.action=TRADE_ACTION_DEAL;
       request.symbol=Symbol();
       request.volume=0.1;     
       request.price=SymbolInfoDouble(Symbol(),SYMBOL_ASK);
       request.type=ORDER_TYPE_BUY;
       OrderSend(request,result); 

It opens another one without closing the previous one. Why?

It seems I don't have control on open position. How to do that?

What number to give to this function?

request.position=PositionGetTicket(1);


Thanks

 
managertop40:

My problem is to get control of the opened position.

If I have just one, I use this code but doesn't work:

It opens another one without closing the previous one. Why?

It seems I don't have control on open position. How to do that?

What number to give to this function?


Thanks


Forum on trading, automated trading systems and testing trading strategies

how to close a position

Vladimir Karputov, 2017.06.07 14:03


Use the trade class CTrade - it's very simple: 

PositionClose

Closes a position for the specified symbol

PositionClosePartial

Partially closes a position on a specified symbol or having a specified ticket


 

In the case you showed, it seems I have to use a class.

Could you explain it better with a simple example, I to use it in my code?

I did:

#include <Trade\Trade.mqh>


[...all other lines...]

class CTrade : public managePosition;

[...all other lines...]

managePosition.PositionClose(Symbol(),ULONG_MAX);

but it returns errors. What is the correct way to use it?


Thanks

 
managertop40 :

In the case you showed, it seems I have to use a class.

Could you explain it better with a simple example, I to use it in my code?

I did:

but it returns errors. What is the correct way to use it?


Thanks


Script "CloseAllPositions" -  example class CTrade, method PositionClose (Closes a position with the specified ticket):

//+------------------------------------------------------------------+
//|                                            CloseAllPositions.mq5 |
//|                        Copyright 2016, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#include <Trade\PositionInfo.mqh>
#include <Trade\Trade.mqh>
CPositionInfo  m_position;                   // trade position object
CTrade         m_trade;                      // trading object
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   for(int i=PositionsTotal()-1;i>=0;i--) // returns the number of current position
      if(m_position.SelectByIndex(i))     // selects the position by index for further access to its properties
         if(m_position.Symbol()==Symbol())
            m_trade.PositionClose(m_position.Ticket()); // close a position by the specified symbol
  }
//+------------------------------------------------------------------+
 

Here's a slightly different example: closing positions on the selected symbol ("EURUSD") and the selected magic (15489):

//+------------------------------------------------------------------+
//|                                            CloseAllPositions.mq5 |
//|                        Copyright 2016, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#include <Trade\PositionInfo.mqh>
#include <Trade\Trade.mqh>
CPositionInfo  m_position;                   // trade position object
CTrade         m_trade;                      // trading object
#property script_show_inputs
string         m_name="EURUSD";              // symbol
ulong          m_magic=15489;                // magic number
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   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_name && m_position.Magic()==m_magic)
            m_trade.PositionClose(m_position.Ticket()); // close a position by the specified symbol
  }
//+------------------------------------------------------------------+
 

Ok, I tried the first solution and it works.

But how to change stop loss or take profit value of an opened position?


Thanks again

 
managertop40:

Ok, I tried the first solution and it works.

But how to change stop loss or take profit value of an opened position?


Thanks again


Class CPositionInfo

StopLoss

Gets the price of position's Stop Loss

TakeProfit

Gets the price of position's Take Profit

Profit

Gets the amount of current profit by position


Modifying a position - this requires a class CTrade:

PositionModify

Modifies position parameters by the specified symbol or position ticket

 
Vladimir Karputov:

Here's a slightly different example: closing positions on the selected symbol ("EURUSD") and the selected magic (15489):Hi friend , please help me an example of entering the function close all Friday, hour and minute, i attech file , thanks

Files:
Reason: