Half Close Position Script

 

Hello, I coded a script for Metatrader 5. I'm not getting a compilation error but I'm getting a warning I need help.

//+------------------------------------------------------------------+
//|                                                     CloseHalf.mq5|
//|                                      Copyright 2021, Your Name  |
//|                                             https://www.example.com |
//+------------------------------------------------------------------+
#property copyright "Sinan"
#property link      "-"
#property version   "1.00"
#property description "Tek tuşla açık olan işlemin yarısını kapatan script kodu"
#property strict

#include <Trade\PositionInfo.mqh>
#include <Trade\Trade.mqh>

CPositionInfo  m_position;                   // trade position object
CTrade         m_trade;                      // trading object

input double InpProfitPercentage = 50.0;     // Close half if Profit Percentage >= 50% of Take Profit

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
{
   if (ProfitPercentageAllPositions() >= InpProfitPercentage)
   {
      if (IsPositionExists())
         CloseHalfPositions();
   }
}

//+------------------------------------------------------------------+
//| Close half of all positions                                      |
//+------------------------------------------------------------------+
void CloseHalfPositions()
{
   for (int i = PositionsTotal() - 1; i >= 0; i--)
   {
      if (m_position.SelectByIndex(i))
      {
         double volume = m_position.Volume() / 2.0;
         ulong roundedVolume = (ulong)MathRound(volume);
         m_trade.PositionClose(m_position.Ticket(), roundedVolume);
      }
   }
}

//+------------------------------------------------------------------+
//| Check if any position exists                                     |
//+------------------------------------------------------------------+
bool IsPositionExists()
{
   for (int i = PositionsTotal() - 1; i >= 0; i--)
   {
      if (m_position.SelectByIndex(i)) 
         return true;
   }
   return false;
}

//+------------------------------------------------------------------+
//| Calculate profit percentage for all positions                    |
//+------------------------------------------------------------------+
double ProfitPercentageAllPositions()
{
   double totalTakeProfit = 0.0;
   double totalProfit = 0.0;

   for (int i = PositionsTotal() - 1; i >= 0; i--)
   {
      if (m_position.SelectByIndex(i))
      {
         totalTakeProfit += m_position.TakeProfit();
         totalProfit += m_position.Profit();
      }
   }

   if (totalTakeProfit > 0.0)
      return (totalProfit / totalTakeProfit) * 100.0;

   return 0.0;
}
2024.02.05 17:27:04.464 Scripts script Yarısını Kapat (USDJPY,H1) removed 
 
         double volume = m_position.Volume() / 2.0;
         ulong roundedVolume = (ulong)MathRound(volume);

You can't just use OrderLots()/2 because that is not a multiple of LotStep, and you can't close or have remaining less than MinLot.

 
Your topic has been moved to the section: Expert Advisors and Automated Trading
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 
William Roeder #: You can't just use OrderLots()/2 because that is not a multiple of LotStep, and you can't close or have remaining less than MinLot.
First of all thank you , I edited the codes again.
double tempRoundedVolume = MathRound(halfVolume / lotStep);
ulong roundedVolume = (ulong)tempRoundedVolume * (ulong)lotStep;

but I keep getting the same warning.

2024.02.05 19:12:02.715 Scripts script Yarısını Kapat (USDJPY,H1) loaded successfully 

after

2024.02.05 19:12:02.733 Scripts script Yarısını Kapat (USDJPY,H1) removed

Reason: