Merged in SPARK-1574 (pull request #67)
SPARK-1574 Approved-by: Jake Norbut <jake.norbut@bluetradesys.com> Approved-by: John Colvin <john.colvin@bluetradesys.com>
This commit is contained in:
commit
35b01451c4
87 files changed
+675
-551
No files matched your search
@@ -9,6 +9,7 @@ using mbts.ui.settings;
|
||||
using mbts.ui.spark;
|
||||
using mbts.ui.utility;
|
||||
using mbts.utils;
|
||||
using bts.utils;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
@@ -17,6 +18,7 @@ using System.Linq;
|
||||
using System.Reactive.Linq;
|
||||
using System.Windows.Input;
|
||||
|
||||
|
||||
namespace mbts.spark {
|
||||
partial class MarketViewVM {
|
||||
public class Row : SecurityMDRow, IDisposable, IEquatable<Row>, DIYGrid.IRowVM, DIYGrid.IActionableRowVM {
|
||||
@@ -27,9 +29,9 @@ namespace mbts.spark {
|
||||
readonly ObservableFilterCollection<OrderRecordImmutable> _working_orders, _working_bids, _working_asks;
|
||||
readonly string _security_text, _security_key_text;
|
||||
readonly Property<double> _bid_price, _ask_price, _last_price, _day_change, _settlement, _session_high, _session_low;
|
||||
readonly Property<ushort> _bid_size, _ask_size;
|
||||
readonly Property<uint> _bid_size, _ask_size;
|
||||
readonly CellProperty<int> _position;
|
||||
readonly CellProperty<ushort> _last_size;
|
||||
readonly CellProperty<uint> _last_size;
|
||||
readonly CellProperty<ulong> _volume;
|
||||
readonly CellProperty<bool> _inside_bids, _inside_asks;
|
||||
readonly CellProperty<int> _working_bid_volume, _working_ask_volume;
|
||||
@@ -47,10 +49,10 @@ namespace mbts.spark {
|
||||
_position = new CellProperty<int>(0);
|
||||
_bid_price = backing(nameof(bid_price), double.NaN);
|
||||
_ask_price = backing(nameof(ask_price), double.NaN);
|
||||
_bid_size = backing<ushort>(nameof(bid_size), 0);
|
||||
_ask_size = backing<ushort>(nameof(ask_size), 0);
|
||||
_bid_size = backing<uint>(nameof(bid_size), 0);
|
||||
_ask_size = backing<uint>(nameof(ask_size), 0);
|
||||
_last_price = backing(nameof(last_price), double.NaN);
|
||||
_last_size = new CellProperty<ushort>(0);
|
||||
_last_size = new CellProperty<uint>(0);
|
||||
_day_change = backing(nameof(day_change), double.NaN);
|
||||
_session_low = backing(nameof(session_low), double.NaN);
|
||||
_session_high = backing(nameof(session_high), double.NaN);
|
||||
@@ -95,11 +97,11 @@ namespace mbts.spark {
|
||||
public bool inside_asks => _inside_asks.value;
|
||||
public int position => _position.value;
|
||||
public double last_price => _last_price.Value;
|
||||
public ushort last_size => _last_size.value;
|
||||
public uint last_size => _last_size.value;
|
||||
public double bid_price => _bid_price.Value;
|
||||
public ushort bid_size => _bid_size.Value;
|
||||
public uint bid_size => _bid_size.Value;
|
||||
public double ask_price => _ask_price.Value;
|
||||
public ushort ask_size => _ask_size.Value;
|
||||
public uint ask_size => _ask_size.Value;
|
||||
public ulong volume => _volume.value;
|
||||
public double settlement => _settlement.Value;
|
||||
public double day_change => _day_change.Value;
|
||||
|
||||
@@ -122,11 +122,11 @@ namespace mbts.spark {
|
||||
#region Filters
|
||||
public IDataGridColumnPersistentFilter<Row, string> security_filter { get; }
|
||||
public IDataGridColumnPersistentFilter<Row, DoubleMinMaxState> bid_price_filter { get; } = new DoubleMinMax<Row>(r => r.bid_price, nameof(Row.bid_price));
|
||||
public IDataGridColumnPersistentFilter<Row, UShortMinMaxState> bid_size_filter { get; } = new UShortMinMax<Row>(r => r.bid_size, nameof(Row.bid_size));
|
||||
public IDataGridColumnPersistentFilter<Row, UIntMinMaxState> bid_size_filter { get; } = new UIntMinMax<Row>(r => r.bid_size, nameof(Row.bid_size));
|
||||
public IDataGridColumnPersistentFilter<Row, DoubleMinMaxState> ask_price_filter { get; } = new DoubleMinMax<Row>(r => r.ask_price, nameof(Row.ask_price));
|
||||
public IDataGridColumnPersistentFilter<Row, UShortMinMaxState> ask_size_filter { get; } = new UShortMinMax<Row>(r => r.ask_size, nameof(Row.ask_size));
|
||||
public IDataGridColumnPersistentFilter<Row, UIntMinMaxState> ask_size_filter { get; } = new UIntMinMax<Row>(r => r.ask_size, nameof(Row.ask_size));
|
||||
public IDataGridColumnPersistentFilter<Row, DoubleMinMaxState> last_price_filter { get; } = new DoubleMinMax<Row>(r => r.last_price, nameof(Row.last_price));
|
||||
public IDataGridColumnPersistentFilter<Row, UShortMinMaxState> last_size_filter { get; } = new UShortMinMax<Row>(r => r.last_size, nameof(Row.last_size));
|
||||
public IDataGridColumnPersistentFilter<Row, UIntMinMaxState> last_size_filter { get; } = new UIntMinMax<Row>(r => r.last_size, nameof(Row.last_size));
|
||||
public IDataGridColumnPersistentFilter<Row, DoubleMinMaxState> day_change_filter { get; } = new DoubleMinMax<Row>(r => r.day_change, nameof(Row.day_change));
|
||||
public IDataGridColumnPersistentFilter<Row, DoubleMinMaxState> settlement_filter { get; } = new DoubleMinMax<Row>(r => r.settlement, nameof(Row.settlement));
|
||||
public IDataGridColumnPersistentFilter<Row, DoubleMinMaxState> session_low_filter { get; } = new DoubleMinMax<Row>(r => r.session_low, nameof(Row.session_low));
|
||||
|
||||
@@ -57,15 +57,15 @@ namespace bts.shell {
|
||||
[ProtoMember(2)]
|
||||
public DoubleMinMaxState bid_price;
|
||||
[ProtoMember(3)]
|
||||
public UShortMinMaxState bid_size;
|
||||
public UIntMinMaxState bid_size;
|
||||
[ProtoMember(4)]
|
||||
public DoubleMinMaxState ask_price;
|
||||
[ProtoMember(5)]
|
||||
public UShortMinMaxState ask_size;
|
||||
public UIntMinMaxState ask_size;
|
||||
[ProtoMember(6)]
|
||||
public DoubleMinMaxState last_price;
|
||||
[ProtoMember(7)]
|
||||
public UShortMinMaxState last_size;
|
||||
public UIntMinMaxState last_size;
|
||||
[ProtoMember(8)]
|
||||
public DoubleMinMaxState day_change;
|
||||
[ProtoMember(9)]
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using bts.api;
|
||||
using bts.utils;
|
||||
using mbts.collections;
|
||||
using mbts.ui.controls;
|
||||
using mbts.ui.spark;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using bts.api;
|
||||
using bts.shell;
|
||||
using bts.spark;
|
||||
using bts.utils;
|
||||
using mbts.spark.utilities;
|
||||
using mbts.ui;
|
||||
using mbts.ui.shell;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using mbts.ui;
|
||||
using bts.utils;
|
||||
using mbts.ui;
|
||||
using mbts.ui.controls;
|
||||
using mbts.utils;
|
||||
using System;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using bts.api;
|
||||
using bts.api.orderman;
|
||||
using bts.shell;
|
||||
using bts.utils;
|
||||
using mbts.spark.filters;
|
||||
using mbts.ui;
|
||||
using mbts.ui.converters;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using bts.api;
|
||||
using bts.api.orderman;
|
||||
using bts.spark;
|
||||
using bts.utils;
|
||||
using mbts;
|
||||
using mbts.spark;
|
||||
using mbts.utils;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using bts.api;
|
||||
using bts.api.orderman;
|
||||
using bts.utils;
|
||||
using mbts.utils;
|
||||
using System;
|
||||
|
||||
|
||||
@@ -24,7 +24,7 @@ namespace bts.spark {
|
||||
IReadOnlyDictionary<uint, SecurityMD> _security = new Dictionary<uint, SecurityMD>();
|
||||
IDisposable _dispose;
|
||||
static readonly IEqualityComparer<OrderRecordImmutable> _id_comparer =
|
||||
mbts.utils.EqualityComparer.Create<OrderRecordImmutable, uint>(a => a.Key);
|
||||
bts.utils.EqualityComparer.Create<OrderRecordImmutable, uint>(a => a.Key);
|
||||
|
||||
public OrderActivitySummarizer(IBus bus, IObservable<TableUpdate<OrderRecordImmutable, uint>> orders) {
|
||||
_bus = bus;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using bts.api.orderman;
|
||||
using bts.utils;
|
||||
using mbts.utils;
|
||||
using System;
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using mbts.spark;
|
||||
using bts.utils;
|
||||
using mbts.spark;
|
||||
using mbts.spark.utilities;
|
||||
using mbts.ui.spark;
|
||||
using mbts.ui.utility;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using bts.api;
|
||||
using bts.api.orderman;
|
||||
using bts.shell;
|
||||
using bts.utils;
|
||||
using mbts.spark;
|
||||
using mbts.ui.settings;
|
||||
using mbts.utils;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using bts.api;
|
||||
using bts.api.orderman;
|
||||
using bts.shell;
|
||||
using bts.utils;
|
||||
using mbts.spark;
|
||||
using mbts.ui.settings;
|
||||
using mbts.utils;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using bts.api;
|
||||
using bts.api.orderman;
|
||||
using bts.shell;
|
||||
using bts.utils;
|
||||
using mbts.spark;
|
||||
using mbts.utils;
|
||||
using System;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using bts.api;
|
||||
using bts.api.orderman;
|
||||
using bts.shell;
|
||||
using bts.utils;
|
||||
using mbts.spark;
|
||||
using mbts.ui;
|
||||
using mbts.ui.settings;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using bts.proto;
|
||||
using bts.utils;
|
||||
using mbts.spark;
|
||||
using mbts.spark.utilities;
|
||||
using mbts.ui.spark;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using bts.api;
|
||||
using bts.utils;
|
||||
using mbts.spark;
|
||||
using mbts.spark.utilities;
|
||||
using mbts.ui.spark;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using bts.proto;
|
||||
using bts.utils;
|
||||
using mbts.spark;
|
||||
using mbts.spark.utilities;
|
||||
using mbts.ui.spark;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using bts.proto;
|
||||
using bts.utils;
|
||||
using mbts.spark;
|
||||
using mbts.spark.utilities;
|
||||
using mbts.ui.spark;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using bts.proto;
|
||||
using bts.utils;
|
||||
using mbts.spark;
|
||||
using mbts.spark.utilities;
|
||||
using mbts.ui.spark;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using bts.proto;
|
||||
using bts.utils;
|
||||
using mbts.spark;
|
||||
using mbts.spark.utilities;
|
||||
using mbts.ui.spark;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using bts.shell;
|
||||
using bts.utils;
|
||||
using mbts.utils;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using mbts.utils;
|
||||
using bts.utils;
|
||||
using mbts.utils;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using bts;
|
||||
using static mbts.spark.utilities.MathUtils;
|
||||
using static bts.utils.MathUtils;
|
||||
|
||||
namespace mbts {
|
||||
static class TickExtensions {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using mbts.ui;
|
||||
using bts.utils;
|
||||
using mbts.ui;
|
||||
using mbts.utils;
|
||||
using ProtoBuf;
|
||||
using System;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using mbts.utils;
|
||||
using bts.utils;
|
||||
using mbts.utils;
|
||||
using ProtoBuf;
|
||||
using System;
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using mbts.spark;
|
||||
using bts.utils;
|
||||
using mbts.spark;
|
||||
using mbts.utils;
|
||||
using ProtoBuf;
|
||||
using System;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using mbts.utils;
|
||||
using bts.utils;
|
||||
using mbts.utils;
|
||||
using ProtoBuf;
|
||||
using System;
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using bts.api;
|
||||
using bts.utils;
|
||||
using mbts.utils;
|
||||
using ProtoBuf;
|
||||
using System;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using mbts.ui;
|
||||
using bts.utils;
|
||||
using mbts.ui;
|
||||
using mbts.utils;
|
||||
using ProtoBuf;
|
||||
using System;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using mbts.utils;
|
||||
using bts.utils;
|
||||
using mbts.utils;
|
||||
using ProtoBuf;
|
||||
using System;
|
||||
using System.Windows;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using mbts.utils;
|
||||
using bts.utils;
|
||||
using mbts.utils;
|
||||
using ProtoBuf;
|
||||
using System;
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using mbts.utils;
|
||||
using bts.utils;
|
||||
using mbts.utils;
|
||||
using ProtoBuf;
|
||||
using System;
|
||||
using System.Windows;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using mbts.utils;
|
||||
using bts.utils;
|
||||
using mbts.utils;
|
||||
using ProtoBuf;
|
||||
using System;
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using mbts.utils;
|
||||
using bts.utils;
|
||||
using mbts.utils;
|
||||
using ProtoBuf;
|
||||
using System;
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using mbts.spark.utilities;
|
||||
using bts.utils;
|
||||
using mbts.spark.utilities;
|
||||
using mbts.ui;
|
||||
using ProtoBuf;
|
||||
using System;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using mbts.spark.utilities;
|
||||
using bts.utils;
|
||||
using mbts.spark.utilities;
|
||||
using mbts.ui.extensions;
|
||||
using mbts.ui.utility;
|
||||
using System;
|
||||
|
||||
@@ -3,7 +3,7 @@ using mbts.spark.filters;
|
||||
using mbts.ui.controls.adorners;
|
||||
using mbts.ui.extensions;
|
||||
using mbts.ui.utility;
|
||||
using mbts.utils;
|
||||
using bts.utils;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
using mbts.spark;
|
||||
using mbts.ui.extensions;
|
||||
using mbts.ui.settings;
|
||||
using mbts.utils;
|
||||
using bts.utils;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using mbts.spark.utilities;
|
||||
using bts.utils;
|
||||
using mbts.spark.utilities;
|
||||
using mbts.ui.settings;
|
||||
using System;
|
||||
using static mbts.ui.controls.DIYGrid;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using bts.proto;
|
||||
using bts.utils;
|
||||
using mbts.utils;
|
||||
using ProtoBuf;
|
||||
using System;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using bts.proto;
|
||||
using bts.utils;
|
||||
using mbts.utils;
|
||||
using ProtoBuf;
|
||||
using System;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using mbts.spark;
|
||||
using bts.utils;
|
||||
using mbts.spark;
|
||||
using mbts.spark.utilities;
|
||||
using mbts.ui.converters;
|
||||
using mbts.ui.shell;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using mbts.spark.utilities;
|
||||
using bts.utils;
|
||||
using mbts.spark.utilities;
|
||||
using System;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
using bts.api;
|
||||
using mbts.utils;
|
||||
using bts.utils;
|
||||
using ProtoBuf;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using bts.api;
|
||||
using bts.utils;
|
||||
using mbts.co2;
|
||||
using mbts.spark.utilities;
|
||||
using mbts.user.settings;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
using mbts.ui;
|
||||
using mbts.ui.utility;
|
||||
using mbts.utils;
|
||||
using bts.utils;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reactive.Linq;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using bts;
|
||||
using bts.api;
|
||||
using bts.utils;
|
||||
using mbts.co2;
|
||||
using mbts.ui;
|
||||
using mbts.ui.controls;
|
||||
@@ -239,12 +240,12 @@ namespace mbts.spark {
|
||||
internal double price_for_point(Point point) =>
|
||||
_price_for_row(_row_for_point(point));
|
||||
|
||||
static ushort? _size_from_row(int row, double[] price_list, NativePriceSize[] depth, ContractSpecificationImmutable spec) {
|
||||
static uint? _size_from_row(int row, double[] price_list, NativePriceSize[] depth, ContractSpecificationImmutable spec) {
|
||||
if (price_list == null || depth == null) return null;
|
||||
else return row <= price_list.Length ? _size_from_price(price_list[row], depth, spec) : null;
|
||||
}
|
||||
|
||||
static ushort? _size_from_price(double price, NativePriceSize[] depth, ContractSpecificationImmutable spec) {
|
||||
static uint? _size_from_price(double price, NativePriceSize[] depth, ContractSpecificationImmutable spec) {
|
||||
if (!double.IsNaN(price) && depth.TryGetFirst(nps => spec.tick_equals(price, nps.price), out NativePriceSize result))
|
||||
return result.size;
|
||||
return null;
|
||||
@@ -259,10 +260,10 @@ namespace mbts.spark {
|
||||
double _price_for_row(int row) =>
|
||||
_price_list == null ? double.NaN : _price_list[row];
|
||||
|
||||
ushort? _bid_size_from_row(int row) =>
|
||||
uint? _bid_size_from_row(int row) =>
|
||||
_size_from_row(row, _price_list, _bids, _vm.security.ContractSpec);
|
||||
|
||||
ushort? _ask_size_from_row(int row) =>
|
||||
uint? _ask_size_from_row(int row) =>
|
||||
_size_from_row(row, _price_list, _asks, _vm.security.ContractSpec);
|
||||
|
||||
int _row_for_price(double price) => LadderRender.index_of(_price_list, price, _price_comparer);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
using bts.api.orderman;
|
||||
using mbts.ui;
|
||||
using mbts.utils;
|
||||
using bts.utils;
|
||||
using ProtoBuf;
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
@@ -142,7 +142,7 @@ namespace mbts.spark {
|
||||
foreach (var nps in rungs) {
|
||||
if (nps.is_nan) break;
|
||||
double price = nps.price;
|
||||
ushort size = nps.size;
|
||||
uint size = nps.size;
|
||||
// prices are in descending order, so get diff from curr_price - price
|
||||
int tick_diff = TickUtils.normalized_tick_diff(spec, curr_price, price);
|
||||
curr_index += tick_diff;
|
||||
|
||||
@@ -8,7 +8,7 @@ namespace mbts.spark {
|
||||
class BBOVM : DisposableViewModelBase {
|
||||
readonly Property<SecurityImmutable> _security;
|
||||
readonly Property<double> _bid, _ask;
|
||||
readonly Property<ushort> _bid_size, _ask_size;
|
||||
readonly Property<uint> _bid_size, _ask_size;
|
||||
|
||||
public BBOVM(IObservable<SecurityMD> security_md)
|
||||
: this(security_md.Select(smd => smd.security).OnDispatcher(), new SecurityMDPump(security_md.Select(smd => smd.md))) { }
|
||||
@@ -17,14 +17,14 @@ namespace mbts.spark {
|
||||
_security = backing(nameof(security), security, SecurityDataHelper.Defaults.SecurityImmutable);
|
||||
_bid = backing(nameof(bid), md_pump.bid_price, double.NaN);
|
||||
_ask = backing(nameof(ask), md_pump.ask_price, double.NaN);
|
||||
_bid_size = backing(nameof(bid_size), md_pump.bid_size, (ushort)0);
|
||||
_ask_size = backing(nameof(ask_size), md_pump.ask_size, (ushort)0);
|
||||
_bid_size = backing(nameof(bid_size), md_pump.bid_size, (uint)0);
|
||||
_ask_size = backing(nameof(ask_size), md_pump.ask_size, (uint)0);
|
||||
}
|
||||
|
||||
public SecurityImmutable security => _security.Value;
|
||||
public double bid => _bid.Value;
|
||||
public double ask => _ask.Value;
|
||||
public ushort bid_size => _bid_size.Value;
|
||||
public ushort ask_size => _ask_size.Value;
|
||||
public uint bid_size => _bid_size.Value;
|
||||
public uint ask_size => _ask_size.Value;
|
||||
}
|
||||
}
|
||||
@@ -11,14 +11,14 @@ namespace mbts.spark.utilities
|
||||
{
|
||||
internal interface ISecurityMDPump
|
||||
{
|
||||
IObservable<(double bid, ushort bid_size, double ask, ushort ask_size)> bbo { get; }
|
||||
IObservable<(double last, ushort last_size, DateTime time_stamp, ulong volume)> last_trade { get; }
|
||||
IObservable<(double bid, uint bid_size, double ask, uint ask_size)> bbo { get; }
|
||||
IObservable<(double last, uint last_size, DateTime time_stamp, ulong volume)> last_trade { get; }
|
||||
IObservable<double> bid_price { get; }
|
||||
IObservable<ushort> bid_size { get; }
|
||||
IObservable<uint> bid_size { get; }
|
||||
IObservable<double> ask_price { get; }
|
||||
IObservable<ushort> ask_size { get; }
|
||||
IObservable<uint> ask_size { get; }
|
||||
IObservable<double> last_price { get; }
|
||||
IObservable<ushort> last_size { get; }
|
||||
IObservable<uint> last_size { get; }
|
||||
IObservable<ulong> volume { get; }
|
||||
IObservable<double> session_low { get; }
|
||||
IObservable<double> session_high { get; }
|
||||
@@ -59,7 +59,7 @@ namespace mbts.spark.utilities
|
||||
|
||||
private ulong _bbo_ref, _last_ref;
|
||||
private double _bid_price, _ask_price, _last_price;
|
||||
private ushort _bid_size, _ask_size, _last_size;
|
||||
private uint _bid_size, _ask_size, _last_size;
|
||||
private ulong _volume;
|
||||
private DateTime _last_time;
|
||||
|
||||
@@ -85,17 +85,17 @@ namespace mbts.spark.utilities
|
||||
return false;
|
||||
}
|
||||
|
||||
private (double, ushort, double, ushort) _select_bbo() { return (_bid_price, _bid_size, _ask_price, _ask_size); }
|
||||
private (double, ushort, DateTime, ulong) _select_last() { return (_last_price, _last_size, _last_time, _volume); }
|
||||
private (double, uint, double, uint) _select_bbo() { return (_bid_price, _bid_size, _ask_price, _ask_size); }
|
||||
private (double, uint, DateTime, ulong) _select_last() { return (_last_price, _last_size, _last_time, _volume); }
|
||||
|
||||
public IObservable<(double bid, ushort bid_size, double ask, ushort ask_size)> bbo => _bbo_pump.Select(_ => _select_bbo()).StartWith(_select_bbo());
|
||||
public IObservable<(double last, ushort last_size, DateTime time_stamp, ulong volume)> last_trade => _last_pump.Select(_ => _select_last()).StartWith(_select_last());
|
||||
public IObservable<(double bid, uint bid_size, double ask, uint ask_size)> bbo => _bbo_pump.Select(_ => _select_bbo()).StartWith(_select_bbo());
|
||||
public IObservable<(double last, uint last_size, DateTime time_stamp, ulong volume)> last_trade => _last_pump.Select(_ => _select_last()).StartWith(_select_last());
|
||||
public IObservable<double> bid_price => _bbo_pump.Select(_ => _bid_price).DistinctUntilChanged().StartWith(_bid_price);
|
||||
public IObservable<ushort> bid_size => _bbo_pump.Select(_ => _bid_size).DistinctUntilChanged().StartWith(_bid_size);
|
||||
public IObservable<uint> bid_size => _bbo_pump.Select(_ => _bid_size).DistinctUntilChanged().StartWith(_bid_size);
|
||||
public IObservable<double> ask_price => _bbo_pump.Select(_ => _ask_price).DistinctUntilChanged().StartWith(_ask_price);
|
||||
public IObservable<ushort> ask_size => _bbo_pump.Select(_ => _ask_size).DistinctUntilChanged().StartWith(_ask_size);
|
||||
public IObservable<uint> ask_size => _bbo_pump.Select(_ => _ask_size).DistinctUntilChanged().StartWith(_ask_size);
|
||||
public IObservable<double> last_price => _bbo_pump.Select(_ => _last_price).DistinctUntilChanged().StartWith(_last_price);
|
||||
public IObservable<ushort> last_size => _bbo_pump.Select(_ => _last_size).DistinctUntilChanged().StartWith(_last_size);
|
||||
public IObservable<uint> last_size => _bbo_pump.Select(_ => _last_size).DistinctUntilChanged().StartWith(_last_size);
|
||||
public IObservable<DateTime> last_time => _last_pump.Select(_ => _last_time).DistinctUntilChanged().StartWith(_last_time);
|
||||
public IObservable<ulong> volume => _last_pump.Select(_ => _volume).DistinctUntilChanged().StartWith(_volume);
|
||||
public IObservable<double> session_low => _root_pump.Select(md => md.session_low).DistinctUntilChanged();
|
||||
|
||||
@@ -3,6 +3,7 @@ using bts.api;
|
||||
using bts.api.admin;
|
||||
using bts.api.orderman;
|
||||
using bts.spark;
|
||||
using bts.utils;
|
||||
using Google.Protobuf;
|
||||
using Google.Protobuf.WellKnownTypes;
|
||||
using mbts.ui;
|
||||
|
||||
@@ -4,11 +4,11 @@ using System.Collections.Generic;
|
||||
namespace mbts.spark {
|
||||
static class SecurityDataHelper {
|
||||
public static class EqualityComparer {
|
||||
public static readonly IEqualityComparer<SecurityMD> MDComparer = utils.EqualityComparer.Create<SecurityMD>(Equals, GetHashCode);
|
||||
public static readonly IEqualityComparer<SecurityMD> MDComparer = bts.utils.EqualityComparer.Create<SecurityMD>(Equals, GetHashCode);
|
||||
public static bool Equals(SecurityMD x, SecurityMD y) => x.security.Id == y.security.Id;
|
||||
public static int GetHashCode(SecurityMD security_md) => security_md.security.Id.GetHashCode();
|
||||
|
||||
public static readonly IEqualityComparer<SecurityImmutable> IDComparer = utils.EqualityComparer.Create<SecurityImmutable, uint>(s => s.Id);
|
||||
public static readonly IEqualityComparer<SecurityImmutable> IDComparer = bts.utils.EqualityComparer.Create<SecurityImmutable, uint>(s => s.Id);
|
||||
}
|
||||
|
||||
public static class Defaults {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using mbts.ui.utility.text;
|
||||
using bts.utils;
|
||||
using mbts.ui.utility.text;
|
||||
using mbts.utils;
|
||||
using System.Collections.Generic;
|
||||
using System.Windows;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using mbts.spark.utilities;
|
||||
using bts.utils;
|
||||
using mbts.spark.utilities;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using bts.utils;
|
||||
using mbts.spark.utilities;
|
||||
using mbts.utils;
|
||||
using System;
|
||||
|
||||
@@ -90,7 +90,7 @@ namespace mbts.co2
|
||||
internal static class Co2NativeTestMethods
|
||||
{
|
||||
[DllImport("libspark", EntryPoint = "co2_get_1234_handle")]
|
||||
internal static extern void get_1234_handle(out UInt64 size, out Co2NativeMethods.BlitPointerTuple pt);
|
||||
internal static extern void get_1234_handle(out UInt64 size, out Co2NativeMethods.BlitPointerTuple pt, out IntPtr hotaddr, out IntPtr coldaddr, out IntPtr headeraddr);
|
||||
|
||||
[DllImport("libspark")]
|
||||
internal static extern void co2_get_sample_hot(out ulong size, ref MDConsumerHandle.HotEntry entry);
|
||||
@@ -110,5 +110,8 @@ namespace mbts.co2
|
||||
[DllImport("libspark")]
|
||||
internal static extern void co2_destroy_test_md(IntPtr handle);
|
||||
|
||||
[DllImport("libspark")]
|
||||
internal static extern void co2_get_zero_and_nan_book(ref NativePriceLevel nps);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -31,18 +31,18 @@ namespace mbts.co2
|
||||
|
||||
// This property exists for specific performance optimization purposes
|
||||
// Most callers should just use get_bbo/get_depth
|
||||
public ulong market_timestamp { get { return has_depth ? _hot_depth->timestamp : _hot->timestamp; } }
|
||||
public ulong market_timestamp { get { return has_depth ? _hot_depth->timestamp.time : _hot->timestamp.time; } }
|
||||
|
||||
public bool get_bbo(ref ulong time, out NativePriceSize bid, out NativePriceSize ask) {
|
||||
var updated = market_timestamp;
|
||||
if (updated > time && updated != UInt64.MaxValue) {
|
||||
if (updated > time && updated != TimeStamp.invalid_value) {
|
||||
if (has_depth) {
|
||||
bid = _hot_depth->bid(0);
|
||||
ask = _hot_depth->ask(0);
|
||||
}
|
||||
else {
|
||||
bid = _hot->bid.load();
|
||||
ask = _hot->ask.load();
|
||||
bid = _hot->book.bid;
|
||||
ask = _hot->book.ask;
|
||||
}
|
||||
time = updated;
|
||||
return true;
|
||||
@@ -72,35 +72,10 @@ namespace mbts.co2
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool get_customer_bbo(ref ulong time, out NativePriceSize customer_bid, out NativePriceSize customer_ask) {
|
||||
var updated = _cold->customer_bbo_updated;
|
||||
if (updated > time) {
|
||||
time = updated;
|
||||
customer_bid = _cold->customer_bid.load();
|
||||
customer_ask = _cold->customer_ask.load();
|
||||
return true;
|
||||
}
|
||||
customer_bid = customer_ask = NativePriceSize.no_market;
|
||||
return false;
|
||||
}
|
||||
|
||||
// This property is for specific optimization purposes
|
||||
// you should probably just call get_last_trade()
|
||||
public ulong trade_timestamp { get { return _cold->last_trade_updated; } }
|
||||
public bool get_last_trade(ref ulong time, out NativePriceSize ps, out DateTime timestamp, out ulong total_volume) {
|
||||
var updated = _cold->last_trade_updated;
|
||||
if (updated > time) {
|
||||
time = updated;
|
||||
timestamp = _cold->last_trade_time;
|
||||
timestamp = timestamp.ToUniversalTime();
|
||||
ps = _cold->last_trade.load();
|
||||
total_volume = _cold->total_volume;
|
||||
return true;
|
||||
}
|
||||
ps = NativePriceSize.no_market;
|
||||
timestamp = new utils.LLDateTime();
|
||||
total_volume = 0;
|
||||
return false;
|
||||
timestamp = _cold->last_trade_time;
|
||||
total_volume = _cold->total_volume;
|
||||
return (_cold->last_trade.load_if_new(ref time, out ps));
|
||||
}
|
||||
public ulong misc_timestamp { get { return _cold->misc_timestamp; } }
|
||||
|
||||
@@ -158,96 +133,86 @@ namespace mbts.co2
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
internal struct HotEntry
|
||||
{
|
||||
// I don't actually need to decode timestamp. If it goes up data has changed
|
||||
public readonly UInt64 timestamp;
|
||||
public readonly AtomicPriceSizeStore bid;
|
||||
public readonly AtomicPriceSizeStore ask;
|
||||
public readonly security_key key;
|
||||
public readonly UInt16 sequence;
|
||||
public readonly byte _status;
|
||||
// we don't actually need to decode timestamp. If it goes up data has changed
|
||||
public readonly TimeStamp timestamp; // 8
|
||||
public readonly NativePriceLevel book; // 24
|
||||
public readonly security_key key; // 4
|
||||
public readonly UInt32 sequence; // 4
|
||||
public readonly byte _status; // 1
|
||||
|
||||
public CoconutSecurityStatus status { get { return (CoconutSecurityStatus)_status; } }
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential, Size = 192)]
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
internal struct HotDepthEntry
|
||||
{
|
||||
public readonly UInt64 timestamp;
|
||||
public readonly security_key key;
|
||||
public readonly TimeStamp timestamp; // 8
|
||||
public readonly security_key key; // 4
|
||||
public readonly UInt32 sequence; // 4
|
||||
|
||||
// The ByValArray type would be more elegant but it does not allocate inline.
|
||||
// [MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
|
||||
|
||||
// 10 direct levels
|
||||
private readonly NativePriceLevel direct0;
|
||||
private readonly NativePriceLevel direct1;
|
||||
private readonly NativePriceLevel direct2;
|
||||
private readonly NativePriceLevel direct3;
|
||||
private readonly NativePriceLevel direct4;
|
||||
private readonly NativePriceLevel direct5;
|
||||
private readonly NativePriceLevel direct6;
|
||||
private readonly NativePriceLevel direct7;
|
||||
private readonly NativePriceLevel direct8;
|
||||
private readonly NativePriceLevel direct9;
|
||||
// 2 implied levels
|
||||
private readonly NativePriceLevel implied0;
|
||||
private readonly NativePriceLevel implied1;
|
||||
private readonly byte _status;
|
||||
|
||||
|
||||
public CoconutSecurityStatus status { get { return (CoconutSecurityStatus)_status; } }
|
||||
public NativePriceSize bid(int depth) {
|
||||
switch (depth) {
|
||||
case 0: return bid0.load();
|
||||
case 1: return bid1.load();
|
||||
case 2: return bid2.load();
|
||||
case 3: return bid3.load();
|
||||
case 4: return bid4.load();
|
||||
case 5: return bid5.load();
|
||||
case 6: return bid6.load();
|
||||
case 7: return bid7.load();
|
||||
case 8: return bid8.load();
|
||||
case 9: return bid9.load();
|
||||
case 0: return direct0.bid;
|
||||
case 1: return direct1.bid;
|
||||
case 2: return direct2.bid;
|
||||
case 3: return direct3.bid;
|
||||
case 4: return direct4.bid;
|
||||
case 5: return direct5.bid;
|
||||
case 6: return direct6.bid;
|
||||
case 7: return direct7.bid;
|
||||
case 8: return direct8.bid;
|
||||
case 9: return direct9.bid;
|
||||
default: throw new ArgumentException("depth");
|
||||
}
|
||||
}
|
||||
public NativePriceSize ask(int depth) {
|
||||
switch (depth) {
|
||||
case 0: return ask0.load();
|
||||
case 1: return ask1.load();
|
||||
case 2: return ask2.load();
|
||||
case 3: return ask3.load();
|
||||
case 4: return ask4.load();
|
||||
case 5: return ask5.load();
|
||||
case 6: return ask6.load();
|
||||
case 7: return ask7.load();
|
||||
case 8: return ask8.load();
|
||||
case 9: return ask9.load();
|
||||
case 0: return direct0.ask;
|
||||
case 1: return direct1.ask;
|
||||
case 2: return direct2.ask;
|
||||
case 3: return direct3.ask;
|
||||
case 4: return direct4.ask;
|
||||
case 5: return direct5.ask;
|
||||
case 6: return direct6.ask;
|
||||
case 7: return direct7.ask;
|
||||
case 8: return direct8.ask;
|
||||
case 9: return direct9.ask;
|
||||
default: throw new ArgumentException("depth");
|
||||
}
|
||||
}
|
||||
|
||||
private AtomicPriceSizeStore bid0;
|
||||
private AtomicPriceSizeStore ask0;
|
||||
private AtomicPriceSizeStore bid1;
|
||||
private AtomicPriceSizeStore ask1;
|
||||
private AtomicPriceSizeStore bid2;
|
||||
private AtomicPriceSizeStore ask2;
|
||||
private AtomicPriceSizeStore bid3;
|
||||
private AtomicPriceSizeStore ask3;
|
||||
private AtomicPriceSizeStore bid4;
|
||||
private AtomicPriceSizeStore ask4;
|
||||
private AtomicPriceSizeStore bid5;
|
||||
private AtomicPriceSizeStore ask5;
|
||||
private AtomicPriceSizeStore bid6;
|
||||
private AtomicPriceSizeStore ask6;
|
||||
private AtomicPriceSizeStore bid7;
|
||||
private AtomicPriceSizeStore ask7;
|
||||
private AtomicPriceSizeStore bid8;
|
||||
private AtomicPriceSizeStore ask8;
|
||||
private AtomicPriceSizeStore bid9;
|
||||
private AtomicPriceSizeStore ask9;
|
||||
|
||||
// This would look much more elegant but it only works by marshaling magic - the layout in memory is not contiguous
|
||||
// [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
|
||||
// public PriceSize[] book = new PriceSize[20];
|
||||
public readonly UInt16 sequence;
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential, Size = 408)]
|
||||
[StructLayout(LayoutKind.Sequential, Size = 392)]
|
||||
internal struct ColdEntry
|
||||
{
|
||||
public int exchange_status;
|
||||
// Last trade
|
||||
public UInt64 last_trade_updated;
|
||||
public AtomicPriceSizeStore last_trade;
|
||||
public AtomicPriceSize last_trade;
|
||||
public ulong total_volume;
|
||||
public utils.LLDateTime last_trade_time;
|
||||
|
||||
//// Customer BBO
|
||||
public UInt64 customer_bbo_updated;
|
||||
public AtomicPriceSizeStore customer_bid;
|
||||
public AtomicPriceSizeStore customer_ask;
|
||||
|
||||
//// Settlement
|
||||
public UInt64 misc_timestamp;
|
||||
public double settlement_price;
|
||||
@@ -279,11 +244,12 @@ namespace mbts.co2
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
internal struct ColdHeader
|
||||
{
|
||||
internal uint version;
|
||||
internal ulong count;
|
||||
internal ulong segment_count;
|
||||
|
||||
internal ulong create_time;
|
||||
internal ulong heartbeat;
|
||||
internal TimeStamp create_time;
|
||||
internal TimeStamp heartbeat;
|
||||
internal bool disable_heartbeat;
|
||||
internal uint heartbeat_ms;
|
||||
internal uint heartbeat_boot_seconds;
|
||||
@@ -295,7 +261,9 @@ namespace mbts.co2
|
||||
internal ulong coconut_status_count; // atomic
|
||||
internal ulong exchange_status_count; // atomic
|
||||
internal ulong rfq_count; // atomic
|
||||
internal ulong customer_bbo_count; // atomic
|
||||
internal ulong gap_count; // atomic
|
||||
|
||||
// trade_buffer, rfq_buffer are not addressable
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -9,25 +9,45 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace mbts.co2
|
||||
{
|
||||
public struct NativePriceSize : IEquatable<NativePriceSize>
|
||||
{
|
||||
[StructLayout(LayoutKind.Explicit)]
|
||||
private struct PriceSizeUnion
|
||||
{
|
||||
[FieldOffset(0)]
|
||||
public UInt64 data;
|
||||
|
||||
[FieldOffset(0)]
|
||||
public Int64 price;
|
||||
[FieldOffset(5)]
|
||||
public sbyte exponent;
|
||||
[FieldOffset(6)]
|
||||
public UInt16 size;
|
||||
}
|
||||
static NativePriceSize() {
|
||||
Log.assert(Marshal.SizeOf(typeof(PriceSizeUnion)) == 8);
|
||||
[StructLayout(LayoutKind.Explicit)]
|
||||
public readonly struct NativePriceLevel {
|
||||
[FieldOffset(0)]
|
||||
readonly Int64 _mantissa_b; // : 56;
|
||||
[FieldOffset(7)]
|
||||
readonly sbyte _exponent_b; // : 8;
|
||||
[FieldOffset(8)]
|
||||
readonly Int64 _mantissa_a; // : 56;
|
||||
[FieldOffset(15)]
|
||||
readonly sbyte _exponent_a; //: 8;
|
||||
[FieldOffset(16)]
|
||||
readonly UInt32 _size_b;
|
||||
[FieldOffset(20)]
|
||||
readonly UInt32 _size_a;
|
||||
|
||||
long decode(long m) {
|
||||
long mantissa = m & 0xFFFFFFFFFFFFFF;
|
||||
// If the 56th bit is set this is negative
|
||||
const long bit56 = 0x80000000000000;
|
||||
if ((mantissa & bit56) != 0) {
|
||||
// Compute 2's compliment manually
|
||||
mantissa = -1 * (((~mantissa) & (bit56 - 1)) + 1);
|
||||
}
|
||||
return mantissa;
|
||||
}
|
||||
|
||||
public Int64 bid_mantissa => decode(_mantissa_b);
|
||||
public sbyte bid_exponent => _exponent_b;
|
||||
public Int64 ask_mantissa => decode(_mantissa_a);
|
||||
public sbyte ask_exponent => _exponent_a;
|
||||
public UInt32 bid_size => _size_b;
|
||||
public UInt32 ask_size => _size_a;
|
||||
|
||||
public NativePriceSize bid => new NativePriceSize(bid_mantissa, bid_exponent, bid_size);
|
||||
public NativePriceSize ask => new NativePriceSize(ask_mantissa, ask_exponent, ask_size);
|
||||
}
|
||||
|
||||
public static class NativePriceSizeHelpers {
|
||||
static double[] s_powers10 = {
|
||||
1E-20, 1E-19, 1E-18, 1E-17, 1E-16, 1E-15, 1E-14, 1E-13, 1E-12, 1E-11, 1E-10, 1E-9, 1E-8, 1E-7, 1E-6, 1E-5, 1E-4, 1E-3, 1E-2, 1E-1,
|
||||
1E0,
|
||||
@@ -35,7 +55,7 @@ namespace mbts.co2
|
||||
};
|
||||
static int s_max_abs_pow10 = s_powers10.Length/2;
|
||||
|
||||
private double pow10(int power) {
|
||||
public static double pow10(int power) {
|
||||
int offset = power + s_max_abs_pow10;
|
||||
Log.assert_d(offset >= 0 && offset < s_powers10.Length);
|
||||
if (offset >= 0 && offset < s_powers10.Length) {
|
||||
@@ -43,84 +63,44 @@ namespace mbts.co2
|
||||
}
|
||||
return double.NaN;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
internal NativePriceSize(UInt64 value) {
|
||||
// appease the compiler
|
||||
_value.price = 0;
|
||||
_value.size = 0;
|
||||
_value.exponent = 0;
|
||||
// actual initialization
|
||||
_value.data = value;
|
||||
}
|
||||
private static void compact_mantissa(ref Int64 mantissa, ref sbyte exponent) {
|
||||
while (mantissa > 0x8000000000 || mantissa < -0x8000000000) {
|
||||
mantissa /= 10;
|
||||
exponent += 1;
|
||||
}
|
||||
}
|
||||
public NativePriceSize(Int64 price, sbyte exponent, UInt16 size) {
|
||||
_value.data = 0;//appeasement
|
||||
compact_mantissa(ref price, ref exponent);
|
||||
if (price >= 0) {
|
||||
_value.price = price & 0x7FFFFFFFFF;
|
||||
}
|
||||
else {
|
||||
_value.price = 0x8000000000 | (price & 0x7FFFFFFFFF);
|
||||
}
|
||||
_value.exponent = exponent;
|
||||
_value.size = size;
|
||||
public readonly struct NativePriceSize : IEquatable<NativePriceSize>
|
||||
{
|
||||
public NativePriceSize(Int64 mantissa, sbyte exponent, uint size) {
|
||||
this.mantissa = mantissa;
|
||||
this.exponent = exponent;
|
||||
this.size = size;
|
||||
}
|
||||
|
||||
public static NativePriceSize no_market = new NativePriceSize(0, -128, 0);
|
||||
public static NativePriceSize no_market = new NativePriceSize(0, 0, 0);
|
||||
|
||||
public Int64 mantissa {
|
||||
get {
|
||||
Int64 price = _value.price & 0xFFFFFFFFFF;
|
||||
// If the 40th bit is set it's negative
|
||||
if ((price & 0x8000000000) != 0) {
|
||||
// Take the 2s compliment of the first 39 bits then negate
|
||||
price = -1 * (((~_value.price) & 0x7FFFFFFFFF) + 1);
|
||||
}
|
||||
return price;
|
||||
}
|
||||
}
|
||||
public sbyte exponent {
|
||||
get {
|
||||
return _value.exponent;
|
||||
}
|
||||
}
|
||||
public Int64 mantissa { get; }
|
||||
public sbyte exponent {get;}
|
||||
|
||||
public double price {
|
||||
get {
|
||||
if (_value.exponent == -128) return double.NaN;
|
||||
return mantissa * pow10(_value.exponent);
|
||||
}
|
||||
}
|
||||
public double price => (is_nan) ? double.NaN : mantissa * NativePriceSizeHelpers.pow10(exponent);
|
||||
|
||||
public bool is_nan {
|
||||
get {
|
||||
return _value.data == no_market._value.data;
|
||||
}
|
||||
}
|
||||
public bool is_nan => this == no_market;
|
||||
|
||||
public ushort size { get { return _value.size; } }
|
||||
public UInt64 raw { get { return _value.data; } }
|
||||
public uint size { get; }
|
||||
|
||||
public override string ToString() {
|
||||
return "{0:0.00} {1}".fmt(price, size);
|
||||
}
|
||||
|
||||
public override bool Equals(object obj) {
|
||||
if (obj is NativePriceSize) return Equals((NativePriceSize)obj);
|
||||
if (obj is NativePriceSize p) return Equals(p);
|
||||
return base.Equals(obj);
|
||||
}
|
||||
public override int GetHashCode() {
|
||||
return _value.data.GetHashCode();
|
||||
return bts.utils.EqualityComparer.aggregate_hash(mantissa.GetHashCode(), exponent.GetHashCode(), size.GetHashCode());
|
||||
}
|
||||
|
||||
public bool Equals(NativePriceSize other) {
|
||||
return _value.data == other._value.data;
|
||||
return size == other.size &&
|
||||
mantissa == other.mantissa &&
|
||||
exponent == other.exponent;
|
||||
}
|
||||
public static bool operator ==(NativePriceSize a, NativePriceSize b) {
|
||||
return a.Equals(b);
|
||||
@@ -128,76 +108,86 @@ namespace mbts.co2
|
||||
public static bool operator !=(NativePriceSize a, NativePriceSize b) {
|
||||
return !a.Equals(b);
|
||||
}
|
||||
|
||||
private PriceSizeUnion _value;
|
||||
}
|
||||
|
||||
internal struct AtomicPriceSizeStore
|
||||
{
|
||||
UInt64 _value;
|
||||
bool store(NativePriceSize ps) {
|
||||
bool result = _value != ps.raw;
|
||||
_value = ps.raw;
|
||||
return result;
|
||||
}
|
||||
struct TimeStamp {
|
||||
Int64 _value;
|
||||
public UInt64 time => (UInt64)Interlocked.Read(ref _value);
|
||||
public static UInt64 invalid_value => UInt64.MaxValue;
|
||||
}
|
||||
|
||||
public NativePriceSize load() {
|
||||
return new NativePriceSize(_value);
|
||||
// This type cannot be generic because we will get error CS0208
|
||||
//internal struct Optimistic<T> where T : struct {...
|
||||
internal struct AtomicPriceSize {
|
||||
TimeStamp _time;
|
||||
NativePriceSize _value;
|
||||
void store(NativePriceSize nps) {
|
||||
_time = new TimeStamp();
|
||||
_value = new NativePriceSize();
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
public (UInt64 time, NativePriceSize value) load() {
|
||||
UInt64 temp;
|
||||
NativePriceSize result;
|
||||
int attempts = 0;
|
||||
do {
|
||||
++attempts;
|
||||
temp = _time.time;
|
||||
result = _value;
|
||||
} while ((temp != _time.time || temp == TimeStamp.invalid_value) && attempts < 1619200);
|
||||
return (temp, result);
|
||||
}
|
||||
public bool load_if_new(ref UInt64 time, out NativePriceSize t) {
|
||||
if (_time.time > time || _time.time == TimeStamp.invalid_value) {
|
||||
(time, t) = load();
|
||||
return (time != TimeStamp.invalid_value);
|
||||
}
|
||||
t = NativePriceSize.no_market;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public struct PriceSize : IEquatable<PriceSize>
|
||||
{
|
||||
readonly double _price;
|
||||
readonly uint _size;
|
||||
|
||||
public PriceSize(double price, uint size)
|
||||
{
|
||||
_price = price;
|
||||
_size = size;
|
||||
this.price = price;
|
||||
this.size = size;
|
||||
}
|
||||
|
||||
public PriceSize(NativePriceSize nps)
|
||||
{
|
||||
_price = nps.is_nan ? double.NaN : nps.price;
|
||||
_size = nps.size;
|
||||
public PriceSize(NativePriceSize nps) {
|
||||
price = nps.price;
|
||||
size = nps.size;
|
||||
}
|
||||
|
||||
public static implicit operator PriceSize(NativePriceSize nps)
|
||||
{
|
||||
public static implicit operator PriceSize(NativePriceSize nps) {
|
||||
return new PriceSize(nps);
|
||||
}
|
||||
|
||||
public static PriceSize no_market = NativePriceSize.no_market;
|
||||
|
||||
public double price { get { return _price; } }
|
||||
public uint size { get { return _size; } }
|
||||
public double price { get; }
|
||||
public uint size { get; }
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
public override string ToString() {
|
||||
return "{0:0.00} {1}".fmt(price, size);
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if(obj is PriceSize) return Equals((PriceSize)obj);
|
||||
public override bool Equals(object obj) {
|
||||
if (obj is PriceSize p) return Equals(p);
|
||||
return base.Equals(obj);
|
||||
}
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return _price.GetHashCode() ^ _size.GetHashCode();
|
||||
public override int GetHashCode() {
|
||||
return bts.utils.EqualityComparer.aggregate_hash(price.GetHashCode(), size.GetHashCode());
|
||||
}
|
||||
|
||||
public bool Equals(PriceSize other)
|
||||
{
|
||||
return _price == other.price && _size == other.size;
|
||||
public bool Equals(PriceSize other) {
|
||||
return price == other.price && size == other.size;
|
||||
}
|
||||
public static bool operator ==(PriceSize a, PriceSize b)
|
||||
{
|
||||
public static bool operator ==(PriceSize a, PriceSize b) {
|
||||
return a.Equals(b);
|
||||
}
|
||||
public static bool operator !=(PriceSize a, PriceSize b)
|
||||
{
|
||||
public static bool operator !=(PriceSize a, PriceSize b) {
|
||||
return !a.Equals(b);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
|
||||
<WarningsAsErrors />
|
||||
<Version>1.0.0</Version>
|
||||
<LangVersion>7.2</LangVersion>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
+4
-5
@@ -1,10 +1,9 @@
|
||||
using mbts.spark.utilities;
|
||||
using System;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace mbts.utils {
|
||||
namespace bts.utils {
|
||||
public static class EqualityComparer {
|
||||
const int _hash_seed = 63607, _hash_multiplier = 6451;
|
||||
|
||||
@@ -15,13 +14,13 @@ namespace mbts.utils {
|
||||
new AnonymousEqualityComparer<T>(equals, get_hash_code);
|
||||
|
||||
public static IEqualityComparer<T> Create<T, TKey>(Func<T, TKey> key_selector) =>
|
||||
Create(key_selector, EqualityComparer<TKey>.Default);
|
||||
Create(key_selector, System.Collections.Generic.EqualityComparer<TKey>.Default);
|
||||
|
||||
public static IEqualityComparer<T> Create<T, TKey>(Func<T, TKey> key_selector, IEqualityComparer<TKey> key_comparer) =>
|
||||
Create<T>((x, y) => key_comparer.Equals(key_selector(x), (key_selector(y))), x => key_selector(x).GetHashCode());
|
||||
|
||||
public static IEqualityComparer<T?> CreateNullable<T, TKey>(Func<T, TKey> key_selector) where T : struct {
|
||||
var eqc = EqualityComparer<TKey>.Default;
|
||||
var eqc = System.Collections.Generic.EqualityComparer<TKey>.Default;
|
||||
return Create<T?>((x, y) => x.HasValue ? y.HasValue && eqc.Equals(key_selector(x.Value), key_selector(y.Value)) : !y.HasValue);
|
||||
}
|
||||
|
||||
+1
-1
@@ -2,7 +2,7 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace mbts.spark.utilities {
|
||||
namespace bts.utils {
|
||||
static class MathUtils {
|
||||
public const double epsilon = 1E-12;
|
||||
public static bool epsilon_equals(double x, double y) =>
|
||||
@@ -14,15 +14,15 @@ namespace mbts.co2.tests
|
||||
[TestGroup]
|
||||
public class Co2Tests
|
||||
{
|
||||
[Test,Ignore("AccessViolation")]
|
||||
[Test]
|
||||
public void BlitPointerTuple() {
|
||||
UInt64 out_size;
|
||||
Co2NativeMethods.BlitPointerTuple tup;
|
||||
Co2NativeTestMethods.get_1234_handle(out out_size, out tup);
|
||||
Co2NativeTestMethods.get_1234_handle(out out_size, out tup, out IntPtr hotaddr, out IntPtr coldaddr, out IntPtr headeraddr);
|
||||
Assert.IsTrue(out_size == (ulong)Marshal.SizeOf(typeof(Co2NativeMethods.BlitPointerTuple)));
|
||||
Assert.IsTrue(tup.hot == new IntPtr(100));
|
||||
Assert.IsTrue(tup.cold_data == new IntPtr(200));
|
||||
Assert.IsTrue(tup.cold_header == new IntPtr(300));
|
||||
Assert.IsTrue(tup.hot == hotaddr);
|
||||
Assert.IsTrue(tup.cold_data == coldaddr);
|
||||
Assert.IsTrue(tup.cold_header == headeraddr);
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -39,7 +39,7 @@ namespace mbts.co2.tests
|
||||
Assert.IsTrue(ps.size == 100);
|
||||
|
||||
// Oversized value
|
||||
ps = new NativePriceSize(2030000000000, -9, 1);
|
||||
ps = new NativePriceSize(203000000000, -8, 1);
|
||||
Assert.AreEqual(ps.price, 2030);
|
||||
Assert.IsTrue(ps.size == 1);
|
||||
|
||||
@@ -54,14 +54,15 @@ namespace mbts.co2.tests
|
||||
var entry = new MDConsumerHandle.HotEntry();
|
||||
Co2NativeTestMethods.co2_get_sample_hot(out size, ref entry);
|
||||
Assert.IsTrue(size == (ulong)Marshal.SizeOf(entry));
|
||||
Assert.IsTrue(entry.timestamp > 0);
|
||||
var bid = entry.bid.load();
|
||||
var ask = entry.ask.load();
|
||||
Assert.IsTrue(entry.timestamp.time > 0);
|
||||
var bid = entry.book.bid;
|
||||
var ask = entry.book.ask;
|
||||
|
||||
Assert.IsTrue(bid.price == 7.2);
|
||||
Assert.IsTrue(bid.size == 101);
|
||||
Assert.IsTrue(ask.price.ToString("0.00") == "-9.70");
|
||||
|
||||
Assert.IsTrue(ask.size == 245);
|
||||
Assert.AreEqual(ask.price.ToString("0.00"), "-9.70");
|
||||
Assert.IsTrue(entry.status == CoconutSecurityStatus.PacketLoss);
|
||||
Assert.IsTrue(entry.key == 1234);
|
||||
Assert.IsTrue(entry.sequence == 777);
|
||||
@@ -106,20 +107,12 @@ namespace mbts.co2.tests
|
||||
Assert.AreEqual(entry_size, (ulong)Marshal.SizeOf(entry), "If this test fails it's probably due to an addition to the FixedSecurity struct in FixedSecurity.h");
|
||||
|
||||
// Last trade
|
||||
Assert.IsTrue(entry.last_trade_updated > 0);
|
||||
var last = entry.last_trade.load();
|
||||
(var time, var last) = entry.last_trade.load();
|
||||
Assert.IsTrue(time > 0);
|
||||
Assert.IsTrue(last.price == 4.2);
|
||||
Assert.IsTrue(last.size == 101);
|
||||
Assert.IsTrue(entry.total_volume == 12345);
|
||||
Assert.IsTrue(compare_date(entry.last_trade_time, DateTime.Now));
|
||||
//// Customer bo
|
||||
Assert.IsTrue(entry.customer_bbo_updated > 0);
|
||||
var cask = entry.customer_ask.load();
|
||||
Assert.IsTrue(cask.price == 2.2);
|
||||
Assert.IsTrue(cask.size == 202);
|
||||
var cbid = entry.customer_bid.load();
|
||||
Assert.IsTrue(cbid.price == 1.1);
|
||||
Assert.IsTrue(cbid.size == 101);
|
||||
//// Settlement
|
||||
Assert.AreEqual(entry.settlement_price, 3.3);
|
||||
Assert.IsTrue(entry.settlement_date == new DateTime(2015, 9, 3));
|
||||
@@ -146,8 +139,8 @@ namespace mbts.co2.tests
|
||||
var header = new MDConsumerHandle.ColdHeader();
|
||||
Co2NativeTestMethods.co2_get_sample_cold_header(ref header);
|
||||
Assert.IsTrue(header.segment_count == 1);
|
||||
Assert.IsTrue(header.create_time > 0);
|
||||
Assert.IsTrue(header.heartbeat > 0);
|
||||
Assert.IsTrue(header.create_time.time > 0);
|
||||
Assert.IsTrue(header.heartbeat.time > 0);
|
||||
Assert.IsTrue(header.security_count == 77000);
|
||||
Assert.IsTrue(header.has_depth == true);
|
||||
Assert.IsTrue(header.platform == Platform.Sim);
|
||||
@@ -155,7 +148,7 @@ namespace mbts.co2.tests
|
||||
Assert.IsTrue(header.coconut_status_count == 101);
|
||||
Assert.IsTrue(header.exchange_status_count == 102);
|
||||
Assert.IsTrue(header.rfq_count == 103);
|
||||
Assert.IsTrue(header.customer_bbo_count == 104);
|
||||
Assert.IsTrue(header.gap_count == 104);
|
||||
}
|
||||
|
||||
[Test,Ignore("")]
|
||||
@@ -216,10 +209,6 @@ namespace mbts.co2.tests
|
||||
Assert.IsTrue(ask.price == 2.2);
|
||||
Assert.IsTrue(ask.size == 202);
|
||||
|
||||
time = 0;
|
||||
Assert.IsTrue(handle.get_customer_bbo(ref time, out bid, out ask));
|
||||
Assert.IsTrue(fmt(bid.price) == fmt(3.3) && bid.size == 303);
|
||||
Assert.IsTrue(ask.price == 4.4 && ask.size == 404);
|
||||
time = 0;
|
||||
DateTime trade_date;
|
||||
ulong total_volume;
|
||||
@@ -246,5 +235,22 @@ namespace mbts.co2.tests
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void price_size_nan() {
|
||||
NativePriceLevel npl = new NativePriceLevel();
|
||||
Assert.IsTrue(npl.bid.is_nan);
|
||||
Assert.IsTrue(double.IsNaN(npl.bid.price));
|
||||
Assert.IsTrue(npl.ask.is_nan);
|
||||
Assert.IsTrue(double.IsNaN(npl.ask.price));
|
||||
|
||||
Co2NativeTestMethods.co2_get_zero_and_nan_book(ref npl);
|
||||
Assert.AreEqual(npl.ask.price, 0);
|
||||
Assert.AreEqual(npl.ask.size, 1u);
|
||||
Assert.AreEqual(npl.bid.size, 0u);
|
||||
Assert.IsTrue(npl.bid.is_nan);
|
||||
Assert.IsTrue(double.IsNaN(npl.bid.price));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,7 @@
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net471</TargetFramework>
|
||||
<!--<TargetFrameworks>net471;netcoreapp2.1</TargetFrameworks>-->
|
||||
<RuntimeIdentifiers>win7-x64;ubuntu.16.04-x64;win7-x86</RuntimeIdentifiers>
|
||||
<RuntimeIdentifiers>win7-x64;ubuntu.16.04-x64</RuntimeIdentifiers>
|
||||
<Authors>Blue Trading Systems</Authors>
|
||||
<Company>Blue Trading Systems</Company>
|
||||
<Product>Spark</Product>
|
||||
|
||||
@@ -36,8 +36,8 @@ string dump(PriceSize ps) {
|
||||
return res.str();
|
||||
}
|
||||
|
||||
string dump(const SingleWriterPriceSizeStore& pss) {
|
||||
return dump(pss.load());
|
||||
string dump(const AtomicPriceSize& pss) {
|
||||
return dump(std::get<PriceSize>(pss.load()));
|
||||
}
|
||||
|
||||
string dump_header(const ColdHeader& header) {
|
||||
@@ -55,7 +55,6 @@ string dump_header(const ColdHeader& header) {
|
||||
res << "'exchange_status_count': " << header.exchange_status_count << ", ";
|
||||
res << "'rfq_count': " << header.rfq_count << ", ";
|
||||
res << "'gaps':" << header.gap_count << ", ";
|
||||
// res << "'customer_bbo_count': " << header.customer_bbo_count;
|
||||
res << "}";
|
||||
return res.str();
|
||||
}
|
||||
@@ -137,7 +136,7 @@ string dump(datetime::Date date) {
|
||||
}
|
||||
|
||||
bool has_value(PriceSize p) { return p.size() > 0; }
|
||||
bool has_value(const SingleWriterPriceSizeStore& p) { return has_value(p.load()); }
|
||||
bool has_value(const AtomicPriceSize& p) { return has_value(std::get<PriceSize>(p.load())); }
|
||||
|
||||
string dump_entry(const ColdEntry& entry) {
|
||||
stringstream res;
|
||||
@@ -146,8 +145,6 @@ string dump_entry(const ColdEntry& entry) {
|
||||
if (has_value(entry.last_trade)) res << "'last_trade': " << dump(entry.last_trade) << ", ";
|
||||
if (entry.total_volume > 0) res << "'total_volume': " << entry.total_volume << ", ";
|
||||
if (entry.last_trade_time > 0) res << "'last_trade_time_ll': " << entry.last_trade_time << ", ";
|
||||
if (has_value(entry.customer_bid)) res << "'customer_bid': " << dump(entry.customer_bid) << ", ";
|
||||
if (has_value(entry.customer_ask)) res << "'customer_ask': " << dump(entry.customer_ask) << ", ";
|
||||
if (!os::is_nan(entry.misc.settlement_price)) res << "'settlement_price': " << entry.misc.settlement_price << ", ";
|
||||
if (entry.misc.settlement_date.year > 0) res << "'settlement_date': " << dump(entry.misc.settlement_date) << ", ";
|
||||
if (!os::is_nan(entry.misc.previous_settlement_price)) res << "'previous_settlement_price': " << entry.misc.previous_settlement_price << ", ";
|
||||
@@ -165,13 +162,13 @@ string dump_entry(const HotDepthEntry& entry) {
|
||||
|
||||
res << "'bids': [";
|
||||
for (uint32_t x = 0; x < COCONUT_MAX_DEPTH; ++x) {
|
||||
res << dump(entry.bid(x)) << ", ";
|
||||
res << dump(entry.at(x)->bid()) << ", ";
|
||||
}
|
||||
res << "], ";
|
||||
|
||||
res << "'asks': [";
|
||||
for (uint32_t x = 0; x < COCONUT_MAX_DEPTH; ++x) {
|
||||
res << dump(entry.ask(x)) << ", ";
|
||||
res << dump(entry.at(x)->ask()) << ", ";
|
||||
}
|
||||
res << "], ";
|
||||
|
||||
@@ -186,8 +183,8 @@ string dump_entry(const HotEntry& entry) {
|
||||
res << "{ ";
|
||||
res << "'timestamp': " << dump(entry.time()) << ", ";
|
||||
res << "'key': " << entry.key << ", ";
|
||||
res << "'bid': " << dump(entry.bid) << ", ";
|
||||
res << "'ask': " << dump(entry.ask) << ", ";
|
||||
res << "'bid': " << dump(entry.book.bid()) << ", ";
|
||||
res << "'ask': " << dump(entry.book.ask()) << ", ";
|
||||
res << "'status': '" << CoconutSecurityStatus_Name((CoconutSecurityStatus)entry.status) << "', ";
|
||||
res << "'sequence': '" << entry.sequence << "', ";
|
||||
res << " }";
|
||||
@@ -216,7 +213,10 @@ void dump_hot_t(SharedListReader<HotHeader, TEntry>& reader) {
|
||||
if (!reader.is_initialized()) { LOG(md,Info,"Unable to initialize section "); return; }
|
||||
auto& header = reader.header();
|
||||
cout << "{ " << endl;
|
||||
cout << "'header': { 'count': " << header.count << ", 'segment_count': " << header.segment_count << ", 'bbo_count': " << header.bbo_count << ", 'depth_count': " << header.depth_count << "}," << endl;
|
||||
cout << "'header': { 'count': " << header.count
|
||||
<< ", 'segment_count': " << header.segment_count
|
||||
<< ", 'direct_count': " << header.direct_count
|
||||
<< ", 'implied_count': " << header.implied_count << "}," << endl;
|
||||
cout << "'entries': [ " << endl;
|
||||
reader.all_entries([&](const TEntry& entry) { cout << dump_entry(entry) << "," << endl; });
|
||||
cout << "] }" << endl;
|
||||
|
||||
@@ -932,13 +932,13 @@ TimeStamp MDConsumerChannel::create_time() const { return _pimpl ? _pimpl->cold_
|
||||
size_t MDConsumerChannel::security_count() const {
|
||||
return _pimpl->cold_header().security_count;
|
||||
}
|
||||
size_t MDConsumerChannel::bbo_count() const {
|
||||
size_t MDConsumerChannel::direct_count() const {
|
||||
return is_initialized() ?
|
||||
_pimpl->hot_header().bbo_count : 0;
|
||||
_pimpl->hot_header().direct_count : 0;
|
||||
}
|
||||
size_t MDConsumerChannel::depth_count() const {
|
||||
size_t MDConsumerChannel::implied_count() const {
|
||||
return is_initialized() ?
|
||||
_pimpl->hot_header().depth_count : 0;
|
||||
_pimpl->hot_header().implied_count : 0;
|
||||
}
|
||||
size_t MDConsumerChannel::trade_count() const {
|
||||
return _pimpl->cold_header().trade_count;
|
||||
@@ -946,9 +946,6 @@ size_t MDConsumerChannel::trade_count() const {
|
||||
size_t MDConsumerChannel::rfq_count() const {
|
||||
return _pimpl->cold_header().rfq_count;
|
||||
}
|
||||
size_t MDConsumerChannel::customer_bbo_count() const {
|
||||
return _pimpl->cold_header().customer_bbo_count;
|
||||
}
|
||||
size_t MDConsumerChannel::gap_count() const {
|
||||
return _pimpl->cold_header().gap_count;
|
||||
}
|
||||
@@ -959,7 +956,10 @@ bool is_heart_beat_ok(const ColdHeader &header ) {
|
||||
uint32_t s_grace_period_ms = header.heartbeat_boot_seconds * 1000;
|
||||
TimeStamp hb = header.heartbeat;
|
||||
TimeStamp now = TimeStamp::now();
|
||||
return s_disable_heartbeat || (now - hb).milliseconds() < s_heartbeat_ms || (now - header.create_time).milliseconds() < s_grace_period_ms;
|
||||
return (header.version == shm_version) &&
|
||||
(s_disable_heartbeat ||
|
||||
(now - hb).milliseconds() < s_heartbeat_ms ||
|
||||
(now - header.create_time).milliseconds() < s_grace_period_ms);
|
||||
}
|
||||
|
||||
}}//namespace
|
||||
|
||||
@@ -87,11 +87,10 @@ public:
|
||||
//
|
||||
// Statistic counters
|
||||
size_t security_count() const;
|
||||
size_t bbo_count() const;
|
||||
size_t depth_count() const;
|
||||
size_t direct_count() const;
|
||||
size_t implied_count() const;
|
||||
size_t trade_count() const;
|
||||
size_t rfq_count() const;
|
||||
size_t customer_bbo_count() const;
|
||||
size_t gap_count() const;
|
||||
|
||||
};
|
||||
|
||||
@@ -46,8 +46,8 @@ TimeStamp copy_bbo(const HotEntry& hot, PriceSize& out_bid, PriceSize& out_ask)
|
||||
const int max_attempts = 1619200;
|
||||
do {
|
||||
t = hot.time();
|
||||
out_bid = hot.bid.load();
|
||||
out_ask = hot.ask.load();
|
||||
out_bid = hot.book.bid();
|
||||
out_ask = hot.book.ask();
|
||||
++attempts;
|
||||
} while((t != hot.time() || t == TimeStamp::invalid()) && attempts < max_attempts);
|
||||
if (attempts >= max_attempts) {
|
||||
@@ -67,8 +67,7 @@ TimeStamp copy_depth(
|
||||
do {
|
||||
t = hot.time();
|
||||
for (size_t x = 0; x < count; ++x) {
|
||||
bid[x] = hot.bid(x).load();
|
||||
ask[x] = hot.ask(x).load();
|
||||
hot.read_direct(x, &bid[x], &ask[x]);
|
||||
}
|
||||
++attempts;
|
||||
} while((t != hot.time() || t == TimeStamp::invalid()) && attempts < max_attempts);
|
||||
@@ -215,42 +214,16 @@ TimeStamp MDConsumerHandle::last_market_update() const {
|
||||
never;
|
||||
}
|
||||
|
||||
bool MDConsumerHandle::get_customer_bbo(TimeStamp& time, PriceSize& out_bid, PriceSize& out_ask) const {
|
||||
switch(_type) {
|
||||
case HANDLETYPE_SYNTHETIC:
|
||||
case HANDLETYPE_VOID:
|
||||
return false;
|
||||
case HANDLETYPE_DEPTH:
|
||||
case HANDLETYPE_BBO: {
|
||||
const ColdEntry* pcold = (_type == HANDLETYPE_BBO) ? bbo.cdata : book.cdata;
|
||||
// This can loop forever if publisher crashes with an invalid time in shm
|
||||
// good thing nobody uses customer_bbo
|
||||
bool result = false;
|
||||
while(pcold->customer_bbo_updated > time || time == TimeStamp::invalid()) {
|
||||
time = pcold->customer_bbo_updated;
|
||||
out_bid = pcold->customer_bid.load();
|
||||
out_ask = pcold->customer_ask.load();
|
||||
result = true;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
never;
|
||||
}
|
||||
|
||||
bool MDConsumerHandle::get_last_trade(TimeStamp& time, PriceSize& trade, datetime::LLDateTime& trade_timestamp, size_t* total_volume) const {
|
||||
switch(_type) {
|
||||
case HANDLETYPE_VOID: return false;
|
||||
case HANDLETYPE_DEPTH:
|
||||
case HANDLETYPE_BBO: {
|
||||
const ColdEntry* pcold = (_type == HANDLETYPE_BBO) ? bbo.cdata : book.cdata;
|
||||
bool result = false;
|
||||
while (pcold->last_trade_updated > time) {
|
||||
time = pcold->last_trade_updated;
|
||||
trade = pcold->last_trade.load();
|
||||
bool result = pcold->last_trade.load_if_new(time, trade);
|
||||
if (result) {
|
||||
trade_timestamp = pcold->last_trade_time;
|
||||
if (total_volume) { *total_volume = pcold->total_volume; }
|
||||
result = true;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -136,7 +136,6 @@ public:
|
||||
bts_assert(false);
|
||||
}
|
||||
|
||||
bool get_customer_bbo(TimeStamp& time, PriceSize& out_bid, PriceSize& out_ask) const;
|
||||
bool get_last_trade(TimeStamp& time, PriceSize& out_trade, datetime::LLDateTime& out_trade_timestamp, size_t* total_volume=nullptr) const;
|
||||
|
||||
TimeStamp misc_timestamp() const;
|
||||
|
||||
@@ -29,7 +29,7 @@ public:
|
||||
_cold.header().has_depth = enable_depth(symbol, type);
|
||||
_cold.header().platform = platform;
|
||||
|
||||
static_assert(sizeof(HotEntry) <= 32,"HotEntry should be less than 32 bytes");
|
||||
// static_assert(sizeof(HotEntry) <= 32,"HotEntry should be less than 32 bytes");
|
||||
}
|
||||
|
||||
MDProducerHandle add_security(const FixedSecurity& security) {
|
||||
|
||||
@@ -77,13 +77,12 @@ void MDProducerHandle::update_bbo(TimeStamp time, const PriceSize bid, const Pri
|
||||
if (!has_depth()) {
|
||||
auto oldtime = hdata().time();
|
||||
hdata().set_time(TimeStamp::invalid());
|
||||
bool set_bid = hdata().bid.store(bid);
|
||||
bool set_ask = hdata().ask.store(ask);
|
||||
bool updated = hdata().book.update(PriceLevel(bid,ask));
|
||||
hdata().status = COCONUTSECURITYSTATUS_OK;
|
||||
if (set_bid || set_ask) {
|
||||
if (updated) {
|
||||
hdata().set_time(time);
|
||||
hdata().sequence = seq;
|
||||
hheader().bbo_count++;
|
||||
hheader().direct_count++;
|
||||
}
|
||||
else {
|
||||
hdata().set_time(oldtime);
|
||||
@@ -92,13 +91,12 @@ void MDProducerHandle::update_bbo(TimeStamp time, const PriceSize bid, const Pri
|
||||
else {
|
||||
auto oldtime = hdepth().time();
|
||||
hdepth().set_time(TimeStamp::invalid());
|
||||
bool set_bid = hdepth().bid(0).store(bid);
|
||||
bool set_ask = hdepth().ask(0).store(ask);
|
||||
bool updated = hdepth().write_direct(0, bid,ask);
|
||||
hdepth().status = COCONUTSECURITYSTATUS_OK;
|
||||
if (set_bid || set_ask) {
|
||||
if (updated) {
|
||||
hdepth().set_time(time);
|
||||
hdepth().sequence = seq;
|
||||
hheader().bbo_count++;
|
||||
hheader().direct_count++;
|
||||
}
|
||||
else {
|
||||
hdepth().set_time(oldtime);
|
||||
@@ -116,10 +114,9 @@ void MDProducerHandle::set_status(CoconutSecurityStatus status) {
|
||||
}
|
||||
|
||||
void MDProducerHandle::add_trade(TimeStamp time, const PriceSize price, LLDateTime tradetime, co2::AggressorSide agside, uint32_t volume) {
|
||||
cdata().last_trade.store(price);
|
||||
cdata().last_trade.store(price, time);
|
||||
cdata().last_trade_time = tradetime;
|
||||
cdata().total_volume = volume;
|
||||
cdata().last_trade_updated = time;
|
||||
|
||||
if (key() != invalid_security_key) {
|
||||
co2::TradeMessage trade;
|
||||
@@ -134,10 +131,9 @@ void MDProducerHandle::add_trade(TimeStamp time, const PriceSize price, LLDateTi
|
||||
}
|
||||
}
|
||||
void MDProducerHandle::set_last_trade(TimeStamp time, const PriceSize ps, datetime::LLDateTime tradetime, uint32_t total_volume) {
|
||||
cdata().last_trade.store(ps);
|
||||
cdata().last_trade.store(ps,time);
|
||||
cdata().last_trade_time = tradetime;
|
||||
cdata().total_volume = total_volume;
|
||||
cdata().last_trade_updated = time;
|
||||
}
|
||||
|
||||
void MDProducerHandle::add_rfq(TimeStamp, const co2::QuoteRequestMessage& rfq) {
|
||||
@@ -147,13 +143,6 @@ void MDProducerHandle::add_rfq(TimeStamp, const co2::QuoteRequestMessage& rfq) {
|
||||
}
|
||||
}
|
||||
|
||||
void MDProducerHandle::update_customer_bbo(TimeStamp time, const PriceSize bid, const PriceSize ask) {
|
||||
cdata().customer_bid.store(bid);
|
||||
cdata().customer_ask.store(ask);
|
||||
cdata().customer_bbo_updated = time;
|
||||
cheader().customer_bbo_count++;
|
||||
}
|
||||
|
||||
void MDProducerHandle::update_exchange_status(TimeStamp, TradingStatus status) {
|
||||
cdata().exchange_status = status;
|
||||
cheader().exchange_status_count++;
|
||||
|
||||
@@ -35,7 +35,7 @@ public:
|
||||
void set_legs(const std::vector<security_key>& legs);
|
||||
|
||||
void update_bbo(TimeStamp time, const PriceSize bid_price, const PriceSize ask_price, uint16_t seq=0); // hot
|
||||
void update_book(TimeStamp time, std::array<PriceSize,COCONUT_MAX_DEPTH>& bids, std::array<PriceSize,COCONUT_MAX_DEPTH>& asks, uint16_t seq=0) {
|
||||
void update_book(TimeStamp time, std::array<PriceSize,COCONUT_MAX_DEPTH>& bids, std::array<PriceSize,COCONUT_MAX_DEPTH>& asks, uint32_t seq=0) {
|
||||
if (!has_depth()) {
|
||||
update_bbo(time, bids[0], asks[0], seq);
|
||||
}
|
||||
@@ -43,13 +43,25 @@ public:
|
||||
auto& book = hdepth();
|
||||
book.set_time(TimeStamp::invalid());
|
||||
for (size_t x = 0; x < COCONUT_MAX_DEPTH; ++x) {
|
||||
book.bid(x).store(bids[x]);
|
||||
book.ask(x).store(asks[x]);
|
||||
book.write_direct(x, bids[x], asks[x]);
|
||||
}
|
||||
book.status = co2::COCONUTSECURITYSTATUS_OK;
|
||||
book.sequence = seq;
|
||||
book.set_time(time);
|
||||
hheader().depth_count++;
|
||||
hheader().direct_count += 1;
|
||||
}
|
||||
}
|
||||
void update_implied(TimeStamp time, std::array<PriceSize,COCONUT_IMPLIED_DEPTH>& bids, std::array<PriceSize,COCONUT_IMPLIED_DEPTH>& asks, uint32_t seq=0) {
|
||||
if (has_depth()) {
|
||||
auto& book = hdepth();
|
||||
book.set_time(TimeStamp::invalid());
|
||||
for (size_t x = 0; x < COCONUT_MAX_DEPTH; ++x) {
|
||||
book.write_implied(x, bids[x], asks[x]);
|
||||
}
|
||||
book.status = co2::COCONUTSECURITYSTATUS_OK;
|
||||
book.sequence = seq;
|
||||
book.set_time(time);
|
||||
hheader().implied_count += 1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -61,7 +73,6 @@ public:
|
||||
void set_last_trade(TimeStamp time, const PriceSize ps, datetime::LLDateTime date, uint32_t total_volume);
|
||||
void add_rfq(TimeStamp time, const co2::QuoteRequestMessage& rfq); // cold
|
||||
|
||||
void update_customer_bbo(TimeStamp time, const PriceSize bid, const PriceSize ask); // cold
|
||||
void update_exchange_status(TimeStamp time, TradingStatus status); // cold
|
||||
|
||||
// Misc fields
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
namespace bts {
|
||||
namespace md {
|
||||
|
||||
BTS_EXPORT PriceSize PriceSize::NO_MARKET = PriceSize(0, -128, 0);
|
||||
BTS_EXPORT PriceSize PriceSize::NO_MARKET = PriceSize();
|
||||
|
||||
int to_exponent(double value) {
|
||||
if (value == 0 && os::is_nan(value)) return 0;
|
||||
|
||||
+90
-53
@@ -6,6 +6,7 @@
|
||||
#include "lib/utils/bts_array_size.h"
|
||||
#include "lib/utils/bts_assert.h"
|
||||
#include "lib/utils/bts_no_copy.h"
|
||||
#include "lib/utils/Optimistic.h"
|
||||
|
||||
#include <atomic>
|
||||
#include <limits>
|
||||
@@ -15,10 +16,10 @@ namespace bts {
|
||||
namespace md {
|
||||
|
||||
typedef double Price;
|
||||
typedef uint16_t Size;
|
||||
typedef uint32_t Size;
|
||||
|
||||
#define LOAD(x) x.load(std::memory_order_acquire)
|
||||
#define STORE(x,value) x.store(value, std::memory_order_release)
|
||||
// #define LOAD(x) x.load(std::memory_order_acquire)
|
||||
// #define STORE(x,value) x.store(value, std::memory_order_release)
|
||||
|
||||
static const double powers_10[] = {
|
||||
1E-20, 1E-19, 1E-18, 1E-17, 1E-16, 1E-15, 1E-14, 1E-13, 1E-12, 1E-11, 1E-10, 1E-9, 1E-8, 1E-7, 1E-6, 1E-5, 1E-4, 1E-3, 1E-2, 1E-1,
|
||||
@@ -49,54 +50,28 @@ inline bool compact_mantissa(Mantissa& mantissa, Exponent& exponent) {
|
||||
}
|
||||
|
||||
class PriceSize {
|
||||
union {
|
||||
uint64_t _val64;
|
||||
// ISO C++ prohibits anonymous structs
|
||||
GCC_DIAG_OFF("-Wpedantic")
|
||||
struct {
|
||||
#ifdef _WIN32
|
||||
// VC++ only packs bit fields when the declared type is the same size
|
||||
int64_t _mantissa : 40;
|
||||
int64_t _exponent : 8;
|
||||
uint64_t _size : 16;
|
||||
#else
|
||||
int64_t _mantissa : 40;
|
||||
int8_t _exponent;
|
||||
uint16_t _size;
|
||||
#endif
|
||||
};
|
||||
GCC_DIAG_RESTORE()
|
||||
};
|
||||
friend class SingleWriterPriceSizeStore;
|
||||
PriceSize(uint64_t val) : _val64(val) { }
|
||||
int64_t _mantissa;
|
||||
int8_t _exponent;
|
||||
uint32_t _size;
|
||||
|
||||
public:
|
||||
// fast path
|
||||
PriceSize(int64_t mantissa, int8_t exponent, Size size) :
|
||||
_mantissa(mantissa),
|
||||
_exponent(exponent),
|
||||
_size(size)
|
||||
{
|
||||
// Attempt to guard against too large mantissa
|
||||
if (compact_mantissa(mantissa, exponent)) {
|
||||
_mantissa = mantissa;
|
||||
_exponent = exponent;
|
||||
}
|
||||
// bts_assert_d(exponent != 0);
|
||||
}
|
||||
PriceSize() : _val64(PriceSize::NO_MARKET._val64) { }
|
||||
PriceSize() : _mantissa(0), _exponent(), _size(0) { }
|
||||
|
||||
// quick copy
|
||||
PriceSize(const PriceSize& other) : _val64(other._val64) { }
|
||||
PriceSize& operator=(const PriceSize& other) {
|
||||
_val64 = other._val64;
|
||||
return *this;
|
||||
}
|
||||
PriceSize(const PriceSize& other) = default;
|
||||
PriceSize& operator=(const PriceSize& other) = default;
|
||||
|
||||
bool operator==(const PriceSize& other) const {
|
||||
return _val64 == other._val64;
|
||||
return (_size == other._size && _mantissa == other._mantissa && _exponent == other._exponent);
|
||||
}
|
||||
bool operator!=(const PriceSize& other) const {
|
||||
return _val64 != other._val64;
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
BTS_EXPORT static PriceSize NO_MARKET;
|
||||
@@ -136,35 +111,97 @@ public:
|
||||
}
|
||||
// This call is relatively slow so don't call it often
|
||||
double price() const {
|
||||
if (_exponent == -128) return NO_PRICE;
|
||||
if (*this == NO_MARKET) return NO_PRICE;
|
||||
return mantissa() * pow10(exponent());
|
||||
}
|
||||
Size size() const {
|
||||
return _size;
|
||||
}
|
||||
};
|
||||
static_assert(sizeof(PriceSize) == sizeof(uint64_t), "PriceSize must be 64 bits for atomic assignment");
|
||||
static_assert(sizeof(PriceSize) == 16, "PriceSize should be small");
|
||||
|
||||
class SingleWriterPriceSizeStore {
|
||||
std::atomic<uint64_t> _store;
|
||||
BTS_NO_COPY(SingleWriterPriceSizeStore);
|
||||
public:
|
||||
SingleWriterPriceSizeStore() : _store() {
|
||||
store(PriceSize::NO_MARKET);
|
||||
typedef utils::Optimistic<PriceSize> AtomicPriceSize;
|
||||
|
||||
//
|
||||
// PriceSize has grown to 12 bytes but because of 8 byte alignment it will pad out to 16 bytes
|
||||
// This structure can store the same data in 24 bytes so it's more efficient for shared memory
|
||||
struct PriceLevel {
|
||||
PriceLevel() :
|
||||
_mantissa_b(0), _exponent_b(0),
|
||||
_mantissa_a(0), _exponent_a(0),
|
||||
_size_b(0), _size_a(0) {}
|
||||
PriceLevel(
|
||||
int64_t bid_mantissa,
|
||||
int8_t bid_exponent,
|
||||
uint32_t bid_size,
|
||||
int64_t ask_mantissa,
|
||||
int8_t ask_exponent,
|
||||
uint32_t ask_size) :
|
||||
_mantissa_b(bid_mantissa), _exponent_b(bid_exponent),
|
||||
_mantissa_a(ask_mantissa), _exponent_a(ask_exponent),
|
||||
_size_b(bid_size),
|
||||
_size_a(ask_size)
|
||||
{}
|
||||
PriceLevel(const PriceSize& bid, const PriceSize& ask):
|
||||
_mantissa_b(bid.mantissa()), _exponent_b(bid.exponent()),
|
||||
_mantissa_a(ask.mantissa()), _exponent_a(ask.exponent()),
|
||||
_size_b(bid.size()), _size_a(ask.size())
|
||||
{}
|
||||
|
||||
#ifdef _WIN32
|
||||
int64_t _mantissa_b : 56;
|
||||
int64_t _exponent_b : 8;
|
||||
int64_t _mantissa_a : 56;
|
||||
int64_t _exponent_a : 8;
|
||||
#else
|
||||
int64_t _mantissa_b : 56;
|
||||
int8_t _exponent_b : 8;
|
||||
int64_t _mantissa_a : 56;
|
||||
int8_t _exponent_a : 8;
|
||||
#endif
|
||||
uint32_t _size_b;
|
||||
uint32_t _size_a;
|
||||
|
||||
uint64_t bid_mantissa() const { return _mantissa_b; }
|
||||
uint64_t ask_mantissa() const { return _mantissa_a; }
|
||||
int8_t bid_exponent() const { return _exponent_b; }
|
||||
int8_t ask_exponent() const { return _exponent_a; }
|
||||
uint32_t bid_size() const { return _size_b; }
|
||||
uint32_t ask_size() const { return _size_a; }
|
||||
|
||||
bool operator==(const PriceLevel& other) const {
|
||||
return _size_a == other._size_a &&
|
||||
_size_b == other._size_b &&
|
||||
_mantissa_a == other._mantissa_a &&
|
||||
_mantissa_b == other._mantissa_b &&
|
||||
_exponent_a == other._exponent_a &&
|
||||
_exponent_b == other._exponent_b;
|
||||
}
|
||||
bool store(PriceSize ps) {
|
||||
uint64_t cur = LOAD(_store);
|
||||
if (cur != ps._val64) {
|
||||
STORE(_store, ps._val64);
|
||||
bool operator!=(const PriceLevel& other) const {
|
||||
return _size_a != other._size_a ||
|
||||
_size_b != other._size_b ||
|
||||
_mantissa_a != other._mantissa_a ||
|
||||
_mantissa_b != other._mantissa_b ||
|
||||
_exponent_a != other._exponent_a ||
|
||||
_exponent_b != other._exponent_b;
|
||||
}
|
||||
|
||||
bool update(const PriceLevel& temp) {
|
||||
if (*this != temp) {
|
||||
*this = temp;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
PriceSize load() const {
|
||||
uint64_t val = LOAD(_store);
|
||||
return PriceSize(val);
|
||||
|
||||
PriceSize bid() const {
|
||||
return PriceSize(_mantissa_b, _exponent_b, _size_b);
|
||||
}
|
||||
PriceSize ask() const {
|
||||
return PriceSize(_mantissa_a, _exponent_a, _size_a);
|
||||
}
|
||||
};
|
||||
static_assert(sizeof(PriceLevel) == 24, "PriceLevel should be small");
|
||||
|
||||
|
||||
}}//namespace
|
||||
|
||||
@@ -26,52 +26,94 @@ namespace md {
|
||||
|
||||
using utils::TimeStamp;
|
||||
|
||||
const uint32_t shm_version = 100;
|
||||
|
||||
struct ALIGNED_(CACHE_LINE) HotHeader {
|
||||
size_t count;
|
||||
size_t segment_count;
|
||||
// Just the counters. Publisher process can compute messages/second by polling these counters
|
||||
size_t bbo_count;
|
||||
size_t depth_count;
|
||||
size_t direct_count;
|
||||
size_t implied_count;
|
||||
};
|
||||
|
||||
// These should not be cache line aligned because they are small enough for multiple to fit on one line easily
|
||||
struct ALIGNED_(32) HotEntry {
|
||||
HotEntry() : timestamp(TimeStamp(0)), bid(), ask(), key(invalid_security_key), sequence(0), status(co2::COCONUTSECURITYSTATUS_OK) {}
|
||||
uint32_t size() const { return sizeof(HotEntry); }
|
||||
private:
|
||||
class HotEntry {
|
||||
std::atomic<TimeStamp> timestamp; // 8
|
||||
public:
|
||||
SingleWriterPriceSizeStore bid; // 8
|
||||
SingleWriterPriceSizeStore ask; // 8
|
||||
HotEntry() : timestamp(TimeStamp(0)), book(), key(invalid_security_key), sequence(0), status(co2::COCONUTSECURITYSTATUS_OK) {}
|
||||
uint32_t size() const { return sizeof(HotEntry); }
|
||||
PriceLevel book; // 24
|
||||
security_key key; // 4
|
||||
uint16_t sequence; // 2
|
||||
uint32_t sequence; // 4
|
||||
uint8_t status; // 1
|
||||
void set_time(const TimeStamp& t) { timestamp.store(t, std::memory_order_release); }
|
||||
TimeStamp time() const { return timestamp.load(std::memory_order_acquire); }
|
||||
};
|
||||
static_assert(sizeof(HotEntry) <= CACHE_LINE, "HotEntry should be small");
|
||||
|
||||
const uint32_t COCONUT_MAX_DEPTH = 10;
|
||||
struct ALIGNED_(CACHE_LINE) HotDepthEntry {
|
||||
HotDepthEntry() :
|
||||
timestamp(TimeStamp(0)), key(invalid_security_key), status(co2::COCONUTSECURITYSTATUS_OK), book(), sequence(0)
|
||||
{ }
|
||||
uint32_t size() const { return sizeof(HotDepthEntry); }
|
||||
private:
|
||||
const uint32_t COCONUT_IMPLIED_DEPTH = 2;
|
||||
class HotDepthEntry {
|
||||
std::atomic<TimeStamp> timestamp; // 8
|
||||
public:
|
||||
security_key key; // NOTE: key is at a different offset than it is in HotEntry
|
||||
uint8_t status; // 1
|
||||
// Interleave bid/ask values so the top of book is always on the first line
|
||||
SingleWriterPriceSizeStore book[COCONUT_MAX_DEPTH*2];
|
||||
uint16_t sequence; // 2
|
||||
HotDepthEntry() :
|
||||
timestamp(TimeStamp(0)),
|
||||
key(invalid_security_key),
|
||||
sequence(0),
|
||||
direct(),
|
||||
implied(),
|
||||
status(co2::COCONUTSECURITYSTATUS_OK)
|
||||
{ }
|
||||
uint32_t size() const { return sizeof(HotDepthEntry); }
|
||||
security_key key; // 4 NOTE: key is at a different offset than it is in HotEntry
|
||||
uint32_t sequence; // 4
|
||||
std::array<PriceLevel, COCONUT_MAX_DEPTH> direct; // 240
|
||||
std::array<PriceLevel, COCONUT_IMPLIED_DEPTH> implied; // 48
|
||||
uint8_t status; // 1
|
||||
|
||||
const PriceLevel* at(uint32_t depth) const {
|
||||
if (depth < direct.size()) {
|
||||
return &direct[depth];
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
void read_direct(uint32_t depth, PriceSize* bid, PriceSize* ask) const {
|
||||
if (depth < direct.size()) {
|
||||
*bid = direct[depth].bid();
|
||||
*ask = direct[depth].ask();
|
||||
}
|
||||
}
|
||||
void read_implied(uint32_t depth, PriceSize* bid, PriceSize* ask) const {
|
||||
if (depth < implied.size()) {
|
||||
*bid = implied[depth].bid();
|
||||
*ask = implied[depth].ask();
|
||||
}
|
||||
}
|
||||
bool write_direct(uint32_t depth, const PriceSize& bid, const PriceSize& ask) {
|
||||
if (depth < direct.size()) {
|
||||
auto temp = PriceLevel(bid,ask);
|
||||
if (direct[depth] != temp) {
|
||||
direct[depth] = PriceLevel(bid,ask);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
bool write_implied(uint32_t depth, const PriceSize& bid, const PriceSize& ask) {
|
||||
if (depth < implied.size()) {
|
||||
auto temp = PriceLevel(bid,ask);
|
||||
if (implied[depth] != temp) {
|
||||
implied[depth] = PriceLevel(bid,ask);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
SingleWriterPriceSizeStore& bid(uint32_t depth) { return book[depth*2]; }
|
||||
const SingleWriterPriceSizeStore& bid(uint32_t depth) const { return book[depth*2]; }
|
||||
SingleWriterPriceSizeStore& ask(uint32_t depth) { return book[depth*2+1]; }
|
||||
const SingleWriterPriceSizeStore& ask(uint32_t depth) const { return book[depth*2+1]; }
|
||||
void set_time(const TimeStamp& t) { timestamp.store(t, std::memory_order_release); }
|
||||
TimeStamp time() const { return timestamp.load(std::memory_order_acquire); }
|
||||
};
|
||||
static_assert(sizeof(HotDepthEntry) <= 320, "HotDepthEntry should be small");
|
||||
|
||||
//
|
||||
// COLD Data, brrr
|
||||
@@ -79,6 +121,7 @@ struct ALIGNED_(CACHE_LINE) HotDepthEntry {
|
||||
|
||||
struct ALIGNED_(CACHE_LINE) ColdHeader {
|
||||
ColdHeader() :
|
||||
version(shm_version),
|
||||
count(0),
|
||||
segment_count(0),
|
||||
create_time(TimeStamp::now()),
|
||||
@@ -92,11 +135,10 @@ struct ALIGNED_(CACHE_LINE) ColdHeader {
|
||||
trade_count(0),
|
||||
coconut_status_count(0),
|
||||
exchange_status_count(0),
|
||||
rfq_count(0),
|
||||
customer_bbo_count(0)
|
||||
rfq_count(0)
|
||||
{}
|
||||
|
||||
// uint32_t version;
|
||||
uint32_t version;
|
||||
size_t count;
|
||||
size_t segment_count;
|
||||
TimeStamp create_time;
|
||||
@@ -113,7 +155,6 @@ struct ALIGNED_(CACHE_LINE) ColdHeader {
|
||||
std::atomic<size_t> coconut_status_count;
|
||||
std::atomic<size_t> exchange_status_count;
|
||||
std::atomic<size_t> rfq_count;
|
||||
std::atomic<size_t> customer_bbo_count;
|
||||
std::atomic<size_t> gap_count;
|
||||
|
||||
// Ring buffer for LastTrade for all securities in this list
|
||||
@@ -186,13 +227,9 @@ struct MiscData {
|
||||
struct ColdEntry {
|
||||
ColdEntry(size_t security_size) :
|
||||
exchange_status(co2::TRADINGSTATUS_UNKNOWN),
|
||||
last_trade_updated(),
|
||||
last_trade(),
|
||||
total_volume(0),
|
||||
last_trade_time(),
|
||||
customer_bbo_updated(),
|
||||
customer_bid(),
|
||||
customer_ask(),
|
||||
misc(),
|
||||
deleted(false)
|
||||
{
|
||||
@@ -205,16 +242,10 @@ struct ColdEntry {
|
||||
co2::TradingStatus exchange_status;
|
||||
|
||||
// Last Trade
|
||||
TimeStamp last_trade_updated;
|
||||
SingleWriterPriceSizeStore last_trade;
|
||||
AtomicPriceSize last_trade;
|
||||
size_t total_volume;
|
||||
datetime::LLDateTime last_trade_time;
|
||||
|
||||
// Customer BBO
|
||||
TimeStamp customer_bbo_updated;
|
||||
SingleWriterPriceSizeStore customer_bid;
|
||||
SingleWriterPriceSizeStore customer_ask;
|
||||
|
||||
MiscData misc;
|
||||
|
||||
bool deleted;
|
||||
|
||||
@@ -55,7 +55,7 @@ message MDRequest {
|
||||
LAST_TRADE = 0x4;
|
||||
TRADE_LOG = 0x8;
|
||||
RFQ = 0x10;
|
||||
CUSTOMER_BBO = 0x20;
|
||||
// CUSTOMER_BBO = 0x20;
|
||||
MISC = 0x40;
|
||||
}
|
||||
string host = 2;
|
||||
@@ -65,7 +65,6 @@ message ChannelUpdate {
|
||||
uint32 key = 1;
|
||||
bts.api.Security security = 2;
|
||||
BookMessage book = 3;
|
||||
TopBookMessage customer_bbo = 4;
|
||||
MiscMessage misc = 5;
|
||||
TradeMessage last_trade = 6;
|
||||
}
|
||||
|
||||
@@ -80,7 +80,9 @@ struct MDConsumerGroup {
|
||||
labels.push_back({ "type", SecurityType_Name(symbol.type()).substr(13) /*SECURITYTYPE_*/ });
|
||||
labels.push_back({ "exch", Venue_Name(symbol.exchange()).substr(6) /*VENUE_*/ });
|
||||
stats.publish(
|
||||
std::make_shared<metrics::LazyCounterMetricL>("spark_md_book_updates", [=]() { return std::max(channel.bbo_count(), channel.depth_count()); }, labels));
|
||||
std::make_shared<metrics::LazyCounterMetricL>("spark_md_book_updates", [=]() {
|
||||
return channel.direct_count() + channel.implied_count();
|
||||
}, labels));
|
||||
stats.publish(
|
||||
std::make_shared<metrics::LazyCounterMetricL>("spark_md_security_count", [=]() { return channel.security_count(); }, labels));
|
||||
stats.publish(
|
||||
@@ -281,15 +283,6 @@ public:
|
||||
set_price(price, trade);
|
||||
}
|
||||
}
|
||||
if (HASFLAG(flags, MDRequest::CUSTOMER_BBO)) {
|
||||
TimeStamp time(snapshot ? 0 : chan.time());
|
||||
PriceSize bid, ask;
|
||||
if (handle.get_customer_bbo(time, bid, ask)) {
|
||||
auto* p = pupd->mutable_customer_bbo();
|
||||
convert(bid, p->mutable_bid());
|
||||
convert(ask, p->mutable_ask());
|
||||
}
|
||||
}
|
||||
if (HASFLAG(flags, MDRequest::MISC)) {
|
||||
TimeStamp time(snapshot ? 0 : chan.time());
|
||||
if (handle.misc_timestamp() > time) {
|
||||
@@ -298,7 +291,6 @@ public:
|
||||
}
|
||||
if (pupd->has_book() ||
|
||||
pupd->has_last_trade() ||
|
||||
pupd->has_customer_bbo() ||
|
||||
pupd->has_misc() ||
|
||||
pupd->has_security()) {
|
||||
reply->add_updates()->CopyFrom(*pupd);
|
||||
|
||||
@@ -62,9 +62,6 @@ class RelayProducer : public IRelayWriter {
|
||||
}
|
||||
handle.update_book(updated, bids, asks);
|
||||
}
|
||||
if (update.has_customer_bbo()) {
|
||||
handle.update_customer_bbo(updated, convert(update.customer_bbo().bid()), convert(update.customer_bbo().ask()));
|
||||
}
|
||||
if (update.has_misc()) {
|
||||
auto& misc = update.misc();
|
||||
handle.update_settlement(updated, misc.settlement_price(), datetime::Date(misc.settlement_date()));
|
||||
|
||||
@@ -17,20 +17,28 @@ std::vector<MDProducerHandle> create_test_securities(MDProducer& producer, const
|
||||
|
||||
void assign_ids(std::vector<MDProducerHandle>::iterator begin, std::vector<MDProducerHandle>::iterator end, security_key start_key = 1000);
|
||||
|
||||
extern "C" BTS_EXPORT void co2_get_1234_handle(size_t& size, MDConsumerHandle& handle) {
|
||||
extern "C" BTS_EXPORT void co2_get_1234_handle(size_t& size, MDConsumerHandle& handle, uintptr_t* hotaddr, uintptr_t* coldaddr, uintptr_t* headeraddr) {
|
||||
// Test that marshaling is working
|
||||
void* phot = reinterpret_cast<HotEntry*>(100);
|
||||
ColdEntry* pcold = reinterpret_cast<ColdEntry*>(200);
|
||||
ColdHeader* pheader = reinterpret_cast<ColdHeader*>(300);
|
||||
auto phot = std::make_unique<HotEntry>();
|
||||
auto pcold = std::make_unique<ColdEntry>(0);
|
||||
std::vector<uint8_t> buffer;
|
||||
buffer.resize(1024);
|
||||
auto pheader = reinterpret_cast<ColdHeader*>(&buffer[0]);
|
||||
pheader->has_depth = false;
|
||||
size = sizeof(MDConsumerHandle);
|
||||
handle.init(*pheader, *pcold, phot);
|
||||
handle.init(*pheader, *pcold, phot.get());
|
||||
// Copy the addresses for comparison
|
||||
*hotaddr = (uintptr_t)phot.get();
|
||||
*coldaddr = (uintptr_t)pcold.get();
|
||||
*headeraddr = (uintptr_t)pheader;
|
||||
}
|
||||
|
||||
extern "C" BTS_EXPORT void co2_get_sample_hot(size_t& size, HotEntry& entry) {
|
||||
size = sizeof(HotEntry);
|
||||
entry.key = 1234;
|
||||
entry.bid.store(PriceSize::from_double<2>(7.2, 101));
|
||||
entry.ask.store(PriceSize::from_double<2>(-9.7, 245));
|
||||
entry.book = PriceLevel(
|
||||
PriceSize::from_double<2>(7.2, 101),
|
||||
PriceSize::from_double<2>(-9.7, 245));
|
||||
entry.sequence = 777;
|
||||
entry.status = COCONUTSECURITYSTATUS_PACKET_LOSS;
|
||||
entry.set_time(utils::TimeStamp::now());
|
||||
@@ -40,8 +48,9 @@ extern "C" BTS_EXPORT void co2_get_sample_depth(size_t& size, HotDepthEntry& ent
|
||||
size = sizeof(HotDepthEntry);
|
||||
entry.key = 1234;
|
||||
for (size_t x = 0; x < COCONUT_MAX_DEPTH; ++x) {
|
||||
entry.bid(x).store(PriceSize::from_double<2>(x + 0.5, x + 1));
|
||||
entry.ask(x).store(PriceSize::from_double<2>(x + 0.6, x + 2));
|
||||
entry.write_direct(x,
|
||||
PriceSize::from_double<2>(x + 0.5, x + 1),
|
||||
PriceSize::from_double<2>(x + 0.6, x + 2));
|
||||
}
|
||||
entry.set_time(utils::TimeStamp::now());
|
||||
entry.status = COCONUTSECURITYSTATUS_PACKET_LOSS;
|
||||
@@ -53,14 +62,9 @@ extern "C" BTS_EXPORT void co2_get_sample_cold(size_t& size, ColdEntry& entry) {
|
||||
entry.exchange_status = TRADINGSTATUS_NORMAL;
|
||||
|
||||
// Last trade
|
||||
entry.last_trade_updated = utils::TimeStamp::now();
|
||||
entry.last_trade.store(PriceSize::from_double<2>(4.2, 101));
|
||||
entry.total_volume = 12345;
|
||||
entry.last_trade_time = datetime::ll_now();
|
||||
// Customer bbo
|
||||
entry.customer_bbo_updated = utils::TimeStamp::now();
|
||||
entry.customer_bid.store(PriceSize::from_double<2>(1.1, 101));
|
||||
entry.customer_ask.store(PriceSize::from_double<2>(2.2, 202));
|
||||
// Settlement
|
||||
entry.misc.settlement_price = 3.3;
|
||||
entry.misc.settlement_date = datetime::Date(2015, 9, 3);
|
||||
@@ -93,7 +97,7 @@ extern "C" BTS_EXPORT void co2_get_sample_cold_header(ColdHeader& header) {
|
||||
header.coconut_status_count = 101;
|
||||
header.exchange_status_count = 102;
|
||||
header.rfq_count = 103;
|
||||
header.customer_bbo_count = 104;
|
||||
header.gap_count = 104;
|
||||
|
||||
// Do not write to trade_buffer or rfq_buffer
|
||||
}
|
||||
@@ -110,7 +114,6 @@ extern "C" BTS_EXPORT MDProducer* co2_produce_test_md() {
|
||||
assign_ids(producers.begin(), producers.end());
|
||||
for (auto& p : producers) {
|
||||
p.update_bbo(utils::TimeStamp::now(), PriceSize::from_double<3>(1.1, 101), PriceSize::from_double<3>(2.2,202));
|
||||
p.update_customer_bbo(utils::TimeStamp::now(), PriceSize::from_double<3>(3.3, 303), PriceSize::from_double<3>(4.4, 404));
|
||||
p.add_trade(utils::TimeStamp::now(), PriceSize::from_double<3>(5.5, 505), datetime::ll_from_date(datetime::Date(2015, 9, 22)), AGGRESSOR_SIDE_NONE, 4200);
|
||||
p.update_settlement(utils::TimeStamp::now(), 6.6, datetime::Date(2000, 8, 8));
|
||||
p.update_settlement(utils::TimeStamp::now(), 6.6, datetime::Date(2000, 8, 9));
|
||||
@@ -134,6 +137,10 @@ extern "C" BTS_EXPORT void co2_destroy_test_md(MDProducer* producer) {
|
||||
config::internal::reset();
|
||||
}
|
||||
|
||||
extern "C" BTS_EXPORT void co2_get_zero_and_nan_book(PriceLevel& price) {
|
||||
price = { PriceSize::NO_MARKET, PriceSize::from_double<7>(0,1) };
|
||||
}
|
||||
|
||||
|
||||
}}}//namespace
|
||||
#endif
|
||||
@@ -197,52 +197,66 @@ public:
|
||||
void test_price_size() {
|
||||
// PriceSize violates some type punning rules
|
||||
// So it is important to have this test work correctly on the target platform
|
||||
PriceSize p, p2;
|
||||
p = PriceSize::from_double<2>(4.5, 14);
|
||||
SingleWriterPriceSizeStore memory;
|
||||
memory.store(p);
|
||||
p2 = memory.load();
|
||||
REQUIRE(p.price() == 4.5);
|
||||
REQUIRE(p.size() == 14);
|
||||
REQUIRE(p2.price() == 4.5);
|
||||
REQUIRE(p2.size() == 14);
|
||||
PriceSize p2;
|
||||
auto bid = PriceSize::from_double<2>(4.5, 14);
|
||||
auto ask = PriceSize::from_double<9>(4.5, 14);
|
||||
PriceLevel memory;
|
||||
memory = PriceLevel(bid,ask);
|
||||
REQUIRE(bid.price() == memory.bid().price());
|
||||
REQUIRE(bid.size() == memory.bid().size());
|
||||
REQUIRE(ask.price() == memory.ask().price());
|
||||
REQUIRE(ask.size() == memory.ask().size());
|
||||
for (auto p : { memory.bid(), memory.ask() }) {
|
||||
REQUIRE(p.price() == 4.5);
|
||||
REQUIRE(p.size() == 14);
|
||||
}
|
||||
|
||||
p = PriceSize::from_double<2>(4.0, 5);
|
||||
memory.store(p);
|
||||
p2 = memory.load();
|
||||
REQUIRE(p2.price() == 4.0);
|
||||
REQUIRE(p2.size() == 5);
|
||||
auto p = PriceSize::from_double<2>(4.0, 5);
|
||||
memory = PriceLevel(p,p);
|
||||
for (auto p2 : { memory.bid(), memory.ask() }) {
|
||||
REQUIRE(p2.price() == 4.0);
|
||||
REQUIRE(p2.size() == 5);
|
||||
}
|
||||
|
||||
p = PriceSize::from_double<2>(-4.2, 6);
|
||||
memory.store(p);
|
||||
p2 = memory.load();
|
||||
REQUIRE(p2.price() == -4.2);
|
||||
REQUIRE(p2.size() == 6);
|
||||
memory = PriceLevel(p,p);
|
||||
for (auto p2 : { memory.bid(), memory.ask() }) {
|
||||
REQUIRE(p2.price() == -4.2);
|
||||
REQUIRE(p2.size() == 6);
|
||||
}
|
||||
|
||||
p = PriceSize(4, -1, 2);
|
||||
memory.store(p);
|
||||
p2 = memory.load();
|
||||
REQUIRE(p2.price() == 0.4);
|
||||
REQUIRE(p2.size() == 2);
|
||||
memory = PriceLevel(p,p);
|
||||
for (auto p2 : { memory.bid(), memory.ask() }) {
|
||||
REQUIRE(p2.price() == 0.4);
|
||||
REQUIRE(p2.size() == 2);
|
||||
}
|
||||
|
||||
// Test requirement for more than 32 bit mantissa
|
||||
double bigval = -9999.9921875;
|
||||
p = PriceSize::from_double<7>(bigval, 42);
|
||||
memory.store(p);
|
||||
p2 = memory.load();
|
||||
REQUIRE(p2.price() == bigval);
|
||||
REQUIRE(p2.size() == 42);
|
||||
memory = PriceLevel(p,p);
|
||||
for (auto p2 : { memory.bid(), memory.ask() }) {
|
||||
REQUIRE(p2.price() == bigval);
|
||||
REQUIRE(p2.size() == 42);
|
||||
}
|
||||
}
|
||||
|
||||
void test_price_size_required_precision() {
|
||||
auto min_tick_2017 = 9999.0078125;
|
||||
auto p17 = PriceSize::from_double<7>(min_tick_2017, 2017);
|
||||
REQUIRE(min_tick_2017 == p17.price());
|
||||
REQUIRE(p17.size() == 2017);
|
||||
PriceLevel m17(p17, p17);
|
||||
for (auto p : { m17.bid(), m17.ask() }) {
|
||||
REQUIRE(min_tick_2017 == p.price());
|
||||
REQUIRE(p.size() == 2017);
|
||||
}
|
||||
auto min_tick_2018 = 1999.00390625;
|
||||
auto p18 = PriceSize::from_double<9>(min_tick_2018, 2018);
|
||||
REQUIRE(min_tick_2018 == p18.price());
|
||||
REQUIRE(p18.size() == 2018);
|
||||
auto p18 = PriceSize::from_double<8>(min_tick_2018, 2018);
|
||||
PriceLevel m18(p18, p18);
|
||||
for (auto p : { m18.bid(), m18.ask() }) {
|
||||
REQUIRE(min_tick_2018 == p.price());
|
||||
REQUIRE(p.size() == 2018);
|
||||
}
|
||||
}
|
||||
|
||||
void test_slow_double_conversion() {
|
||||
|
||||
@@ -20,7 +20,7 @@ namespace test {
|
||||
// std::tuple is hard to read in the debugger
|
||||
struct PS {
|
||||
double price;
|
||||
uint16_t size;
|
||||
Size size;
|
||||
bool operator == (const PS& other) const {
|
||||
return price == other.price && size == other.size;
|
||||
}
|
||||
|
||||
@@ -975,6 +975,19 @@ BTS_EXPORT bool is_finite(double d) {
|
||||
#endif
|
||||
}
|
||||
|
||||
int cache_size_l3_bytes() {
|
||||
// note: index1,2 are the L1 and L2 caches
|
||||
std::fstream stream( "/sys/devices/system/cpu/cpu0/cache/index3/size", std::ios_base::in);
|
||||
if (stream) {
|
||||
int sz;
|
||||
stream >> sz;
|
||||
return sz * 1024;
|
||||
}
|
||||
else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Interop functions
|
||||
//
|
||||
|
||||
@@ -85,6 +85,7 @@ inline bool is_nan(float f) { return std::isnan(f); }
|
||||
inline double nan () { return std::numeric_limits<double>::quiet_NaN(); }
|
||||
BTS_EXPORT float fnan ();
|
||||
BTS_EXPORT bool is_finite(double d);
|
||||
int cache_size_l3_bytes();
|
||||
}}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,47 @@
|
||||
#pragma once
|
||||
#include "TimeStamp.h"
|
||||
#include <atomic>
|
||||
#include <tuple>
|
||||
|
||||
namespace bts {
|
||||
namespace utils {
|
||||
|
||||
template <class T>
|
||||
class Optimistic {
|
||||
std::atomic<TimeStamp> _time;
|
||||
T _data;
|
||||
public:
|
||||
Optimistic() : _time(TimeStamp(0)), _data(T()) {}
|
||||
|
||||
void store(const T& t) {
|
||||
store(t, TimeStamp::now());
|
||||
}
|
||||
void store(const T& t, TimeStamp time) {
|
||||
_time.store(TimeStamp::invalid(), std::memory_order_release);
|
||||
_data = t;
|
||||
_time.store(time, std::memory_order_release);
|
||||
}
|
||||
|
||||
std::tuple<TimeStamp,T> load() const {
|
||||
TimeStamp temp;
|
||||
T result;
|
||||
int attempts = 0;
|
||||
const int max_attempts = 1619200;
|
||||
do {
|
||||
++attempts;
|
||||
temp = _time;
|
||||
result = _data;
|
||||
} while((temp != _time || temp == TimeStamp::invalid()) && attempts < max_attempts);
|
||||
if (attempts >= max_attempts) return std::make_tuple(TimeStamp::invalid(), result);
|
||||
return std::make_tuple(temp,result);
|
||||
}
|
||||
bool load_if_new(TimeStamp& time, T& result) const {
|
||||
if (_time > time || _time == TimeStamp::invalid()) {
|
||||
std::tie(time, result) = load();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
}}
|
||||
@@ -8,6 +8,8 @@
|
||||
#include "lib/jetfix/jetfix.h"
|
||||
#include "lib/mercury/broker/CoreEventBroker.h"
|
||||
#include "lib/mercury/mercury_utils/FatWrapper.h"
|
||||
#include "lib/os/os.h"
|
||||
#include "lib/md/PriceSize.h"
|
||||
|
||||
#include <benchmark/benchmark.h>
|
||||
#include <boost/container/flat_map.hpp>
|
||||
|
||||
Reference in new issue
Block a user