MQL5: update function to use new protocol

This commit is contained in:
Viacheslav Demydiuk
2024-01-14 21:57:56 +02:00
parent caac11cdad
commit 0edb00beb3
+181 -195
View File
@@ -361,7 +361,6 @@ void OnTimer()
} }
} }
int executeCommand() int executeCommand()
{ {
int commandType = 0; int commandType = 0;
@@ -381,7 +380,7 @@ int executeCommand()
} }
#endif #endif
string response = CreateSuccessResponse(); string response = "";
switch (commandType) switch (commandType)
{ {
@@ -395,7 +394,7 @@ int executeCommand()
response = Execute_GetQuote(); response = Execute_GetQuote();
break; break;
case 63: //OrderCloseAll case 63: //OrderCloseAll
Execute_OrderCloseAll(); response = Execute_OrderCloseAll();
break; break;
case 64: //PositionClose case 64: //PositionClose
Execute_PositionClose(); Execute_PositionClose();
@@ -488,13 +487,13 @@ int executeCommand()
Execute_HistoryDealGetString(); Execute_HistoryDealGetString();
break; break;
case 32: //AccountInfoDouble case 32: //AccountInfoDouble
Execute_AccountInfoDouble(); response = Execute_AccountInfoDouble();
break; break;
case 33: //AccountInfoInteger case 33: //AccountInfoInteger
Execute_AccountInfoInteger(); response = Execute_AccountInfoInteger();
break; break;
case 34: //AccountInfoString case 34: //AccountInfoString
Execute_AccountInfoString(); response = Execute_AccountInfoString();
break; break;
case 35: //SeriesInfoInteger case 35: //SeriesInfoInteger
Execute_SeriesInfoInteger(); Execute_SeriesInfoInteger();
@@ -999,9 +998,12 @@ int executeCommand()
break; break;
} }
if (!sendResponse(ExpertHandle, response, _response_error)) if (response != "")
{ {
PrintResponseError("sendResponse", _response_error); \ if (!sendResponse(ExpertHandle, response, _response_error))
{
PrintResponseError("sendResponse", _response_error); \
}
} }
return (commandType); return (commandType);
@@ -1038,6 +1040,8 @@ int executeCommand()
#define SEND_DOUBLE_RESPONSE(response) SEND_RESPONSE_OR_PRINT_ERROR(sendDoubleResponse, response, __FUNCTION__) #define SEND_DOUBLE_RESPONSE(response) SEND_RESPONSE_OR_PRINT_ERROR(sendDoubleResponse, response, __FUNCTION__)
#define SEND_STRING_RESPONSE(response) SEND_RESPONSE_OR_PRINT_ERROR(sendStringResponse, response, __FUNCTION__) #define SEND_STRING_RESPONSE(response) SEND_RESPONSE_OR_PRINT_ERROR(sendStringResponse, response, __FUNCTION__)
#define PRINT_MSG_AND_RETURN_VALUE(msg,value) PrintFormat("%s: %s",__FUNCTION__,msg);return value
#define CHECK_JSON_VALUE(jo, name_value, return_fail_result) if (jo.getValue(name_value) == NULL) { PRINT_MSG_AND_RETURN_VALUE(StringFormat("failed to get %s from JSON!", name_value), return_fail_result); }
//------------------------------------------------------------- //-------------------------------------------------------------
string Execute_GetQuote() string Execute_GetQuote()
{ {
@@ -1048,16 +1052,15 @@ string Execute_GetQuote()
return CreateSuccessResponse(quote.CreateJson()); return CreateSuccessResponse(quote.CreateJson());
} }
void Execute_Request() string Execute_Request()
{ {
string request; string request;
StringInit(request, 1000, 0); StringInit(request, 1000, 0);
if (!getStringValue(ExpertHandle, 0, request, _error)) if (!getPayload(ExpertHandle, request, _error))
{ {
PrintParamError("Request", "Request", _error); PrintParamError("Request", "Request", _error);
sendErrorResponse(ExpertHandle, -1, _error, _response_error); return CreateErrorResponse(-1, "Failed to get request");
return;
} }
string response = ""; string response = "";
@@ -1069,155 +1072,148 @@ void Execute_Request()
response = OnRequest(request); response = OnRequest(request);
} }
if (!sendStringResponse(ExpertHandle, response, _response_error)) return response;
{
PrintResponseError("Request", _response_error);
}
} }
void Execute_OrderSend() string Execute_OrderCloseAll()
{ {
MqlTradeRequest request; OrderCloseAll();
ReadMqlTradeRequestFromCommand(request); return CreateSuccessResponse();
MqlTradeResult result;
bool retVal = OrderSend(request, result);
if (!sendStringResponse(ExpertHandle, ResultToString(retVal, result), _response_error))
{
PrintResponseError("OrderSend", _response_error);
}
} }
void Execute_OrderCloseAll() JSONObject* GetPayload()
{ {
if (!sendBooleanResponse(ExpertHandle, OrderCloseAll(), _response_error)) string payload;
StringInit(payload, 5000, 0);
if (!getPayload(ExpertHandle, payload, _error))
{ {
PrintResponseError("OrderCloseAll", _response_error); PrintParamError("GetPayload", "GetPayload", _error);
return NULL;
} }
JSONParser payload_parser;
JSONValue *payload_json = payload_parser.parse(payload);
if (payload_json == NULL)
{
PrintFormat("%s [ERROR]: %d - %s", __FUNCTION__, (string)payload_parser.getErrorCode(), payload_parser.getErrorMessage());
return NULL;
}
return payload_json.isObject() ? payload_json : NULL;
} }
void Execute_PositionClose() string Execute_PositionClose()
{ {
ulong ticket; JSONObject *jo = GetPayload();
ulong deviation; if (jo == NULL)
return CreateErrorResponse(-1, "Failed to get payload");
//Ticket
CHECK_JSON_VALUE(jo, "Ticket", CreateErrorResponse(-1, "Undefinded mandatory parameter Ticket"));
ulong ticket = jo.getLong("Ticket");
//Deviation
CHECK_JSON_VALUE(jo, "Ticket", CreateErrorResponse(-1, "Undefinded mandatory parameter Deviation"));
ulong deviation = jo.getLong("Deviation");
delete jo;
CTrade trade; CTrade trade;
bool result = trade.PositionClose(ticket, deviation);
if (!getULongValue(ExpertHandle, 0, ticket, _error)) return CreateSuccessResponse(new JSONBool(result));
{
PrintParamError("PositionClose", "ticket", _error);
sendErrorResponse(ExpertHandle, -1, _error, _response_error);
return;
}
if (!getULongValue(ExpertHandle, 1, deviation, _error))
{
PrintParamError("PositionClose", "deviation", _error);
sendErrorResponse(ExpertHandle, -1, _error, _response_error);
return;
}
if (!sendBooleanResponse(ExpertHandle, trade.PositionClose(ticket, deviation), _response_error))
{
PrintResponseError("PositionClose", _response_error);
}
} }
void Execute_OrderCalcMargin() string Execute_OrderCalcMargin()
{ {
int action; JSONObject *jo = GetPayload();
string symbol; if (jo == NULL)
double volume; return CreateErrorResponse(-1, "Failed to get payload");
double price;
StringInit(symbol, 100, 0);
if (!getIntValue(ExpertHandle, 0, action, _error)) //Action
{ CHECK_JSON_VALUE(jo, "Action", CreateErrorResponse(-1, "Undefinded mandatory parameter Action"));
PrintParamError("OrderCalcMargin", "action", _error); int action = jo.getInt("Action");
sendErrorResponse(ExpertHandle, -1, _error, _response_error);
return; //Symbol
} CHECK_JSON_VALUE(jo, "Symbol", CreateErrorResponse(-1, "Undefinded mandatory parameter Symbol"));
if (!getStringValue(ExpertHandle, 1, symbol, _error)) string symbol = jo.getString("Symbol");
{
PrintParamError("OrderCalcMargin", "symbol", _error); //Volume
sendErrorResponse(ExpertHandle, -1, _error, _response_error); CHECK_JSON_VALUE(jo, "Volume", CreateErrorResponse(-1, "Undefinded mandatory parameter Volume"));
return; double volume = jo.getDouble("Volume");
}
if (!getDoubleValue(ExpertHandle, 2, volume, _error)) //Price
{ CHECK_JSON_VALUE(jo, "Price", CreateErrorResponse(-1, "Undefinded mandatory parameter Price"));
PrintParamError("OrderCalcMargin", "volume", _error); double price = jo.getDouble("Price");
sendErrorResponse(ExpertHandle, -1, _error, _response_error);
return; delete jo;
}
if (!getDoubleValue(ExpertHandle, 3, price, _error))
{
PrintParamError("OrderCalcMargin", "price", _error);
sendErrorResponse(ExpertHandle, -1, _error, _response_error);
return;
}
double margin; double margin;
bool retVal = OrderCalcMargin((ENUM_ORDER_TYPE)action, symbol, volume, price, margin); bool ok = OrderCalcMargin((ENUM_ORDER_TYPE)action, symbol, volume, price, margin);
if (!sendStringResponse(ExpertHandle, ResultToString(retVal, margin), _response_error)) #ifdef __DEBUG_LOG__
{ PrintFormat("%s: return value = %s", __FUNCTION__, ok ? "true" : "false");
PrintResponseError("OrderCalcMargin", _response_error); #endif
}
JSONObject* result_value_jo = new JSONObject();
result_value_jo.put("RetVal", new JSONBool(ok));
result_value_jo.put("Result", new JSONNumber(margin));
return CreateSuccessResponse(result_value_jo);
} }
void Execute_OrderCalcProfit() string Execute_OrderCalcProfit()
{ {
int action; JSONObject *jo = GetPayload();
string symbol; if (jo == NULL)
double volume; return CreateErrorResponse(-1, "Failed to get payload");
double price_open;
double price_close;
StringInit(symbol, 100, 0);
if (!getIntValue(ExpertHandle, 0, action, _error)) //Action
{ CHECK_JSON_VALUE(jo, "Action", CreateErrorResponse(-1, "Undefinded mandatory parameter Action"));
PrintParamError("OrderCalcProfit", "action", _error); int action = jo.getInt("Action");
sendErrorResponse(ExpertHandle, -1, _error, _response_error);
return; //Symbol
} CHECK_JSON_VALUE(jo, "Symbol", CreateErrorResponse(-1, "Undefinded mandatory parameter Symbol"));
if (!getStringValue(ExpertHandle, 1, symbol, _error)) string symbol = jo.getString("Symbol");
{
PrintParamError("OrderCalcProfit", "symbol", _error); //Volume
sendErrorResponse(ExpertHandle, -1, _error, _response_error); CHECK_JSON_VALUE(jo, "Volume", CreateErrorResponse(-1, "Undefinded mandatory parameter Volume"));
return; double volume = jo.getDouble("Volume");
}
if (!getDoubleValue(ExpertHandle, 2, volume, _error)) //PriceOpen
{ CHECK_JSON_VALUE(jo, "PriceOpen", CreateErrorResponse(-1, "Undefinded mandatory parameter PriceOpen"));
PrintParamError("OrderCalcProfit", "volume", _error); double price_open = jo.getDouble("PriceOpen");
sendErrorResponse(ExpertHandle, -1, _error, _response_error);
return; //PriceClose
} CHECK_JSON_VALUE(jo, "PriceClose", CreateErrorResponse(-1, "Undefinded mandatory parameter PriceClose"));
if (!getDoubleValue(ExpertHandle, 3, price_open, _error)) double price_close = jo.getDouble("PriceClose");
{
PrintParamError("OrderCalcProfit", "price_open", _error); delete jo;
sendErrorResponse(ExpertHandle, -1, _error, _response_error);
return;
}
if (!getDoubleValue(ExpertHandle, 4, price_close, _error))
{
PrintParamError("OrderCalcProfit", "price_close", _error);
sendErrorResponse(ExpertHandle, -1, _error, _response_error);
return;
}
double profit; double profit;
bool retVal = OrderCalcProfit((ENUM_ORDER_TYPE)action, symbol, volume, price_open, price_close, profit); bool ok = OrderCalcProfit((ENUM_ORDER_TYPE)action, symbol, volume, price_open, price_close, profit);
if (!sendStringResponse(ExpertHandle, ResultToString(retVal, profit), _response_error)) #ifdef __DEBUG_LOG__
{ PrintFormat("%s: return value = %s", __FUNCTION__, ok ? "true" : "false");
PrintResponseError("OrderCalcProfit", _response_error); #endif
}
JSONObject* result_value_jo = new JSONObject();
result_value_jo.put("RetVal", new JSONBool(ok));
result_value_jo.put("Result", new JSONNumber(profit));
return CreateSuccessResponse(result_value_jo);
} }
void Execute_PositionGetTicket() string Execute_PositionGetTicket()
{ {
int index; JSONObject *jo = GetPayload();
GET_INT_VALUE(0, index, "index"); if (jo == NULL)
return CreateErrorResponse(-1, "Failed to get payload");
//Index
CHECK_JSON_VALUE(jo, "Index", CreateErrorResponse(-1, "Undefinded mandatory parameter Index"));
int index = jo.getInt("Index");
#ifdef __DEBUG_LOG__ #ifdef __DEBUG_LOG__
PrintFormat("%s: index = %d", __FUNCTION__, index); PrintFormat("%s: index = %d", __FUNCTION__, index);
@@ -1229,31 +1225,27 @@ void Execute_PositionGetTicket()
PrintFormat("%s: result = %u", __FUNCTION__, result); PrintFormat("%s: result = %u", __FUNCTION__, result);
#endif #endif
SEND_ULONG_RESPONSE(result); return CreateSuccessResponse(new JSONNumber(result));
} }
void Execute_PositionsTotal() string Execute_PositionsTotal()
{ {
if (!sendIntResponse(ExpertHandle, PositionsTotal(), _response_error)) int result = PositionsTotal();
{ return CreateSuccessResponse(new JSONNumber(result));
PrintResponseError("PositionsTotal", _response_error);
}
} }
void Execute_PositionGetSymbol() string Execute_PositionGetSymbol()
{ {
int index; JSONObject *jo = GetPayload();
if (!getIntValue(ExpertHandle, 0, index, _error)) if (jo == NULL)
{ return CreateErrorResponse(-1, "Failed to get payload");
PrintParamError("PositionGetSymbol", "index", _error);
sendErrorResponse(ExpertHandle, -1, _error, _response_error);
return;
}
if (!sendStringResponse(ExpertHandle, PositionGetSymbol(index), _response_error)) //Index
{ CHECK_JSON_VALUE(jo, "Index", CreateErrorResponse(-1, "Undefinded mandatory parameter Index"));
PrintResponseError("PositionGetSymbol", _response_error); int index = jo.getInt("Index");
}
string symbol = PositionGetSymbol(index);
return CreateSuccessResponse(new JSONString(symbol));
} }
void Execute_PositionSelect() void Execute_PositionSelect()
@@ -1682,55 +1674,52 @@ void Execute_HistoryDealGetString()
} }
} }
void Execute_AccountInfoDouble() string Execute_AccountInfoDouble()
{ {
int property_id; JSONObject *jo = GetPayload();
if (jo == NULL)
return CreateErrorResponse(-1, "Failed to get payload");
if (!getIntValue(ExpertHandle, 0, property_id, _error)) //PropertyId
{ CHECK_JSON_VALUE(jo, "PropertyId", CreateErrorResponse(-1, "Undefinded mandatory parameter PropertyId"));
PrintParamError("AccountInfoDouble", "property_id", _error); int property_id = jo.getLong("PropertyId");
sendErrorResponse(ExpertHandle, -1, _error, _response_error);
return;
}
if (!sendDoubleResponse(ExpertHandle, AccountInfoDouble((ENUM_ACCOUNT_INFO_DOUBLE)property_id), _response_error)) delete jo;
{
PrintResponseError("AccountInfoDouble", _response_error); double result = AccountInfoDouble((ENUM_ACCOUNT_INFO_DOUBLE)property_id);
} return CreateSuccessResponse(new JSONNumber(result));
} }
void Execute_AccountInfoInteger() string Execute_AccountInfoInteger()
{ {
int property_id; JSONObject *jo = GetPayload();
if (jo == NULL)
return CreateErrorResponse(-1, "Failed to get payload");
if (!getIntValue(ExpertHandle, 0, property_id, _error)) //PropertyId
{ CHECK_JSON_VALUE(jo, "PropertyId", CreateErrorResponse(-1, "Undefinded mandatory parameter PropertyId"));
PrintParamError("AccountInfoInteger", "property_id", _error); int property_id = jo.getLong("PropertyId");
sendErrorResponse(ExpertHandle, -1, _error, _response_error);
return;
}
if (!sendLongResponse(ExpertHandle, AccountInfoInteger((ENUM_ACCOUNT_INFO_INTEGER)property_id), _response_error)) delete jo;
{
PrintResponseError("AccountInfoInteger", _response_error); int result = AccountInfoInteger((ENUM_ACCOUNT_INFO_INTEGER)property_id);
} return CreateSuccessResponse(new JSONNumber(result));
} }
void Execute_AccountInfoString() string Execute_AccountInfoString()
{ {
int property_id; JSONObject *jo = GetPayload();
if (jo == NULL)
return CreateErrorResponse(-1, "Failed to get payload");
if (!getIntValue(ExpertHandle, 0, property_id, _error)) //PropertyId
{ CHECK_JSON_VALUE(jo, "PropertyId", CreateErrorResponse(-1, "Undefinded mandatory parameter PropertyId"));
PrintParamError("AccountInfoString", "property_id", _error); int property_id = jo.getLong("PropertyId");
sendErrorResponse(ExpertHandle, -1, _error, _response_error);
return;
}
if (!sendStringResponse(ExpertHandle, AccountInfoString((ENUM_ACCOUNT_INFO_STRING)property_id), _response_error)) delete jo;
{
PrintResponseError("AccountInfoString", _response_error); string result = AccountInfoString((ENUM_ACCOUNT_INFO_STRING)property_id);
} return CreateSuccessResponse(new JSONString(result));
} }
void Execute_SeriesInfoInteger() void Execute_SeriesInfoInteger()
@@ -7240,9 +7229,6 @@ int iCustomT(string symbol, ENUM_TIMEFRAMES timeframe, string name, T &p[], int
} }
} }
#define PRINT_MSG_AND_RETURN_VALUE(msg,value) PrintFormat("%s: %s",__FUNCTION__,msg);return value
#define CHECK_JSON_VALUE(jo, name_value, return_fail_result) if (jo.getValue(name_value) == NULL) { PRINT_MSG_AND_RETURN_VALUE(StringFormat("failed to get %s from JSON!", name_value), return_fail_result); }
string ExecuteRequest_OrderSend(JSONObject *jo) string ExecuteRequest_OrderSend(JSONObject *jo)
{ {
CHECK_JSON_VALUE(jo, "TradeRequest", CreateErrorResponse(-1, "Undefinded mandatory parameter TradeRequest")); CHECK_JSON_VALUE(jo, "TradeRequest", CreateErrorResponse(-1, "Undefinded mandatory parameter TradeRequest"));
@@ -7932,12 +7918,12 @@ bool JsonToMqlTradeRequest(JSONObject *jo, MqlTradeRequest& request)
JSONObject* MqlTickToJson(const MqlTick& tick) JSONObject* MqlTickToJson(const MqlTick& tick)
{ {
JSONObject *jo = new JSONObject(); JSONObject *jo = new JSONObject();
jo.put("MtTime", new JSONNumber(tick.time)); jo.put("Time", new JSONNumber(tick.time));
jo.put("bid", new JSONNumber(tick.bid)); jo.put("Bid", new JSONNumber(tick.bid));
jo.put("ask", new JSONNumber(tick.ask)); jo.put("Ask", new JSONNumber(tick.ask));
jo.put("last", new JSONNumber(tick.last)); jo.put("Last", new JSONNumber(tick.last));
jo.put("volume", new JSONNumber(tick.volume)); jo.put("Volume", new JSONNumber(tick.volume));
jo.put("volume_real", new JSONNumber(tick.volume_real)); jo.put("VolumeReal", new JSONNumber(tick.volume_real));
return jo; return jo;
} }