Files
spark/src/client/spark/Predicates/Data/Events/OrderFilledEventPredicate.cs
T
John Colvin ecf3f922df refactor: Replace OrderSecurity refs w/ OrderRecordTuple
The latter type was introduced to support the websocket server. Unlike
the old 'OrderSecurity' types, it has no wpf deps, and can thus be built
on Linux
2023-02-04 00:04:43 -05:00

119 lines
5.2 KiB
C#

namespace mbts {
using bts.api;
using bts.api.orderman;
using bts.shell;
using bts.utils;
using mbts.spark;
using mbts.ui.settings;
using ProtoBuf;
using Spark.Api.Orders;
using System;
using System.Collections.Generic;
static class OrderFilledEventPredicateVMFactory {
public static Dictionary<OrderFilledEventPredicate.PredicateTypeOneofCase, ITemplatedPropertyWrapperVM<OrderFilledEventPredicate>> get_oneofs(IBus bus) =>
new Dictionary<OrderFilledEventPredicate.PredicateTypeOneofCase, ITemplatedPropertyWrapperVM<OrderFilledEventPredicate>> {
{ OrderFilledEventPredicate.PredicateTypeOneofCase.AnyOrder, PredicateVMWrapper.create<OrderFilledEventPredicate>(p => p.security = null) },
{ OrderFilledEventPredicate.PredicateTypeOneofCase.SecurityOrder, SecurityCriteriaVMFactory.create<OrderFilledEventPredicate>(bus, o => o.security?.is_value ?? new SecurityCriteria(),
(p, s) => {
if (p.security == null) {
p.security = new SecurityPredicate();
}
p.security.is_value = s;
})
}
};
}
[ProtoContract]
class OrderFilledEventPredicate : IEquatable<OrderFilledEventPredicate> {
public enum PredicateTypeOneofCase {
[PredicateCase("(any order)")]
AnyOrder,
[PredicateCase("choose securities")]
SecurityOrder
}
[ProtoMember(1)]
public SecurityPredicate security { get; set; }
public PredicateTypeOneofCase predicate_type_case {
get {
if (security != null) {
return PredicateTypeOneofCase.SecurityOrder;
}
return PredicateTypeOneofCase.AnyOrder;
}
}
public IPredicate<OrderRecordTuple> create(object obj) =>
create_predicate((INotificationServiceDataCache)obj);
public IPredicate<OrderRecordTuple> create_predicate(INotificationServiceDataCache notification_service_data_cache) =>
new FillTrigger(_create_model(notification_service_data_cache));
public INotificationPredicate<OrderRecordTuple> create_toast(INotificationServiceDataCache notification_service_data_cache) =>
new FillToastNotificationTrigger(_create_model(notification_service_data_cache));
public bool Equals(OrderFilledEventPredicate other) => other != null && other.security.safe_equals(other.security);
public override bool Equals(object obj) => Equals(obj as OrderFilledEventPredicate);
public override int GetHashCode() => security == null ? 0 : security.GetHashCode();
FillTriggerModel _create_model(INotificationServiceDataCache nsdc) =>
new FillTriggerModel(nsdc, security?.create());
class FillTriggerModel {
readonly IPredicate<SecurityImmutable?> _security_predicate;
readonly INotificationServiceDataCache _data_cache;
public FillTriggerModel(INotificationServiceDataCache notification_service_data_cache, IPredicate<SecurityImmutable?> security_predicate) {
_data_cache = notification_service_data_cache;
_security_predicate = security_predicate;
}
// Please review carfully
public (OrderRecordTuple oldval, OrderRecordTuple newval)? get_filled_order_args(OrderRecordTuple ort) {
if (ort.order.is_exchange_order() && OrderEventPredicateHelper.check_security_predicate(ort, _security_predicate)) {
if (_data_cache.get_previous_order_state(ort.order.Key) is var oldval && oldval.HasValue) {
if (ort.order.filled_quantity() > oldval?.order.filled_quantity()) {
return (oldval.Value, ort);
}
} else if (!_data_cache.is_first_order_update && ort.order.filled_quantity() > 0) {
// old order will be a copy of new order, except with filledQuantity set to 0
var old_state = ort.order.State.CloneToMutable();
old_state.FilledQuantity = 0;
return (new(ort.order.with(state: old_state), ort.security, ort.piq), ort);
}
}
return null;
}
}
class FillTrigger : IPredicate<OrderRecordTuple> {
protected readonly FillTriggerModel _model;
public FillTrigger(FillTriggerModel model) => _model = model;
public bool trigger(OrderRecordTuple order) => _model.get_filled_order_args(order).HasValue;
}
class FillToastNotificationTrigger : FillTrigger, INotificationPredicate<OrderRecordTuple> {
public FillToastNotificationTrigger(FillTriggerModel model) : base(model) { }
public new IToastNotification trigger(OrderRecordTuple value) {
var args = _model.get_filled_order_args(value);
if (args.HasValue) {
return ToastNotifications.order_filled(args.Value.oldval, args.Value.newval);
}
return ToastNotifications.empty;
//return args ? ToastNotifications.order_filled(oldval, newval) : ToastNotifications.empty;
}
}
}
}