mirror of
https://github.com/vdemydiuk/mtapi.git
synced 2026-07-28 02:57:56 +00:00
Issue #204: [MT5] Added function OrderSendAsync
This commit is contained in:
@@ -89,6 +89,7 @@
|
||||
<Compile Include="Requests\MarketBookGetRequest.cs" />
|
||||
<Compile Include="Requests\OrderCheckRequest.cs" />
|
||||
<Compile Include="Requests\OrderCheckResult.cs" />
|
||||
<Compile Include="Requests\OrderSendAsyncRequest.cs" />
|
||||
<Compile Include="Requests\OrderSendRequest.cs" />
|
||||
<Compile Include="Requests\PositionCloseRequest.cs" />
|
||||
<Compile Include="Requests\PositionCloseResult.cs" />
|
||||
|
||||
@@ -134,6 +134,38 @@ namespace MtApi5
|
||||
return response != null && response.RetVal;
|
||||
}
|
||||
|
||||
///<summary>
|
||||
///Function is used for conducting asynchronous trade operations without waiting for the trade server's response to a sent request.
|
||||
///</summary>
|
||||
///<param name="request">Reference to a object of MqlTradeRequest type describing the trade activity of the client.</param>
|
||||
///<param name="result">Reference to a object of MqlTradeResult type describing the result of trade operation in case of a successful completion (if true is returned).</param>
|
||||
/// <returns>
|
||||
/// Returns true if the request is sent to a trade server. In case the request is not sent, it returns false.
|
||||
/// In case the request is sent, in the result variable the response code contains TRADE_RETCODE_PLACED value (code 10008) – "order placed".
|
||||
/// Successful execution means only the fact of sending, but does not give any guarantee that the request has reached the trade server and has been accepted for processing.
|
||||
/// When processing the received request, a trade server sends a reply to a client terminal notifying of change in the current state of positions,
|
||||
/// orders and deals, which leads to the generation of the Trade event.
|
||||
/// </returns>
|
||||
public bool OrderSendAsync(MqlTradeRequest request, out MqlTradeResult result)
|
||||
{
|
||||
Log.Debug($"OrderSend: request = {request}");
|
||||
|
||||
if (request == null)
|
||||
{
|
||||
Log.Warn("OrderSend: request is not defined!");
|
||||
result = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
var response = SendRequest<OrderSendResult>(new OrderSendAsyncRequest
|
||||
{
|
||||
TradeRequest = request
|
||||
});
|
||||
|
||||
result = response?.TradeResult;
|
||||
return response != null && response.RetVal;
|
||||
}
|
||||
|
||||
///<summary>
|
||||
///The function calculates the margin required for the specified order type, on the current account
|
||||
///, in the current market environment not taking into account current pending orders and open positions
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace MtApi5.Requests
|
||||
{
|
||||
internal class OrderSendAsyncRequest : RequestBase
|
||||
{
|
||||
public override RequestType RequestType => RequestType.OrderSendAsync;
|
||||
|
||||
public MqlTradeRequest TradeRequest { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -18,6 +18,7 @@ namespace MtApi5.Requests
|
||||
PositionClose = 11,
|
||||
SymbolInfoTick = 12,
|
||||
Buy = 13,
|
||||
Sell = 14
|
||||
Sell = 14,
|
||||
OrderSendAsync = 15
|
||||
}
|
||||
}
|
||||
Binary file not shown.
+29
-2
@@ -1,7 +1,7 @@
|
||||
#property copyright "Vyacheslav Demidyuk"
|
||||
#property link ""
|
||||
|
||||
#property version "1.8"
|
||||
#property version "1.9"
|
||||
#property description "MtApi (MT5) connection expert"
|
||||
|
||||
#include <json.mqh>
|
||||
@@ -6961,7 +6961,10 @@ string OnRequest(string json)
|
||||
break;
|
||||
case 14: //Sell
|
||||
response = ExecuteRequest_Sell(jo);
|
||||
break;
|
||||
break;
|
||||
case 15: //OrderSendAsync
|
||||
response = ExecuteRequest_OrderSendAsync(jo);
|
||||
break;
|
||||
default:
|
||||
PrintFormat("%s [WARNING]: Unknown request type %d", __FUNCTION__, requestType);
|
||||
response = CreateErrorResponse(-1, "Unknown request type");
|
||||
@@ -7171,6 +7174,30 @@ string ExecuteRequest_OrderSend(JSONObject *jo)
|
||||
return CreateSuccessResponse("Value", result_value_jo);
|
||||
}
|
||||
|
||||
string ExecuteRequest_OrderSendAsync(JSONObject *jo)
|
||||
{
|
||||
CHECK_JSON_VALUE(jo, "TradeRequest", CreateErrorResponse(-1, "Undefinded mandatory parameter TradeRequest"));
|
||||
JSONObject* trade_request_jo = jo.getObject("TradeRequest");
|
||||
|
||||
MqlTradeRequest trade_request = {0};
|
||||
bool converted = JsonToMqlTradeRequest(trade_request_jo, trade_request);
|
||||
if (converted == false)
|
||||
return CreateErrorResponse(-1, "Failed to parse parameter TradeRequest");
|
||||
|
||||
MqlTradeResult trade_result = {0};
|
||||
bool ok = OrderSendAsync(trade_request, trade_result);
|
||||
|
||||
JSONObject* result_value_jo = new JSONObject();
|
||||
result_value_jo.put("RetVal", new JSONBool(ok));
|
||||
result_value_jo.put("TradeResult", MqlTradeResultToJson(trade_result));
|
||||
|
||||
#ifdef __DEBUG_LOG__
|
||||
PrintFormat("%s: return value = %s", __FUNCTION__, ok ? "true" : "false");
|
||||
#endif
|
||||
|
||||
return CreateSuccessResponse("Value", result_value_jo);
|
||||
}
|
||||
|
||||
string ExecuteRequest_PositionOpen(JSONObject *jo)
|
||||
{
|
||||
//Symbol
|
||||
|
||||
Reference in New Issue
Block a user