Skip to content

Commit

Permalink
Issue #107: Added function PositionCloseAll
Browse files Browse the repository at this point in the history
  • Loading branch information
vdemydiuk committed Jun 6, 2018
1 parent 897892d commit 0c38d37
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 5 deletions.
3 changes: 2 additions & 1 deletion MtApi5/Mt5CommandType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ internal enum Mt5CommandType
GlobalVariablesDeleteAll = 157,
GlobalVariablesTotal = 158,

UnlockTicks = 159
UnlockTicks = 159,
PositionCloseAll = 160
}
}
9 changes: 9 additions & 0 deletions MtApi5/MtApi5Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -521,11 +521,20 @@ public string HistoryDealGetString(ulong ticketNumber, ENUM_DEAL_PROPERTY_STRING
///<summary>
///Close all open positions.
///</summary>
[Obsolete("OrderCloseAll is deprecated, please use PositionCloseAll instead.")]
public bool OrderCloseAll()
{
return SendCommand<bool>(Mt5CommandType.OrderCloseAll, null);
}

///<summary>
///Close all open positions. Returns count of closed positions.
///</summary>
public int PositionCloseAll()
{
return SendCommand<int>(Mt5CommandType.PositionCloseAll, null);
}

///<summary>
///Closes a position with the specified ticket.
///</summary>
Expand Down
2 changes: 2 additions & 0 deletions TestClients/MtApi5TestClient/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -486,13 +486,15 @@
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Button Grid.Row="0" Command="{Binding PositionOpenCommand}" Content="PositionOpen" Margin="2" HorizontalAlignment="Left"/>
<StackPanel Grid.Row="1" Orientation="Horizontal" Margin="4">
<TextBlock Text="Ticket:" VerticalAlignment="Center"/>
<TextBox Text="{Binding PositionTicketValue}" Width="150" Margin="2"/>
<Button Command="{Binding PositionCloseCommand}" Content="PositionClose" Margin="2"/>
</StackPanel>
<Button Grid.Row="2" Command="{Binding PositionCloseAllCommand}" Content="PositionCloseAll" Margin="2" HorizontalAlignment="Left"/>
</Grid>
</TabItem>

Expand Down
8 changes: 8 additions & 0 deletions TestClients/MtApi5TestClient/ViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public class ViewModel : INotifyPropertyChanged

public DelegateCommand PositionOpenCommand { get; private set; }
public DelegateCommand PositionCloseCommand { get; private set; }
public DelegateCommand PositionCloseAllCommand { get; private set; }

public DelegateCommand GetLastErrorCommand { get; private set; }
public DelegateCommand ResetLastErrorCommand { get; private set; }
Expand Down Expand Up @@ -375,6 +376,7 @@ private void InitCommands()

PositionOpenCommand = new DelegateCommand(ExecutePositionOpen);
PositionCloseCommand = new DelegateCommand(ExecutePositionClose);
PositionCloseAllCommand = new DelegateCommand(ExecutePositionCloseAll);

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

private async void ExecutePositionCloseAll(object obj)
{
var retVal = await Execute(() => _mtApiClient.PositionCloseAll());
AddLog($"PositionCloseAll: count = {retVal}");
}

private async void ExecutePrint(object obj)
{
var message = MessageText;
Expand Down
Binary file modified mq5/MtApi5.ex5
Binary file not shown.
30 changes: 26 additions & 4 deletions mq5/MtApi5.mq5
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,9 @@ int executeCommand()
case 159: //UnlockTiks
Execute_UnlockTicks();
break;
case 160: //PositionCloseAll
Execute_PositionCloseAll();
break;
case 204: //TerminalInfoInteger
Execute_TerminalInfoInteger();
break;
Expand Down Expand Up @@ -6527,6 +6530,17 @@ void Execute_UnlockTicks()
}
}

void Execute_PositionCloseAll()
{
int res = PositionCloseAll();

#ifdef __DEBUG_LOG__
PrintFormat("%s: result = %d", __FUNCTION__, res);
#endif

SEND_INT_RESPONSE(res)
}

void Execute_TerminalInfoInteger()
{
int propertyId;
Expand Down Expand Up @@ -6714,6 +6728,18 @@ bool OrderCloseAll()
return true;
}

int PositionCloseAll()
{
CTrade trade;
int total = PositionsTotal();
int i = total -1;
while (i >= 0)
{
if (trade.PositionClose(PositionGetSymbol(i))) i--;
}
return total;
}

//------------ Requests -------------------------------------------------------

string OnRequest(string json)
Expand Down Expand Up @@ -7408,10 +7434,6 @@ private:

void SendMtEvent(MtEventTypes eventType, MtEvent* mtEvent)
{
#ifdef __DEBUG_LOG__
PrintFormat("%s: eventType = %d", __FUNCTION__, eventType);
#endif

JSONObject* json = mtEvent.CreateJson();
if (sendEvent(ExpertHandle, (int)eventType, json.toString(), _error))
{
Expand Down

0 comments on commit 0c38d37

Please sign in to comment.