diff --git a/MtApi5/MqlTradeResult.cs b/MtApi5/MqlTradeResult.cs index 0d10af81..8e091708 100755 --- a/MtApi5/MqlTradeResult.cs +++ b/MtApi5/MqlTradeResult.cs @@ -42,7 +42,7 @@ namespace MtApi5 public override string ToString() { - return $"Retcode={Retcode}; Deal={Deal}; Order={Order}; Volume={Volume}; Price={Price}; Bid={Bid}; Ask={Ask}; Comment={Comment}; Request_id={Request_id}"; + return $"Retcode={Retcode}; Comment={Comment}; Deal={Deal}; Order={Order}; Volume={Volume}; Price={Price}; Bid={Bid}; Ask={Ask}; Request_id={Request_id}"; } } } diff --git a/MtApi5/MtApi5.csproj b/MtApi5/MtApi5.csproj index 763fc203..18f351c1 100755 --- a/MtApi5/MtApi5.csproj +++ b/MtApi5/MtApi5.csproj @@ -69,6 +69,7 @@ + diff --git a/MtApi5/MtApi5Client.cs b/MtApi5/MtApi5Client.cs index 25463eaf..eec8392b 100755 --- a/MtApi5/MtApi5Client.cs +++ b/MtApi5/MtApi5Client.cs @@ -544,10 +544,26 @@ namespace MtApi5 /// true - successful check of the basic structures, otherwise - false. public bool PositionOpen(string symbol, ENUM_ORDER_TYPE orderType, double volume, double price, double sl, double tp, string comment , out MqlTradeResult result) { - var commandParameters = new ArrayList { symbol, (int)orderType, volume, price, sl, tp, comment }; + Log.Debug($"PositionOpen: symbol = {symbol}, orderType = {orderType}, volume = {volume}, price = {price}, sl = {sl}, tp = {tp}, comment = {comment}"); - var strResult = SendCommand(Mt5CommandType.PositionOpenWithResult, commandParameters); - return strResult.ParseResult(ParamSeparator, out result); + var response = SendRequest(new PositionOpenRequest + { + Symbol = symbol, + OrderType = orderType, + Volume = volume, + Price = price, + Sl = sl, + Tp = tp, + Comment = comment + }); + + result = response?.TradeResult; + return response != null && response.RetVal; + + //var commandParameters = new ArrayList { symbol, (int)orderType, volume, price, sl, tp, comment }; + + //var strResult = SendCommand(Mt5CommandType.PositionOpenWithResult, commandParameters); + //return strResult.ParseResult(ParamSeparator, out result); } #endregion diff --git a/MtApi5/MtConverters.cs b/MtApi5/MtConverters.cs index d0447d7c..ae7a3d4c 100755 --- a/MtApi5/MtConverters.cs +++ b/MtApi5/MtConverters.cs @@ -14,48 +14,48 @@ namespace MtApi5 return quote != null ? new Mt5Quote(quote.Instrument, quote.Bid, quote.Ask) : null; } - public static bool ParseResult(this string inputString, char separator, out MqlTradeResult result) - { - Log.Debug($"ParseResult: inputString = {inputString}, separator = {separator}"); + //public static bool ParseResult(this string inputString, char separator, out MqlTradeResult result) + //{ + // Log.Debug($"ParseResult: inputString = {inputString}, separator = {separator}"); - var retVal = false; - result = null; + // var retVal = false; + // result = null; - if (string.IsNullOrEmpty(inputString) == false) - { - var values = inputString.Split(separator); - if (values.Length == 10) - { - try - { - retVal = int.Parse(values[0]) != 0; + // if (string.IsNullOrEmpty(inputString) == false) + // { + // var values = inputString.Split(separator); + // if (values.Length == 10) + // { + // try + // { + // retVal = int.Parse(values[0]) != 0; - var retcode = uint.Parse(values[1]); - var deal = ulong.Parse(values[2]); - var order = ulong.Parse(values[3]); - var volume = double.Parse(values[4]); - var price = double.Parse(values[5]); - var bid = double.Parse(values[6]); - var ask = double.Parse(values[7]); - var comment = values[8]; - var requestId = uint.Parse(values[9]); + // var retcode = uint.Parse(values[1]); + // var deal = ulong.Parse(values[2]); + // var order = ulong.Parse(values[3]); + // var volume = double.Parse(values[4]); + // var price = double.Parse(values[5]); + // var bid = double.Parse(values[6]); + // var ask = double.Parse(values[7]); + // var comment = values[8]; + // var requestId = uint.Parse(values[9]); - result = new MqlTradeResult(retcode, deal, order, volume, price, bid, ask, comment, requestId); - } - catch (Exception ex) - { - Log.Error($"ParseResult: {ex.Message}"); - retVal = false; - } - } - } - else - { - Log.Warn("ParseResult: input srting is null or empty!"); - } + // result = new MqlTradeResult(retcode, deal, order, volume, price, bid, ask, comment, requestId); + // } + // catch (Exception ex) + // { + // Log.Error($"ParseResult: {ex.Message}"); + // retVal = false; + // } + // } + // } + // else + // { + // Log.Warn("ParseResult: input srting is null or empty!"); + // } - return retVal; - } + // return retVal; + //} public static bool ParseResult(this string inputString, char separator, out MqlTradeCheckResult result) { diff --git a/MtApi5/Requests/PositionOpenRequest.cs b/MtApi5/Requests/PositionOpenRequest.cs new file mode 100644 index 00000000..289b5516 --- /dev/null +++ b/MtApi5/Requests/PositionOpenRequest.cs @@ -0,0 +1,15 @@ +namespace MtApi5.Requests +{ + internal class PositionOpenRequest: RequestBase + { + public override RequestType RequestType => RequestType.PositionOpen; + + public string Symbol { get; set; } + public ENUM_ORDER_TYPE OrderType { get; set; } + public double Volume { get; set; } + public double Price { get; set; } + public double Sl { get; set; } + public double Tp { get; set; } + public string Comment { get; set; } + } +} \ No newline at end of file diff --git a/MtApi5/Requests/RequestType.cs b/MtApi5/Requests/RequestType.cs index e7f2517c..45ae1599 100755 --- a/MtApi5/Requests/RequestType.cs +++ b/MtApi5/Requests/RequestType.cs @@ -1,13 +1,13 @@ -using System.Diagnostics.CodeAnalysis; +// ReSharper disable InconsistentNaming namespace MtApi5.Requests { - [SuppressMessage("ReSharper", "InconsistentNaming")] internal enum RequestType { Unknown = 0, CopyTicks = 1, iCustom = 2, - OrderSend = 3 + OrderSend = 3, + PositionOpen = 4 } } \ No newline at end of file diff --git a/mq5/MtApi5.ex5 b/mq5/MtApi5.ex5 index 5993d1e7..157704c4 100755 Binary files a/mq5/MtApi5.ex5 and b/mq5/MtApi5.ex5 differ diff --git a/mq5/MtApi5.mq5 b/mq5/MtApi5.mq5 index 9bb80f5d..6a660a01 100755 --- a/mq5/MtApi5.mq5 +++ b/mq5/MtApi5.mq5 @@ -39,7 +39,7 @@ bool getBooleanValue(int expertHandle, int paramIndex, bool& res, string& err); #import -//#define __DEBUG_LOG__ +#define __DEBUG_LOG__ input int Port = 8228; @@ -3112,8 +3112,11 @@ void Execute_PositionOpen(bool isTradeResultRequired) return; } - PrintFormat("Execute_PositionOpen: symbol = %s, order_type = %d, volume = %f, price = %f, sl = %f, tp = %f, comment = %s", - symbol, order_type, volume, price, sl, tp, comment); + +#ifdef __DEBUG_LOG__ + PrintFormat("%s: symbol = %s, order_type = %d, volume = %f, price = %f, sl = %f, tp = %f, comment = %s", + __FUNCTION__, symbol, order_type, volume, price, sl, tp, comment); +#endif CTrade trade; bool ok = trade.PositionOpen(symbol, (ENUM_ORDER_TYPE)order_type, volume, price, sl, tp, comment); @@ -5582,6 +5585,9 @@ string OnRequest(string json) case 3: //OrderSend response = ExecuteRequest_OrderSend(jo); break; + case 4: //PositionOpen + response = ExecuteRequest_PositionOpen(jo); + break; default: PrintFormat("%s [WARNING]: Unknown request type %d", __FUNCTION__, requestType); response = CreateErrorResponse(-1, "Unknown request type"); @@ -5838,9 +5844,11 @@ bool JsonToMqlTradeRequest(JSONObject *jo, MqlTradeRequest& request) request.expiration = (datetime)jo.getInt("MtExpiration"); //Comment - CHECK_JSON_VALUE(jo, "Comment", false); - StringInit(request.comment, 1000, 0); - request.comment = jo.getString("Comment"); + if (jo.getValue("Comment") != NULL) + { + StringInit(request.comment, 1000, 0); + request.comment = jo.getString("Comment"); + } //Position CHECK_JSON_VALUE(jo, "Position", false); @@ -5890,4 +5898,55 @@ string ExecuteRequest_OrderSend(JSONObject *jo) #endif return CreateSuccessResponse("Value", result_value_jo); +} + +string ExecuteRequest_PositionOpen(JSONObject *jo) +{ + //Symbol + CHECK_JSON_VALUE(jo, "Symbol", CreateErrorResponse(-1, "Undefinded mandatory parameter Symbol")); + string symbol = jo.getString("Symbol"); + + //OrderType + CHECK_JSON_VALUE(jo, "OrderType", CreateErrorResponse(-1, "Undefinded mandatory parameter OrderType")); + ENUM_ORDER_TYPE order_type = (ENUM_ORDER_TYPE) jo.getInt("OrderType"); + + //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, order_type = %d, volume = %f, price = %f, sl = %f, tp = %f, comment = %s", + __FUNCTION__, symbol, order_type, volume, price, sl, tp, comment); +#endif + + CTrade trade; + bool ok = trade.PositionOpen(symbol, order_type, volume, 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); } \ No newline at end of file