Skip to content

Commit d82b6a5

Browse files
author
EarnForex
authored
1.14
1. Fixed a bug that could result in a flood of order modification requests to the trading server without actually modifying anything. 2. Fixed all double comparisons to avoid potential issues. 3. Fixed a minor bug in the MT5 version of the EA that could lead to a failure while updating a pending order.
1 parent d972da9 commit d82b6a5

2 files changed

Lines changed: 25 additions & 26 deletions

File tree

ChartPatternHelper.mq4

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
//+------------------------------------------------------------------+
22
//| Chart Pattern Helper |
3-
//| Copyright © 2013-2023, EarnForex.com |
3+
//| Copyright © 2024, EarnForex.com |
44
//| https://www.earnforex.com/ |
55
//+------------------------------------------------------------------+
6-
#property copyright "Copyright © 2013-2023, EarnForex"
6+
#property copyright "Copyright © 2024, EarnForex"
77
#property link "https://www.earnforex.com/metatrader-expert-advisors/ChartPatternHelper/"
8-
#property version "1.13"
8+
#property version "1.14"
99
#property strict
1010

1111
#include <stdlib.mqh>
@@ -609,7 +609,6 @@ double FindLowerTPViaChannel()
609609

610610
// Get the lower of main and auxiliary lines.
611611
TP = MathMin(ObjectGetValueByTime(0, TPChannel, Time[0], 0), ObjectGetValueByTime(0, TPChannel, Time[0], 1));
612-
613612
return NormalizeDouble(TP, _Digits);
614613
}
615614

