mirror of
https://github.com/vdemydiuk/mtapi.git
synced 2026-08-09 17:01:02 +00:00
Added OrderSendRequest/OrderSendResponse. Changed functions OrderSend to use request pattern
This commit is contained in:
+2
-1
@@ -78,11 +78,12 @@
|
||||
<Compile Include="OrderSelectSource.cs" />
|
||||
<Compile Include="Requests\GetOrderRequest.cs" />
|
||||
<Compile Include="Requests\GetOrdersRequest.cs" />
|
||||
<Compile Include="Requests\OrderSendRequest.cs" />
|
||||
<Compile Include="Requests\RequestBase.cs" />
|
||||
<Compile Include="Requests\RequestContainer.cs" />
|
||||
<Compile Include="Requests\RequestType.cs" />
|
||||
<Compile Include="Responses\GetOrderResponse.cs" />
|
||||
<Compile Include="Responses\GetOrdersResponse.cs" />
|
||||
<Compile Include="Responses\OrderSendResponse.cs" />
|
||||
<Compile Include="Responses\ResponseBase.cs" />
|
||||
<Compile Include="SeriesIdentifier.cs" />
|
||||
<Compile Include="TradeOperation.cs" />
|
||||
|
||||
+33
-13
@@ -96,13 +96,30 @@ namespace MtApi
|
||||
|
||||
#region Trading functions
|
||||
|
||||
public int OrderSend(string symbol, TradeOperation cmd, double volume, double price, int slippage, double stoploss, double takeprofit
|
||||
, string comment, int magic, DateTime expiration, Color arrow_color)
|
||||
private int InternalOrderSend(string symbol, TradeOperation cmd, double volume, double? price, int? slippage, double? stoploss, double? takeprofit
|
||||
, string comment, int? magic, DateTime? expiration, Color? arrowColor)
|
||||
{
|
||||
var commandParameters = new ArrayList { symbol, (int)cmd, volume, price, slippage, stoploss, takeprofit
|
||||
, comment, magic, MtApiTimeConverter.ConvertToMtTime(expiration), MtApiColorConverter.ConvertToMtColor(arrow_color) };
|
||||
var response = SendRequest<OrderSendResponse>(new OrderSendRequest
|
||||
{
|
||||
Symbol = symbol,
|
||||
Cmd = (int)cmd,
|
||||
Volume = volume,
|
||||
Price = price,
|
||||
Slippage = slippage,
|
||||
Stoploss = stoploss,
|
||||
Takeprofit = takeprofit,
|
||||
Comment = comment,
|
||||
Magic = magic,
|
||||
Expiration = expiration.HasValue ? MtApiTimeConverter.ConvertToMtTime(expiration.Value) : default(int?),
|
||||
ArrowColor = arrowColor.HasValue ? MtApiColorConverter.ConvertToMtColor(arrowColor.Value) : default(int?)
|
||||
});
|
||||
return response != null ? response.Ticket : -1;
|
||||
}
|
||||
|
||||
return sendCommand<int>(MtCommandType.OrderSend, commandParameters);
|
||||
public int OrderSend(string symbol, TradeOperation cmd, double volume, double price, int slippage, double stoploss, double takeprofit
|
||||
, string comment, int magic, DateTime expiration, Color arrowColor)
|
||||
{
|
||||
return InternalOrderSend(symbol, cmd, volume, price, slippage, stoploss, takeprofit, comment, magic, expiration, arrowColor);
|
||||
}
|
||||
|
||||
public int OrderSend(string symbol, TradeOperation cmd, double volume, double price, int slippage, double stoploss, double takeprofit
|
||||
@@ -131,9 +148,8 @@ namespace MtApi
|
||||
public int OrderSend(string symbol, TradeOperation cmd, double volume, string price, int slippage, double stoploss, double takeprofit)
|
||||
{
|
||||
double dPrice = 0;
|
||||
if (Double.TryParse(price, out dPrice))
|
||||
return OrderSend(symbol, cmd, volume, dPrice, slippage, stoploss, takeprofit, null, 0, DateTime.MinValue, Color.Empty);
|
||||
return 0;
|
||||
return double.TryParse(price, out dPrice) ?
|
||||
OrderSend(symbol, cmd, volume, dPrice, slippage, stoploss, takeprofit, null, 0, DateTime.MinValue, Color.Empty) : 0;
|
||||
}
|
||||
|
||||
public int OrderSendBuy(string symbol, double volume, int slippage)
|
||||
@@ -158,14 +174,14 @@ namespace MtApi
|
||||
|
||||
public int OrderSendBuy(string symbol, double volume, int slippage, double stoploss, double takeprofit, string comment, int magic)
|
||||
{
|
||||
var commandParameters = new ArrayList { symbol, volume, slippage, stoploss, takeprofit, comment, magic };
|
||||
return sendCommand<int>(MtCommandType.OrderSendBuy, commandParameters);
|
||||
return InternalOrderSend(symbol, TradeOperation.OP_BUY, volume, null, slippage, stoploss, takeprofit, comment,
|
||||
magic, null, null);
|
||||
}
|
||||
|
||||
public int OrderSendSell(string symbol, double volume, int slippage, double stoploss, double takeprofit, string comment, int magic)
|
||||
{
|
||||
var commandParameters = new ArrayList { symbol, volume, slippage, stoploss, takeprofit, comment, magic };
|
||||
return sendCommand<int>(MtCommandType.OrderSendSell, commandParameters);
|
||||
return InternalOrderSend(symbol, TradeOperation.OP_SELL, volume, null, slippage, stoploss, takeprofit, comment,
|
||||
magic, null, null);
|
||||
}
|
||||
|
||||
public bool OrderClose(int ticket, double lots, double price, int slippage, Color color)
|
||||
@@ -1331,7 +1347,11 @@ namespace MtApi
|
||||
if (request == null)
|
||||
return default(T);
|
||||
|
||||
var serializer = RequestContainer.CreateNew(request).Serialize();
|
||||
var serializer = JsonConvert.SerializeObject(request, Newtonsoft.Json.Formatting.None,
|
||||
new JsonSerializerSettings
|
||||
{
|
||||
NullValueHandling = NullValueHandling.Ignore
|
||||
});
|
||||
var commandParameters = new ArrayList { serializer };
|
||||
|
||||
MtResponseString res;
|
||||
|
||||
@@ -10,9 +10,9 @@ namespace MtApi
|
||||
//NoCommand = 0
|
||||
|
||||
//trade operations
|
||||
OrderSend = 1,
|
||||
OrderSendBuy = 1001,
|
||||
OrderSendSell = 1002,
|
||||
// OrderSend = 1,
|
||||
// OrderSendBuy = 1001,
|
||||
// OrderSendSell = 1002,
|
||||
OrderClose = 2,
|
||||
OrderCloseByCurrentPrice = 152,
|
||||
OrderCloseBy = 3,
|
||||
|
||||
@@ -5,5 +5,10 @@
|
||||
public int Index { get; set; }
|
||||
public int Select { get; set; }
|
||||
public int Pool { get; set; }
|
||||
|
||||
public override RequestType RequestType
|
||||
{
|
||||
get { return RequestType.GetOrder; }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,5 +3,10 @@
|
||||
public class GetOrdersRequest: RequestBase
|
||||
{
|
||||
public int Pool { get; set; }
|
||||
|
||||
public override RequestType RequestType
|
||||
{
|
||||
get { return RequestType.GetOrders; }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
namespace MtApi.Requests
|
||||
{
|
||||
public class OrderSendRequest: RequestBase
|
||||
{
|
||||
public string Symbol { get; set; }
|
||||
public int Cmd { get; set; }
|
||||
public double Volume { get; set; }
|
||||
|
||||
public double? Price { get; set; }
|
||||
public int? Slippage { get; set; }
|
||||
public double? Stoploss { get; set; }
|
||||
public double? Takeprofit { get; set; }
|
||||
public string Comment { get; set; }
|
||||
public int? Magic { get; set; }
|
||||
public int? Expiration { get; set; }
|
||||
public int? ArrowColor { get; set; }
|
||||
|
||||
public override RequestType RequestType
|
||||
{
|
||||
get { return RequestType.OrderSend; }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,9 @@
|
||||
namespace MtApi.Requests
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace MtApi.Requests
|
||||
{
|
||||
public abstract class RequestBase
|
||||
{
|
||||
{
|
||||
public abstract RequestType RequestType { get; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,33 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace MtApi.Requests
|
||||
{
|
||||
public class RequestContainer
|
||||
{
|
||||
private static readonly Dictionary<Type, RequestType> RequestTypes = new Dictionary<Type, RequestType>();
|
||||
|
||||
static RequestContainer()
|
||||
{
|
||||
RequestTypes[typeof (GetOrderRequest)] = RequestType.GetOrder;
|
||||
RequestTypes[typeof(GetOrdersRequest)] = RequestType.GetOrders;
|
||||
}
|
||||
|
||||
public RequestType RequestType { get; set; }
|
||||
public RequestBase Request { get; set; }
|
||||
|
||||
public virtual string Serialize()
|
||||
{
|
||||
return JsonConvert.SerializeObject(this);
|
||||
}
|
||||
|
||||
public static RequestContainer CreateNew<T>(T request) where T: RequestBase
|
||||
{
|
||||
if (RequestTypes.ContainsKey(request.GetType()) == false)
|
||||
throw new ArgumentException("Unknown request type");
|
||||
|
||||
return new RequestContainer { RequestType = RequestTypes[request.GetType()], Request = request };
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@
|
||||
{
|
||||
Unknown = 0,
|
||||
GetOrder = 1,
|
||||
GetOrders = 2
|
||||
GetOrders = 2,
|
||||
OrderSend = 3
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace MtApi.Responses
|
||||
{
|
||||
public class OrderSendResponse: ResponseBase
|
||||
{
|
||||
public int Ticket { get; set; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user