mirror of
https://github.com/vdemydiuk/mtapi.git
synced 2026-08-16 04:08:10 +00:00
Issue #91: Added MarketBookGetRequest to perform function MarketBookGet (MT5)
This commit is contained in:
@@ -1,8 +1,4 @@
|
|||||||
using System;
|
// ReSharper disable InconsistentNaming
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
|
|
||||||
namespace MtApi5
|
namespace MtApi5
|
||||||
{
|
{
|
||||||
public class MqlBookInfo
|
public class MqlBookInfo
|
||||||
@@ -14,8 +10,13 @@ namespace MtApi5
|
|||||||
this.volume = volume;
|
this.volume = volume;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ENUM_BOOK_TYPE type { get; private set; } // Order type from ENUM_BOOK_TYPE enumeration
|
public ENUM_BOOK_TYPE type { get; } // Order type from ENUM_BOOK_TYPE enumeration
|
||||||
public double price { get; private set; } // Price
|
public double price { get; } // Price
|
||||||
public long volume { get; private set; } // Volume
|
public long volume { get; } // Volume
|
||||||
|
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
return $"{type}|{price}|{volume}";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -95,7 +95,7 @@ namespace MtApi5
|
|||||||
SymbolInfoSessionTrade = 59,
|
SymbolInfoSessionTrade = 59,
|
||||||
MarketBookAdd = 60,
|
MarketBookAdd = 60,
|
||||||
MarketBookRelease = 61,
|
MarketBookRelease = 61,
|
||||||
MarketBookGet = 62,
|
//MarketBookGet = 62,
|
||||||
OrderCloseAll = 63,
|
OrderCloseAll = 63,
|
||||||
|
|
||||||
//CTrade
|
//CTrade
|
||||||
|
|||||||
@@ -68,6 +68,7 @@
|
|||||||
<Compile Include="Mt5Quote.cs" />
|
<Compile Include="Mt5Quote.cs" />
|
||||||
<Compile Include="Requests\CopyTicksRequest.cs" />
|
<Compile Include="Requests\CopyTicksRequest.cs" />
|
||||||
<Compile Include="Requests\ICustomRequest.cs" />
|
<Compile Include="Requests\ICustomRequest.cs" />
|
||||||
|
<Compile Include="Requests\MarketBookGetRequest.cs" />
|
||||||
<Compile Include="Requests\OrderCheckRequest.cs" />
|
<Compile Include="Requests\OrderCheckRequest.cs" />
|
||||||
<Compile Include="Requests\OrderCheckResult.cs" />
|
<Compile Include="Requests\OrderCheckResult.cs" />
|
||||||
<Compile Include="Requests\OrderSendRequest.cs" />
|
<Compile Include="Requests\OrderSendRequest.cs" />
|
||||||
|
|||||||
+5
-14
@@ -1434,22 +1434,13 @@ namespace MtApi5
|
|||||||
///<param name="book">Reference to an array of Depth of Market records.</param>
|
///<param name="book">Reference to an array of Depth of Market records.</param>
|
||||||
public bool MarketBookGet(string symbol, out MqlBookInfo[] book)
|
public bool MarketBookGet(string symbol, out MqlBookInfo[] book)
|
||||||
{
|
{
|
||||||
var commandParameters = new ArrayList { symbol };
|
var response = SendRequest<List<MqlBookInfo>>(new MarketBookGetRequest
|
||||||
|
|
||||||
var retVal = SendCommand<MtMqlBookInfo[]>(Mt5CommandType.MarketBookGet, commandParameters);
|
|
||||||
|
|
||||||
book = null;
|
|
||||||
if (retVal != null)
|
|
||||||
{
|
{
|
||||||
book = new MqlBookInfo[retVal.Length];
|
Symbol = symbol
|
||||||
|
});
|
||||||
|
|
||||||
foreach (var t in retVal)
|
book = response?.ToArray();
|
||||||
{
|
return response != null;
|
||||||
book[0] = new MqlBookInfo((ENUM_BOOK_TYPE)t.type, t.price, t.volume);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return book != null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
namespace MtApi5.Requests
|
||||||
|
{
|
||||||
|
internal class MarketBookGetRequest: RequestBase
|
||||||
|
{
|
||||||
|
public override RequestType RequestType => RequestType.MarketBookGet;
|
||||||
|
|
||||||
|
public string Symbol { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,6 +9,7 @@ namespace MtApi5.Requests
|
|||||||
iCustom = 2,
|
iCustom = 2,
|
||||||
OrderSend = 3,
|
OrderSend = 3,
|
||||||
PositionOpen = 4,
|
PositionOpen = 4,
|
||||||
OrderCheck = 5
|
OrderCheck = 5,
|
||||||
|
MarketBookGet = 6
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -807,14 +807,14 @@ namespace MtApi5TestClient
|
|||||||
|
|
||||||
private async void ExecuteMarketBookAdd(object o)
|
private async void ExecuteMarketBookAdd(object o)
|
||||||
{
|
{
|
||||||
var retVal = await Execute(() => _mtApiClient.MarketBookAdd("CHFJPY"));
|
var retVal = await Execute(() => _mtApiClient.MarketBookAdd("EURUSD"));
|
||||||
AddLog($"MarketBookAdd(CHFJPY): result = {retVal}");
|
AddLog($"MarketBookAdd(EURUSD): result = {retVal}");
|
||||||
}
|
}
|
||||||
|
|
||||||
private async void ExecuteMarketBookRelease(object o)
|
private async void ExecuteMarketBookRelease(object o)
|
||||||
{
|
{
|
||||||
var retVal = await Execute(() => _mtApiClient.MarketBookRelease("CHFJPY"));
|
var retVal = await Execute(() => _mtApiClient.MarketBookRelease("EURUSD"));
|
||||||
AddLog($"MarketBookRelease(CHFJPY): result = {retVal}");
|
AddLog($"MarketBookRelease(EURUSD): result = {retVal}");
|
||||||
}
|
}
|
||||||
|
|
||||||
private async void ExecuteMarketBookGet(object o)
|
private async void ExecuteMarketBookGet(object o)
|
||||||
@@ -832,13 +832,11 @@ namespace MtApi5TestClient
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
AddLog("MarketBookGet(EURUSD): success");
|
AddLog($"MarketBookGet(EURUSD): success. Count = {result.Length}");
|
||||||
|
|
||||||
for (var i = 0; i < result.Length; i++)
|
for (var i = 0; i < result.Length; i++)
|
||||||
{
|
{
|
||||||
AddLog($"MarketBookGet: book[{i}].price = {result[i].price}");
|
AddLog($"MarketBookGet: [{i}] - {result[i].price} | {result[i].volume} | {result[i].type}");
|
||||||
AddLog($"MarketBookGet: book[{i}].price = {result[i].volume}");
|
|
||||||
AddLog($"MarketBookGet: book[{i}].price = {result[i].type}");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1039,6 +1037,10 @@ namespace MtApi5TestClient
|
|||||||
{
|
{
|
||||||
result = func();
|
result = func();
|
||||||
}
|
}
|
||||||
|
catch (ExecutionException ex)
|
||||||
|
{
|
||||||
|
AddLog($"Exception: {ex.ErrorCode} - {ex.Message}");
|
||||||
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
AddLog($"Exception: {ex.Message}");
|
AddLog($"Exception: {ex.Message}");
|
||||||
|
|||||||
Binary file not shown.
+44
-37
@@ -45,7 +45,6 @@ input int Port = 8228;
|
|||||||
|
|
||||||
int ExpertHandle;
|
int ExpertHandle;
|
||||||
|
|
||||||
//string message;
|
|
||||||
string _error;
|
string _error;
|
||||||
string _response_error;
|
string _response_error;
|
||||||
bool isCrashed = false;
|
bool isCrashed = false;
|
||||||
@@ -482,9 +481,8 @@ int executeCommand()
|
|||||||
case 61: //MarketBookRelease
|
case 61: //MarketBookRelease
|
||||||
Execute_MarketBookRelease();
|
Execute_MarketBookRelease();
|
||||||
break;
|
break;
|
||||||
case 62: //MarketBookGet
|
// case 62: //MarketBookGet
|
||||||
Execute_MarketBookGet();
|
// break;
|
||||||
break;
|
|
||||||
case 65: //PositionOpen
|
case 65: //PositionOpen
|
||||||
Execute_PositionOpen(false);
|
Execute_PositionOpen(false);
|
||||||
break;
|
break;
|
||||||
@@ -3036,38 +3034,6 @@ void Execute_MarketBookRelease()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Execute_MarketBookGet()
|
|
||||||
{
|
|
||||||
string symbol;
|
|
||||||
StringInit(symbol, 100, 0);
|
|
||||||
|
|
||||||
if (!getStringValue(ExpertHandle, 0, symbol, _error))
|
|
||||||
{
|
|
||||||
PrintParamError("MarketBookGet", "symbol", _error);
|
|
||||||
sendErrorResponse(ExpertHandle, -1, _error, _response_error);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
MqlBookInfo book[];
|
|
||||||
bool retVal = MarketBookGet(symbol, book);
|
|
||||||
|
|
||||||
if(retVal)
|
|
||||||
{
|
|
||||||
int size = ArraySize(book);
|
|
||||||
if (!sendMqlBookInfoArrayResponse(ExpertHandle, book, size, _response_error))
|
|
||||||
{
|
|
||||||
PrintResponseError("MarketBookGet", _response_error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (!sendVoidResponse(ExpertHandle, _response_error))
|
|
||||||
{
|
|
||||||
PrintResponseError("MarketBookGet", _response_error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void Execute_PositionOpen(bool isTradeResultRequired)
|
void Execute_PositionOpen(bool isTradeResultRequired)
|
||||||
{
|
{
|
||||||
string symbol;
|
string symbol;
|
||||||
@@ -5634,6 +5600,9 @@ string OnRequest(string json)
|
|||||||
case 5: //OrderCheck
|
case 5: //OrderCheck
|
||||||
response = ExecuteRequest_OrderCheck(jo);
|
response = ExecuteRequest_OrderCheck(jo);
|
||||||
break;
|
break;
|
||||||
|
case 6: //MarketBookGet
|
||||||
|
response = ExecuteRequest_MarketBookGet(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");
|
||||||
@@ -5719,7 +5688,7 @@ string ExecuteRequest_CopyTicks(JSONObject *jo)
|
|||||||
jaTicks.put(i, Serialize(ticks[i]));
|
jaTicks.put(i, Serialize(ticks[i]));
|
||||||
}
|
}
|
||||||
|
|
||||||
return CreateSuccessResponse("Value", jaTicks);;
|
return CreateSuccessResponse("Value", jaTicks);
|
||||||
}
|
}
|
||||||
|
|
||||||
string ExecuteRequest_iCustom(JSONObject *jo)
|
string ExecuteRequest_iCustom(JSONObject *jo)
|
||||||
@@ -6033,4 +6002,42 @@ string ExecuteRequest_OrderCheck(JSONObject *jo)
|
|||||||
result_value_jo.put("TradeCheckResult", MqlTradeCheckResultToJson(trade_check_result));
|
result_value_jo.put("TradeCheckResult", MqlTradeCheckResultToJson(trade_check_result));
|
||||||
|
|
||||||
return CreateSuccessResponse("Value", result_value_jo);
|
return CreateSuccessResponse("Value", result_value_jo);
|
||||||
|
}
|
||||||
|
|
||||||
|
JSONObject* MqlBookInfoToJson(MqlBookInfo& info)
|
||||||
|
{
|
||||||
|
JSONObject *jo = new JSONObject();
|
||||||
|
jo.put("type", new JSONNumber((int)info.type));
|
||||||
|
jo.put("price", new JSONNumber(info.price));
|
||||||
|
jo.put("volume", new JSONNumber(info.volume));
|
||||||
|
return jo;
|
||||||
|
}
|
||||||
|
|
||||||
|
string ExecuteRequest_MarketBookGet(JSONObject *jo)
|
||||||
|
{
|
||||||
|
CHECK_JSON_VALUE(jo, "Symbol", CreateErrorResponse(-1, "Undefinded mandatory parameter Symbol"));
|
||||||
|
string symbol = jo.getString("Symbol");
|
||||||
|
|
||||||
|
MqlBookInfo info_array[];
|
||||||
|
bool ok = MarketBookGet(symbol, info_array);
|
||||||
|
|
||||||
|
#ifdef __DEBUG_LOG__
|
||||||
|
PrintFormat("%s: return value = %s.", __FUNCTION__, ok ? "true" : "false");
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if (!ok)
|
||||||
|
return CreateErrorResponse(GetLastError(), "MarketBookGet failed");
|
||||||
|
|
||||||
|
int size = ArraySize(info_array);
|
||||||
|
JSONArray* book_ja = new JSONArray();
|
||||||
|
for(int i = 0; i < size; i++)
|
||||||
|
{
|
||||||
|
book_ja.put(i, MqlBookInfoToJson(info_array[i]));
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef __DEBUG_LOG__
|
||||||
|
PrintFormat("%s: array size = %d.", __FUNCTION__, size);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return CreateSuccessResponse("Value", book_ja);
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user