@@ -822,7 +821,7 @@ void AdjustUpperAndLowerOrders()
822821
}
823822
// If volume needs to be updated - delete and recreate order with new volume.
824823
// Also check if EA will be able to create new pending order at current price.
825-
else if ((UpdatePendingVolume) && (NormalizeDouble(OrderLots(), LotStep_digits) != NormalizeDouble(NewVolume, LotStep_digits)))
824+
else if ((UpdatePendingVolume) && (MathAbs(OrderLots() - NewVolume) > LotStep / 2))
826825
{
827826
if ((UpperEntry - Ask > StopLevel) || (Ask - UpperEntry > StopLevel)) // Order can be re-created.
828827
{
@@ -867,8 +866,8 @@ void AdjustUpperAndLowerOrders()
867866
}
868867
continue;
869868
}
870-
// Otherwise update entry/SL/TP if at least one of them has changed.
871-
else if ((NormalizeDouble(OrderOpenPrice(), Digits) != NormalizeDouble(UpperEntry, Digits)) || (NormalizeDouble(OrderStopLoss(), Digits) != NormalizeDouble(UpperSL, Digits)) || (NormalizeDouble(OrderTakeProfit(), Digits) != NormalizeDouble(UpperTP, Digits)))
869+
// Otherwise, update entry/SL/TP if at least one of them has changed.
870+
else if ((MathAbs(OrderOpenPrice() - UpperEntry) > _Point / 2) || (MathAbs(OrderStopLoss() - UpperSL) > _Point / 2) || (MathAbs(OrderTakeProfit() - UpperTP) > _Point / 2))
872871
{
873872
// Avoid error 130 based on entry.
874873
if (UpperEntry - Ask > StopLevel) // Current price below entry.
@@ -879,7 +878,7 @@ void AdjustUpperAndLowerOrders()
879878
{
880879
order_type_string = "Limit";
881880
}
882-
else if ((UpperEntry != OrderOpenPrice())) continue;
881+
else if (MathAbs(OrderOpenPrice() - UpperEntry) > _Point / 2) continue;
883882
// Avoid error 130 based on stop-loss.
884883
if (UpperEntry - UpperSL <= StopLevel)
885884
{
@@ -949,7 +948,7 @@ void AdjustUpperAndLowerOrders()
949948
}
950949
}
951950
// Adjust TP only.
952-
if ((NormalizeDouble(OrderTakeProfit(), Digits) != NormalizeDouble(UpperTP, Digits)))
951+
if (MathAbs(OrderTakeProfit() - UpperTP) > _Point / 2)
953952
{
954953
// Avoid frozen context. In all modification cases.
955954
if ((FreezeLevel != 0) && (MathAbs(OrderOpenPrice() - Ask) <= FreezeLevel))
@@ -985,7 +984,7 @@ void AdjustUpperAndLowerOrders()
985984
}
986985
}
987986
// If volume needs to be updated - delete and recreate order with new volume. Also check if EA will be able to create new pending order at current price.
988-
else if ((UpdatePendingVolume) && (NormalizeDouble(OrderLots(), Digits) != NormalizeDouble(NewVolume, Digits)))
987+
else if ((UpdatePendingVolume) && (MathAbs(OrderLots() - NewVolume) > LotStep / 2))
989988
{
990989
if ((Bid - LowerEntry > StopLevel) || (LowerEntry - Bid > StopLevel)) // Order can be re-created
991990
{
@@ -1030,8 +1029,8 @@ void AdjustUpperAndLowerOrders()
10301029
}
10311030
continue;
10321031
}
1033-
// Otherwise just update what needs to be updated.
1034-
else if ((NormalizeDouble(OrderOpenPrice(), Digits) != NormalizeDouble(LowerEntry, Digits)) || (NormalizeDouble(OrderStopLoss(), Digits) != NormalizeDouble(LowerSL, Digits)) || (NormalizeDouble(OrderTakeProfit(), Digits) != NormalizeDouble(LowerTP, Digits)))
1032+
// Otherwise, just update what needs to be updated.
1033+
else if ((MathAbs(OrderOpenPrice() - LowerEntry) > _Point / 2) || (MathAbs(OrderStopLoss() - LowerSL) > _Point / 2) || (MathAbs(OrderTakeProfit() - LowerTP) > _Point / 2))
10351034
{
10361035
// Avoid error 130 based on entry.
10371036
if (Bid - LowerEntry > StopLevel) // Current price above entry.
@@ -1042,7 +1041,7 @@ void AdjustUpperAndLowerOrders()
10421041
{
10431042
order_type_string = "Limit";
10441043
}
1045-
else if (LowerEntry != OrderOpenPrice()) continue;
1044+
else if (MathAbs(OrderOpenPrice() - LowerEntry) > _Point / 2) continue;
10461045
// Avoid error 130 based on stop-loss.
10471046
if (LowerSL - LowerEntry <= StopLevel)
10481047
{
@@ -1112,7 +1111,7 @@ void AdjustUpperAndLowerOrders()
11121111
}
11131112
}
11141113
// Adjust TP only.
1115-
if ((NormalizeDouble(OrderTakeProfit(), Digits) != NormalizeDouble(LowerTP, Digits)))
1114+
if (MathAbs(OrderTakeProfit() - LowerTP) > _Point / 2)
11161115
{
11171116
// Avoid frozen context. In all modification cases.
11181117
if ((FreezeLevel != 0) && (MathAbs(Bid - OrderOpenPrice()) <= FreezeLevel))

ChartPatternHelper.mq5

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
//+------------------------------------------------------------------+
22
//| Chart Pattern Helper |
3-
//| Copyright © 2013-2023, EarnForex.com |
3+
//| Copyright © 2024, EarnForex.com |
44
//| https://www.earnforex.com/ |
55
//+------------------------------------------------------------------+
6-
#property copyright "Copyright © 2013-2023, EarnForex"
6+
#property copyright "Copyright © 2024, EarnForex"
77
#property link "https://www.earnforex.com/metatrader-expert-advisors/ChartPatternHelper/"
8-
#property version "1.13"
8+
#property version "1.14"
99

1010
#property description "Uses graphic objects (horizontal/trend lines, channels) to enter trades."
1111
#property description "Works in two modes:"
@@ -845,7 +845,7 @@ void AdjustUpperAndLowerOrders()
845845
{
846846
Output("Skipping Modify Buy TP because open price is too close to Ask. FreezeLevel = " + DoubleToString(FreezeLevel, _Digits) + " OpenPrice = " + DoubleToString(PositionGetDouble(POSITION_PRICE_OPEN), 8) + " Ask = " + DoubleToString(Ask, _Digits));
847847
}
848-
else if ((NormalizeDouble(PositionGetDouble(POSITION_TP), _Digits) != NormalizeDouble(UpperTP, _Digits)) && (UpperTP != 0))
848+
else if ((MathAbs(PositionGetDouble(POSITION_TP) - UpperTP) > _Point / 2) && (UpperTP != 0))
849849
{
850850
if (!Trade.PositionModify(_Symbol, PositionGetDouble(POSITION_SL), UpperTP))
851851
{
@@ -889,7 +889,7 @@ void AdjustUpperAndLowerOrders()
889889
{
890890
Output("Skipping Modify Sell TP because open price is too close to Bid. FreezeLevel = " + DoubleToString(FreezeLevel, _Digits) + " OpenPrice = " + DoubleToString(PositionGetDouble(POSITION_PRICE_OPEN), 8) + " Bid = " + DoubleToString(Bid, _Digits));
891891
}
892-
else if ((NormalizeDouble(PositionGetDouble(POSITION_TP), _Digits) != LowerTP) && (NormalizeDouble(LowerTP, _Digits) != 0))
892+
else if ((MathAbs(PositionGetDouble(POSITION_TP) - LowerTP) > _Point / 2) && (NormalizeDouble(LowerTP, _Digits) != 0))
893893
{
894894
if (!Trade.PositionModify(_Symbol, PositionGetDouble(POSITION_SL), LowerTP))
895895
{
@@ -917,7 +917,7 @@ void AdjustUpperAndLowerOrders()
917917
// Delete existing pending order
918918
if ((HaveBuy) || ((HaveSell) && (OneCancelsOther)) || (!UseUpper)) Trade.OrderDelete(ticket);
919919
// If volume needs to be updated - delete and recreate order with new volume. Also check if EA will be able to create new pending order at current price.
920-
else if ((UpdatePendingVolume) && (OrderGetDouble(ORDER_VOLUME_CURRENT) != NewVolume))
920+
else if ((UpdatePendingVolume) && (MathAbs(OrderGetDouble(ORDER_VOLUME_CURRENT) - NewVolume) > LotStep / 2))
921921
{
922922
if ((UpperEntry - Ask > StopLevel) || (Ask - UpperEntry > StopLevel)) // Order can be re-created
923923
{
@@ -965,7 +965,7 @@ void AdjustUpperAndLowerOrders()
965965
continue;
966966
}
967967
// Otherwise, just update what needs to be updated.
968-
else if ((NormalizeDouble(OrderGetDouble(ORDER_PRICE_OPEN), _Digits) != NormalizeDouble(UpperEntry, _Digits)) || (NormalizeDouble(OrderGetDouble(ORDER_SL), _Digits) != NormalizeDouble(UpperSL, _Digits)) || (NormalizeDouble(OrderGetDouble(ORDER_TP), _Digits) != NormalizeDouble(UpperTP, _Digits)))
968+
else if ((MathAbs(OrderGetDouble(ORDER_PRICE_OPEN) - UpperEntry) > _Point / 2) || (MathAbs(OrderGetDouble(ORDER_SL) - UpperSL) > _Point / 2) || (MathAbs(OrderGetDouble(ORDER_TP) - UpperTP) > _Point / 2))
969969
{
970970
// Avoid error 130 based on entry.
971971
if (UpperEntry - Ask > StopLevel) // Current price below entry
@@ -976,7 +976,7 @@ void AdjustUpperAndLowerOrders()
976976
{
977977
order_type_string = "Limit";
978978
}
979-
else if (NormalizeDouble(OrderGetDouble(ORDER_PRICE_OPEN), _Digits) != NormalizeDouble(LowerEntry, _Digits)) continue;
979+
else if (MathAbs(OrderGetDouble(ORDER_PRICE_OPEN) - UpperEntry) > _Point / 2) continue;
980980
// Avoid error 130 based on stop-loss.
981981
if (UpperEntry - UpperSL <= StopLevel)
982982
{
@@ -1016,7 +1016,7 @@ void AdjustUpperAndLowerOrders()
10161016
// Delete existing pending order.
10171017
if (((HaveBuy) && (OneCancelsOther)) || (HaveSell) || (!UseLower)) Trade.OrderDelete(ticket);
10181018
// If volume needs to be updated - delete and recreate order with new volume. Also check if EA will be able to create new pending order at current price.
1019-
else if ((UpdatePendingVolume) && (OrderGetDouble(ORDER_VOLUME_CURRENT) != NewVolume) && (Bid - LowerEntry > StopLevel))
1019+
else if ((UpdatePendingVolume) && (MathAbs(OrderGetDouble(ORDER_VOLUME_CURRENT) - NewVolume) > LotStep / 2) && (Bid - LowerEntry > StopLevel))
10201020
{
10211021
if ((Bid - LowerEntry > StopLevel) || (LowerEntry - Bid > StopLevel)) // Order can be re-created
10221022
{
@@ -1062,8 +1062,8 @@ void AdjustUpperAndLowerOrders()
10621062
}
10631063
continue;
10641064
}
1065-
// Otherwise just update what needs to be updated
1066-
else if ((NormalizeDouble(OrderGetDouble(ORDER_PRICE_OPEN), _Digits) != NormalizeDouble(LowerEntry, _Digits)) || (NormalizeDouble(OrderGetDouble(ORDER_SL), _Digits) != NormalizeDouble(LowerSL, _Digits)) || (NormalizeDouble(OrderGetDouble(ORDER_TP), _Digits) != NormalizeDouble(LowerTP, _Digits)))
1065+
// Otherwise, just update what needs to be updated
1066+
else if ((MathAbs(OrderGetDouble(ORDER_PRICE_OPEN) - LowerEntry) > _Point / 2) || (MathAbs(OrderGetDouble(ORDER_SL) - LowerSL) > _Point / 2) || (MathAbs(OrderGetDouble(ORDER_TP) - LowerTP) > _Point / 2))
10671067
{
10681068
// Avoid error 130 based on entry.
10691069
if (Bid - LowerEntry > StopLevel) // Current price above entry
@@ -1074,7 +1074,7 @@ void AdjustUpperAndLowerOrders()
10741074
{
10751075
order_type_string = "Limit";
10761076
}
1077-
else if (NormalizeDouble(OrderGetDouble(ORDER_PRICE_OPEN), _Digits) != NormalizeDouble(LowerEntry, _Digits)) continue;
1077+
else if (MathAbs(OrderGetDouble(ORDER_PRICE_OPEN) - LowerEntry) > _Point / 2) continue;
10781078
// Avoid error 130 based on stop-loss.
10791079
if (LowerSL - LowerEntry <= StopLevel)
10801080
{

0 commit comments

Comments
 (0)