Skip to content

Commit

Permalink
1.121
Browse files Browse the repository at this point in the history
1. Fixed a potential division by zero error.
  • Loading branch information
EarnForex authored Jan 26, 2025
1 parent 6010178 commit 82624e1
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
4 changes: 2 additions & 2 deletions MQL4/Experts/Account Protector/Account Protector.mq4
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
//+------------------------------------------------------------------+
#property copyright "EarnForex.com"
#property link "https://www.earnforex.com/metatrader-expert-advisors/Account-Protector/"
#property version "1.12"
string Version = "1.12";
#property version "1.121"
string Version = "1.121";
#property strict

#property description "Protects account balance by applying given actions when set conditions trigger."
Expand Down
8 changes: 6 additions & 2 deletions MQL4/Experts/Account Protector/Account Protector.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -1331,8 +1331,11 @@ void CAccountProtector::CalculateDailyProfitLossAndStartingBalance()

if ((OrderType() == OP_BUY) || (OrderType() == OP_SELL))
{
if (OrderType() == OP_BUY) FloatingProfitPoints += (int)MathRound((MarketInfo(OrderSymbol(), MODE_BID) - OrderOpenPrice()) / MarketInfo(OrderSymbol(), MODE_POINT));
else if (OrderType() == OP_SELL) FloatingProfitPoints += (int)MathRound((OrderOpenPrice() - MarketInfo(OrderSymbol(), MODE_ASK)) / MarketInfo(OrderSymbol(), MODE_POINT));
if (MarketInfo(OrderSymbol(), MODE_POINT) != 0)
{
if (OrderType() == OP_BUY) FloatingProfitPoints += (int)MathRound((MarketInfo(OrderSymbol(), MODE_BID) - OrderOpenPrice()) / MarketInfo(OrderSymbol(), MODE_POINT));
else if (OrderType() == OP_SELL) FloatingProfitPoints += (int)MathRound((OrderOpenPrice() - MarketInfo(OrderSymbol(), MODE_ASK)) / MarketInfo(OrderSymbol(), MODE_POINT));
}
NumberOfMarketOrders++;
}
else if ((OrderType() == OP_BUYSTOP) || (OrderType() == OP_SELLSTOP) || (OrderType() == OP_BUYLIMIT) || (OrderType() == OP_SELLLIMIT)) NumberOfPendingOrders++;
Expand Down Expand Up @@ -1377,6 +1380,7 @@ void CAccountProtector::CalculateDailyProfitLossAndStartingBalance()
realized_daily_profit_loss_units += OrderProfit();
if (sets.CountCommSwaps) realized_daily_profit_loss_units += OrderCommission() + OrderSwap();

if (MarketInfo(OrderSymbol(), MODE_POINT) == 0) break; // Avoid division by zero.
if (OrderType() == OP_BUY) realized_daily_profit_loss_points += (int)MathRound((OrderClosePrice() - OrderOpenPrice()) / MarketInfo(OrderSymbol(), MODE_POINT));
else if (OrderType() == OP_SELL) realized_daily_profit_loss_points += (int)MathRound((OrderOpenPrice() - OrderClosePrice()) / MarketInfo(OrderSymbol(), MODE_POINT));
break; // Order already processed - no point to process this order with other magic numbers.
Expand Down
4 changes: 2 additions & 2 deletions MQL5/Experts/Account Protector/Account Protector.mq5
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
//+------------------------------------------------------------------+
#property copyright "EarnForex.com"
#property link "https://www.earnforex.com/metatrader-expert-advisors/Account-Protector/"
#property version "1.12"
string Version = "1.12";
#property version "1.121"
string Version = "1.121";
#property strict

#property description "Protects account balance by applying given actions when set conditions trigger."
Expand Down
18 changes: 11 additions & 7 deletions MQL5/Experts/Account Protector/Account Protector.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -1345,8 +1345,11 @@ void CAccountProtector::CalculateDailyProfitLossAndStartingBalance()
FloatingProfit += PositionGetDouble(POSITION_PROFIT);
if (sets.CountCommSwaps) FloatingProfit += HistoryDealGetDouble(ticket, DEAL_COMMISSION) + PositionGetDouble(POSITION_SWAP);

