Files
spark/src/client/spark_historical/FillModel/FillModelBase.cs
T

141 lines
5.0 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using bts.api;
using bts.co2;
using Google.Protobuf.WellKnownTypes;
using MathNet.Numerics.Statistics;
using mbts.co2;
namespace bts.history
{
public class FillModelBase
{
readonly protected Dictionary<string, co2.BookMessage> _books = new Dictionary<string, co2.BookMessage>();
readonly protected Dictionary<ulong, uint> _disqual = new Dictionary<ulong, uint>();
public event Action<FillEntry> on_fill;
public event Action<OrderStatus> on_status_due_to_fill;
public Bucket outgoing_latency { get; }
public FillModelBase(Bucket outgoing_latency)
{
this.outgoing_latency = outgoing_latency;
}
public virtual void reset()
{
_books.Clear();
_disqual.Clear();
on_fill = null;
on_status_due_to_fill = null;
}
public virtual void process_book(Timestamp transact_time, string security_desc, BookMessage book)
{
_books[security_desc] = book;
}
protected co2.BookMessage get_book(string security_desc)
{
if (!_books.TryGetValue(security_desc, out co2.BookMessage book))
{
_books[security_desc] = book = new co2.BookMessage();
}
return book;
}
protected bool check_cross(OrderStatus status)
{
var fills = check_my_cross(status);
if (fills.Count > 0)
return fills.Any(x => x.fully_filled);
return false;
}
List<FillEntry> check_my_cross(OrderStatus status)
{
List<FillEntry> fills = new List<FillEntry>();
var book = get_book(status.order.security_desc);
if (status.order.side == Side.Buy)
{
int level = 0; int nasks = Math.Min(book.Asks.Count, 10);
while (status.state.Quantity > 0 && level < nasks)
{
var currask = book.ask_at(level);
if (status.state.Price >= currask || status.order.market_order)
{
var qty = Math.Min(status.state.Quantity, book.asksize_at(level));
if (qty > 0)
fills.Add(generate_fill(status.TransactTime, status, currask, (uint)qty, book, "CROSS"));
}
else
break;
level++;
}
}
else
{
int level = 0; int nbids = Math.Min(book.Bids.Count, 10);
while (status.state.Quantity > 0 && level < nbids)
{
var currbid = book.bid_at(level);
if (status.state.Price <= currbid || status.order.market_order)
{
var qty = Math.Min(status.state.Quantity, book.bidsize_at(level));
if (qty > 0)
fills.Add(generate_fill(status.TransactTime, status, currbid, (uint)qty, book, "CROSS"));
}
else
break;
level++;
}
}
return fills;
}
protected FillEntry generate_fill(Timestamp asof, OrderStatus status, double price, uint qty, co2.BookMessage book = null, string tag = null)
{
var f = new FillEntry(asof, status.order, status.order.side, price, qty);
f.SendingTime = f.TransactTime.add_nanos((int)outgoing_latency.LowerBound);
// f.piq = piq;
// f.queue_len = qlen;
f.bidsize = book?.bidsize_at(0) ?? 0;
f.bid = book?.bid_at(0) ?? 0;
f.ask = book?.ask_at(0) ?? 0;
f.asksize = book?.asksize_at(0) ?? 0;
f.tag = tag;
if (qty > status.state.Quantity)
throw new InvalidOperationException($"too much fill {qty} working quantity {status.state.Quantity}");
status.state.Quantity -= qty;
if (status.state.Quantity == 0)
{
Log.Testing.info("Full FILL " + Stringizer.to_string(f) + (tag ?? ""));
f.fully_filled = true;
}
else
{
Log.Testing.info("Partial FILL " + Stringizer.to_string(f));
}
var clone = status.clone();
clone.TransactTime = asof;
clone.SendingTime = clone.TransactTime.add_nanos((int)outgoing_latency.LowerBound);
clone.state.LastPrice = f.price;
clone.state.AvgPrice = status.state.AvgPrice * status.state.FilledQuantity + f.price * f.size;
clone.state.FilledQuantity += (uint)f.size;
clone.state.AvgPrice /= status.state.FilledQuantity;
if (f.fully_filled)
{
clone.state.Status = api.orderman.OrderStatus.Completed;
_disqual[status.order.order_id] = int.MaxValue;
}
if (this.on_status_due_to_fill != null)
this.on_status_due_to_fill(clone);
if (this.on_fill != null)
this.on_fill(f);
return f;
}
}
}