258 lines
11 KiB
C#
258 lines
11 KiB
C#
namespace bts.websocket_core {
|
|
using bts.api;
|
|
using bts.api.admin;
|
|
using bts.api.core;
|
|
using bts.api.orderman;
|
|
|
|
using mbts;
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
using Spark.Api.Orders;
|
|
|
|
using System;
|
|
|
|
public class WebsocketRestHandler {
|
|
readonly static Lazy<AdminService> _admin = new Lazy<AdminService>(() => Locator.locate<AdminService>(), true);
|
|
private static (bool, CustomOrderDefinition?, string) TryGetCustomDef(WebsocketRequest req) {
|
|
string status = "";
|
|
CustomOrderDefinition customdef;
|
|
JObject? custom = req.parameters["custom"] as JObject;
|
|
if (!WebsocketJsonParsers.TryParse(
|
|
req.parameters,
|
|
"order_type",
|
|
out CustomOrderDefinition.CustomOneofCase? order_type,
|
|
out status)
|
|
|| order_type == null) {
|
|
return (false, null, status);
|
|
}
|
|
switch (order_type) {
|
|
case CustomOrderDefinition.CustomOneofCase.Limit: {
|
|
customdef = new CustomOrderDefinition() {
|
|
Limit = new LimitOrderDefinition()
|
|
};
|
|
break;
|
|
}
|
|
|
|
// case CustomOrderDefinition.CustomOneofCase.Spreader: {
|
|
// // if (custom == null) {
|
|
// // message = "custom not present";
|
|
// // return false;
|
|
// // }
|
|
// //ENTER SPREADER CODE HERE
|
|
// break;
|
|
// }
|
|
case CustomOrderDefinition.CustomOneofCase.Iceberg: {
|
|
if (custom == null) {
|
|
return (false, null, "custom not present");
|
|
}
|
|
if (!WebsocketJsonParsers.TryParse(custom, "show_quantity", out double? sq, out status) || sq == null) {
|
|
return (false, null, status);
|
|
}
|
|
customdef = new CustomOrderDefinition() {
|
|
Iceberg = new IcebergOrderDefinition() {
|
|
ShowQuantity = (double)sq,
|
|
Child = new CustomOrderDefinition() {
|
|
Limit = new LimitOrderDefinition()
|
|
}
|
|
}
|
|
};
|
|
break;
|
|
}
|
|
case CustomOrderDefinition.CustomOneofCase.Cover: {
|
|
if (custom == null) {
|
|
return (false, null, "custom not present");
|
|
}
|
|
if (!WebsocketJsonParsers.TryParse(
|
|
custom,
|
|
"hedge_ticks",
|
|
out uint? ht,
|
|
out status)
|
|
|| ht == null) {
|
|
return (false, null, status);
|
|
}
|
|
if (!WebsocketJsonParsers.TryParse(
|
|
custom,
|
|
"cancel_on_external_hedge_cancel",
|
|
out bool? coehc,
|
|
out status)
|
|
|| coehc == null) {
|
|
return (false, null, status);
|
|
}
|
|
if (!WebsocketJsonParsers.TryParse(
|
|
custom,
|
|
"cancel_hedges_on_cancel",
|
|
out bool? choc,
|
|
out status)
|
|
|| choc == null) {
|
|
return (false, null, status);
|
|
}
|
|
if (!WebsocketJsonParsers.TryParse(
|
|
custom,
|
|
"detach_hedges",
|
|
out bool? dh,
|
|
out status)
|
|
|| dh == null) {
|
|
return (false, null, status);
|
|
}
|
|
customdef = new CustomOrderDefinition() {
|
|
Cover = new CoverOrderDefinition() {
|
|
Hedge = new CustomOrderDefinition() {
|
|
Limit = new LimitOrderDefinition()
|
|
},
|
|
HedgeTicks = (uint)ht,
|
|
CancelOnExternalHedgeCancel = (bool)coehc,
|
|
CancelHedgesOnCancel = (bool)choc,
|
|
DetachHedges = (bool)dh,
|
|
Child = new CustomOrderDefinition() {
|
|
Limit = new LimitOrderDefinition()
|
|
}
|
|
}
|
|
};
|
|
break;
|
|
}
|
|
case CustomOrderDefinition.CustomOneofCase.Market: {
|
|
customdef = new CustomOrderDefinition() {
|
|
Market = new MarketOrderDefinition()
|
|
};
|
|
break;
|
|
}
|
|
default: {
|
|
return (false, null, "order_type not correct");
|
|
}
|
|
};
|
|
return (true, customdef, status);
|
|
}
|
|
private async static Task<(bool, OrderDefinition?, string)> TryGetOrderDefAsync(WebsocketRequest req, IReadOnlyDictionary<uint, SecurityMD> securities) {
|
|
if (req.parameters == null) {
|
|
return (false, null, "parameters not present");
|
|
}
|
|
if (!WebsocketJsonParsers.TryParse(req.parameters, "user", out string? usr, out string status)
|
|
|| usr == null) {
|
|
return (false, null, status);
|
|
}
|
|
if (!WebsocketJsonParsers.TryParse(req.parameters, "account", out string? act, out status)
|
|
|| act == null) {
|
|
return (false, null, status);
|
|
}
|
|
Result<AdminData> adminResult = await _admin.Value.get_admin_data_async(new VoidMessage());
|
|
uint user_id;
|
|
uint account_id;
|
|
if (!adminResult.success || adminResult.value == null) {
|
|
return (false, null, adminResult.error_message);
|
|
}
|
|
AdminData admin = adminResult.value;
|
|
string username = usr!;
|
|
string accountname = act!;
|
|
BTSUser? user = admin.Users.FirstOrDefault(x => username != null ? x.Name.ToLowerInvariant() == username.ToLowerInvariant() : true);
|
|
if (user == null) {
|
|
return (false, null, "user not correct");
|
|
}
|
|
user_id = (uint)user.Id;
|
|
BTSAccount? account = admin.Accounts.FirstOrDefault(x => x.Name.ToLowerInvariant() == accountname.ToLowerInvariant());
|
|
if (account == null) {
|
|
return (false, null, "account not corrent");
|
|
}
|
|
account_id = (uint)account.Id;
|
|
if (!WebsocketJsonParsers.TryParse(req.parameters, "side", out Side? side, out status)
|
|
|| side == null) {
|
|
return (false, null, status);
|
|
}
|
|
if (!WebsocketJsonParsers.TryParse(
|
|
req.parameters, "exchange_securty_name", out string? exchange_securty_name, out status) || exchange_securty_name == null) {
|
|
return (false, null, status);
|
|
}
|
|
if (!WebsocketJsonParsers.TryParse(req.parameters, "venue", out Venue? exchange, out status) || exchange == null) {
|
|
return (false, null, status);
|
|
}
|
|
var _securites = securities;
|
|
if (!_securites.Values.TryGetFirst(sc_md =>
|
|
(
|
|
sc_md.security.Exchange == exchange &&
|
|
sc_md.security.ToString() == exchange_securty_name),
|
|
out SecurityMD sc_md
|
|
)) {
|
|
return (false, null, "security not found");
|
|
}
|
|
|
|
(var check, var customdef, status) = TryGetCustomDef(req);
|
|
if (check == false) {
|
|
return (false, null, status);
|
|
}
|
|
return (true, new OrderDefinition {
|
|
TimeInForce = TimeInForce.Gtc,
|
|
SecurityKey = sc_md.id,
|
|
Side = (Side)side,
|
|
UserId = user_id,
|
|
AccountId = account_id,
|
|
CustomDef = customdef
|
|
}, status);
|
|
}
|
|
public static async Task<(bool success, string status)> DeleteOrderAsync(WebsocketRequest req, OrdersClient oc) {
|
|
if (req.parameters == null) {
|
|
return (false, "parameters not present");
|
|
}
|
|
if (!WebsocketJsonParsers.TryParse(req.parameters, "order_id", out uint? key, out string status) || key == null) {
|
|
return (false, status);
|
|
}
|
|
Response res = await oc.TryDeleteOrderAsync((uint)key);
|
|
status = res.ErrorCode.ToString();
|
|
return (res.ErrorCode != OrderError.Ok, status);
|
|
}
|
|
public static async Task<(bool success, string status)> UpdateOrderAsync(WebsocketRequest request, OrdersClient oc) {
|
|
if (request.parameters == null) {
|
|
return (false, "parameters not present");
|
|
}
|
|
if (!WebsocketJsonParsers.TryParse(request.parameters, "order_id", out uint? key, out string status) || key == null) {
|
|
return (false, status);
|
|
}
|
|
if (!WebsocketJsonParsers.TryParse(request.parameters, "quantity", out double? quantity, out status) || quantity == null) {
|
|
return (false, status);
|
|
}
|
|
|
|
if (!WebsocketJsonParsers.TryParse(request.parameters, "price", out double? price, out status) || price == null) {
|
|
return (false, status);
|
|
}
|
|
(var check, var customdef, status) = TryGetCustomDef(request);
|
|
if (check == false || customdef == null) {
|
|
return (false, status);
|
|
}
|
|
Response res = await oc.TryUpdateOrderAsync(customdef, (double)price, (double)quantity, (uint)key);
|
|
status = res.ErrorCode.ToString();
|
|
return (res.ErrorCode != OrderError.Ok, status);
|
|
}
|
|
public static async Task<(bool success, uint? orderId, string status)> EnterOrderAsync(
|
|
WebsocketRequest request, OrdersClient ordersClient, IReadOnlyDictionary<uint, SecurityMD> securities) {
|
|
double? quantity = null;
|
|
double? price = null;
|
|
OrderDefinition? orderDefinition = null;
|
|
uint? orderId = null;
|
|
string status = string.Empty;
|
|
|
|
if (!WebsocketJsonParsers.TryParse(request.parameters, "quantity", out quantity, out status) || quantity == null) {
|
|
return (false, null, status);
|
|
}
|
|
|
|
if (!WebsocketJsonParsers.TryParse(request.parameters, "price", out price, out status) || price == null) {
|
|
return (false, null, status);
|
|
}
|
|
|
|
(bool success, orderDefinition, status) = await TryGetOrderDefAsync(request, securities);
|
|
if (!success || orderDefinition == null) {
|
|
return (success, null, status);
|
|
}
|
|
|
|
var result = await ordersClient.TryEnterOrderAsync(orderDefinition, (uint)price, (double)quantity);
|
|
|
|
if (result.ErrorCode != OrderError.Ok) {
|
|
return (false, null, result.ErrorCode.ToString());
|
|
}
|
|
|
|
orderId = result.OrderKey;
|
|
status = result.ErrorCode.ToString();
|
|
|
|
return (true, orderId, status);
|
|
}
|
|
}
|
|
}
|