Skip to content

Commit 0c38d37

Browse files
committed
Issue #107: Added function PositionCloseAll
1 parent 897892d commit 0c38d37

File tree

6 files changed

+47
-5
lines changed

6 files changed

+47
-5
lines changed

MtApi5/Mt5CommandType.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ internal enum Mt5CommandType
250250
GlobalVariablesDeleteAll = 157,
251251
GlobalVariablesTotal = 158,
252252

253-
UnlockTicks = 159
253+
UnlockTicks = 159,
254+
PositionCloseAll = 160
254255
}
255256
}

MtApi5/MtApi5Client.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,11 +521,20 @@ public string HistoryDealGetString(ulong ticketNumber, ENUM_DEAL_PROPERTY_STRING
521521
///<summary>
522522
///Close all open positions.
523523
///</summary>
524+
[Obsolete("OrderCloseAll is deprecated, please use PositionCloseAll instead.")]
524525
public bool OrderCloseAll()
525526
{
526527
return SendCommand<bool>(Mt5CommandType.OrderCloseAll, null);
527528
}
528529

530+
///<summary>
531+
///Close all open positions. Returns count of closed positions.
532+
///</summary>
533+
public int PositionCloseAll()
534+
{
535+
return SendCommand<int>(Mt5CommandType.PositionCloseAll, null);
536+
}
537+
529538
///<summary>
530539
///Closes a position with the specified ticket.
531540
///</summary>

TestClients/MtApi5TestClient/MainWindow.xaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,13 +486,15 @@
486486
<Grid.RowDefinitions>
487487
<RowDefinition Height="Auto"/>
488488
<RowDefinition Height="Auto"/>
489+
<RowDefinition Height="Auto"/>
489490
</Grid.RowDefinitions>
490491
<Button Grid.Row="0" Command="{Binding PositionOpenCommand}" Content="PositionOpen" Margin="2" HorizontalAlignment="Left"/>
491492
<StackPanel Grid.Row="1" Orientation="Horizontal" Margin="4">
492493
<TextBlock Text="Ticket:" VerticalAlignment="Center"/>
493494
<TextBox Text="{Binding PositionTicketValue}" Width="150" Margin="2"/>
494495
<Button Command="{Binding PositionCloseCommand}" Content="PositionClose" Margin="2"/>
495496
</StackPanel>
497+
<Button Grid.Row="2" Command="{Binding PositionCloseAllCommand}" Content="PositionCloseAll" Margin="2" HorizontalAlignment="Left"/>
496498
</Grid>
497499
</TabItem>
498500

TestClients/MtApi5TestClient/ViewModel.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public class ViewModel : INotifyPropertyChanged
6767

6868
public DelegateCommand PositionOpenCommand { get; private set; }
6969
public DelegateCommand PositionCloseCommand { get; private set; }
70+
public DelegateCommand PositionCloseAllCommand { get; private set; }
7071

7172
public DelegateCommand GetLastErrorCommand { get; private set; }
7273
public DelegateCommand ResetLastErrorCommand { get; private set; }
@@ -375,6 +376,7 @@ private void InitCommands()
375376

376377
PositionOpenCommand = new DelegateCommand(ExecutePositionOpen);
377378
PositionCloseCommand = new DelegateCommand(ExecutePositionClose);
379+
PositionCloseAllCommand = new DelegateCommand(ExecutePositionCloseAll);
378380

379381
PrintCommand = new DelegateCommand(ExecutePrint);
380382
AlertCommand = new DelegateCommand(ExecuteAlert);
@@ -1126,6 +1128,12 @@ private async void ExecutePositionClose(object obj)
11261128
AddLog($"PositionClose: ticket {ticket} retVal = {retVal}, result = {tradeResult}");
11271129
}
11281130

1131+
private async void ExecutePositionCloseAll(object obj)
1132+
{
1133+
var retVal = await Execute(() => _mtApiClient.PositionCloseAll());
1134+
AddLog($"PositionCloseAll: count = {retVal}");
1135+
}
1136+
11291137
private async void ExecutePrint(object obj)
11301138
{
11311139
var message = MessageText;

mq5/MtApi5.ex5

254 Bytes
Binary file not shown.

mq5/MtApi5.mq5

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,9 @@ int executeCommand()
791791
case 159: //UnlockTiks
792792
Execute_UnlockTicks();
793793
break;
794+
case 160: //PositionCloseAll
795+
Execute_PositionCloseAll();
796+
break;
794797
case 204: //TerminalInfoInteger
795798
Execute_TerminalInfoInteger();
796799
break;
@@ -6527,6 +6530,17 @@ void Execute_UnlockTicks()
65276530
}
65286531
}
65296532

6533+
void Execute_PositionCloseAll()
6534+
{
6535+
int res = PositionCloseAll();
6536+
6537+
#ifdef __DEBUG_LOG__
6538+
PrintFormat("%s: result = %d", __FUNCTION__, res);
6539+
#endif
6540+
6541+
SEND_INT_RESPONSE(res)
6542+
}
6543+
65306544
void Execute_TerminalInfoInteger()
65316545
{
65326546
int propertyId;
@@ -6714,6 +6728,18 @@ bool OrderCloseAll()
67146728
return true;
67156729
}
67166730

6731+
int PositionCloseAll()
6732+
{
6733+
CTrade trade;
6734+
int total = PositionsTotal();
6735+
int i = total -1;
6736+
while (i >= 0)
6737+
{
6738+
if (trade.PositionClose(PositionGetSymbol(i))) i--;
6739+
}
6740+
return total;
6741+
}
6742+
67176743
//------------ Requests -------------------------------------------------------
67186744

67196745
string OnRequest(string json)
@@ -7408,10 +7434,6 @@ private:
74087434

74097435
void SendMtEvent(MtEventTypes eventType, MtEvent* mtEvent)
74107436
{
7411-
#ifdef __DEBUG_LOG__
7412-
PrintFormat("%s: eventType = %d", __FUNCTION__, eventType);
7413-
#endif
7414-
74157437
JSONObject* json = mtEvent.CreateJson();
74167438
if (sendEvent(ExpertHandle, (int)eventType, json.toString(), _error))
74177439
{

0 commit comments

Comments
 (0)