I propose a solution in the form of a script (a script is a one-time program).
The code:
//+------------------------------------------------------------------+ //| Full or partial closure.mq5 | //| Copyright © 2022, Vladimir Karputov | //| https://www.mql5.com/en/users/barabashkakvn | //+------------------------------------------------------------------+ #property copyright "Copyright © 2022, Vladimir Karputov" #property link "https://www.mql5.com/en/users/barabashkakvn" #property version "1.001" //--- #include <Trade\PositionInfo.mqh> #include <Trade\Trade.mqh> CPositionInfo m_position; // trade position object CTrade m_trade; // trading object //--- #property script_show_inputs //+------------------------------------------------------------------+ //| Enum Full Or Partial | //+------------------------------------------------------------------+ enum ENUM_FULL_OR_PARTIAL { full=0, // Full partial=1, // Partial }; //--- input parameters input string InpSymbol = "EURUSD"; // Close positions 'Symbol' input ulong InpMagic = 200; // Close positions 'Magic number' input ENUM_FULL_OR_PARTIAL InpFullOrPartial = full; // Close ... input double InpFullLots = 0.6; // Full lots input double InpPartialLots = 0.2; // Partial lots input ulong InpMagicEA = 379013688; // 'Full or partial closure' 'Magic number' //+------------------------------------------------------------------+ //| Script program start function | //+------------------------------------------------------------------+ void OnStart() { //--- m_trade.SetExpertMagicNumber(InpMagic); m_trade.SetMarginMode(); m_trade.SetTypeFillingBySymbol(Symbol()); //--- 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()==InpSymbol && m_position.Magic()==InpMagic) { if(InpFullOrPartial==full) { if(CompareDoubles(m_position.Volume(),InpFullLots)) if(!m_trade.PositionClose(m_position.Ticket())) // close a position by the specified m_symbol Print(__FILE__," ",__FUNCTION__,", ERROR Close ... Full: ","CTrade.PositionClose ",m_position.Ticket()); } else { if(m_position.Volume()>InpPartialLots) if(!m_trade.PositionClosePartial(m_position.Ticket(),InpPartialLots)) // close a position by the specified m_symbol Print(__FILE__," ",__FUNCTION__,", ERROR Close ... Partial: ","CTrade.PositionClose ",m_position.Ticket()); } } } //+------------------------------------------------------------------+ //| Compare doubles | //+------------------------------------------------------------------+ bool CompareDoubles(double number1,double number2) { if(NormalizeDouble(number1-number2,8)==0) return(true); else return(false); } //+------------------------------------------------------------------+
Files:

You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
I am trying to figure out how two different conditions will close partial or full a position.
k if true will close full being Lots the size of the operation, kp if true will close partially the operation in Small_lots size. but if I am able to close partial the expert gets blocked, I appreciate if someone is able to help me, I provide my thought code but still not work.