Issue #89: Changed function PositionOpen to use json request/response

This commit is contained in:
vdemydiuk
2018-02-19 15:54:03 +02:00
parent c9c188f697
commit d6640300f5
8 changed files with 141 additions and 50 deletions
+1 -1
View File
@@ -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}";
}
}
}
+1
View File
@@ -69,6 +69,7 @@
<Compile Include="Requests\CopyTicksRequest.cs" />
<Compile Include="Requests\ICustomRequest.cs" />
<Compile Include="Requests\OrderSendRequest.cs" />
<Compile Include="Requests\PositionOpenRequest.cs" />
<Compile Include="Requests\RequestBase.cs" />
<Compile Include="Requests\RequestType.cs" />
<Compile Include="Requests\OrderSendResult.cs" />
+19 -3
View File
@@ -544,10 +544,26 @@ namespace MtApi5
/// <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)
{
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);
return strResult.ParseResult(ParamSeparator, out result);
var response = SendRequest<OrderSendResult>(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<string>(Mt5CommandType.PositionOpenWithResult, commandParameters);
//return strResult.ParseResult(ParamSeparator, out result);
}
#endregion
+37 -37
View File
@@ -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)
{
+15
View File
@@ -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; }
}
}
+3 -3
View File
@@ -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
}
}
BIN
View File
Binary file not shown.
+65 -6
View File
@@ -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);
}