mirror of
https://github.com/vdemydiuk/mtapi.git
synced 2026-08-09 08:57:52 +00:00
Issue #202: [MT5] added Buy/Sell functions
This commit is contained in:
@@ -78,6 +78,7 @@
|
|||||||
<Compile Include="Events\Mt5EventTypes.cs" />
|
<Compile Include="Events\Mt5EventTypes.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
<Compile Include="Mt5Quote.cs" />
|
<Compile Include="Mt5Quote.cs" />
|
||||||
|
<Compile Include="Requests\BuyRequest.cs" />
|
||||||
<Compile Include="Requests\ChartTimePriceToXyRequest.cs" />
|
<Compile Include="Requests\ChartTimePriceToXyRequest.cs" />
|
||||||
<Compile Include="Requests\ChartTimePriceToXyResult.cs" />
|
<Compile Include="Requests\ChartTimePriceToXyResult.cs" />
|
||||||
<Compile Include="Requests\ChartXyToTimePriceRequest.cs" />
|
<Compile Include="Requests\ChartXyToTimePriceRequest.cs" />
|
||||||
@@ -96,6 +97,7 @@
|
|||||||
<Compile Include="Requests\RequestType.cs" />
|
<Compile Include="Requests\RequestType.cs" />
|
||||||
<Compile Include="Requests\OrderSendResult.cs" />
|
<Compile Include="Requests\OrderSendResult.cs" />
|
||||||
<Compile Include="Requests\Response.cs" />
|
<Compile Include="Requests\Response.cs" />
|
||||||
|
<Compile Include="Requests\SellRequest.cs" />
|
||||||
<Compile Include="Requests\SymbolInfoStringRequest.cs" />
|
<Compile Include="Requests\SymbolInfoStringRequest.cs" />
|
||||||
<Compile Include="Requests\SymbolInfoStringResult.cs" />
|
<Compile Include="Requests\SymbolInfoStringResult.cs" />
|
||||||
<Compile Include="Requests\SymbolInfoTickRequest.cs" />
|
<Compile Include="Requests\SymbolInfoTickRequest.cs" />
|
||||||
|
|||||||
@@ -683,6 +683,64 @@ namespace MtApi5
|
|||||||
|
|
||||||
return SendCommand<bool>(Mt5CommandType.PositionClosePartial_byTicket, commandParameters);
|
return SendCommand<bool>(Mt5CommandType.PositionClosePartial_byTicket, commandParameters);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Opens a long position with specified parameters with current market Ask price
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="result">output result</param>
|
||||||
|
/// <param name="volume">Requested position volume.</param>
|
||||||
|
/// <param name="symbol">Position symbol. If it is not specified, the current symbol will be used.</param>
|
||||||
|
/// <param name="price">Execution price.</param>
|
||||||
|
/// <param name="sl">Stop Loss price.</param>
|
||||||
|
/// <param name="tp">Take Profit price.</param>
|
||||||
|
/// <param name="comment">Comment.</param>
|
||||||
|
/// <returns>true - successful check of the structures, otherwise - false.</returns>
|
||||||
|
public bool Buy(out MqlTradeResult result, double volume, string symbol = null, double price = 0.0, double sl = 0.0, double tp = 0.0, string comment = null)
|
||||||
|
{
|
||||||
|
Log.Debug($"Buy: volume = {volume}, symbol = {symbol}, sl = {sl}, tp = {tp}, comment = {comment}");
|
||||||
|
|
||||||
|
var response = SendRequest<OrderSendResult>(new BuyRequest
|
||||||
|
{
|
||||||
|
Volume = volume,
|
||||||
|
Symbol = symbol,
|
||||||
|
Price = price,
|
||||||
|
Sl = sl,
|
||||||
|
Tp = tp,
|
||||||
|
Comment = comment
|
||||||
|
});
|
||||||
|
|
||||||
|
result = response?.TradeResult;
|
||||||
|
return response != null && response.RetVal;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Opens a short position with specified parameters with current market Bid price
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="result">output result</param>
|
||||||
|
/// <param name="volume">Requested position volume.</param>
|
||||||
|
/// <param name="symbol">Position symbol. If it is not specified, the current symbol will be used.</param>
|
||||||
|
/// <param name="price">Execution price.</param>
|
||||||
|
/// <param name="sl">Stop Loss price.</param>
|
||||||
|
/// <param name="tp">Take Profit price.</param>
|
||||||
|
/// <param name="comment">Comment.</param>
|
||||||
|
/// <returns>true - successful check of the structures, otherwise - false.</returns>
|
||||||
|
public bool Sell(out MqlTradeResult result, double volume, string symbol = null, double price = 0.0, double sl = 0.0, double tp = 0.0, string comment = null)
|
||||||
|
{
|
||||||
|
Log.Debug($"Sell: volume = {volume}, symbol = {symbol}, sl = {sl}, tp = {tp}, comment = {comment}");
|
||||||
|
|
||||||
|
var response = SendRequest<OrderSendResult>(new SellRequest
|
||||||
|
{
|
||||||
|
Volume = volume,
|
||||||
|
Symbol = symbol,
|
||||||
|
Price = price,
|
||||||
|
Sl = sl,
|
||||||
|
Tp = tp,
|
||||||
|
Comment = comment
|
||||||
|
});
|
||||||
|
|
||||||
|
result = response?.TradeResult;
|
||||||
|
return response != null && response.RetVal;
|
||||||
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Account Information functions
|
#region Account Information functions
|
||||||
|
|||||||
@@ -0,0 +1,14 @@
|
|||||||
|
namespace MtApi5.Requests
|
||||||
|
{
|
||||||
|
internal class BuyRequest : RequestBase
|
||||||
|
{
|
||||||
|
public override RequestType RequestType => RequestType.Buy;
|
||||||
|
|
||||||
|
public double Volume { get; set; }
|
||||||
|
public string Symbol { get; set; }
|
||||||
|
public double Price { get; set; }
|
||||||
|
public double Sl { get; set; }
|
||||||
|
public double Tp { get; set; }
|
||||||
|
public string Comment { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -16,6 +16,8 @@ namespace MtApi5.Requests
|
|||||||
ChartTimePriceToXY = 9,
|
ChartTimePriceToXY = 9,
|
||||||
ChartXYToTimePrice = 10,
|
ChartXYToTimePrice = 10,
|
||||||
PositionClose = 11,
|
PositionClose = 11,
|
||||||
SymbolInfoTick = 12
|
SymbolInfoTick = 12,
|
||||||
|
Buy = 13,
|
||||||
|
Sell = 14
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
namespace MtApi5.Requests
|
||||||
|
{
|
||||||
|
internal class SellRequest : RequestBase
|
||||||
|
{
|
||||||
|
public override RequestType RequestType => RequestType.Sell;
|
||||||
|
|
||||||
|
public double Volume { get; set; }
|
||||||
|
public string Symbol { get; set; }
|
||||||
|
public double Price { get; set; }
|
||||||
|
public double Sl { get; set; }
|
||||||
|
public double Tp { get; set; }
|
||||||
|
public string Comment { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -497,6 +497,7 @@
|
|||||||
<RowDefinition Height="Auto"/>
|
<RowDefinition Height="Auto"/>
|
||||||
<RowDefinition Height="Auto"/>
|
<RowDefinition Height="Auto"/>
|
||||||
<RowDefinition Height="Auto"/>
|
<RowDefinition Height="Auto"/>
|
||||||
|
<RowDefinition Height="Auto"/>
|
||||||
</Grid.RowDefinitions>
|
</Grid.RowDefinitions>
|
||||||
<Button Grid.Row="0" Command="{Binding PositionOpenCommand}" Content="PositionOpen" Margin="2" HorizontalAlignment="Left"/>
|
<Button Grid.Row="0" Command="{Binding PositionOpenCommand}" Content="PositionOpen" Margin="2" HorizontalAlignment="Left"/>
|
||||||
<StackPanel Grid.Row="1" Orientation="Horizontal" Margin="4">
|
<StackPanel Grid.Row="1" Orientation="Horizontal" Margin="4">
|
||||||
@@ -505,6 +506,10 @@
|
|||||||
<Button Command="{Binding PositionCloseCommand}" Content="PositionClose" Margin="2"/>
|
<Button Command="{Binding PositionCloseCommand}" Content="PositionClose" Margin="2"/>
|
||||||
</StackPanel>
|
</StackPanel>
|
||||||
<Button Grid.Row="2" Command="{Binding PositionCloseAllCommand}" Content="PositionCloseAll" Margin="2" HorizontalAlignment="Left"/>
|
<Button Grid.Row="2" Command="{Binding PositionCloseAllCommand}" Content="PositionCloseAll" Margin="2" HorizontalAlignment="Left"/>
|
||||||
|
<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"/>
|
||||||
|
</StackPanel>
|
||||||
</Grid>
|
</Grid>
|
||||||
</TabItem>
|
</TabItem>
|
||||||
|
|
||||||
|
|||||||
@@ -73,6 +73,8 @@ namespace MtApi5TestClient
|
|||||||
public DelegateCommand PositionOpenCommand { get; private set; }
|
public DelegateCommand PositionOpenCommand { get; private set; }
|
||||||
public DelegateCommand PositionCloseCommand { get; private set; }
|
public DelegateCommand PositionCloseCommand { get; private set; }
|
||||||
public DelegateCommand PositionCloseAllCommand { get; private set; }
|
public DelegateCommand PositionCloseAllCommand { get; private set; }
|
||||||
|
public DelegateCommand BuyCommand { get; private set; }
|
||||||
|
public DelegateCommand SellCommand { get; private set; }
|
||||||
|
|
||||||
public DelegateCommand GetLastErrorCommand { get; private set; }
|
public DelegateCommand GetLastErrorCommand { get; private set; }
|
||||||
public DelegateCommand ResetLastErrorCommand { get; private set; }
|
public DelegateCommand ResetLastErrorCommand { get; private set; }
|
||||||
@@ -386,6 +388,8 @@ namespace MtApi5TestClient
|
|||||||
PositionOpenCommand = new DelegateCommand(ExecutePositionOpen);
|
PositionOpenCommand = new DelegateCommand(ExecutePositionOpen);
|
||||||
PositionCloseCommand = new DelegateCommand(ExecutePositionClose);
|
PositionCloseCommand = new DelegateCommand(ExecutePositionClose);
|
||||||
PositionCloseAllCommand = new DelegateCommand(ExecutePositionCloseAll);
|
PositionCloseAllCommand = new DelegateCommand(ExecutePositionCloseAll);
|
||||||
|
BuyCommand = new DelegateCommand(ExecuteBuy);
|
||||||
|
SellCommand = new DelegateCommand(ExecuteSell);
|
||||||
|
|
||||||
PrintCommand = new DelegateCommand(ExecutePrint);
|
PrintCommand = new DelegateCommand(ExecutePrint);
|
||||||
AlertCommand = new DelegateCommand(ExecuteAlert);
|
AlertCommand = new DelegateCommand(ExecuteAlert);
|
||||||
@@ -1211,6 +1215,26 @@ namespace MtApi5TestClient
|
|||||||
AddLog($"PositionCloseAll: count = {retVal}");
|
AddLog($"PositionCloseAll: count = {retVal}");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async void ExecuteBuy(object obj)
|
||||||
|
{
|
||||||
|
const string symbol = "EURUSD";
|
||||||
|
const double volume = 0.1;
|
||||||
|
MqlTradeResult tradeResult = null;
|
||||||
|
|
||||||
|
var retVal = await Execute(() => _mtApiClient.Buy(out tradeResult, volume, symbol));
|
||||||
|
AddLog($"Buy: symbol EURUSD retVal = {retVal}, result = {tradeResult}");
|
||||||
|
}
|
||||||
|
|
||||||
|
private async void ExecuteSell(object obj)
|
||||||
|
{
|
||||||
|
const string symbol = "EURUSD";
|
||||||
|
const double volume = 0.1;
|
||||||
|
MqlTradeResult tradeResult = null;
|
||||||
|
|
||||||
|
var retVal = await Execute(() => _mtApiClient.Sell(out tradeResult, volume, symbol));
|
||||||
|
AddLog($"Sell: symbol EURUSD retVal = {retVal}, result = {tradeResult}");
|
||||||
|
}
|
||||||
|
|
||||||
private async void ExecutePrint(object obj)
|
private async void ExecutePrint(object obj)
|
||||||
{
|
{
|
||||||
var message = MessageText;
|
var message = MessageText;
|
||||||
|
|||||||
Binary file not shown.
+99
-1
@@ -1,7 +1,7 @@
|
|||||||
#property copyright "Vyacheslav Demidyuk"
|
#property copyright "Vyacheslav Demidyuk"
|
||||||
#property link ""
|
#property link ""
|
||||||
|
|
||||||
#property version "1.7"
|
#property version "1.8"
|
||||||
#property description "MtApi (MT5) connection expert"
|
#property description "MtApi (MT5) connection expert"
|
||||||
|
|
||||||
#include <json.mqh>
|
#include <json.mqh>
|
||||||
@@ -6956,6 +6956,12 @@ string OnRequest(string json)
|
|||||||
case 12: //SymbolInfoTick
|
case 12: //SymbolInfoTick
|
||||||
response = ExecuteRequest_SymbolInfoTick(jo);
|
response = ExecuteRequest_SymbolInfoTick(jo);
|
||||||
break;
|
break;
|
||||||
|
case 13: //Buy
|
||||||
|
response = ExecuteRequest_Buy(jo);
|
||||||
|
break;
|
||||||
|
case 14: //Sell
|
||||||
|
response = ExecuteRequest_Sell(jo);
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
PrintFormat("%s [WARNING]: Unknown request type %d", __FUNCTION__, requestType);
|
PrintFormat("%s [WARNING]: Unknown request type %d", __FUNCTION__, requestType);
|
||||||
response = CreateErrorResponse(-1, "Unknown request type");
|
response = CreateErrorResponse(-1, "Unknown request type");
|
||||||
@@ -7487,6 +7493,98 @@ string ExecuteRequest_SymbolInfoTick(JSONObject *jo)
|
|||||||
return CreateSuccessResponse("Value", MqlTickToJson(tick));
|
return CreateSuccessResponse("Value", MqlTickToJson(tick));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
string ExecuteRequest_Buy(JSONObject *jo)
|
||||||
|
{
|
||||||
|
//Symbol
|
||||||
|
string symbol=Symbol();
|
||||||
|
if (jo.getValue("Symbol") != NULL)
|
||||||
|
symbol = jo.getString("Symbol");
|
||||||
|
|
||||||
|
//Volume
|
||||||
|
CHECK_JSON_VALUE(jo, "Volume", CreateErrorResponse(-1, "Undefinded mandatory parameter Volume"));
|
||||||
|
double volume = jo.getDouble("Volume");
|
||||||
|
|
||||||
|
//Price
|
||||||
|
CHECK_JSON_VALUE(jo, "Price", CreateErrorResponse(-1, "Undefinded mandatory parameter Price"));
|
||||||
|
double price = jo.getDouble("Price");
|
||||||
|
|
||||||
|
//Sl
|
||||||
|
CHECK_JSON_VALUE(jo, "Sl", CreateErrorResponse(-1, "Undefinded mandatory parameter Sl"));
|
||||||
|
double sl = jo.getDouble("Sl");
|
||||||
|
|
||||||
|
//Tp
|
||||||
|
CHECK_JSON_VALUE(jo, "Tp", CreateErrorResponse(-1, "Undefinded mandatory parameter Tp"));
|
||||||
|
double tp = jo.getDouble("Tp");
|
||||||
|
|
||||||
|
//Comment
|
||||||
|
string comment="";
|
||||||
|
if (jo.getValue("Comment") != NULL)
|
||||||
|
comment = jo.getString("Comment");
|
||||||
|
|
||||||
|
#ifdef __DEBUG_LOG__
|
||||||
|
PrintFormat("%s: symbol = %s, volume = %f, price = %f, sl = %f, tp = %f, comment = %s",
|
||||||
|
__FUNCTION__, symbol, volume, price, sl, tp, comment);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
CTrade trade;
|
||||||
|
bool ok = trade.Buy(volume, symbol, price, sl, tp, comment);
|
||||||
|
|
||||||
|
MqlTradeResult trade_result={0};
|
||||||
|
trade.Result(trade_result);
|
||||||
|
|
||||||
|
JSONObject* result_value_jo = new JSONObject();
|
||||||
|
result_value_jo.put("RetVal", new JSONBool(ok));
|
||||||
|
result_value_jo.put("TradeResult", MqlTradeResultToJson(trade_result));
|
||||||
|
|
||||||
|
return CreateSuccessResponse("Value", result_value_jo);
|
||||||
|
}
|
||||||
|
|
||||||
|
string ExecuteRequest_Sell(JSONObject *jo)
|
||||||
|
{
|
||||||
|
//Symbol
|
||||||
|
string symbol=Symbol();
|
||||||
|
if (jo.getValue("Symbol") != NULL)
|
||||||
|
symbol = jo.getString("Symbol");
|
||||||
|
|
||||||
|
//Volume
|
||||||
|
CHECK_JSON_VALUE(jo, "Volume", CreateErrorResponse(-1, "Undefinded mandatory parameter Volume"));
|
||||||
|
double volume = jo.getDouble("Volume");
|
||||||
|
|
||||||
|
//Price
|
||||||
|
CHECK_JSON_VALUE(jo, "Price", CreateErrorResponse(-1, "Undefinded mandatory parameter Price"));
|
||||||
|
double price = jo.getDouble("Price");
|
||||||
|
|
||||||
|
//Sl
|
||||||
|
CHECK_JSON_VALUE(jo, "Sl", CreateErrorResponse(-1, "Undefinded mandatory parameter Sl"));
|
||||||
|
double sl = jo.getDouble("Sl");
|
||||||
|
|
||||||
|
//Tp
|
||||||
|
CHECK_JSON_VALUE(jo, "Tp", CreateErrorResponse(-1, "Undefinded mandatory parameter Tp"));
|
||||||
|
double tp = jo.getDouble("Tp");
|
||||||
|
|
||||||
|
//Comment
|
||||||
|
string comment="";
|
||||||
|
if (jo.getValue("Comment") != NULL)
|
||||||
|
comment = jo.getString("Comment");
|
||||||
|
|
||||||
|
#ifdef __DEBUG_LOG__
|
||||||
|
PrintFormat("%s: symbol = %s, volume = %f, price = %f, sl = %f, tp = %f, comment = %s",
|
||||||
|
__FUNCTION__, symbol, volume, price, sl, tp, comment);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
CTrade trade;
|
||||||
|
bool ok = trade.Sell(volume, symbol, price, sl, tp, comment);
|
||||||
|
|
||||||
|
MqlTradeResult trade_result={0};
|
||||||
|
trade.Result(trade_result);
|
||||||
|
|
||||||
|
JSONObject* result_value_jo = new JSONObject();
|
||||||
|
result_value_jo.put("RetVal", new JSONBool(ok));
|
||||||
|
result_value_jo.put("TradeResult", MqlTradeResultToJson(trade_result));
|
||||||
|
|
||||||
|
return CreateSuccessResponse("Value", result_value_jo);
|
||||||
|
}
|
||||||
|
|
||||||
//------------ Events -------------------------------------------------------
|
//------------ Events -------------------------------------------------------
|
||||||
|
|
||||||
enum MtEventTypes
|
enum MtEventTypes
|
||||||
|
|||||||
Reference in New Issue
Block a user