if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY) FloatingProfitPoints += (int)MathRound((SymbolInfoDouble(PositionGetString(POSITION_SYMBOL), SYMBOL_BID) - PositionGetDouble(POSITION_PRICE_OPEN)) / SymbolInfoDouble(PositionGetString(POSITION_SYMBOL), SYMBOL_POINT));
else if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL) FloatingProfitPoints += (int)MathRound((PositionGetDouble(POSITION_PRICE_OPEN) - SymbolInfoDouble(PositionGetString(POSITION_SYMBOL), SYMBOL_ASK)) / SymbolInfoDouble(PositionGetString(POSITION_SYMBOL), SYMBOL_POINT));
if (SymbolInfoDouble(PositionGetString(POSITION_SYMBOL), SYMBOL_POINT) != 0)
{
if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY) FloatingProfitPoints += (int)MathRound((SymbolInfoDouble(PositionGetString(POSITION_SYMBOL), SYMBOL_BID) - PositionGetDouble(POSITION_PRICE_OPEN)) / SymbolInfoDouble(PositionGetString(POSITION_SYMBOL), SYMBOL_POINT));
else if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL) FloatingProfitPoints += (int)MathRound((PositionGetDouble(POSITION_PRICE_OPEN) - SymbolInfoDouble(PositionGetString(POSITION_SYMBOL), SYMBOL_ASK)) / SymbolInfoDouble(PositionGetString(POSITION_SYMBOL), SYMBOL_POINT));
}
NumberOfMarketOrders++;
break; // Position already processed - no point to process this order with other magic numbers.
}
Expand Down Expand Up @@ -1452,8 +1455,11 @@ void CAccountProtector::CalculateDailyProfitLossAndStartingBalance()
double deal_entry_price = HistoryDealGetDouble(entry_deal_ticket, DEAL_PRICE);

// This is a very crude and imprecise method when used on a Netting account, but calculating points profit/loss across uninformly sized positions is crazy to start with.
if (HistoryDealGetInteger(entry_deal_ticket, DEAL_TYPE) == DEAL_TYPE_BUY) realized_daily_profit_loss_points += (int)MathRound((exit_deal_exit_prices[i] - deal_entry_price) / SymbolInfoDouble(HistoryDealGetString(entry_deal_ticket, DEAL_SYMBOL), SYMBOL_POINT));
else if (HistoryDealGetInteger(entry_deal_ticket, DEAL_TYPE) == DEAL_TYPE_SELL) realized_daily_profit_loss_points += (int)MathRound((deal_entry_price - exit_deal_exit_prices[i]) / SymbolInfoDouble(HistoryDealGetString(entry_deal_ticket, DEAL_SYMBOL), SYMBOL_POINT));
if (SymbolInfoDouble(HistoryDealGetString(entry_deal_ticket, DEAL_SYMBOL), SYMBOL_POINT) != 0)
{
if (HistoryDealGetInteger(entry_deal_ticket, DEAL_TYPE) == DEAL_TYPE_BUY) realized_daily_profit_loss_points += (int)MathRound((exit_deal_exit_prices[i] - deal_entry_price) / SymbolInfoDouble(HistoryDealGetString(entry_deal_ticket, DEAL_SYMBOL), SYMBOL_POINT));
else if (HistoryDealGetInteger(entry_deal_ticket, DEAL_TYPE) == DEAL_TYPE_SELL) realized_daily_profit_loss_points += (int)MathRound((deal_entry_price - exit_deal_exit_prices[i]) / SymbolInfoDouble(HistoryDealGetString(entry_deal_ticket, DEAL_SYMBOL), SYMBOL_POINT));
}
break; // Move on to the next exit deal.
}
}
Expand All @@ -1467,8 +1473,6 @@ void CAccountProtector::CalculateDailyProfitLossAndStartingBalance()
DailyProfitLossPoints += FloatingProfitPoints;
}
// Percentage of balance at the start of the day calculated by subtracting the current daily profit from the current balance.
//Print("daily_profit_loss_units = ", daily_profit_loss_units);
//Print("prev balance = ", AccountInfoDouble(ACCOUNT_BALANCE) - realized_daily_profit_loss_units);
DailyStartingBalance = AccountInfoDouble(ACCOUNT_BALANCE) - realized_daily_profit_loss_units;
if ((!DisableDailyProfitLossPercGE) || (!DisableDailyProfitLossPercLE))
{
Expand Down Expand Up @@ -5312,7 +5316,7 @@ void CAccountProtector::Logging_Condition_Is_Met()
}
ArrayResize(PositionsByProfit, market, 100); // Reserve extra physical memory to increase the resizing speed.
if ((CloseFirst != ENUM_CLOSE_TRADES_MOST_DISTANT_FIRST) && (CloseFirst != ENUM_CLOSE_TRADES_NEAREST_FIRST)) PositionsByProfit[market - 1][0] = position_floating_profit; // Normal profit.
else PositionsByProfit[market - 1][0] = MathAbs(PositionGetDouble(POSITION_PRICE_OPEN) - PositionGetDouble(POSITION_PRICE_CURRENT)) / SymbolInfoDouble(PositionGetString(POSITION_SYMBOL), SYMBOL_POINT);
else if (SymbolInfoDouble(PositionGetString(POSITION_SYMBOL), SYMBOL_POINT) != 0) PositionsByProfit[market - 1][0] = MathAbs(PositionGetDouble(POSITION_PRICE_OPEN) - PositionGetDouble(POSITION_PRICE_CURRENT)) / SymbolInfoDouble(PositionGetString(POSITION_SYMBOL), SYMBOL_POINT);
PositionsByProfit[market - 1][1] = (double)ticket;
TotalVolume += PositionGetDouble(POSITION_VOLUME); // Used to close trades, so no need to filter by P/L.
}
Expand Down

0 comments on commit 82624e1

Please sign in to comment.