The correct example is:
//+------------------------------------------------------------------+ //| Close Positions Current Symbol.mq5 | //| Copyright © 2020, Vladimir Karputov | //+------------------------------------------------------------------+ #property copyright "Copyright © 2020, Vladimir Karputov" #property version "1.000" #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 positions if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties if(m_position.Symbol()==Symbol()) { if(!m_trade.PositionClose(m_position.Ticket())) // close a position by the specified m_symbol Print(__FILE__," ",__FUNCTION__,", ERROR: ","CTrade.PositionClose ",m_position.Ticket()); } } //+------------------------------------------------------------------+
The correct example is:
but I just want to close one type of positions buy or sell
how can I do it before it
but I just want to close one type of positions buy or sell
how can I do it before it
Code (very simplified code) :
//+------------------------------------------------------------------+ //| Close positions | //+------------------------------------------------------------------+ void ClosePositions(const ENUM_POSITION_TYPE pos_type) { 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()==Symbol()) if(m_position.PositionType()==pos_type) { if(m_position.PositionType()==POSITION_TYPE_BUY) if(!m_trade.PositionClose(m_position.Ticket())) // close a position by the specified m_symbol Print(__FILE__," ",__FUNCTION__,", ERROR: ","CTrade.PositionClose ",m_position.Ticket()); if(m_position.PositionType()==POSITION_TYPE_SELL) if(!m_trade.PositionClose(m_position.Ticket())) // close a position by the specified m_symbol Print(__FILE__," ",__FUNCTION__,", ERROR: ","CTrade.PositionClose ",m_position.Ticket()); } }
you are great man, you made me rich
yeeey.
Code (very simplified code) :
one last question, how can I modify the position for further continuing. How can I modify the position's SL and TP
I meant how can I take some little risk to Trail my takeprofit little bit further.
one last u
one last question, how can I modify the position for further continuing. How can I modify the position's SL and TP
I meant how can I take some little risk to Trail my takeprofit little bit further.
Maybe you should try to read the documentation ?
Have you understood why your initial code what not working properly ?

- www.mql5.com
Maybe you should try to read the documentation ?
Have you understood why your initial code what not working properly ?
Only difference is seemed to me as PositionInfo.mqh
Only difference is seemed to me as PositionInfo.mqh
CPositionInfo shopping class allows you to write simple and understandable code.
Let's step by step this code:
1. for(int i=PositionsTotal()-1; i>=0; i--) // returns the number of current positions 2. if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties 3. if(m_position.Symbol()==Symbol()) { 4. if(!m_trade.PositionClose(m_position.Ticket())) // close a position by the specified m_symbol Print(__FILE__," ",__FUNCTION__,", ERROR: ","CTrade.PositionClose ",m_position.Ticket()); }
1 -> passage through the list of all positions TO ZERO
2. -> SelectByIndex (Selects the position by index for further access to its properties)
3. -> compare position symbol and current symbol
4. -> PositionClose (closes a position with the specified ticket)

- www.mql5.com

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hİ all
Both close function is not working for me
the code is below,
do you see the reason for it?
trade.PositionClose(PositionGetSymbol(i)); trade.PositionClose(ticket);