Topic: Modification of the function Max_OpenPos = 1; // Max open positions (c
The main steps are:
Modify Position Counting: Change the function that counts positions to consider all open positions in the account, regardless of magic number or symbol.
Update Position Opening Check: Ensure that before opening a new position, the EA checks if there is already at least one open position across the entire account.
Maintain Compatibility with Other Protections: Ensure that other existing protections in the EA continue to work as expected.
Modify the Global Position Counting Function
int CountPositions(void)
{
return PositionsTotal();
}
Update the SetPosStats Function to Reflect Global Counting
void SetPosStats(void)
{
posStatCount = CountPositions();
posStatLots = 0;
posStatProfit = 0;
for(int i = PositionsTotal() - 1; i >= 0; i -= 1)
{
const ulong ticket = PositionGetTicket(i);
if(ticket == 0 || !PositionSelectByTicket(ticket))
continue;
posStatLots += PositionGetDouble(POSITION_VOLUME);
posStatProfit += PositionGetDouble(POSITION_PROFIT) +
PositionGetDouble(POSITION_SWAP);
}
}
Update the Check in OpenPosition
void OpenPosition(Signal &signal)
{
entryProtectionMessage = "";
const int spread = (int)((Ask() - Bid()) / _Point);
// Verificações de proteção de entrada
if (Max_OpenPos > sigma && posStatCount >= Max_OpenPos)
entryProtectionMessage += StringFormat("Protection: Max open positions: %d, current: %d\n",
Max_OpenPos, posStatCount);
if (Max_OpenLots > sigma && posStatLots > Max_OpenLots - sigma)
entryProtectionMessage += StringFormat("Protection: Max open lots: %.2f, current: %.2f\n",
Max_OpenLots, posStatLots);
if (Max_Spread > sigma && spread > Max_Spread)
entryProtectionMessage += StringFormat("Protection: Max spread: %d, current: %d\n",
Max_Spread, spread);
if (MaxDailyLoss > sigma && dailyLoss >= MaxDailyLoss)
entryProtectionMessage += StringFormat("Protection: Max daily loss: %d, current: %.2f\n",
MaxDailyLoss, dailyLoss);
if (Max_Daily_DD > sigma && dailyDrawdown >= Max_Daily_DD)
entryProtectionMessage += StringFormat("Protection: Max daily drawdown: %.2f%%, current: %.2f%%\n",
Max_Daily_DD, dailyDrawdown);
if (GlobalVariableGet(accEntrySuspendGlobalVarName) > sigma)
entryProtectionMessage += StringFormat("New entries are suspended until the Daily reset hour: %d",
Daily_Reset);
const int newsIndex = NewsFilterActive();
if (newsIndex > -1)
{
const NewsRecord newsRecord = newsRecords[newsIndex];
const datetime timeShift = (datetime) MathRound((TimeLocal() - TimeGMT()) / 3600.0) * 3600;
const string priority = newsRecord.priority == "high" ? "[high]" : "[med]";
entryProtectionMessage += StringFormat("News filter: %s %s %s %s\n",
priority,
TimeToString(newsRecord.time + timeShift,
TIME_DATE | TIME_MINUTES),
newsRecord.currency,
newsRecord.title);
}
if (entryProtectionMessage != "")
{
entryProtectionMessage = TimeToString(TimeCurrent()) + " " +
"An entry order was canceled:\n" +
entryProtectionMessage;
return;
}
// Verifica a direção de negociação antes de executar a ordem
const int command = OrderDirectionToCommand(signal.Direction);
// Verifica se a direção da negociação é permitida
if ((tradeDirection == SomenteCompra && command != OP_BUY) ||
(tradeDirection == SomenteVenda && command != OP_SELL))
{
// Não prossegue se a direção da negociação não corresponde
return;
}
const double stopLoss = GetStopLossPrice(command, signal.StopLossPips);
const double takeProfit = GetTakeProfitPrice(command, signal.TakeProfitPips);
const double posLots = Entry_Amount;
if (ManageOrderSend(command, posLots, stopLoss, takeProfit, 0, signal.MagicNumber))
{
posStatCount += 1;
posStatLots += posLots;
}
}
Update the OnTick Function to Reflect the Changes
void OnTick(void)
{
if(!MQLInfoInteger(MQL_TESTER))
{
UpdateAccountProtection();
CheckAccountProtection();
const datetime time = TimeCurrent();
if(time > lastStatsUpdate + 3)
{
lastStatsUpdate = time;
SetPosStats(); // Agora considera todas as posições
UpdateStats();
}
if(time > lastNewsUpdate + 6*60*60 || !isNewsFeedOk)
{
lastNewsUpdate = time;
LoadNews();
}
}
if(IsForceSessionClose())
{
CloseAllPositions();
return;
}
const datetime time = Time(0);
if(time > barTime)
{
barTime = time;
OnBar();
}
}