Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
151 changes: 151 additions & 0 deletions MtApi5/MtApi5Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,157 @@ public bool Sell(out MqlTradeResult? result, double volume, string? symbol = nul
result = response?.Result;
return response != null && response.RetVal;
}

/// <summary>
/// Places a Buy Limit pending order (buy at a price lower than the current market price) with specified parameters
/// </summary>
/// <param name="result">output result</param>
/// <param name="volume">Requested order volume.</param>
/// <param name="price">Order execution price.</param>
/// <param name="symbol">Order symbol. If it is not specified, the current symbol will be used.</param>
/// <param name="sl">Stop Loss price.</param>
/// <param name="tp">Take Profit price.</param>
/// <param name="typeTime">Order expiration type.</param>
/// <param name="expiration">Order expiration time (used with typeTime ORDER_TIME_SPECIFIED or ORDER_TIME_SPECIFIED_DAY).</param>
/// <param name="comment">Comment.</param>
/// <returns>true - successful check of the structures, otherwise - false.</returns>
public bool BuyLimit(out MqlTradeResult? result, double volume, double price, string? symbol = null, double sl = 0.0, double tp = 0.0, ENUM_ORDER_TYPE_TIME typeTime = ENUM_ORDER_TYPE_TIME.ORDER_TIME_GTC, DateTime? expiration = null, string? comment = null)
{
Dictionary<string, object> cmdParams = new() { { "Volume", volume }, { "Price", price }, { "Sl", sl }, { "Tp", tp },
{ "TypeTime", (int)typeTime }, { "Expiration", Mt5TimeConverter.ConvertToMtTime(expiration) } };
if (symbol != null)
cmdParams["Symbol"] = symbol;
if (comment != null)
cmdParams["Comment"] = comment;

var response = SendCommand<FuncResult<MqlTradeResult>>(ExecutorHandle, Mt5CommandType.BuyLimit, cmdParams);

result = response?.Result;
return response != null && response.RetVal;
}

/// <summary>
/// Places a Sell Limit pending order (sell at a price higher than the current market price) with specified parameters
/// </summary>
/// <param name="result">output result</param>
/// <param name="volume">Requested order volume.</param>
/// <param name="price">Order execution price.</param>
/// <param name="symbol">Order symbol. If it is not specified, the current symbol will be used.</param>
/// <param name="sl">Stop Loss price.</param>
/// <param name="tp">Take Profit price.</param>
/// <param name="typeTime">Order expiration type.</param>
/// <param name="expiration">Order expiration time (used with typeTime ORDER_TIME_SPECIFIED or ORDER_TIME_SPECIFIED_DAY).</param>
/// <param name="comment">Comment.</param>
/// <returns>true - successful check of the structures, otherwise - false.</returns>
public bool SellLimit(out MqlTradeResult? result, double volume, double price, string? symbol = null, double sl = 0.0, double tp = 0.0, ENUM_ORDER_TYPE_TIME typeTime = ENUM_ORDER_TYPE_TIME.ORDER_TIME_GTC, DateTime? expiration = null, string? comment = null)
{
Dictionary<string, object> cmdParams = new() { { "Volume", volume }, { "Price", price }, { "Sl", sl }, { "Tp", tp },
{ "TypeTime", (int)typeTime }, { "Expiration", Mt5TimeConverter.ConvertToMtTime(expiration) } };
if (symbol != null)
cmdParams["Symbol"] = symbol;
if (comment != null)
cmdParams["Comment"] = comment;

var response = SendCommand<FuncResult<MqlTradeResult>>(ExecutorHandle, Mt5CommandType.SellLimit, cmdParams);

result = response?.Result;
return response != null && response.RetVal;
}

