Add: enter synthetic spreader orders through

This commit is contained in:
arham committed 2026-02-18 05:36:02 +00:00
1 parent 528a00cf98
commit f01e8a578d
2 files changed
+71 -18

No files matched your search

@@ -7,13 +7,16 @@ namespace bts.websocket_core {
using bts.api.synthetic;
using bts.co2;
using bts.md;
using bts.spark;
using Spark.Api.Admin;
using Spark.Api.MarketData;
using Spark.Api.Orders;
using Spark.Core.Disposables;
using Spark.Core.Reactive;
using System.Collections.Concurrent;
using System.Reactive.Linq;
using System.Text.Json;
using System.Text.Json.Nodes;
@@ -21,10 +24,12 @@ namespace bts.websocket_core {
/// This does all the bookkeeping for connections and data, and passes
/// it over to the business layer CURRENT VERSION IS JUST TO SHOW HOW TO USE GATEWAY
/// </summary>
public class WebsocketDataSync : IWebsocketDataSync {
public class WebsocketDataSync : IWebsocketDataSync, IDisposable {
readonly AdminService adminService;
readonly Spark.Api.IServiceProvider _service_ctx;
readonly ISecurityResolver securityResolver;
IReadOnlyDictionary<uint, ExecutionStrategyImmutable> synthStrategies = new Dictionary<uint, ExecutionStrategyImmutable>(0);
IDisposable? synthStrategiesSub;
public WebsocketDataSync(Spark.Api.IServiceProvider service_ctx, IMarketDataClient mdClient, ISecurityResolver securityResolver) {
_service_ctx = service_ctx;
@@ -204,6 +209,10 @@ namespace bts.websocket_core {
var synth_sec_service = _service_ctx.GetService<SyntheticSecurityService>();
var admin_client = new AdminClient(admin_service, UserTuple.admin);
var md_relay = _service_ctx.GetService<MarketDataRelay>();
Log.isnull(synthStrategiesSub);
synthStrategiesSub = synth_sec_service.subscribei_metadata()
.Select(upd => upd.all.Where(m => m.HasStrategy).ToDictionary(m => m.SecurityKey, m => m.Strategy))
.Subscribe(ss => synthStrategies = ss);
Orders_Client = new OrdersClient(
order_service, position_service, admin_client, securityResolver, TimeSpan.FromSeconds(0.1));
var r = await Orders_Client.GetRegisteredDynamicOrderTypesAsync();
@@ -318,7 +327,7 @@ namespace bts.websocket_core {
public async void handle_get(WebsocketRequest req) { await HandleRestRequest(req,WebsocketRestHandler.Get);}
public async void handle_enter_order(WebsocketRequest req) {
await HandleRestRequest(req, (req, Orders_Client) => WebsocketRestHandler.EnterOrderAsync(
req, Orders_Client, _securities, adminService, Dynamic_Descriptors
req, Orders_Client, _securities, adminService, Dynamic_Descriptors, synthStrategies
));
}
public async void handle_update_order(WebsocketRequest req) { await HandleRestRequest(req,(req, Orders_Client) => WebsocketRestHandler.UpdateOrderAsync(req,Orders_Client,Dynamic_Descriptors));}
@@ -366,7 +375,7 @@ namespace bts.websocket_core {
send_message(ms, cid);
}
public void send_message(object value, WebsocketClientID id) {
var requestJson = JsonSerializer.Serialize(value);
var requestJson = System.Text.Json.JsonSerializer.Serialize(value);
Gateway.send_to_clients(
new List<WebsocketOutgoingMessage>() {
new WebsocketOutgoingMessage(id, requestJson)
@@ -374,5 +383,8 @@ namespace bts.websocket_core {
}
public void Dispose() {
Disposable.Dispose(ref synthStrategiesSub);
}
}
}
@@ -127,13 +127,13 @@ namespace bts.websocket_core {
return (false, null, key + " not correct");
}
private static (bool, CustomOrderDefinition?, string) TryGetCustomDef(WebsocketRequest req, IReadOnlyDictionary<string, DynamicOrderDescriptor> Dynamic_Descriptors) {
private static (bool, CustomOrderDefinition?, string) TryGetCustomDef(WebsocketRequest req, IReadOnlyDictionary<string, DynamicOrderDescriptor> Dynamic_Descriptors, SecurityImmutable? sec=null, ExecutionStrategyImmutable? synth_Strats =null) {
if (req.parameters["options"] is not JsonObject custom) {
return (false, null, "options not correct");
}
return TryGetCustomDef(custom, Dynamic_Descriptors);
return TryGetCustomDef(custom, Dynamic_Descriptors, sec, synth_Strats);
}
private static (bool, CustomOrderDefinition?, string) TryGetCustomDef(JsonObject custom, IReadOnlyDictionary<string, DynamicOrderDescriptor> Dynamic_Descriptors) {
private static (bool, CustomOrderDefinition?, string) TryGetCustomDef(JsonObject custom, IReadOnlyDictionary<string, DynamicOrderDescriptor> Dynamic_Descriptors, SecurityImmutable? sec =null, ExecutionStrategyImmutable? synth_Strats =null) {
CustomOrderDefinition customdef;
if (!WebsocketJsonParsers.TryParse<CustomOrderDefinition.CustomOneofCase>(custom, "order_type", out CustomOrderDefinition.CustomOneofCase? order_type, out string status) || order_type == null) {
@@ -181,11 +181,17 @@ namespace bts.websocket_core {
return (false, null, "timebox not enabled on api");
}
case CustomOrderDefinition.CustomOneofCase.Spreader: {
// if (custom == null) {
// message = "custom not present";
// return false;
// }
return (false, null, "spreaders not enabled on api");
if(sec is null)
return (false, null, "Could not find Security");
if(synth_Strats is null)
return (false, null, "Could not find Synthetic Strat");
customdef = new CustomOrderDefinition() {
Spreader = new SpreaderOrderDefinition() {
Security = sec.Value.CloneToMutable(),
ExecutionStrategy = synth_Strats.Value.CloneToMutable()
}
};
break;
}
case CustomOrderDefinition.CustomOneofCase.Tick: {
return (false, null, "Ticks not enabled on api");
@@ -363,7 +369,8 @@ namespace bts.websocket_core {
WebsocketRequest req,
IReadOnlyDictionary<uint, SecurityImmutable> securities,
AdminService adminService,
IReadOnlyDictionary<string, DynamicOrderDescriptor> Dynamic_Descriptors
IReadOnlyDictionary<string, DynamicOrderDescriptor> Dynamic_Descriptors,
IReadOnlyDictionary<uint, ExecutionStrategyImmutable> Synth_Strats
) {
if (req.parameters == null) {
return (false, null, "parameters not present");
@@ -373,7 +380,7 @@ namespace bts.websocket_core {
!WebsocketJsonParsers.TryParse<Venue>(req.parameters, "venue", out Venue? exchange, out status) || exchange == null ||
!WebsocketJsonParsers.TryParse<WebsocketJsonParsers.StringWrapper>(req.parameters, "account", out WebsocketJsonParsers.StringWrapper? act, out status) || act == null ||
!WebsocketJsonParsers.TryParse<Side>(req.parameters, "side", out Side? side, out status) || side == null ||
!WebsocketJsonParsers.TryParse<WebsocketJsonParsers.StringWrapper>(req.parameters, "exchange_security_name", out WebsocketJsonParsers.StringWrapper? exchange_securty_name, out status) || exchange_securty_name == null
!WebsocketJsonParsers.TryParse<WebsocketJsonParsers.StringWrapper>(req.parameters, "exchange_security_name", out WebsocketJsonParsers.StringWrapper? exchange_security_name, out status) || exchange_security_name == null
) {
return (false, null, status);
}
@@ -403,13 +410,14 @@ namespace bts.websocket_core {
if (!_securites.Values.TryGetFirst(sec =>
(
sec.Exchange == exchange &&
sec.ToString() == exchange_securty_name.Value.Val),
sec.ToString() == exchange_security_name.Value.Val),
out var sec
)) {
return (false, null, "security not found");
}
Synth_Strats.TryGetValue(sec.Id,out var strat);
(var check, var customdef, status) = TryGetCustomDef(req, Dynamic_Descriptors);
(var check, var customdef, status) = TryGetCustomDef(req, Dynamic_Descriptors, sec, strat);
if (check == false) {
return (false, null, status);
}
@@ -488,17 +496,18 @@ namespace bts.websocket_core {
status = res.ErrorCode.ToString();
return (res.ErrorCode != OrderError.Ok, null, status);
}
public static async Task<(bool success, uint? orderId, string status)> EnterOrderAsync(
WebsocketRequest request,
OrdersClient ordersClient,
IReadOnlyDictionary<uint, SecurityImmutable> securities,
AdminService adminService,
IReadOnlyDictionary<string, DynamicOrderDescriptor> Dynamic_Descriptors) {
IReadOnlyDictionary<string, DynamicOrderDescriptor> Dynamic_Descriptors, IReadOnlyDictionary<uint, ExecutionStrategyImmutable> execution_strats) {
OrderDefinition? orderDefinition = null;
uint? orderId = null;
string status = string.Empty;
(bool success, orderDefinition, status) = await TryGetOrderDefAsync(request, securities, adminService, Dynamic_Descriptors);
(bool success, orderDefinition, status) = await TryGetOrderDefAsync(request, securities, adminService, Dynamic_Descriptors, execution_strats);
if (
!success || orderDefinition == null ||
@@ -507,7 +516,39 @@ namespace bts.websocket_core {
return (false, null, status);
}
var result = await ordersClient.TryEnterOrderAsync(orderDefinition, (uint)price, (double)quantity);
var result = await ordersClient.TryEnterOrderAsync(orderDefinition, (double)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);
}
public static async Task<(bool success, uint? orderId, string status)> CreateSynthAsync(
WebsocketRequest request,
OrdersClient ordersClient,
IReadOnlyDictionary<uint, SecurityImmutable> securities,
AdminService adminService,
IReadOnlyDictionary<string, DynamicOrderDescriptor> Dynamic_Descriptors, IReadOnlyDictionary<uint, ExecutionStrategyImmutable> execution_strats) {
OrderDefinition? orderDefinition = null;
uint? orderId = null;
string status = string.Empty;
(bool success, orderDefinition, status) = await TryGetOrderDefAsync(request, securities, adminService, Dynamic_Descriptors, execution_strats);
if (
!success || orderDefinition == null ||
!WebsocketJsonParsers.TryParse(request.parameters, "quantity", out double? quantity, out status) || quantity == null ||
!WebsocketJsonParsers.TryParse(request.parameters, "price", out double? price, out status) || price == null) {
return (false, null, status);
}
var result = await ordersClient.TryEnterOrderAsync(orderDefinition, (double)price, (double)quantity);
if (result.ErrorCode != OrderError.Ok) {
return (false, null, result.ErrorCode.ToString());