Skip to content

Commit 82624e1

Browse files
author
EarnForex
authored
1.121
1. Fixed a potential division by zero error.
1 parent 6010178 commit 82624e1

File tree

4 files changed

+21
-13
lines changed

4 files changed

+21
-13
lines changed

MQL4/Experts/Account Protector/Account Protector.mq4

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
//+------------------------------------------------------------------+
66
#property copyright "EarnForex.com"
77
#property link "https://www.earnforex.com/metatrader-expert-advisors/Account-Protector/"
8-
#property version "1.12"
9-
string Version = "1.12";
8+
#property version "1.121"
9+
string Version = "1.121";
1010
#property strict
1111

1212
#property description "Protects account balance by applying given actions when set conditions trigger."

MQL4/Experts/Account Protector/Account Protector.mqh

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,8 +1331,11 @@ void CAccountProtector::CalculateDailyProfitLossAndStartingBalance()
13311331

13321332
if ((OrderType() == OP_BUY) || (OrderType() == OP_SELL))
13331333
{
1334-
if (OrderType() == OP_BUY) FloatingProfitPoints += (int)MathRound((MarketInfo(OrderSymbol(), MODE_BID) - OrderOpenPrice()) / MarketInfo(OrderSymbol(), MODE_POINT));
1335-
else if (OrderType() == OP_SELL) FloatingProfitPoints += (int)MathRound((OrderOpenPrice() - MarketInfo(OrderSymbol(), MODE_ASK)) / MarketInfo(OrderSymbol(), MODE_POINT));
1334+
if (MarketInfo(OrderSymbol(), MODE_POINT) != 0)
1335+
{
1336+
if (OrderType() == OP_BUY) FloatingProfitPoints += (int)MathRound((MarketInfo(OrderSymbol(), MODE_BID) - OrderOpenPrice()) / MarketInfo(OrderSymbol(), MODE_POINT));
1337+
else if (OrderType() == OP_SELL) FloatingProfitPoints += (int)MathRound((OrderOpenPrice() - MarketInfo(OrderSymbol(), MODE_ASK)) / MarketInfo(OrderSymbol(), MODE_POINT));
1338+
}
13361339
NumberOfMarketOrders++;
13371340
}
13381341
else if ((OrderType() == OP_BUYSTOP) || (OrderType() == OP_SELLSTOP) || (OrderType() == OP_BUYLIMIT) || (OrderType() == OP_SELLLIMIT)) NumberOfPendingOrders++;
@@ -1377,6 +1380,7 @@ void CAccountProtector::CalculateDailyProfitLossAndStartingBalance()
13771380
realized_daily_profit_loss_units += OrderProfit();
13781381
if (sets.CountCommSwaps) realized_daily_profit_loss_units += OrderCommission() + OrderSwap();
13791382

1383+
if (MarketInfo(OrderSymbol(), MODE_POINT) == 0) break; // Avoid division by zero.
13801384
if (OrderType() == OP_BUY) realized_daily_profit_loss_points += (int)MathRound((OrderClosePrice() - OrderOpenPrice()) / MarketInfo(OrderSymbol(), MODE_POINT));
13811385
else if (OrderType() == OP_SELL) realized_daily_profit_loss_points += (int)MathRound((OrderOpenPrice() - OrderClosePrice()) / MarketInfo(OrderSymbol(), MODE_POINT));
13821386
break; // Order already processed - no point to process this order with other magic numbers.

MQL5/Experts/Account Protector/Account Protector.mq5

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
//+------------------------------------------------------------------+
66
#property copyright "EarnForex.com"
77
#property link "https://www.earnforex.com/metatrader-expert-advisors/Account-Protector/"
8-
#property version "1.12"
9-
string Version = "1.12";
8+
#property version "1.121"
9+
string Version = "1.121";
1010
#property strict
1111

1212
#property description "Protects account balance by applying given actions when set conditions trigger."

MQL5/Experts/Account Protector/Account Protector.mqh

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,8 +1345,11 @@ void CAccountProtector::CalculateDailyProfitLossAndStartingBalance()
13451345
FloatingProfit += PositionGetDouble(POSITION_PROFIT);
13461346
if (sets.CountCommSwaps) FloatingProfit += HistoryDealGetDouble(ticket, DEAL_COMMISSION) + PositionGetDouble(POSITION_SWAP);
13471347

1348-
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));
1349-
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));
1348+
if (SymbolInfoDouble(PositionGetString(POSITION_SYMBOL), SYMBOL_POINT) != 0)
1349+
{
1350+
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));
1351+
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));
1352+
}
13501353
NumberOfMarketOrders++;
13511354
break; // Position already processed - no point to process this order with other magic numbers.
13521355
}
@@ -1452,8 +1455,11 @@ void CAccountProtector::CalculateDailyProfitLossAndStartingBalance()
14521455
double deal_entry_price = HistoryDealGetDouble(entry_deal_ticket, DEAL_PRICE);
14531456

14541457
// 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.
1455-
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));
1456-
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));
1458+
if (SymbolInfoDouble(HistoryDealGetString(entry_deal_ticket, DEAL_SYMBOL), SYMBOL_POINT) != 0)
1459+
{
1460+
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));
1461+
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));
1462+
}
14571463
break; // Move on to the next exit deal.
14581464
}
14591465
}
@@ -1467,8 +1473,6 @@ void CAccountProtector::CalculateDailyProfitLossAndStartingBalance()
14671473
DailyProfitLossPoints += FloatingProfitPoints;
14681474
}
14691475
// Percentage of balance at the start of the day calculated by subtracting the current daily profit from the current balance.
1470-
//Print("daily_profit_loss_units = ", daily_profit_loss_units);
1471-
//Print("prev balance = ", AccountInfoDouble(ACCOUNT_BALANCE) - realized_daily_profit_loss_units);
14721476
DailyStartingBalance = AccountInfoDouble(ACCOUNT_BALANCE) - realized_daily_profit_loss_units;
14731477
if ((!DisableDailyProfitLossPercGE) || (!DisableDailyProfitLossPercLE))
14741478
{
@@ -5312,7 +5316,7 @@ void CAccountProtector::Logging_Condition_Is_Met()
53125316
}
53135317
ArrayResize(PositionsByProfit, market, 100); // Reserve extra physical memory to increase the resizing speed.
53145318
if ((CloseFirst != ENUM_CLOSE_TRADES_MOST_DISTANT_FIRST) && (CloseFirst != ENUM_CLOSE_TRADES_NEAREST_FIRST)) PositionsByProfit[market - 1][0] = position_floating_profit; // Normal profit.
5315-
else PositionsByProfit[market - 1][0] = MathAbs(PositionGetDouble(POSITION_PRICE_OPEN) - PositionGetDouble(POSITION_PRICE_CURRENT)) / SymbolInfoDouble(PositionGetString(POSITION_SYMBOL), SYMBOL_POINT);
5319+
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);
53165320
PositionsByProfit[market - 1][1] = (double)ticket;
53175321
TotalVolume += PositionGetDouble(POSITION_VOLUME); // Used to close trades, so no need to filter by P/L.
53185322
}

0 commit comments

Comments
 (0)