/// <summary>
/// Places a Buy Stop pending order (buy at a price higher than the current market price) with specified parameters
/// </summary>
/// <param name="result">output result</param>
/// <param name="volume">Requested order volume.</param>
/// <param name="price">Order execution price.</param>
/// <param name="symbol">Order symbol. If it is not specified, the current symbol will be used.</param>
/// <param name="sl">Stop Loss price.</param>
/// <param name="tp">Take Profit price.</param>
/// <param name="typeTime">Order expiration type.</param>
/// <param name="expiration">Order expiration time (used with typeTime ORDER_TIME_SPECIFIED or ORDER_TIME_SPECIFIED_DAY).</param>
/// <param name="comment">Comment.</param>
/// <returns>true - successful check of the structures, otherwise - false.</returns>
public bool BuyStop(out MqlTradeResult? result, double volume, double price, string? symbol = null, double sl = 0.0, double tp = 0.0, ENUM_ORDER_TYPE_TIME typeTime = ENUM_ORDER_TYPE_TIME.ORDER_TIME_GTC, DateTime? expiration = null, string? comment = null)
{
Dictionary<string, object> cmdParams = new() { { "Volume", volume }, { "Price", price }, { "Sl", sl }, { "Tp", tp },
{ "TypeTime", (int)typeTime }, { "Expiration", Mt5TimeConverter.ConvertToMtTime(expiration) } };
if (symbol != null)
cmdParams["Symbol"] = symbol;
if (comment != null)
cmdParams["Comment"] = comment;

var response = SendCommand<FuncResult<MqlTradeResult>>(ExecutorHandle, Mt5CommandType.BuyStop, cmdParams);

result = response?.Result;
return response != null && response.RetVal;
}

/// <summary>
/// Places a Sell Stop pending order (sell at a price lower than the current market price) with specified parameters
/// </summary>
/// <param name="result">output result</param>
/// <param name="volume">Requested order volume.</param>
/// <param name="price">Order execution price.</param>
/// <param name="symbol">Order symbol. If it is not specified, the current symbol will be used.</param>
/// <param name="sl">Stop Loss price.</param>
/// <param name="tp">Take Profit price.</param>
/// <param name="typeTime">Order expiration type.</param>
/// <param name="expiration">Order expiration time (used with typeTime ORDER_TIME_SPECIFIED or ORDER_TIME_SPECIFIED_DAY).</param>
/// <param name="comment">Comment.</param>
/// <returns>true - successful check of the structures, otherwise - false.</returns>
public bool SellStop(out MqlTradeResult? result, double volume, double price, string? symbol = null, double sl = 0.0, double tp = 0.0, ENUM_ORDER_TYPE_TIME typeTime = ENUM_ORDER_TYPE_TIME.ORDER_TIME_GTC, DateTime? expiration = null, string? comment = null)
{
Dictionary<string, object> cmdParams = new() { { "Volume", volume }, { "Price", price }, { "Sl", sl }, { "Tp", tp },
{ "TypeTime", (int)typeTime }, { "Expiration", Mt5TimeConverter.ConvertToMtTime(expiration) } };
if (symbol != null)
cmdParams["Symbol"] = symbol;
if (comment != null)
cmdParams["Comment"] = comment;

var response = SendCommand<FuncResult<MqlTradeResult>>(ExecutorHandle, Mt5CommandType.SellStop, cmdParams);

result = response?.Result;
return response != null && response.RetVal;
}

/// <summary>
/// Modifies the parameters of a previously placed pending order
/// </summary>
/// <param name="result">output result</param>
/// <param name="ticket">Ticket of the pending order to be modified.</param>
/// <param name="price">New order execution price.</param>
/// <param name="sl">New Stop Loss price.</param>
/// <param name="tp">New Take Profit price.</param>
/// <param name="typeTime">Order expiration type.</param>
/// <param name="expiration">Order expiration time (used with typeTime ORDER_TIME_SPECIFIED or ORDER_TIME_SPECIFIED_DAY).</param>
/// <param name="stoplimit">Limit order price for the StopLimit order.</param>
/// <returns>true - successful check of the structures, otherwise - false.</returns>
public bool OrderModify(out MqlTradeResult? result, ulong ticket, double price, double sl, double tp, ENUM_ORDER_TYPE_TIME typeTime = ENUM_ORDER_TYPE_TIME.ORDER_TIME_GTC, DateTime? expiration = null, double stoplimit = 0.0)
{
Dictionary<string, object> cmdParams = new() { { "Ticket", ticket }, { "Price", price }, { "Sl", sl }, { "Tp", tp },
{ "TypeTime", (int)typeTime }, { "Expiration", Mt5TimeConverter.ConvertToMtTime(expiration) }, { "Stoplimit", stoplimit } };

var response = SendCommand<FuncResult<MqlTradeResult>>(ExecutorHandle, Mt5CommandType.OrderModify, cmdParams);

result = response?.Result;
return response != null && response.RetVal;
}

