@@ -76,7 +76,7 @@ private:
76
76
double m_DPIScale ;
77
77
bool NoPanelMaximization ; // Crutch variable to prevent panel maximization when Maximize() is called at the indicator's initialization.
78
78
CArrayLong *PartiallyClosedOrders ; // Stores order tickets that have been partially closed by Eliminate_Orders().
79
- double PositionsByProfit [][2 ]; // Stores position (order) ticket and its floating profit/loss. Gets filled and sorted when some condition is met and actions are about to get triggered.
79
+ double PositionsByProfit [][2 ]; // Stores position (order) ticket and its floating profit/loss. When CloseMostDistantFirst == true, the profit is replaced with absolute distance. Gets filled and sorted when some condition is met and actions are about to get triggered.
80
80
ENUM_CONDITIONS TriggeredCondition ; // Checked for position array sorting (when optimal) and for partial position closure cancellation when UseTotalVolume is enabled.
81
81
double ClosedVolume ; // How much of the volume has already been closed - for partial closure when UseTotalVolume == true.
82
82
double TotalVolume ; // Store the total volume of filtered trades.
@@ -399,7 +399,7 @@ EVENT_MAP_END(CAppDialog)
399
399
CAccountProtector ::CAccountProtector ()
400
400
{
401
401
m_FileName = " AP_" + IntegerToString (ChartID ()) + " .txt" ;
402
- LogFile = - 1 ;
402
+ LogFile = INVALID_HANDLE ;
403
403
QuantityClosedMarketOrders = 0 ;
404
404
QuantityDeletedPendingOrders = 0 ;
405
405
IsANeedToContinueClosingOrders = false ;
@@ -3669,12 +3669,12 @@ void CAccountProtector::Close_All_Positions()
3669
3669
3670
3670
// Check condition to know whether and how to sort PositionsByProfit.
3671
3671
// Switching sorting modes because the array is traversed backwards - from total - 1 to 0.
3672
- if ((TriggeredCondition == Floating_loss_rises_to_perecentage ) || (TriggeredCondition == Floating_loss_rises_to_currency_units ) || (TriggeredCondition == Floating_loss_rises_to_points )) ArraySort (PositionsByProfit , WHOLE_ARRAY , 0 , MODE_DESCEND );
3672
+ if (CloseMostDistantFirst ) ArraySort (PositionsByProfit , WHOLE_ARRAY , 0 , MODE_ASCEND ); // Regardless of condition.
3673
+ else if ((TriggeredCondition == Floating_loss_rises_to_perecentage ) || (TriggeredCondition == Floating_loss_rises_to_currency_units ) || (TriggeredCondition == Floating_loss_rises_to_points )) ArraySort (PositionsByProfit , WHOLE_ARRAY , 0 , MODE_DESCEND );
3673
3674
else if ((TriggeredCondition == Floating_profit_rises_to_perecentage ) || (TriggeredCondition == Floating_profit_rises_to_currency_units ) || (TriggeredCondition == Floating_profit_rises_to_points )) ArraySort (PositionsByProfit , WHOLE_ARRAY , 0 , MODE_ASCEND );
3674
3675
// Otherwise no sorting needed.
3675
3676
3676
3677
int total = ArrayRange (PositionsByProfit , 0 ); // We already have an array with all tickets.
3677
-
3678
3678
// Closing market orders. Going backwards to delete an order from the array after closing it.
3679
3679
for (int i = total - 1 ; i >= 0 ; i --)
3680
3680
{
@@ -4088,14 +4088,21 @@ void CAccountProtector::EquityTrailing()
4088
4088
string AdditionalFunds_Asterisk = " " ;
4089
4089
if (AdditionalFunds != 0 ) AdditionalFunds_Asterisk = " *" ;
4090
4090
Logging (" Account Protector: Equity stop-loss of " + DoubleToString (sets .doubleCurrentEquityStopLoss , 2 ) + " hit at " + DoubleToString (AE , 2 ) + AdditionalFunds_Asterisk + " . Closing all positions." );
4091
+ if (AlertOnEquityTS ) Alert (" Account Protector: Equity stop-loss of " + DoubleToString (sets .doubleCurrentEquityStopLoss , 2 ) + " hit at " + DoubleToString (AE , 2 ) + AdditionalFunds_Asterisk + " . Closing all positions." );
4091
4092
Logging_Condition_Is_Met ();
4092
4093
Close_All_Positions ();
4093
4094
4094
- sets .boolEquityTrailingStop = false ;
4095
- m_ChkEquityTrailingStop .Checked (false );
4096
-
4097
- m_LblCurrentEquityStopLoss .Hide ();
4098
- m_BtnResetEquityStopLoss .Hide ();
4095
+ if (!DoNotDisableEquityTS )
4096
+ {
4097
+ sets .boolEquityTrailingStop = false ;
4098
+ m_ChkEquityTrailingStop .Checked (false );
4099
+ m_LblCurrentEquityStopLoss .Hide ();
4100
+ m_BtnResetEquityStopLoss .Hide ();
4101
+ }
4102
+ else
4103
+ {
4104
+ sets .doubleCurrentEquityStopLoss = 0 ;
4105
+ }
4099
4106
4100
4107
SaveSettingsOnDisk ();
4101
4108
MoveAndResize ();
@@ -4236,8 +4243,8 @@ void CAccountProtector::Logging_Condition_Is_Met()
4236
4243
double floating_profit = 0 ;
4237
4244
ClosedVolume = 0 ;
4238
4245
TotalVolume = 0 ;
4239
- if (LogFileName == " " ) return ;
4240
4246
for (i = 0 ; i < OrdersTotal (); i ++)
4247
+ {
4241
4248
if (!OrderSelect (i , SELECT_BY_POS )) Logging (" Account Protector: OrderSelect failed " + IntegerToString (GetLastError ()));
4242
4249
else
4243
4250
{
@@ -4255,14 +4262,18 @@ void CAccountProtector::Logging_Condition_Is_Met()
4255
4262
floating_profit += order_floating_profit ;
4256
4263
market ++;
4257
4264
ArrayResize (PositionsByProfit , market , 100 ); // Reserve extra physical memory to increase the resizing speed.
4258
- PositionsByProfit [market - 1 ][0 ] = order_floating_profit ;
4265
+ if (!CloseMostDistantFirst ) PositionsByProfit [market - 1 ][0 ] = order_floating_profit ; // Normal profit.
4266
+ else PositionsByProfit [market - 1 ][0 ] = MathAbs (OrderOpenPrice () - OrderClosePrice ()) / SymbolInfoDouble (OrderSymbol (), SYMBOL_POINT );
4259
4267
PositionsByProfit [market - 1 ][1 ] = OrderTicket ();
4260
4268
TotalVolume += OrderLots ();
4261
4269
}
4262
4270
else if ((OrderType () == OP_BUYSTOP ) || (OrderType () == OP_SELLSTOP ) || (OrderType () == OP_BUYLIMIT ) || (OrderType () == OP_SELLLIMIT )) pending ++;
4263
4271
break ; // Order already processed - no point to process this order with other magic numbers.
4264
4272
}
4265
4273
}
4274
+ }
4275
+
4276
+ if (LogFileName == " " ) return ;
4266
4277
4267
4278
string AdditionalFunds_Asterisk = " " ;
4268
4279
if (AdditionalFunds != 0 ) AdditionalFunds_Asterisk = " *" ;
@@ -4329,7 +4340,7 @@ void CAccountProtector::CheckOneCondition(T &SettingsEditValue, bool &SettingsCh
4329
4340
Logging (" CONDITION IS MET: " + EventDescription );
4330
4341
TriggeredCondition = triggered_condition ;
4331
4342
Trigger_Actions (EventDescription );
4332
- if (!DoNotResetConditions )
4343
+ if (!DoNotDisableConditions )
4333
4344
{
4334
4345
SettingsCheckboxValue = false ;
4335
4346
SettingsEditValue = 0 ;
@@ -4597,7 +4608,7 @@ void CAccountProtector::Trigger_Actions(string title)
4597
4608
// Close all positions.
4598
4609
if (sets .ClosePos )
4599
4610
{
4600
- if (!DoNotResetActions ) sets .ClosePos = false ;
4611
+ if (!DoNotDisableActions ) sets .ClosePos = false ;
4601
4612
Logging (" ACTION IS TAKEN: Close positions (" + DoubleToString (sets .doubleClosePercentage , 2 ) + " % of " + EnumToString (sets .CloseWhichPositions ) + " )." );
4602
4613
PartiallyClosedOrders .Clear ();
4603
4614
Close_All_Positions ();
@@ -4608,7 +4619,7 @@ void CAccountProtector::Trigger_Actions(string title)
4608
4619
// Delete all pending orders.
4609
4620
if (sets .DeletePend )
4610
4621
{
4611
- if (!DoNotResetActions ) sets .DeletePend = false ;
4622
+ if (!DoNotDisableActions ) sets .DeletePend = false ;
4612
4623
Logging (" ACTION IS TAKEN: Delete all pending orders." );
4613
4624
Delete_All_Pending_Orders ();
4614
4625
sets .Triggered = true ;
@@ -4618,7 +4629,7 @@ void CAccountProtector::Trigger_Actions(string title)
4618
4629
// Disable autotrading.
4619
4630
if (sets .DisAuto )
4620
4631
{
4621
- if (!DoNotResetActions ) sets .DisAuto = false ;
4632
+ if (!DoNotDisableActions ) sets .DisAuto = false ;
4622
4633
Logging (" ACTION IS TAKEN: Disable autotrading." );
4623
4634
// Toggle AutoTrading button. "2" in GetAncestor call is the "root window".
4624
4635
if (TerminalInfoInteger (TERMINAL_TRADE_ALLOWED )) SendMessageW (GetAncestor (WindowHandle (Symbol (), Period ()), 2 ), WM_COMMAND , 33020 , 0 );
@@ -4630,7 +4641,7 @@ void CAccountProtector::Trigger_Actions(string title)
4630
4641
// Send emails.
4631
4642
if (sets .SendMails )
4632
4643
{
4633
- if (!DoNotResetActions ) sets .SendMails = false ;
4644
+ if (!DoNotDisableActions ) sets .SendMails = false ;
4634
4645
Logging (" ACTION IS TAKEN: Send email." );
4635
4646
PrepareSubjectBody (subject , body , title , TimeCurrent (), QuantityClosedMarketOrders , QuantityDeletedPendingOrders , WasAutoTradingDisabled , WasNotificationSent , false , WasPlatformClosed , WasAutoTradingEnabled , WasRecapturedSnapshots );
4636
4647
SendMailFunction (subject , body );
@@ -4641,7 +4652,7 @@ void CAccountProtector::Trigger_Actions(string title)
4641
4652
// Send push notifications.
4642
4653
if (sets .SendNotif )
4643
4654
{
4644
- if (!DoNotResetActions ) sets .SendNotif = false ;
4655
+ if (!DoNotDisableActions ) sets .SendNotif = false ;
4645
4656
Logging (" ACTION IS TAKEN: Send push notifications." );
4646
4657
PrepareSubjectBody (subject , body , title , TimeCurrent (), QuantityClosedMarketOrders , QuantityDeletedPendingOrders , WasAutoTradingDisabled , false , WasMailSent , WasPlatformClosed , WasAutoTradingEnabled , WasRecapturedSnapshots , true );
4647
4658
SendNotificationFunction (subject , body );
@@ -4652,15 +4663,15 @@ void CAccountProtector::Trigger_Actions(string title)
4652
4663
// Close platform.
4653
4664
if (sets .ClosePlatform )
4654
4665
{
4655
- if (!DoNotResetActions ) sets .ClosePlatform = false ;
4666
+ if (!DoNotDisableActions ) sets .ClosePlatform = false ;
4656
4667
Logging (" ACTION IS TAKEN: Close platform." );
4657
4668
TerminalClose (0 );
4658
4669
}
4659
4670
4660
4671
// Enable autotrading.
4661
4672
if (sets .EnableAuto )
4662
4673
{
4663
- if (!DoNotResetActions ) sets .EnableAuto = false ;
4674
+ if (!DoNotDisableActions ) sets .EnableAuto = false ;
4664
4675
Logging (" ACTION IS TAKEN: Enable autotrading." );
4665
4676
// Toggle AutoTrading button. "2" in GetAncestor call is the "root window".
4666
4677
if (!TerminalInfoInteger (TERMINAL_TRADE_ALLOWED )) SendMessageW (GetAncestor (WindowHandle (Symbol (), Period ()), 2 ), WM_COMMAND , 33020 , 0 );
@@ -4671,7 +4682,7 @@ void CAccountProtector::Trigger_Actions(string title)
4671
4682
// Recapture snapshots.
4672
4683
if (sets .RecaptureSnapshots )
4673
4684
{
4674
- if (!DoNotResetActions ) sets .RecaptureSnapshots = false ;
4685
+ if (!DoNotDisableActions ) sets .RecaptureSnapshots = false ;
4675
4686
Logging (" ACTION IS TAKEN: Recapture snapshots." );
4676
4687
UpdateEquitySnapshot ();
4677
4688
UpdateMarginSnapshot ();
@@ -4688,14 +4699,14 @@ void CAccountProtector::Logging(string message)
4688
4699
{
4689
4700
if (StringLen (LogFileName ) > 0 )
4690
4701
{
4691
- string filename = LogFileName + " .log " ;
4692
- if (LogFile < 0 ) LogFile = FileOpen (filename , FILE_CSV | FILE_READ | FILE_WRITE , ' ' );
4693
- if (LogFile < 1 ) Alert (" Cannot open file for logging: " , filename , " ." );
4702
+ string filename = LogFileName ;
4703
+ if (LogFile == INVALID_HANDLE ) LogFile = FileOpen (filename , FILE_CSV | FILE_READ | FILE_WRITE , ' ' );
4704
+ if (LogFile == INVALID_HANDLE ) Alert (" Cannot open file for logging: " , filename , " ." );
4694
4705
else if (FileSeek (LogFile , 0 , SEEK_END ))
4695
4706
{
4696
4707
FileWrite (LogFile , TimeToString (TimeLocal (), TIME_DATE | TIME_MINUTES | TIME_SECONDS ), " " , message );
4697
4708
FileClose (LogFile );
4698
- LogFile = - 1 ;
4709
+ LogFile = INVALID_HANDLE ;
4699
4710
}
4700
4711
else Alert (" Unexpected error accessing file: " , filename , " ." );
4701
4712
}
0 commit comments