Hi all
the documentation mentions PositionClose() as part of the class CTrade.
I am directly manipulating the orders, not using class.
How can I close positions?
Current code (DOES NOT WORK):
EDIT: I saw multiple similar questions and the answer is always to "use class" but I am not familiar with class and method coding, so I would rather do it directly the old fashioned way. Should I bite the bullet and just start using classes to learn how to?
//////////// /+------------------------------------------------------------------+ //| SembolHepsiniKapat.mq5 | //| Copyright 2018, MetaQuotes Software Corp. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2018, MetaQuotes Software Corp." #property link "https://www.mql5.com" #property version "1.00" #include <trade/trade.mqh> void OnStart() { CTrade trade; int i=PositionsTotal()-1; while (i>=0) { if (trade.PositionClose(PositionGetTicket(i))) Print("Kapatıldı"); } i--; }
Thanks.
That and some other post from "@Vladimir Karputov" shown me the way. Cheers
Hi all
the documentation mentions PositionClose() as part of the class CTrade.
I am directly manipulating the orders, not using class.
How can I close positions?
Current code (DOES NOT WORK):
ulong ticket;
EDIT: I saw multiple similar questions and the answer is always to "use class" but I am not familiar with class and method coding, so I would rather do it directly the old fashioned way. Should I bite the bullet and just start using classes to learn how to?
Hope this helps:
CTrade trade; if(PositionsTotal()>0){ for(i=0;i<PositionsTotal();i++){ ulong iTicket=PositionGetTicket(i); if(!trade.PositionClose(iTicket,ULONG_MAX)){ Print("PositionClose error ",trade.ResultRetcode()); return; } } }
Your code is wrong. Deleting positions must be carried out TO ZERO! Example:
//+------------------------------------------------------------------+ //| Close all positions | //+------------------------------------------------------------------+ void CloseAllPositions(void) { 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()==InpMagic) { if(m_position.PositionType()==POSITION_TYPE_BUY) { if(!m_trade.PositionClose(m_position.Ticket())) // close a position by the specified m_symbol if(InpPrintLog) Print(__FILE__," ",__FUNCTION__,", ERROR: ","BUY PositionClose ",m_position.Ticket(),", ",m_trade.ResultRetcodeDescription()); } if(m_position.PositionType()==POSITION_TYPE_SELL) { if(!m_trade.PositionClose(m_position.Ticket())) // close a position by the specified m_symbol if(InpPrintLog) Print(__FILE__," ",__FUNCTION__,", ERROR: ","SELL PositionClose ",m_position.Ticket(),", ",m_trade.ResultRetcodeDescription()); } } }
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi all
the documentation mentions PositionClose() as part of the class CTrade.
I am directly manipulating the orders, not using class.
How can I close positions?
Current code (DOES NOT WORK):
ulong ticket;
EDIT: I saw multiple similar questions and the answer is always to "use class" but I am not familiar with class and method coding, so I would rather do it directly the old fashioned way. Should I bite the bullet and just start using classes to learn how to?