/// <summary>
/// Removes a previously placed pending order
/// </summary>
/// <param name="result">output result</param>
/// <param name="ticket">Ticket of the pending order to be deleted.</param>
/// <returns>true - successful check of the structures, otherwise - false.</returns>
public bool OrderDelete(out MqlTradeResult? result, ulong ticket)
{
Dictionary<string, object> cmdParams = new() { { "Ticket", ticket } };

var response = SendCommand<FuncResult<MqlTradeResult>>(ExecutorHandle, Mt5CommandType.OrderDelete, cmdParams);

result = response?.Result;
return response != null && response.RetVal;
}
#endregion

#region Account Information functions
Expand Down
9 changes: 8 additions & 1 deletion MtApi5/MtProtocol/Mt5CommandType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,13 @@ internal enum Mt5CommandType
OrderCheck = 303,
Buy = 304,
Sell = 305,
GetSymbols = 306
GetSymbols = 306,

BuyLimit = 320,
SellLimit = 321,
BuyStop = 322,
SellStop = 323,
OrderModify = 324,
OrderDelete = 325
}
}
2 changes: 2 additions & 0 deletions TestClients/MtApi5TestClient/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,8 @@
<StackPanel Grid.Row="3" Orientation="Horizontal" Margin="4">
<Button Command="{Binding BuyCommand}" Content="Buy" Width="60" Margin="2" HorizontalAlignment="Left"/>
<Button Command="{Binding SellCommand}" Content="Sell" Width="60" Margin="2" HorizontalAlignment="Left"/>
<Button Command="{Binding BuyLimitCommand}" Content="BuyLimit" Width="60" Margin="2" HorizontalAlignment="Left"/>
<Button Command="{Binding OrderDeleteCommand}" Content="OrderDelete" Width="80" Margin="2" HorizontalAlignment="Left"/>
</StackPanel>
</Grid>
</TabItem>
Expand Down
33 changes: 33 additions & 0 deletions TestClients/MtApi5TestClient/ViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ public class ViewModel : INotifyPropertyChanged
public DelegateCommand PositionCloseAllCommand { get; private set; }
public DelegateCommand BuyCommand { get; private set; }
public DelegateCommand SellCommand { get; private set; }
public DelegateCommand BuyLimitCommand { get; private set; }
public DelegateCommand OrderDeleteCommand { get; private set; }

public DelegateCommand GetLastErrorCommand { get; private set; }
public DelegateCommand ResetLastErrorCommand { get; private set; }
Expand Down Expand Up @@ -450,6 +452,8 @@ private void InitCommands()
PositionCloseAllCommand = new DelegateCommand(ExecutePositionCloseAll);
BuyCommand = new DelegateCommand(ExecuteBuy);
SellCommand = new DelegateCommand(ExecuteSell);
BuyLimitCommand = new DelegateCommand(ExecuteBuyLimit);
OrderDeleteCommand = new DelegateCommand(ExecuteOrderDelete);

PrintCommand = new DelegateCommand(ExecutePrint);
AlertCommand = new DelegateCommand(ExecuteAlert);
Expand Down Expand Up @@ -1302,6 +1306,35 @@ private async void ExecuteSell(object obj)
AddLog($"Sell: symbol EURUSD retVal = {retVal}, result = {tradeResult}");
}

private async void ExecuteBuyLimit(object obj)
{
const string symbol = "EURUSD";
const double volume = 0.1;

MqlTick tick = null;
var gotTick = await Execute(() => _mtApiClient.SymbolInfoTick(symbol, out tick));
if (gotTick == false || tick == null)
{
AddLog("BuyLimit: failed to get current tick");
return;
}

var price = Math.Round(tick.bid - 0.00500, 5);
MqlTradeResult tradeResult = null;

var retVal = await Execute(() => _mtApiClient.BuyLimit(out tradeResult, volume, price, symbol));
AddLog($"BuyLimit: symbol {symbol} price {price} retVal = {retVal}, result = {tradeResult}");
}

private async void ExecuteOrderDelete(object obj)
{
var ticket = PositionTicketValue;
MqlTradeResult tradeResult = null;

var retVal = await Execute(() => _mtApiClient.OrderDelete(out tradeResult, ticket));
AddLog($"OrderDelete: ticket {ticket} retVal = {retVal}, result = {tradeResult}");
}

private async void ExecutePrint(object obj)
{
var message = MessageText;
Expand Down
Binary file modified mq5/MtApi5.ex5
Binary file not shown.
Loading