
- www.mql5.com
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:
Closes a position for the specified symbol | |
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
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
Vladimir Karputov, 2017.06.07 14:03
Use the trade class CTrade - it's very simple:
Closes a position for the specified symbol | |
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
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
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:
Gets the price of position's Stop Loss | |
Gets the price of position's Take Profit | |
Gets the amount of current profit by position |
Modifying a position - this requires a class CTrade:
Modifies position parameters by the specified symbol or position ticket |
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

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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