feat (internal): Add price ticker to md client
This commit is contained in:
1 parent
b7c018b664
commit
d62f88691c
2 files changed
+173
No files matched your search
@@ -75,6 +75,41 @@ namespace bts.md {
|
||||
TradesSource.create(
|
||||
_source.consumer, securities(filter.security_criteria), _poll_interval);
|
||||
|
||||
public IObservable<TableUpdate<PriceTicker, uint>> price_ticker(
|
||||
SecurityCriteria? criteria = null) {
|
||||
// This MDSource's IObservable provides callbacks when a security is added or removed, but
|
||||
// doesn't detect ticker changes. To avoid maintaining complicated state, create a brand new
|
||||
// IObservable of ticker updates whenever the securities change.
|
||||
return criteria.subscribe_md(_source.subscribe_securities())
|
||||
.Select(fixed_sec_price_tickers)
|
||||
.FlattenTableSeries();
|
||||
|
||||
IObservable<TableUpdate<PriceTicker, uint>> fixed_sec_price_tickers(
|
||||
IEnumerable<SecurityMD> security_mds) {
|
||||
// Convert 'security_mds' to a list of PollablePriceTickers.
|
||||
var cached_tickers = security_mds
|
||||
.Select(s => new PollablePriceTicker(s))
|
||||
.ToList();
|
||||
|
||||
return Observable.Interval(_poll_interval).Scan(
|
||||
TableUpdate<PriceTicker, uint>.mk_empty(get_sec_id),
|
||||
poll_tickers);
|
||||
|
||||
uint get_sec_id(PriceTicker ticker) =>
|
||||
ticker.security.Id;
|
||||
|
||||
TableUpdate<PriceTicker, uint> poll_tickers(TableUpdate<PriceTicker, uint> table, long _) {
|
||||
var bd = table.to_builder();
|
||||
foreach (var cticker in cached_tickers) {
|
||||
if (cticker.poll_changes()) {
|
||||
bd.add_or_update(cticker.to_ticker());
|
||||
}
|
||||
}
|
||||
return bd.to_table();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class PollableBook {
|
||||
readonly SecurityMD _security_md;
|
||||
readonly uint _num_depth_levels;
|
||||
@@ -106,5 +141,48 @@ namespace bts.md {
|
||||
.Select(nps => new PriceSize(nps.size, nps.direct_size, nps.implied_size, nps.price))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
class PollablePriceTicker {
|
||||
readonly SecurityMD _security_md;
|
||||
ulong _bbo_poll_time = 0;
|
||||
ulong _last_poll_time = 0;
|
||||
NativePriceSize _bid;
|
||||
NativePriceSize _ask;
|
||||
NativePriceSize _last;
|
||||
PriceTicker _ticker;
|
||||
ulong _volume;
|
||||
|
||||
public PollablePriceTicker(SecurityMD security_md) =>
|
||||
_security_md = security_md;
|
||||
|
||||
public PriceTicker to_ticker() =>
|
||||
_ticker;
|
||||
|
||||
public bool poll_changes() {
|
||||
var md = _security_md.md;
|
||||
md.get_bbo(ref _bbo_poll_time, out _bid, out _ask);
|
||||
md.get_last_trade(ref _last_poll_time, out _last, out _, out _volume);
|
||||
return Swap.toggle_to(ref _ticker, _mk_ticker());
|
||||
}
|
||||
|
||||
PriceTicker _mk_ticker() {
|
||||
var md = _security_md.md;
|
||||
return new(
|
||||
_security_md.security,
|
||||
_security_md.quantity_display,
|
||||
md.session_high,
|
||||
md.session_low,
|
||||
_volume,
|
||||
_bid.price,
|
||||
_bid.size,
|
||||
_ask.price,
|
||||
_ask.size,
|
||||
md.fixing_price,
|
||||
_last.price,
|
||||
_last.size);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
#nullable enable
|
||||
namespace Spark.Api.MarketData {
|
||||
using bts.api;
|
||||
using bts.api.domain;
|
||||
|
||||
using Spark.Core.Collections;
|
||||
|
||||
using System;
|
||||
|
||||
/// <summary>
|
||||
/// Price/trade statistics for a particular security (fast moving)
|
||||
/// </summary>
|
||||
public readonly struct PriceTicker : IEquatable<PriceTicker> {
|
||||
public PriceTicker(
|
||||
SecurityImmutable security,
|
||||
TickDisplaySettingsImmutable quantity_display,
|
||||
double high,
|
||||
double low,
|
||||
double volume,
|
||||
double bid,
|
||||
double bid_quantity,
|
||||
double ask,
|
||||
double ask_quantity,
|
||||
double mark,
|
||||
double last,
|
||||
double last_quantity
|
||||
) {
|
||||
this.security = security;
|
||||
this.quantity_display = quantity_display;
|
||||
this.high = high;
|
||||
this.low = low;
|
||||
this.volume = volume;
|
||||
this.bid = bid;
|
||||
this.bid_quantity = bid_quantity;
|
||||
this.ask = ask;
|
||||
this.ask_quantity = ask_quantity;
|
||||
this.mark = mark;
|
||||
this.last = last;
|
||||
this.last_quantity = last_quantity;
|
||||
}
|
||||
|
||||
public SecurityImmutable security { get; }
|
||||
public TickDisplaySettingsImmutable quantity_display { get; }
|
||||
public double high { get; }
|
||||
public double low { get; }
|
||||
public double volume { get; }
|
||||
public double bid { get; }
|
||||
public double bid_quantity { get; }
|
||||
public double ask { get; }
|
||||
public double ask_quantity { get; }
|
||||
public double mark { get; }
|
||||
public double last { get; }
|
||||
public double last_quantity { get; }
|
||||
|
||||
public bool Equals(PriceTicker other) =>
|
||||
security.Equals(other.security) &&
|
||||
quantity_display.Equals(other.quantity_display) &&
|
||||
high.Equals(other.high) &&
|
||||
low.Equals(other.low) &&
|
||||
volume.Equals(other.volume) &&
|
||||
bid.Equals(other.bid) &&
|
||||
bid_quantity.Equals(other.bid_quantity) &&
|
||||
ask.Equals(other.ask) &&
|
||||
ask_quantity.Equals(other.ask_quantity) &&
|
||||
mark.Equals(other.mark) &&
|
||||
last.Equals(other.last) &&
|
||||
last_quantity.Equals(other.last_quantity);
|
||||
|
||||
public override bool Equals(object? obj) =>
|
||||
obj is PriceTicker other && Equals(other);
|
||||
|
||||
public override int GetHashCode() =>
|
||||
new HashCode()
|
||||
.with_values(new int[] {
|
||||
security.Id.GetHashCode(),
|
||||
high.GetHashCode(),
|
||||
low.GetHashCode(),
|
||||
volume.GetHashCode(),
|
||||
bid.GetHashCode(),
|
||||
bid_quantity.GetHashCode(),
|
||||
ask.GetHashCode(),
|
||||
ask_quantity.GetHashCode(),
|
||||
mark.GetHashCode(),
|
||||
last.GetHashCode(),
|
||||
last_quantity.GetHashCode()
|
||||
})
|
||||
.ToHashCode();
|
||||
|
||||
public static bool operator ==(PriceTicker left, PriceTicker right) =>
|
||||
left.Equals(right);
|
||||
|
||||
public static bool operator !=(PriceTicker left, PriceTicker right) =>
|
||||
!(left == right);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user