mirror of
https://github.com/vdemydiuk/mtapi.git
synced 2026-08-17 20:58:11 +00:00
Issue #89: Changed function PositionOpen to use json request/response
This commit is contained in:
@@ -42,7 +42,7 @@ namespace MtApi5
|
|||||||
|
|
||||||
public override string ToString()
|
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}";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -69,6 +69,7 @@
|
|||||||
<Compile Include="Requests\CopyTicksRequest.cs" />
|
<Compile Include="Requests\CopyTicksRequest.cs" />
|
||||||
<Compile Include="Requests\ICustomRequest.cs" />
|
<Compile Include="Requests\ICustomRequest.cs" />
|
||||||
<Compile Include="Requests\OrderSendRequest.cs" />
|
<Compile Include="Requests\OrderSendRequest.cs" />
|
||||||
|
<Compile Include="Requests\PositionOpenRequest.cs" />
|
||||||
<Compile Include="Requests\RequestBase.cs" />
|
<Compile Include="Requests\RequestBase.cs" />
|
||||||
<Compile Include="Requests\RequestType.cs" />
|
<Compile Include="Requests\RequestType.cs" />
|
||||||
<Compile Include="Requests\OrderSendResult.cs" />
|
<Compile Include="Requests\OrderSendResult.cs" />
|
||||||
|
|||||||
+19
-3
@@ -544,10 +544,26 @@ namespace MtApi5
|
|||||||
/// <returns>true - successful check of the basic structures, otherwise - false.</returns>
|
/// <returns>true - successful check of the basic structures, otherwise - false.</returns>
|
||||||
public bool PositionOpen(string symbol, ENUM_ORDER_TYPE orderType, double volume, double price, double sl, double tp, string comment , out MqlTradeResult result)
|
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<string>(Mt5CommandType.PositionOpenWithResult, commandParameters);
|
var response = SendRequest<OrderSendResult>(new PositionOpenRequest
|
||||||
return strResult.ParseResult(ParamSeparator, out result);
|
{
|
||||||
|
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<string>(Mt5CommandType.PositionOpenWithResult, commandParameters);
|
||||||
|
//return strResult.ParseResult(ParamSeparator, out result);
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|||||||
+37
-37
@@ -14,48 +14,48 @@ namespace MtApi5
|
|||||||
return quote != null ? new Mt5Quote(quote.Instrument, quote.Bid, quote.Ask) : null;
|
return quote != null ? new Mt5Quote(quote.Instrument, quote.Bid, quote.Ask) : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static bool ParseResult(this string inputString, char separator, out MqlTradeResult result)
|
//public static bool ParseResult(this string inputString, char separator, out MqlTradeResult result)
|
||||||
{
|
//{
|
||||||
Log.Debug($"ParseResult: inputString = {inputString}, separator = {separator}");
|
// Log.Debug($"ParseResult: inputString = {inputString}, separator = {separator}");
|
||||||
|
|
||||||
var retVal = false;
|
// var retVal = false;
|
||||||
result = null;
|
// result = null;
|
||||||
|
|
||||||
if (string.IsNullOrEmpty(inputString) == false)
|
// if (string.IsNullOrEmpty(inputString) == false)
|
||||||
{
|
// {
|
||||||
var values = inputString.Split(separator);
|
// var values = inputString.Split(separator);
|
||||||
if (values.Length == 10)
|
// if (values.Length == 10)
|
||||||
{
|
// {
|
||||||
try
|
// try
|
||||||
{
|
// {
|
||||||
retVal = int.Parse(values[0]) != 0;
|
// retVal = int.Parse(values[0]) != 0;
|
||||||
|
|
||||||
var retcode = uint.Parse(values[1]);
|
// var retcode = uint.Parse(values[1]);
|
||||||
var deal = ulong.Parse(values[2]);
|
// var deal = ulong.Parse(values[2]);
|
||||||
var order = ulong.Parse(values[3]);
|
// var order = ulong.Parse(values[3]);
|
||||||
var volume = double.Parse(values[4]);
|
// var volume = double.Parse(values[4]);
|
||||||
var price = double.Parse(values[5]);
|
// var price = double.Parse(values[5]);
|
||||||
var bid = double.Parse(values[6]);
|
// var bid = double.Parse(values[6]);
|
||||||
var ask = double.Parse(values[7]);
|
// var ask = double.Parse(values[7]);
|
||||||
var comment = values[8];
|
// var comment = values[8];
|
||||||
var requestId = uint.Parse(values[9]);
|
// var requestId = uint.Parse(values[9]);
|
||||||
|
|
||||||
result = new MqlTradeResult(retcode, deal, order, volume, price, bid, ask, comment, requestId);
|
// result = new MqlTradeResult(retcode, deal, order, volume, price, bid, ask, comment, requestId);
|
||||||
}
|
// }
|
||||||
catch (Exception ex)
|
// catch (Exception ex)
|
||||||
{
|
// {
|
||||||
Log.Error($"ParseResult: {ex.Message}");
|
// Log.Error($"ParseResult: {ex.Message}");
|
||||||
retVal = false;
|
// retVal = false;
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
else
|
// else
|
||||||
{
|
// {
|
||||||
Log.Warn("ParseResult: input srting is null or empty!");
|
// 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)
|
public static bool ParseResult(this string inputString, char separator, out MqlTradeCheckResult result)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -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; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,13 +1,13 @@
|
|||||||
using System.Diagnostics.CodeAnalysis;
|
// ReSharper disable InconsistentNaming
|
||||||
|
|
||||||
namespace MtApi5.Requests
|
namespace MtApi5.Requests
|
||||||
{
|
{
|
||||||
[SuppressMessage("ReSharper", "InconsistentNaming")]
|
|
||||||
internal enum RequestType
|
internal enum RequestType
|
||||||
{
|
{
|
||||||
Unknown = 0,
|
Unknown = 0,
|
||||||
CopyTicks = 1,
|
CopyTicks = 1,
|
||||||
iCustom = 2,
|
iCustom = 2,
|
||||||
OrderSend = 3
|
OrderSend = 3,
|
||||||
|
PositionOpen = 4
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Binary file not shown.
+65
-6
@@ -39,7 +39,7 @@
|
|||||||
bool getBooleanValue(int expertHandle, int paramIndex, bool& res, string& err);
|
bool getBooleanValue(int expertHandle, int paramIndex, bool& res, string& err);
|
||||||
#import
|
#import
|
||||||
|
|
||||||
//#define __DEBUG_LOG__
|
#define __DEBUG_LOG__
|
||||||
|
|
||||||
input int Port = 8228;
|
input int Port = 8228;
|
||||||
|
|
||||||
@@ -3112,8 +3112,11 @@ void Execute_PositionOpen(bool isTradeResultRequired)
|
|||||||
return;
|
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;
|
CTrade trade;
|
||||||
bool ok = trade.PositionOpen(symbol, (ENUM_ORDER_TYPE)order_type, volume, price, sl, tp, comment);
|
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
|
case 3: //OrderSend
|
||||||
response = ExecuteRequest_OrderSend(jo);
|
response = ExecuteRequest_OrderSend(jo);
|
||||||
break;
|
break;
|
||||||
|
case 4: //PositionOpen
|
||||||
|
response = ExecuteRequest_PositionOpen(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");
|
||||||
@@ -5838,9 +5844,11 @@ bool JsonToMqlTradeRequest(JSONObject *jo, MqlTradeRequest& request)
|
|||||||
request.expiration = (datetime)jo.getInt("MtExpiration");
|
request.expiration = (datetime)jo.getInt("MtExpiration");
|
||||||
|
|
||||||
//Comment
|
//Comment
|
||||||
CHECK_JSON_VALUE(jo, "Comment", false);
|
if (jo.getValue("Comment") != NULL)
|
||||||
StringInit(request.comment, 1000, 0);
|
{
|
||||||
request.comment = jo.getString("Comment");
|
StringInit(request.comment, 1000, 0);
|
||||||
|
request.comment = jo.getString("Comment");
|
||||||
|
}
|
||||||
|
|
||||||
//Position
|
//Position
|
||||||
CHECK_JSON_VALUE(jo, "Position", false);
|
CHECK_JSON_VALUE(jo, "Position", false);
|
||||||
@@ -5890,4 +5898,55 @@ string ExecuteRequest_OrderSend(JSONObject *jo)
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
return CreateSuccessResponse("Value", result_value_jo);
|
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);
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user