189 lines
7.8 KiB
C#
189 lines
7.8 KiB
C#
#nullable enable
|
|
namespace mbts {
|
|
using bts;
|
|
using bts.api;
|
|
using bts.api.domain;
|
|
using bts.utils;
|
|
|
|
using mbts.co2;
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.Immutable;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
|
|
public readonly struct SecurityMD : IEquatable<SecurityMD> {
|
|
public static readonly Func<SecurityMD, uint> key_fn = smd => smd.security.Id;
|
|
|
|
public SecurityMD(
|
|
SecurityImmutable security,
|
|
IMDConsumerHandle md,
|
|
TickDisplaySettingsImmutable? price_display = null,
|
|
TickDisplaySettingsImmutable? quantity_display = null,
|
|
IEnumerable<TickDisplayRuleImmutable>? price_display_rules = null) {
|
|
this.security = security;
|
|
this.md = md;
|
|
this.price_display = price_display ?? Security.Defaults.price_display_settingsi;
|
|
this.quantity_display = quantity_display ??
|
|
Security.Defaults.quantity_display_settingsi;
|
|
this.price_display_rules = price_display_rules
|
|
.empty_if_null()
|
|
.OrderBy(r => r.MaxValue)
|
|
.ToImmutableList();
|
|
// Validate new 'price rules' config (be strict...this is a hotfix to prod)
|
|
Log.assert(this.price_display_rules.All(r => r.MaxValue.is_real()));
|
|
Log.assert(this.price_display_rules.Select(r => r.MaxValue).IsDistinct());
|
|
}
|
|
|
|
public SecurityImmutable security { get; }
|
|
public uint id => security.Id;
|
|
public IMDConsumerHandle md { get; }
|
|
public TickDisplaySettingsImmutable price_display { get; }
|
|
public TickDisplaySettingsImmutable quantity_display { get; }
|
|
public ImmutableList<TickDisplayRuleImmutable> price_display_rules { get; }
|
|
|
|
public override int GetHashCode() =>
|
|
(int)security.Id;
|
|
|
|
public override bool Equals(object? obj) =>
|
|
obj is SecurityMD other && Equals(other);
|
|
|
|
public bool Equals(SecurityMD other) =>
|
|
security.Equals(other.security);
|
|
|
|
public override string ToString() =>
|
|
security.ToString()!;
|
|
|
|
public static bool operator ==(SecurityMD left, SecurityMD right) =>
|
|
Equals(left, right);
|
|
|
|
public static bool operator !=(SecurityMD left, SecurityMD right) =>
|
|
!(left == right);
|
|
}
|
|
|
|
static class SecurityMDExtensions {
|
|
const uint DEPTH_COUNT = 10;
|
|
[ThreadStatic]
|
|
static NativePriceSize[] leg1Bids;
|
|
|
|
[ThreadStatic]
|
|
static NativePriceSize[] leg1Asks;
|
|
|
|
static SecurityMDExtensions() {
|
|
leg1Bids = new NativePriceSize[DEPTH_COUNT];
|
|
leg1Asks = new NativePriceSize[DEPTH_COUNT];
|
|
}
|
|
|
|
public static Book Depth(this SecurityMD security, IReadOnlyDictionary<uint, SecurityMD> securities, ref ulong time) {
|
|
var bids = new NativePriceSize[DEPTH_COUNT];
|
|
var asks = new NativePriceSize[DEPTH_COUNT];
|
|
var bidsOut = new (double, double, double)[(int)DEPTH_COUNT];
|
|
var asksOut = new (double, double, double)[(int)DEPTH_COUNT];
|
|
if (!security.security.HasSynthetic) {
|
|
security.md.get_depth(ref time, ref bids, ref asks, DEPTH_COUNT);
|
|
for (int i = 0; i < DEPTH_COUNT; i++) {
|
|
bidsOut[i] = (bids[i].price, bids[i].size, bids[i].notional_size);
|
|
asksOut[i] = (asks[i].price, asks[i].size, asks[i].notional_size);
|
|
}
|
|
return new(bidsOut, asksOut);
|
|
}
|
|
Log.assert(security.security.Synthetic.Legs.TryGetFirst(out var leg1));
|
|
if (!securities.TryGetValue(leg1.Security.Id, out var sec1)) {
|
|
return Book.Empty;
|
|
}
|
|
ulong sec1Time = 0;
|
|
if (!sec1.md.get_depth(ref sec1Time, ref leg1Bids, ref leg1Asks, DEPTH_COUNT)) {
|
|
return Book.Empty;
|
|
}
|
|
security.md.get_depth(ref time, ref bids, ref asks, DEPTH_COUNT);
|
|
int leg1BidsLevel = 0;
|
|
int leg1AsksLevel = 0;
|
|
double leg1BidsConsumed = 0;
|
|
double leg1AsksConsumed = 0;
|
|
double takeQuantity;
|
|
double notional;
|
|
for (int i = 0; i < DEPTH_COUNT && !bids[i].is_nan; i++) {
|
|
takeQuantity = bids[i].size * leg1.Quantity;
|
|
notional = getNotionalValue(ref leg1Bids, ref leg1BidsLevel, ref leg1BidsConsumed, ref takeQuantity);
|
|
bidsOut[i] = (bids[i].price, bids[i].size, notional);
|
|
takeQuantity = asks[i].size * leg1.Quantity;
|
|
notional = getNotionalValue(ref leg1Asks, ref leg1AsksLevel, ref leg1AsksConsumed, ref takeQuantity);
|
|
asksOut[i] = (asks[i].price, asks[i].size, notional);
|
|
}
|
|
return new(bidsOut, asksOut);
|
|
}
|
|
|
|
public static Book Depth(this SecurityMD security, NativePriceSize[] bids, NativePriceSize[] asks, IReadOnlyDictionary<uint, SecurityMD> securities) {
|
|
bids ??= new NativePriceSize[NativePriceHelpers.COCONUT_MAX_DEPTH];
|
|
asks ??= new NativePriceSize[NativePriceHelpers.COCONUT_MAX_DEPTH];
|
|
var bidsOut = new (double, double, double)[bids.Length];
|
|
var asksOut = new (double, double, double)[asks.Length];
|
|
var depthCount = (uint)Math.Min(bids.Length, asks.Length);
|
|
if (!security.security.HasSynthetic) {
|
|
for (int i = 0; i < depthCount; i++) {
|
|
bidsOut[i] = (bids[i].price, bids[i].size, bids[i].notional_size);
|
|
asksOut[i] = (asks[i].price, asks[i].size, asks[i].notional_size);
|
|
}
|
|
return new(bidsOut, asksOut);
|
|
}
|
|
Log.assert(security.security.Synthetic.Legs.TryGetFirst(out var leg1));
|
|
if (!securities.TryGetValue(leg1.Security.Id, out var sec1)) {
|
|
return Book.Empty;
|
|
}
|
|
ulong sec1Time = 0;
|
|
if (!sec1.md.get_depth(ref sec1Time, ref leg1Bids, ref leg1Asks, depthCount)) {
|
|
return Book.Empty;
|
|
}
|
|
int leg1BidsLevel = 0;
|
|
int leg1AsksLevel = 0;
|
|
double leg1BidsConsumed = 0;
|
|
double leg1AsksConsumed = 0;
|
|
double takeQuantity;
|
|
double notional;
|
|
for (int i = 0; i < depthCount && !bids[i].is_nan; i++) {
|
|
takeQuantity = bids[i].size;
|
|
notional = getNotionalValue(ref leg1Bids, ref leg1BidsLevel, ref leg1BidsConsumed, ref takeQuantity);
|
|
bidsOut[i] = (bids[i].price, bids[i].size, notional);
|
|
takeQuantity = asks[i].size;
|
|
notional = getNotionalValue(ref leg1Asks, ref leg1AsksLevel, ref leg1AsksConsumed, ref takeQuantity);
|
|
asksOut[i] = (asks[i].price, asks[i].size, notional);
|
|
}
|
|
return new(bidsOut, asksOut);
|
|
}
|
|
|
|
static double getNotionalValue(ref NativePriceSize[] depth, ref int level, ref double levelConsumed, ref double takeQuantity) {
|
|
if (level >= depth.Length) {
|
|
return double.NaN;
|
|
}
|
|
double notionalValue = 0;
|
|
while (takeQuantity > MathUtils.epsilon<double>() && level < depth.Length && !depth[level].is_nan) {
|
|
var lvlDepth = (double)depth[level].size - levelConsumed;
|
|
if (lvlDepth <= 0) {
|
|
level++;
|
|
levelConsumed = 0;
|
|
continue;
|
|
}
|
|
var lvlTake = Math.Min(takeQuantity, lvlDepth);
|
|
notionalValue += lvlTake * depth[level].price;
|
|
levelConsumed += lvlTake;
|
|
takeQuantity -= lvlDepth;
|
|
}
|
|
return notionalValue;
|
|
}
|
|
|
|
public class Book {
|
|
public Book(IEnumerable<(double Price, double Volume, double NotionalValue)> bids, IEnumerable<(double Price, double Quantity, double NotionalValue)> asks) {
|
|
Bids = bids;
|
|
Asks = asks;
|
|
}
|
|
|
|
public static Book Empty =>
|
|
new(Array.Empty<(double, double, double)>(), Array.Empty<(double, double, double)>());
|
|
|
|
public IEnumerable<(double Price, double Volume, double NotionalValue)> Bids { get; }
|
|
public IEnumerable<(double Price, double Volume, double NotionalValue)> Asks { get; }
|
|
}
|
|
}
|
|
}
|