Compare commits
153
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0282ad6df2 | ||
|
|
e3c1ea70f2 | ||
|
|
93b93969f5 | ||
|
|
0a10111846 | ||
|
|
eee00cc407 | ||
|
|
0eb58eaba8 | ||
|
|
12f6733d1e | ||
|
|
93a0984ed1 | ||
|
|
b7e9c393a0 | ||
|
|
aa602b3fd6 | ||
|
|
b4b8cabdae | ||
|
|
cfe06869b3 | ||
|
|
b177ed1cc0 | ||
|
|
ff17a64933 | ||
|
|
81a1fe11f6 | ||
|
|
ad5bf88fcf | ||
|
|
e15f155ac9 | ||
|
|
6d6822cd65 | ||
|
|
ace0c70516 | ||
|
|
e9e5059bc2 | ||
|
|
cde72f51a0 | ||
|
|
e1fd49ae32 | ||
|
|
274f3f98e4 | ||
|
|
a4b7d84318 | ||
|
|
be17205094 | ||
|
|
6ef9bee8e4 | ||
|
|
2c8b3563f1 | ||
|
|
71f7e64ccb | ||
|
|
c31b44ecfb | ||
|
|
19efb77e07 | ||
|
|
76de640421 | ||
|
|
fb030742d3 | ||
|
|
73608f1b45 | ||
|
|
58aebe7002 | ||
|
|
299cb0b29e | ||
|
|
06663f711e | ||
|
|
51479f4885 | ||
|
|
5b520ba241 | ||
|
|
f0651a5341 | ||
|
|
897e0990a2 | ||
|
|
30e1ae5c3c | ||
|
|
efe83f81ec | ||
|
|
5fb21ada97 | ||
|
|
58edd07f6b | ||
|
|
9d81354c3a | ||
|
|
0f37be89a4 | ||
|
|
f6586fe636 | ||
|
|
7827ccc0e7 | ||
|
|
af6a2acc0a | ||
|
|
84abf59a55 | ||
|
|
2fc1d30bf1 | ||
|
|
4dcd5d63a1 | ||
|
|
63e5697b20 | ||
|
|
8e5cb100a7 | ||
|
|
938bee1817 | ||
|
|
d48af15480 | ||
|
|
e3b2e98cd9 | ||
|
|
ee53fb854c | ||
|
|
d36e2d61f6 | ||
|
|
bfff3ea5fe | ||
|
|
059c03d289 | ||
|
|
8c561f7886 | ||
|
|
288567d1eb | ||
|
|
ecfc5405fe | ||
|
|
8db34a2a11 | ||
|
|
91094c4d84 | ||
|
|
dae8c85d73 | ||
|
|
abdd98082f | ||
|
|
2f6aa785ab | ||
|
|
7acf1f7043 | ||
|
|
1c3c7a078a | ||
|
|
93ace52bfa | ||
|
|
b5d97c0b1b | ||
|
|
284aa53ad4 | ||
|
|
8a9952f52d | ||
|
|
748aafaab3 | ||
|
|
3ba30c70de | ||
|
|
e72f465999 | ||
|
|
f3d93f7749 | ||
|
|
8d142b8a74 | ||
|
|
8210b9f35f | ||
|
|
12b20b05ce | ||
|
|
72efbccdbd | ||
|
|
33643e9eab | ||
|
|
0f6dc6f40b | ||
|
|
8c778f5e41 | ||
|
|
f308654200 | ||
|
|
bdf5b980fc | ||
|
|
914ac597cd | ||
|
|
4aa6a77680 | ||
|
|
6b59dd84ee | ||
|
|
4e2c5f2d17 | ||
|
|
9a1b94a1b0 | ||
|
|
13b3c55771 | ||
|
|
d0a6fcdb9c | ||
|
|
1eb13028c1 | ||
|
|
424e5ff16b | ||
|
|
bed389885b | ||
|
|
85cf25a57a | ||
|
|
61471d4675 | ||
|
|
d83435d49d | ||
|
|
582fc10dce | ||
|
|
5969985d24 | ||
|
|
fabe7d665c | ||
|
|
7ce75bb8e7 | ||
|
|
b8e1c3cd60 | ||
|
|
4909944694 | ||
|
|
7258af09b4 | ||
|
|
7f2ce6c1a9 | ||
|
|
4835725e1c | ||
|
|
6a9e8de475 | ||
|
|
4639951acc | ||
|
|
d8de211aac | ||
|
|
87394bc60b | ||
|
|
beba30f65c | ||
|
|
2d58424d8f | ||
|
|
d1bbfa3791 | ||
|
|
aab17f460f | ||
|
|
461ef53322 | ||
|
|
2486bc90b2 | ||
|
|
1c5cbaf60b | ||
|
|
b8de447cfb | ||
|
|
8bfc2d40f9 | ||
|
|
c09fd71c94 | ||
|
|
f3b037553e | ||
|
|
d386d93166 | ||
|
|
5217742c71 | ||
|
|
c59ec95460 | ||
|
|
70f01583ec | ||
|
|
66d2f9eb63 | ||
|
|
4625bed8b8 | ||
|
|
f18fb332b5 | ||
|
|
8746a6f5ca | ||
|
|
702d47abb5 | ||
|
|
bf2a54717c | ||
|
|
9db779fb25 | ||
|
|
f55ddec708 | ||
|
|
6e942dd2d4 | ||
|
|
142310a17a | ||
|
|
7f99a89a95 | ||
|
|
bfce663b4b | ||
|
|
33c743643d | ||
|
|
858e0a389b | ||
|
|
9c3bb85674 | ||
|
|
f4930e8cd4 | ||
|
|
f21e7f3602 | ||
|
|
b56c35aca4 | ||
|
|
e6ac83196e | ||
|
|
7b6f74fc4a | ||
|
|
f754953fac | ||
|
|
2dd1143d9f | ||
|
|
e1ee3275d8 | ||
|
|
2549e406f3 |
No files matched your search
@@ -1,15 +1,10 @@
|
||||
{
|
||||
"dockerFile": "Dockerfile",
|
||||
// Creates local dir to mount to HOME in docker
|
||||
"initializeCommand": "mkdir -p ${localEnv:HOME}/.bts/devcontainer-mounts/${devcontainerId}",
|
||||
"hostRequirements": {
|
||||
"memory": "4gb",
|
||||
"storage": "6gb"
|
||||
"memory": "8gb",
|
||||
"storage": "10gb"
|
||||
},
|
||||
"postCreateCommand": "/home/bts/spark/docker/sparkd/dev/devcontainer_post_create.sh",
|
||||
"containerUser": "bts",
|
||||
"workspaceMount": "source=${localWorkspaceFolder},target=/home/bts/spark,type=bind",
|
||||
"workspaceFolder": "/home/bts/spark",
|
||||
"updateRemoteUserUID": true,
|
||||
"shutdownAction": "none",
|
||||
// Note: If running multiple containers on the same host, each must use a
|
||||
@@ -17,7 +12,7 @@
|
||||
// example, if 8000-8003 is taken, go with 9000-9003, then 10000-10003, etc.
|
||||
"runArgs": [
|
||||
"--shm-size",
|
||||
"2gb",
|
||||
"8gb",
|
||||
"-p",
|
||||
"8000:8000",
|
||||
"-p",
|
||||
@@ -29,22 +24,15 @@
|
||||
"-p",
|
||||
"12345:12345"
|
||||
],
|
||||
"mounts": [
|
||||
"source=${localEnv:HOME}/.bts/devcontainer-mounts/${devcontainerId},target=/home/bts,type=bind"
|
||||
],
|
||||
"customizations": {
|
||||
"vscode": {
|
||||
"settings": {
|
||||
"clangd.arguments": [
|
||||
"--compile-commands-dir=${workspaceFolder}/build/clang/debug"
|
||||
]
|
||||
},
|
||||
"extensions": [
|
||||
"eamodio.gitlens",
|
||||
"llvm-vs-code-extensions.vscode-clangd",
|
||||
"ms-vscode.cmake-tools",
|
||||
"ms-vscode.cpptools"
|
||||
"ms-vscode.cpptools",
|
||||
"xaver.clang-format"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
-1
Submodule docker/linux deleted from 56c61231f3.
Submodule docker/windows deleted from de7d40c09d.
+1
-1
Submodule externals updated: 8069cc893f...959b63f2f6.
@@ -8,6 +8,7 @@ using MathNet.Numerics.Providers.FourierTransform;
|
||||
using BCG;
|
||||
using System.Windows.Automation;
|
||||
using bts.collections;
|
||||
using mbts.co2;
|
||||
|
||||
namespace mbts.spark {
|
||||
// Publishes theos for all options (on the side, also publishes the theos for TC2).
|
||||
@@ -114,11 +115,53 @@ namespace mbts.spark {
|
||||
}
|
||||
sym_theos.set_data(k, tp);
|
||||
}
|
||||
_override_theos(ref rv);
|
||||
foreach (var kv in rv) {
|
||||
_bb.post(SparkTags.TC2Tags.LiveTheosTag(kv.Key), kv.Value);
|
||||
}
|
||||
}
|
||||
|
||||
private void _override_theos(ref Dictionary<(string, Venue), ExpStrikeKeyedData<TheoPoint>> theos) {
|
||||
if (_compute_midmarket) {
|
||||
SystemContractData sc_data = _bb.read_state(SparkTags.SystemContractsTag);
|
||||
foreach (var kv in theos) {
|
||||
string sym = kv.Key.Item1;
|
||||
Venue exchange = kv.Key.Item2;
|
||||
foreach (var t in kv.Value.get_data_linearly()) {
|
||||
DateTime exp = t.first;
|
||||
double strike = t.second;
|
||||
TheoPoint tp = t.third;
|
||||
var call_sec_md = sc_data.get_option(exchange, sym, exp, strike, PutOrCall.Call);
|
||||
{
|
||||
NativePriceSize call_bid, call_ask;
|
||||
ulong time = 0;
|
||||
call_sec_md.Value.md.get_bbo(ref time, out call_bid, out call_ask);
|
||||
double call_mid = (call_bid.price + call_ask.price) / 2;
|
||||
tp.call = call_mid;
|
||||
}
|
||||
{
|
||||
var put_sec_md = sc_data.get_option(exchange, sym, exp, strike, PutOrCall.Put);
|
||||
NativePriceSize put_bid, put_ask;
|
||||
ulong time = 0;
|
||||
put_sec_md.Value.md.get_bbo(ref time, out put_bid, out put_ask);
|
||||
double put_mid = (put_bid.price + put_ask.price) / 2;
|
||||
tp.put = put_mid;
|
||||
}
|
||||
}
|
||||
}
|
||||
//replace theos with market bid
|
||||
// get it from the bus and override
|
||||
} else {
|
||||
// do nothing, passes unchanged
|
||||
}
|
||||
}
|
||||
|
||||
private bool _compute_midmarket {
|
||||
get {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void _interp_and_publish() {
|
||||
var timer = Stopwatch.StartNew();
|
||||
|
||||
@@ -281,5 +281,16 @@ namespace mbts.spark {
|
||||
else
|
||||
return tp.put + (tp.pdelta * driver_diff) + (0.5 * tp.pgamma * driver_diff * driver_diff);
|
||||
}
|
||||
|
||||
public static TheoPoint extrapolate(TheoPoint tp, double driver, bool is_call) {
|
||||
TheoPoint rv = tp.Clone() as TheoPoint;
|
||||
double h = driver - tp.driver;
|
||||
if (is_call) {
|
||||
rv.call = tp.call + h * (tp.cdelta);
|
||||
} else {
|
||||
rv.put = tp.put + h * (tp.cdelta);
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -13,7 +13,7 @@ namespace mbts.analytics {
|
||||
// prices (specified in the vs settings).
|
||||
// It has public setters that get
|
||||
// 1. contracts
|
||||
// 2. vs inout
|
||||
// 2. vs input
|
||||
// 3. exp params(drivers, interst rate etc)
|
||||
// 4. driver prices
|
||||
// 5. vol time
|
||||
|
||||
@@ -133,17 +133,31 @@ namespace bts {
|
||||
|
||||
// set 'unit quantity' for each sec
|
||||
foreach (var smd in exch_secs.Values) {
|
||||
var meta = _security_metadata.get(smd.security);
|
||||
if (meta.HasValue) {
|
||||
exch_secs[smd.id] = new(
|
||||
smd.security,
|
||||
smd.md,
|
||||
meta.Value.HasPriceDisplay ? meta.Value.PriceDisplay : null,
|
||||
meta.Value.HasQuantityDisplay ? meta.Value.QuantityDisplay : null);
|
||||
}
|
||||
var (priceDisplay, qtyDisplay) = GetDisplaySettings(smd.security);
|
||||
exch_secs[smd.id] = new(smd.security, smd.md, priceDisplay, qtyDisplay);
|
||||
}
|
||||
_bus.post(SparkTags.ServerSecuritiesTag, exch_secs);
|
||||
}
|
||||
|
||||
(TickDisplaySettingsImmutable? Price, TickDisplaySettingsImmutable? Quantity) GetDisplaySettings(SecurityImmutable sec) {
|
||||
var meta = _security_metadata.get(sec);
|
||||
var priceDisplay = meta?.PriceDisplay ?? Security.Defaults.price_display_settingsi;
|
||||
var qtyDisplay = meta?.QuantityDisplay ?? Security.Defaults.quantity_display_settingsi;
|
||||
if (sec.HasContractSpec) {
|
||||
var pd = priceDisplay.CloneToMutable();
|
||||
pd.TickFactors = TickUtils.get_tick(sec, 0).to_api_tick_factors();
|
||||
priceDisplay = new(pd);
|
||||
if (sec.ContractSpec.QuantityInfos.TryGetFirst(out var qtyInfo)) {
|
||||
var qd = qtyDisplay.CloneToMutable();
|
||||
qd.ShowDecimalPart = qtyInfo.TickBase.is_real_non_zero() && !(qtyInfo.TickUnit / qtyInfo.TickBase).is_int();
|
||||
qd.TickFactors ??= new();
|
||||
qd.TickFactors.TickBase = qtyInfo.TickBase;
|
||||
qd.TickFactors.TickUnit = qtyInfo.TickUnit;
|
||||
qtyDisplay = new(qd);
|
||||
}
|
||||
}
|
||||
return (priceDisplay, qtyDisplay);
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose() =>
|
||||
|
||||
@@ -12,8 +12,13 @@ namespace bts.shell.market_view {
|
||||
class MarketViewFilterStates {
|
||||
[ProtoMember(37)]
|
||||
public Venue[] venue { get; set; }
|
||||
|
||||
[ProtoMember(38)]
|
||||
public string[] currency { get; set; }
|
||||
|
||||
[ProtoMember(1)]
|
||||
public string security { get; set; }
|
||||
|
||||
[ProtoMember(2)]
|
||||
public DoubleMinMaxState bid_price { get; set; }
|
||||
|
||||
@@ -128,6 +133,7 @@ namespace bts.shell.market_view {
|
||||
var pb = new ColumnPredicateBuilder<MarketViewVM.Row>();
|
||||
// security
|
||||
pb.add_discrete_values(venue, r => r.venue);
|
||||
pb.add_discrete_values(currency, r => r.currency);
|
||||
pb.add_regex(security, r => r.security.ToString());
|
||||
pb.add_regex(security_key, r => r.security_key.ToString());
|
||||
|
||||
|
||||
@@ -98,7 +98,14 @@ namespace mbts.spark {
|
||||
_sec_meta_store = sec_meta_store;
|
||||
_settings_hub = settings_hub;
|
||||
security_filter = new RegexFilterVM<Row>(r => r.security.ToString(), nameof(Row.security));
|
||||
currency_filter =new RegexFilterVM<Row>(r => r.currency, nameof(Row.currency));
|
||||
currency_filter = DiscreteValuesFilterVM.Create<Row, string>(
|
||||
bus.subscribe(SparkTags.SecuritiesTag)
|
||||
.Sample(TimeSpan.FromSeconds(1))
|
||||
.Select(sec_mds => sec_mds.Values.Select(smd => smd.security.Currency).Distinct().ToList()),
|
||||
r => r.currency,
|
||||
nameof(Row.currency),
|
||||
EqualityComparer<string>.Default,
|
||||
Comparer<string>.Default);
|
||||
// rows
|
||||
_wrapped_rows = _rows.WrapSelector(r => r.security_md, s => {
|
||||
var r = new Row(_bus, s, _orders_model, _position_model);
|
||||
@@ -124,7 +131,7 @@ namespace mbts.spark {
|
||||
|
||||
add_col(ColKey.Venue, r => r.venue.ToString(), nameof(Row.venue), venue_filter, FilterType.DiscreteValues, ColWidth.Exchange);
|
||||
add_col(ColKey.Security, r => r.security.ToString(), nameof(Row.security), security_filter, FilterType.Regex, ColWidth.Security);
|
||||
add_col(ColKey.Currency, r => r.currency, nameof(Row.currency), currency_filter, FilterType.Regex, ColWidth.Currency);
|
||||
add_col(ColKey.Currency, r => r.currency, nameof(Row.currency), currency_filter, FilterType.DiscreteValues, ColWidth.Currency);
|
||||
add_col(ColKey.Volume, r => r.volume_text, nameof(Row.volume), volume_filter, FilterType.ULongMinMax, ColWidth.Volume);
|
||||
|
||||
// last
|
||||
@@ -391,7 +398,7 @@ namespace mbts.spark {
|
||||
|
||||
public IDataGridColumnPersistentFilter<Row, Venue[]> venue_filter { get; } = DiscreteValuesFilterVM.Common.exchange<Row>(r => r.venue, nameof(Row.venue));
|
||||
public IDataGridColumnPersistentFilter<Row, string> security_filter { get; }
|
||||
public IDataGridColumnPersistentFilter<Row, string> currency_filter { get; }
|
||||
public IDataGridColumnPersistentFilter<Row, string[]> currency_filter { get; }
|
||||
public IDataGridColumnPersistentFilter<Row, DoubleMinMaxState> bid_price_filter { get; } = new DoubleMinMax<Row>(r => r.bid_price, nameof(Row.bid_price));
|
||||
public IDataGridColumnPersistentFilter<Row, DoubleMinMaxState> bid_qty_filter { get; } = new DoubleMinMax<Row>(r => r.bid_qty_display, nameof(Row.bid_qty_display));
|
||||
public IDataGridColumnPersistentFilter<Row, DoubleMinMaxState> dbid_qty_filter { get; } = new DoubleMinMax<Row>(r => r.dbid_qty_display, nameof(Row.dbid_qty_display));
|
||||
@@ -538,6 +545,7 @@ namespace mbts.spark {
|
||||
public MarketViewFilterStates get_filter_states() =>
|
||||
new() {
|
||||
venue = venue_filter.get_state(),
|
||||
currency = currency_filter.get_state(),
|
||||
security = security_filter.get_state(),
|
||||
bid_price = bid_price_filter.get_state(),
|
||||
bid_merged_size = bid_qty_filter.get_state(),
|
||||
@@ -584,6 +592,7 @@ namespace mbts.spark {
|
||||
|
||||
using var _ = rows_view.DeferRefresh();
|
||||
venue_filter.load_state(filters.venue);
|
||||
currency_filter.load_state(filters.currency);
|
||||
security_filter.load_state(filters.security);
|
||||
bid_price_filter.load_state(filters.bid_price);
|
||||
bid_qty_filter.load_state(filters.bid_merged_size);
|
||||
|
||||
@@ -237,6 +237,17 @@ namespace mbts.spark {
|
||||
DefaultColumnWidths.Qty,
|
||||
get_fg: r => r.side == Side.Buy ? BTSColors.MarketData.bid : BTSColors.MarketData.ask);
|
||||
|
||||
add_col(
|
||||
Columns.NotionalValue,
|
||||
DefaultColumnHeaders.NotionalValue,
|
||||
r => _get_row(r).security.spec_or_default().format(r.fill.notional_value()),
|
||||
new DoubleMinMax<FillTuple>(r => r.fill.notional_value()),
|
||||
DataGridFilterKnownTemplates.DoubleMinMax,
|
||||
fs => fs.notional_value,
|
||||
(fs, cf) => fs.notional_value = cf,
|
||||
DefaultColumnWidths.NotionalValue,
|
||||
get_fg: r => r.side == Side.Buy ? BTSColors.MarketData.bid : BTSColors.MarketData.ask);
|
||||
|
||||
add_col(
|
||||
Columns.Price,
|
||||
DefaultColumnHeaders.Price,
|
||||
|
||||
@@ -21,6 +21,7 @@ namespace mbts.ui.spark {
|
||||
Side,
|
||||
Price,
|
||||
Size,
|
||||
NotionalValue,
|
||||
FillKind,
|
||||
FillStatus,
|
||||
FillKey,
|
||||
|
||||
@@ -61,6 +61,10 @@ namespace bts.shell.fills {
|
||||
[DIYGridColumnID(nameof(Columns.Size))]
|
||||
public DoubleMinMaxState size { get; set; }
|
||||
|
||||
[ProtoMember(18)]
|
||||
[DIYGridColumnID(nameof(Columns.NotionalValue))]
|
||||
public DoubleMinMaxState notional_value { get; set; }
|
||||
|
||||
[ProtoMember(11)]
|
||||
[DIYGridColumnID(nameof(Columns.FillKey))]
|
||||
public string fill_key { get; set; }
|
||||
@@ -113,6 +117,7 @@ namespace bts.shell.fills {
|
||||
pb.add_discrete_values(fs.fill_status, ft => ft.fill.FillStatus);
|
||||
// numeric
|
||||
pb.add_numeric(fs.size, ft => ft.fill.LastShares);
|
||||
pb.add_numeric(fs.notional_value, ft => ft.fill.notional_value());
|
||||
pb.add_numeric(fs.price, ft => ft.fill.LastPrice);
|
||||
|
||||
if (fs.exp_date.HasValue) {
|
||||
|
||||
@@ -408,18 +408,18 @@ namespace mbts.spark {
|
||||
|
||||
[ProtoContract]
|
||||
class LadderPanelPricePointSettings : IEquatable<LadderPanelPricePointSettings> {
|
||||
public static LadderPanelPricePointSettings historic_default =>
|
||||
public static LadderPanelPricePointSettings Default =>
|
||||
new LadderPanelPricePointSettings {
|
||||
day_change = new DaySettings {
|
||||
// (neutral)
|
||||
font = _default_font,
|
||||
background_color = Colors.Transparent.to_uint(),
|
||||
font = _default_font.With(font_color: Colors.Black.to_uint()),
|
||||
background_color = Color.FromRgb(113, 201, 69).to_uint(),
|
||||
// negative
|
||||
negative_font_color = Colors.White.to_uint(),
|
||||
negative_background_color = Colors.Red.to_uint(),
|
||||
// positive
|
||||
positive_font_color = Colors.White.to_uint(),
|
||||
positive_background_color = Colors.Green.to_uint()
|
||||
positive_background_color = Colors.Green.to_uint(),
|
||||
},
|
||||
session_low = _default_item.With(_default_item.font.With(font_color: BTSColors.MarketData.session_low)),
|
||||
session_high = _default_item.With(_default_item.font.With(font_color: BTSColors.MarketData.session_high)),
|
||||
@@ -431,21 +431,9 @@ namespace mbts.spark {
|
||||
theo = _default_item,
|
||||
delta = _default_item,
|
||||
vega = _default_item,
|
||||
volume = _default_item
|
||||
volume = _default_item,
|
||||
};
|
||||
|
||||
public static LadderPanelPricePointSettings tt_default {
|
||||
get {
|
||||
var rv = historic_default;
|
||||
rv.day_change.background_color = Color.FromRgb(113, 201, 69).to_uint();
|
||||
rv.day_change.font.font_color = Colors.Black.to_uint();
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
|
||||
public static LadderPanelPricePointSettings Default => LadderSettings.mimic_tt ?
|
||||
tt_default : historic_default;
|
||||
|
||||
[ProtoMember(1)]
|
||||
public DaySettings day_change { get; set; }
|
||||
[ProtoMember(2)]
|
||||
@@ -607,11 +595,6 @@ namespace mbts.spark {
|
||||
uint _ticks_per_row;
|
||||
List<FairValueLine> _fair_value_lines;
|
||||
|
||||
static LadderSettings() {
|
||||
var knobs = config.Configuration.get<bts.config.LadderKnobs>();
|
||||
mimic_tt = knobs.MimicTt;
|
||||
}
|
||||
|
||||
// Default proto constructor
|
||||
// Added this so that when clients upgrade to a version with new settings,
|
||||
// the default values are applied correctly; thx to John for this tip
|
||||
@@ -643,9 +626,7 @@ namespace mbts.spark {
|
||||
status_message_visible = true;
|
||||
}
|
||||
|
||||
public static bool mimic_tt { get; }
|
||||
|
||||
public static LadderSettings historic_default {
|
||||
public static LadderSettings Default {
|
||||
get {
|
||||
LadderColumnSettings
|
||||
orders_column = LadderColumnSettings.default_orders,
|
||||
@@ -713,11 +694,6 @@ namespace mbts.spark {
|
||||
}
|
||||
}
|
||||
|
||||
public static LadderSettings tt_default => historic_default;
|
||||
|
||||
public static LadderSettings Default => mimic_tt ?
|
||||
tt_default : historic_default;
|
||||
|
||||
public enum BadgeStyle { Normal, Alternate }
|
||||
|
||||
// TODO: rename to PriceDisplayFormat (today is merge monday, since this isn't a functional change, it is not worth the potential merge headaches right now)
|
||||
@@ -1039,7 +1015,6 @@ namespace mbts.spark {
|
||||
a.ladder_order_limits.safe_equals(b.ladder_order_limits) &&
|
||||
a._ticks_per_row == b._ticks_per_row &&
|
||||
a.panel_price_point_settings.safe_equals(b.panel_price_point_settings) &&
|
||||
a._ticks_per_row == b._ticks_per_row &&
|
||||
EqualityComparer.safe_sequence_equals(a.fair_value_lines, b.fair_value_lines);
|
||||
|
||||
public bool Equals(LadderSettings other) =>
|
||||
|
||||
@@ -107,6 +107,7 @@ namespace mbts.spark {
|
||||
ImmutableDictionary<AlgoKey, LadderFavoriteAlgoMetadata> _favorite_algo_metadata_map =
|
||||
ImmutableDictionary<AlgoKey, LadderFavoriteAlgoMetadata>.Empty;
|
||||
RegionalTickFactors? _spec;
|
||||
IReadOnlyDictionary<uint, SecurityMD> _securities = new Dictionary<uint, SecurityMD>(0);
|
||||
|
||||
LadderVM(
|
||||
IBus bus,
|
||||
@@ -329,7 +330,8 @@ namespace mbts.spark {
|
||||
}),
|
||||
WorkLoop.Start($"Ladder '{security}'", _tick),
|
||||
ObservableAutoWrapper.dispose_on_removed(orders, true),
|
||||
Disposable.Create(() => _on_disposed_impl?.Invoke(true)));
|
||||
Disposable.Create(() => _on_disposed_impl?.Invoke(true)),
|
||||
bus.subscribe(SparkTags.SecuritiesTag).Subscribe(secs => _securities = secs));
|
||||
|
||||
PropertyAutoWrapper<LadderSettings, bool> bool_prop(
|
||||
string name, Func<LadderSettings, bool> get_bool) =>
|
||||
@@ -405,7 +407,9 @@ namespace mbts.spark {
|
||||
event Action<bool>? _on_disposed_impl;
|
||||
|
||||
public IMDConsumerHandle md => _model.md;
|
||||
public SecurityMD security_md => _model.security_md;
|
||||
public IBus bus => _bus;
|
||||
public IReadOnlyDictionary<uint, SecurityMD> securities => _securities;
|
||||
|
||||
// we return only entries who have piqs and whose piqs are not uint.Max
|
||||
// uint.Max happens when the server is restarted but the client is still up
|
||||
|
||||
@@ -17,7 +17,6 @@ using Spark.Core.Disposables;
|
||||
using Spark.Core.Math;
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Collections.Specialized;
|
||||
@@ -38,9 +37,6 @@ namespace mbts.spark {
|
||||
partial class LadderGridHost : Control {
|
||||
static readonly int _initial_row_count;
|
||||
static readonly bool _show_scrollbar;
|
||||
static readonly bool _lighten_market_cells;
|
||||
static readonly Color? _bid_market_cell_background;
|
||||
static readonly Color? _ask_market_cell_background;
|
||||
|
||||
readonly VisualHost _visual_host = new();
|
||||
readonly LadderOrdersHost _ordershost;
|
||||
@@ -84,6 +80,7 @@ namespace mbts.spark {
|
||||
LadderTactileAdorner _tactile_adorner;
|
||||
ulong _prices_updated;
|
||||
NativePriceSize[] _bids = new NativePriceSize[NativePriceHelpers.COCONUT_MAX_DEPTH], _asks = new NativePriceSize[NativePriceHelpers.COCONUT_MAX_DEPTH];
|
||||
SecurityMDExtensions.Book _depth = SecurityMDExtensions.Book.Empty;
|
||||
bool _price_dirty = false, _piq_dirty = false;
|
||||
LadderPriceList _price_list = new() {
|
||||
length = _initial_row_count,
|
||||
@@ -96,6 +93,8 @@ namespace mbts.spark {
|
||||
Func<NativePriceSize, string> _fmt_mkt_depth_item_impl = MDDisplayHelper.get_formatter_nps(
|
||||
MarketSizeDisplay.get_default(), Security.Defaults.quantity_display_settingsi, Security.Defaults.price_display_settingsi);
|
||||
Func<double, double, string> _fmt_mkt_quantity_impl = (v, p) => v.ToString();
|
||||
Func<LadderPriceLevel, string> _fmt_price_level_impl = MDDisplayHelper.get_formatter_lpl(
|
||||
MarketSizeDisplay.get_default(), Security.Defaults.quantity_display_settingsi, Security.Defaults.price_display_settingsi);
|
||||
int _old_tooltip_row = int.MaxValue;
|
||||
NativePriceSize? _old_last_trade;
|
||||
string _direction_arrow = string.Empty;
|
||||
@@ -107,17 +106,7 @@ namespace mbts.spark {
|
||||
static LadderGridHost() {
|
||||
var knobs = config.Configuration.get<bts.config.LadderKnobs>();
|
||||
_show_scrollbar = knobs.ShowScrollbar;
|
||||
_lighten_market_cells = knobs.LightenMarketCells;
|
||||
_initial_row_count = (int)knobs.ScrollableTickWindowSize;
|
||||
// For market cell backgrounds, 0 means null. Note that the full
|
||||
// range of colors can still be expressed, as zero is merely one of
|
||||
// 2^24 possible representations of 'transparent.'
|
||||
if (knobs.BidMarketCellBackground != 0) {
|
||||
_bid_market_cell_background = BTSColors.Ladder.bid_column_background;
|
||||
}
|
||||
if (knobs.AskMarketCellBackground != 0) {
|
||||
_ask_market_cell_background = BTSColors.Ladder.ask_column_background;
|
||||
}
|
||||
}
|
||||
|
||||
public LadderGridHost() {
|
||||
@@ -872,13 +861,13 @@ namespace mbts.spark {
|
||||
switch (column.column_type) {
|
||||
case ColumnType.Order: _ordershost.set_col_width(column.settings.width); break;
|
||||
case ColumnType.Bid:
|
||||
render_market_col(_bids, _bid_market_cell_background);
|
||||
render_market_col(_depth.Bids);
|
||||
break;
|
||||
case ColumnType.Price:
|
||||
_refresh_prices(column);
|
||||
break;
|
||||
case ColumnType.Ask:
|
||||
render_market_col(_asks, _ask_market_cell_background);
|
||||
render_market_col(_depth.Asks);
|
||||
break;
|
||||
case ColumnType.Volume:
|
||||
_volume_container.mark_dirty();
|
||||
@@ -891,9 +880,9 @@ namespace mbts.spark {
|
||||
if (_mkt_break_lines.high_render_index.HasValue)
|
||||
_render_session_line(_session_high_vis, _mkt_break_lines.high_render_index.Value, _settings.session_high_line.color);
|
||||
}
|
||||
void render_market_col(NativePriceSize[] depth, Color? market_cell_background) {
|
||||
void render_market_col(IEnumerable<(double Price, double Volume, double NotionalValue)> depth) {
|
||||
var mid = LadderRender.index_of(_price_list, _last_origin_price, _price_comparer);
|
||||
LadderRender.render_sizes(column.visual_tup.visual, column.visual_tup.glyph_context, mid, _price_list, depth, _settings.row_height, _spec, column.settings, _fmt_mkt_depth_item, _lighten_market_cells, market_cell_background);
|
||||
LadderRender.render_sizes(column.visual_tup.visual, column.visual_tup.glyph_context, mid, _price_list, depth, _settings.row_height, _spec, column.settings, _fmt_price_level);
|
||||
}
|
||||
}
|
||||
_apply_column_visual_offset();
|
||||
@@ -939,6 +928,7 @@ namespace mbts.spark {
|
||||
// **use local vm to protect against re-assignment,
|
||||
// so the events are unhooked from the same instance to which they were added
|
||||
// when the subs are disposed.
|
||||
Disposable.dispose(ref _vm_sub);
|
||||
var local_vm = _vm;
|
||||
local_vm.on_tick += _tick;
|
||||
local_vm.request_recenter += on_request_recenter;
|
||||
@@ -1110,6 +1100,7 @@ namespace mbts.spark {
|
||||
ulong unused = 0;
|
||||
bool last_trade_changed = _vm.md.get_last_trade(ref unused, out _last_trade, out DateTime _, out ulong __);
|
||||
bool depth_changed = _vm.md.get_depth(ref _prices_updated, ref _bids, ref _asks, NativePriceHelpers.COCONUT_MAX_DEPTH);
|
||||
_depth = _vm.security_md.Depth(_bids, _asks, _vm.securities);
|
||||
_mkt_break_lines.refresh();
|
||||
if (_price_list is null) {
|
||||
Log.assert(_price_dirty);
|
||||
@@ -1322,8 +1313,8 @@ namespace mbts.spark {
|
||||
void _refresh_sizes() {
|
||||
Log.assert_d(_price_list != null);
|
||||
int origin_index = LadderRender.index_of(_price_list, _mkt_break_lines.origin, _price_comparer);
|
||||
LadderRender.render_sizes(_bid_vis_tup.visual, _bid_vis_tup.glyph_context, origin_index, _price_list, _bids, _settings.row_height, _spec, _settings.bid_column, _fmt_mkt_depth_item, _lighten_market_cells, _bid_market_cell_background);
|
||||
LadderRender.render_sizes(_ask_vis_tup.visual, _ask_vis_tup.glyph_context, origin_index, _price_list, _asks, _settings.row_height, _spec, _settings.ask_column, _fmt_mkt_depth_item, _lighten_market_cells, _ask_market_cell_background);
|
||||
LadderRender.render_sizes(_bid_vis_tup.visual, _bid_vis_tup.glyph_context, origin_index, _price_list, _depth.Bids, _settings.row_height, _spec, _settings.bid_column, _fmt_price_level);
|
||||
LadderRender.render_sizes(_ask_vis_tup.visual, _ask_vis_tup.glyph_context, origin_index, _price_list, _depth.Asks, _settings.row_height, _spec, _settings.ask_column, _fmt_price_level);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -1506,11 +1497,13 @@ namespace mbts.spark {
|
||||
static GlyphContext glyph_context(LadderColumnSettings cs) =>
|
||||
GlyphContext.create(cs.font_family, cs.font_weight);
|
||||
|
||||
var price_display = _spec.tick_factors(0).to_api_tick_display_settingsi();
|
||||
var price_display = security.regional_ticks().tick_factors(0).to_api_tick_display_settingsi();
|
||||
_fmt_mkt_depth_item_impl = MDDisplayHelper.get_formatter_nps(
|
||||
_settings.size_display, _qty_display, price_display);
|
||||
_fmt_mkt_quantity_impl = MDDisplayHelper.get_value_formatter(
|
||||
_settings.size_display, _qty_display, price_display);
|
||||
_fmt_price_level_impl = MDDisplayHelper.get_formatter_lpl(
|
||||
_settings.size_display, _qty_display, price_display);
|
||||
_set_columns();
|
||||
_apply_column_visual_offset();
|
||||
if (prev_row_height != _settings.row_height)
|
||||
@@ -1520,18 +1513,16 @@ namespace mbts.spark {
|
||||
_mkt_break_lines.fair_value_line_views.Clear();
|
||||
// Clear and re-add the columns to the viewer to ensure offset/visibility is correct
|
||||
_visual_host.Clear();
|
||||
if (LadderSettings.mimic_tt) {
|
||||
// The session_range_vis sits beneath the grid backgrounds (on the
|
||||
// z-axis) to avoid obfuscating the font color (dj wants it to be
|
||||
// pure white). Historically, it sat atop the price column, with a
|
||||
// low opacity to slightly darken the range of prices inside of the
|
||||
// session range (i.e., the price-band of the day's trades). In
|
||||
// conjuction with this change, the price column's background
|
||||
// opacity is made slightly less than 1 (in the default settings),
|
||||
// while the font color remains fully opaque, so this layer will
|
||||
// darken the background without affecting the font color.
|
||||
_visual_host.AddVisualChild(_session_range_vis);
|
||||
}
|
||||
// The session_range_vis sits beneath the grid backgrounds (on the
|
||||
// z-axis) to avoid obfuscating the font color (dj wants it to be
|
||||
// pure white). Historically, it sat atop the price column, with a
|
||||
// low opacity to slightly darken the range of prices inside of the
|
||||
// session range (i.e., the price-band of the day's trades). In
|
||||
// conjuction with this change, the price column's background
|
||||
// opacity is made slightly less than 1 (in the default settings),
|
||||
// while the font color remains fully opaque, so this layer will
|
||||
// darken the background without affecting the font color.
|
||||
_visual_host.AddVisualChild(_session_range_vis);
|
||||
// Renders the grid lines and column backgrounds
|
||||
_visual_host.AddVisualChild(_gridvis);
|
||||
// Orders column
|
||||
@@ -1571,8 +1562,6 @@ namespace mbts.spark {
|
||||
with_vis => _visual_host.RemoveVisualChild(with_vis.vis));
|
||||
_visual_host.AddVisualChild(_session_low_vis);
|
||||
_visual_host.AddVisualChild(_session_high_vis);
|
||||
if (!LadderSettings.mimic_tt)
|
||||
_visual_host.AddVisualChild(_session_range_vis);
|
||||
_render_grid_background();
|
||||
_init_tactile_adorner();
|
||||
// this effectively sets the background of the "Last" column //deleted to fix color
|
||||
@@ -1594,6 +1583,16 @@ namespace mbts.spark {
|
||||
return impl(value);
|
||||
}
|
||||
|
||||
// This is designed to be a redundant null check for _fmt_mkt_depth_item_impl.
|
||||
string _fmt_price_level(LadderPriceLevel value) {
|
||||
// Assign the impl delegate to a local variable, just in case it is reassigned on another thread.
|
||||
// This shouldn't happen, but better to avoid the potential null ref just in case it does. Expect the unexpected.
|
||||
var impl = _fmt_price_level_impl;
|
||||
impl ??= MDDisplayHelper.get_formatter_lpl(
|
||||
MarketSizeDisplay.get_default(), _qty_display, security.regional_ticks().tick_factors(0).to_api_tick_display_settingsi());
|
||||
return impl(value);
|
||||
}
|
||||
|
||||
// This is designed to be a redundant null check for _fmt_mkt_quantity_impl.
|
||||
string _fmt_mkt_quantity(double value, double price) {
|
||||
// Assign the impl delegate to a local variable, just in case it is reassigned on another thread.
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
namespace mbts.spark;
|
||||
|
||||
using bts;
|
||||
using bts.utils;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
|
||||
readonly struct LadderPriceLevel : IEquatable<LadderPriceLevel> {
|
||||
public LadderPriceLevel(double price = double.NaN, double volume = 0, double notionalValue = double.NaN) {
|
||||
Price = price;
|
||||
Volume = volume;
|
||||
NotionalValue = notionalValue.is_nan() ? volume * price : notionalValue;
|
||||
}
|
||||
|
||||
public static LadderPriceLevel Empty => new();
|
||||
|
||||
public double Price { get; }
|
||||
public double Volume { get; }
|
||||
public double NotionalValue { get; }
|
||||
|
||||
public LadderPriceLevel Add(double price, double volume) {
|
||||
if (!(volume > 0)) {
|
||||
return this;
|
||||
}
|
||||
if (!(Volume > 0)) {
|
||||
return new(price, volume);
|
||||
}
|
||||
Log.assert(Price.is_real());
|
||||
Log.assert(price.is_real());
|
||||
var newVolume = Volume + volume;
|
||||
var newPrice = (Price * Volume + price * volume) / newVolume;
|
||||
return new(newPrice, newVolume);
|
||||
}
|
||||
|
||||
public LadderPriceLevel Add(IEnumerable<(double Price, double Volume)> items) {
|
||||
var (totalPrice, totalVolume) = items.Sum();
|
||||
return totalVolume > 0
|
||||
? Add(totalPrice / totalVolume, totalVolume)
|
||||
: this;
|
||||
}
|
||||
|
||||
public Builder ToBuilder() =>
|
||||
new() { Cost = Price, Volume = Volume };
|
||||
|
||||
public LadderPriceLevel With(double price = double.NaN, double volume = double.NaN, double notionalValue = double.NaN) =>
|
||||
new(price.if_nan(Price), volume.if_nan(Volume), notionalValue.if_nan(NotionalValue));
|
||||
|
||||
public bool Equals(LadderPriceLevel other) =>
|
||||
MathUtils.epsilon_eq_comparer.Equals(Price, other.Price)
|
||||
&& MathUtils.epsilon_eq_comparer.Equals(Volume, other.Volume);
|
||||
|
||||
public override bool Equals([NotNullWhen(true)] object obj) =>
|
||||
obj is LadderPriceLevel other && Equals(other);
|
||||
|
||||
public override int GetHashCode() =>
|
||||
HashCode.Combine(
|
||||
MathUtils.epsilon_eq_comparer.GetHashCode(Price),
|
||||
MathUtils.epsilon_eq_comparer.GetHashCode(Volume));
|
||||
|
||||
public class Builder {
|
||||
double notionalValue = double.NaN;
|
||||
|
||||
public double Cost { get; set; } = double.NaN;
|
||||
public double Volume { get; set; } = 0;
|
||||
public double NotionalValue => notionalValue.is_nan() ? Volume * Cost : notionalValue;
|
||||
|
||||
public void Add(double price, double volume) {
|
||||
if (!(volume > 0)) {
|
||||
return;
|
||||
}
|
||||
Log.assert(price.is_real());
|
||||
Cost += price * volume;
|
||||
Volume += volume;
|
||||
}
|
||||
|
||||
public void Add(double price, double volume, double notionalValue) {
|
||||
if (!(volume > 0)) {
|
||||
return;
|
||||
}
|
||||
Log.assert(price.is_real());
|
||||
Cost += price * volume;
|
||||
Volume += volume;
|
||||
if (this.notionalValue.is_nan()) {
|
||||
this.notionalValue = notionalValue;
|
||||
} else{
|
||||
this.notionalValue += notionalValue;
|
||||
}
|
||||
}
|
||||
|
||||
public LadderPriceLevel ToImmutable() =>
|
||||
Volume > 0
|
||||
? new(Cost / Volume, Volume, NotionalValue)
|
||||
: Empty;
|
||||
}
|
||||
}
|
||||
@@ -57,8 +57,7 @@ namespace mbts.spark {
|
||||
static class LadderRender {
|
||||
// Note: the orders-column has a separate "row_padding" value: Badge.DefaultMargin
|
||||
// (see how it is incorporated in measure_row_height() below)
|
||||
public static Thickness row_padding => LadderSettings.mimic_tt ?
|
||||
new Thickness(4, 2, 4, 2) : new Thickness(6);
|
||||
public static Thickness row_padding => new Thickness(4, 2, 4, 2);
|
||||
|
||||
const string tick_separator_period = ".";
|
||||
const string tick_separator_dash = "-";
|
||||
@@ -214,70 +213,78 @@ namespace mbts.spark {
|
||||
}
|
||||
}
|
||||
|
||||
public static void RenderSizes(
|
||||
DrawingVisual dv,
|
||||
GlyphContext glyphContext,
|
||||
int originIndex,
|
||||
IList<double> prices,
|
||||
IEnumerable<LadderPriceLevel> levels,
|
||||
double rowHeight,
|
||||
RegionalTickFactors spec,
|
||||
LadderColumnSettings settings,
|
||||
Func<LadderPriceLevel, string> fmt) {
|
||||
var bg = settings.background.rgb_color().blend((BTSColor)0x2AFFFFFF).to_brush();
|
||||
using (var dc = dv.RenderOpen())
|
||||
using (_draw_with_clip(dc, settings, rowHeight, prices.Count))
|
||||
using (var levelsEn = (levels ?? Enumerable.Empty<LadderPriceLevel>()).GetEnumerator()) {
|
||||
var fg = settings.foreground.rgb_brush();
|
||||
var currIdx = originIndex;
|
||||
var currPrice = prices[currIdx];
|
||||
while (levelsEn.MoveNext()) {
|
||||
var level = levelsEn.Current;
|
||||
var ticks = spec.count_ticks(level.Price);
|
||||
var price = ticks.HasValue ? spec.tickup(0, ticks.Value) : level.Price;
|
||||
// prices are in descending order, so get diff from curr_price - price
|
||||
var tickDiff = spec.tickdiff(currPrice, price).assert_value();
|
||||
if (!MathUtils.try_increment(ref currIdx, tickDiff) || currIdx < 0 || currIdx >= prices.Count) {
|
||||
continue;
|
||||
}
|
||||
currPrice = price;
|
||||
if (bg is not null) {
|
||||
var y = currIdx * rowHeight + GRID_LINE_HEIGHT;
|
||||
var height = rowHeight - GRID_LINE_HEIGHT;
|
||||
dc.DrawRectangle(bg, null, new Rect(0, y, settings.width, height));
|
||||
}
|
||||
var gr = format_text(fmt(level), currIdx, rowHeight, settings, glyphContext);
|
||||
dc.DrawGlyphRun(fg, gr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static IEnumerable<LadderPriceLevel> GetLevels(IEnumerable<(double Price, double Volume, double NotionalValue)> depth, RegionalTickFactors spec) =>
|
||||
depth.TakeWhile(nps => nps.Volume > 0 && nps.Price.is_real())
|
||||
.AggregateConsecutiveGroups(
|
||||
LadderPriceLevel.Empty.ToBuilder,
|
||||
(bd, rung) => {
|
||||
bd.Add(rung.Price, rung.Volume, rung.NotionalValue);
|
||||
return bd;
|
||||
},
|
||||
rung => {
|
||||
var ticks = spec.count_ticks(rung.Price);
|
||||
return ticks.HasValue ? spec.tickup(0, ticks.Value) : rung.Price;
|
||||
})
|
||||
.Select(bd => bd.ToImmutable());
|
||||
|
||||
public static void render_sizes(
|
||||
DrawingVisual dv,
|
||||
GlyphContext glyph_context,
|
||||
int origin_index,
|
||||
IList<double> prices,
|
||||
NativePriceSize[] rungs,
|
||||
IEnumerable<(double Price, double Volume, double NotionalValue)> rungs,
|
||||
double row_height,
|
||||
RegionalTickFactors spec,
|
||||
LadderColumnSettings settings,
|
||||
Func<NativePriceSize, string> fmt,
|
||||
bool lighten_market_cells,
|
||||
Color? market_cell_background) {
|
||||
//var background = new SolidColorBrush(settings.background.rgb_color());
|
||||
Brush market_cell_background_brush;
|
||||
if (market_cell_background.HasValue) {
|
||||
market_cell_background_brush = new SolidColorBrush(market_cell_background.Value);
|
||||
} else if (lighten_market_cells) {
|
||||
market_cell_background_brush = settings.background.rgb_color()
|
||||
.blend((BTSColor)0x2AFFFFFF)
|
||||
.to_brush();
|
||||
} else {
|
||||
market_cell_background_brush = null;
|
||||
}
|
||||
// market_cell_background_brush = settings.background.rgb_color().blend((BTSColor)0xFF000000).to_brush();
|
||||
using (var dc = dv.RenderOpen())
|
||||
using (_draw_with_clip(dc, settings, row_height, prices.Count)) {
|
||||
//dc.DrawRectangle(background, null, new Rect(0, 0, settings.width, prices.Count * row_height));
|
||||
if (rungs.is_null_or_empty()) {
|
||||
return;
|
||||
}
|
||||
var foreground = settings.foreground.rgb_brush();
|
||||
int curr_index = origin_index;
|
||||
double curr_price = prices[curr_index];
|
||||
NativePriceSize curr_size = NativePriceSize.NO_MARKET;
|
||||
var row_tick_groups = rungs
|
||||
.TakeWhile(nps => !nps.is_nan)
|
||||
.AggregateConsecutiveGroups(() => NativePriceSize.NO_MARKET,
|
||||
(aggr, rung) => rung.with(
|
||||
direct_size: aggr.direct_size + rung.direct_size,
|
||||
implied_size: aggr.implied_size + rung.implied_size),
|
||||
rung => {
|
||||
var ticks = spec.count_ticks(rung.price);
|
||||
return ticks.HasValue ? spec.tickup(0, ticks.Value) : rung.price;
|
||||
});
|
||||
foreach (var rung in row_tick_groups) {
|
||||
//double price = spec.round_to_tick(rung.price);
|
||||
var ticks = spec.count_ticks(rung.price);
|
||||
var price = ticks.HasValue ? spec.tickup(0, ticks.Value) : rung.price;
|
||||
// prices are in descending order, so get diff from curr_price - price
|
||||
var tick_diff = spec.tickdiff(curr_price, price).assert_value();
|
||||
if (!MathUtils.try_increment(ref curr_index, tick_diff)) {
|
||||
continue;
|
||||
}
|
||||
curr_price = price;
|
||||
if (curr_index >= 0 && curr_index < prices.Count) {
|
||||
if (market_cell_background_brush != null)
|
||||
dc.DrawRectangle(market_cell_background_brush, null,
|
||||
new Rect(0, curr_index * row_height + GRID_LINE_HEIGHT,
|
||||
settings.width, row_height - GRID_LINE_HEIGHT));
|
||||
var gr = format_text(fmt(rung), curr_index, row_height, settings, glyph_context);
|
||||
dc.DrawGlyphRun(foreground, gr);
|
||||
} else break;
|
||||
}
|
||||
}
|
||||
Func<LadderPriceLevel, string> fmt) {
|
||||
RenderSizes(
|
||||
dv,
|
||||
glyph_context,
|
||||
origin_index,
|
||||
prices,
|
||||
GetLevels(rungs, spec),
|
||||
row_height,
|
||||
spec,
|
||||
settings,
|
||||
fmt);
|
||||
}
|
||||
|
||||
public static void render_volumes(
|
||||
|
||||
@@ -182,7 +182,7 @@
|
||||
</Style>
|
||||
|
||||
<Style x:Key="CancelAll" TargetType="Button" BasedOn="{StaticResource LadderMenuButton}">
|
||||
<Setter Property="Grid.Row" Value="2" />
|
||||
<Setter Property="Grid.Row" Value="0"/>
|
||||
<Setter Property="Content">
|
||||
<Setter.Value>
|
||||
<TextBlock VerticalAlignment="Center">Cancel All</TextBlock>
|
||||
@@ -220,10 +220,7 @@
|
||||
</Setter.Value>
|
||||
</Setter>
|
||||
</Style>
|
||||
<Style x:Key="TT_CancelAll" TargetType="Button" BasedOn="{StaticResource CancelAll}">
|
||||
<Setter Property="Grid.Row" Value="0"/>
|
||||
</Style>
|
||||
<Style x:Key="TT_CancelBuys" TargetType="Button" BasedOn="{StaticResource CancelAll}">
|
||||
<Style x:Key="CancelBuys" TargetType="Button" BasedOn="{StaticResource CancelAll}">
|
||||
<Setter Property="Grid.Row" Value="1" />
|
||||
<Setter Property="Content">
|
||||
<Setter.Value>
|
||||
@@ -231,7 +228,7 @@
|
||||
</Setter.Value>
|
||||
</Setter>
|
||||
</Style>
|
||||
<Style x:Key="TT_CancelSells" TargetType="Button" BasedOn="{StaticResource CancelAll}">
|
||||
<Style x:Key="CancelSells" TargetType="Button" BasedOn="{StaticResource CancelAll}">
|
||||
<Setter Property="Grid.Row" Value="2" />
|
||||
<Setter Property="Content">
|
||||
<Setter.Value>
|
||||
@@ -239,83 +236,6 @@
|
||||
</Setter.Value>
|
||||
</Setter>
|
||||
</Style>
|
||||
<Style x:Key="CancelBuys" TargetType="Button" BasedOn="{StaticResource LadderMenuButton}">
|
||||
<Setter Property="Grid.Row" Value="0" />
|
||||
<Setter Property="Content">
|
||||
<Setter.Value>
|
||||
<TextBlock VerticalAlignment="Center" Foreground="{Binding bid_foreground}">
|
||||
Cancel Buys
|
||||
</TextBlock>
|
||||
</Setter.Value>
|
||||
</Setter>
|
||||
<Setter Property="Template">
|
||||
<Setter.Value>
|
||||
<ControlTemplate TargetType="Button">
|
||||
<Border
|
||||
x:Name="Border"
|
||||
BorderThickness="{TemplateBinding BorderThickness}"
|
||||
BorderBrush="{u:BTSColor UI.Button.normal_border}"
|
||||
Background="{Binding bid_background}"
|
||||
Padding="{TemplateBinding Padding}"
|
||||
SnapsToDevicePixels="True">
|
||||
<ContentPresenter
|
||||
HorizontalAlignment="Center"
|
||||
VerticalAlignment="Center"
|
||||
RecognizesAccessKey="True"
|
||||
Name="button_content" />
|
||||
</Border>
|
||||
<ControlTemplate.Triggers>
|
||||
<Trigger Property="IsMouseOver" Value="True">
|
||||
<Setter TargetName="Border" Property="Background" Value="#99167ed3" />
|
||||
<Setter
|
||||
TargetName="Border"
|
||||
Property="BorderBrush"
|
||||
Value="{u:BTSColor UI.Button.mouseover_border}" />
|
||||
</Trigger>
|
||||
</ControlTemplate.Triggers>
|
||||
</ControlTemplate>
|
||||
</Setter.Value>
|
||||
</Setter>
|
||||
</Style>
|
||||
<Style x:Key="CancelSells" TargetType="Button" BasedOn="{StaticResource LadderMenuButton}">
|
||||
<Setter Property="Background" Value="{u:BTSColor MarketData.ask}" />
|
||||
<Setter Property="Grid.Row" Value="1" />
|
||||
<Setter Property="Content">
|
||||
<Setter.Value>
|
||||
<TextBlock VerticalAlignment="Center" Foreground="{Binding ask_foreground}">
|
||||
Cancel Sells
|
||||
</TextBlock>
|
||||
</Setter.Value>
|
||||
</Setter>
|
||||
<Setter Property="Template">
|
||||
<Setter.Value>
|
||||
<ControlTemplate TargetType="Button">
|
||||
<Border
|
||||
x:Name="Border"
|
||||
BorderThickness="{TemplateBinding BorderThickness}"
|
||||
BorderBrush="{u:BTSColor UI.Button.normal_border}"
|
||||
Background="{Binding ask_background}"
|
||||
Padding="{TemplateBinding Padding}"
|
||||
SnapsToDevicePixels="True" >
|
||||
<ContentPresenter
|
||||
HorizontalAlignment="Center"
|
||||
VerticalAlignment="Center"
|
||||
RecognizesAccessKey="True"
|
||||
Name="button_content" />
|
||||
</Border>
|
||||
<ControlTemplate.Triggers>
|
||||
<Trigger Property="IsMouseOver" Value="True">
|
||||
<Setter TargetName="Border" Property="Background" Value="#99c00000" />
|
||||
<Setter
|
||||
TargetName="Border"
|
||||
Property="BorderBrush"
|
||||
Value="{u:BTSColor UI.Button.mouseover_border}" />
|
||||
</Trigger>
|
||||
</ControlTemplate.Triggers>
|
||||
</ControlTemplate>
|
||||
</Setter.Value>
|
||||
</Setter>
|
||||
</Style>
|
||||
<Style x:Key="SettingsButton" TargetType="Button" BasedOn="{StaticResource LadderMenuButton}">
|
||||
<Setter Property="Content">
|
||||
<Setter.Value>
|
||||
@@ -1177,19 +1097,19 @@
|
||||
<Button
|
||||
Command="{Binding cancel_cmd}"
|
||||
CommandParameter="Bids"
|
||||
Style="{StaticResource {x:Static sp:LadderView.CancelBuysButtonStyleKey}}"
|
||||
Style="{StaticResource CancelBuys}"
|
||||
Visibility="{Binding
|
||||
show_cancel_buys, Converter={StaticResource BooleanToVisibilityConverter}}" />
|
||||
<Button
|
||||
Command="{Binding cancel_cmd}"
|
||||
CommandParameter="Asks"
|
||||
Style="{StaticResource {x:Static sp:LadderView.CancelSellsButtonStyleKey}}"
|
||||
Style="{StaticResource CancelSells}"
|
||||
Visibility="{Binding
|
||||
show_cancel_sells, Converter={StaticResource BooleanToVisibilityConverter}}" />
|
||||
<Button
|
||||
Command="{Binding cancel_cmd}"
|
||||
CommandParameter="All"
|
||||
Style="{StaticResource {x:Static sp:LadderView.CancelAllButtonStyleKey}}"
|
||||
Style="{StaticResource CancelAll}"
|
||||
Visibility="{Binding
|
||||
show_cancel_all, Converter={StaticResource BooleanToVisibilityConverter}}" />
|
||||
</Grid>
|
||||
|
||||
@@ -27,22 +27,6 @@ namespace mbts.spark {
|
||||
MinZoomFactor = 0.5,
|
||||
MaxZoomFactor = 1.5;
|
||||
|
||||
static LadderView() {
|
||||
var knobs = config.Configuration.get<bts.config.LadderKnobs>();
|
||||
MimicTT = knobs.MimicTt;
|
||||
}
|
||||
|
||||
public static bool MimicTT { get; }
|
||||
|
||||
public static string CancelBuysButtonStyleKey =>
|
||||
MimicTT ? "TT_CancelBuys" : "CancelBuys";
|
||||
|
||||
public static string CancelSellsButtonStyleKey =>
|
||||
MimicTT ? "TT_CancelSells" : "CancelSells";
|
||||
|
||||
public static string CancelAllButtonStyleKey =>
|
||||
MimicTT ? "TT_CancelAll" : "CancelAll";
|
||||
|
||||
public static IMultiValueConverter PositionToolTipConverter =
|
||||
ValueConverter.create<AccountTuple?, bool, bool, bool, string>((account, show_day_pos, show_raw_day_pos, show_total_pos) => {
|
||||
const string day_pos_text = "day", raw_day_pos_text = "raw day", total_pos_text = "total";
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
using bts;
|
||||
using bts;
|
||||
using bts.api;
|
||||
using bts.api.core;
|
||||
using bts.api.orderman;
|
||||
@@ -223,6 +223,10 @@ namespace mbts.spark {
|
||||
Add(qty_column = new SizeColumn(
|
||||
new DoubleMinMax<OrderRecordTupleWithRoot>(
|
||||
ort => ort.order.order.quantity(), nameof(OrderBookRowVM.quantity))));
|
||||
// notional value
|
||||
Add(notional_value_column = new NotionalValueColumn(
|
||||
new DoubleMinMax<OrderRecordTupleWithRoot>(
|
||||
ort => ort.notional_value, nameof(OrderBookRowVM.notional_value))));
|
||||
// edit quantity column
|
||||
// not usually part of the main order book, but it's useful to add it
|
||||
// rather than debugging the popup window
|
||||
@@ -232,6 +236,10 @@ namespace mbts.spark {
|
||||
new DoubleMinMax<OrderRecordTupleWithRoot>(
|
||||
ort => ort.order.order.filled_quantity(),
|
||||
nameof(OrderBookRowVM.filled_quantity))));
|
||||
// filled notional value
|
||||
Add(filled_notional_value_column = new FilledNotionalValueColumn(
|
||||
new DoubleMinMax<OrderRecordTupleWithRoot>(
|
||||
ort => ort.filled_notional_value, nameof(OrderBookRowVM.filled_notional_value))));
|
||||
// price
|
||||
Add(price_column = new SimpleTextColumn<DoubleMinMaxState>(
|
||||
Columns.Price,
|
||||
@@ -363,9 +371,11 @@ namespace mbts.spark {
|
||||
public SimpleTextColumn<OrderSubType[]> sub_type_column { get; }
|
||||
public SideColumn side_column { get; }
|
||||
public SizeColumn qty_column { get; }
|
||||
public NotionalValueColumn notional_value_column { get; }
|
||||
public EditableQtyColumn edit_qty_column { get; }
|
||||
public SimpleTextColumn<DoubleMinMaxState> price_column { get; }
|
||||
public FilledColumn filled_qty_column { get; }
|
||||
public FilledNotionalValueColumn filled_notional_value_column { get; }
|
||||
public SimpleTextColumn<DoubleMinMaxState> last_price_column { get; }
|
||||
public SimpleTextColumn<DoubleMinMaxState> avg_price_column { get; }
|
||||
public SimpleTextColumn<OrderStatus[]> status_column { get; }
|
||||
@@ -391,8 +401,10 @@ namespace mbts.spark {
|
||||
order_sub_types = sub_type_column.filter.get_state(),
|
||||
sides = side_column.filter.get_state(),
|
||||
qty = qty_column.filter.get_state(),
|
||||
notional_value = notional_value_column.filter.get_state(),
|
||||
price = price_column.filter.get_state(),
|
||||
qty_filled = filled_qty_column.filter.get_state(),
|
||||
filled_notional_value = filled_notional_value_column.filter.get_state(),
|
||||
avg_price = avg_price_column.filter.get_state(),
|
||||
last_price = last_price_column.filter.get_state(),
|
||||
statuses = status_column.filter.get_state(),
|
||||
@@ -415,7 +427,9 @@ namespace mbts.spark {
|
||||
account_column.filter.load_state(fs.accounts);
|
||||
price_column.filter.load_state(fs.price);
|
||||
qty_column.filter.load_state(fs.qty);
|
||||
notional_value_column.filter.load_state(fs.notional_value);
|
||||
filled_qty_column.filter.load_state(fs.qty_filled);
|
||||
filled_notional_value_column.filter.load_state(fs.filled_notional_value);
|
||||
side_column.filter.load_state(fs.sides);
|
||||
order_type_column.filter.load_state(fs.order_types);
|
||||
root_algo_type_column.filter.load_state(fs.root_algo_types);
|
||||
@@ -645,6 +659,32 @@ namespace mbts.spark {
|
||||
DefaultCellPadding);
|
||||
}
|
||||
|
||||
public class NotionalValueColumn : OrderbookColumnBase<DoubleMinMaxState>, IRenderColumn<OrderBookRowVM> {
|
||||
public NotionalValueColumn(IDataGridColumnPersistentFilter<OrderRecordTupleWithRoot, DoubleMinMaxState> filter)
|
||||
: base(
|
||||
Columns.NotionalValue,
|
||||
DefaultColumnHeaders.NotionalValue,
|
||||
r => r.notional_value_text,
|
||||
DefaultColumnWidths.NotionalValue,
|
||||
filter,
|
||||
DataGridFilterKnownTemplates.DoubleMinMax,
|
||||
_wrap_comparer(r => r.notional_value)) { }
|
||||
|
||||
public void draw(OrderBookRowVM row, DrawingContext dc, ColumnGeometry position, double row_height) =>
|
||||
CellRender.draw_text(
|
||||
dc,
|
||||
_get_glyph_context(row),
|
||||
font_size,
|
||||
row.notional_value_text,
|
||||
position,
|
||||
row_height,
|
||||
RenderUtils.get_side_brush(row),
|
||||
background_brush,
|
||||
horizontal_alignment,
|
||||
VerticalAlignment.Center,
|
||||
DefaultCellPadding);
|
||||
}
|
||||
|
||||
public class FilledColumn : OrderbookColumnBase<DoubleMinMaxState>, IRenderColumn<OrderBookRowVM> {
|
||||
public FilledColumn(IDataGridColumnPersistentFilter<OrderRecordTupleWithRoot, DoubleMinMaxState> filter)
|
||||
: base(Columns.Filled, "Filled Qty", r => r.display_filled_quantity.ToString(), DefaultColumnWidths.Qty, filter, DataGridFilterKnownTemplates.DoubleMinMax, _wrap_comparer(r => r.filled_quantity)) { }
|
||||
@@ -664,6 +704,32 @@ namespace mbts.spark {
|
||||
DefaultCellPadding);
|
||||
}
|
||||
|
||||
public class FilledNotionalValueColumn : OrderbookColumnBase<DoubleMinMaxState>, IRenderColumn<OrderBookRowVM> {
|
||||
public FilledNotionalValueColumn(IDataGridColumnPersistentFilter<OrderRecordTupleWithRoot, DoubleMinMaxState> filter)
|
||||
: base(
|
||||
Columns.FilledNotionalValue,
|
||||
DefaultColumnHeaders.FilledNotionalValue,
|
||||
r => r.filled_notional_value_text,
|
||||
DefaultColumnWidths.FilledNotionalValue,
|
||||
filter,
|
||||
DataGridFilterKnownTemplates.DoubleMinMax,
|
||||
_wrap_comparer(r => r.filled_notional_value)) { }
|
||||
|
||||
public void draw(OrderBookRowVM row, DrawingContext dc, ColumnGeometry position, double row_height) =>
|
||||
CellRender.draw_text(
|
||||
dc,
|
||||
_get_glyph_context(row),
|
||||
font_size,
|
||||
row.filled_notional_value_text,
|
||||
position,
|
||||
row_height,
|
||||
RenderUtils.get_side_brush(row),
|
||||
background_brush,
|
||||
horizontal_alignment,
|
||||
VerticalAlignment.Center,
|
||||
DefaultCellPadding);
|
||||
}
|
||||
|
||||
public class OrderBookOrderTypeColumn : OrderTypeColumn<Columns>, ICVOrderBookColumn {
|
||||
readonly BindingBase _foreground_binding;
|
||||
ListSortDirection? _cached_sort_direction;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
using bts;
|
||||
using bts;
|
||||
using bts.api;
|
||||
using bts.api.orderman;
|
||||
using bts.collections;
|
||||
@@ -108,6 +108,8 @@ namespace mbts.spark {
|
||||
public double display_quantity => quantity.safe_divide(_display_qty_divisor);
|
||||
public string display_quantity_text =>
|
||||
quantity_display_settings.to_tick_display_settings().format(display_quantity);
|
||||
public double notional_value => order_tuple.notional_value;
|
||||
public string notional_value_text => _fmt_price(notional_value);
|
||||
public TickDisplaySettingsImmutable quantity_display_settings {
|
||||
get => _quantity_display_settings;
|
||||
set {
|
||||
@@ -128,6 +130,8 @@ namespace mbts.spark {
|
||||
filled_quantity.safe_divide(_display_qty_divisor);
|
||||
public string display_filled_quantity_text =>
|
||||
quantity_display_settings.to_tick_display_settings().format(display_filled_quantity);
|
||||
public double filled_notional_value => order_tuple.filled_notional_value;
|
||||
public string filled_notional_value_text => _fmt_price(filled_notional_value);
|
||||
public string last_msg => order.last_msg();
|
||||
// piq
|
||||
public uint piq => order_tuple.order.piq ?? uint.MaxValue;
|
||||
@@ -355,9 +359,15 @@ namespace mbts.spark {
|
||||
if (_prospective_qty_translator_backing != null)
|
||||
_prospective_qty_translator_backing.actual_value = quantity;
|
||||
}
|
||||
if ((Columns.NotionalValue & dirty) == Columns.NotionalValue) {
|
||||
RaisePropertyChanged(nameof(notional_value), nameof(notional_value_text));
|
||||
}
|
||||
if ((Columns.Filled & dirty) != 0) {
|
||||
RaisePropertyChanged(nameof(filled_quantity), nameof(display_filled_quantity));
|
||||
}
|
||||
if ((Columns.FilledNotionalValue & dirty) == Columns.FilledNotionalValue) {
|
||||
RaisePropertyChanged(nameof(filled_notional_value), nameof(filled_notional_value_text));
|
||||
}
|
||||
// badge fill
|
||||
if (old_order_tuple.order.meta?.badge_color != order_tuple.order.meta?.badge_color) {
|
||||
// invalidate by setting to null (will be re-instantiated in the public getter)
|
||||
@@ -521,6 +531,12 @@ namespace mbts.spark {
|
||||
if (oldval.order.piq != newval.order.piq) {
|
||||
dirty |= Columns.Piq;
|
||||
}
|
||||
if (oldval.notional_value != newval.notional_value) {
|
||||
dirty |= Columns.NotionalValue;
|
||||
}
|
||||
if (oldval.filled_notional_value != newval.filled_notional_value) {
|
||||
dirty |= Columns.FilledNotionalValue;
|
||||
}
|
||||
return dirty;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,160 +1,170 @@
|
||||
using bts.api;
|
||||
using bts.api.orderman;
|
||||
using bts.utils;
|
||||
|
||||
using mbts;
|
||||
using mbts.spark;
|
||||
using mbts.spark.filters;
|
||||
using mbts.ui;
|
||||
using mbts.ui.utility;
|
||||
|
||||
using ProtoBuf;
|
||||
|
||||
using Spark.Api.Orders;
|
||||
|
||||
using System;
|
||||
|
||||
namespace bts.shell.orderbook {
|
||||
[ProtoContract]
|
||||
class FilterStates : IEquatable<FilterStates> {
|
||||
[ProtoMember(1)]
|
||||
public SecurityCriteria security_criteria;
|
||||
[ProtoMember(2)]
|
||||
public string order_key_regex;
|
||||
[ProtoMember(3)]
|
||||
public TimeFilterTuple time_created;
|
||||
[ProtoMember(4)]
|
||||
public string[] users;
|
||||
[ProtoMember(5)]
|
||||
public string[] accounts;
|
||||
[ProtoMember(6)]
|
||||
public Venue[] exchanges;
|
||||
[ProtoMember(7)]
|
||||
public TimeInForce[] tifs;
|
||||
//[ProtoMember(8)]
|
||||
//public CustomOrderDefinition.CustomOneofCase[] order_types;
|
||||
[ProtoMember(23)]
|
||||
public AlgoID[] order_types;
|
||||
[ProtoMember(24)]
|
||||
public AlgoID[] root_algo_types;
|
||||
[ProtoMember(9)]
|
||||
public Side[] sides;
|
||||
[ProtoMember(10)]
|
||||
public DoubleMinMaxState qty;
|
||||
[ProtoMember(11)]
|
||||
public DoubleMinMaxState price;
|
||||
[ProtoMember(12)]
|
||||
public DoubleMinMaxState qty_filled;
|
||||
[ProtoMember(13)]
|
||||
public DoubleMinMaxState avg_price;
|
||||
[ProtoMember(14)]
|
||||
public DoubleMinMaxState last_price;
|
||||
[ProtoMember(15)]
|
||||
public OrderStatus[] statuses;
|
||||
[ProtoMember(16)]
|
||||
public string exchange_message_regex;
|
||||
[ProtoMember(17)]
|
||||
public TimeFilterTuple last_change;
|
||||
//[ProtoMember(18)]
|
||||
//public string security_regex;
|
||||
[ProtoMember(19)]
|
||||
public api.orderman.OrderSubType[] order_sub_types;
|
||||
[ProtoMember(20)]
|
||||
public string[] symbols;
|
||||
[ProtoMember(21)]
|
||||
public string parent_key_regex;
|
||||
[ProtoMember(22)]
|
||||
public string attached_target_order_key_regex;
|
||||
[ProtoMember(25)]
|
||||
public UIntMinMaxState piq;
|
||||
|
||||
public bool Equals(FilterStates other) =>
|
||||
other != null &&
|
||||
security_criteria.safe_equals(other.security_criteria) &&
|
||||
order_key_regex == other.order_key_regex &&
|
||||
parent_key_regex == other.parent_key_regex &&
|
||||
attached_target_order_key_regex == other.attached_target_order_key_regex &&
|
||||
time_created.Equals(other.time_created) &&
|
||||
EqualityComparer.safe_sequence_equals(users, other.users) &&
|
||||
EqualityComparer.safe_sequence_equals(accounts, other.accounts) &&
|
||||
EqualityComparer.safe_sequence_equals(exchanges, other.exchanges) &&
|
||||
EqualityComparer.safe_sequence_equals(tifs, other.tifs) &&
|
||||
EqualityComparer.safe_sequence_equals(order_types, other.order_types) &&
|
||||
EqualityComparer.safe_sequence_equals(root_algo_types, other.root_algo_types) &&
|
||||
EqualityComparer.safe_sequence_equals(sides, other.sides) &&
|
||||
qty.safe_equals(other.qty) &&
|
||||
price.safe_equals(other.price) &&
|
||||
qty_filled.safe_equals(other.qty_filled) &&
|
||||
avg_price.safe_equals(other.avg_price) &&
|
||||
last_price.safe_equals(other.last_price) &&
|
||||
EqualityComparer.safe_sequence_equals(statuses, other.statuses) &&
|
||||
exchange_message_regex == other.exchange_message_regex &&
|
||||
last_change.Equals(other.last_change) &&
|
||||
EqualityComparer.safe_sequence_equals(order_sub_types, other.order_sub_types) &&
|
||||
EqualityComparer.safe_sequence_equals(symbols, other.symbols) &&
|
||||
piq.safe_equals(other.piq);
|
||||
|
||||
public override bool Equals(object obj) => Equals(obj as FilterStates);
|
||||
|
||||
public override int GetHashCode() =>
|
||||
EqualityComparer.aggregate_hash(
|
||||
security_criteria?.GetHashCode() ?? 0,
|
||||
order_key_regex?.GetHashCode() ?? 0,
|
||||
parent_key_regex?.GetHashCode() ?? 0,
|
||||
attached_target_order_key_regex?.GetHashCode() ?? 0,
|
||||
time_created.GetHashCode(),
|
||||
EqualityComparer.safe_aggregate_hash(users),
|
||||
EqualityComparer.safe_aggregate_hash(accounts),
|
||||
EqualityComparer.safe_aggregate_hash(exchanges),
|
||||
EqualityComparer.safe_aggregate_hash(tifs),
|
||||
EqualityComparer.safe_aggregate_hash(order_types),
|
||||
EqualityComparer.safe_aggregate_hash(root_algo_types),
|
||||
EqualityComparer.safe_aggregate_hash(sides),
|
||||
qty?.GetHashCode() ?? 0,
|
||||
price?.GetHashCode() ?? 0,
|
||||
qty_filled?.GetHashCode() ?? 0,
|
||||
avg_price?.GetHashCode() ?? 0,
|
||||
last_price?.GetHashCode() ?? 0,
|
||||
EqualityComparer.safe_aggregate_hash(statuses),
|
||||
exchange_message_regex?.GetHashCode() ?? 0,
|
||||
last_change.GetHashCode(),
|
||||
EqualityComparer.safe_aggregate_hash(order_sub_types),
|
||||
EqualityComparer.safe_aggregate_hash(symbols),
|
||||
piq?.GetHashCode() ?? 0);
|
||||
}
|
||||
|
||||
static class FilterStatesExtensions {
|
||||
public static Func<OrderRecordTupleWithRoot, bool> build_predicate(this FilterStates fs) {
|
||||
var pb = new ColumnPredicateBuilder<OrderRecordTupleWithRoot>();
|
||||
// security
|
||||
pb.add_security_criteria(fs.security_criteria, o => o.order.security);
|
||||
// regex
|
||||
pb.add_regex(fs.order_key_regex, o => o.order.order.Key.ToString());
|
||||
pb.add_regex(fs.exchange_message_regex, o => o.order.order.State.LastMsg);
|
||||
pb.add_regex(fs.attached_target_order_key_regex, o => o.order.order.attached_target_order_key().ToString());
|
||||
pb.add_regex(fs.parent_key_regex, o => o.order.order.ParentKey.ToString());
|
||||
// time
|
||||
pb.add_time_filter(fs.time_created, o => o.order.order.created());
|
||||
pb.add_time_filter(fs.last_change, o => o.order.order.last_change());
|
||||
// discrete values
|
||||
pb.add_discrete_values(fs.users, o => o.order.order.user());
|
||||
pb.add_discrete_values(fs.accounts, o => o.order.order.account());
|
||||
pb.add_discrete_values(fs.exchanges, o => o.order.security?.Exchange ?? Venue.Unset);
|
||||
pb.add_discrete_values(fs.tifs, o => o.order.order.time_in_force());
|
||||
pb.add_discrete_values(fs.order_types, o => AlgoID.from_custom_def(o.order.order.Def.CustomDef));
|
||||
pb.add_discrete_values(fs.root_algo_types, o => AlgoID.from_algo_key(o.root.algo?.key ?? default));
|
||||
pb.add_discrete_values(fs.order_sub_types, o => o.order.order.sub_type());
|
||||
pb.add_discrete_values(fs.sides, o => o.order.order.side());
|
||||
pb.add_discrete_values(fs.statuses, o => o.order.order.status());
|
||||
pb.add_discrete_values(fs.symbols, o => o.order.security?.Symbol);
|
||||
// numeric
|
||||
pb.add_numeric(fs.qty, o => o.order.order.quantity());
|
||||
pb.add_price(fs.price, o => o.order.order.ui_price(), o => o.order.security);
|
||||
pb.add_numeric(fs.qty_filled, o => o.order.order.filled_quantity());
|
||||
pb.add_numeric(fs.avg_price, o => o.order.order.avg_price());
|
||||
pb.add_numeric(fs.last_price, o => o.order.order.last_price());
|
||||
pb.add_numeric(fs.piq, o => o.order.piq ?? uint.MaxValue);
|
||||
return pb.to_predicate();
|
||||
}
|
||||
}
|
||||
}
|
||||
using bts.api;
|
||||
using bts.api.orderman;
|
||||
using bts.utils;
|
||||
|
||||
using mbts;
|
||||
using mbts.spark;
|
||||
using mbts.spark.filters;
|
||||
using mbts.ui;
|
||||
using mbts.ui.utility;
|
||||
|
||||
using ProtoBuf;
|
||||
|
||||
using Spark.Api.Orders;
|
||||
|
||||
using System;
|
||||
|
||||
namespace bts.shell.orderbook {
|
||||
[ProtoContract]
|
||||
class FilterStates : IEquatable<FilterStates> {
|
||||
[ProtoMember(1)]
|
||||
public SecurityCriteria security_criteria;
|
||||
[ProtoMember(2)]
|
||||
public string order_key_regex;
|
||||
[ProtoMember(3)]
|
||||
public TimeFilterTuple time_created;
|
||||
[ProtoMember(4)]
|
||||
public string[] users;
|
||||
[ProtoMember(5)]
|
||||
public string[] accounts;
|
||||
[ProtoMember(6)]
|
||||
public Venue[] exchanges;
|
||||
[ProtoMember(7)]
|
||||
public TimeInForce[] tifs;
|
||||
//[ProtoMember(8)]
|
||||
//public CustomOrderDefinition.CustomOneofCase[] order_types;
|
||||
[ProtoMember(23)]
|
||||
public AlgoID[] order_types;
|
||||
[ProtoMember(24)]
|
||||
public AlgoID[] root_algo_types;
|
||||
[ProtoMember(9)]
|
||||
public Side[] sides;
|
||||
[ProtoMember(10)]
|
||||
public DoubleMinMaxState qty;
|
||||
[ProtoMember(26)]
|
||||
public DoubleMinMaxState notional_value;
|
||||
[ProtoMember(11)]
|
||||
public DoubleMinMaxState price;
|
||||
[ProtoMember(12)]
|
||||
public DoubleMinMaxState qty_filled;
|
||||
[ProtoMember(27)]
|
||||
public DoubleMinMaxState filled_notional_value;
|
||||
[ProtoMember(13)]
|
||||
public DoubleMinMaxState avg_price;
|
||||
[ProtoMember(14)]
|
||||
public DoubleMinMaxState last_price;
|
||||
[ProtoMember(15)]
|
||||
public OrderStatus[] statuses;
|
||||
[ProtoMember(16)]
|
||||
public string exchange_message_regex;
|
||||
[ProtoMember(17)]
|
||||
public TimeFilterTuple last_change;
|
||||
//[ProtoMember(18)]
|
||||
//public string security_regex;
|
||||
[ProtoMember(19)]
|
||||
public api.orderman.OrderSubType[] order_sub_types;
|
||||
[ProtoMember(20)]
|
||||
public string[] symbols;
|
||||
[ProtoMember(21)]
|
||||
public string parent_key_regex;
|
||||
[ProtoMember(22)]
|
||||
public string attached_target_order_key_regex;
|
||||
[ProtoMember(25)]
|
||||
public UIntMinMaxState piq;
|
||||
|
||||
public bool Equals(FilterStates other) =>
|
||||
other != null &&
|
||||
security_criteria.safe_equals(other.security_criteria) &&
|
||||
order_key_regex == other.order_key_regex &&
|
||||
parent_key_regex == other.parent_key_regex &&
|
||||
attached_target_order_key_regex == other.attached_target_order_key_regex &&
|
||||
time_created.Equals(other.time_created) &&
|
||||
EqualityComparer.safe_sequence_equals(users, other.users) &&
|
||||
EqualityComparer.safe_sequence_equals(accounts, other.accounts) &&
|
||||
EqualityComparer.safe_sequence_equals(exchanges, other.exchanges) &&
|
||||
EqualityComparer.safe_sequence_equals(tifs, other.tifs) &&
|
||||
EqualityComparer.safe_sequence_equals(order_types, other.order_types) &&
|
||||
EqualityComparer.safe_sequence_equals(root_algo_types, other.root_algo_types) &&
|
||||
EqualityComparer.safe_sequence_equals(sides, other.sides) &&
|
||||
qty.safe_equals(other.qty) &&
|
||||
notional_value.safe_equals(other.notional_value) &&
|
||||
price.safe_equals(other.price) &&
|
||||
qty_filled.safe_equals(other.qty_filled) &&
|
||||
filled_notional_value.safe_equals(other.filled_notional_value) &&
|
||||
avg_price.safe_equals(other.avg_price) &&
|
||||
last_price.safe_equals(other.last_price) &&
|
||||
EqualityComparer.safe_sequence_equals(statuses, other.statuses) &&
|
||||
exchange_message_regex == other.exchange_message_regex &&
|
||||
last_change.Equals(other.last_change) &&
|
||||
EqualityComparer.safe_sequence_equals(order_sub_types, other.order_sub_types) &&
|
||||
EqualityComparer.safe_sequence_equals(symbols, other.symbols) &&
|
||||
piq.safe_equals(other.piq);
|
||||
|
||||
public override bool Equals(object obj) => Equals(obj as FilterStates);
|
||||
|
||||
public override int GetHashCode() =>
|
||||
EqualityComparer.aggregate_hash(
|
||||
security_criteria?.GetHashCode() ?? 0,
|
||||
order_key_regex?.GetHashCode() ?? 0,
|
||||
parent_key_regex?.GetHashCode() ?? 0,
|
||||
attached_target_order_key_regex?.GetHashCode() ?? 0,
|
||||
time_created.GetHashCode(),
|
||||
EqualityComparer.safe_aggregate_hash(users),
|
||||
EqualityComparer.safe_aggregate_hash(accounts),
|
||||
EqualityComparer.safe_aggregate_hash(exchanges),
|
||||
EqualityComparer.safe_aggregate_hash(tifs),
|
||||
EqualityComparer.safe_aggregate_hash(order_types),
|
||||
EqualityComparer.safe_aggregate_hash(root_algo_types),
|
||||
EqualityComparer.safe_aggregate_hash(sides),
|
||||
qty?.GetHashCode() ?? 0,
|
||||
notional_value?.GetHashCode() ?? 0,
|
||||
price?.GetHashCode() ?? 0,
|
||||
qty_filled?.GetHashCode() ?? 0,
|
||||
filled_notional_value?.GetHashCode() ?? 0,
|
||||
avg_price?.GetHashCode() ?? 0,
|
||||
last_price?.GetHashCode() ?? 0,
|
||||
EqualityComparer.safe_aggregate_hash(statuses),
|
||||
exchange_message_regex?.GetHashCode() ?? 0,
|
||||
last_change.GetHashCode(),
|
||||
EqualityComparer.safe_aggregate_hash(order_sub_types),
|
||||
EqualityComparer.safe_aggregate_hash(symbols),
|
||||
piq?.GetHashCode() ?? 0);
|
||||
}
|
||||
|
||||
static class FilterStatesExtensions {
|
||||
public static Func<OrderRecordTupleWithRoot, bool> build_predicate(this FilterStates fs) {
|
||||
var pb = new ColumnPredicateBuilder<OrderRecordTupleWithRoot>();
|
||||
// security
|
||||
pb.add_security_criteria(fs.security_criteria, o => o.order.security);
|
||||
// regex
|
||||
pb.add_regex(fs.order_key_regex, o => o.order.order.Key.ToString());
|
||||
pb.add_regex(fs.exchange_message_regex, o => o.order.order.State.LastMsg);
|
||||
pb.add_regex(fs.attached_target_order_key_regex, o => o.order.order.attached_target_order_key().ToString());
|
||||
pb.add_regex(fs.parent_key_regex, o => o.order.order.ParentKey.ToString());
|
||||
// time
|
||||
pb.add_time_filter(fs.time_created, o => o.order.order.created());
|
||||
pb.add_time_filter(fs.last_change, o => o.order.order.last_change());
|
||||
// discrete values
|
||||
pb.add_discrete_values(fs.users, o => o.order.order.user());
|
||||
pb.add_discrete_values(fs.accounts, o => o.order.order.account());
|
||||
pb.add_discrete_values(fs.exchanges, o => o.order.security?.Exchange ?? Venue.Unset);
|
||||
pb.add_discrete_values(fs.tifs, o => o.order.order.time_in_force());
|
||||
pb.add_discrete_values(fs.order_types, o => AlgoID.from_custom_def(o.order.order.Def.CustomDef));
|
||||
pb.add_discrete_values(fs.root_algo_types, o => AlgoID.from_algo_key(o.root.algo?.key ?? default));
|
||||
pb.add_discrete_values(fs.order_sub_types, o => o.order.order.sub_type());
|
||||
pb.add_discrete_values(fs.sides, o => o.order.order.side());
|
||||
pb.add_discrete_values(fs.statuses, o => o.order.order.status());
|
||||
pb.add_discrete_values(fs.symbols, o => o.order.security?.Symbol);
|
||||
// numeric
|
||||
pb.add_numeric(fs.qty, o => o.order.order.quantity());
|
||||
pb.add_numeric(fs.notional_value, o => o.notional_value);
|
||||
pb.add_price(fs.price, o => o.order.order.ui_price(), o => o.order.security);
|
||||
pb.add_numeric(fs.qty_filled, o => o.order.order.filled_quantity());
|
||||
pb.add_numeric(fs.filled_notional_value, o => o.filled_notional_value);
|
||||
pb.add_numeric(fs.avg_price, o => o.order.order.avg_price());
|
||||
pb.add_numeric(fs.last_price, o => o.order.order.last_price());
|
||||
pb.add_numeric(fs.piq, o => o.order.piq ?? uint.MaxValue);
|
||||
return pb.to_predicate();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -29,7 +29,7 @@ namespace bts.shell.orderbook {
|
||||
static OrderBookGridSettingsManager() {
|
||||
// make font-weight immutable for all columns (the font-weight rule is hard-coded: Regular for root-orders, Light for all others)
|
||||
// TODO: Remove the immutable font-weight flags when cell animation is ready
|
||||
var col_settings_flags = EnumHelper.get_values<Columns>().ToDictionary(
|
||||
var col_settings_flags = Enum.GetValuesAsUnderlyingType<Columns>().Cast<Columns>().ToDictionary(
|
||||
c => c.ToString(), c => GridColumnSettingsFeatureFlags.ImmutableFontWeight);
|
||||
// make font-color immutable for Side, Size, and Filled columns (the font-color rule is hard-coded: MarketData.bid for Buy orders, MarketData.ask for Sells)
|
||||
// TODO: Remove the immutable font-color flags when cell animation is ready
|
||||
|
||||
@@ -1,270 +1,277 @@
|
||||
using bts.api.orderman;
|
||||
using bts.shell.fills;
|
||||
|
||||
using mbts;
|
||||
using mbts.ui;
|
||||
using mbts.ui.controls;
|
||||
using mbts.ui.settings;
|
||||
|
||||
using ProtoBuf;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reactive.Linq;
|
||||
using System.Windows;
|
||||
|
||||
using Columns = mbts.spark.OrderBookGridView.ColumnKeys;
|
||||
using GridSettings = mbts.ui.settings.GridSettings;
|
||||
|
||||
namespace bts.shell.orderbook {
|
||||
using ColHeader = mbts.ui.controls.DIYGrid.DefaultColumnHeaders;
|
||||
using ColWidth = mbts.ui.controls.DIYGrid.DefaultColumnWidths;
|
||||
|
||||
[ProtoContract]
|
||||
class OrderBookStartupData {
|
||||
const bool
|
||||
_default_show_right_panel = true,
|
||||
_default_show_order_params = true;
|
||||
const double
|
||||
_default_right_panel_width = 400,
|
||||
_default_order_params_height = 250,
|
||||
_default_fills_height = 300,
|
||||
_default_ladder_height = 350;
|
||||
[ProtoMember(16)]
|
||||
OrderBookRightPanelSettings _right_panel;
|
||||
// ALREADY USED TAGS
|
||||
// [ProtoMember(5)]
|
||||
// public readonly List<SortingCriteria> sort_criteria;
|
||||
|
||||
public OrderBookRightPanelSettings right_panel => _right_panel ?? (_right_panel = new OrderBookRightPanelSettings());
|
||||
/// TODO: remove properties below, and redirect callers to <see cref="right_panel"/>
|
||||
// right panel (host)
|
||||
public bool show_right_panel { get => right_panel.show; set => right_panel.show = value; }
|
||||
public double right_panel_width { get => right_panel.width; set => right_panel.width = value; }
|
||||
// order params
|
||||
public bool show_order_params { get => right_panel.stack.show_order_params; set => right_panel.stack.show_order_params = value; }
|
||||
public double order_params_height { get; set; }
|
||||
// ladder
|
||||
public bool show_ladder { get; set; }
|
||||
public double ladder_height { get => right_panel.stack.ladder_height; set => right_panel.stack.ladder_height = value; }
|
||||
// fills
|
||||
public bool show_fills { get => right_panel.stack.show_fills; set => right_panel.stack.show_fills = value; }
|
||||
public double fills_height { get => right_panel.stack.fills_height; set => right_panel.stack.fills_height = value; }
|
||||
|
||||
// filters
|
||||
[ProtoMember(10)]
|
||||
public FilterStates filters;
|
||||
// grid
|
||||
[ProtoMember(15)]
|
||||
[Obsolete]
|
||||
public GridSettings grid { get; set; }
|
||||
// orderbook grid
|
||||
[ProtoMember(17)]
|
||||
public OrderBookGridSettings orderbook_grid { get; set; }
|
||||
// fills settings
|
||||
[ProtoMember(26)]
|
||||
public OrderBookFillsGridSettings fills_settings { get; set; }
|
||||
// predicate animations
|
||||
[ProtoMember(20)]
|
||||
[Obsolete]
|
||||
List<OrderRecordTuplePredicateAnimations> _predicate_animations;
|
||||
// use defaults
|
||||
[ProtoMember(25)]
|
||||
public bool use_defaults;
|
||||
// menu display
|
||||
[ProtoMember(27)]
|
||||
public SparkMenuDisplaySettings menu_display { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Please use <see cref="Default"/> to get a new instance (the only reason the default constructor still exists is for ProtoBuf deserialization).
|
||||
/// </summary>
|
||||
[Obsolete]
|
||||
public OrderBookStartupData() {
|
||||
show_right_panel = _default_show_right_panel;
|
||||
show_order_params = _default_show_order_params;
|
||||
right_panel_width = _default_right_panel_width;
|
||||
order_params_height = _default_order_params_height;
|
||||
fills_height = _default_fills_height;
|
||||
ladder_height = _default_ladder_height;
|
||||
}
|
||||
|
||||
public OrderBookStartupData(FilterStates filters, OrderBookGridSettings orderbook_grid = default, bool show_right_panel = false, double right_panel_width = 0, double fills_height = 0, bool show_fills = false, bool use_defaults = false) {
|
||||
this.filters = filters;
|
||||
this.use_defaults = use_defaults;
|
||||
this.show_right_panel = show_right_panel;
|
||||
this.right_panel_width = right_panel_width;
|
||||
this.fills_height = fills_height;
|
||||
this.show_fills = show_fills;
|
||||
this.orderbook_grid = orderbook_grid;
|
||||
#pragma warning disable CS0612 // Type or member is obsolete
|
||||
_predicate_animations = new List<OrderRecordTuplePredicateAnimations>();
|
||||
if (predicate_animations != null)
|
||||
_predicate_animations.AddRange(predicate_animations);
|
||||
#pragma warning restore CS0612 // Type or member is obsolete
|
||||
}
|
||||
|
||||
#pragma warning disable CS0612 // Type or member is obsolete
|
||||
[Obsolete]
|
||||
public IReadOnlyList<OrderRecordTuplePredicateAnimations> predicate_animations => _predicate_animations ?? (_predicate_animations = new List<OrderRecordTuplePredicateAnimations>());
|
||||
public OrderBookGridSettings grid_settings_wrapper() {
|
||||
if (use_defaults)
|
||||
return null;
|
||||
if (orderbook_grid == null) {
|
||||
if (grid == null)
|
||||
grid = _default_general_grid;
|
||||
orderbook_grid = new OrderBookGridSettings(grid.Clone(), _predicate_animations);
|
||||
}
|
||||
return orderbook_grid;
|
||||
}
|
||||
#pragma warning restore CS0612 // Type or member is obsolete
|
||||
|
||||
public static OrderBookStartupData Default =>
|
||||
new OrderBookStartupData(
|
||||
new FilterStates { statuses = new[] { OrderStatus.Working } },
|
||||
DefaultGrid,
|
||||
_default_show_right_panel,
|
||||
_default_right_panel_width,
|
||||
_default_fills_height,
|
||||
use_defaults: true) {
|
||||
show_order_params = _default_show_order_params,
|
||||
order_params_height = _default_order_params_height,
|
||||
fills_settings = DefaultFillsGrid
|
||||
};
|
||||
|
||||
public static OrderBookGridSettings DefaultGrid => new OrderBookGridSettings(_default_general_grid) {
|
||||
algo_tree_display_mode = AlgoTreeDisplayMode.RootAndLeafNodes
|
||||
};
|
||||
|
||||
public static OrderBookFillsGridSettings DefaultFillsGrid {
|
||||
get {
|
||||
var default_fills = FillsSettings.Default();
|
||||
var rv = new OrderBookFillsGridSettings {
|
||||
sorting_criteria = default_fills.sorting_criteria.ToList(),
|
||||
filters = default_fills.filters,
|
||||
general = FillsSettings.MiniGridSettings
|
||||
};
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
static GridSettings _default_general_grid => GridSettings.Default.with(DefaultColumns.standard, DefaultColumns.sorting_criteria);
|
||||
public static OrderBookGridSettings default_mini_grid => new OrderBookGridSettings(GridSettings.Default.with(DefaultColumns.mini, DefaultColumns.sorting_criteria)) {
|
||||
algo_tree_display_mode = AlgoTreeDisplayMode.RootAndLeafNodes
|
||||
};
|
||||
|
||||
static class DefaultColumns {
|
||||
// standard
|
||||
public static GridColumnSettings security => new GridColumnSettings(Columns.Security.ToString(), DIYGrid.DefaultColumnHeaders.Security, width: DIYGrid.DefaultColumnWidths.Security);
|
||||
public static GridColumnSettings symbol => new GridColumnSettings(Columns.Symbol.ToString(), DIYGrid.DefaultColumnHeaders.Symbol, width: DIYGrid.DefaultColumnWidths.Symbol, horizontal_alignment: HorizontalAlignment.Left);
|
||||
public static GridColumnSettings time_created => new GridColumnSettings(Columns.Time.ToString(), "Time", width: DIYGrid.DefaultColumnWidths.Time, horizontal_alignment: HorizontalAlignment.Left);
|
||||
public static GridColumnSettings last_change => new GridColumnSettings(Columns.LastChange.ToString(), "Last Change", width: DIYGrid.DefaultColumnWidths.Time, horizontal_alignment: HorizontalAlignment.Left);
|
||||
public static GridColumnSettings user => new GridColumnSettings(Columns.User.ToString(), DIYGrid.DefaultColumnHeaders.User, width: DIYGrid.DefaultColumnWidths.User, horizontal_alignment: HorizontalAlignment.Left);
|
||||
public static GridColumnSettings account => new GridColumnSettings(Columns.Account.ToString(), DIYGrid.DefaultColumnHeaders.Account, width: DIYGrid.DefaultColumnWidths.Account, horizontal_alignment: HorizontalAlignment.Left);
|
||||
public static GridColumnSettings exchange => new GridColumnSettings(Columns.Exchange.ToString(), DIYGrid.DefaultColumnHeaders.Exchange, width: DIYGrid.DefaultColumnWidths.Exchange, horizontal_alignment: HorizontalAlignment.Left);
|
||||
public static GridColumnSettings time_in_force => new GridColumnSettings(Columns.TimeInForce.ToString(), DIYGrid.DefaultColumnHeaders.TimeInForce, width: DIYGrid.DefaultColumnWidths.TimeInForce);
|
||||
public static GridColumnSettings order_type => new GridColumnSettings(Columns.OrderType.ToString(), DIYGrid.DefaultColumnHeaders.OrderType, width: DIYGrid.DefaultColumnWidths.OrderType);
|
||||
public static GridColumnSettings root_algo_type => new GridColumnSettings(Columns.RootAlgoType.ToString(), DIYGrid.DefaultColumnHeaders.RootAlgoType, width: DIYGrid.DefaultColumnWidths.OrderType);
|
||||
public static GridColumnSettings sub_type => new GridColumnSettings(Columns.SubType.ToString(), "Sub Type", width: 80, horizontal_alignment: HorizontalAlignment.Left);
|
||||
|
||||
public static GridColumnSettings side => new(
|
||||
Columns.Side.ToString(), ColHeader.Side,
|
||||
width: ColWidth.Side,
|
||||
horizontal_alignment: HorizontalAlignment.Left);
|
||||
|
||||
public static GridColumnSettings quantity => new GridColumnSettings(Columns.Size.ToString(), "Qty", width: DIYGrid.DefaultColumnWidths.Qty, horizontal_alignment: HorizontalAlignment.Right);
|
||||
public static GridColumnSettings price => new GridColumnSettings(Columns.Price.ToString(), DIYGrid.DefaultColumnHeaders.Price, width: DIYGrid.DefaultColumnWidths.Price, horizontal_alignment: HorizontalAlignment.Right);
|
||||
|
||||
public static GridColumnSettings filled_quantity => new(
|
||||
Columns.Filled.ToString(),
|
||||
ColHeader.FilledQuantity,
|
||||
width: ColWidth.Filled,
|
||||
horizontal_alignment: HorizontalAlignment.Right);
|
||||
|
||||
public static GridColumnSettings avg_price => new(
|
||||
Columns.AvgFilled.ToString(),
|
||||
ColHeader.AvgFilled,
|
||||
width: ColWidth.AveragePrice,
|
||||
horizontal_alignment: HorizontalAlignment.Right);
|
||||
|
||||
public static GridColumnSettings last_price => new GridColumnSettings(Columns.LastPrice.ToString(), "Last Filled", width: DIYGrid.DefaultColumnWidths.LastPrice, horizontal_alignment: HorizontalAlignment.Right);
|
||||
public static GridColumnSettings status => new GridColumnSettings(Columns.Status.ToString(), "Status", width: 80, horizontal_alignment: HorizontalAlignment.Left);
|
||||
public static GridColumnSettings last_message => new GridColumnSettings(Columns.LastMessage.ToString(), "Last Message", width: DIYGrid.DefaultColumnWidths.LastMessage, horizontal_alignment: HorizontalAlignment.Left);
|
||||
public static GridColumnSettings security_key => new GridColumnSettings(Columns.SecurityKey.ToString(), DIYGrid.DefaultColumnHeaders.SecurityKey, is_visible: false, width: DIYGrid.DefaultColumnWidths.SecurityKey, horizontal_alignment: HorizontalAlignment.Left);
|
||||
public static GridColumnSettings order_key => new GridColumnSettings(Columns.OrderKey.ToString(), DIYGrid.DefaultColumnHeaders.OrderKey, width: DIYGrid.DefaultColumnWidths.OrderKey, horizontal_alignment: HorizontalAlignment.Left);
|
||||
public static GridColumnSettings parent_key => new GridColumnSettings(Columns.ParentKey.ToString(), "Parent Key", width: DIYGrid.DefaultColumnWidths.ParentKey, horizontal_alignment: HorizontalAlignment.Left);
|
||||
public static GridColumnSettings attached_target_order_key => new GridColumnSettings(Columns.AttachedTargetOrderKey.ToString(), DIYGrid.DefaultColumnHeaders.AttachedTargetOrderKey, width: DIYGrid.DefaultColumnWidths.ParentKey, horizontal_alignment: HorizontalAlignment.Left);
|
||||
public static GridColumnSettings piq => new GridColumnSettings(Columns.Piq.ToString(), "PIQ", width: DIYGrid.DefaultColumnWidths.Qty, horizontal_alignment: HorizontalAlignment.Right);
|
||||
// editable
|
||||
public static GridColumnSettings cancel => new GridColumnSettings(Columns.Cancel.ToString(), DIYGrid.DefaultColumnHeaders.Cancel, width: DIYGrid.DefaultColumnWidths.MiniOrderBook.Cancel);
|
||||
public static GridColumnSettings attach_detach => new GridColumnSettings(Columns.Attach.ToString(), DIYGrid.DefaultColumnHeaders.AttachDetach, width: DIYGrid.DefaultColumnWidths.MiniOrderBook.AttachDetach);
|
||||
public static GridColumnSettings update => new GridColumnSettings(Columns.Update.ToString(), DIYGrid.DefaultColumnHeaders.Update, width: DIYGrid.DefaultColumnWidths.MiniOrderBook.Update);
|
||||
public static GridColumnSettings edit_quantity => new GridColumnSettings(Columns.EditableQty.ToString(), DIYGrid.DefaultColumnHeaders.Qty, width: DIYGrid.DefaultColumnWidths.MiniOrderBook.EditableSize);
|
||||
public static GridColumnSettings edit_price => new GridColumnSettings(Columns.EditablePrice.ToString(), DIYGrid.DefaultColumnHeaders.Price, width: DIYGrid.DefaultColumnWidths.MiniOrderBook.EditablePrice);
|
||||
public static GridColumnSettings commit => new GridColumnSettings(Columns.Commit.ToString(), width: DIYGrid.DefaultColumnWidths.MiniOrderBook.Commit);
|
||||
|
||||
public static IEnumerable<SortingCriteria> sorting_criteria {
|
||||
get {
|
||||
yield return new SortingCriteria(time_created.id, false);
|
||||
}
|
||||
}
|
||||
|
||||
public static IEnumerable<GridColumnSettings> standard {
|
||||
get {
|
||||
yield return security;
|
||||
yield return symbol;
|
||||
yield return time_created;
|
||||
yield return last_change;
|
||||
yield return user;
|
||||
yield return account;
|
||||
yield return exchange;
|
||||
yield return time_in_force;
|
||||
yield return order_type;
|
||||
yield return root_algo_type;
|
||||
yield return sub_type;
|
||||
yield return side;
|
||||
yield return quantity;
|
||||
yield return price;
|
||||
yield return piq;
|
||||
yield return filled_quantity;
|
||||
yield return avg_price;
|
||||
yield return last_price;
|
||||
yield return status;
|
||||
yield return last_message;
|
||||
yield return security_key;
|
||||
yield return order_key;
|
||||
yield return parent_key;
|
||||
yield return attached_target_order_key;
|
||||
}
|
||||
}
|
||||
|
||||
public static IEnumerable<GridColumnSettings> editable {
|
||||
get {
|
||||
yield return cancel;
|
||||
yield return update;
|
||||
yield return edit_quantity;
|
||||
yield return edit_price;
|
||||
yield return commit;
|
||||
yield return attach_detach;
|
||||
}
|
||||
}
|
||||
|
||||
private static HashSet<GridColumnSettings> _mini_mode_columns_to_hide = new HashSet<GridColumnSettings> {
|
||||
symbol, last_change, exchange, sub_type, quantity, price, avg_price, last_price, status, last_message, security_key, order_key, parent_key, attached_target_order_key
|
||||
};
|
||||
|
||||
public static IEnumerable<GridColumnSettings> mini {
|
||||
get {
|
||||
foreach (var col_setting in editable)
|
||||
yield return col_setting;
|
||||
foreach (var col_setting in standard) {
|
||||
if (_mini_mode_columns_to_hide.Contains(col_setting)) {
|
||||
yield return col_setting.with(is_visible: false);
|
||||
} else {
|
||||
yield return col_setting;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
using bts.api.orderman;
|
||||
using bts.shell.fills;
|
||||
|
||||
using mbts;
|
||||
using mbts.ui;
|
||||
using mbts.ui.controls;
|
||||
using mbts.ui.settings;
|
||||
|
||||
using ProtoBuf;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reactive.Linq;
|
||||
using System.Windows;
|
||||
|
||||
using Columns = mbts.spark.OrderBookGridView.ColumnKeys;
|
||||
using GridSettings = mbts.ui.settings.GridSettings;
|
||||
|
||||
namespace bts.shell.orderbook {
|
||||
using ColHeader = mbts.ui.controls.DIYGrid.DefaultColumnHeaders;
|
||||
using ColWidth = mbts.ui.controls.DIYGrid.DefaultColumnWidths;
|
||||
|
||||
[ProtoContract]
|
||||
class OrderBookStartupData {
|
||||
const bool
|
||||
_default_show_right_panel = true,
|
||||
_default_show_order_params = true;
|
||||
const double
|
||||
_default_right_panel_width = 400,
|
||||
_default_order_params_height = 250,
|
||||
_default_fills_height = 300,
|
||||
_default_ladder_height = 350;
|
||||
[ProtoMember(16)]
|
||||
OrderBookRightPanelSettings _right_panel;
|
||||
// ALREADY USED TAGS
|
||||
// [ProtoMember(5)]
|
||||
// public readonly List<SortingCriteria> sort_criteria;
|
||||
|
||||
public OrderBookRightPanelSettings right_panel => _right_panel ?? (_right_panel = new OrderBookRightPanelSettings());
|
||||
/// TODO: remove properties below, and redirect callers to <see cref="right_panel"/>
|
||||
// right panel (host)
|
||||
public bool show_right_panel { get => right_panel.show; set => right_panel.show = value; }
|
||||
public double right_panel_width { get => right_panel.width; set => right_panel.width = value; }
|
||||
// order params
|
||||
public bool show_order_params { get => right_panel.stack.show_order_params; set => right_panel.stack.show_order_params = value; }
|
||||
public double order_params_height { get; set; }
|
||||
// ladder
|
||||
public bool show_ladder { get; set; }
|
||||
public double ladder_height { get => right_panel.stack.ladder_height; set => right_panel.stack.ladder_height = value; }
|
||||
// fills
|
||||
public bool show_fills { get => right_panel.stack.show_fills; set => right_panel.stack.show_fills = value; }
|
||||
public double fills_height { get => right_panel.stack.fills_height; set => right_panel.stack.fills_height = value; }
|
||||
|
||||
// filters
|
||||
[ProtoMember(10)]
|
||||
public FilterStates filters;
|
||||
// grid
|
||||
[ProtoMember(15)]
|
||||
[Obsolete]
|
||||
public GridSettings grid { get; set; }
|
||||
// orderbook grid
|
||||
[ProtoMember(17)]
|
||||
public OrderBookGridSettings orderbook_grid { get; set; }
|
||||
// fills settings
|
||||
[ProtoMember(26)]
|
||||
public OrderBookFillsGridSettings fills_settings { get; set; }
|
||||
// predicate animations
|
||||
[ProtoMember(20)]
|
||||
[Obsolete]
|
||||
List<OrderRecordTuplePredicateAnimations> _predicate_animations;
|
||||
// use defaults
|
||||
[ProtoMember(25)]
|
||||
public bool use_defaults;
|
||||
// menu display
|
||||
[ProtoMember(27)]
|
||||
public SparkMenuDisplaySettings menu_display { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Please use <see cref="Default"/> to get a new instance (the only reason the default constructor still exists is for ProtoBuf deserialization).
|
||||
/// </summary>
|
||||
[Obsolete]
|
||||
public OrderBookStartupData() {
|
||||
show_right_panel = _default_show_right_panel;
|
||||
show_order_params = _default_show_order_params;
|
||||
right_panel_width = _default_right_panel_width;
|
||||
order_params_height = _default_order_params_height;
|
||||
fills_height = _default_fills_height;
|
||||
ladder_height = _default_ladder_height;
|
||||
}
|
||||
|
||||
public OrderBookStartupData(FilterStates filters, OrderBookGridSettings orderbook_grid = default, bool show_right_panel = false, double right_panel_width = 0, double fills_height = 0, bool show_fills = false, bool use_defaults = false) {
|
||||
this.filters = filters;
|
||||
this.use_defaults = use_defaults;
|
||||
this.show_right_panel = show_right_panel;
|
||||
this.right_panel_width = right_panel_width;
|
||||
this.fills_height = fills_height;
|
||||
this.show_fills = show_fills;
|
||||
this.orderbook_grid = orderbook_grid;
|
||||
#pragma warning disable CS0612 // Type or member is obsolete
|
||||
_predicate_animations = new List<OrderRecordTuplePredicateAnimations>();
|
||||
if (predicate_animations != null)
|
||||
_predicate_animations.AddRange(predicate_animations);
|
||||
#pragma warning restore CS0612 // Type or member is obsolete
|
||||
}
|
||||
|
||||
#pragma warning disable CS0612 // Type or member is obsolete
|
||||
[Obsolete]
|
||||
public IReadOnlyList<OrderRecordTuplePredicateAnimations> predicate_animations => _predicate_animations ?? (_predicate_animations = new List<OrderRecordTuplePredicateAnimations>());
|
||||
public OrderBookGridSettings grid_settings_wrapper() {
|
||||
if (use_defaults)
|
||||
return null;
|
||||
if (orderbook_grid == null) {
|
||||
if (grid == null)
|
||||
grid = _default_general_grid;
|
||||
orderbook_grid = new OrderBookGridSettings(grid.Clone(), _predicate_animations);
|
||||
}
|
||||
return orderbook_grid;
|
||||
}
|
||||
#pragma warning restore CS0612 // Type or member is obsolete
|
||||
|
||||
public static OrderBookStartupData Default =>
|
||||
new OrderBookStartupData(
|
||||
new FilterStates { statuses = new[] { OrderStatus.Working } },
|
||||
DefaultGrid,
|
||||
_default_show_right_panel,
|
||||
_default_right_panel_width,
|
||||
_default_fills_height,
|
||||
use_defaults: true) {
|
||||
show_order_params = _default_show_order_params,
|
||||
order_params_height = _default_order_params_height,
|
||||
fills_settings = DefaultFillsGrid
|
||||
};
|
||||
|
||||
public static OrderBookGridSettings DefaultGrid => new OrderBookGridSettings(_default_general_grid) {
|
||||
algo_tree_display_mode = AlgoTreeDisplayMode.RootAndLeafNodes
|
||||
};
|
||||
|
||||
public static OrderBookFillsGridSettings DefaultFillsGrid {
|
||||
get {
|
||||
var default_fills = FillsSettings.Default();
|
||||
var rv = new OrderBookFillsGridSettings {
|
||||
sorting_criteria = default_fills.sorting_criteria.ToList(),
|
||||
filters = default_fills.filters,
|
||||
general = FillsSettings.MiniGridSettings
|
||||
};
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
static GridSettings _default_general_grid => GridSettings.Default.with(DefaultColumns.standard, DefaultColumns.sorting_criteria);
|
||||
public static OrderBookGridSettings default_mini_grid => new OrderBookGridSettings(GridSettings.Default.with(DefaultColumns.mini, DefaultColumns.sorting_criteria)) {
|
||||
algo_tree_display_mode = AlgoTreeDisplayMode.RootAndLeafNodes
|
||||
};
|
||||
|
||||
static class DefaultColumns {
|
||||
// standard
|
||||
public static GridColumnSettings security => new GridColumnSettings(Columns.Security.ToString(), DIYGrid.DefaultColumnHeaders.Security, width: DIYGrid.DefaultColumnWidths.Security);
|
||||
public static GridColumnSettings symbol => new GridColumnSettings(Columns.Symbol.ToString(), DIYGrid.DefaultColumnHeaders.Symbol, width: DIYGrid.DefaultColumnWidths.Symbol, horizontal_alignment: HorizontalAlignment.Left);
|
||||
public static GridColumnSettings time_created => new GridColumnSettings(Columns.Time.ToString(), "Time", width: DIYGrid.DefaultColumnWidths.Time, horizontal_alignment: HorizontalAlignment.Left);
|
||||
public static GridColumnSettings last_change => new GridColumnSettings(Columns.LastChange.ToString(), "Last Change", width: DIYGrid.DefaultColumnWidths.Time, horizontal_alignment: HorizontalAlignment.Left);
|
||||
public static GridColumnSettings user => new GridColumnSettings(Columns.User.ToString(), DIYGrid.DefaultColumnHeaders.User, width: DIYGrid.DefaultColumnWidths.User, horizontal_alignment: HorizontalAlignment.Left);
|
||||
public static GridColumnSettings account => new GridColumnSettings(Columns.Account.ToString(), DIYGrid.DefaultColumnHeaders.Account, width: DIYGrid.DefaultColumnWidths.Account, horizontal_alignment: HorizontalAlignment.Left);
|
||||
public static GridColumnSettings exchange => new GridColumnSettings(Columns.Exchange.ToString(), DIYGrid.DefaultColumnHeaders.Exchange, width: DIYGrid.DefaultColumnWidths.Exchange, horizontal_alignment: HorizontalAlignment.Left);
|
||||
public static GridColumnSettings time_in_force => new GridColumnSettings(Columns.TimeInForce.ToString(), DIYGrid.DefaultColumnHeaders.TimeInForce, width: DIYGrid.DefaultColumnWidths.TimeInForce);
|
||||
public static GridColumnSettings order_type => new GridColumnSettings(Columns.OrderType.ToString(), DIYGrid.DefaultColumnHeaders.OrderType, width: DIYGrid.DefaultColumnWidths.OrderType);
|
||||
public static GridColumnSettings root_algo_type => new GridColumnSettings(Columns.RootAlgoType.ToString(), DIYGrid.DefaultColumnHeaders.RootAlgoType, width: DIYGrid.DefaultColumnWidths.OrderType);
|
||||
public static GridColumnSettings sub_type => new GridColumnSettings(Columns.SubType.ToString(), "Sub Type", width: 80, horizontal_alignment: HorizontalAlignment.Left);
|
||||
|
||||
public static GridColumnSettings side => new(
|
||||
Columns.Side.ToString(), ColHeader.Side,
|
||||
width: ColWidth.Side,
|
||||
horizontal_alignment: HorizontalAlignment.Left);
|
||||
|
||||
public static GridColumnSettings quantity => new GridColumnSettings(Columns.Size.ToString(), "Qty", width: DIYGrid.DefaultColumnWidths.Qty, horizontal_alignment: HorizontalAlignment.Right);
|
||||
public static GridColumnSettings notional_value => new GridColumnSettings(Columns.NotionalValue.ToString(), DIYGrid.DefaultColumnHeaders.NotionalValue, width: DIYGrid.DefaultColumnWidths.NotionalValue, horizontal_alignment: HorizontalAlignment.Right);
|
||||
public static GridColumnSettings price => new GridColumnSettings(Columns.Price.ToString(), DIYGrid.DefaultColumnHeaders.Price, width: DIYGrid.DefaultColumnWidths.Price, horizontal_alignment: HorizontalAlignment.Right);
|
||||
|
||||
public static GridColumnSettings filled_quantity => new(
|
||||
Columns.Filled.ToString(),
|
||||
ColHeader.FilledQuantity,
|
||||
width: ColWidth.Filled,
|
||||
horizontal_alignment: HorizontalAlignment.Right);
|
||||
|
||||
public static GridColumnSettings filled_notional_value => new(
|
||||
Columns.FilledNotionalValue.ToString(),
|
||||
ColHeader.FilledNotionalValue,
|
||||
width: ColWidth.FilledNotionalValue,
|
||||
horizontal_alignment: HorizontalAlignment.Right);
|
||||
|
||||
public static GridColumnSettings avg_price => new(
|
||||
Columns.AvgFilled.ToString(),
|
||||
ColHeader.AvgFilled,
|
||||
width: ColWidth.AveragePrice,
|
||||
horizontal_alignment: HorizontalAlignment.Right);
|
||||
|
||||
public static GridColumnSettings last_price => new GridColumnSettings(Columns.LastPrice.ToString(), "Last Filled", width: DIYGrid.DefaultColumnWidths.LastPrice, horizontal_alignment: HorizontalAlignment.Right);
|
||||
public static GridColumnSettings status => new GridColumnSettings(Columns.Status.ToString(), "Status", width: 80, horizontal_alignment: HorizontalAlignment.Left);
|
||||
public static GridColumnSettings last_message => new GridColumnSettings(Columns.LastMessage.ToString(), "Last Message", width: DIYGrid.DefaultColumnWidths.LastMessage, horizontal_alignment: HorizontalAlignment.Left);
|
||||
public static GridColumnSettings security_key => new GridColumnSettings(Columns.SecurityKey.ToString(), DIYGrid.DefaultColumnHeaders.SecurityKey, is_visible: false, width: DIYGrid.DefaultColumnWidths.SecurityKey, horizontal_alignment: HorizontalAlignment.Left);
|
||||
public static GridColumnSettings order_key => new GridColumnSettings(Columns.OrderKey.ToString(), DIYGrid.DefaultColumnHeaders.OrderKey, width: DIYGrid.DefaultColumnWidths.OrderKey, horizontal_alignment: HorizontalAlignment.Left);
|
||||
public static GridColumnSettings parent_key => new GridColumnSettings(Columns.ParentKey.ToString(), "Parent Key", width: DIYGrid.DefaultColumnWidths.ParentKey, horizontal_alignment: HorizontalAlignment.Left);
|
||||
public static GridColumnSettings attached_target_order_key => new GridColumnSettings(Columns.AttachedTargetOrderKey.ToString(), DIYGrid.DefaultColumnHeaders.AttachedTargetOrderKey, width: DIYGrid.DefaultColumnWidths.ParentKey, horizontal_alignment: HorizontalAlignment.Left);
|
||||
public static GridColumnSettings piq => new GridColumnSettings(Columns.Piq.ToString(), "PIQ", width: DIYGrid.DefaultColumnWidths.Qty, horizontal_alignment: HorizontalAlignment.Right);
|
||||
// editable
|
||||
public static GridColumnSettings cancel => new GridColumnSettings(Columns.Cancel.ToString(), DIYGrid.DefaultColumnHeaders.Cancel, width: DIYGrid.DefaultColumnWidths.MiniOrderBook.Cancel);
|
||||
public static GridColumnSettings attach_detach => new GridColumnSettings(Columns.Attach.ToString(), DIYGrid.DefaultColumnHeaders.AttachDetach, width: DIYGrid.DefaultColumnWidths.MiniOrderBook.AttachDetach);
|
||||
public static GridColumnSettings update => new GridColumnSettings(Columns.Update.ToString(), DIYGrid.DefaultColumnHeaders.Update, width: DIYGrid.DefaultColumnWidths.MiniOrderBook.Update);
|
||||
public static GridColumnSettings edit_quantity => new GridColumnSettings(Columns.EditableQty.ToString(), DIYGrid.DefaultColumnHeaders.Qty, width: DIYGrid.DefaultColumnWidths.MiniOrderBook.EditableSize);
|
||||
public static GridColumnSettings edit_price => new GridColumnSettings(Columns.EditablePrice.ToString(), DIYGrid.DefaultColumnHeaders.Price, width: DIYGrid.DefaultColumnWidths.MiniOrderBook.EditablePrice);
|
||||
public static GridColumnSettings commit => new GridColumnSettings(Columns.Commit.ToString(), width: DIYGrid.DefaultColumnWidths.MiniOrderBook.Commit);
|
||||
|
||||
public static IEnumerable<SortingCriteria> sorting_criteria {
|
||||
get {
|
||||
yield return new SortingCriteria(time_created.id, false);
|
||||
}
|
||||
}
|
||||
|
||||
public static IEnumerable<GridColumnSettings> standard {
|
||||
get {
|
||||
yield return security;
|
||||
yield return symbol;
|
||||
yield return time_created;
|
||||
yield return last_change;
|
||||
yield return user;
|
||||
yield return account;
|
||||
yield return exchange;
|
||||
yield return time_in_force;
|
||||
yield return order_type;
|
||||
yield return root_algo_type;
|
||||
yield return sub_type;
|
||||
yield return side;
|
||||
yield return quantity;
|
||||
yield return price;
|
||||
yield return piq;
|
||||
yield return filled_quantity;
|
||||
yield return avg_price;
|
||||
yield return last_price;
|
||||
yield return status;
|
||||
yield return last_message;
|
||||
yield return security_key;
|
||||
yield return order_key;
|
||||
yield return parent_key;
|
||||
yield return attached_target_order_key;
|
||||
}
|
||||
}
|
||||
|
||||
public static IEnumerable<GridColumnSettings> editable {
|
||||
get {
|
||||
yield return cancel;
|
||||
yield return update;
|
||||
yield return edit_quantity;
|
||||
yield return edit_price;
|
||||
yield return commit;
|
||||
yield return attach_detach;
|
||||
}
|
||||
}
|
||||
|
||||
private static HashSet<GridColumnSettings> _mini_mode_columns_to_hide = new HashSet<GridColumnSettings> {
|
||||
symbol, last_change, exchange, sub_type, quantity, price, avg_price, last_price, status, last_message, security_key, order_key, parent_key, attached_target_order_key
|
||||
};
|
||||
|
||||
public static IEnumerable<GridColumnSettings> mini {
|
||||
get {
|
||||
foreach (var col_setting in editable)
|
||||
yield return col_setting;
|
||||
foreach (var col_setting in standard) {
|
||||
if (_mini_mode_columns_to_hide.Contains(col_setting)) {
|
||||
yield return col_setting.with(is_visible: false);
|
||||
} else {
|
||||
yield return col_setting;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,107 +1,109 @@
|
||||
using bts.api.orderman;
|
||||
|
||||
using mbts.ui;
|
||||
using mbts.ui.controls;
|
||||
using mbts.ui.converters;
|
||||
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Data;
|
||||
using System.Windows.Media;
|
||||
|
||||
namespace mbts.spark {
|
||||
internal partial class OrderBookGridView : DIYGrid {
|
||||
public const string
|
||||
SubTypeFilterTemplateKey = "SubTypeFilterTemplate",
|
||||
ExpandCollapseToggleStyleKey = "ExpandCollapseToggleStyle",
|
||||
RowStyleKey = "RowStyle",
|
||||
RowTemplateKey = "RowTemplateKey",
|
||||
CVRowStyleKey = "CVRowStyle",
|
||||
CVRowTemplateKey = "CVRowTemplateKey";
|
||||
|
||||
// IMPORTANT: these enums are stored as strings in the general-purpose GridColumnSettings proto, and are used to ID THE COLUMNS!
|
||||
// Do NOT RENAME them! However, if you must, be sure to write an IShellWindowMigrationRule or something to account for backwards compatability.
|
||||
public enum ColumnKeys {
|
||||
Security = 1,
|
||||
Symbol = 1 << 1,
|
||||
Time = 1 << 2,
|
||||
LastChange = 1 << 3,
|
||||
User = 1 << 4,
|
||||
Account = 1 << 5,
|
||||
Exchange = 1 << 6,
|
||||
TimeInForce = 1 << 7,
|
||||
OrderType = 1 << 8,
|
||||
SubType = 1 << 9,
|
||||
Side = 1 << 10,
|
||||
Size = 1 << 11,
|
||||
Price = 1 << 12,
|
||||
Filled = 1 << 13,
|
||||
AvgFilled = 1 << 14,
|
||||
LastPrice = 1 << 15,
|
||||
Status = 1 << 16,
|
||||
LastMessage = 1 << 17,
|
||||
SecurityKey = 1 << 18,
|
||||
OrderKey = 1 << 19,
|
||||
ParentKey = 1 << 20,
|
||||
AttachedTargetOrderKey = 1 << 21,
|
||||
Cancel = 1 << 22,
|
||||
EditableQty = 1 << 23,
|
||||
EditablePrice = 1 << 24,
|
||||
Attach = 1 << 25,
|
||||
Detach = 1 << 26,
|
||||
Update = 1 << 27,
|
||||
Commit = 1 << 28,
|
||||
RootAlgoType = 1 << 29,
|
||||
Piq = 1 << 30,
|
||||
Check = 1 << 31
|
||||
}
|
||||
|
||||
public OrderBookGridView() {
|
||||
InitializeComponent();
|
||||
var row_template_factory = new FrameworkElementFactory(typeof(OrderBookRowView));
|
||||
row_template_factory.SetValue(OrderBookRowView.BackgroundProperty, RowBackgroundHelper.row_background_binding);
|
||||
RowTemplate = new DataTemplate { VisualTree = row_template_factory };
|
||||
}
|
||||
|
||||
static class RowBackgroundHelper {
|
||||
public static readonly BindingBase row_background_binding;
|
||||
|
||||
static RowBackgroundHelper() {
|
||||
var bg_binding = new MultiBinding();
|
||||
bg_binding.Bindings.Add(new Binding(nameof(OrderBookRowVM.status)));
|
||||
bg_binding.Bindings.Add(new Binding(nameof(ListBoxItem.IsSelected)) { RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(ListBoxItem), 1) });
|
||||
double overlay_opacity = config.Configuration.get<bts.config.OrderBookOptions>().StatusColorOpacityWhenSelected;
|
||||
if (overlay_opacity > 0.1 && overlay_opacity < 0.9) {
|
||||
bg_binding.Converter = ValueConverter.create<OrderStatus, bool, Brush>((status, is_selected) => {
|
||||
var color = _status_color(status);
|
||||
return color.HasValue ? new SolidColorBrush(color.Value) { Opacity = is_selected ? overlay_opacity : 1.0 } : null;
|
||||
});
|
||||
} else {
|
||||
bg_binding.Converter = ValueConverter.create<OrderStatus, bool, Brush>((status, is_selected) => {
|
||||
if (is_selected)
|
||||
return null;
|
||||
var color = _status_color(status);
|
||||
return color.HasValue ? new SolidColorBrush(color.Value) : null;
|
||||
});
|
||||
}
|
||||
row_background_binding = bg_binding;
|
||||
}
|
||||
|
||||
static Color? _status_color(OrderStatus status) {
|
||||
switch (status) {
|
||||
case OrderStatus.Canceled: return BTSColors.Orders.canceled_order_background;
|
||||
case OrderStatus.Completed: return BTSColors.Orders.completed_order_background;
|
||||
case OrderStatus.Disconnected:
|
||||
case OrderStatus.RejectedBySpark:
|
||||
case OrderStatus.Error:
|
||||
return BTSColors.Orders.error_order_background;
|
||||
default: return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class OrderBookRowView : DIYGrid.HighPerformanceRow<OrderBookRowVM> {
|
||||
public OrderBookRowView() : base() { }
|
||||
}
|
||||
}
|
||||
using bts.api.orderman;
|
||||
|
||||
using mbts.ui;
|
||||
using mbts.ui.controls;
|
||||
using mbts.ui.converters;
|
||||
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Data;
|
||||
using System.Windows.Media;
|
||||
|
||||
namespace mbts.spark {
|
||||
internal partial class OrderBookGridView : DIYGrid {
|
||||
public const string
|
||||
SubTypeFilterTemplateKey = "SubTypeFilterTemplate",
|
||||
ExpandCollapseToggleStyleKey = "ExpandCollapseToggleStyle",
|
||||
RowStyleKey = "RowStyle",
|
||||
RowTemplateKey = "RowTemplateKey",
|
||||
CVRowStyleKey = "CVRowStyle",
|
||||
CVRowTemplateKey = "CVRowTemplateKey";
|
||||
|
||||
// IMPORTANT: these enums are stored as strings in the general-purpose GridColumnSettings proto, and are used to ID THE COLUMNS!
|
||||
// Do NOT RENAME them! However, if you must, be sure to write an IShellWindowMigrationRule or something to account for backwards compatability.
|
||||
public enum ColumnKeys : long {
|
||||
Security = 1,
|
||||
Symbol = 1 << 1,
|
||||
Time = 1 << 2,
|
||||
LastChange = 1 << 3,
|
||||
User = 1 << 4,
|
||||
Account = 1 << 5,
|
||||
Exchange = 1 << 6,
|
||||
TimeInForce = 1 << 7,
|
||||
OrderType = 1 << 8,
|
||||
SubType = 1 << 9,
|
||||
Side = 1 << 10,
|
||||
Size = 1 << 11,
|
||||
Price = 1 << 12,
|
||||
Filled = 1 << 13,
|
||||
AvgFilled = 1 << 14,
|
||||
LastPrice = 1 << 15,
|
||||
Status = 1 << 16,
|
||||
LastMessage = 1 << 17,
|
||||
SecurityKey = 1 << 18,
|
||||
OrderKey = 1 << 19,
|
||||
ParentKey = 1 << 20,
|
||||
AttachedTargetOrderKey = 1 << 21,
|
||||
Cancel = 1 << 22,
|
||||
EditableQty = 1 << 23,
|
||||
EditablePrice = 1 << 24,
|
||||
Attach = 1 << 25,
|
||||
Detach = 1 << 26,
|
||||
Update = 1 << 27,
|
||||
Commit = 1 << 28,
|
||||
RootAlgoType = 1 << 29,
|
||||
Piq = 1 << 30,
|
||||
Check = 1 << 31,
|
||||
NotionalValue = 1L << 32,
|
||||
FilledNotionalValue = 1L << 33,
|
||||
}
|
||||
|
||||
public OrderBookGridView() {
|
||||
InitializeComponent();
|
||||
var row_template_factory = new FrameworkElementFactory(typeof(OrderBookRowView));
|
||||
row_template_factory.SetValue(OrderBookRowView.BackgroundProperty, RowBackgroundHelper.row_background_binding);
|
||||
RowTemplate = new DataTemplate { VisualTree = row_template_factory };
|
||||
}
|
||||
|
||||
static class RowBackgroundHelper {
|
||||
public static readonly BindingBase row_background_binding;
|
||||
|
||||
static RowBackgroundHelper() {
|
||||
var bg_binding = new MultiBinding();
|
||||
bg_binding.Bindings.Add(new Binding(nameof(OrderBookRowVM.status)));
|
||||
bg_binding.Bindings.Add(new Binding(nameof(ListBoxItem.IsSelected)) { RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(ListBoxItem), 1) });
|
||||
double overlay_opacity = config.Configuration.get<bts.config.OrderBookOptions>().StatusColorOpacityWhenSelected;
|
||||
if (overlay_opacity > 0.1 && overlay_opacity < 0.9) {
|
||||
bg_binding.Converter = ValueConverter.create<OrderStatus, bool, Brush>((status, is_selected) => {
|
||||
var color = _status_color(status);
|
||||
return color.HasValue ? new SolidColorBrush(color.Value) { Opacity = is_selected ? overlay_opacity : 1.0 } : null;
|
||||
});
|
||||
} else {
|
||||
bg_binding.Converter = ValueConverter.create<OrderStatus, bool, Brush>((status, is_selected) => {
|
||||
if (is_selected)
|
||||
return null;
|
||||
var color = _status_color(status);
|
||||
return color.HasValue ? new SolidColorBrush(color.Value) : null;
|
||||
});
|
||||
}
|
||||
row_background_binding = bg_binding;
|
||||
}
|
||||
|
||||
static Color? _status_color(OrderStatus status) {
|
||||
switch (status) {
|
||||
case OrderStatus.Canceled: return BTSColors.Orders.canceled_order_background;
|
||||
case OrderStatus.Completed: return BTSColors.Orders.completed_order_background;
|
||||
case OrderStatus.Disconnected:
|
||||
case OrderStatus.RejectedBySpark:
|
||||
case OrderStatus.Error:
|
||||
return BTSColors.Orders.error_order_background;
|
||||
default: return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class OrderBookRowView : DIYGrid.HighPerformanceRow<OrderBookRowVM> {
|
||||
public OrderBookRowView() : base() { }
|
||||
}
|
||||
}
|
||||
@@ -1,378 +1,380 @@
|
||||
using bts.api;
|
||||
using bts.utils;
|
||||
|
||||
using mbts;
|
||||
using mbts.spark;
|
||||
using mbts.ui;
|
||||
using mbts.ui.shell;
|
||||
|
||||
using Spark.Core.Collections;
|
||||
using Spark.Core.Data;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Linq;
|
||||
using System.Reactive.Linq;
|
||||
|
||||
using ColumnKeys = bts.shell.pnl.PNLView.Columns;
|
||||
using PNLKey = bts.AccountSecurityKey;
|
||||
|
||||
namespace bts.shell.pnl {
|
||||
class PNLRowVM : DisposableViewModelBase, IEquatable<PNLRowVM> {
|
||||
public static readonly IComparer<PNLRowVM> key_comparer = Comparer<PNLKey>.Default.wrap<PNLKey, PNLRowVM>(r => r.key);
|
||||
static readonly Func<bool, string> _fmt_has_day_trades = hdt => hdt ? "Yes" : "No";
|
||||
//
|
||||
readonly PNLCellIndexBuilder<PNLRowData> _cell_map = new PNLCellIndexBuilder<PNLRowData>();
|
||||
readonly PNLCellEmbeddedAnimationModel<PNLRowData> _animator;
|
||||
readonly ISecurityMetadataStore _sec_meta_store;
|
||||
bool _refresh_normalized_data = true, _is_selected;
|
||||
PNLNormalizedData _normalized_data;
|
||||
api.domain.TickDisplaySettingsImmutable? _quantity_display_backing;
|
||||
|
||||
public PNLRowVM(
|
||||
PNLRowData data,
|
||||
ISecurityMetadataStore sec_meta_store,
|
||||
ImmutableDictionary<ColumnKeys, PNLCellAppearance> default_appearances,
|
||||
IReadOnlyList<IPNLAnimationTrigger<PNLRowData>> triggers,
|
||||
Func<double, string> fmt_fill_price,
|
||||
Func<double, string> fmt_market_price) {
|
||||
//
|
||||
this.data = data;
|
||||
_sec_meta_store = sec_meta_store;
|
||||
_animator = new PNLCellEmbeddedAnimationModel<PNLRowData>(default_appearances, triggers);
|
||||
_animator.on_row(data);
|
||||
pnl_price = _cell_map.add_price_double(ColumnKeys.PNLPrice, d => d.pnl_price);
|
||||
account_cell = _cell_map.add(ColumnKeys.Account, d => d.account);
|
||||
security_type_cell = _cell_map.add(ColumnKeys.SecurityType, d => d.security?.ContractCase ?? Security.ContractOneofCase.None);
|
||||
exchange_cell = _cell_map.add(ColumnKeys.Exchange, d => d.security?.Exchange ?? Venue.Unset);
|
||||
expiration_cell = _cell_map.add(
|
||||
ColumnKeys.Expiration,
|
||||
d => d.expiration,
|
||||
e => SecurityFormatter.format_expiration(e));
|
||||
symbol_cell = _cell_map.add(ColumnKeys.Symbol, d => d.symbol);
|
||||
security_cell = _cell_map.add(
|
||||
ColumnKeys.Security,
|
||||
d => d.security,
|
||||
null,
|
||||
EqualityComparer<SecurityImmutable?>.Default,
|
||||
Comparer<SecurityImmutable?>.Default);
|
||||
security_key = _cell_map.add(ColumnKeys.SecurityKey, d => d.key.security_id);
|
||||
cpp = _cell_map.add(ColumnKeys.CPP, d => d.effective_cpp);
|
||||
// sod
|
||||
sod_pnl = _cell_map.add_pnl_double(ColumnKeys.SODPNL, d => d.sod_pnl);
|
||||
sod_net_pos = _cell_map.add_net_pos(
|
||||
ColumnKeys.SODNetPos, d => d.display_sod_net_pos, fmt_qty);
|
||||
sod_long_pos = _cell_map.add_long_pos(
|
||||
ColumnKeys.SODLongPos, d => d.display_sod_long_pos, fmt_qty);
|
||||
sod_long_avg_price = _cell_map.add_long_price(ColumnKeys.SODLongAvgPrice, d => d.sod_long_avg_price);
|
||||
sod_short_pos = _cell_map.add_short_pos(
|
||||
ColumnKeys.SODShortPos, d => d.display_sod_short_pos, fmt_qty);
|
||||
sod_short_avg_price = _cell_map.add_short_price(ColumnKeys.SODShortAvgPrice, d => d.sod_short_avg_price);
|
||||
sod_delta = _cell_map.add_pnl_double(ColumnKeys.SODDelta, d => d.sod_delta, fmt_greek);
|
||||
sod_gamma = _cell_map.add_pnl_double(ColumnKeys.SODGamma, d => d.sod_gamma, fmt_greek);
|
||||
sod_vega = _cell_map.add_pnl_double(ColumnKeys.SODVega, d => d.sod_vega, fmt_greek);
|
||||
sod_theta = _cell_map.add_pnl_double(ColumnKeys.SODTheta, d => d.sod_theta, fmt_greek);
|
||||
|
||||
// day
|
||||
day_pnl = _cell_map.add_pnl_double(ColumnKeys.DayPNL, d => d.day_pnl);
|
||||
day_net_pos = _cell_map.add_net_pos(
|
||||
ColumnKeys.DayNetPos, d => d.display_day_net_pos, fmt_qty);
|
||||
day_long_pos = _cell_map.add_long_pos(
|
||||
ColumnKeys.DayLongPos, d => d.display_day_long_pos, fmt_qty);
|
||||
day_long_avg_price = _cell_map.add_long_price(ColumnKeys.DayLongAvgPrice, d => d.day_long_avg_price);
|
||||
day_short_pos = _cell_map.add_short_pos(
|
||||
ColumnKeys.DayShortPos, d => d.display_day_short_pos, fmt_qty);
|
||||
day_short_avg_price = _cell_map.add_short_price(ColumnKeys.DayShortAvgPrice, d => d.day_short_avg_price);
|
||||
day_delta = _cell_map.add_pnl_double(ColumnKeys.DayDelta, d => d.day_delta, fmt_greek);
|
||||
day_gamma = _cell_map.add_pnl_double(ColumnKeys.DayGamma, d => d.day_gamma, fmt_greek);
|
||||
day_vega = _cell_map.add_pnl_double(ColumnKeys.DayVega, d => d.day_vega, fmt_greek);
|
||||
day_theta = _cell_map.add_pnl_double(ColumnKeys.DayTheta, d => d.day_theta, fmt_greek);
|
||||
has_day_trades = _cell_map.add(ColumnKeys.HasDayTrades, d => d.has_day_trades, _fmt_has_day_trades);
|
||||
// working
|
||||
working_net_pos = _cell_map.add_net_pos(
|
||||
ColumnKeys.WorkingNetPos, d => d.display_working_net_pos, fmt_qty);
|
||||
var qty_display = _sec_meta_store.get_qty_display(security);
|
||||
working_long_pos = _cell_map.add_working_long(
|
||||
ColumnKeys.WorkingLongPos,
|
||||
d => d.working.long_data,
|
||||
d => d.bid_price,
|
||||
qty_display);
|
||||
working_long_avg_price = _cell_map.add_long_price(ColumnKeys.WorkingLongAvgPrice, d => d.working_long_avg_price);
|
||||
working_short_pos = _cell_map.add_working_short(
|
||||
ColumnKeys.WorkingShortPos, r => r.working.short_data, r => r.ask_price, qty_display);
|
||||
working_short_avg_price = _cell_map.add_short_price(ColumnKeys.WorkingShortAvgPrice, d => d.working_short_avg_price);
|
||||
// total
|
||||
total_pnl = _cell_map.add_pnl_double(ColumnKeys.TotalPNL, d => d.total_pnl);
|
||||
total_net_pos = _cell_map.add_net_pos(
|
||||
ColumnKeys.TotalNetPos, d => d.display_total_net_pos, fmt_qty);
|
||||
total_long_pos = _cell_map.add_long_pos(
|
||||
ColumnKeys.TotalLongPos, d => d.display_total_long_pos, fmt_qty);
|
||||
total_long_avg_price = _cell_map.add_long_price(ColumnKeys.TotalLongAvgPrice, d => d.total_long_avg_price);
|
||||
total_short_pos = _cell_map.add_short_pos(
|
||||
ColumnKeys.TotalShortPos, d => d.display_total_short_pos, fmt_qty);
|
||||
total_short_avg_price = _cell_map.add_short_price(ColumnKeys.TotalShortAvgPrice, d => d.total_short_avg_price);
|
||||
total_delta = _cell_map.add_pnl_double(
|
||||
ColumnKeys.TotalDelta, d => d.total_delta, fmt_greek);
|
||||
total_gamma = _cell_map.add_pnl_double(
|
||||
ColumnKeys.TotalGamma, d => d.total_gamma, fmt_greek);
|
||||
total_vega = _cell_map.add_pnl_double(ColumnKeys.TotalVega, d => d.total_vega, fmt_greek);
|
||||
total_theta = _cell_map.add_pnl_double(
|
||||
ColumnKeys.TotalTheta, d => d.total_theta, fmt_greek);
|
||||
|
||||
// market
|
||||
bid_price = _cell_map.add_price_double(ColumnKeys.BidPrice, d => d.bid_price);
|
||||
ask_price = _cell_map.add_price_double(ColumnKeys.AskPrice, d => d.ask_price);
|
||||
mid_price = _cell_map.add_price_double(ColumnKeys.MidPrice, d => d.mid_price);
|
||||
last_price = _cell_map.add_price_double(ColumnKeys.LastPrice, d => d.last_price);
|
||||
settlement_price = _cell_map.add_price_double(ColumnKeys.SettlementPrice, d => d.settlement_price);
|
||||
|
||||
_set_price_formatting_delegates(fmt_fill_price, fmt_market_price);
|
||||
_cell_map.on_row(data);
|
||||
_cell_map.set_appearances(_animator.cell_appearances);
|
||||
_dispose = _cell_map.animation_model.OnDispatcher().Subscribe(new_animations => {
|
||||
animations = new_animations.Select(kv => new mbts.ui.spark.CellAnimation(kv.Key.column, kv.Key.type, kv.Value.color.to_brush())).ToList();
|
||||
RaisePropertyChanged(nameof(animations));
|
||||
});
|
||||
|
||||
static string fmt_greek(double v) =>
|
||||
v.is_nan() ? "-" : v.ToString("#,0.00");
|
||||
|
||||
string fmt_qty(double v) =>
|
||||
quantity_display.format(v);
|
||||
}
|
||||
|
||||
// events
|
||||
public event Action<PNLNormalizedData> updated;
|
||||
public event Action<bool> is_selected_changed;
|
||||
|
||||
// data
|
||||
public PNLKey key => data.key;
|
||||
public PNLRowData data { get; private set; }
|
||||
public PNLNormalizedData normalized_data {
|
||||
get {
|
||||
if (_refresh_normalized_data) {
|
||||
_refresh_normalized_data = false;
|
||||
// Only count outrights (spread positions should be normalized into outrights)
|
||||
var sec_type = security?.ContractCase ?? Security.ContractOneofCase.None;
|
||||
var (
|
||||
sod_long,
|
||||
sod_short,
|
||||
sod_delta,
|
||||
sod_gamma,
|
||||
sod_vega,
|
||||
sod_theta,
|
||||
day_long,
|
||||
day_short,
|
||||
day_delta,
|
||||
day_gamma,
|
||||
day_vega,
|
||||
day_theta) = sec_type switch {
|
||||
Security.ContractOneofCase.Future or Security.ContractOneofCase.Option => (
|
||||
data.sod.long_pos.round_to_decimal(),
|
||||
data.sod.short_pos.round_to_decimal(),
|
||||
data.sod.delta.round_to_decimal(),
|
||||
data.sod.gamma.round_to_decimal(),
|
||||
data.sod.vega.round_to_decimal(),
|
||||
data.sod.theta.round_to_decimal(),
|
||||
data.day.long_pos.round_to_decimal(),
|
||||
data.day.short_pos.round_to_decimal(),
|
||||
data.day.delta.round_to_decimal(),
|
||||
data.day.gamma.round_to_decimal(),
|
||||
data.day.vega.round_to_decimal(),
|
||||
data.day.theta.round_to_decimal()),
|
||||
_ => (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
|
||||
};
|
||||
|
||||
var working_long = data.working_long_pos.round_to_decimal();
|
||||
var working_short = data.working_short_pos.round_to_decimal();
|
||||
|
||||
var nd = new PNLNormalizedData() {
|
||||
// sod
|
||||
sod_pnl = sod_pnl.value.round_to_decimal(),
|
||||
sod_long_pos = sod_long,
|
||||
sod_short_pos = sod_short,
|
||||
sod_delta = sod_delta,
|
||||
sod_gamma = sod_gamma,
|
||||
sod_vega = sod_vega,
|
||||
sod_theta = sod_theta,
|
||||
display_sod_long_pos = sod_long,
|
||||
display_sod_short_pos = sod_short,
|
||||
|
||||
// day
|
||||
day_pnl = day_pnl.value.round_to_decimal(),
|
||||
day_long_pos = day_long,
|
||||
day_short_pos = day_short,
|
||||
day_delta = day_delta,
|
||||
day_gamma = day_gamma,
|
||||
day_vega = day_vega,
|
||||
day_theta = day_theta,
|
||||
display_day_long_pos = day_long,
|
||||
display_day_short_pos = day_short,
|
||||
|
||||
// working
|
||||
working_long_pos = working_long,
|
||||
working_short_pos = working_short,
|
||||
display_working_long_pos = working_long,
|
||||
display_working_short_pos = working_short
|
||||
};
|
||||
if (_normalized_data != nd) {
|
||||
_normalized_data = nd;
|
||||
updated?.Invoke(nd);
|
||||
}
|
||||
}
|
||||
return _normalized_data;
|
||||
}
|
||||
}
|
||||
// security
|
||||
public string account_name => data.account_name;
|
||||
public Venue exchange => security?.Exchange ?? Venue.Unset;
|
||||
public string symbol => security.symbol_or_default();
|
||||
public DateTime expiration => data.expiration;
|
||||
public Security.ContractOneofCase security_type => security?.ContractCase ?? Security.ContractOneofCase.None;
|
||||
public SecurityImmutable? security => data.security;
|
||||
// cells
|
||||
public PNLCellVM<PNLRowData, AccountTuple> account_cell { get; }
|
||||
public PNLCellVM<PNLRowData, Venue> exchange_cell { get; }
|
||||
public PNLCellVM<PNLRowData, DateTime> expiration_cell { get; }
|
||||
public PNLCellVM<PNLRowData, string> symbol_cell { get; }
|
||||
public PNLCellVM<PNLRowData, Security.ContractOneofCase> security_type_cell { get; }
|
||||
public PNLCellVM<PNLRowData, SecurityImmutable?> security_cell { get; }
|
||||
public PNLCellVM<PNLRowData, uint> security_key { get; }
|
||||
public PNLCellVM<PNLRowData, double> cpp { get; }
|
||||
// sod
|
||||
public PNLCellVM<PNLRowData, double> sod_pnl { get; }
|
||||
public PNLCellVM<PNLRowData, double> sod_net_pos { get; }
|
||||
public PNLCellVM<PNLRowData, double> sod_long_pos { get; }
|
||||
public PNLCellVM<PNLRowData, double> sod_long_avg_price { get; }
|
||||
public PNLCellVM<PNLRowData, double> sod_short_pos { get; }
|
||||
public PNLCellVM<PNLRowData, double> sod_short_avg_price { get; }
|
||||
public PNLCellVM<PNLRowData, double> sod_delta { get; }
|
||||
public PNLCellVM<PNLRowData, double> sod_gamma { get; }
|
||||
public PNLCellVM<PNLRowData, double> sod_vega { get; }
|
||||
public PNLCellVM<PNLRowData, double> sod_theta { get; }
|
||||
|
||||
// day
|
||||
public PNLCellVM<PNLRowData, double> day_pnl { get; }
|
||||
public PNLCellVM<PNLRowData, double> day_net_pos { get; }
|
||||
public PNLCellVM<PNLRowData, double> day_long_pos { get; }
|
||||
public PNLCellVM<PNLRowData, double> day_long_avg_price { get; }
|
||||
public PNLCellVM<PNLRowData, double> day_short_pos { get; }
|
||||
public PNLCellVM<PNLRowData, double> day_short_avg_price { get; }
|
||||
public PNLCellVM<PNLRowData, double> day_delta { get; }
|
||||
public PNLCellVM<PNLRowData, double> day_gamma { get; }
|
||||
public PNLCellVM<PNLRowData, double> day_vega { get; }
|
||||
public PNLCellVM<PNLRowData, double> day_theta { get; }
|
||||
public PNLCellVM<PNLRowData, bool> has_day_trades { get; }
|
||||
// working
|
||||
public PNLCellVM<PNLRowData, double> working_net_pos { get; }
|
||||
public PNLCellVM<PNLRowData, WorkingOrderSideData> working_long_pos { get; }
|
||||
public PNLCellVM<PNLRowData, double> working_long_avg_price { get; }
|
||||
public PNLCellVM<PNLRowData, WorkingOrderSideData> working_short_pos { get; }
|
||||
public PNLCellVM<PNLRowData, double> working_short_avg_price { get; }
|
||||
// total
|
||||
public PNLCellVM<PNLRowData, double> total_pnl { get; }
|
||||
public PNLCellVM<PNLRowData, double> total_net_pos { get; }
|
||||
public PNLCellVM<PNLRowData, double> total_long_pos { get; }
|
||||
public PNLCellVM<PNLRowData, double> total_long_avg_price { get; }
|
||||
public PNLCellVM<PNLRowData, double> total_short_pos { get; }
|
||||
public PNLCellVM<PNLRowData, double> total_short_avg_price { get; }
|
||||
public PNLCellVM<PNLRowData, double> total_delta { get; }
|
||||
public PNLCellVM<PNLRowData, double> total_gamma { get; }
|
||||
public PNLCellVM<PNLRowData, double> total_vega { get; }
|
||||
public PNLCellVM<PNLRowData, double> total_theta { get; }
|
||||
|
||||
// (market)
|
||||
public PNLCellVM<PNLRowData, double> bid_price { get; }
|
||||
public PNLCellVM<PNLRowData, double> mid_price { get; }
|
||||
public PNLCellVM<PNLRowData, double> ask_price { get; }
|
||||
public PNLCellVM<PNLRowData, double> last_price { get; }
|
||||
public PNLCellVM<PNLRowData, double> settlement_price { get; }
|
||||
public PNLCellVM<PNLRowData, double> pnl_price { get; }
|
||||
|
||||
// display
|
||||
public api.domain.TickDisplaySettingsImmutable quantity_display {
|
||||
get {
|
||||
if (_quantity_display_backing.HasValue) {
|
||||
return _quantity_display_backing.Value;
|
||||
}
|
||||
if (security.HasValue) {
|
||||
_quantity_display_backing = _sec_meta_store.get_qty_display(security.Value);
|
||||
}
|
||||
return _quantity_display_backing ??= Security.Defaults.quantity_display_settingsi;
|
||||
}
|
||||
}
|
||||
|
||||
public IList<mbts.ui.spark.CellAnimation> animations { get; private set; }
|
||||
|
||||
public bool is_selected {
|
||||
get => _is_selected;
|
||||
set {
|
||||
if (Set(ref _is_selected, value))
|
||||
is_selected_changed?.Invoke(value);
|
||||
}
|
||||
}
|
||||
|
||||
IEnumerable<PNLCellVM<PNLRowData, double>> _market_price_cells {
|
||||
get {
|
||||
yield return pnl_price;
|
||||
yield return last_price;
|
||||
yield return bid_price;
|
||||
yield return mid_price;
|
||||
yield return ask_price;
|
||||
yield return settlement_price;
|
||||
}
|
||||
}
|
||||
|
||||
// includes sod, day, total, and working avg price columns
|
||||
IEnumerable<PNLCellVM<PNLRowData, double>> _fill_price_cells {
|
||||
get {
|
||||
// sod
|
||||
yield return sod_long_avg_price;
|
||||
yield return sod_short_avg_price;
|
||||
// day
|
||||
yield return day_long_avg_price;
|
||||
yield return day_short_avg_price;
|
||||
// total
|
||||
yield return total_long_avg_price;
|
||||
yield return total_short_avg_price;
|
||||
// working
|
||||
yield return working_long_avg_price;
|
||||
yield return working_short_avg_price;
|
||||
}
|
||||
}
|
||||
|
||||
public void update(PNLRowData data) {
|
||||
_quantity_display_backing = null;
|
||||
_refresh_normalized_data = true;
|
||||
this.data = data;
|
||||
_cell_map.on_row(data);
|
||||
_animator.on_row(data);
|
||||
_cell_map.set_appearances(_animator.cell_appearances);
|
||||
}
|
||||
|
||||
public void on_settings(ImmutableDictionary<ColumnKeys, PNLCellAppearance> defaults, IReadOnlyList<IPNLAnimationTrigger<PNLRowData>> triggers, Func<double, string> fmt_fill_price, Func<double, string> fmt_market_price) {
|
||||
_animator.on_settings(defaults, triggers);
|
||||
_cell_map.set_appearances(_animator.cell_appearances);
|
||||
_set_price_formatting_delegates(fmt_fill_price, fmt_market_price);
|
||||
}
|
||||
|
||||
public bool Equals(PNLRowVM other) =>
|
||||
other != null && key == other.key;
|
||||
|
||||
public override int GetHashCode() =>
|
||||
key.GetHashCode();
|
||||
|
||||
public override string ToString() =>
|
||||
$"account: {account_name} exchange: {exchange} symbol: {symbol} security: {security} buy_quantity: {day_long_pos} sell_quantity: {day_short_pos} pnl: {total_pnl}";
|
||||
|
||||
void _set_price_formatting_delegates(Func<double, string> fmt_fill_price, Func<double, string> fmt_market_price) {
|
||||
foreach (var cell in _fill_price_cells) {
|
||||
cell.format = fmt_fill_price;
|
||||
}
|
||||
foreach (var cell in _market_price_cells) {
|
||||
cell.format = fmt_market_price;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
using bts.api;
|
||||
using bts.utils;
|
||||
|
||||
using mbts;
|
||||
using mbts.spark;
|
||||
using mbts.ui;
|
||||
using mbts.ui.shell;
|
||||
|
||||
using Spark.Core.Collections;
|
||||
using Spark.Core.Data;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Linq;
|
||||
using System.Reactive.Linq;
|
||||
|
||||
using ColumnKeys = bts.shell.pnl.PNLView.Columns;
|
||||
using PNLKey = bts.AccountSecurityKey;
|
||||
|
||||
namespace bts.shell.pnl {
|
||||
class PNLRowVM : DisposableViewModelBase, IEquatable<PNLRowVM> {
|
||||
public static readonly IComparer<PNLRowVM> key_comparer = Comparer<PNLKey>.Default.wrap<PNLKey, PNLRowVM>(r => r.key);
|
||||
static readonly Func<bool, string> _fmt_has_day_trades = hdt => hdt ? "Yes" : "No";
|
||||
//
|
||||
readonly PNLCellIndexBuilder<PNLRowData> _cell_map = new PNLCellIndexBuilder<PNLRowData>();
|
||||
readonly PNLCellEmbeddedAnimationModel<PNLRowData> _animator;
|
||||
readonly ISecurityMetadataStore _sec_meta_store;
|
||||
bool _refresh_normalized_data = true, _is_selected;
|
||||
PNLNormalizedData _normalized_data;
|
||||
api.domain.TickDisplaySettingsImmutable? _quantity_display_backing;
|
||||
|
||||
public PNLRowVM(
|
||||
PNLRowData data,
|
||||
ISecurityMetadataStore sec_meta_store,
|
||||
ImmutableDictionary<ColumnKeys, PNLCellAppearance> default_appearances,
|
||||
IReadOnlyList<IPNLAnimationTrigger<PNLRowData>> triggers,
|
||||
Func<double, string> fmt_fill_price,
|
||||
Func<double, string> fmt_market_price) {
|
||||
//
|
||||
this.data = data;
|
||||
_sec_meta_store = sec_meta_store;
|
||||
_animator = new PNLCellEmbeddedAnimationModel<PNLRowData>(default_appearances, triggers);
|
||||
_animator.on_row(data);
|
||||
pnl_price = _cell_map.add_price_double(ColumnKeys.PNLPrice, d => d.pnl_price);
|
||||
account_cell = _cell_map.add(ColumnKeys.Account, d => d.account);
|
||||
security_type_cell = _cell_map.add(ColumnKeys.SecurityType, d => d.security?.ContractCase ?? Security.ContractOneofCase.None);
|
||||
exchange_cell = _cell_map.add(ColumnKeys.Exchange, d => d.security?.Exchange ?? Venue.Unset);
|
||||
expiration_cell = _cell_map.add(
|
||||
ColumnKeys.Expiration,
|
||||
d => d.expiration,
|
||||
e => e == DateTime.MinValue
|
||||
? "N/A"
|
||||
: SecurityFormatter.format_expiration(e));
|
||||
symbol_cell = _cell_map.add(ColumnKeys.Symbol, d => d.symbol);
|
||||
security_cell = _cell_map.add(
|
||||
ColumnKeys.Security,
|
||||
d => d.security,
|
||||
null,
|
||||
EqualityComparer<SecurityImmutable?>.Default,
|
||||
Comparer<SecurityImmutable?>.Default);
|
||||
security_key = _cell_map.add(ColumnKeys.SecurityKey, d => d.key.security_id);
|
||||
cpp = _cell_map.add(ColumnKeys.CPP, d => d.effective_cpp);
|
||||
// sod
|
||||
sod_pnl = _cell_map.add_pnl_double(ColumnKeys.SODPNL, d => d.sod_pnl);
|
||||
sod_net_pos = _cell_map.add_net_pos(
|
||||
ColumnKeys.SODNetPos, d => d.display_sod_net_pos, fmt_qty);
|
||||
sod_long_pos = _cell_map.add_long_pos(
|
||||
ColumnKeys.SODLongPos, d => d.display_sod_long_pos, fmt_qty);
|
||||
sod_long_avg_price = _cell_map.add_long_price(ColumnKeys.SODLongAvgPrice, d => d.sod_long_avg_price);
|
||||
sod_short_pos = _cell_map.add_short_pos(
|
||||
ColumnKeys.SODShortPos, d => d.display_sod_short_pos, fmt_qty);
|
||||
sod_short_avg_price = _cell_map.add_short_price(ColumnKeys.SODShortAvgPrice, d => d.sod_short_avg_price);
|
||||
sod_delta = _cell_map.add_pnl_double(ColumnKeys.SODDelta, d => d.sod_delta, fmt_greek);
|
||||
sod_gamma = _cell_map.add_pnl_double(ColumnKeys.SODGamma, d => d.sod_gamma, fmt_greek);
|
||||
sod_vega = _cell_map.add_pnl_double(ColumnKeys.SODVega, d => d.sod_vega, fmt_greek);
|
||||
sod_theta = _cell_map.add_pnl_double(ColumnKeys.SODTheta, d => d.sod_theta, fmt_greek);
|
||||
|
||||
// day
|
||||
day_pnl = _cell_map.add_pnl_double(ColumnKeys.DayPNL, d => d.day_pnl);
|
||||
day_net_pos = _cell_map.add_net_pos(
|
||||
ColumnKeys.DayNetPos, d => d.display_day_net_pos, fmt_qty);
|
||||
day_long_pos = _cell_map.add_long_pos(
|
||||
ColumnKeys.DayLongPos, d => d.display_day_long_pos, fmt_qty);
|
||||
day_long_avg_price = _cell_map.add_long_price(ColumnKeys.DayLongAvgPrice, d => d.day_long_avg_price);
|
||||
day_short_pos = _cell_map.add_short_pos(
|
||||
ColumnKeys.DayShortPos, d => d.display_day_short_pos, fmt_qty);
|
||||
day_short_avg_price = _cell_map.add_short_price(ColumnKeys.DayShortAvgPrice, d => d.day_short_avg_price);
|
||||
day_delta = _cell_map.add_pnl_double(ColumnKeys.DayDelta, d => d.day_delta, fmt_greek);
|
||||
day_gamma = _cell_map.add_pnl_double(ColumnKeys.DayGamma, d => d.day_gamma, fmt_greek);
|
||||
day_vega = _cell_map.add_pnl_double(ColumnKeys.DayVega, d => d.day_vega, fmt_greek);
|
||||
day_theta = _cell_map.add_pnl_double(ColumnKeys.DayTheta, d => d.day_theta, fmt_greek);
|
||||
has_day_trades = _cell_map.add(ColumnKeys.HasDayTrades, d => d.has_day_trades, _fmt_has_day_trades);
|
||||
// working
|
||||
working_net_pos = _cell_map.add_net_pos(
|
||||
ColumnKeys.WorkingNetPos, d => d.display_working_net_pos, fmt_qty);
|
||||
var qty_display = _sec_meta_store.get_qty_display(security);
|
||||
working_long_pos = _cell_map.add_working_long(
|
||||
ColumnKeys.WorkingLongPos,
|
||||
d => d.working.long_data,
|
||||
d => d.bid_price,
|
||||
qty_display);
|
||||
working_long_avg_price = _cell_map.add_long_price(ColumnKeys.WorkingLongAvgPrice, d => d.working_long_avg_price);
|
||||
working_short_pos = _cell_map.add_working_short(
|
||||
ColumnKeys.WorkingShortPos, r => r.working.short_data, r => r.ask_price, qty_display);
|
||||
working_short_avg_price = _cell_map.add_short_price(ColumnKeys.WorkingShortAvgPrice, d => d.working_short_avg_price);
|
||||
// total
|
||||
total_pnl = _cell_map.add_pnl_double(ColumnKeys.TotalPNL, d => d.total_pnl);
|
||||
total_net_pos = _cell_map.add_net_pos(
|
||||
ColumnKeys.TotalNetPos, d => d.display_total_net_pos, fmt_qty);
|
||||
total_long_pos = _cell_map.add_long_pos(
|
||||
ColumnKeys.TotalLongPos, d => d.display_total_long_pos, fmt_qty);
|
||||
total_long_avg_price = _cell_map.add_long_price(ColumnKeys.TotalLongAvgPrice, d => d.total_long_avg_price);
|
||||
total_short_pos = _cell_map.add_short_pos(
|
||||
ColumnKeys.TotalShortPos, d => d.display_total_short_pos, fmt_qty);
|
||||
total_short_avg_price = _cell_map.add_short_price(ColumnKeys.TotalShortAvgPrice, d => d.total_short_avg_price);
|
||||
total_delta = _cell_map.add_pnl_double(
|
||||
ColumnKeys.TotalDelta, d => d.total_delta, fmt_greek);
|
||||
total_gamma = _cell_map.add_pnl_double(
|
||||
ColumnKeys.TotalGamma, d => d.total_gamma, fmt_greek);
|
||||
total_vega = _cell_map.add_pnl_double(ColumnKeys.TotalVega, d => d.total_vega, fmt_greek);
|
||||
total_theta = _cell_map.add_pnl_double(
|
||||
ColumnKeys.TotalTheta, d => d.total_theta, fmt_greek);
|
||||
|
||||
// market
|
||||
bid_price = _cell_map.add_price_double(ColumnKeys.BidPrice, d => d.bid_price);
|
||||
ask_price = _cell_map.add_price_double(ColumnKeys.AskPrice, d => d.ask_price);
|
||||
mid_price = _cell_map.add_price_double(ColumnKeys.MidPrice, d => d.mid_price);
|
||||
last_price = _cell_map.add_price_double(ColumnKeys.LastPrice, d => d.last_price);
|
||||
settlement_price = _cell_map.add_price_double(ColumnKeys.SettlementPrice, d => d.settlement_price);
|
||||
|
||||
_set_price_formatting_delegates(fmt_fill_price, fmt_market_price);
|
||||
_cell_map.on_row(data);
|
||||
_cell_map.set_appearances(_animator.cell_appearances);
|
||||
_dispose = _cell_map.animation_model.OnDispatcher().Subscribe(new_animations => {
|
||||
animations = new_animations.Select(kv => new mbts.ui.spark.CellAnimation(kv.Key.column, kv.Key.type, kv.Value.color.to_brush())).ToList();
|
||||
RaisePropertyChanged(nameof(animations));
|
||||
});
|
||||
|
||||
static string fmt_greek(double v) =>
|
||||
v.is_nan() ? "-" : v.ToString("#,0.00");
|
||||
|
||||
string fmt_qty(double v) =>
|
||||
quantity_display.format(v);
|
||||
}
|
||||
|
||||
// events
|
||||
public event Action<PNLNormalizedData> updated;
|
||||
public event Action<bool> is_selected_changed;
|
||||
|
||||
// data
|
||||
public PNLKey key => data.key;
|
||||
public PNLRowData data { get; private set; }
|
||||
public PNLNormalizedData normalized_data {
|
||||
get {
|
||||
if (_refresh_normalized_data) {
|
||||
_refresh_normalized_data = false;
|
||||
// Only count outrights (spread positions should be normalized into outrights)
|
||||
var sec_type = security?.ContractCase ?? Security.ContractOneofCase.None;
|
||||
var (
|
||||
sod_long,
|
||||
sod_short,
|
||||
sod_delta,
|
||||
sod_gamma,
|
||||
sod_vega,
|
||||
sod_theta,
|
||||
day_long,
|
||||
day_short,
|
||||
day_delta,
|
||||
day_gamma,
|
||||
day_vega,
|
||||
day_theta) = sec_type switch {
|
||||
Security.ContractOneofCase.Future or Security.ContractOneofCase.Option => (
|
||||
data.sod.long_pos.round_to_decimal(),
|
||||
data.sod.short_pos.round_to_decimal(),
|
||||
data.sod.delta.round_to_decimal(),
|
||||
data.sod.gamma.round_to_decimal(),
|
||||
data.sod.vega.round_to_decimal(),
|
||||
data.sod.theta.round_to_decimal(),
|
||||
data.day.long_pos.round_to_decimal(),
|
||||
data.day.short_pos.round_to_decimal(),
|
||||
data.day.delta.round_to_decimal(),
|
||||
data.day.gamma.round_to_decimal(),
|
||||
data.day.vega.round_to_decimal(),
|
||||
data.day.theta.round_to_decimal()),
|
||||
_ => (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
|
||||
};
|
||||
|
||||
var working_long = data.working_long_pos.round_to_decimal();
|
||||
var working_short = data.working_short_pos.round_to_decimal();
|
||||
|
||||
var nd = new PNLNormalizedData() {
|
||||
// sod
|
||||
sod_pnl = sod_pnl.value.round_to_decimal(),
|
||||
sod_long_pos = sod_long,
|
||||
sod_short_pos = sod_short,
|
||||
sod_delta = sod_delta,
|
||||
sod_gamma = sod_gamma,
|
||||
sod_vega = sod_vega,
|
||||
sod_theta = sod_theta,
|
||||
display_sod_long_pos = sod_long,
|
||||
display_sod_short_pos = sod_short,
|
||||
|
||||
// day
|
||||
day_pnl = day_pnl.value.round_to_decimal(),
|
||||
day_long_pos = day_long,
|
||||
day_short_pos = day_short,
|
||||
day_delta = day_delta,
|
||||
day_gamma = day_gamma,
|
||||
day_vega = day_vega,
|
||||
day_theta = day_theta,
|
||||
display_day_long_pos = day_long,
|
||||
display_day_short_pos = day_short,
|
||||
|
||||
// working
|
||||
working_long_pos = working_long,
|
||||
working_short_pos = working_short,
|
||||
display_working_long_pos = working_long,
|
||||
display_working_short_pos = working_short
|
||||
};
|
||||
if (_normalized_data != nd) {
|
||||
_normalized_data = nd;
|
||||
updated?.Invoke(nd);
|
||||
}
|
||||
}
|
||||
return _normalized_data;
|
||||
}
|
||||
}
|
||||
// security
|
||||
public string account_name => data.account_name;
|
||||
public Venue exchange => security?.Exchange ?? Venue.Unset;
|
||||
public string symbol => security.symbol_or_default();
|
||||
public DateTime expiration => data.expiration;
|
||||
public Security.ContractOneofCase security_type => security?.ContractCase ?? Security.ContractOneofCase.None;
|
||||
public SecurityImmutable? security => data.security;
|
||||
// cells
|
||||
public PNLCellVM<PNLRowData, AccountTuple> account_cell { get; }
|
||||
public PNLCellVM<PNLRowData, Venue> exchange_cell { get; }
|
||||
public PNLCellVM<PNLRowData, DateTime> expiration_cell { get; }
|
||||
public PNLCellVM<PNLRowData, string> symbol_cell { get; }
|
||||
public PNLCellVM<PNLRowData, Security.ContractOneofCase> security_type_cell { get; }
|
||||
public PNLCellVM<PNLRowData, SecurityImmutable?> security_cell { get; }
|
||||
public PNLCellVM<PNLRowData, uint> security_key { get; }
|
||||
public PNLCellVM<PNLRowData, double> cpp { get; }
|
||||
// sod
|
||||
public PNLCellVM<PNLRowData, double> sod_pnl { get; }
|
||||
public PNLCellVM<PNLRowData, double> sod_net_pos { get; }
|
||||
public PNLCellVM<PNLRowData, double> sod_long_pos { get; }
|
||||
public PNLCellVM<PNLRowData, double> sod_long_avg_price { get; }
|
||||
public PNLCellVM<PNLRowData, double> sod_short_pos { get; }
|
||||
public PNLCellVM<PNLRowData, double> sod_short_avg_price { get; }
|
||||
public PNLCellVM<PNLRowData, double> sod_delta { get; }
|
||||
public PNLCellVM<PNLRowData, double> sod_gamma { get; }
|
||||
public PNLCellVM<PNLRowData, double> sod_vega { get; }
|
||||
public PNLCellVM<PNLRowData, double> sod_theta { get; }
|
||||
|
||||
// day
|
||||
public PNLCellVM<PNLRowData, double> day_pnl { get; }
|
||||
public PNLCellVM<PNLRowData, double> day_net_pos { get; }
|
||||
public PNLCellVM<PNLRowData, double> day_long_pos { get; }
|
||||
public PNLCellVM<PNLRowData, double> day_long_avg_price { get; }
|
||||
public PNLCellVM<PNLRowData, double> day_short_pos { get; }
|
||||
public PNLCellVM<PNLRowData, double> day_short_avg_price { get; }
|
||||
public PNLCellVM<PNLRowData, double> day_delta { get; }
|
||||
public PNLCellVM<PNLRowData, double> day_gamma { get; }
|
||||
public PNLCellVM<PNLRowData, double> day_vega { get; }
|
||||
public PNLCellVM<PNLRowData, double> day_theta { get; }
|
||||
public PNLCellVM<PNLRowData, bool> has_day_trades { get; }
|
||||
// working
|
||||
public PNLCellVM<PNLRowData, double> working_net_pos { get; }
|
||||
public PNLCellVM<PNLRowData, WorkingOrderSideData> working_long_pos { get; }
|
||||
public PNLCellVM<PNLRowData, double> working_long_avg_price { get; }
|
||||
public PNLCellVM<PNLRowData, WorkingOrderSideData> working_short_pos { get; }
|
||||
public PNLCellVM<PNLRowData, double> working_short_avg_price { get; }
|
||||
// total
|
||||
public PNLCellVM<PNLRowData, double> total_pnl { get; }
|
||||
public PNLCellVM<PNLRowData, double> total_net_pos { get; }
|
||||
public PNLCellVM<PNLRowData, double> total_long_pos { get; }
|
||||
public PNLCellVM<PNLRowData, double> total_long_avg_price { get; }
|
||||
public PNLCellVM<PNLRowData, double> total_short_pos { get; }
|
||||
public PNLCellVM<PNLRowData, double> total_short_avg_price { get; }
|
||||
public PNLCellVM<PNLRowData, double> total_delta { get; }
|
||||
public PNLCellVM<PNLRowData, double> total_gamma { get; }
|
||||
public PNLCellVM<PNLRowData, double> total_vega { get; }
|
||||
public PNLCellVM<PNLRowData, double> total_theta { get; }
|
||||
|
||||
// (market)
|
||||
public PNLCellVM<PNLRowData, double> bid_price { get; }
|
||||
public PNLCellVM<PNLRowData, double> mid_price { get; }
|
||||
public PNLCellVM<PNLRowData, double> ask_price { get; }
|
||||
public PNLCellVM<PNLRowData, double> last_price { get; }
|
||||
public PNLCellVM<PNLRowData, double> settlement_price { get; }
|
||||
public PNLCellVM<PNLRowData, double> pnl_price { get; }
|
||||
|
||||
// display
|
||||
public api.domain.TickDisplaySettingsImmutable quantity_display {
|
||||
get {
|
||||
if (_quantity_display_backing.HasValue) {
|
||||
return _quantity_display_backing.Value;
|
||||
}
|
||||
if (security.HasValue) {
|
||||
_quantity_display_backing = _sec_meta_store.get_qty_display(security.Value);
|
||||
}
|
||||
return _quantity_display_backing ??= Security.Defaults.quantity_display_settingsi;
|
||||
}
|
||||
}
|
||||
|
||||
public IList<mbts.ui.spark.CellAnimation> animations { get; private set; }
|
||||
|
||||
public bool is_selected {
|
||||
get => _is_selected;
|
||||
set {
|
||||
if (Set(ref _is_selected, value))
|
||||
is_selected_changed?.Invoke(value);
|
||||
}
|
||||
}
|
||||
|
||||
IEnumerable<PNLCellVM<PNLRowData, double>> _market_price_cells {
|
||||
get {
|
||||
yield return pnl_price;
|
||||
yield return last_price;
|
||||
yield return bid_price;
|
||||
yield return mid_price;
|
||||
yield return ask_price;
|
||||
yield return settlement_price;
|
||||
}
|
||||
}
|
||||
|
||||
// includes sod, day, total, and working avg price columns
|
||||
IEnumerable<PNLCellVM<PNLRowData, double>> _fill_price_cells {
|
||||
get {
|
||||
// sod
|
||||
yield return sod_long_avg_price;
|
||||
yield return sod_short_avg_price;
|
||||
// day
|
||||
yield return day_long_avg_price;
|
||||
yield return day_short_avg_price;
|
||||
// total
|
||||
yield return total_long_avg_price;
|
||||
yield return total_short_avg_price;
|
||||
// working
|
||||
yield return working_long_avg_price;
|
||||
yield return working_short_avg_price;
|
||||
}
|
||||
}
|
||||
|
||||
public void update(PNLRowData data) {
|
||||
_quantity_display_backing = null;
|
||||
_refresh_normalized_data = true;
|
||||
this.data = data;
|
||||
_cell_map.on_row(data);
|
||||
_animator.on_row(data);
|
||||
_cell_map.set_appearances(_animator.cell_appearances);
|
||||
}
|
||||
|
||||
public void on_settings(ImmutableDictionary<ColumnKeys, PNLCellAppearance> defaults, IReadOnlyList<IPNLAnimationTrigger<PNLRowData>> triggers, Func<double, string> fmt_fill_price, Func<double, string> fmt_market_price) {
|
||||
_animator.on_settings(defaults, triggers);
|
||||
_cell_map.set_appearances(_animator.cell_appearances);
|
||||
_set_price_formatting_delegates(fmt_fill_price, fmt_market_price);
|
||||
}
|
||||
|
||||
public bool Equals(PNLRowVM other) =>
|
||||
other != null && key == other.key;
|
||||
|
||||
public override int GetHashCode() =>
|
||||
key.GetHashCode();
|
||||
|
||||
public override string ToString() =>
|
||||
$"account: {account_name} exchange: {exchange} symbol: {symbol} security: {security} buy_quantity: {day_long_pos} sell_quantity: {day_short_pos} pnl: {total_pnl}";
|
||||
|
||||
void _set_price_formatting_delegates(Func<double, string> fmt_fill_price, Func<double, string> fmt_market_price) {
|
||||
foreach (var cell in _fill_price_cells) {
|
||||
cell.format = fmt_fill_price;
|
||||
}
|
||||
foreach (var cell in _market_price_cells) {
|
||||
cell.format = fmt_market_price;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -65,9 +65,9 @@ namespace BCG {
|
||||
public static NMSheetTheoBBOCross get_cross_state(double bid_price, double ask_price, double theo, double delta, bool is_call, double driver_tick_size, bool is_proximity_alert_enabled) {
|
||||
double abs_delta = Math.Abs(delta);
|
||||
abs_delta = abs_delta == 0 ? Double.Epsilon : abs_delta;
|
||||
if (!Double.IsNaN(theo) && !Double.IsNaN(bid_price) && theo < bid_price)
|
||||
if (!Double.IsNaN(theo) && !Double.IsNaN(bid_price) && (bid_price - theo) > _basic_crosss_eps)
|
||||
return new NMSheetTheoBBOCross(CrossState.bid_cross, 0);
|
||||
else if (!Double.IsNaN(theo) && !Double.IsNaN(ask_price) && theo > ask_price)
|
||||
else if (!Double.IsNaN(theo) && !Double.IsNaN(ask_price) && (theo - ask_price) > _basic_crosss_eps)
|
||||
return new NMSheetTheoBBOCross(CrossState.ask_cross, 0);
|
||||
else if (is_proximity_alert_enabled) {
|
||||
/// bool is passed to bypass doing the below
|
||||
@@ -87,6 +87,7 @@ namespace BCG {
|
||||
return NMSheetTheoBBOCross.get_nulled_obj();
|
||||
}
|
||||
|
||||
private static double _basic_crosss_eps = 0.01;
|
||||
private static double _n_max_ticks = 100.0;
|
||||
private static NMSheetTheoBBOCross _nulled_obj = new NMSheetTheoBBOCross(CrossState.none, 0);
|
||||
public static NMSheetTheoBBOCross get_nulled_obj() { return _nulled_obj; }
|
||||
|
||||
@@ -1,161 +1,161 @@
|
||||
<Window
|
||||
x:Class="mbts.ui.spark.OrderTicket"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:msys="clr-namespace:bts.api;assembly=spark_api"
|
||||
xmlns:u="clr-namespace:mbts.ui"
|
||||
xmlns:cv="clr-namespace:mbts.ui.converters"
|
||||
xmlns:usp="clr-namespace:mbts.ui.spark"
|
||||
xmlns:sys="clr-namespace:System;assembly=mscorlib"
|
||||
u:WindowStyle.SizeToContent="WidthAndHeight"
|
||||
u:WindowStyle.TitleContent="{Binding bbo, Mode=OneWay}"
|
||||
Style="{StaticResource BTSWindowTitleTemplate}"
|
||||
>
|
||||
<Window.Resources>
|
||||
<ResourceDictionary>
|
||||
<ResourceDictionary.MergedDictionaries>
|
||||
<ResourceDictionary Source="/view/Resources/Converters.xaml"/>
|
||||
</ResourceDictionary.MergedDictionaries>
|
||||
<sys:Double x:Key="HorizontalSpacing">8.0</sys:Double>
|
||||
<sys:Double x:Key="VerticalSpacing">4.0</sys:Double>
|
||||
<sys:Double x:Key="ButtonHeight">22.0</sys:Double>
|
||||
<Style x:Key="SellButton" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
|
||||
<Setter Property="Background" Value="{u:BTSColor MarketData.ask}" />
|
||||
<Setter Property="Foreground" Value="White" />
|
||||
<Setter Property="MinHeight" Value="40" />
|
||||
<Setter Property="FontWeight" Value="Bold" />
|
||||
<Setter Property="HorizontalAlignment" Value="Stretch" />
|
||||
<Setter Property="VerticalAlignment" Value="Stretch" />
|
||||
<Setter Property="Content">
|
||||
<Setter.Value>
|
||||
<TextBlock VerticalAlignment="Center" Text="Sell" />
|
||||
</Setter.Value>
|
||||
</Setter>
|
||||
<Setter Property="Template">
|
||||
<Setter.Value>
|
||||
<ControlTemplate TargetType="Button">
|
||||
<Border
|
||||
x:Name="Border"
|
||||
CornerRadius="0"
|
||||
BorderThickness="1"
|
||||
BorderBrush="{u:BTSColor UI.Button.normal_border}"
|
||||
Background="{u:BTSColor MarketData.ask}"
|
||||
Padding="2"
|
||||
Height="Auto"
|
||||
SnapsToDevicePixels="True">
|
||||
<ContentPresenter
|
||||
HorizontalAlignment="Center"
|
||||
VerticalAlignment="Center"
|
||||
RecognizesAccessKey="True"
|
||||
Name="button_content"
|
||||
/>
|
||||
</Border>
|
||||
<ControlTemplate.Triggers>
|
||||
<Trigger Property="IsMouseOver" Value="True">
|
||||
<Setter TargetName="Border" Property="Background" Value="#99c00000" />
|
||||
<Setter TargetName="Border" Property="BorderBrush" Value="{u:BTSColor UI.Button.mouseover_border}" />
|
||||
</Trigger>
|
||||
</ControlTemplate.Triggers>
|
||||
</ControlTemplate>
|
||||
</Setter.Value>
|
||||
</Setter>
|
||||
</Style>
|
||||
<Style x:Key="BuyButton" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
|
||||
<Setter Property="Background" Value="{u:BTSColor MarketData.bid}" />
|
||||
<Setter Property="Foreground" Value="White" />
|
||||
<Setter Property="MinHeight" Value="40" />
|
||||
<Setter Property="FontWeight" Value="Bold" />
|
||||
<Setter Property="HorizontalAlignment" Value="Stretch" />
|
||||
<Setter Property="VerticalAlignment" Value="Stretch" />
|
||||
<Setter Property="Content">
|
||||
<Setter.Value>
|
||||
<TextBlock VerticalAlignment="Center" Text="Buy" />
|
||||
</Setter.Value>
|
||||
</Setter>
|
||||
<Setter Property="Template">
|
||||
<Setter.Value>
|
||||
<ControlTemplate TargetType="Button">
|
||||
<Border
|
||||
x:Name="Border"
|
||||
CornerRadius="0"
|
||||
BorderThickness="1"
|
||||
BorderBrush="{u:BTSColor UI.Button.normal_border}"
|
||||
Background="{u:BTSColor MarketData.bid}"
|
||||
Padding="2"
|
||||
Height="Auto"
|
||||
SnapsToDevicePixels="True">
|
||||
<ContentPresenter
|
||||
HorizontalAlignment="Center"
|
||||
VerticalAlignment="Center"
|
||||
RecognizesAccessKey="True"
|
||||
Name="button_content"
|
||||
/>
|
||||
</Border>
|
||||
<ControlTemplate.Triggers>
|
||||
<Trigger Property="IsMouseOver" Value="True">
|
||||
<Setter TargetName="Border" Property="Background" Value="#99167ed3" />
|
||||
<Setter TargetName="Border" Property="BorderBrush" Value="{u:BTSColor UI.Button.mouseover_border}" />
|
||||
</Trigger>
|
||||
</ControlTemplate.Triggers>
|
||||
</ControlTemplate>
|
||||
</Setter.Value>
|
||||
</Setter>
|
||||
</Style>
|
||||
</ResourceDictionary>
|
||||
</Window.Resources>
|
||||
<Window.InputBindings>
|
||||
<KeyBinding Key="Esc" Command="{x:Static SystemCommands.CloseWindowCommand}"/>
|
||||
</Window.InputBindings>
|
||||
<Grid Margin="10">
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="Auto"/>
|
||||
<RowDefinition Height="{Binding Source={StaticResource VerticalSpacing}}"/>
|
||||
<RowDefinition Height="Auto"/>
|
||||
</Grid.RowDefinitions>
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition MinWidth="120"/>
|
||||
<ColumnDefinition Width="{Binding Source={StaticResource HorizontalSpacing}}"/>
|
||||
<ColumnDefinition MinWidth="120"/>
|
||||
</Grid.ColumnDefinitions>
|
||||
<usp:OrderModificationControl Grid.ColumnSpan="3" />
|
||||
<Button Grid.Row="2" Style="{StaticResource BuyButton}" Command="{Binding send_order}" CommandParameter="{u:Enum Buy, Type=msys:Side}" />
|
||||
<Button Grid.Row="2" Grid.Column="2" Style="{StaticResource SellButton}" Command="{Binding send_order}" CommandParameter="{u:Enum Sell, Type=msys:Side}" />
|
||||
</Grid>
|
||||
<u:WindowStyle.TitleContentTemplate>
|
||||
<DataTemplate>
|
||||
<TextBlock Style="{StaticResource TextBlock.Dark}">
|
||||
<TextBlock.Inlines>
|
||||
<Run FontWeight="Bold" Text="{Binding security, Mode=OneWay}" />
|
||||
<Run Foreground="{u:BTSColor MarketData.bid}" FontSize="10">
|
||||
<MultiBinding Converter="{x:Static cv:TickDisplayConverter.Instance}" StringFormat="({0})">
|
||||
<Binding Path="bid_size" Mode="OneWay"/>
|
||||
<Binding Path="DataContext.qty_display" RelativeSource="{RelativeSource AncestorType=usp:OrderTicket}"/>
|
||||
</MultiBinding>
|
||||
</Run>
|
||||
<Run Foreground="{u:BTSColor MarketData.bid}" FontWeight="Bold">
|
||||
<Run.Text>
|
||||
<MultiBinding Converter="{StaticResource TickDisplayConverter}" Mode="OneWay">
|
||||
<Binding Path="bid"/>
|
||||
<Binding Path="security.ContractSpec"/>
|
||||
</MultiBinding>
|
||||
</Run.Text>
|
||||
</Run>
|
||||
<Run Foreground="{u:BTSColor MarketData.ask}" FontWeight="Bold">
|
||||
<Run.Text>
|
||||
<MultiBinding Converter="{StaticResource TickDisplayConverter}" Mode="OneWay">
|
||||
<Binding Path="ask"/>
|
||||
<Binding Path="security.ContractSpec"/>
|
||||
</MultiBinding>
|
||||
</Run.Text>
|
||||
</Run>
|
||||
<Run Foreground="{u:BTSColor MarketData.ask}" FontSize="10">
|
||||
<MultiBinding Converter="{x:Static cv:TickDisplayConverter.Instance}" StringFormat="({0})">
|
||||
<Binding Path="ask_size" Mode="OneWay"/>
|
||||
<Binding Path="DataContext.qty_display" RelativeSource="{RelativeSource AncestorType=usp:OrderTicket}"/>
|
||||
</MultiBinding>
|
||||
</Run>
|
||||
</TextBlock.Inlines>
|
||||
</TextBlock>
|
||||
</DataTemplate>
|
||||
</u:WindowStyle.TitleContentTemplate>
|
||||
</Window>
|
||||
<Window
|
||||
x:Class="mbts.ui.spark.OrderTicket"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:msys="clr-namespace:bts.api;assembly=spark_api"
|
||||
xmlns:u="clr-namespace:mbts.ui"
|
||||
xmlns:cv="clr-namespace:mbts.ui.converters"
|
||||
xmlns:usp="clr-namespace:mbts.ui.spark"
|
||||
xmlns:sys="clr-namespace:System;assembly=mscorlib"
|
||||
u:WindowStyle.SizeToContent="WidthAndHeight"
|
||||
u:WindowStyle.TitleContent="{Binding bbo, Mode=OneWay}"
|
||||
Style="{StaticResource BTSWindowTitleTemplate}"
|
||||
>
|
||||
<Window.Resources>
|
||||
<ResourceDictionary>
|
||||
<ResourceDictionary.MergedDictionaries>
|
||||
<ResourceDictionary Source="/view/Resources/Converters.xaml"/>
|
||||
</ResourceDictionary.MergedDictionaries>
|
||||
<sys:Double x:Key="HorizontalSpacing">8.0</sys:Double>
|
||||
<sys:Double x:Key="VerticalSpacing">4.0</sys:Double>
|
||||
<sys:Double x:Key="ButtonHeight">22.0</sys:Double>
|
||||
<Style x:Key="SellButton" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
|
||||
<Setter Property="Background" Value="{u:BTSColor MarketData.ask_dark}" />
|
||||
<Setter Property="Foreground" Value="White" />
|
||||
<Setter Property="MinHeight" Value="40" />
|
||||
<Setter Property="FontWeight" Value="Bold" />
|
||||
<Setter Property="HorizontalAlignment" Value="Stretch" />
|
||||
<Setter Property="VerticalAlignment" Value="Stretch" />
|
||||
<Setter Property="Content">
|
||||
<Setter.Value>
|
||||
<TextBlock VerticalAlignment="Center" Text="Sell" />
|
||||
</Setter.Value>
|
||||
</Setter>
|
||||
<Setter Property="Template">
|
||||
<Setter.Value>
|
||||
<ControlTemplate TargetType="Button">
|
||||
<Border
|
||||
x:Name="Border"
|
||||
CornerRadius="0"
|
||||
BorderThickness="1"
|
||||
BorderBrush="{u:BTSColor UI.Button.normal_border}"
|
||||
Background="{u:BTSColor MarketData.ask}"
|
||||
Padding="2"
|
||||
Height="Auto"
|
||||
SnapsToDevicePixels="True">
|
||||
<ContentPresenter
|
||||
HorizontalAlignment="Center"
|
||||
VerticalAlignment="Center"
|
||||
RecognizesAccessKey="True"
|
||||
Name="button_content"
|
||||
/>
|
||||
</Border>
|
||||
<ControlTemplate.Triggers>
|
||||
<Trigger Property="IsMouseOver" Value="True">
|
||||
<Setter TargetName="Border" Property="Background" Value="#99c00000" />
|
||||
<Setter TargetName="Border" Property="BorderBrush" Value="{u:BTSColor UI.Button.mouseover_border}" />
|
||||
</Trigger>
|
||||
</ControlTemplate.Triggers>
|
||||
</ControlTemplate>
|
||||
</Setter.Value>
|
||||
</Setter>
|
||||
</Style>
|
||||
<Style x:Key="BuyButton" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
|
||||
<Setter Property="Background" Value="{u:BTSColor MarketData.bid_dark}" />
|
||||
<Setter Property="Foreground" Value="White" />
|
||||
<Setter Property="MinHeight" Value="40" />
|
||||
<Setter Property="FontWeight" Value="Bold" />
|
||||
<Setter Property="HorizontalAlignment" Value="Stretch" />
|
||||
<Setter Property="VerticalAlignment" Value="Stretch" />
|
||||
<Setter Property="Content">
|
||||
<Setter.Value>
|
||||
<TextBlock VerticalAlignment="Center" Text="Buy" />
|
||||
</Setter.Value>
|
||||
</Setter>
|
||||
<Setter Property="Template">
|
||||
<Setter.Value>
|
||||
<ControlTemplate TargetType="Button">
|
||||
<Border
|
||||
x:Name="Border"
|
||||
CornerRadius="0"
|
||||
BorderThickness="1"
|
||||
BorderBrush="{u:BTSColor UI.Button.normal_border}"
|
||||
Background="{u:BTSColor MarketData.bid}"
|
||||
Padding="2"
|
||||
Height="Auto"
|
||||
SnapsToDevicePixels="True">
|
||||
<ContentPresenter
|
||||
HorizontalAlignment="Center"
|
||||
VerticalAlignment="Center"
|
||||
RecognizesAccessKey="True"
|
||||
Name="button_content"
|
||||
/>
|
||||
</Border>
|
||||
<ControlTemplate.Triggers>
|
||||
<Trigger Property="IsMouseOver" Value="True">
|
||||
<Setter TargetName="Border" Property="Background" Value="#99167ed3" />
|
||||
<Setter TargetName="Border" Property="BorderBrush" Value="{u:BTSColor UI.Button.mouseover_border}" />
|
||||
</Trigger>
|
||||
</ControlTemplate.Triggers>
|
||||
</ControlTemplate>
|
||||
</Setter.Value>
|
||||
</Setter>
|
||||
</Style>
|
||||
</ResourceDictionary>
|
||||
</Window.Resources>
|
||||
<Window.InputBindings>
|
||||
<KeyBinding Key="Esc" Command="{x:Static SystemCommands.CloseWindowCommand}"/>
|
||||
</Window.InputBindings>
|
||||
<Grid Margin="10">
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="Auto"/>
|
||||
<RowDefinition Height="{Binding Source={StaticResource VerticalSpacing}}"/>
|
||||
<RowDefinition Height="Auto"/>
|
||||
</Grid.RowDefinitions>
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition MinWidth="120"/>
|
||||
<ColumnDefinition Width="{Binding Source={StaticResource HorizontalSpacing}}"/>
|
||||
<ColumnDefinition MinWidth="120"/>
|
||||
</Grid.ColumnDefinitions>
|
||||
<usp:OrderModificationControl Grid.ColumnSpan="3" />
|
||||
<Button Grid.Row="2" Style="{StaticResource BuyButton}" Command="{Binding send_order}" CommandParameter="{u:Enum Buy, Type=msys:Side}" />
|
||||
<Button Grid.Row="2" Grid.Column="2" Style="{StaticResource SellButton}" Command="{Binding send_order}" CommandParameter="{u:Enum Sell, Type=msys:Side}" />
|
||||
</Grid>
|
||||
<u:WindowStyle.TitleContentTemplate>
|
||||
<DataTemplate>
|
||||
<TextBlock Style="{StaticResource TextBlock.Dark}">
|
||||
<TextBlock.Inlines>
|
||||
<Run FontWeight="Bold" Text="{Binding security, Mode=OneWay}" />
|
||||
<Run Foreground="{u:BTSColor MarketData.bid}" FontSize="10">
|
||||
<MultiBinding Converter="{x:Static cv:TickDisplayConverter.Instance}" StringFormat="({0})">
|
||||
<Binding Path="bid_size" Mode="OneWay"/>
|
||||
<Binding Path="DataContext.qty_display" RelativeSource="{RelativeSource AncestorType=usp:OrderTicket}"/>
|
||||
</MultiBinding>
|
||||
</Run>
|
||||
<Run Foreground="{u:BTSColor MarketData.bid}" FontWeight="Bold">
|
||||
<Run.Text>
|
||||
<MultiBinding Converter="{StaticResource TickDisplayConverter}" Mode="OneWay">
|
||||
<Binding Path="bid"/>
|
||||
<Binding Path="security.ContractSpec"/>
|
||||
</MultiBinding>
|
||||
</Run.Text>
|
||||
</Run>
|
||||
<Run Foreground="{u:BTSColor MarketData.ask}" FontWeight="Bold">
|
||||
<Run.Text>
|
||||
<MultiBinding Converter="{StaticResource TickDisplayConverter}" Mode="OneWay">
|
||||
<Binding Path="ask"/>
|
||||
<Binding Path="security.ContractSpec"/>
|
||||
</MultiBinding>
|
||||
</Run.Text>
|
||||
</Run>
|
||||
<Run Foreground="{u:BTSColor MarketData.ask}" FontSize="10">
|
||||
<MultiBinding Converter="{x:Static cv:TickDisplayConverter.Instance}" StringFormat="({0})">
|
||||
<Binding Path="ask_size" Mode="OneWay"/>
|
||||
<Binding Path="DataContext.qty_display" RelativeSource="{RelativeSource AncestorType=usp:OrderTicket}"/>
|
||||
</MultiBinding>
|
||||
</Run>
|
||||
</TextBlock.Inlines>
|
||||
</TextBlock>
|
||||
</DataTemplate>
|
||||
</u:WindowStyle.TitleContentTemplate>
|
||||
</Window>
|
||||
@@ -64,8 +64,7 @@ namespace mbts.analytics {
|
||||
if (tl.tl_row.size > 0) {
|
||||
start_long_pos += tl.tl_row.size;
|
||||
start_long_total_cost += cost;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
start_short_pos += Math.Abs(tl.tl_row.size);
|
||||
start_short_total_cost += cost;
|
||||
}
|
||||
@@ -78,8 +77,7 @@ namespace mbts.analytics {
|
||||
if (tl.tl_row.size > 0) {
|
||||
day_long_pos += tl.tl_row.size;
|
||||
day_long_total_cost += cost;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
day_short_pos += Math.Abs(tl.tl_row.size);
|
||||
day_short_total_cost += cost;
|
||||
}
|
||||
@@ -165,6 +163,13 @@ namespace mbts.analytics {
|
||||
is_day_trade = _is_day_trade(tl)
|
||||
};
|
||||
pnl_trades.Add(pnl_tl);
|
||||
} else if (tl.security_type == SecurityType.Spot) {
|
||||
PNLTradeRow pnl_tl = new PNLTradeRow() {
|
||||
tl_row = tl,
|
||||
md = tl_md,
|
||||
is_day_trade = _is_day_trade(tl)
|
||||
};
|
||||
pnl_trades.Add(pnl_tl);
|
||||
}
|
||||
}
|
||||
return pnl_trades;
|
||||
|
||||
@@ -4,6 +4,9 @@ using bts.api;
|
||||
|
||||
using BCG;
|
||||
using mbts.co2;
|
||||
using System.Xml.Serialization;
|
||||
using ZstdSharp.Unsafe;
|
||||
using bts.proto;
|
||||
|
||||
namespace mbts.analytics {
|
||||
// Pairs ITradeLogRow along with the data (theo, md) that is needed to compute
|
||||
@@ -18,20 +21,88 @@ namespace mbts.analytics {
|
||||
|
||||
public SecurityImmutable security { get { return md.security; } }
|
||||
|
||||
private double _option_pnl(double trade_price, double now_price) {
|
||||
double coin_terms = (now_price - trade_price) * tl_row.size;
|
||||
double usd_terms = coin_terms * driver;
|
||||
return usd_terms;
|
||||
// private double _option_pnl(double trade_price, double now_price) {
|
||||
// double coin_terms = (now_price - trade_price) * tl_row.size;
|
||||
// double usd_terms = coin_terms * driver;
|
||||
// return usd_terms;
|
||||
// }
|
||||
|
||||
private bool _is_notional {
|
||||
get {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private double _future_pnl(double trade_price, double now_price) {
|
||||
// the qty is in USD, so this is the initial cash flow
|
||||
double init_asset_value = tl_row.size;
|
||||
double asset_size = init_asset_value / tl_row.price;
|
||||
double now_asset_value = asset_size * now_price;
|
||||
return now_asset_value - init_asset_value;
|
||||
private bool _is_inverse_option_contract {
|
||||
get {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private double _cpp {
|
||||
get {
|
||||
return 1.0;
|
||||
}
|
||||
}
|
||||
|
||||
private bool _is_reverse_option_contract {
|
||||
get {
|
||||
// check for Deribit options
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private double _option_pnl {
|
||||
get {
|
||||
if (_is_notional) {
|
||||
var now_price = theo;
|
||||
double coin_terms = (now_price - tl_row.price) * tl_row.size;
|
||||
double usd_terms = coin_terms * driver;
|
||||
return usd_terms;
|
||||
} else {
|
||||
return _cpp * tl_row.size * (theo - tl_row.price);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private double _spot_pnl {
|
||||
get {
|
||||
// both are computed the same way
|
||||
return _future_pnl;
|
||||
}
|
||||
}
|
||||
|
||||
private double _future_pnl {
|
||||
get {
|
||||
if (_is_notional) {
|
||||
// TODO:
|
||||
// let it fall through right now
|
||||
// the qty is in USD, so this is the initial cash flow
|
||||
var now_price = theo;
|
||||
double init_asset_value = tl_row.size;
|
||||
double asset_size = init_asset_value / tl_row.price;
|
||||
double now_asset_value = asset_size * now_price;
|
||||
return now_asset_value - init_asset_value;
|
||||
} else {
|
||||
// TODO: is position pnl, compute it wrt settlement price?
|
||||
return tl_row.size * (theo - tl_row.price);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// private double _future_pnl(double trade_price, double now_price) {
|
||||
// if (_is_notional) {
|
||||
// // the qty is in USD, so this is the initial cash flow
|
||||
// double init_asset_value = tl_row.size;
|
||||
// double asset_size = init_asset_value / tl_row.price;
|
||||
// double now_asset_value = asset_size * now_price;
|
||||
// return now_asset_value - init_asset_value;
|
||||
// } else {
|
||||
// return tl_row.size * (theo - tl_row.price);
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
public double driver {
|
||||
get {
|
||||
if (tl_row.security_type == SecurityType.Option) {
|
||||
@@ -44,35 +115,46 @@ namespace mbts.analytics {
|
||||
}
|
||||
public double theo {
|
||||
get {
|
||||
if (tl_row.security_type == SecurityType.Option) {
|
||||
return tl_row.call_put == PutOrCall.Call ?
|
||||
opt_theo.fwd_normalized_call : opt_theo.fwd_normalized_put;
|
||||
} else if (tl_row.security_type == SecurityType.Future) {
|
||||
return fut_theo.theo;
|
||||
switch (tl_row.security_type) {
|
||||
// TODO:
|
||||
case SecurityType.Option:
|
||||
if (_is_inverse_option_contract) {
|
||||
return tl_row.call_put == PutOrCall.Call ?
|
||||
opt_theo.fwd_normalized_call : opt_theo.fwd_normalized_put;
|
||||
} else {
|
||||
return tl_row.call_put == PutOrCall.Call ?
|
||||
opt_theo.call : opt_theo.put;
|
||||
}
|
||||
// future and options, just get mid market
|
||||
case SecurityType.Future:
|
||||
//return fut_theo.theo;
|
||||
case SecurityType.Spot:
|
||||
var bbo = top_of_book;
|
||||
return (bbo.Item1 + bbo.Item2) / 2;
|
||||
default:
|
||||
return Double.NaN;
|
||||
}
|
||||
return Double.NaN;
|
||||
}
|
||||
}
|
||||
public double pnl {
|
||||
get {
|
||||
double rv = Double.NaN;
|
||||
if (is_day_trade) {
|
||||
if (tl_row.security_type == SecurityType.Future) {
|
||||
rv = _future_pnl(tl_row.price, theo);
|
||||
} else if (tl_row.security_type == SecurityType.Option) {
|
||||
rv = _option_pnl(tl_row.price, theo);
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
// use settle price ?
|
||||
if (tl_row.security_type == SecurityType.Future) {
|
||||
rv = _future_pnl(tl_row.price, theo);
|
||||
} else if (tl_row.security_type == SecurityType.Option) {
|
||||
rv = _option_pnl(tl_row.price, theo);
|
||||
switch (tl_row.security_type) {
|
||||
case SecurityType.Option:
|
||||
rv = _option_pnl;
|
||||
break;
|
||||
case SecurityType.Future:
|
||||
rv = _future_pnl;
|
||||
break;
|
||||
case SecurityType.Spot:
|
||||
// = tl_row.size * (theo - tl_row.price);
|
||||
rv = _spot_pnl;
|
||||
break;
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
|
||||
public double settle_price {
|
||||
get {
|
||||
return md.md == null ? Double.NaN : md.md.settlement_price;
|
||||
|
||||
@@ -36,6 +36,10 @@ namespace mbts {
|
||||
}
|
||||
|
||||
public static bool is_unexpired(bts.api.SecurityTuple st, DateTime exp_date) {
|
||||
if (st.Venue == Venue.Cboe || st.Venue == Venue.Glbx) {
|
||||
// server is already getting teh righ contracts
|
||||
return true;
|
||||
}
|
||||
TimeSpan exp_time = get_exp_time(st);
|
||||
DateTime full_exp_utc = exp_date.ToUniversalTime().Date.Add(exp_time);
|
||||
DateTime now_utc = DateTime.UtcNow;
|
||||
@@ -43,6 +47,8 @@ namespace mbts {
|
||||
}
|
||||
|
||||
public static bool is_unexpired(bts.api.SecurityImmutable sec) {
|
||||
if (sec.security_tuple().Venue == Venue.Cboe || sec.security_tuple().Venue == Venue.Glbx)
|
||||
return true;
|
||||
var sec_type = sec.security_type();
|
||||
switch (sec_type) {
|
||||
case bts.api.SecurityType.Future:
|
||||
|
||||
@@ -58,9 +58,10 @@ namespace BCG {
|
||||
}
|
||||
// TODO: put it as a setting
|
||||
bts.Log.assert(theos.Length == drivers.Length);
|
||||
for (int i = 0; i < theos.Length; i++) {
|
||||
theos[i] /= drivers[i];
|
||||
}
|
||||
// only do this when its deribit crypto option, TODO
|
||||
// for (int i = 0; i < theos.Length; i++) {
|
||||
// theos[i] /= drivers[i];
|
||||
// }
|
||||
return new SpreadValue(spread_spec, theos, deltas, gammas, vegas, adj_vegas, oevs, thetas, slope_vegas, drivers, qs_midpoint_theos, pos, additional_delta);
|
||||
}
|
||||
}
|
||||
@@ -142,9 +143,21 @@ namespace BCG {
|
||||
return tp ?? mbts.spark.TheoPoint.getNulledObject();
|
||||
}
|
||||
|
||||
private static mbts.spark.TheoPoint _compute_single_option_leg_at_tied_price(DateTime option_expiration, double strike, bool is_call, OptionTheoInterpolationData sparse_theo_map, double driver_price) {
|
||||
mbts.spark.TheoPoint tp = sparse_theo_map == null ? mbts.spark.TheoPoint.getNulledObject() : sparse_theo_map.interpolate_theos(option_expiration, strike, driver_price);
|
||||
return tp ?? mbts.spark.TheoPoint.getNulledObject();
|
||||
private static mbts.spark.TheoPoint _compute_single_option_leg_at_tied_price(DateTime option_expiration, double strike, bool is_call, OptionTheoInterpolationData sparse_theo_map,
|
||||
ExpStrikeKeyedData<mbts.spark.TheoPoint> live_symbol_theo_map, double driver_price) {
|
||||
// mbts.spark.TheoPoint tp = sparse_theo_map == null ? mbts.spark.TheoPoint.getNulledObject() : sparse_theo_map.interpolate_theos(option_expiration, strike, driver_price);
|
||||
// return tp ?? mbts.spark.TheoPoint.getNulledObject();
|
||||
// TOD: config here
|
||||
return _compute_single_option_leg_at_tied_price_from_live(option_expiration, strike, is_call, live_symbol_theo_map, driver_price);
|
||||
}
|
||||
|
||||
private static mbts.spark.TheoPoint _compute_single_option_leg_at_tied_price_from_live(DateTime option_expiration, double strike, bool is_call,
|
||||
ExpStrikeKeyedData<mbts.spark.TheoPoint> live_symbol_theo_map, double driver_price) {
|
||||
var tp = live_symbol_theo_map.get_data(option_expiration, strike);
|
||||
if (tp == null)
|
||||
return mbts.spark.TheoPoint.getNulledObject();
|
||||
return TheoPoint.extrapolate(tp, driver_price, is_call);
|
||||
|
||||
}
|
||||
|
||||
/// this is the only relatively tricky thing here
|
||||
@@ -174,7 +187,7 @@ namespace BCG {
|
||||
if (leg_price_spec.primaryFrozen) /// tied
|
||||
{
|
||||
effective_driver = leg_price_spec.primary;
|
||||
return _compute_single_option_leg_at_tied_price(spread_option_leg.contract.expiration, spread_option_leg.contract.strike, spread_option_leg.contract.isCall, symbol_sparse_theo_map, leg_price_spec.primary);
|
||||
return _compute_single_option_leg_at_tied_price(spread_option_leg.contract.expiration, spread_option_leg.contract.strike, spread_option_leg.contract.isCall, symbol_sparse_theo_map, live_symbol_theo_map, leg_price_spec.primary);
|
||||
} else {
|
||||
mbts.spark.TheoPoint tp = _compute_single_option_leg_at_live_price(spread_option_leg.contract.expiration, spread_option_leg.contract.strike, spread_option_leg.contract.isCall, live_symbol_theo_map);
|
||||
effective_driver = tp.driver;
|
||||
@@ -188,7 +201,7 @@ namespace BCG {
|
||||
future_theo_map.TryGetValue(driver_price_info.first, out symbol_future_theo_map);
|
||||
double driver_price = StatelessFutureTheoCalculator.transform_price(symbol_future_theo_map, leg_price_spec.driverExpiration, leg_price_spec.primary, driver_price_info.second);
|
||||
effective_driver = leg_price_spec.primary;
|
||||
return _compute_single_option_leg_at_tied_price(spread_option_leg.contract.expiration, spread_option_leg.contract.strike, spread_option_leg.contract.isCall, symbol_sparse_theo_map, driver_price);
|
||||
return _compute_single_option_leg_at_tied_price(spread_option_leg.contract.expiration, spread_option_leg.contract.strike, spread_option_leg.contract.isCall, symbol_sparse_theo_map, live_symbol_theo_map, driver_price);
|
||||
} else {
|
||||
bts.Log.Ui.error(String.Format("This spread is live but the driver expiration {0} is different from the system driver expiration {1} for {2}",
|
||||
spread_option_leg.legPriceSpec.driverExpiration, driver_price_info.second, spread_option_leg.contract.product));
|
||||
|
||||
@@ -216,8 +216,10 @@ namespace mbts.ui {
|
||||
public static BTSColor ask_text => BTSCoreColors.red_dark;
|
||||
public static BTSColor bid_text => BTSCoreColors.blue;
|
||||
public static BTSColor bid => 0xff76b7fa;
|
||||
public static BTSColor bid_dark => 0xff26239a;
|
||||
public static BTSColor bid_alt => BTSCoreColors.blue_light.with(0x70);
|
||||
public static BTSColor ask => 0xffff6666;
|
||||
public static BTSColor ask_dark => 0xff7a2331;
|
||||
public static BTSColor ask_alt => 0x70ffb4b4;
|
||||
public static BTSColor last => BTSCoreColors.black;
|
||||
public static BTSColor bid_ask_alt_blend => BTSCoreColors.purple_light;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
namespace mbts.ui.controls {
|
||||
namespace mbts.ui.controls {
|
||||
using bts;
|
||||
using bts.utils;
|
||||
|
||||
@@ -26,7 +26,7 @@
|
||||
public const double
|
||||
Account = 62,
|
||||
CheckMark = 42,
|
||||
Currency = 54,
|
||||
Currency = 68,
|
||||
Exchange = 66,
|
||||
FillKey = 68,
|
||||
OrderKey = 78,
|
||||
@@ -43,7 +43,9 @@
|
||||
Side = 47,
|
||||
Qty = 48,
|
||||
MergedQty = 68,
|
||||
NotionalValue = 68,
|
||||
Filled = 56,
|
||||
FilledNotionalValue = 81,
|
||||
Symbol = 60,
|
||||
Time = 112,
|
||||
TimeOfDay = 84,
|
||||
@@ -122,6 +124,7 @@
|
||||
SecurityType = "Security Type",
|
||||
Side = "B/S",
|
||||
Qty = "Qty",
|
||||
NotionalValue = "Notional",
|
||||
MergedQty = "Qty",
|
||||
DirectQty = "Qty (D)",
|
||||
ImpliedQty = "Qty (I)",
|
||||
@@ -144,6 +147,7 @@
|
||||
FillKind = "Fill Kind",
|
||||
FilledPrice = "Fill Price",
|
||||
FilledQuantity = "Fill Qty",
|
||||
FilledNotionalValue = "Fill Notional",
|
||||
FillStatus = "Fill Status",
|
||||
Name = "Name",
|
||||
Description = "Description",
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -19,7 +19,7 @@
|
||||
<sys:Double x:Key="ButtonHeight">22.0</sys:Double>
|
||||
|
||||
<Style x:Key="SellButton" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
|
||||
<Setter Property="Background" Value="{u:BTSColor MarketData.ask}"/>
|
||||
<Setter Property="Background" Value="{u:BTSBrush MarketData.ask_dark}"/>
|
||||
<Setter Property="BorderBrush" Value="{u:BTSColor UI.Button.normal_border}"/>
|
||||
<Setter Property="Foreground" Value="{u:BTSColor UI.default_text}" />
|
||||
<Setter Property="MinHeight" Value="40"/>
|
||||
@@ -32,7 +32,7 @@
|
||||
</Style>
|
||||
|
||||
<Style x:Key="BuyButton" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
|
||||
<Setter Property="Background" Value="{u:BTSColor MarketData.bid}"/>
|
||||
<Setter Property="Background" Value="{u:BTSBrush MarketData.bid_dark}"/>
|
||||
<Setter Property="BorderBrush" Value="{u:BTSColor UI.Button.normal_border}"/>
|
||||
<Setter Property="Padding" Value="2"/>
|
||||
<Setter Property="Foreground" Value="{u:BTSColor UI.default_text}" />
|
||||
|
||||
@@ -100,7 +100,7 @@
|
||||
}
|
||||
|
||||
void PerformMEFComposition(string config_source) {
|
||||
Log.Ui.info($"this is v2.2.1 Generating MEF dependencies for the shell" +
|
||||
Log.Ui.info($"Generating MEF dependencies for the shell" +
|
||||
$"({typeof(IInitializable)}s, {typeof(IShellModule)}s, " +
|
||||
$"{typeof(IShellDaemon)}s, etc.)");
|
||||
try {
|
||||
|
||||
@@ -154,37 +154,48 @@ namespace mbts.ui.spark {
|
||||
|
||||
static class MDDisplayHelper {
|
||||
/// This delegate should be created by calling <see cref="get_formatter(MarketSizeDisplay)"/>
|
||||
public delegate string SizeFormatter(double merged, double implied, double price);
|
||||
public delegate string SizeFormatter(double merged, double implied, double price, double notional);
|
||||
/// The private delegates below use refs bc the <see cref="SizeFormatter"/> returned by <see cref="get_formatter(MarketSizeDisplay)"/>
|
||||
/// internally uses several <see cref="_SizeFormatter"/>s to add different text components to the resultant string.
|
||||
/// The refs, therefor, avoid making extra copies of the variables.
|
||||
/// These are private members, so any code-readability cost is confined to a limited scope.
|
||||
delegate void _SizeFormatter(ref double merged, ref double implied, ref double price, ref StringBuilder sb);
|
||||
delegate void _SizeFormatter(ref double merged, ref double implied, ref double price, ref double notional, ref StringBuilder sb);
|
||||
delegate void _SizeFormatterNPS(ref NativePriceSize nps, ref StringBuilder sb);
|
||||
|
||||
public static Func<double, double, string> get_value_formatter(
|
||||
MarketSizeDisplay size_display, TickDisplaySettingsImmutable display, TickDisplaySettingsImmutable price_display) {
|
||||
var raw_fmt_size = get_formatter(size_display, display, price_display);
|
||||
return (q, p) => raw_fmt_size(q, 0, p);
|
||||
MarketSizeDisplay size_display, TickDisplaySettingsImmutable display, TickDisplaySettingsImmutable notional_value_display) {
|
||||
var raw_fmt_size = get_formatter(size_display, display, notional_value_display);
|
||||
return (q, p) => raw_fmt_size(q, 0, p, double.NaN);
|
||||
}
|
||||
|
||||
public static Func<LadderPriceLevel, string> get_formatter_lpl(
|
||||
MarketSizeDisplay size_display, TickDisplaySettingsImmutable display, TickDisplaySettingsImmutable notional_value_display) {
|
||||
var raw_fmt_size = get_formatter(size_display, display, notional_value_display);
|
||||
return nps => raw_fmt_size(
|
||||
nps.Volume,
|
||||
0,
|
||||
nps.Price,
|
||||
nps.NotionalValue);
|
||||
}
|
||||
|
||||
public static Func<NativePriceSize, string> get_formatter_nps(
|
||||
MarketSizeDisplay size_display, TickDisplaySettingsImmutable display, TickDisplaySettingsImmutable price_display) {
|
||||
var raw_fmt_size = get_formatter(size_display, display, price_display);
|
||||
MarketSizeDisplay size_display, TickDisplaySettingsImmutable display, TickDisplaySettingsImmutable notional_value_display) {
|
||||
var raw_fmt_size = get_formatter(size_display, display, notional_value_display);
|
||||
return nps => raw_fmt_size(
|
||||
nps.implied_size + nps.direct_size,
|
||||
nps.implied_size,
|
||||
nps.price);
|
||||
nps.price,
|
||||
nps.notional_size);
|
||||
}
|
||||
|
||||
public static SizeFormatter get_formatter(
|
||||
MarketSizeDisplay size_display, TickDisplaySettingsImmutable display, TickDisplaySettingsImmutable price_display) {
|
||||
_SizeFormatter implied_absent = _get_formatter(size_display.normal, display, price_display);
|
||||
MarketSizeDisplay size_display, TickDisplaySettingsImmutable display, TickDisplaySettingsImmutable notional_value_display) {
|
||||
_SizeFormatter implied_absent = _get_formatter(size_display.normal, display, notional_value_display);
|
||||
_SizeFormatter implied_present = _get_formatter(
|
||||
size_display.implied_present ?? size_display.normal, display, price_display);
|
||||
return (merged, implied, price) => {
|
||||
size_display.implied_present ?? size_display.normal, display, notional_value_display);
|
||||
return (merged, implied, price, notional) => {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
(implied > 0 ? implied_present : implied_absent)(ref merged, ref implied, ref price, ref sb);
|
||||
(implied > 0 ? implied_present : implied_absent)(ref merged, ref implied, ref price, ref notional, ref sb);
|
||||
return sb.ToString();
|
||||
};
|
||||
}
|
||||
@@ -205,13 +216,13 @@ namespace mbts.ui.spark {
|
||||
static _SizeFormatter _get_formatter(
|
||||
MarketSizeDisplay.SizeFormattingElement[] fmt_elements,
|
||||
TickDisplaySettingsImmutable display,
|
||||
TickDisplaySettingsImmutable price_display) {
|
||||
TickDisplaySettingsImmutable notional_value_display) {
|
||||
_SizeFormatter[] formatters = fmt_elements.is_null_or_empty() ?
|
||||
Array.Empty<_SizeFormatter>() :
|
||||
CollectionHelper.convert_array(fmt_elements, sfe => _get_formatter(sfe, display, price_display));
|
||||
return new _SizeFormatter((ref double m, ref double i, ref double p, ref StringBuilder sb) => {
|
||||
CollectionHelper.convert_array(fmt_elements, sfe => _get_formatter(sfe, display, notional_value_display));
|
||||
return new _SizeFormatter((ref double m, ref double i, ref double p, ref double n, ref StringBuilder sb) => {
|
||||
foreach (var f in formatters)
|
||||
f(ref m, ref i, ref p, ref sb);
|
||||
f(ref m, ref i, ref p, ref n, ref sb);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -237,7 +248,7 @@ namespace mbts.ui.spark {
|
||||
static _SizeFormatter _get_formatter(
|
||||
MarketSizeDisplay.SizeFormattingElement fmt_element,
|
||||
TickDisplaySettingsImmutable display,
|
||||
TickDisplaySettingsImmutable price_display) =>
|
||||
TickDisplaySettingsImmutable notional_value_display) =>
|
||||
fmt_element.type switch {
|
||||
MarketSizeDisplay.SizeFormattingElementType.Const =>
|
||||
SizeTokenFormatters.char_formatter(fmt_element.data ?? string.Empty),
|
||||
@@ -248,7 +259,7 @@ namespace mbts.ui.spark {
|
||||
MarketSizeDisplay.SizeFormattingElementType.Merged =>
|
||||
SizeTokenFormatters.merged_formatter(display),
|
||||
MarketSizeDisplay.SizeFormattingElementType.MergedNotional =>
|
||||
SizeTokenFormatters.merged_notional_formatter(price_display),
|
||||
SizeTokenFormatters.merged_notional_formatter(notional_value_display),
|
||||
_ => throw ExceptionBuilder.Argument.unexpected_case(
|
||||
fmt_element.type, nameof(fmt_element.type)),
|
||||
};
|
||||
@@ -256,24 +267,24 @@ namespace mbts.ui.spark {
|
||||
static class SizeTokenFormatters {
|
||||
public static _SizeFormatter char_formatter(string data) {
|
||||
string local = data.ToString();
|
||||
return (ref double m, ref double i, ref double p, ref StringBuilder sb) =>
|
||||
return (ref double m, ref double i, ref double p, ref double n, ref StringBuilder sb) =>
|
||||
sb.Append(local);
|
||||
}
|
||||
public static _SizeFormatter merged_formatter(TickDisplaySettingsImmutable display) =>
|
||||
(ref double m, ref double i, ref double p, ref StringBuilder sb) =>
|
||||
(ref double m, ref double i, ref double p, ref double n, ref StringBuilder sb) =>
|
||||
sb.Append(display.format(m));
|
||||
|
||||
public static _SizeFormatter direct_formatter(TickDisplaySettingsImmutable display) =>
|
||||
(ref double m, ref double i, ref double p, ref StringBuilder sb) =>
|
||||
(ref double m, ref double i, ref double p, ref double n, ref StringBuilder sb) =>
|
||||
sb.Append(display.format(m - i));
|
||||
|
||||
public static _SizeFormatter implied_formatter(TickDisplaySettingsImmutable display) =>
|
||||
(ref double m, ref double i, ref double p, ref StringBuilder sb) =>
|
||||
(ref double m, ref double i, ref double p, ref double n, ref StringBuilder sb) =>
|
||||
sb.Append(display.format(i));
|
||||
|
||||
public static _SizeFormatter merged_notional_formatter(TickDisplaySettingsImmutable display) =>
|
||||
(ref double m, ref double i, ref double p, ref StringBuilder sb) =>
|
||||
sb.Append(display.format(m * p));
|
||||
(ref double m, ref double i, ref double p, ref double n, ref StringBuilder sb) =>
|
||||
sb.Append(display.format(n));
|
||||
|
||||
public static _SizeFormatterNPS char_formatter_nps(string data) {
|
||||
string local = data.ToString();
|
||||
@@ -373,9 +384,10 @@ namespace mbts.ui.spark {
|
||||
_display = backing(nameof(display), fmt);
|
||||
|
||||
string fmt() {
|
||||
const double SAMPLE_PRICE = 25.0;
|
||||
var impl = MDDisplayHelper.get_formatter(
|
||||
owner.create(), Security.Defaults.quantity_display_settingsi, Security.Defaults.price_display_settingsi);
|
||||
return impl(merged, implied, 1);
|
||||
return impl(merged, implied, 1, merged * SAMPLE_PRICE);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@ namespace mbts {
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
|
||||
public readonly struct SecurityMD : IEquatable<SecurityMD> {
|
||||
public static readonly Func<SecurityMD, uint> key_fn = smd => smd.security.Id;
|
||||
@@ -60,4 +61,128 @@ namespace mbts {
|
||||
public static bool operator !=(SecurityMD left, SecurityMD right) =>
|
||||
!(left == right);
|
||||
}
|
||||
|
||||
static class SecurityMDExtensions {
|
||||
const uint DEPTH_COUNT = 10;
|
||||
[ThreadStatic]
|
||||
static NativePriceSize[] leg1Bids;
|
||||
|
||||
[ThreadStatic]
|
||||
static NativePriceSize[] leg1Asks;
|
||||
|
||||
static SecurityMDExtensions() {
|
||||
leg1Bids = new NativePriceSize[DEPTH_COUNT];
|
||||
leg1Asks = new NativePriceSize[DEPTH_COUNT];
|
||||
}
|
||||
|
||||
public static Book Depth(this SecurityMD security, IReadOnlyDictionary<uint, SecurityMD> securities, ref ulong time) {
|
||||
var bids = new NativePriceSize[DEPTH_COUNT];
|
||||
var asks = new NativePriceSize[DEPTH_COUNT];
|
||||
var bidsOut = new (double, double, double)[(int)DEPTH_COUNT];
|
||||
var asksOut = new (double, double, double)[(int)DEPTH_COUNT];
|
||||
if (!security.security.HasSynthetic) {
|
||||
security.md.get_depth(ref time, ref bids, ref asks, DEPTH_COUNT);
|
||||
for (int i = 0; i < DEPTH_COUNT; i++) {
|
||||
bidsOut[i] = (bids[i].price, bids[i].size, bids[i].notional_size);
|
||||
asksOut[i] = (asks[i].price, asks[i].size, asks[i].notional_size);
|
||||
}
|
||||
return new(bidsOut, asksOut);
|
||||
}
|
||||
Log.assert(security.security.Synthetic.Legs.TryGetFirst(out var leg1));
|
||||
if (!securities.TryGetValue(leg1.Security.Id, out var sec1)) {
|
||||
return Book.Empty;
|
||||
}
|
||||
ulong sec1Time = 0;
|
||||
if (!sec1.md.get_depth(ref sec1Time, ref leg1Bids, ref leg1Asks, DEPTH_COUNT)) {
|
||||
return Book.Empty;
|
||||
}
|
||||
security.md.get_depth(ref time, ref bids, ref asks, DEPTH_COUNT);
|
||||
int leg1BidsLevel = 0;
|
||||
int leg1AsksLevel = 0;
|
||||
double leg1BidsConsumed = 0;
|
||||
double leg1AsksConsumed = 0;
|
||||
double takeQuantity;
|
||||
double notional;
|
||||
for (int i = 0; i < DEPTH_COUNT && !bids[i].is_nan; i++) {
|
||||
takeQuantity = bids[i].size * leg1.Quantity;
|
||||
notional = getNotionalValue(ref leg1Bids, ref leg1BidsLevel, ref leg1BidsConsumed, ref takeQuantity);
|
||||
bidsOut[i] = (bids[i].price, bids[i].size, notional);
|
||||
takeQuantity = asks[i].size * leg1.Quantity;
|
||||
notional = getNotionalValue(ref leg1Asks, ref leg1AsksLevel, ref leg1AsksConsumed, ref takeQuantity);
|
||||
asksOut[i] = (asks[i].price, asks[i].size, notional);
|
||||
}
|
||||
return new(bidsOut, asksOut);
|
||||
}
|
||||
|
||||
public static Book Depth(this SecurityMD security, NativePriceSize[] bids, NativePriceSize[] asks, IReadOnlyDictionary<uint, SecurityMD> securities) {
|
||||
bids ??= new NativePriceSize[NativePriceHelpers.COCONUT_MAX_DEPTH];
|
||||
asks ??= new NativePriceSize[NativePriceHelpers.COCONUT_MAX_DEPTH];
|
||||
var bidsOut = new (double, double, double)[bids.Length];
|
||||
var asksOut = new (double, double, double)[asks.Length];
|
||||
var depthCount = (uint)Math.Min(bids.Length, asks.Length);
|
||||
if (!security.security.HasSynthetic) {
|
||||
for (int i = 0; i < depthCount; i++) {
|
||||
bidsOut[i] = (bids[i].price, bids[i].size, bids[i].notional_size);
|
||||
asksOut[i] = (asks[i].price, asks[i].size, asks[i].notional_size);
|
||||
}
|
||||
return new(bidsOut, asksOut);
|
||||
}
|
||||
Log.assert(security.security.Synthetic.Legs.TryGetFirst(out var leg1));
|
||||
if (!securities.TryGetValue(leg1.Security.Id, out var sec1)) {
|
||||
return Book.Empty;
|
||||
}
|
||||
ulong sec1Time = 0;
|
||||
if (!sec1.md.get_depth(ref sec1Time, ref leg1Bids, ref leg1Asks, depthCount)) {
|
||||
return Book.Empty;
|
||||
}
|
||||
int leg1BidsLevel = 0;
|
||||
int leg1AsksLevel = 0;
|
||||
double leg1BidsConsumed = 0;
|
||||
double leg1AsksConsumed = 0;
|
||||
double takeQuantity;
|
||||
double notional;
|
||||
for (int i = 0; i < depthCount && !bids[i].is_nan; i++) {
|
||||
takeQuantity = bids[i].size;
|
||||
notional = getNotionalValue(ref leg1Bids, ref leg1BidsLevel, ref leg1BidsConsumed, ref takeQuantity);
|
||||
bidsOut[i] = (bids[i].price, bids[i].size, notional);
|
||||
takeQuantity = asks[i].size;
|
||||
notional = getNotionalValue(ref leg1Asks, ref leg1AsksLevel, ref leg1AsksConsumed, ref takeQuantity);
|
||||
asksOut[i] = (asks[i].price, asks[i].size, notional);
|
||||
}
|
||||
return new(bidsOut, asksOut);
|
||||
}
|
||||
|
||||
static double getNotionalValue(ref NativePriceSize[] depth, ref int level, ref double levelConsumed, ref double takeQuantity) {
|
||||
if (level >= depth.Length) {
|
||||
return double.NaN;
|
||||
}
|
||||
double notionalValue = 0;
|
||||
while (takeQuantity > MathUtils.epsilon<double>() && level < depth.Length && !depth[level].is_nan) {
|
||||
var lvlDepth = (double)depth[level].size - levelConsumed;
|
||||
if (lvlDepth <= 0) {
|
||||
level++;
|
||||
levelConsumed = 0;
|
||||
continue;
|
||||
}
|
||||
var lvlTake = Math.Min(takeQuantity, lvlDepth);
|
||||
notionalValue += lvlTake * depth[level].price;
|
||||
levelConsumed += lvlTake;
|
||||
takeQuantity -= lvlDepth;
|
||||
}
|
||||
return notionalValue;
|
||||
}
|
||||
|
||||
public class Book {
|
||||
public Book(IEnumerable<(double Price, double Volume, double NotionalValue)> bids, IEnumerable<(double Price, double Quantity, double NotionalValue)> asks) {
|
||||
Bids = bids;
|
||||
Asks = asks;
|
||||
}
|
||||
|
||||
public static Book Empty =>
|
||||
new(Array.Empty<(double, double, double)>(), Array.Empty<(double, double, double)>());
|
||||
|
||||
public IEnumerable<(double Price, double Volume, double NotionalValue)> Bids { get; }
|
||||
public IEnumerable<(double Price, double Volume, double NotionalValue)> Asks { get; }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -69,6 +69,20 @@ namespace Spark.Api.Orders {
|
||||
public uint security_id => order.security_id;
|
||||
public SecurityImmutable? security => order.security;
|
||||
|
||||
// TODO: remove assumption that synthetic quantities are already notional values
|
||||
public double notional_value =>
|
||||
(security?.HasSynthetic ?? false)
|
||||
&& security.Value.Synthetic.PriceCalc == SyntheticContract.Types.PriceCalc.NotionalLeg
|
||||
? order.order.quantity()
|
||||
: order.order.notional_value();
|
||||
|
||||
// TODO: remove assumption that synthetic quantities are already notional values
|
||||
public double filled_notional_value =>
|
||||
(security?.HasSynthetic ?? false)
|
||||
&& security.Value.Synthetic.PriceCalc == SyntheticContract.Types.PriceCalc.NotionalLeg
|
||||
? order.order.filled_quantity()
|
||||
: order.order.filled_notional_value();
|
||||
|
||||
public bool Equals(OrderRecordTupleWithRoot other) =>
|
||||
order.Equals(other.order) &&
|
||||
EqualityComparer.nullable_equals(_root, other._root);
|
||||
|
||||
@@ -48,6 +48,12 @@ namespace Spark.Api.Orders {
|
||||
public static double quantity(this OrderRecordImmutable order) =>
|
||||
order.HasState ? order.State.quantity() : 0;
|
||||
|
||||
public static double notional_value(this OrderRecordImmutable order) =>
|
||||
order.quantity() * order.ui_price();
|
||||
|
||||
public static double filled_notional_value(this OrderRecordImmutable order) =>
|
||||
order.avg_price() * order.filled_quantity();
|
||||
|
||||
public static double working_quantity(this OrderRecordImmutable order) =>
|
||||
order.HasState ? order.State.working_quantity() : 0;
|
||||
|
||||
@@ -487,5 +493,8 @@ namespace Spark.Api.Orders {
|
||||
CustomOrderDefinition.CustomOneofCase.Composite => "COMP",
|
||||
_ => algo_type.ToString(),
|
||||
};
|
||||
|
||||
public static double notional_value(this FillImmutable fill) =>
|
||||
fill.LastShares * fill.LastPrice;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
|
||||
@@ -10,8 +10,9 @@ namespace bts
|
||||
static int _needs_init_spark_lib = 0;
|
||||
|
||||
public static void init(string config_source = null) {
|
||||
var version = Assembly.GetExecutingAssembly().GetName().Version;
|
||||
Console.WriteLine(
|
||||
$"Configuring BTS with {(config_source is null ? "defaults" : $"'{config_source}'")}");
|
||||
$"Configuring Spark {version} with {(config_source is null ? "defaults" : $"'{config_source}'")}");
|
||||
var args = Environment.GetCommandLineArgs();
|
||||
utils.BTSNativeMethods.provide_command_line(args, args.Length);
|
||||
|
||||
|
||||
@@ -65,10 +65,10 @@ namespace bts.utils {
|
||||
public static bool epsilon_equals<T>(this T x, T y, T epsilon) where T : IFloatingPoint<T> =>
|
||||
MathUtils.epsilon_equals(x, y, epsilon);
|
||||
|
||||
public static bool marginal_equals<T>(T x, T y) where T : IFloatingPoint<T> =>
|
||||
marginal_equals(x, y, MathUtils.epsilon<T>());
|
||||
public static bool MarginalEquals<T>(this T x, T y) where T : IFloatingPoint<T> =>
|
||||
MarginalEquals(x, y, MathUtils.epsilon<T>());
|
||||
|
||||
public static bool marginal_equals<T>(T x, T y, T margin) where T : IFloatingPoint<T> =>
|
||||
public static bool MarginalEquals<T>(this T x, T y, T margin) where T : IFloatingPoint<T> =>
|
||||
MathUtils.marginal_equals(x, y, margin);
|
||||
|
||||
public static bool is_within_range<T>(
|
||||
|
||||
@@ -13,6 +13,9 @@ message DatabentoMarketDataConfig {
|
||||
double replay_sleep_timer_sec = 50;
|
||||
string key = 60;
|
||||
string dataset = 70;
|
||||
bool filter_securities_one_year= 80;
|
||||
uint32 filter_securities_upto_n_months= 80;
|
||||
bool load_reference_data = 90;
|
||||
uint32 debug_sec = 100;
|
||||
bool only_securities = 110;
|
||||
string bbo_start_time = 120;
|
||||
}
|
||||
@@ -0,0 +1,174 @@
|
||||
#include "OrderService.h"
|
||||
#include "lib/api/services/OrderService.pb.h"
|
||||
|
||||
namespace bts::api::orderman {
|
||||
|
||||
#define PARSE_STRING(name, dest, it) \
|
||||
it = j.find(#name); \
|
||||
if (it != j.end()) { \
|
||||
dest.mutable_##name()->clear(); \
|
||||
it->get_to(*dest.mutable_##name()); \
|
||||
}
|
||||
#define PARSE_VALUE(name, dest, it) \
|
||||
it = j.find(#name); \
|
||||
if (it != j.end()) { \
|
||||
dest.set_##name(it->get<decltype(dest.name())>()); \
|
||||
}
|
||||
#define PARSE_STRUCT(name, dest, it) \
|
||||
it = j.find(#name); \
|
||||
if (it != j.end()) { \
|
||||
it->get_to(*dest.mutable_##name()); \
|
||||
}
|
||||
#define PARSE_ARRAY(name, dest, it) \
|
||||
it = j.find(#name); \
|
||||
if (it != j.end() && it->is_array()) { \
|
||||
c.mutable_##name()->Clear(); \
|
||||
it->get_to(*c.mutable_##name()); \
|
||||
}
|
||||
#define PARSE_ONEOF(name, dest, it) \
|
||||
if (!parsed_oneof) { \
|
||||
it = j.find(#name); \
|
||||
if (it != j.end()) { \
|
||||
it->get_to(*dest.mutable_##name()); \
|
||||
parsed_oneof = true; \
|
||||
} \
|
||||
}
|
||||
#define PARSE_ONEOFS(for_each) \
|
||||
{ \
|
||||
bool parsed_oneof = false; \
|
||||
for_each(PARSE_ONEOF) \
|
||||
}
|
||||
|
||||
#define WRITE_VALUE(src, name) j[#name] = src.name()
|
||||
#define WRITE_STRUCT(src, name) \
|
||||
if (src.has_##name()) { \
|
||||
j[#name] = src.name(); \
|
||||
}
|
||||
|
||||
#define WRITE_IF_NOT_ZERO(src, name) \
|
||||
if (src.name() != 0) { \
|
||||
j[#name] = src.name(); \
|
||||
}
|
||||
#define WRITE_IF_NOT_NAN(src, name) \
|
||||
if (!std::isnan(src.name())) { \
|
||||
j[#name] = src.name(); \
|
||||
}
|
||||
#define WRITE_IF_NOT_EMPTY(src, name) \
|
||||
if (!src.name().empty()) { \
|
||||
j[#name] = src.name(); \
|
||||
}
|
||||
#define WRITE_TIMESTAMP(src, name) \
|
||||
j[#name] = bts::core::TimeStamp::from_nanos( \
|
||||
src.name().seconds() * 1'000'000'000 + src.name().nanos()) \
|
||||
.to_iso_string()
|
||||
|
||||
#define PARSE_TIMESTAMP(name, dest, it) \
|
||||
it = j.find(#name); \
|
||||
if (it != j.end()) { \
|
||||
bts::core::TimeStamp tmp_ts = \
|
||||
bts::core::TimeStamp::from_iso_string(it->get<std::string>()); \
|
||||
tmp_ts.to_protobuf(*dest.mutable_##name()); \
|
||||
}
|
||||
|
||||
void to_json(nlohmann::json &j, const Fill &f) {
|
||||
WRITE_IF_NOT_ZERO(f, order_key);
|
||||
WRITE_IF_NOT_ZERO(f, security_key);
|
||||
WRITE_IF_NOT_EMPTY(f, account);
|
||||
WRITE_IF_NOT_NAN(f, last_price);
|
||||
WRITE_IF_NOT_NAN(f, avg_price);
|
||||
WRITE_VALUE(f, side);
|
||||
WRITE_IF_NOT_ZERO(f, last_shares);
|
||||
WRITE_IF_NOT_EMPTY(f, fill_key);
|
||||
WRITE_TIMESTAMP(f, timestamp);
|
||||
WRITE_IF_NOT_ZERO(f, internal_account_id);
|
||||
WRITE_VALUE(f, fill_kind);
|
||||
WRITE_VALUE(f, fill_status);
|
||||
}
|
||||
|
||||
void from_json(const nlohmann::json &j, Fill &f) {
|
||||
nlohmann::json::const_iterator it;
|
||||
PARSE_VALUE(order_key, f, it);
|
||||
PARSE_VALUE(security_key, f, it);
|
||||
PARSE_STRING(account, f, it);
|
||||
PARSE_VALUE(last_price, f, it);
|
||||
PARSE_VALUE(avg_price, f, it);
|
||||
PARSE_VALUE(side, f, it);
|
||||
PARSE_VALUE(last_shares, f, it);
|
||||
PARSE_STRING(fill_key, f, it);
|
||||
PARSE_TIMESTAMP(timestamp, f, it);
|
||||
PARSE_VALUE(internal_account_id, f, it);
|
||||
PARSE_VALUE(fill_kind, f, it);
|
||||
PARSE_VALUE(fill_status, f, it);
|
||||
}
|
||||
|
||||
void to_json(nlohmann::json &j, const NativeOrderState &n) {
|
||||
WRITE_VALUE(n, exchange_order_id);
|
||||
WRITE_VALUE(n, last_msg_seq_num);
|
||||
WRITE_VALUE(n, sub_error_code);
|
||||
WRITE_VALUE(n, native_status);
|
||||
WRITE_VALUE(n, routing_record);
|
||||
WRITE_VALUE(n, native_type);
|
||||
WRITE_VALUE(n, last_transact_time);
|
||||
WRITE_VALUE(n, last_transact_time_unix_ns);
|
||||
WRITE_VALUE(n, stop_px);
|
||||
WRITE_VALUE(n, exchange_order_id_u64);
|
||||
}
|
||||
|
||||
void from_json(const nlohmann::json &j, NativeOrderState &n) {
|
||||
nlohmann::json::const_iterator it;
|
||||
PARSE_STRING(exchange_order_id, n, it);
|
||||
PARSE_VALUE(last_msg_seq_num, n, it);
|
||||
PARSE_VALUE(sub_error_code, n, it);
|
||||
PARSE_VALUE(native_status, n, it);
|
||||
PARSE_STRUCT(routing_record, n, it);
|
||||
PARSE_VALUE(native_type, n, it);
|
||||
PARSE_STRING(last_transact_time, n, it);
|
||||
PARSE_VALUE(last_transact_time_unix_ns, n, it);
|
||||
PARSE_VALUE(stop_px, n, it);
|
||||
PARSE_VALUE(exchange_order_id_u64, n, it);
|
||||
}
|
||||
|
||||
void to_json(nlohmann::json &j, const RoutingRecord &r) {
|
||||
WRITE_IF_NOT_EMPTY(r, session);
|
||||
WRITE_IF_NOT_EMPTY(r, account);
|
||||
WRITE_IF_NOT_EMPTY(r, operator_id);
|
||||
WRITE_VALUE(r, cti_code);
|
||||
WRITE_VALUE(r, customer_or_firm);
|
||||
WRITE_IF_NOT_EMPTY(r, smp_id);
|
||||
WRITE_VALUE(r, smp_instruction);
|
||||
WRITE_IF_NOT_EMPTY(r, location);
|
||||
WRITE_IF_NOT_EMPTY(r, cust_order_handing_inst);
|
||||
WRITE_IF_NOT_EMPTY(r, sender_compid);
|
||||
WRITE_IF_NOT_EMPTY(r, clearing_firm);
|
||||
WRITE_IF_NOT_EMPTY(r, customer_account_ref_id);
|
||||
WRITE_IF_NOT_EMPTY(r, account_code);
|
||||
WRITE_IF_NOT_EMPTY(r, company_id);
|
||||
WRITE_IF_NOT_EMPTY(r, authorized_trader);
|
||||
WRITE_IF_NOT_EMPTY(r, authorized_group);
|
||||
WRITE_IF_NOT_EMPTY(r, cust_order_handing_inst);
|
||||
WRITE_IF_NOT_EMPTY(r, order_server_id);
|
||||
}
|
||||
|
||||
void from_json(const nlohmann::json &j, RoutingRecord &r) {
|
||||
nlohmann::json::const_iterator it;
|
||||
PARSE_STRING(session, r, it);
|
||||
PARSE_STRING(account, r, it);
|
||||
PARSE_STRING(operator_id, r, it);
|
||||
PARSE_VALUE(cti_code, r, it);
|
||||
PARSE_VALUE(customer_or_firm, r, it);
|
||||
PARSE_STRING(smp_id, r, it);
|
||||
PARSE_VALUE(smp_instruction, r, it);
|
||||
PARSE_STRING(location, r, it);
|
||||
PARSE_STRING(cust_order_handling_inst, r, it);
|
||||
PARSE_STRING(sender_compid, r, it);
|
||||
PARSE_STRING(clearing_firm, r, it);
|
||||
PARSE_STRING(customer_account_ref_id, r, it);
|
||||
PARSE_STRING(account_code, r, it);
|
||||
PARSE_STRING(company_id, r, it);
|
||||
PARSE_STRING(authorized_trader, r, it);
|
||||
PARSE_STRING(authorized_group, r, it);
|
||||
PARSE_STRING(cust_order_handing_inst, r, it);
|
||||
PARSE_STRING(order_server_id, r, it);
|
||||
}
|
||||
|
||||
} // namespace bts::api::orderman
|
||||
@@ -1,6 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#include "lib/api/services/OrderService.pb.h"
|
||||
#include "lib/api/services/native_order_type.h"
|
||||
#include "lib/api/services/order_status.h"
|
||||
#include "lib/api/services/time_in_force.h"
|
||||
#include "lib/api/services/trigger_price_type.h"
|
||||
#include "lib/core/EnumMacros.h"
|
||||
#include "lib/core/FMT_IMPL.h"
|
||||
#include "lib/io/JsonReader.h"
|
||||
@@ -10,22 +14,70 @@
|
||||
|
||||
namespace bts::api::orderman {
|
||||
|
||||
PB_ENUM_FROM_JSON(OrderStatus)
|
||||
PB_ENUM_TO_JSON(OrderStatus)
|
||||
PB_ENUM_FROM_JSON(Fill_FillKind)
|
||||
PB_ENUM_TO_JSON(Fill_FillKind)
|
||||
|
||||
PB_ENUM_FROM_JSON(TriggerPriceType)
|
||||
PB_ENUM_TO_JSON(TriggerPriceType)
|
||||
PB_ENUM_FROM_JSON(Fill_FillStatus)
|
||||
PB_ENUM_TO_JSON(Fill_FillStatus)
|
||||
|
||||
void to_json(nlohmann::json &j, const Fill &f);
|
||||
void from_json(const nlohmann::json &j, Fill &f);
|
||||
|
||||
PB_ENUM_FROM_JSON(OrderSubType)
|
||||
PB_ENUM_TO_JSON(OrderSubType)
|
||||
|
||||
PB_ENUM_FROM_JSON(SelfMatchPreventionInstruction)
|
||||
PB_ENUM_TO_JSON(SelfMatchPreventionInstruction)
|
||||
|
||||
PB_ENUM_FROM_JSON(CustomerOrFirm)
|
||||
PB_ENUM_TO_JSON(CustomerOrFirm)
|
||||
|
||||
void to_json(nlohmann::json &j, const RoutingRecord &r);
|
||||
void from_json(const nlohmann::json &j, RoutingRecord &r);
|
||||
|
||||
PB_ENUM_FROM_JSON(NativeOrderStatus)
|
||||
PB_ENUM_TO_JSON(NativeOrderStatus)
|
||||
|
||||
PB_ENUM_FROM_JSON(NativeOrderState_SubError)
|
||||
PB_ENUM_TO_JSON(NativeOrderState_SubError)
|
||||
|
||||
void to_json(nlohmann::json &j, const NativeOrderState &n);
|
||||
void from_json(const nlohmann::json &j, NativeOrderState &n);
|
||||
|
||||
} // namespace bts::api::orderman
|
||||
|
||||
FMT_IMPL(
|
||||
bts::api::orderman::OrderStatus,
|
||||
s,
|
||||
bts::api::orderman::Fill_FillKind,
|
||||
k,
|
||||
"{}",
|
||||
bts::api::orderman::OrderStatus_Name(s));
|
||||
bts::api::orderman::Fill_FillKind_Name(k));
|
||||
|
||||
FMT_IMPL(
|
||||
bts::api::orderman::TriggerPriceType,
|
||||
bts::api::orderman::Fill_FillStatus,
|
||||
s,
|
||||
"{}",
|
||||
bts::api::orderman::Fill_FillStatus(s));
|
||||
|
||||
FMT_IMPL(
|
||||
bts::api::orderman::OrderSubType,
|
||||
t,
|
||||
"{}",
|
||||
bts::api::orderman::TriggerPriceType_Name(t));
|
||||
bts::api::orderman::OrderSubType_Name(t));
|
||||
|
||||
FMT_IMPL(
|
||||
bts::api::orderman::SelfMatchPreventionInstruction,
|
||||
s,
|
||||
"{}",
|
||||
bts::api::orderman::SelfMatchPreventionInstruction_Name(s));
|
||||
|
||||
FMT_IMPL(
|
||||
bts::api::orderman::NativeOrderStatus,
|
||||
s,
|
||||
"{}",
|
||||
bts::api::orderman::NativeOrderStatus_Name(s));
|
||||
|
||||
FMT_IMPL(
|
||||
bts::api::orderman::NativeOrderState_SubError,
|
||||
s,
|
||||
"{}",
|
||||
bts::api::orderman::NativeOrderState_SubError_Name(s));
|
||||
@@ -0,0 +1,57 @@
|
||||
#pragma once
|
||||
|
||||
#include "lib/api/services/OrderService.pb.h"
|
||||
#include "lib/core/EnumMacros.h"
|
||||
#include "lib/core/FMT_IMPL.h"
|
||||
#include "lib/io/JsonReader.h"
|
||||
#include "lib/logging/logging.h"
|
||||
#include "lib/utils/string_utils.h"
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
namespace bts::api::orderman {
|
||||
|
||||
/**
|
||||
* Parses NativeOrderType from a string. Unlike `bts::api::orderman::NativeOrderType_Parse`,
|
||||
* this implementation is case insensitive, and makes the 'NATIVE_ORDER_TYPE_' prefix optional.
|
||||
*/
|
||||
bool native_order_type_parse_smart(
|
||||
const std::string &str, NativeOrderType *result);
|
||||
|
||||
PB_ENUM_FROM_JSON(NativeOrderType)
|
||||
PB_ENUM_TO_JSON(NativeOrderType)
|
||||
|
||||
std::ostream &operator<<(std::ostream &out, NativeOrderType t) noexcept;
|
||||
|
||||
//////////////////////////////////////////////////
|
||||
// Definition
|
||||
//////////////////////////////////////////////////
|
||||
|
||||
inline bool native_order_type_parse_smart(
|
||||
const std::string &str, NativeOrderType *result) {
|
||||
if (NativeOrderType_Parse(str, result)) {
|
||||
return true;
|
||||
}
|
||||
std::string s = bts::system::string_utils::copy_to_upper(str);
|
||||
if (NativeOrderType_Parse(s, result)) {
|
||||
return true;
|
||||
}
|
||||
const char *prefix = "NATIVE_ORDER_TYPE_";
|
||||
if (!s.starts_with(prefix)) {
|
||||
s.insert(0, prefix);
|
||||
}
|
||||
return NativeOrderType_Parse(s, result);
|
||||
}
|
||||
|
||||
inline std::ostream &operator<<(std::ostream &out, NativeOrderType t) noexcept {
|
||||
out << NativeOrderType_Name(t);
|
||||
return out;
|
||||
}
|
||||
|
||||
} // namespace bts::api::orderman
|
||||
|
||||
FMT_IMPL(
|
||||
bts::api::orderman::NativeOrderType,
|
||||
t,
|
||||
"{}",
|
||||
bts::api::orderman::NativeOrderType_Name(t));
|
||||
@@ -0,0 +1,56 @@
|
||||
#pragma once
|
||||
|
||||
#include "lib/api/services/OrderService.pb.h"
|
||||
#include "lib/core/EnumMacros.h"
|
||||
#include "lib/core/FMT_IMPL.h"
|
||||
#include "lib/io/JsonReader.h"
|
||||
#include "lib/logging/logging.h"
|
||||
#include "lib/utils/string_utils.h"
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
namespace bts::api::orderman {
|
||||
|
||||
/**
|
||||
* Parses OrderStatus from a string. Unlike `bts::api::orderman::OrderStatus_Parse`,
|
||||
* this implementation is case insensitive, and makes the 'ORDER_STATUS_' prefix optional.
|
||||
*/
|
||||
bool order_status_parse_smart(const std::string &str, OrderStatus *result);
|
||||
|
||||
PB_ENUM_FROM_JSON(OrderStatus)
|
||||
PB_ENUM_TO_JSON(OrderStatus)
|
||||
|
||||
std::ostream &operator<<(std::ostream &out, OrderStatus s) noexcept;
|
||||
|
||||
//////////////////////////////////////////////////
|
||||
// Definition
|
||||
//////////////////////////////////////////////////
|
||||
|
||||
inline bool order_status_parse_smart(
|
||||
const std::string &str, OrderStatus *result) {
|
||||
if (OrderStatus_Parse(str, result)) {
|
||||
return true;
|
||||
}
|
||||
std::string s = bts::system::string_utils::copy_to_upper(str);
|
||||
if (OrderStatus_Parse(s, result)) {
|
||||
return true;
|
||||
}
|
||||
const char *prefix = "ORDER_STATUS_";
|
||||
if (!s.starts_with(prefix)) {
|
||||
s.insert(0, prefix);
|
||||
}
|
||||
return OrderStatus_Parse(s, result);
|
||||
}
|
||||
|
||||
inline std::ostream &operator<<(std::ostream &out, OrderStatus s) noexcept {
|
||||
out << OrderStatus_Name(s);
|
||||
return out;
|
||||
}
|
||||
|
||||
} // namespace bts::api::orderman
|
||||
|
||||
FMT_IMPL(
|
||||
bts::api::orderman::OrderStatus,
|
||||
s,
|
||||
"{}",
|
||||
bts::api::orderman::OrderStatus_Name(s));
|
||||
@@ -0,0 +1,56 @@
|
||||
#pragma once
|
||||
|
||||
#include "lib/api/services/OrderService.pb.h"
|
||||
#include "lib/core/EnumMacros.h"
|
||||
#include "lib/core/FMT_IMPL.h"
|
||||
#include "lib/io/JsonReader.h"
|
||||
#include "lib/logging/logging.h"
|
||||
#include "lib/utils/string_utils.h"
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
namespace bts::api::orderman {
|
||||
|
||||
/**
|
||||
* Parses TimeInForce from a string. Unlike `bts::api::orderman::TimeInForce_Parse`,
|
||||
* this implementation is case insensitive, and makes the 'TIME_IN_FORCE_' prefix optional.
|
||||
*/
|
||||
bool time_in_force_parse_smart(const std::string &str, TimeInForce *result);
|
||||
|
||||
PB_ENUM_FROM_JSON(TimeInForce)
|
||||
PB_ENUM_TO_JSON(TimeInForce)
|
||||
|
||||
std::ostream &operator<<(std::ostream &out, TimeInForce tif) noexcept;
|
||||
|
||||
//////////////////////////////////////////////////
|
||||
// Definition
|
||||
//////////////////////////////////////////////////
|
||||
|
||||
inline bool time_in_force_parse_smart(
|
||||
const std::string &str, TimeInForce *result) {
|
||||
if (TimeInForce_Parse(str, result)) {
|
||||
return true;
|
||||
}
|
||||
std::string s = bts::system::string_utils::copy_to_upper(str);
|
||||
if (TimeInForce_Parse(s, result)) {
|
||||
return true;
|
||||
}
|
||||
const char *prefix = "TIME_IN_FORCE_";
|
||||
if (!s.starts_with(prefix)) {
|
||||
s.insert(0, prefix);
|
||||
}
|
||||
return TimeInForce_Parse(s, result);
|
||||
}
|
||||
|
||||
inline std::ostream &operator<<(std::ostream &out, TimeInForce tif) noexcept {
|
||||
out << TimeInForce_Name(tif);
|
||||
return out;
|
||||
}
|
||||
|
||||
} // namespace bts::api::orderman
|
||||
|
||||
FMT_IMPL(
|
||||
bts::api::orderman::TimeInForce,
|
||||
v,
|
||||
"{}",
|
||||
bts::api::orderman::TimeInForce_Name(v));
|
||||
@@ -0,0 +1,58 @@
|
||||
#pragma once
|
||||
|
||||
#include "lib/api/services/OrderService.pb.h"
|
||||
#include "lib/core/EnumMacros.h"
|
||||
#include "lib/core/FMT_IMPL.h"
|
||||
#include "lib/io/JsonReader.h"
|
||||
#include "lib/logging/logging.h"
|
||||
#include "lib/utils/string_utils.h"
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
namespace bts::api::orderman {
|
||||
|
||||
/**
|
||||
* Parses TriggerPriceType from a string. Unlike `bts::api::orderman::TriggerPriceType_Parse`,
|
||||
* this implementation is case insensitive, and makes the 'TRIGGER_PRICE_TYPE_' prefix optional.
|
||||
*/
|
||||
bool trigger_price_type_parse_smart(
|
||||
const std::string &str, TriggerPriceType *result);
|
||||
|
||||
PB_ENUM_FROM_JSON(TriggerPriceType)
|
||||
PB_ENUM_TO_JSON(TriggerPriceType)
|
||||
|
||||
std::ostream &operator<<(std::ostream &out, TriggerPriceType t) noexcept;
|
||||
|
||||
//////////////////////////////////////////////////
|
||||
// Definition
|
||||
//////////////////////////////////////////////////
|
||||
|
||||
inline bool trigger_price_type_parse_smart(
|
||||
const std::string &str, TriggerPriceType *result) {
|
||||
if (TriggerPriceType_Parse(str, result)) {
|
||||
return true;
|
||||
}
|
||||
std::string s = bts::system::string_utils::copy_to_upper(str);
|
||||
if (TriggerPriceType_Parse(s, result)) {
|
||||
return true;
|
||||
}
|
||||
const char *prefix = "TRIGGER_PRICE_TYPE_";
|
||||
if (!s.starts_with(prefix)) {
|
||||
s.insert(0, prefix);
|
||||
}
|
||||
return TriggerPriceType_Parse(s, result);
|
||||
}
|
||||
|
||||
inline std::ostream &operator<<(
|
||||
std::ostream &out, TriggerPriceType t) noexcept {
|
||||
out << TriggerPriceType_Name(t);
|
||||
return out;
|
||||
}
|
||||
|
||||
} // namespace bts::api::orderman
|
||||
|
||||
FMT_IMPL(
|
||||
bts::api::orderman::TriggerPriceType,
|
||||
t,
|
||||
"{}",
|
||||
bts::api::orderman::TriggerPriceType_Name(t));
|
||||
@@ -75,6 +75,7 @@ function(get_cpp_sources dest)
|
||||
expression/ParserExceptions.cpp
|
||||
expression/Value.cpp
|
||||
expression/tests/expression_parser_tests.cpp
|
||||
io/JsonReader.cpp
|
||||
logging/LoggerHub.cpp
|
||||
logging/Sink.cpp
|
||||
logging/logging.cpp
|
||||
|
||||
@@ -74,6 +74,7 @@ function(get_cpp_sources dest)
|
||||
expression/ParserExceptions.cpp
|
||||
expression/Value.cpp
|
||||
expression/tests/expression_parser_tests.cpp
|
||||
io/JsonReader.cpp
|
||||
logging/LoggerHub.cpp
|
||||
logging/Sink.cpp
|
||||
logging/logging.cpp
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
#pragma once
|
||||
|
||||
#include "lib/utils/LockBox.h"
|
||||
#include "lib/utils/bts_assert.h"
|
||||
#include "lib/utils/type_name.h"
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
|
||||
namespace bts::collections {
|
||||
|
||||
template <class KeyT, class ValueT>
|
||||
class ConcurrentFactory {
|
||||
utils::LockBox<std::unordered_map<KeyT, std::shared_ptr<ValueT>>> store;
|
||||
|
||||
public:
|
||||
std::shared_ptr<ValueT> add(
|
||||
const KeyT &key,
|
||||
std::function<void(std::shared_ptr<ValueT> &)> callback) {
|
||||
return store.lock([&](auto &map) {
|
||||
auto [it, is_new] = map.try_emplace(key, std::make_shared<ValueT>());
|
||||
bts_assert_msgf(
|
||||
is_new, "{}::add. Duplicate key '{}'",
|
||||
utils::cached_type_name<ConcurrentFactory<KeyT, ValueT>>(), key);
|
||||
callback(it->second);
|
||||
return it->second;
|
||||
});
|
||||
}
|
||||
|
||||
std::size_t erase(const KeyT &key) {
|
||||
return store.lock([&](auto &map) {
|
||||
map.erase(key);
|
||||
return map.size();
|
||||
});
|
||||
}
|
||||
|
||||
std::size_t size() const {
|
||||
return store.lock([&](auto &map) { return map.size(); });
|
||||
}
|
||||
|
||||
template <
|
||||
typename CallbackT = std::function<void(std::shared_ptr<ValueT> &)>>
|
||||
std::shared_ptr<const ValueT> get(const KeyT &key, CallbackT callback) {
|
||||
return store.lock([&](auto &map) {
|
||||
auto [it, is_new] = map.try_emplace(key, std::make_shared<ValueT>());
|
||||
bts_assert_msgf(
|
||||
!is_new, "{}::get. Key not found '{}'",
|
||||
utils::cached_type_name<ConcurrentFactory<KeyT, ValueT>>(), key);
|
||||
callback(it->second);
|
||||
return it->second;
|
||||
});
|
||||
}
|
||||
|
||||
template <
|
||||
typename CallbackT =
|
||||
std::function<void(std::shared_ptr<ValueT> &, bool)>>
|
||||
std::shared_ptr<const ValueT> get_or_add(
|
||||
const KeyT &key, CallbackT callback) {
|
||||
return store.lock([&](auto &map) {
|
||||
auto [it, is_new] = map.try_emplace(key);
|
||||
if (is_new) {
|
||||
it->second = std::make_shared<ValueT>();
|
||||
}
|
||||
callback(it->second, is_new);
|
||||
return it->second;
|
||||
});
|
||||
}
|
||||
|
||||
template <
|
||||
typename CallbackT =
|
||||
std::function<void(std::shared_ptr<ValueT> &, bool)>>
|
||||
std::shared_ptr<const ValueT> replace(const KeyT &key, CallbackT callback) {
|
||||
return store.lock([&](auto &map) {
|
||||
auto [it, is_new] = map.try_emplace(key);
|
||||
bts_assert_msgf(
|
||||
!is_new, "{}::replace. Key not found '{}'",
|
||||
utils::cached_type_name<ConcurrentFactory<KeyT, ValueT>>(), key);
|
||||
it->second = std::make_shared<ValueT>();
|
||||
callback(it->second, is_new);
|
||||
return it->second;
|
||||
});
|
||||
}
|
||||
|
||||
template <
|
||||
typename CallbackT =
|
||||
std::function<void(std::shared_ptr<ValueT> &, bool)>>
|
||||
std::shared_ptr<const ValueT> replace_or_add(
|
||||
const KeyT &key, CallbackT callback) {
|
||||
return store.lock([&](auto &map) {
|
||||
auto [it, is_new] = map.try_emplace(key);
|
||||
it->second = std::make_shared<ValueT>();
|
||||
callback(it->second, is_new);
|
||||
return it->second;
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace bts::collections
|
||||
@@ -0,0 +1,17 @@
|
||||
OkxExeCfg {
|
||||
value{
|
||||
account : "ACC A"
|
||||
ops_id : "op_id_bts"
|
||||
secret_key : "DAF6CBB09475D2520B88D2070788A976"
|
||||
api_key : "4492a9ab-5784-454c-acc7-38dc2a503ed0"
|
||||
passphrase : "Pakistan@123"
|
||||
ws_url : "wss://wspap.okx.com:8443"
|
||||
rest_url : "https://www.okx.com"
|
||||
ws_ping_timeout_seconds : 10
|
||||
ws_ping_interval_seconds : 25
|
||||
ws_reconnect_interval_seconds : 5
|
||||
ws_reconnect_max_count : 5
|
||||
simulated_trading : true
|
||||
tdmode : "cross"
|
||||
}
|
||||
}
|
||||
@@ -1,34 +1,34 @@
|
||||
OrdermanCfg {
|
||||
rule: "DeploymentType.type == 'dev'"
|
||||
value {
|
||||
executor {
|
||||
thread_name: "oman_orders"
|
||||
poll: false
|
||||
hosted_actor: "MDSource"
|
||||
hosted_actor: "OrderManager"
|
||||
hosted_actor: "ExecutionGateway"
|
||||
}
|
||||
executor {
|
||||
thread_name: "oman_slow"
|
||||
poll: false
|
||||
hosted_actor: "*"
|
||||
}
|
||||
}
|
||||
rule: "DeploymentType.type == 'dev'"
|
||||
value {
|
||||
executor {
|
||||
thread_name: "oman_orders"
|
||||
poll: false
|
||||
hosted_actor: "MDSource"
|
||||
hosted_actor: "OrderManager"
|
||||
hosted_actor: "ExecutionGateway"
|
||||
}
|
||||
executor {
|
||||
thread_name: "oman_slow"
|
||||
poll: false
|
||||
hosted_actor: "*"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
OrdermanCfg {
|
||||
value {
|
||||
executor {
|
||||
thread_name: "oman_orders"
|
||||
poll: true
|
||||
hosted_actor: "MDSource"
|
||||
hosted_actor: "OrderManager"
|
||||
hosted_actor: "ExecutionGateway"
|
||||
}
|
||||
executor {
|
||||
thread_name: "oman_slow"
|
||||
poll: false
|
||||
hosted_actor: "*"
|
||||
}
|
||||
}
|
||||
}
|
||||
value {
|
||||
executor {
|
||||
thread_name: "oman_orders"
|
||||
poll: true
|
||||
hosted_actor: "MDSource"
|
||||
hosted_actor: "OrderManager"
|
||||
hosted_actor: "ExecutionGateway"
|
||||
}
|
||||
executor {
|
||||
thread_name: "oman_slow"
|
||||
poll: false
|
||||
hosted_actor: "*"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -37,6 +37,7 @@ SymbolLists {
|
||||
list {
|
||||
key: "CBOE"
|
||||
symbol:"SPX"
|
||||
symbol:"SPXW"
|
||||
}
|
||||
list {
|
||||
key: "BINANCE_US"
|
||||
|
||||
@@ -39,8 +39,6 @@ LadderKnobs {
|
||||
security_timeout_secs: 60
|
||||
use_algo_param_grid: true
|
||||
max_mbo_orders: 200
|
||||
lighten_market_cells: true
|
||||
mimic_tt: true
|
||||
scrollable_tick_window_size: 256
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
syntax = "proto3";
|
||||
package bts.config;
|
||||
message OkxExeCfg {
|
||||
string account = 1;
|
||||
string ops_id = 2;
|
||||
string secret_key = 3;
|
||||
string api_key = 4;
|
||||
string passphrase = 5;
|
||||
string ws_url = 6;
|
||||
string rest_url = 7;
|
||||
uint32 ws_ping_timeout_seconds = 8;
|
||||
uint32 ws_ping_interval_seconds = 9;
|
||||
uint32 ws_reconnect_interval_seconds = 10;
|
||||
uint32 ws_reconnect_max_count = 11;
|
||||
bool simulated_trading = 12;
|
||||
string tdmode = 13;
|
||||
|
||||
}
|
||||
@@ -82,7 +82,7 @@ message LadderKnobs {
|
||||
bool show_scrollbar = 9;
|
||||
// DJ wants bid/ask cells with markets (i.e., size > 0) to have a
|
||||
// lighter background than those without markets.
|
||||
bool lighten_market_cells = 10;
|
||||
// bool lighten_market_cells = 10;
|
||||
|
||||
// For market cell backgrounds, 0 means 'ignore this color setting' (i.e.,
|
||||
// employ the ladder's default behavior instead).
|
||||
@@ -97,9 +97,9 @@ message LadderKnobs {
|
||||
// Pure Red : 0xFFFF0000
|
||||
// Pure Blue : 0xFF00FF00
|
||||
// Pure Green : 0xFF0000FF
|
||||
uint32 bid_market_cell_background = 11;
|
||||
uint32 ask_market_cell_background = 12;
|
||||
bool mimic_tt = 13;
|
||||
// uint32 bid_market_cell_background = 11;
|
||||
// uint32 ask_market_cell_background = 12;
|
||||
// bool mimic_tt = 13;
|
||||
// Defines the number of price rows rendered by the ladder. The smaller this
|
||||
// window is, the cheaper it is to render the ladder grid (redraw prices,
|
||||
// sizes, etc.). However, if the window is too small, scrolling may appear
|
||||
|
||||
@@ -110,6 +110,19 @@
|
||||
ENUM_TO_JSON(type) \
|
||||
ENUM_EQUALITY_OPS(type)
|
||||
|
||||
#define ENUM_CLASS_DEF_NO_JSON(type, for_all) \
|
||||
class type { \
|
||||
public: \
|
||||
enum Value { for_all(ENUM_DECLARE_VALUE) }; \
|
||||
\
|
||||
ENUM_BASIC_MEMBERS(type) \
|
||||
}; \
|
||||
\
|
||||
ENUM_OUTPUT_OPS(type) \
|
||||
ENUM_NAME_DEF(type, for_all) \
|
||||
ENUM_PARSE_DEF(type, for_all) \
|
||||
ENUM_EQUALITY_OPS(type)
|
||||
|
||||
#define ENUM_BITWISE_COMPARISON_OP(enum_type, numeric_type, op) \
|
||||
constexpr enum_type operator op(enum_type lhs, enum_type rhs) noexcept { \
|
||||
return static_cast<enum_type>(static_cast<numeric_type>(lhs) \
|
||||
@@ -154,3 +167,4 @@
|
||||
inline void to_json(nlohmann::json &j, const enum_type &value) { \
|
||||
j = enum_type##_Name(value); \
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include "TimeStamp.h"
|
||||
#include "lib/utils/datetime.h"
|
||||
#include <boost/date_time/posix_time/time_formatters.hpp>
|
||||
#include <google/protobuf/timestamp.pb.h>
|
||||
|
||||
namespace bts::core {
|
||||
|
||||
@@ -43,6 +44,12 @@ bool TimeStamp::from_ymd(
|
||||
return true;
|
||||
}
|
||||
|
||||
void TimeStamp::to_protobuf(google::protobuf::Timestamp &ts) const {
|
||||
ts.set_seconds(seconds());
|
||||
bts_assert(nanos() >= seconds() * 1'000'000'000);
|
||||
ts.set_nanos(nanos() - seconds() * 1'000'000'000);
|
||||
}
|
||||
|
||||
TimeStamp TimeStamp::from_iso_string(const std::string &iso_string) {
|
||||
auto ptime = datetime::parse_iso_date_time_string(iso_string);
|
||||
return from_ptime(ptime);
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#include <boost/date_time/time.hpp>
|
||||
#include <cstdint>
|
||||
#include <fmt/format.h>
|
||||
#include <google/protobuf/timestamp.pb.h>
|
||||
#include <limits>
|
||||
#include <string>
|
||||
|
||||
@@ -87,6 +88,7 @@ struct TimeStamp {
|
||||
// Gets a TimeStamp representing this date at midnight.
|
||||
TimeStamp date() const;
|
||||
constexpr boost::posix_time::ptime to_ptime() const;
|
||||
void to_protobuf(google::protobuf::Timestamp &ts) const;
|
||||
std::string to_iso_string() const;
|
||||
// current epoch ns
|
||||
static uint64_t now_ns() {
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
#pragma once
|
||||
|
||||
#include "lib/core/EnumMacros.h"
|
||||
#include "lib/core/FMT_IMPL.h"
|
||||
#include "lib/domain_model/Security.pb.h"
|
||||
#include "lib/io/JsonReader.h"
|
||||
#include "lib/logging/logging.h"
|
||||
#include "lib/utils/string_utils.h"
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
namespace bts::api {
|
||||
|
||||
/**
|
||||
* Parses PutOrCall from a string. Unlike `bts::api::SecurityType_Parse`, this
|
||||
* implementation is case insensitive, and makes the 'PUTORCALL_' prefix optional.
|
||||
*/
|
||||
bool put_or_call_parse_smart(const std::string &str, PutOrCall *result);
|
||||
|
||||
PB_ENUM_FROM_JSON(PutOrCall)
|
||||
PB_ENUM_TO_JSON(PutOrCall)
|
||||
|
||||
std::ostream &operator<<(std::ostream &out, const PutOrCall &poc) noexcept;
|
||||
|
||||
//////////////////////////////////////////////////
|
||||
// Definition
|
||||
//////////////////////////////////////////////////
|
||||
|
||||
inline bool put_or_call_parse_smart(const std::string &str, PutOrCall *result) {
|
||||
if (PutOrCall_Parse(str, result)) {
|
||||
return true;
|
||||
}
|
||||
std::string s = bts::system::string_utils::copy_to_upper(str);
|
||||
if (PutOrCall_Parse(s, result)) {
|
||||
return true;
|
||||
}
|
||||
const char *prefix = "PUTORCALL_";
|
||||
if (!s.starts_with(prefix)) {
|
||||
s.insert(0, prefix);
|
||||
}
|
||||
return PutOrCall_Parse(s, result);
|
||||
}
|
||||
|
||||
inline std::ostream &operator<<(
|
||||
std::ostream &out, const PutOrCall &poc) noexcept {
|
||||
out << PutOrCall_Name(poc);
|
||||
return out;
|
||||
}
|
||||
|
||||
} // namespace bts::api
|
||||
|
||||
FMT_IMPL(bts::api::PutOrCall, poc, "{}", bts::api::PutOrCall_Name(poc));
|
||||
@@ -2,10 +2,13 @@
|
||||
#include "lib/config/config.h"
|
||||
#include "lib/config/protos/orders_fills_cleanup.pb.h"
|
||||
#include "lib/domain_model/Security.pb.h"
|
||||
#include "lib/io/JsonMacros.h"
|
||||
#include "lib/md/common.h"
|
||||
#include "lib/utils/pb_json_macros.h"
|
||||
|
||||
#include <boost/date_time/posix_time/posix_time.hpp>
|
||||
#include <google/protobuf/text_format.h>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
namespace bts {
|
||||
namespace securities {
|
||||
@@ -191,3 +194,389 @@ std::string unique_descriptor(const api::Security &security) {
|
||||
|
||||
} // namespace securities
|
||||
} // namespace bts
|
||||
|
||||
namespace bts::api {
|
||||
|
||||
using nlohmann::json;
|
||||
using std::uint32_t;
|
||||
|
||||
void to_json(json &j, const api::ContractQuantityInfo &cqi) {
|
||||
PB_JSON_WRITE_IF_NOT_ZERO(j, cqi, min_value);
|
||||
PB_JSON_WRITE_IF_NOT_ZERO(j, cqi, max_value);
|
||||
PB_JSON_WRITE_IF_NOT_ZERO(j, cqi, tick_unit);
|
||||
PB_JSON_WRITE_IF_NOT_ZERO(j, cqi, tick_base);
|
||||
PB_JSON_WRITE_IF_NOT_ZERO(j, cqi, multiplier);
|
||||
PB_JSON_WRITE_IF_NOT_ZERO(j, cqi, max_price);
|
||||
}
|
||||
|
||||
void from_json(const json &j, api::ContractQuantityInfo &cqi) {
|
||||
json::const_iterator it;
|
||||
PB_JSON_READ_VALUE(j, cqi, min_value, it);
|
||||
PB_JSON_READ_VALUE(j, cqi, max_value, it);
|
||||
PB_JSON_READ_VALUE(j, cqi, max_value, it);
|
||||
PB_JSON_READ_VALUE(j, cqi, tick_unit, it);
|
||||
PB_JSON_READ_VALUE(j, cqi, tick_base, it);
|
||||
PB_JSON_READ_VALUE(j, cqi, max_price, it);
|
||||
}
|
||||
|
||||
void to_json(json &j, const api::ContractSpecification &spec) {
|
||||
PB_JSON_WRITE_IF_NOT_ZERO(j, spec, low_tick);
|
||||
PB_JSON_WRITE_IF_NOT_ZERO(j, spec, price_break);
|
||||
PB_JSON_WRITE_IF_NOT_ZERO(j, spec, high_tick);
|
||||
PB_JSON_WRITE_IF_NOT_ZERO(j, spec, cash_per_point);
|
||||
PB_JSON_WRITE_IF_NOT_ZERO(j, spec, price_multiplier);
|
||||
PB_JSON_WRITE_IF_NOT_ZERO(j, spec, main_fraction);
|
||||
PB_JSON_WRITE_IF_NOT_ZERO(j, spec, sub_fraction);
|
||||
PB_JSON_WRITE_IF_NOT_ZERO(j, spec, price_display_format);
|
||||
PB_JSON_WRITE_IF_NOT_EMPTY(j, spec, quantity_infos);
|
||||
PB_JSON_WRITE_IF_NOT_EMPTY(j, spec, notional_quantity_infos);
|
||||
}
|
||||
|
||||
void from_json(const json &j, api::ContractSpecification &spec) {
|
||||
json::const_iterator it;
|
||||
PB_JSON_READ_VALUE(j, spec, low_tick, it);
|
||||
PB_JSON_READ_VALUE(j, spec, price_break, it);
|
||||
PB_JSON_READ_VALUE(j, spec, high_tick, it);
|
||||
PB_JSON_READ_VALUE(j, spec, cash_per_point, it);
|
||||
PB_JSON_READ_VALUE(j, spec, price_multiplier, it);
|
||||
PB_JSON_READ_VALUE(j, spec, main_fraction, it);
|
||||
PB_JSON_READ_VALUE(j, spec, sub_fraction, it);
|
||||
PB_JSON_READ_VALUE(j, spec, price_display_format, it);
|
||||
PB_JSON_READ_ARRAY(j, spec, quantity_infos, it);
|
||||
PB_JSON_READ_ARRAY(j, spec, notional_quantity_infos, it);
|
||||
}
|
||||
|
||||
void to_json(json &, const SpotContract &) {}
|
||||
void from_json(const json &, SpotContract &) {}
|
||||
|
||||
template <class ExpContract>
|
||||
static void exp_to_json(json &j, const ExpContract &ec) {
|
||||
j = bts::core::TimeStamp::from_ymd(
|
||||
ec.expiration_year(), ec.expiration_month(), ec.expiration_day())
|
||||
.to_iso_string();
|
||||
}
|
||||
|
||||
template <class ExpContract>
|
||||
static void exp_from_json(const json &j, ExpContract &ec) {
|
||||
const auto dt = bts::core::TimeStamp::from_iso_string(j.get<std::string>());
|
||||
uint32_t y, m, d;
|
||||
dt.to_date(y, m, d);
|
||||
ec.set_expiration_year(y);
|
||||
ec.set_expiration_month(m);
|
||||
ec.set_expiration_day(d);
|
||||
}
|
||||
|
||||
void to_json(json &j, const FutureContract &f) {
|
||||
exp_to_json(j["expiration"], f);
|
||||
}
|
||||
|
||||
void from_json(const json &j, FutureContract &f) {
|
||||
const auto it = j.find("expiration");
|
||||
if (it != j.end()) {
|
||||
exp_from_json(*it, f);
|
||||
}
|
||||
}
|
||||
|
||||
void to_json(json &j, const OptionContract &opt) {
|
||||
exp_to_json(j["expiration"], opt);
|
||||
PB_JSON_WRITE_VALUE(j, opt, strike);
|
||||
PB_JSON_WRITE_VALUE(j, opt, call_put);
|
||||
}
|
||||
|
||||
void from_json(const json &j, OptionContract &opt) {
|
||||
json::const_iterator it;
|
||||
it = j.find("expiration");
|
||||
if (it != j.end()) {
|
||||
exp_from_json(*it, opt);
|
||||
}
|
||||
PB_JSON_READ_VALUE(j, opt, strike, it);
|
||||
PB_JSON_READ_VALUE(j, opt, call_put, it);
|
||||
}
|
||||
|
||||
void to_json(json &j, const ContractLeg &leg) {
|
||||
PB_JSON_WRITE_STRUCT(j, leg, security);
|
||||
PB_JSON_WRITE_VALUE(j, leg, quantity);
|
||||
PB_JSON_WRITE_VALUE(j, leg, side);
|
||||
PB_JSON_WRITE_VALUE(j, leg, price_multiplier);
|
||||
PB_JSON_WRITE_IF_NOT_ZERO(j, leg, leg_delta);
|
||||
PB_JSON_WRITE_VALUE(j, leg, price_calc);
|
||||
}
|
||||
|
||||
void from_json(const json &j, ContractLeg &leg) {
|
||||
json::const_iterator it;
|
||||
PB_JSON_READ_STRUCT(j, leg, security, it);
|
||||
PB_JSON_READ_VALUE(j, leg, quantity, it);
|
||||
PB_JSON_READ_VALUE(j, leg, side, it);
|
||||
PB_JSON_READ_VALUE(j, leg, price_multiplier, it);
|
||||
PB_JSON_READ_VALUE(j, leg, leg_delta, it);
|
||||
PB_JSON_READ_VALUE(j, leg, price_calc, it);
|
||||
}
|
||||
|
||||
void to_json(json &j, const SpreadContract &spd) {
|
||||
PB_JSON_WRITE_IF_NOT_EMPTY(j, spd, legs);
|
||||
}
|
||||
|
||||
void from_json(const json &j, SpreadContract &spd) {
|
||||
json::const_iterator it;
|
||||
PB_JSON_READ_ARRAY(j, spd, legs, it);
|
||||
}
|
||||
|
||||
void to_json(json &j, const SyntheticContract &syn) {
|
||||
PB_JSON_WRITE_IF_NOT_EMPTY(j, syn, name);
|
||||
PB_JSON_WRITE_IF_NOT_ZERO(j, syn, user_id);
|
||||
PB_JSON_WRITE_IF_NOT_EMPTY(j, syn, legs);
|
||||
PB_JSON_WRITE_VALUE(j, syn, price_calc);
|
||||
}
|
||||
|
||||
void from_json(const json &j, SyntheticContract &syn) {
|
||||
json::const_iterator it;
|
||||
PB_JSON_READ_STRING(j, syn, name, it);
|
||||
PB_JSON_READ_VALUE(j, syn, user_id, it);
|
||||
PB_JSON_READ_ARRAY(j, syn, legs, it);
|
||||
PB_JSON_READ_VALUE(j, syn, price_calc, it);
|
||||
}
|
||||
|
||||
void to_json(json &j, const GLBXExchangeData &data) {
|
||||
PB_JSON_WRITE_IF_NOT_ZERO(j, data, exchange_id);
|
||||
PB_JSON_WRITE_IF_NOT_EMPTY(j, data, security_desc);
|
||||
PB_JSON_WRITE_IF_NOT_EMPTY(j, data, cfi_code);
|
||||
PB_JSON_WRITE_IF_NOT_EMPTY(j, data, cme_symbol);
|
||||
PB_JSON_WRITE_IF_NOT_ZERO(j, data, market_segment_id);
|
||||
}
|
||||
|
||||
void from_json(const json &j, GLBXExchangeData &data) {
|
||||
json::const_iterator it;
|
||||
PB_JSON_READ_VALUE(j, data, exchange_id, it);
|
||||
PB_JSON_READ_STRING(j, data, security_desc, it);
|
||||
PB_JSON_READ_STRING(j, data, cfi_code, it);
|
||||
PB_JSON_READ_STRING(j, data, cme_symbol, it);
|
||||
PB_JSON_READ_VALUE(j, data, market_segment_id, it);
|
||||
}
|
||||
|
||||
void to_json(json &j, const CBOEExchangeData &data) {
|
||||
PB_JSON_WRITE_IF_NOT_ZERO(j, data, exchange_id);
|
||||
PB_JSON_WRITE_IF_NOT_EMPTY(j, data, security_desc);
|
||||
PB_JSON_WRITE_IF_NOT_ZERO(j, data, class_key);
|
||||
PB_JSON_WRITE_IF_NOT_EMPTY(j, data, source);
|
||||
}
|
||||
|
||||
void from_json(const json &j, CBOEExchangeData &data) {
|
||||
json::const_iterator it;
|
||||
PB_JSON_READ_VALUE(j, data, exchange_id, it);
|
||||
PB_JSON_READ_STRING(j, data, security_desc, it);
|
||||
PB_JSON_READ_VALUE(j, data, class_key, it);
|
||||
PB_JSON_READ_STRING(j, data, source, it);
|
||||
}
|
||||
|
||||
void to_json(json &j, const CBOECFEExchangeData &data) {
|
||||
PB_JSON_WRITE_IF_NOT_ZERO(j, data, exchange_id);
|
||||
PB_JSON_WRITE_IF_NOT_EMPTY(j, data, security_desc);
|
||||
PB_JSON_WRITE_IF_NOT_ZERO(j, data, class_key);
|
||||
}
|
||||
|
||||
void from_json(const json &j, CBOECFEExchangeData &data) {
|
||||
json::const_iterator it;
|
||||
PB_JSON_READ_VALUE(j, data, exchange_id, it);
|
||||
PB_JSON_READ_STRING(j, data, security_desc, it);
|
||||
PB_JSON_READ_VALUE(j, data, class_key, it);
|
||||
}
|
||||
|
||||
void to_json(json &j, const ICEExchangeData &data) {
|
||||
PB_JSON_WRITE_IF_NOT_ZERO(j, data, exchange_id);
|
||||
PB_JSON_WRITE_IF_NOT_EMPTY(j, data, security_desc);
|
||||
PB_JSON_WRITE_IF_NOT_ZERO(j, data, price_denominator);
|
||||
PB_JSON_WRITE_IF_NOT_ZERO(j, data, deal_denominator);
|
||||
PB_JSON_WRITE_IF_NOT_ZERO(j, data, settle_denominator);
|
||||
PB_JSON_WRITE_IF_NOT_EMPTY(j, data, market_description);
|
||||
PB_JSON_WRITE_IF_NOT_EMPTY(j, data, cleared_alias);
|
||||
PB_JSON_WRITE_IF_NOT_EMPTY(j, data, product_name);
|
||||
PB_JSON_WRITE_IF_NOT_EMPTY(j, data, hub_alias);
|
||||
PB_JSON_WRITE_IF_NOT_EMPTY(j, data, strip_name);
|
||||
PB_JSON_WRITE_IF_NOT_EMPTY(j, data, mic_code);
|
||||
PB_JSON_WRITE_IF_TRUE(j, data, is_strip);
|
||||
}
|
||||
|
||||
void from_json(const json &j, ICEExchangeData &data) {
|
||||
json::const_iterator it;
|
||||
PB_JSON_READ_VALUE(j, data, exchange_id, it);
|
||||
PB_JSON_READ_STRING(j, data, security_desc, it);
|
||||
PB_JSON_READ_VALUE(j, data, price_denominator, it);
|
||||
PB_JSON_READ_VALUE(j, data, deal_denominator, it);
|
||||
PB_JSON_READ_VALUE(j, data, settle_denominator, it);
|
||||
PB_JSON_READ_STRING(j, data, market_description, it);
|
||||
PB_JSON_READ_STRING(j, data, cleared_alias, it);
|
||||
PB_JSON_READ_STRING(j, data, product_name, it);
|
||||
PB_JSON_READ_STRING(j, data, hub_alias, it);
|
||||
PB_JSON_READ_STRING(j, data, strip_name, it);
|
||||
PB_JSON_READ_STRING(j, data, mic_code, it);
|
||||
PB_JSON_READ_VALUE(j, data, is_strip, it);
|
||||
}
|
||||
|
||||
void to_json(json &j, const TAIFEXExchangeData &data) {
|
||||
PB_JSON_WRITE_IF_NOT_ZERO(j, data, exchange_id);
|
||||
PB_JSON_WRITE_IF_NOT_EMPTY(j, data, security_desc);
|
||||
PB_JSON_WRITE_IF_NOT_ZERO(j, data, class_key);
|
||||
}
|
||||
|
||||
void from_json(const json &j, TAIFEXExchangeData &data) {
|
||||
json::const_iterator it;
|
||||
PB_JSON_READ_VALUE(j, data, exchange_id, it);
|
||||
PB_JSON_READ_STRING(j, data, security_desc, it);
|
||||
PB_JSON_READ_VALUE(j, data, class_key, it);
|
||||
}
|
||||
|
||||
void to_json(json &j, const DeribitExchangeData &data) {
|
||||
PB_JSON_WRITE_IF_NOT_ZERO(j, data, exchange_id);
|
||||
PB_JSON_WRITE_IF_NOT_EMPTY(j, data, security_desc);
|
||||
}
|
||||
|
||||
void from_json(const json &j, DeribitExchangeData &data) {
|
||||
json::const_iterator it;
|
||||
PB_JSON_READ_VALUE(j, data, exchange_id, it);
|
||||
PB_JSON_READ_STRING(j, data, security_desc, it);
|
||||
}
|
||||
|
||||
void to_json(json &j, const FTXExchangeData &data) {
|
||||
PB_JSON_WRITE_IF_NOT_ZERO(j, data, exchange_id);
|
||||
PB_JSON_WRITE_IF_NOT_EMPTY(j, data, security_desc);
|
||||
}
|
||||
|
||||
void from_json(const json &j, FTXExchangeData &data) {
|
||||
json::const_iterator it;
|
||||
PB_JSON_READ_VALUE(j, data, exchange_id, it);
|
||||
PB_JSON_READ_STRING(j, data, security_desc, it);
|
||||
}
|
||||
|
||||
void to_json(json &j, const BinanceExchangeData &data) {
|
||||
PB_JSON_WRITE_IF_NOT_ZERO(j, data, exchange_id);
|
||||
PB_JSON_WRITE_IF_NOT_EMPTY(j, data, security_desc);
|
||||
}
|
||||
|
||||
void from_json(const json &j, BinanceExchangeData &data) {
|
||||
json::const_iterator it;
|
||||
PB_JSON_READ_VALUE(j, data, exchange_id, it);
|
||||
PB_JSON_READ_STRING(j, data, security_desc, it);
|
||||
}
|
||||
|
||||
void to_json(json &j, const BinanceUsdsFuturesExchangeData &data) {
|
||||
PB_JSON_WRITE_IF_NOT_ZERO(j, data, exchange_id);
|
||||
PB_JSON_WRITE_IF_NOT_EMPTY(j, data, security_desc);
|
||||
}
|
||||
|
||||
void from_json(const json &j, BinanceUsdsFuturesExchangeData &data) {
|
||||
json::const_iterator it;
|
||||
PB_JSON_READ_VALUE(j, data, exchange_id, it);
|
||||
PB_JSON_READ_STRING(j, data, security_desc, it);
|
||||
}
|
||||
|
||||
void to_json(json &j, const BinanceCoinFuturesExchangeData &data) {
|
||||
PB_JSON_WRITE_IF_NOT_ZERO(j, data, exchange_id);
|
||||
PB_JSON_WRITE_IF_NOT_EMPTY(j, data, security_desc);
|
||||
}
|
||||
|
||||
void from_json(const json &j, BinanceCoinFuturesExchangeData &data) {
|
||||
json::const_iterator it;
|
||||
PB_JSON_READ_VALUE(j, data, exchange_id, it);
|
||||
PB_JSON_READ_STRING(j, data, security_desc, it);
|
||||
}
|
||||
|
||||
void to_json(json &j, const BinanceUsExchangeData &data) {
|
||||
PB_JSON_WRITE_IF_NOT_ZERO(j, data, exchange_id);
|
||||
PB_JSON_WRITE_IF_NOT_EMPTY(j, data, security_desc);
|
||||
}
|
||||
|
||||
void from_json(const json &j, BinanceUsExchangeData &data) {
|
||||
json::const_iterator it;
|
||||
PB_JSON_READ_VALUE(j, data, exchange_id, it);
|
||||
PB_JSON_READ_STRING(j, data, security_desc, it);
|
||||
}
|
||||
|
||||
void to_json(json &j, const OkxExchangeData &data) {
|
||||
PB_JSON_WRITE_IF_NOT_ZERO(j, data, exchange_id);
|
||||
PB_JSON_WRITE_IF_NOT_EMPTY(j, data, security_desc);
|
||||
}
|
||||
|
||||
void from_json(const json &j, OkxExchangeData &data) {
|
||||
json::const_iterator it;
|
||||
PB_JSON_READ_VALUE(j, data, exchange_id, it);
|
||||
PB_JSON_READ_STRING(j, data, security_desc, it);
|
||||
}
|
||||
|
||||
void to_json(json &j, const CoinbaseExchangeData &data) {
|
||||
PB_JSON_WRITE_IF_NOT_ZERO(j, data, exchange_id);
|
||||
PB_JSON_WRITE_IF_NOT_EMPTY(j, data, security_desc);
|
||||
}
|
||||
|
||||
void from_json(const json &j, CoinbaseExchangeData &data) {
|
||||
json::const_iterator it;
|
||||
PB_JSON_READ_VALUE(j, data, exchange_id, it);
|
||||
PB_JSON_READ_STRING(j, data, security_desc, it);
|
||||
}
|
||||
|
||||
void to_json(json &j, const Security &s) {
|
||||
// clang-format off
|
||||
#define CONTRACT_ONEOF(oneof) \
|
||||
oneof(j, s, Spot, spot) \
|
||||
oneof(j, s, Future, future) \
|
||||
oneof(j, s, Option, option) \
|
||||
oneof(j, s, Spread, spread) \
|
||||
oneof(j, s, Synthetic, synthetic)
|
||||
|
||||
#define EXCHANGE_DATA_ONEOF(oneof) \
|
||||
oneof(j, s, Glbx, glbx) \
|
||||
oneof(j, s, Cboe, cboe) \
|
||||
oneof(j, s, Cfe, cfe) \
|
||||
oneof(j, s, Ice, ice) \
|
||||
oneof(j, s, Deribit, deribit) \
|
||||
oneof(j, s, Ftx, ftx) \
|
||||
oneof(j, s, Binance, binance) \
|
||||
oneof(j, s, BinanceCoinFutures, binance_coin_futures) \
|
||||
oneof(j, s, BinanceUsdsFutures, binance_usds_futures) \
|
||||
oneof(j, s, BinanceUs, binance_us) \
|
||||
oneof(j, s, Coinbase, coinbase) \
|
||||
oneof(j, s, Okx, okx) \
|
||||
oneof(j, s, Taifex, taifex)
|
||||
// clang-format on
|
||||
|
||||
PB_JSON_WRITE_IF_NOT_ZERO(j, s, id);
|
||||
PB_JSON_WRITE_IF_NOT_EMPTY(j, s, symbol);
|
||||
PB_JSON_WRITE_VALUE(j, s, exchange);
|
||||
PB_JSON_WRITE_STRUCT(j, s, contract_spec);
|
||||
PB_JSON_WRITE_IF_NOT_EMPTY(j, s, currency);
|
||||
PB_JSON_WRITE_ONEOF(j, s, Contract, CONTRACT_ONEOF);
|
||||
PB_JSON_WRITE_ONEOF(j, s, ExchangeData, EXCHANGE_DATA_ONEOF);
|
||||
}
|
||||
|
||||
void from_json(const json &j, Security &s) {
|
||||
json::const_iterator it;
|
||||
|
||||
#define SEC_TYPE_ONEOF(oneof) \
|
||||
oneof(j, s, future, it); \
|
||||
oneof(j, s, option, it); \
|
||||
oneof(j, s, spread, it); \
|
||||
oneof(j, s, synthetic, it)
|
||||
|
||||
#define CONTRACT_CASE_ONEOF(oneof) \
|
||||
oneof(j, s, glbx, it); \
|
||||
oneof(j, s, cboe, it); \
|
||||
oneof(j, s, cfe, it); \
|
||||
oneof(j, s, ice, it); \
|
||||
oneof(j, s, deribit, it); \
|
||||
oneof(j, s, ftx, it); \
|
||||
oneof(j, s, binance, it); \
|
||||
oneof(j, s, binance_usds_futures, it); \
|
||||
oneof(j, s, binance_coin_futures, it); \
|
||||
oneof(j, s, binance_us, it); \
|
||||
oneof(j, s, coinbase, it); \
|
||||
oneof(j, s, okx, it); \
|
||||
oneof(j, s, taifex, it);
|
||||
|
||||
PB_JSON_READ_VALUE(j, s, id, it);
|
||||
PB_JSON_READ_STRING(j, s, symbol, it);
|
||||
PB_JSON_READ_STRING(j, s, currency, it);
|
||||
PB_JSON_READ_STRUCT(j, s, contract_spec, it);
|
||||
PB_JSON_READ_ONEOFS(SEC_TYPE_ONEOF);
|
||||
PB_JSON_READ_ONEOFS(CONTRACT_CASE_ONEOF);
|
||||
}
|
||||
|
||||
} // namespace bts::api
|
||||
@@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "lib/core/FMT_IMPL.h"
|
||||
#include "lib/domain_model/PutOrCall.h"
|
||||
#include "lib/domain_model/Security.pb.h"
|
||||
#include "lib/domain_model/SecurityType.h"
|
||||
#include "lib/domain_model/Side.h"
|
||||
@@ -9,6 +9,7 @@
|
||||
#include "lib/utils/bts_assert.h"
|
||||
|
||||
#include <cmath>
|
||||
#include <nlohmann/json_fwd.hpp>
|
||||
|
||||
namespace boost {
|
||||
namespace gregorian {
|
||||
@@ -16,14 +17,83 @@ class date;
|
||||
}
|
||||
} // namespace boost
|
||||
|
||||
namespace bts {
|
||||
namespace bts::api {
|
||||
|
||||
namespace api {
|
||||
class Security;
|
||||
class ICEExchangeData;
|
||||
} // namespace api
|
||||
|
||||
namespace securities {
|
||||
void to_json(nlohmann::json &j, const ContractQuantityInfo &c);
|
||||
void from_json(const nlohmann::json &j, ContractQuantityInfo &c);
|
||||
|
||||
void to_json(nlohmann::json &j, const ContractSpecification &c);
|
||||
void from_json(const nlohmann::json &j, ContractSpecification &c);
|
||||
|
||||
void to_json(nlohmann::json &j, const ContractLeg &c);
|
||||
void from_json(const nlohmann::json &j, ContractLeg &c);
|
||||
|
||||
void to_json(nlohmann::json &j, const SpotContract &c);
|
||||
void from_json(const nlohmann::json &j, SpotContract &c);
|
||||
|
||||
void to_json(nlohmann::json &j, const FutureContract &c);
|
||||
void from_json(const nlohmann::json &j, FutureContract &c);
|
||||
|
||||
void to_json(nlohmann::json &j, const OptionContract &c);
|
||||
void from_json(const nlohmann::json &j, OptionContract &c);
|
||||
|
||||
void to_json(nlohmann::json &j, const SpreadContract &c);
|
||||
void from_json(const nlohmann::json &j, SpreadContract &c);
|
||||
|
||||
void to_json(nlohmann::json &j, const SyntheticContract_PriceCalc &c);
|
||||
void from_json(const nlohmann::json &j, SyntheticContract_PriceCalc &c);
|
||||
|
||||
void to_json(nlohmann::json &j, const SyntheticContract &c);
|
||||
void from_json(const nlohmann::json &j, SyntheticContract &c);
|
||||
|
||||
void to_json(nlohmann::json &j, const GLBXExchangeData &data);
|
||||
void from_json(const nlohmann::json &j, GLBXExchangeData &data);
|
||||
|
||||
void to_json(nlohmann::json &j, const CBOEExchangeData &data);
|
||||
void from_json(const nlohmann::json &j, CBOEExchangeData &data);
|
||||
|
||||
void to_json(nlohmann::json &j, const CBOECFEExchangeData &data);
|
||||
void from_json(const nlohmann::json &j, CBOECFEExchangeData &data);
|
||||
|
||||
void to_json(nlohmann::json &j, const ICEExchangeData &data);
|
||||
void from_json(const nlohmann::json &j, ICEExchangeData &data);
|
||||
|
||||
void to_json(nlohmann::json &j, const DeribitExchangeData &data);
|
||||
void from_json(const nlohmann::json &j, DeribitExchangeData &data);
|
||||
|
||||
void to_json(nlohmann::json &j, const FTXExchangeData &data);
|
||||
void from_json(const nlohmann::json &j, FTXExchangeData &data);
|
||||
|
||||
void to_json(nlohmann::json &j, const BinanceExchangeData &data);
|
||||
void from_json(const nlohmann::json &j, BinanceExchangeData &data);
|
||||
|
||||
void to_json(nlohmann::json &j, const BinanceUsdsFuturesExchangeData &data);
|
||||
void from_json(const nlohmann::json &j, BinanceUsdsFuturesExchangeData &data);
|
||||
|
||||
void to_json(nlohmann::json &j, const BinanceCoinFuturesExchangeData &data);
|
||||
void from_json(const nlohmann::json &j, BinanceCoinFuturesExchangeData &data);
|
||||
|
||||
void to_json(nlohmann::json &j, const BinanceUsExchangeData &data);
|
||||
void from_json(const nlohmann::json &j, BinanceUsExchangeData &data);
|
||||
|
||||
void to_json(nlohmann::json &j, const OkxExchangeData &data);
|
||||
void from_json(const nlohmann::json &j, OkxExchangeData &data);
|
||||
|
||||
void to_json(nlohmann::json &j, const CoinbaseExchangeData &data);
|
||||
void from_json(const nlohmann::json &j, CoinbaseExchangeData &data);
|
||||
|
||||
void to_json(nlohmann::json &j, const Security &s);
|
||||
void from_json(const nlohmann::json &j, Security &s);
|
||||
|
||||
PB_ENUM_FROM_JSON(SyntheticContract_PriceCalc);
|
||||
PB_ENUM_TO_JSON(SyntheticContract_PriceCalc);
|
||||
|
||||
} // namespace bts::api
|
||||
|
||||
namespace bts::securities {
|
||||
|
||||
/**
|
||||
* Gets a descriptor string for a security (should be unique within a particular exchange).
|
||||
@@ -60,7 +130,8 @@ inline api::Side other_side(api::Side side) {
|
||||
return side;
|
||||
}
|
||||
}
|
||||
template <class V> class TwoSided {
|
||||
template <class V>
|
||||
class TwoSided {
|
||||
public:
|
||||
TwoSided() : buy(), sell() {}
|
||||
|
||||
@@ -104,7 +175,8 @@ inline bool is_worse(api::Side s, Price p1, Price p2) {
|
||||
return is_better(s, p2, p1);
|
||||
}
|
||||
|
||||
template <class C> void apply_to_both_sides(C c) {
|
||||
template <class C>
|
||||
void apply_to_both_sides(C c) {
|
||||
c(api::SIDE_BUY);
|
||||
c(api::SIDE_SELL);
|
||||
}
|
||||
@@ -178,8 +250,8 @@ inline Price encode_exchange_price(api::Security const &sec, Price in) {
|
||||
return in;
|
||||
}
|
||||
|
||||
inline double
|
||||
raw_round_to_quote_price(double raw_price, double tick_size, api::Side side) {
|
||||
inline double raw_round_to_quote_price(
|
||||
double raw_price, double tick_size, api::Side side) {
|
||||
double factor = 1000000000;
|
||||
auto i_price = llround(factor * raw_price);
|
||||
auto i_tick_size = llround(tick_size * factor);
|
||||
@@ -222,8 +294,8 @@ inline double checked_round_to_quote_price(
|
||||
}
|
||||
}
|
||||
|
||||
inline Price
|
||||
round_to_quote_price(api::Security const &sec, Price in, api::Side side) {
|
||||
inline Price round_to_quote_price(
|
||||
api::Security const &sec, Price in, api::Side side) {
|
||||
auto p = in.value();
|
||||
bts_assert_d(!std::isnan(p));
|
||||
if (p < -sec.contract_spec().price_break() ||
|
||||
@@ -287,8 +359,8 @@ inline const std::string &descriptor(
|
||||
// Looking to round md::IntPrice? Look in IntPrice.h
|
||||
|
||||
// computes price_2 - price_1 in ticks
|
||||
inline int
|
||||
price_diff_to_ticks(double price_1, double price_2, double tick_size) {
|
||||
inline int price_diff_to_ticks(
|
||||
double price_1, double price_2, double tick_size) {
|
||||
return static_cast<int>(std::round((price_2 - price_1) / tick_size));
|
||||
}
|
||||
// returns the difference (in ticks) bw and order and a market.
|
||||
@@ -312,8 +384,8 @@ inline int order_diff_market(
|
||||
// simple double equality can not be used to compare prices.
|
||||
// this function deems 2 prices to be equal if they are within
|
||||
// 1 % of the tick size
|
||||
inline bool
|
||||
test_price_equality(double price_1, double price_2, double tick_size) {
|
||||
inline bool test_price_equality(
|
||||
double price_1, double price_2, double tick_size) {
|
||||
return std::fabs(price_2 - price_1) < 0.01 * tick_size;
|
||||
}
|
||||
inline bool gr_than_eq(double p1, double p2, double tick_size) {
|
||||
@@ -362,7 +434,4 @@ inline bool is_spread_of_strips(const bts::api::Security &sec) {
|
||||
return false;
|
||||
}
|
||||
|
||||
} // namespace securities
|
||||
} // namespace bts
|
||||
|
||||
FMT_IMPL(bts::api::Security, s, "{}", bts::securities::descriptor(s));
|
||||
} // namespace bts::securities
|
||||
@@ -205,6 +205,7 @@ message SyntheticContract {
|
||||
enum PriceCalc {
|
||||
PRICECALC_STANDARD = 0;
|
||||
PRICECALC_NET_CHANGE = 1;
|
||||
PRICECALC_NOTIONAL_LEG = 2;
|
||||
// RATIO = 2;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,18 +3,14 @@
|
||||
#define _TO_JSON_FIELD(field) j[#field] = t.field;
|
||||
#define _FROM_JSON_FIELD(field) bts::io::JsonReader{&j, #field}.read(t.field);
|
||||
|
||||
#define SPARK_TO_JSON_IMPL(type, for_each) \
|
||||
inline void to_json(nlohmann::json &j, const type &t) { \
|
||||
FOR_EACH(_TO_JSON_FIELD) \
|
||||
}
|
||||
#define SPARK_TO_JSON_IMPL(type, fields) \
|
||||
void to_json(nlohmann::json &j, const type &t) { fields(_TO_JSON_FIELD) }
|
||||
|
||||
#define SPARK_FROM_JSON_IMPL(type, for_each) \
|
||||
inline void from_json(const nlohmann::json &j, type &t) { \
|
||||
FOR_EACH(_FROM_JSON_FIELD) \
|
||||
}
|
||||
#define SPARK_FROM_JSON_IMPL(type, fields) \
|
||||
void from_json(const nlohmann::json &j, type &t) { fields(_FROM_JSON_FIELD) }
|
||||
|
||||
#define OSTREAM_JSON_IMPL(type) \
|
||||
inline std::ostream &operator<<(std::ostream &out, const type &t) { \
|
||||
std::ostream &operator<<(std::ostream &out, const type &t) { \
|
||||
nlohmann::json j; \
|
||||
to_json(j, t); \
|
||||
out << j.dump(2); \
|
||||
|
||||
+39
-1
@@ -94,7 +94,18 @@ class JsonReader {
|
||||
* @return True if the read succeeds, otherwise false
|
||||
*/
|
||||
template <class T>
|
||||
bool read_or(const std::string &name, T &result, T default_value) const;
|
||||
bool read_or(const std::string &name, T &result, T default_value = {}) const;
|
||||
|
||||
template <class T>
|
||||
bool read_optional_or(
|
||||
std::optional<T> &result,
|
||||
std::optional<T> default_value = std::nullopt) const;
|
||||
|
||||
template <class T>
|
||||
bool read_optional_or(
|
||||
const std::string &name,
|
||||
std::optional<T> &result,
|
||||
std::optional<T> default_value = std::nullopt) const;
|
||||
|
||||
private:
|
||||
const nlohmann::json *json_;
|
||||
@@ -269,6 +280,33 @@ inline bool JsonReader::read_or(
|
||||
return false;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
inline bool JsonReader::read_optional_or(
|
||||
std::optional<T> &result, std::optional<T> default_value) const {
|
||||
if (read_optional(result)) {
|
||||
return true;
|
||||
}
|
||||
result = std::move(default_value);
|
||||
return false;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
inline bool JsonReader::read_optional_or(
|
||||
const std::string &name,
|
||||
std::optional<T> &result,
|
||||
std::optional<T> default_value) const {
|
||||
if (json_ && json_->is_object()) {
|
||||
auto it = json_->find(name);
|
||||
if (it != json_->end()) {
|
||||
if (JsonReader{&it.value()}.read_optional(result)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
result = std::move(default_value);
|
||||
return false;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
bool JsonReader::read_flat(
|
||||
const std::vector<nlohmann::json> &items, std::vector<T> &result) {
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "lib/logging/logging.h"
|
||||
|
||||
#include <atomic>
|
||||
#include <curl/curl.h>
|
||||
#include <curl/easy.h>
|
||||
@@ -12,6 +14,7 @@ struct CurlSession {
|
||||
|
||||
CurlSession() {
|
||||
if (++ref_count == 1) {
|
||||
LOG(Http, Info, "Initializing curl (global)");
|
||||
curl_global_init(CURL_GLOBAL_ALL);
|
||||
}
|
||||
curl = curl_easy_init();
|
||||
@@ -20,6 +23,7 @@ struct CurlSession {
|
||||
~CurlSession() {
|
||||
curl_easy_cleanup(curl);
|
||||
if (--ref_count == 0) {
|
||||
LOG(Http, Info, "Cleaning up curl (global)");
|
||||
curl_global_cleanup();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,7 +34,31 @@ static size_t write_headers(
|
||||
void *ptr, size_t size, size_t nmemb, IHttpResponse *data) {
|
||||
return data->write_headers((char *)ptr, size, nmemb);
|
||||
}
|
||||
|
||||
// static int curl_debug_callback(CURL *handle, curl_infotype type, char *data, size_t size, void *userptr) {
|
||||
// (void)handle;
|
||||
// (void)type;
|
||||
// (void)userptr;
|
||||
// switch (type) {
|
||||
// case CURLINFO_TEXT:
|
||||
// LOG(Http, Info, "CURLINFO_TEXT: {}", std::string(data, size));
|
||||
// break;
|
||||
// case CURLINFO_HEADER_IN:
|
||||
// LOG(Http, Info, "Header In: {}", std::string(data, size));
|
||||
// break;
|
||||
// case CURLINFO_HEADER_OUT:
|
||||
// LOG(Http, Info, "Header Out: {}", std::string(data, size));
|
||||
// break;
|
||||
// case CURLINFO_DATA_IN:
|
||||
// LOG(Http, Info, "Data In: {} bytes, in: {}", size,std::string(data, size));
|
||||
// break;
|
||||
// case CURLINFO_DATA_OUT:
|
||||
// LOG(Http, Info, "Data Out: {} bytes, out: {}", size, std::string(data, size));
|
||||
// break;
|
||||
// default:
|
||||
// break;
|
||||
// }
|
||||
// return 0;
|
||||
// }
|
||||
void HttpClient::get(
|
||||
string url,
|
||||
unordered_map<string, string> headers,
|
||||
@@ -91,7 +115,12 @@ void HttpClient::send(const HttpRequest &req, IHttpResponse *response) {
|
||||
CURL *curl = session.curl;
|
||||
CurlHeaders curl_headers{req.headers};
|
||||
// Prepare req
|
||||
curl_easy_setopt(curl, CURLOPT_FAILONERROR, true);
|
||||
//to debug curl requests
|
||||
// curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, curl_debug_callback);
|
||||
// Disable signals to avoid strange stack corruption related to set/longjmp
|
||||
// https://stackoverflow.com/questions/9191668/error-longjmp-causes-uninitialized-stack-frame
|
||||
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L);
|
||||
curl_easy_setopt(curl, CURLOPT_FAILONERROR, 0L);
|
||||
curl_easy_setopt(curl, CURLOPT_URL, req.url.c_str());
|
||||
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, curl_headers.data());
|
||||
if (response != nullptr) {
|
||||
@@ -108,6 +137,7 @@ void HttpClient::send(const HttpRequest &req, IHttpResponse *response) {
|
||||
curl_easy_setopt(curl, CURLOPT_HTTPPOST, 1L);
|
||||
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, req.body.c_str());
|
||||
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, req.body.size());
|
||||
// curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
|
||||
break;
|
||||
case io::http::HttpMethod::PUT:
|
||||
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
#pragma once
|
||||
|
||||
#include <fmt/format.h>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <string>
|
||||
|
||||
namespace bts::io {
|
||||
|
||||
/**
|
||||
* This wrapper struct provides a means to format arbitrary objects as json
|
||||
* in templated strings (e.g., LOG macros).
|
||||
*/
|
||||
template <class T>
|
||||
struct JsonFormatter {
|
||||
T value;
|
||||
|
||||
std::string to_string() const { return fmt::format("{}", *this); }
|
||||
};
|
||||
|
||||
template <class T>
|
||||
JsonFormatter<T> json_formatter(T value) {
|
||||
return JsonFormatter<T>{std::move(value)};
|
||||
}
|
||||
|
||||
} // namespace bts::io
|
||||
|
||||
template <class T>
|
||||
struct fmt::formatter<bts::io::JsonFormatter<T>> {
|
||||
int indent = -1;
|
||||
|
||||
constexpr auto parse(fmt::format_parse_context &ctx) -> decltype(ctx.end()) {
|
||||
const auto end = ctx.end();
|
||||
auto it = ctx.begin();
|
||||
if (it != end) {
|
||||
indent = 0;
|
||||
do {
|
||||
int x = *it - '0';
|
||||
if (x < 0 || x > 9) {
|
||||
return it;
|
||||
}
|
||||
indent = indent * 10 + x;
|
||||
} while (++it != end);
|
||||
}
|
||||
return it;
|
||||
}
|
||||
|
||||
template <typename FormatContext>
|
||||
auto format(const bts::io::JsonFormatter<T> &jf, FormatContext &ctx) const
|
||||
-> decltype(ctx.out()) {
|
||||
nlohmann::json j;
|
||||
to_json(j, jf.value);
|
||||
return fmt::format_to(ctx.out(), "{}", j.dump(indent));
|
||||
}
|
||||
};
|
||||
@@ -3,14 +3,19 @@
|
||||
#include "lib/core/TimeSpan.h"
|
||||
#include "lib/core/TimeStamp.h"
|
||||
#include "lib/io/websocket/websocket_client_config.h"
|
||||
#include "lib/io/websocket/websocket_client_logger.h"
|
||||
#include "lib/io/websocket/websocket_client_state.h"
|
||||
#include "lib/io/websocket/websocket_close_status.h"
|
||||
#include "lib/io/websocket/websocket_error_code.h"
|
||||
#include "lib/io/websocket/websocketpp_exception_formatter.h"
|
||||
#include "lib/logging/logging.h"
|
||||
#include "lib/utils/bts_assert.h"
|
||||
#include "lib/utils/formatters/exception_formatter.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <atomic>
|
||||
#include <boost/asio/io_context.hpp>
|
||||
#include <chrono>
|
||||
#include <exception>
|
||||
#include <fmt/format.h>
|
||||
#include <mutex>
|
||||
@@ -18,6 +23,7 @@
|
||||
#include <optional>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <system_error>
|
||||
#include <thread>
|
||||
#include <websocketpp/close.hpp>
|
||||
#include <websocketpp/common/connection_hdl.hpp>
|
||||
@@ -28,11 +34,51 @@
|
||||
#include <websocketpp/config/core_client.hpp>
|
||||
#include <websocketpp/roles/client_endpoint.hpp>
|
||||
|
||||
namespace websocketpp::config {
|
||||
|
||||
/// Client config with asio transport and TLS enabled
|
||||
struct bts_asio_tls_client : public core_client {
|
||||
typedef bts_asio_tls_client type;
|
||||
typedef core_client base;
|
||||
|
||||
typedef base::concurrency_type concurrency_type;
|
||||
|
||||
typedef base::request_type request_type;
|
||||
typedef base::response_type response_type;
|
||||
|
||||
typedef base::message_type message_type;
|
||||
typedef base::con_msg_manager_type con_msg_manager_type;
|
||||
typedef base::endpoint_msg_manager_type endpoint_msg_manager_type;
|
||||
|
||||
typedef websocketpp::log::WebsocketClientLogger<
|
||||
websocketpp::log::ElevelAdapter>
|
||||
elog_type;
|
||||
typedef websocketpp::log::WebsocketClientLogger<
|
||||
websocketpp::log::AlevelAdapter>
|
||||
alog_type;
|
||||
|
||||
typedef base::rng_type rng_type;
|
||||
|
||||
struct transport_config : public base::transport_config {
|
||||
typedef type::concurrency_type concurrency_type;
|
||||
typedef type::alog_type alog_type;
|
||||
typedef type::elog_type elog_type;
|
||||
typedef type::request_type request_type;
|
||||
typedef type::response_type response_type;
|
||||
typedef transport::asio::tls_socket::endpoint socket_type;
|
||||
};
|
||||
|
||||
typedef transport::asio::endpoint<transport_config> transport_type;
|
||||
};
|
||||
|
||||
} // namespace websocketpp::config
|
||||
|
||||
namespace bts::websocket {
|
||||
|
||||
struct WebsocketClientSnapshot {
|
||||
int client_id;
|
||||
WebsocketClientState state;
|
||||
int n_reconnects;
|
||||
int version;
|
||||
};
|
||||
|
||||
@@ -41,9 +87,10 @@ struct WebsocketClientSnapshot {
|
||||
FMT_IMPL(
|
||||
bts::websocket::WebsocketClientSnapshot,
|
||||
t,
|
||||
"id: {}; state: {}; version: {}",
|
||||
"id: {}; state: {}; n_reconnects: {}; version: {}",
|
||||
t.client_id,
|
||||
t.state,
|
||||
t.n_reconnects,
|
||||
t.version);
|
||||
|
||||
namespace bts::websocket {
|
||||
@@ -61,10 +108,11 @@ using std::size_t;
|
||||
using std::string;
|
||||
using std::thread;
|
||||
using std::uint32_t;
|
||||
using websocketpp::config::asio_tls_client;
|
||||
using std::chrono::nanoseconds;
|
||||
using std::this_thread::sleep_for;
|
||||
|
||||
using WebsocketppClient =
|
||||
websocketpp::client<websocketpp::config::asio_tls_client>;
|
||||
websocketpp::client<websocketpp::config::bts_asio_tls_client>;
|
||||
using ConnectionHdl = websocketpp::connection_hdl;
|
||||
using ConnectionPtr = WebsocketppClient::connection_ptr;
|
||||
using MessageTypePtr = websocketpp::config::core_client::message_type::ptr;
|
||||
@@ -85,9 +133,16 @@ using State = WebsocketClientState;
|
||||
#define ERROR(...) LOG(Http, Error, __VA_ARGS__)
|
||||
#define ERROR_FN(...) LOG(Http, Error, "WebsocketClient::" __VA_ARGS__)
|
||||
|
||||
static bts::core::TimeSpan reconnect_interval(std::uint32_t n_attempts) {
|
||||
std::int64_t secs = 5 * n_attempts;
|
||||
secs = std::clamp(secs, 5L, 300L);
|
||||
return bts::core::TimeSpan::from_seconds(secs);
|
||||
}
|
||||
|
||||
struct WebsocketClient::PImpl {
|
||||
WebsocketClientConfig config;
|
||||
WebsocketppClient client{};
|
||||
boost::asio::io_context *io_context = nullptr;
|
||||
WebsocketppClient::connection_ptr con;
|
||||
optional<OpenHandler> open_handler;
|
||||
optional<MessageHandler> message_handler;
|
||||
@@ -97,8 +152,48 @@ struct WebsocketClient::PImpl {
|
||||
optional<thread> io_thread;
|
||||
int client_id{client_id_counter++};
|
||||
mutex instance_mx;
|
||||
State state;
|
||||
State state = State::IDLE;
|
||||
int version = 0;
|
||||
int n_reconnects = 0;
|
||||
|
||||
PImpl(WebsocketClientConfig config, boost::asio::io_context *io_context)
|
||||
: config(std::move(config)), io_context(io_context) {
|
||||
client.get_alog().set_name(this->config.name);
|
||||
client.get_elog().set_name(this->config.name);
|
||||
client.set_access_channels(websocketpp::log::alevel::all);
|
||||
// client.clear_access_channels(websocketpp::log::alevel::frame_payload);
|
||||
client.set_error_channels(websocketpp::log::elevel::all);
|
||||
if (io_context == nullptr) {
|
||||
client.init_asio();
|
||||
} else {
|
||||
client.init_asio(io_context);
|
||||
}
|
||||
client.set_message_handler(
|
||||
[this](auto hdl, auto msg) { on_message(hdl, msg); });
|
||||
client.set_open_handler([this](auto hdl) { on_open(hdl); });
|
||||
client.set_ping_handler([](auto /*hdl*/, auto /*msg*/) {
|
||||
LOG(Http, Info, "Received ping");
|
||||
return true;
|
||||
});
|
||||
client.set_pong_timeout(0);
|
||||
client.set_pong_handler([](auto /*hdl*/, auto /*msg*/) {
|
||||
LOG(Http, Info, "Received pong");
|
||||
return true;
|
||||
});
|
||||
client.set_tls_init_handler([&](auto hdl) { return on_tls_init(hdl); });
|
||||
client.set_close_handler([=, this](auto hdl) {
|
||||
on_close(hdl);
|
||||
if (config.reconnect_on_close) {
|
||||
sleep_for(nanoseconds{reconnect_interval(n_reconnects).nanos()});
|
||||
connect();
|
||||
}
|
||||
});
|
||||
client.set_fail_handler([this](auto hdl) {
|
||||
const auto ec = client.get_con_from_hdl(hdl)->get_ec();
|
||||
ERROR_FN("fail_handler. {}; err: {}", snapshot(), ec.message());
|
||||
on_fail(hdl);
|
||||
});
|
||||
}
|
||||
|
||||
~PImpl() {
|
||||
if (io_thread) {
|
||||
@@ -119,7 +214,7 @@ struct WebsocketClient::PImpl {
|
||||
}
|
||||
|
||||
constexpr WebsocketClientSnapshot snapshot() const {
|
||||
return {client_id, state, version};
|
||||
return {client_id, state, n_reconnects, version};
|
||||
}
|
||||
|
||||
void close(websocketpp::close::status::value code, std::string reason) {
|
||||
@@ -163,8 +258,7 @@ struct WebsocketClient::PImpl {
|
||||
/**
|
||||
* Connects to the websocket server
|
||||
*/
|
||||
void connect(
|
||||
const map<string, string> &headers, bool run_in_background = true) {
|
||||
void connect() {
|
||||
lock_guard<mutex> lock{instance_mx};
|
||||
INFO_FN("connect. {}", snapshot());
|
||||
switch (state.value) {
|
||||
@@ -176,9 +270,6 @@ struct WebsocketClient::PImpl {
|
||||
}
|
||||
set_state(State::OPENING);
|
||||
try {
|
||||
init_logging();
|
||||
init_handlers();
|
||||
// Get connection
|
||||
websocketpp::lib::error_code ec;
|
||||
con = client.get_connection(config.url, ec);
|
||||
if (ec) {
|
||||
@@ -186,46 +277,27 @@ struct WebsocketClient::PImpl {
|
||||
return;
|
||||
}
|
||||
// Add headers
|
||||
for (const auto &[k, v] : headers) {
|
||||
for (const auto &[k, v] : config.headers) {
|
||||
con->append_header(k, v);
|
||||
}
|
||||
// Connect
|
||||
client.connect(con);
|
||||
if (run_in_background) {
|
||||
bts_assert_msgf(
|
||||
!io_thread, "Websocket io_thread already instantiated. {}",
|
||||
snapshot());
|
||||
io_thread = std::thread{[this]() { run(); }};
|
||||
if (config.connect_timeout > core::TimeSpan::zero()) {
|
||||
wait_for_connect(config.connect_timeout);
|
||||
}
|
||||
}
|
||||
} catch (const std::exception &ex) {
|
||||
std::string str = ex.what();
|
||||
ERROR_FN(
|
||||
"connect_no_timeout. Failed to connect. {}; err: {}", snapshot(),
|
||||
ex);
|
||||
str);
|
||||
set_state(State::IDLE);
|
||||
// Don't wait for connect
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void wait_for_connect(const TimeSpan timeout) {
|
||||
INFO_FN("wait_for_connect. {}; timeout: {}", snapshot(), timeout);
|
||||
const auto start = TimeStamp::now();
|
||||
while (state == State::OPENING) {
|
||||
const auto elapsed = TimeStamp::now() - start;
|
||||
const auto rem = timeout - elapsed;
|
||||
if (rem < TimeSpan::zero()) {
|
||||
throw std::runtime_error{fmt::format(
|
||||
"Timed out waiting for connection after {} secs. {}",
|
||||
elapsed.total_seconds(), snapshot())};
|
||||
}
|
||||
WARN_FN(
|
||||
"wait_for_connect. Still waiting. {}; timeout: {}; rem: {}",
|
||||
snapshot(), timeout, rem);
|
||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
}
|
||||
void reconnect() {
|
||||
WARN_FN("reconnect. {}", snapshot());
|
||||
sleep_for(nanoseconds{reconnect_interval(n_reconnects).nanos()});
|
||||
++n_reconnects;
|
||||
connect();
|
||||
}
|
||||
|
||||
void run() {
|
||||
@@ -236,11 +308,16 @@ struct WebsocketClient::PImpl {
|
||||
ERROR_FN(
|
||||
"io_thread. Websocket io threw websocketpp::exception. {}; "
|
||||
"err: {}",
|
||||
snapshot(), ex);
|
||||
snapshot(), std::string(ex.what()));
|
||||
bts_assert_msgf(false, "io_thread. Websocket io threw websocketpp::exception. {}; "
|
||||
"err: {}",
|
||||
snapshot(), std::string(ex.what()));
|
||||
} catch (const std::exception &ex) {
|
||||
ERROR_FN(
|
||||
"io_thread. Websocket io threw std::exception. {}; err: {}",
|
||||
snapshot(), ex);
|
||||
snapshot(), std::string(ex.what()));
|
||||
bts_assert_msgf(false, "io_thread. Websocket io threw std::exception. {}; err: {}",
|
||||
snapshot(), std::string(ex.what()));
|
||||
} catch (...) {
|
||||
bts_assert_msgf(
|
||||
false,
|
||||
@@ -251,10 +328,33 @@ struct WebsocketClient::PImpl {
|
||||
INFO("Websocket 'run' thread end. {}", snapshot());
|
||||
}
|
||||
|
||||
void run_one() {
|
||||
try {
|
||||
client.run_one();
|
||||
} catch (const websocketpp::exception &ex) {
|
||||
ERROR_FN(
|
||||
"io_thread (run_one). Websocket io threw websocketpp::exception. "
|
||||
"{}; err: {}",
|
||||
snapshot(), ex);
|
||||
} catch (const std::exception &ex) {
|
||||
ERROR_FN(
|
||||
"io_thread (run_one). Websocket io threw std::exception. {}; err: "
|
||||
"{}",
|
||||
snapshot(), ex);
|
||||
} catch (...) {
|
||||
bts_assert_msgf(
|
||||
false,
|
||||
"io_thread (run_one). Websocket io threw unknown exception. Could "
|
||||
"this have been thrown by a callback delegate? {}",
|
||||
snapshot());
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
static std::atomic<int> client_id_counter;
|
||||
|
||||
auto on_tls_init(ConnectionHdl /*hdl*/) {
|
||||
std::shared_ptr<boost::asio::ssl::context> on_tls_init(
|
||||
ConnectionHdl /*hdl*/) {
|
||||
INFO_FN("on_tls_init. state: {}; version: {}", state, version);
|
||||
auto result =
|
||||
websocketpp::lib::make_shared<SslContext>(SslContext::sslv23_client);
|
||||
@@ -262,44 +362,13 @@ struct WebsocketClient::PImpl {
|
||||
return result;
|
||||
}
|
||||
|
||||
void init_handlers() {
|
||||
INFO_FN("init_handlers. state: {}; version: {}", state, version);
|
||||
bts_assert(!io_thread);
|
||||
client.init_asio();
|
||||
client.set_message_handler(
|
||||
[this](auto hdl, auto msg) { on_message(hdl, msg); });
|
||||
client.set_open_handler([this](auto hdl) { on_open(hdl); });
|
||||
client.set_ping_handler([](auto /*hdl*/, auto /*msg*/) {
|
||||
LOG(Http, Info, "Received ping");
|
||||
return true;
|
||||
});
|
||||
client.set_pong_timeout(0);
|
||||
client.set_pong_handler([](auto /*hdl*/, auto /*msg*/) {
|
||||
LOG(Http, Info, "Received pong");
|
||||
return true;
|
||||
});
|
||||
client.set_tls_init_handler(
|
||||
[this](auto hdl) { return on_tls_init(hdl); });
|
||||
client.set_close_handler([this](auto hdl) { on_close(hdl); });
|
||||
client.set_fail_handler([this](auto hdl) {
|
||||
const auto ec = client.get_con_from_hdl(hdl)->get_ec();
|
||||
ERROR_FN("fail_handler. {}; err: {}", snapshot(), ec.message());
|
||||
});
|
||||
}
|
||||
|
||||
void init_logging() {
|
||||
INFO_FN("init_logging. {}", snapshot());
|
||||
client.set_access_channels(websocketpp::log::alevel::all);
|
||||
// client.clear_access_channels(websocketpp::log::alevel::frame_payload);
|
||||
client.set_error_channels(websocketpp::log::elevel::all);
|
||||
}
|
||||
|
||||
void on_open(ConnectionHdl hdl) {
|
||||
lock_guard<mutex> lock{instance_mx};
|
||||
INFO_FN("on_open. {}", snapshot());
|
||||
const auto ec = client.get_con_from_hdl(hdl)->get_ec();
|
||||
if (ec) {
|
||||
ERROR_FN("on_open. {}; err: {}", snapshot(), ec.message());
|
||||
bts_assert(false);
|
||||
}
|
||||
switch (state.value) {
|
||||
case State::OPENING:
|
||||
@@ -350,11 +419,34 @@ struct WebsocketClient::PImpl {
|
||||
if (close_handler) {
|
||||
(*close_handler)(ec);
|
||||
}
|
||||
if (config.reconnect_on_close) {
|
||||
reconnect();
|
||||
}
|
||||
}
|
||||
|
||||
void on_fail(ConnectionHdl hdl) {
|
||||
lock_guard<mutex> lock{instance_mx};
|
||||
ERROR_FN("on_fail. {}", snapshot());
|
||||
set_state(State::IDLE);
|
||||
const auto con = client.get_con_from_hdl(hdl);
|
||||
const auto ec = con->get_ec();
|
||||
if (ec) {
|
||||
ERROR_FN(
|
||||
"on_fail. {}; local_code: {}; "
|
||||
"local_reason: {}; remote_code: {}; remote_reason: {}; err: {}",
|
||||
snapshot(), con->get_local_close_code(),
|
||||
con->get_local_close_reason(), con->get_remote_close_code(),
|
||||
con->get_remote_close_reason(), ec.message());
|
||||
}
|
||||
if (config.reconnect_on_fail) {
|
||||
reconnect();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
WebsocketClient::WebsocketClient(WebsocketClientConfig config)
|
||||
: me_(std::make_unique<PImpl>(std::move(config))) {}
|
||||
WebsocketClient::WebsocketClient(
|
||||
WebsocketClientConfig config, boost::asio::io_context *io_context)
|
||||
: me_(std::make_unique<PImpl>(std::move(config), io_context)) {}
|
||||
|
||||
WebsocketClient::~WebsocketClient() {
|
||||
me_->close(websocketpp::close::status::going_away, "");
|
||||
@@ -383,13 +475,16 @@ WebsocketClient::CloseHandler &WebsocketClient::close_handler() {
|
||||
return *me_->close_handler;
|
||||
}
|
||||
|
||||
void WebsocketClient::connect(
|
||||
const std::map<std::string, std::string> &headers, bool run_in_background) {
|
||||
me_->connect(headers, run_in_background);
|
||||
}
|
||||
void WebsocketClient::connect() { me_->connect(); }
|
||||
|
||||
void WebsocketClient::run() { me_->run(); }
|
||||
|
||||
void WebsocketClient::run_one() { me_->run_one(); }
|
||||
|
||||
boost::asio::io_context *WebsocketClient::io_context() {
|
||||
return me_->io_context;
|
||||
}
|
||||
|
||||
websocketpp::lib::error_code WebsocketClient::send_text(const string &text) {
|
||||
return me_->send_text(text);
|
||||
}
|
||||
@@ -407,4 +502,6 @@ bool WebsocketClient::is_connected() const {
|
||||
|
||||
void WebsocketClient::ping(const string &payload) { me_->con->ping(payload); }
|
||||
|
||||
const string &WebsocketClient::url() const { return me_->config.url; }
|
||||
|
||||
} // namespace bts::websocket
|
||||
@@ -3,6 +3,7 @@
|
||||
#include "lib/io/websocket/websocket_client_config.h"
|
||||
#include "lib/utils/Delegate.h"
|
||||
|
||||
#include <boost/asio/io_context.hpp>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <websocketpp/config/core_client.hpp>
|
||||
@@ -12,28 +13,33 @@ namespace bts::websocket {
|
||||
|
||||
class WebsocketClient {
|
||||
public:
|
||||
explicit WebsocketClient(WebsocketClientConfig config);
|
||||
WebsocketClient(
|
||||
WebsocketClientConfig config,
|
||||
boost::asio::io_context *io_context = nullptr);
|
||||
~WebsocketClient();
|
||||
|
||||
using OpenHandler = bts::core::Delegate<void(const std::error_code &)>;
|
||||
using MessageHandler = bts::core::Delegate<void(const std::string &)>;
|
||||
using CloseHandler = bts::core::Delegate<void(const std::error_code &)>;
|
||||
|
||||
void connect(
|
||||
const std::map<std::string, std::string> &headers = {},
|
||||
bool run_in_background = true);
|
||||
void connect();
|
||||
void disconnect();
|
||||
bool is_connected() const;
|
||||
boost::asio::io_context *io_context();
|
||||
void run();
|
||||
void run_one();
|
||||
CloseHandler &open_handler();
|
||||
MessageHandler &message_handler();
|
||||
CloseHandler &close_handler();
|
||||
websocketpp::lib::error_code send_text(const std::string &text);
|
||||
void ping(const std::string &payload = "");
|
||||
|
||||
const std::string &url() const;
|
||||
|
||||
private:
|
||||
struct PImpl;
|
||||
std::unique_ptr<PImpl> me_;
|
||||
};
|
||||
|
||||
} // namespace bts::websocket
|
||||
|
||||
@@ -5,9 +5,14 @@
|
||||
namespace bts::websocket {
|
||||
|
||||
struct WebsocketClientConfig {
|
||||
std::string url;
|
||||
core::TimeSpan connect_timeout;
|
||||
core::TimeSpan pong_timeout;
|
||||
std::string url{};
|
||||
core::TimeSpan connect_timeout = core::TimeSpan::zero();
|
||||
core::TimeSpan pong_timeout = core::TimeSpan::zero();
|
||||
bool reconnect_on_close = false;
|
||||
bool reconnect_on_fail = false;
|
||||
std::map<std::string, std::string> headers{};
|
||||
// name to use in logs
|
||||
std::string name{};
|
||||
};
|
||||
|
||||
} // namespace bts::websocket
|
||||
@@ -0,0 +1,88 @@
|
||||
#pragma once
|
||||
|
||||
#include "lib/logging/Common.h"
|
||||
#include "lib/logging/logging.h"
|
||||
|
||||
#include <websocketpp/concurrency/basic.hpp>
|
||||
#include <websocketpp/logger/basic.hpp>
|
||||
#include <websocketpp/logger/levels.hpp>
|
||||
|
||||
namespace websocketpp::log {
|
||||
|
||||
struct ElevelAdapter {
|
||||
typedef elevel LevelT;
|
||||
|
||||
constexpr bts::log::Level operator()(level channel) {
|
||||
switch (channel) {
|
||||
case elevel::devel:
|
||||
return bts::Diag;
|
||||
case elevel::library:
|
||||
case elevel::warn:
|
||||
return bts::Warn;
|
||||
case elevel::rerror:
|
||||
return bts::Error;
|
||||
case elevel::fatal:
|
||||
return bts::Fatal;
|
||||
default:
|
||||
return bts::Info;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
struct AlevelAdapter {
|
||||
typedef alevel LevelT;
|
||||
|
||||
constexpr bts::log::Level operator()(level channel) {
|
||||
switch (channel) {
|
||||
case alevel::control:
|
||||
case alevel::frame_header:
|
||||
case alevel::frame_payload:
|
||||
case alevel::message_header:
|
||||
case alevel::message_payload:
|
||||
case alevel::debug_handshake:
|
||||
case alevel::debug_close:
|
||||
case alevel::devel:
|
||||
return bts::Diag;
|
||||
case alevel::disconnect:
|
||||
return bts::Warn;
|
||||
case alevel::fail:
|
||||
return bts::Error;
|
||||
default:
|
||||
return bts::Info;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template <typename LevelAdapter>
|
||||
class WebsocketClientLogger
|
||||
: public basic<concurrency::basic, typename LevelAdapter::LevelT> {
|
||||
typedef basic<concurrency::basic, typename LevelAdapter::LevelT> base;
|
||||
|
||||
std::string name;
|
||||
LevelAdapter level_adapter;
|
||||
|
||||
public:
|
||||
WebsocketClientLogger(
|
||||
level c, channel_type_hint::value h = channel_type_hint::access)
|
||||
: base(c, h) {}
|
||||
|
||||
void set_name(std::string name) {
|
||||
concurrency::basic::scoped_lock_type lock{this->m_lock};
|
||||
this->name = std::move(name);
|
||||
}
|
||||
|
||||
void write(level channel, const std::string &msg) {
|
||||
concurrency::basic::scoped_lock_type lock{this->m_lock};
|
||||
if (!this->dynamic_test(channel)) {
|
||||
return;
|
||||
}
|
||||
LOG(Http, level_adapter(channel), "WebsocketClient-{} {}. {}", name,
|
||||
std::string{LevelAdapter::LevelT::channel_name(channel)}, msg);
|
||||
}
|
||||
|
||||
void write(level channel, const char *msg) {
|
||||
write(channel, std::string{msg});
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace websocketpp::log
|
||||
@@ -9,6 +9,7 @@
|
||||
#include <boost/date_time/posix_time/posix_time.hpp>
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <boost/filesystem/path.hpp>
|
||||
#include <cstdint>
|
||||
#include <fmt/ostream.h>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
@@ -94,7 +95,7 @@ std::ifstream::pos_type filesize(std::string const &filename) {
|
||||
Sink file_sink(LoggingConfig const &cfg) {
|
||||
static std::ofstream stream;
|
||||
static uint64_t bytes_written = 0;
|
||||
const auto max_sz = 120 * 1024 * 1024;
|
||||
const uintmax_t max_sz = 1024 * 1024 * 1024 ;
|
||||
|
||||
auto f = formatter(cfg);
|
||||
auto path = log_path();
|
||||
@@ -107,7 +108,7 @@ Sink file_sink(LoggingConfig const &cfg) {
|
||||
|
||||
if (boost::filesystem::exists(path)) {
|
||||
auto sz = boost::filesystem::file_size(path);
|
||||
if (sz > max_sz && false) {
|
||||
if (sz > max_sz) {
|
||||
auto backup = boost::filesystem::path(path.string() + ".old");
|
||||
boost::filesystem::rename(path, backup);
|
||||
}
|
||||
@@ -118,8 +119,7 @@ Sink file_sink(LoggingConfig const &cfg) {
|
||||
return [=](LogMsg const &m) {
|
||||
auto msg = f(m);
|
||||
bytes_written += msg.size();
|
||||
if (bytes_written >= max_sz && false) {
|
||||
return;
|
||||
if (bytes_written >= max_sz) {
|
||||
bytes_written = 0;
|
||||
stream.close();
|
||||
auto backup = boost::filesystem::path(path.string() + ".old");
|
||||
|
||||
@@ -68,74 +68,88 @@ struct CCSecSearchResult {
|
||||
struct IncrementalBookData {
|
||||
std::map<domain::SecurityID, domain::Book> book_map;
|
||||
|
||||
bool update_incremental(const IMarketDataListener::BookOp &op) {
|
||||
//DONT NEED THIS?
|
||||
// Validate inputs first
|
||||
if (op.price <= 0.0 || op.quantity < 0) {
|
||||
LOG(md, Error, "Invalid input - Price: {}, Quantity: {}", op.price, op.quantity);
|
||||
return false;
|
||||
}
|
||||
bool update_incremental(const IMarketDataListener::BookOp &op) {
|
||||
//DONT NEED THIS?
|
||||
// Validate inputs first
|
||||
if (op.price <= 0.0 || op.quantity < 0) {
|
||||
LOG(md, Error, "Invalid input - Price: {}, Quantity: {}", op.price,
|
||||
op.quantity);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Logging setup
|
||||
//DO WE NEED TO PUT THESE IN A STRING
|
||||
std::string side_str = (op.side == domain::Side::Buy) ? "Buy" : "Sell";
|
||||
std::string op_type_str;
|
||||
switch (op.op_type) {
|
||||
case IMarketDataListener::BookOpType::Add: op_type_str = "Add"; break;
|
||||
case IMarketDataListener::BookOpType::MODIFY: op_type_str = "Modify"; break;
|
||||
case IMarketDataListener::BookOpType::REMOVE: op_type_str = "Delete"; break;
|
||||
default: op_type_str = "Unknown"; break;
|
||||
}
|
||||
//CAN WE DO THIS WITHOUT ASSIGNING TO STRINGS
|
||||
std::stringstream op_details;
|
||||
op_details << "Operation: Price: " << op.price << ", Quantity: " << op.quantity
|
||||
<< ", Side: " << side_str << ", Type: " << op_type_str << "\n";
|
||||
// Logging setup
|
||||
//DO WE NEED TO PUT THESE IN A STRING
|
||||
std::string side_str = (op.side == domain::Side::Buy) ? "Buy" : "Sell";
|
||||
std::string op_type_str;
|
||||
switch (op.op_type) {
|
||||
case IMarketDataListener::BookOpType::Add:
|
||||
op_type_str = "Add";
|
||||
break;
|
||||
case IMarketDataListener::BookOpType::MODIFY:
|
||||
op_type_str = "Modify";
|
||||
break;
|
||||
case IMarketDataListener::BookOpType::REMOVE:
|
||||
op_type_str = "Delete";
|
||||
break;
|
||||
default:
|
||||
op_type_str = "Unknown";
|
||||
break;
|
||||
}
|
||||
//CAN WE DO THIS WITHOUT ASSIGNING TO STRINGS
|
||||
std::stringstream op_details;
|
||||
op_details << "Operation: Price: " << op.price
|
||||
<< ", Quantity: " << op.quantity << ", Side: " << side_str
|
||||
<< ", Type: " << op_type_str << "\n";
|
||||
|
||||
LOG(md, Info, "Book Increment: {}", op_details.str());
|
||||
LOG(md, Info, "Book Increment: {}", op_details.str());
|
||||
|
||||
// Get book and validate
|
||||
auto book = get_book(op.id);
|
||||
if (book == nullptr) {
|
||||
LOG(md, Error, "Book not found for id: {}", op.id);
|
||||
return false;
|
||||
}
|
||||
// Get book and validate
|
||||
auto book = get_book(op.id);
|
||||
if (book == nullptr) {
|
||||
LOG(md, Error, "Book not found for id: {}", op.id);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get correct side of the book
|
||||
auto& book_side = (op.side == domain::Side::Buy) ? book->bids() : book->asks();
|
||||
// Get correct side of the book
|
||||
auto &book_side =
|
||||
(op.side == domain::Side::Buy) ? book->bids() : book->asks();
|
||||
|
||||
// Log initial state
|
||||
std::stringstream book_before;
|
||||
book_before << "Book before update (" << side_str << " side):\n";
|
||||
for (const auto& level : book_side) {
|
||||
book_before << "Price: " << level.price << ", Quantity: " << level.total_quantity << "\n";
|
||||
}
|
||||
// Log initial state
|
||||
std::stringstream book_before;
|
||||
book_before << "Book before update (" << side_str << " side):\n";
|
||||
for (const auto &level : book_side) {
|
||||
book_before << "Price: " << level.price
|
||||
<< ", Quantity: " << level.total_quantity << "\n";
|
||||
}
|
||||
|
||||
/*-----------*/
|
||||
if(op.side == domain::Side::Buy){
|
||||
switch(op.op_type){
|
||||
case IMarketDataListener::BookOpType::Add:
|
||||
{
|
||||
/*-----------*/
|
||||
if (op.side == domain::Side::Buy) {
|
||||
switch (op.op_type) {
|
||||
case IMarketDataListener::BookOpType::Add: {
|
||||
int insert_position = 0;
|
||||
for (size_t i = 0; i < book_side.size(); i++){//not sure how to access book
|
||||
if(book_side[i].price != 0 && book_side[i].price > op.price ){
|
||||
for (size_t i = 0; i < book_side.size();
|
||||
i++) { //not sure how to access book
|
||||
if (book_side[i].price != 0 && book_side[i].price > op.price) {
|
||||
//book_side is the book we have ?
|
||||
//op.price is what we received ?
|
||||
insert_position++;
|
||||
continue;
|
||||
}else {//need j to be last level
|
||||
for (size_t j =fmin(book_side.size()-1,5-1); j > i; j--){
|
||||
book_side[j] = book_side[j-1];
|
||||
} else { //need j to be last level
|
||||
for (size_t j = fmin(book_side.size() - 1, 5 - 1); j > i;
|
||||
j--) {
|
||||
book_side[j] = book_side[j - 1];
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
book_side[insert_position].price = op.price;
|
||||
book_side[insert_position].total_quantity = op.quantity;//dont know how to add quantity
|
||||
book_side[insert_position].total_quantity =
|
||||
op.quantity; //dont know how to add quantity
|
||||
break;
|
||||
}
|
||||
case IMarketDataListener::BookOpType::MODIFY:{
|
||||
for(size_t i = 0; i < fmin(book_side.size(),5); i++){
|
||||
if(book_side[i].price != 0 && book_side[i].price == op.price){
|
||||
case IMarketDataListener::BookOpType::MODIFY: {
|
||||
for (size_t i = 0; i < fmin(book_side.size(), 5); i++) {
|
||||
if (book_side[i].price != 0 && book_side[i].price == op.price) {
|
||||
//want to update quantity if price is a match
|
||||
book_side[i].total_quantity = op.quantity;
|
||||
break;
|
||||
@@ -143,103 +157,113 @@ bool update_incremental(const IMarketDataListener::BookOp &op) {
|
||||
}
|
||||
break;
|
||||
}
|
||||
case IMarketDataListener::BookOpType::REMOVE:{
|
||||
case IMarketDataListener::BookOpType::REMOVE: {
|
||||
int found_position = -1;
|
||||
for (size_t i = 0; i < fmin(book_side.size(),5); i++){
|
||||
if(book_side[i].price == op.price){//trying to find based on price only
|
||||
for (size_t i = 0; i < fmin(book_side.size(), 5); i++) {
|
||||
if (book_side[i].price ==
|
||||
op.price) { //trying to find based on price only
|
||||
found_position = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
size_t level = found_position;
|
||||
//would've had 4 here; want to be at last level
|
||||
for(size_t i = found_position; i <fmin(book_side.size(),5); i++){
|
||||
book_side[i] = book_side[i+1];
|
||||
//would've had 4 here; want to be at last level
|
||||
for (size_t i = found_position; i < fmin(book_side.size(), 5);
|
||||
i++) {
|
||||
book_side[i] = book_side[i + 1];
|
||||
level++;
|
||||
}
|
||||
if(found_position != -1){
|
||||
if (found_position != -1) {
|
||||
//trying to set last level to 0
|
||||
book_side[level].total_quantity =domain::Quantity(0);
|
||||
book_side[level].total_quantity = domain::Quantity(0);
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
LOG(md,Error, "In the Default Case for IMarketDataListener::BookOp {}");
|
||||
}
|
||||
}else if (op.side == domain::Side::Sell){
|
||||
switch(op.op_type){
|
||||
case IMarketDataListener::BookOpType::Add:{
|
||||
LOG(md, Error,
|
||||
"In the Default Case for IMarketDataListener::BookOp {}");
|
||||
}
|
||||
} else if (op.side == domain::Side::Sell) {
|
||||
switch (op.op_type) {
|
||||
case IMarketDataListener::BookOpType::Add: {
|
||||
int insert_position = 0;
|
||||
for (int i = 0; i < fmin(book_side.size(),5); i++) {
|
||||
if (book_side[i].price != 0 && book_side[i].price < op.price) {
|
||||
insert_position++;
|
||||
continue;
|
||||
}
|
||||
}//need i to be at the last level
|
||||
for (int i = fmin(book_side.size()-1,5-1); i > insert_position; i--) {
|
||||
book_side[i].price = book_side[i - 1].price;
|
||||
book_side[i].total_quantity = book_side[i - 1].total_quantity;
|
||||
for (int i = 0; i < fmin(book_side.size(), 5); i++) {
|
||||
if (book_side[i].price != 0 && book_side[i].price < op.price) {
|
||||
insert_position++;
|
||||
continue;
|
||||
}
|
||||
} //need i to be at the last level
|
||||
for (int i = fmin(book_side.size() - 1, 5 - 1); i > insert_position;
|
||||
i--) {
|
||||
book_side[i].price = book_side[i - 1].price;
|
||||
book_side[i].total_quantity = book_side[i - 1].total_quantity;
|
||||
}
|
||||
book_side[insert_position].price = op.price;
|
||||
book_side[insert_position].total_quantity = op.quantity;
|
||||
book_side[insert_position].total_quantity = op.quantity;
|
||||
break;
|
||||
}
|
||||
case IMarketDataListener::BookOpType::MODIFY:{
|
||||
for (int i = 0; i < fmin(book_side.size(),5); i++) {
|
||||
if (book_side[i].price != 0 && book_side[i].price == op.price) {
|
||||
// For modify, we might want to update or keep the same value
|
||||
book_side[i].price = op.price;
|
||||
book_side[i].total_quantity = op.quantity;
|
||||
break;
|
||||
}
|
||||
}break;
|
||||
case IMarketDataListener::BookOpType::MODIFY: {
|
||||
for (int i = 0; i < fmin(book_side.size(), 5); i++) {
|
||||
if (book_side[i].price != 0 && book_side[i].price == op.price) {
|
||||
// For modify, we might want to update or keep the same value
|
||||
book_side[i].price = op.price;
|
||||
book_side[i].total_quantity = op.quantity;
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case IMarketDataListener::BookOpType::REMOVE: {
|
||||
int found_position = -1;
|
||||
for (size_t i = 0; i < fmin(book_side.size(),5); i++){
|
||||
if(book_side[i].price == op.price){
|
||||
for (size_t i = 0; i < fmin(book_side.size(), 5); i++) {
|
||||
if (book_side[i].price == op.price) {
|
||||
found_position = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
size_t level =found_position;
|
||||
for (int i = found_position; i < fmin(book_side.size(),5); i++){
|
||||
book_side[i].price = book_side[i+1].price;
|
||||
book_side[i].total_quantity = book_side[i+1].total_quantity;
|
||||
size_t level = found_position;
|
||||
for (int i = found_position; i < fmin(book_side.size(), 5); i++) {
|
||||
book_side[i].price = book_side[i + 1].price;
|
||||
book_side[i].total_quantity = book_side[i + 1].total_quantity;
|
||||
level++;
|
||||
}
|
||||
if(found_position != -1){
|
||||
book_side[fmin(book_side.size()-1,level)].total_quantity = domain::Quantity(0);
|
||||
if (found_position != -1) {
|
||||
book_side[fmin(book_side.size() - 1, level)].total_quantity =
|
||||
domain::Quantity(0);
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
LOG(md, Error, "In the Default Case for IMarketDataListener::BookOp {}");
|
||||
LOG(md, Error,
|
||||
"In the Default Case for IMarketDataListener::BookOp {}");
|
||||
break;
|
||||
}
|
||||
}
|
||||
/*---------------------*/
|
||||
int counter = 0;
|
||||
for (auto &level : book_side) {
|
||||
if (counter > 4) {
|
||||
level.total_quantity = domain::Quantity(0);
|
||||
}
|
||||
counter++;
|
||||
}
|
||||
/*---------------------*/
|
||||
int counter =0;
|
||||
for(auto &level:book_side){
|
||||
if(counter >4){
|
||||
level.total_quantity =domain::Quantity(0);
|
||||
|
||||
// Log final state
|
||||
std::stringstream book_after;
|
||||
book_after << "Book after update (" << side_str << " side):\n";
|
||||
for (const auto &level : book_side) {
|
||||
book_after << "Price: " << level.price
|
||||
<< ", Quantity: " << level.total_quantity << "\n";
|
||||
}
|
||||
counter++;
|
||||
|
||||
std::string book_before_after_op =
|
||||
book_before.str() + "\n" + book_after.str() + "\n" + op_details.str();
|
||||
LOG(md, Diag, "Book Increment desc: {}", book_before_after_op);
|
||||
bts_assert_msgf(
|
||||
book->is_sorted(), "book not sorted {}", book_before_after_op);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Log final state
|
||||
std::stringstream book_after;
|
||||
book_after << "Book after update (" << side_str << " side):\n";
|
||||
for (const auto& level : book_side) {
|
||||
book_after << "Price: " << level.price << ", Quantity: " << level.total_quantity << "\n";
|
||||
}
|
||||
|
||||
std::string book_before_after_op = book_before.str() + "\n" + book_after.str() + "\n" + op_details.str();
|
||||
LOG(md, Diag, "Book Increment desc: {}", book_before_after_op);
|
||||
bts_assert_msgf(book->is_sorted(), "book not sorted {}", book_before_after_op);
|
||||
|
||||
return true;
|
||||
}
|
||||
domain::Book const *get_const_book(domain::SecurityID id) const {
|
||||
auto it = book_map.find(id);
|
||||
if (it == book_map.end()) {
|
||||
@@ -266,7 +290,7 @@ struct GeneralMarketDataListener::PImpl {
|
||||
// if this is set to true, then the security key passed on in the
|
||||
// on_securities function is used. If this is not set to true, then a new key
|
||||
// is generated
|
||||
const bool use_given_sec_key = false;
|
||||
const bool use_given_sec_key = true;
|
||||
const std::string cc_name_prefix = "cc_security";
|
||||
size_t n_bk_updates = 0;
|
||||
bts::persistence::IDGenerator &id_gen =
|
||||
@@ -289,8 +313,7 @@ struct GeneralMarketDataListener::PImpl {
|
||||
GeneralSecurityIndex &index,
|
||||
MDProducer &md_producer,
|
||||
std::shared_ptr<bts::orderman::SimExecutionGateway> sim_ex_gwy)
|
||||
: venue(venue), index(index),
|
||||
md_producer(md_producer),
|
||||
: venue(venue), index(index), md_producer(md_producer),
|
||||
sim_ex_gwy(std::move(sim_ex_gwy)) {
|
||||
if (this->sim_ex_gwy != nullptr) {
|
||||
this->sim_ex_gwy->set_producers(&this->index);
|
||||
@@ -452,7 +475,7 @@ void GeneralMarketDataListener::on_book(
|
||||
|
||||
void GeneralMarketDataListener::on_book_increment(
|
||||
const IMarketDataListener::BookOp &book_op) {
|
||||
if(!me->inc_book_data.update_incremental(book_op)){
|
||||
if (!me->inc_book_data.update_incremental(book_op)) {
|
||||
return;
|
||||
}
|
||||
domain::Book const *book_ptr = me->inc_book_data.get_const_book(book_op.id);
|
||||
@@ -659,6 +682,18 @@ static bool get_qty_info(
|
||||
qty_tick_unit = 1;
|
||||
qty_tick_base = 1000000;
|
||||
break;
|
||||
case 10'000'000:
|
||||
qty_tick_unit = 1;
|
||||
qty_tick_base = 10'000'000;
|
||||
break;
|
||||
case 100'000'000:
|
||||
qty_tick_unit = 1;
|
||||
qty_tick_base = 100'000'000;
|
||||
break;
|
||||
case 1'000'000'000:
|
||||
qty_tick_unit = 1;
|
||||
qty_tick_base = 1'000'000'000;
|
||||
break;
|
||||
case 4:
|
||||
qty_tick_unit = 25;
|
||||
qty_tick_base = 100;
|
||||
@@ -766,11 +801,15 @@ void GeneralMarketDataListener::_on_book(
|
||||
_possibly_match_with_sim_orders(
|
||||
bts::md::SecurityKey(book.security_id().value()));
|
||||
LOG(md, Diag,
|
||||
"GeneralMarketDataListener::_on_book post id: {} bid: {}/{}/{} ask: "
|
||||
"GeneralMarketDataListener::_on_book sec: {} sec_id: {} handle_id: {} "
|
||||
"bid: "
|
||||
"{}/{}/{} ask: "
|
||||
"{}/{}/{}",
|
||||
handle->id, me->bids.at(0).price(), me->bids.at(0).direct_size(),
|
||||
me->bids.at(0).size(), me->asks.at(0).price(),
|
||||
me->asks.at(0).direct_size(), me->asks.at(0).size());
|
||||
std::string(handle->handle.security().security_desc()),
|
||||
book.security_id(), handle->id, me->bids.at(0).price(),
|
||||
me->bids.at(0).direct_size(), me->bids.at(0).size(),
|
||||
me->asks.at(0).price(), me->asks.at(0).direct_size(),
|
||||
me->asks.at(0).size());
|
||||
handle->handle.update_direct(time.value(), me->bids, me->asks);
|
||||
}
|
||||
|
||||
|
||||
@@ -435,7 +435,8 @@ class SyntheticConsumerInst {
|
||||
void add_security(const api::Security &sec) {
|
||||
Mutex::scoped_lock lock(_workmutex);
|
||||
if (_data.find(sec.id()) == _data.end()) {
|
||||
LOG(md, Info, "MDConsumer::add_security. {}", sec);
|
||||
LOG(md, Info, "MDConsumer::add_security. {}",
|
||||
bts::securities::descriptor(sec));
|
||||
auto ptr = to_strategy(make_shared<api::Security>(sec));
|
||||
vector<IncompleteStrategy> vec;
|
||||
vec.push_back(ptr);
|
||||
|
||||
@@ -14,6 +14,15 @@
|
||||
namespace bts {
|
||||
namespace md {
|
||||
|
||||
#define ASSERT_FN(test, fn, ...) \
|
||||
bts_assert_msg(test, "SecurityCache::" fn ". " __VA_ARGS__)
|
||||
#define LOG_FN(level, fn, ...) \
|
||||
LOG(md, level, "SecurityCache::" fn ". " __VA_ARGS__)
|
||||
#define DIAG_FN(fn, ...) LOG_FN(Diag, fn, __VA_ARGS__)
|
||||
#define INFO_FN(fn, ...) LOG_FN(Info, fn, __VA_ARGS__)
|
||||
#define WARN_FN(fn, ...) LOG_FN(Warn, fn, __VA_ARGS__)
|
||||
#define ERROR_FN(fn, ...) LOG_FN(Error, fn, __VA_ARGS__)
|
||||
|
||||
using std::string;
|
||||
using std::unordered_map;
|
||||
|
||||
@@ -99,6 +108,9 @@ struct SecurityCacheIndex {
|
||||
// Insert but don't overwrite existing
|
||||
auto result =
|
||||
cache.insert(std::make_pair(SecurityKey(security->id()), security));
|
||||
venue_type_ids.get_or_add(security->exchange())
|
||||
.get_or_add(bts::securities::security_type(*security))
|
||||
.try_emplace(bts::securities::descriptor(*security), security->id());
|
||||
return result.second;
|
||||
}
|
||||
|
||||
@@ -166,8 +178,8 @@ struct SecurityCacheImpl {
|
||||
};
|
||||
|
||||
void SecurityCacheImpl::enqueue_update_db(const SecurityPtr &security) {
|
||||
LOG(md, Diag, "SecurityCacheImpl::enqueue_update_db security: {}",
|
||||
bts::securities::descriptor(*security));
|
||||
DIAG_FN(
|
||||
"enqueue_update_db", "security: {}", securities::descriptor(*security));
|
||||
update_queue.push(security);
|
||||
}
|
||||
|
||||
@@ -180,7 +192,13 @@ void SecurityCacheImpl::flush() {
|
||||
work.push_back(temp);
|
||||
}
|
||||
if (!work.empty()) {
|
||||
INFO_FN("flush", "Saving securities to db. count: {}", work.size());
|
||||
if (db::insert_or_replace(work) == Result::error) {
|
||||
WARN_FN(
|
||||
"flush",
|
||||
"Failed to save securities to db. Will retry on next flush. "
|
||||
"count: {}",
|
||||
work.size());
|
||||
// If we fail to insert then retry later
|
||||
for (auto &s : work)
|
||||
update_queue.push(std::move(s));
|
||||
@@ -270,14 +288,18 @@ std::vector<api::Security const *> SecurityCache::get_securities() {
|
||||
}
|
||||
|
||||
bool SecurityCache::add_live_security(const SecurityPtr &security) {
|
||||
bts_assert(security);
|
||||
LOG(md, Diag, "SecurityCache::add_live_security security: {}",
|
||||
bts::securities::descriptor(*security));
|
||||
return _me->cache.lock([&](SecurityCacheIndex &cache) {
|
||||
ASSERT_FN(security, "add_live_security", "security is null");
|
||||
const bool is_new_sec = _me->cache.lock([&](SecurityCacheIndex &cache) {
|
||||
const bool is_new_sec = cache.add_live_security(security);
|
||||
_me->enqueue_update_db(security);
|
||||
return is_new_sec;
|
||||
});
|
||||
if (is_new_sec) {
|
||||
INFO_FN(
|
||||
"add_live_security", "new security: {}",
|
||||
bts::securities::descriptor(*security));
|
||||
}
|
||||
return is_new_sec;
|
||||
}
|
||||
|
||||
void SecurityCache::flush_to_disk() { _me->flush(); }
|
||||
|
||||
@@ -7,7 +7,6 @@
|
||||
#include "lib/utils/checked_arithmetic.h"
|
||||
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <boost/pool/object_pool.hpp>
|
||||
#include <memory>
|
||||
#include <sqlite/sqlite3.h>
|
||||
#include <stdexcept>
|
||||
|
||||
@@ -509,6 +509,91 @@ void convert_security(
|
||||
}
|
||||
}
|
||||
|
||||
void to_json(nlohmann::json &j, const FixedSecurity &f) {
|
||||
j = nlohmann::json{
|
||||
{"key", f.key},
|
||||
{"symbol", f.symbol()},
|
||||
{"type", api::SecurityType_Name(f.type)},
|
||||
{"venue", api::Venue_Name(f.exchange)},
|
||||
{"alt_symbol", f.alt_symbol()},
|
||||
{"cfi_code", f.cfi_code()},
|
||||
{"security_desc", f.security_desc()},
|
||||
{"platform_id", f.platform_id()},
|
||||
{"display_factor", f.display_factor},
|
||||
{"numeric_id", f.numeric_id},
|
||||
{"cboe_class_key", f.cboe_class_key},
|
||||
{"main_frac", (int)f.cme_main_fraction},
|
||||
{"sub_frac", (int)f.cme_sub_fraction},
|
||||
{"price_format", (int)f.cme_price_display_format},
|
||||
{"market_segment", (int)f.cme_market_segment_id},
|
||||
|
||||
{"price_break", f.price_break},
|
||||
{"low_tick", f.low_tick},
|
||||
{"high_tick", f.high_tick},
|
||||
{"cpp", f.cash_per_point},
|
||||
|
||||
{"ice_market_description", f.ice_market_description()},
|
||||
{"ice_cleared_alias", f.ice_cleared_alias()},
|
||||
{"ice_product_name", f.ice_product_name()},
|
||||
{"ice_hub_alias", f.ice_hub_alias()},
|
||||
{"ice_strip_name", f.ice_strip_name()},
|
||||
{"mic_code", f.mic_code()},
|
||||
};
|
||||
// Qty Info
|
||||
{
|
||||
auto qty_infos_j = nlohmann::json::array();
|
||||
float min_value, max_value, tick_unit, tick_base, max_price;
|
||||
bool is_notional;
|
||||
for (int8_t i = 0; f.get_quantity_info(
|
||||
i, min_value, max_value, tick_unit, tick_base, max_price,
|
||||
is_notional);
|
||||
i++) {
|
||||
auto qty_info_j = nlohmann::json::object();
|
||||
qty_info_j["min_value"] = min_value;
|
||||
qty_info_j["max_value"] = max_value;
|
||||
qty_info_j["tick_unit"] = tick_unit;
|
||||
qty_info_j["tick_base"] = tick_base;
|
||||
qty_info_j["max_price"] = max_price;
|
||||
qty_info_j["is_notional"] = is_notional;
|
||||
qty_infos_j.push_back(std::move(qty_info_j));
|
||||
}
|
||||
j["qty_infos"] = std::move(qty_infos_j);
|
||||
}
|
||||
|
||||
switch (f.type) {
|
||||
case api::SECURITYTYPE_SPOT:
|
||||
break;
|
||||
case api::SECURITYTYPE_FUTURE: {
|
||||
auto *fut = static_cast<const FixedFutureSecurity *>(&f);
|
||||
core::TimeStamp exp;
|
||||
core::TimeStamp::from_ymd(
|
||||
fut->expiration_year, fut->expiration_month, fut->expiration_day,
|
||||
&exp);
|
||||
j["expiration"] = exp.to_iso_string();
|
||||
break;
|
||||
}
|
||||
case api::SECURITYTYPE_OPTION: {
|
||||
auto *opt = static_cast<const FixedOptionSecurity *>(&f);
|
||||
core::TimeStamp exp;
|
||||
core::TimeStamp::from_ymd(
|
||||
opt->expiration_year, opt->expiration_month, opt->expiration_day,
|
||||
&exp);
|
||||
j["expiration"] = exp.to_iso_string();
|
||||
j["strike"] = opt->strike;
|
||||
j["callput"] = api::PutOrCall_Name(opt->call_put);
|
||||
break;
|
||||
}
|
||||
case api::SECURITYTYPE_SPREAD: {
|
||||
auto *spd = static_cast<const FixedSpreadSecurityXL *>(&f);
|
||||
std::vector<FixedSpreadLeg> legs{spd->legs, spd->legs + spd->leg_count()};
|
||||
j["legs"] = legs;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace md
|
||||
} // namespace bts
|
||||
|
||||
|
||||
@@ -273,71 +273,6 @@ inline void to_json(nlohmann::json &j, const FixedSpreadLeg &f) {
|
||||
};
|
||||
}
|
||||
|
||||
inline void to_json(nlohmann::json &j, const FixedSecurity &f) {
|
||||
j = nlohmann::json{
|
||||
{"key", f.key},
|
||||
{"symbol", f.symbol()},
|
||||
{"type", api::SecurityType_Name(f.type)},
|
||||
{"venue", api::Venue_Name(f.exchange)},
|
||||
{"alt_symbol", f.alt_symbol()},
|
||||
{"cfi_code", f.cfi_code()},
|
||||
{"security_desc", f.security_desc()},
|
||||
{"platform_id", f.platform_id()},
|
||||
{"display_factor", f.display_factor},
|
||||
{"numeric_id", f.numeric_id},
|
||||
{"cboe_class_key", f.cboe_class_key},
|
||||
{"main_frac", (int)f.cme_main_fraction},
|
||||
{"sub_frac", (int)f.cme_sub_fraction},
|
||||
{"price_format", (int)f.cme_price_display_format},
|
||||
{"market_segment", (int)f.cme_market_segment_id},
|
||||
|
||||
{"price_break", f.price_break},
|
||||
{"low_tick", f.low_tick},
|
||||
{"high_tick", f.high_tick},
|
||||
{"cpp", f.cash_per_point},
|
||||
|
||||
{"ice_market_description", f.ice_market_description()},
|
||||
{"ice_cleared_alias", f.ice_cleared_alias()},
|
||||
{"ice_product_name", f.ice_product_name()},
|
||||
{"ice_hub_alias", f.ice_hub_alias()},
|
||||
{"ice_strip_name", f.ice_strip_name()},
|
||||
{"mic_code", f.mic_code()},
|
||||
};
|
||||
|
||||
switch (f.type) {
|
||||
case api::SECURITYTYPE_SPOT:
|
||||
break;
|
||||
case api::SECURITYTYPE_FUTURE: {
|
||||
auto *fut = static_cast<const FixedFutureSecurity *>(&f);
|
||||
core::TimeStamp exp;
|
||||
core::TimeStamp::from_ymd(
|
||||
fut->expiration_year, fut->expiration_month, fut->expiration_day,
|
||||
&exp);
|
||||
j["expiration"] = exp.to_iso_string();
|
||||
break;
|
||||
}
|
||||
case api::SECURITYTYPE_OPTION: {
|
||||
auto *opt = static_cast<const FixedOptionSecurity *>(&f);
|
||||
core::TimeStamp exp;
|
||||
core::TimeStamp::from_ymd(
|
||||
opt->expiration_year, opt->expiration_month, opt->expiration_day,
|
||||
&exp);
|
||||
j["expiration"] = exp.to_iso_string();
|
||||
j["strike"] = opt->strike;
|
||||
j["callput"] = api::PutOrCall_Name(opt->call_put);
|
||||
break;
|
||||
}
|
||||
case api::SECURITYTYPE_SPREAD: {
|
||||
auto *spd = static_cast<const FixedSpreadSecurityXL *>(&f);
|
||||
std::vector<FixedSpreadLeg> legs{spd->legs, spd->legs + spd->leg_count()};
|
||||
j["legs"] = legs;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
inline std::ostream &operator<<(
|
||||
std::ostream &out, const FixedSecurity &f) noexcept {
|
||||
out << io::JsonDumper::dump(f);
|
||||
|
||||
@@ -8,7 +8,10 @@ namespace mercury {
|
||||
BrokerProxy::BrokerProxy(ILocalBroker &real_broker)
|
||||
: _real_broker(real_broker), _id(real_broker.id()), _cycles(0),
|
||||
_outgoing(config::get<config::Mercury>().queue_size()),
|
||||
_incoming(config::get<config::Mercury>().queue_size()), _action_pool() {}
|
||||
_incoming(config::get<config::Mercury>().queue_size()), _action_pool() {
|
||||
_action_pool.set_max_count(4096 * 10);
|
||||
_action_pool.set_log_freq(100);
|
||||
}
|
||||
|
||||
BrokerProxy::~BrokerProxy() {}
|
||||
|
||||
@@ -96,5 +99,60 @@ void BrokerProxy::schedule_action(std::function<void()> const &action) {
|
||||
send(*m);
|
||||
}
|
||||
|
||||
void BrokerProxy::do_work(trace::PerfLog &log) {
|
||||
_cycles++;
|
||||
_outgoing.consume_all([this, &log](BrokerMsg *m) {
|
||||
switch (m->msg_type) {
|
||||
case BrokerMsgType::ADD_NEIGHBOUR:
|
||||
_real_broker.add_neighbour(*m->neighbour);
|
||||
break;
|
||||
case BrokerMsgType::REMOVE_NEIGHBOUR:
|
||||
_real_broker.remove_neighbour(m->broker_id);
|
||||
break;
|
||||
case BrokerMsgType::ADVERTISE:
|
||||
_real_broker.advertise(m->broker_id, m->stream_id, m->topic);
|
||||
break;
|
||||
case BrokerMsgType::UNADVERTISE:
|
||||
_real_broker.unadvertise(m->broker_id, m->stream_id);
|
||||
break;
|
||||
case BrokerMsgType::SUBSCRIBE:
|
||||
_real_broker.subscribe(m->broker_id, m->sub_id, m->stream_id);
|
||||
break;
|
||||
case BrokerMsgType::UNSUBSCRIBE:
|
||||
_real_broker.unsubscribe(m->broker_id, m->sub_id, m->stream_id);
|
||||
break;
|
||||
case BrokerMsgType::NOTIFY: {
|
||||
LOG(Latency, Diag, "Notify broker {}. Cycles: {}", _id.value(),
|
||||
_cycles);
|
||||
auto scoped_do_work = trace::log_scope(log, {}, "BrokerProxy::notify");
|
||||
_real_broker.notify(
|
||||
m->broker_id, m->stream_id, m->sub_id, m->notification);
|
||||
} break;
|
||||
case BrokerMsgType::LOCAL_SUBSCRIBE:
|
||||
_real_broker.local_subscribe(
|
||||
m->local_sub_id, m->filter, m->local_handler);
|
||||
break;
|
||||
case BrokerMsgType::LOCAL_UNSUBSCRIBE:
|
||||
_real_broker.local_unsubscribe(m->local_sub_id);
|
||||
break;
|
||||
case BrokerMsgType::EXECUTE_ACTION: {
|
||||
auto start = Timer::now();
|
||||
LOG(Latency, Diag, "Executing lambda on broker {}", _id.value());
|
||||
m->action();
|
||||
auto stop = Timer::now();
|
||||
LOG(Latency, Diag,
|
||||
"Finished executing lambda on broker {}. Took: {}us", _id.value(),
|
||||
us_diff(stop, start));
|
||||
break;
|
||||
}
|
||||
default:
|
||||
bts_assert(false);
|
||||
break;
|
||||
}
|
||||
_incoming.drain();
|
||||
_incoming.push(m);
|
||||
});
|
||||
}
|
||||
|
||||
} // namespace mercury
|
||||
} // namespace bts
|
||||
@@ -5,8 +5,8 @@
|
||||
#include "lib/mercury/topics/Topic.h"
|
||||
#include "lib/tracing/PerfLog.h"
|
||||
#include "lib/utils/GrowableSPSCQueue.h"
|
||||
#include "lib/utils/object_pool.h"
|
||||
|
||||
#include <boost/pool/object_pool.hpp>
|
||||
#include <memory>
|
||||
|
||||
namespace bts {
|
||||
@@ -80,61 +80,7 @@ class BrokerProxy : public ILocalBroker {
|
||||
send(m);
|
||||
}
|
||||
|
||||
void do_work(trace::PerfLog &log) {
|
||||
_cycles++;
|
||||
_outgoing.consume_all([this, &log](BrokerMsg *m) {
|
||||
switch (m->msg_type) {
|
||||
case BrokerMsgType::ADD_NEIGHBOUR:
|
||||
_real_broker.add_neighbour(*m->neighbour);
|
||||
break;
|
||||
case BrokerMsgType::REMOVE_NEIGHBOUR:
|
||||
_real_broker.remove_neighbour(m->broker_id);
|
||||
break;
|
||||
case BrokerMsgType::ADVERTISE:
|
||||
_real_broker.advertise(m->broker_id, m->stream_id, m->topic);
|
||||
break;
|
||||
case BrokerMsgType::UNADVERTISE:
|
||||
_real_broker.unadvertise(m->broker_id, m->stream_id);
|
||||
break;
|
||||
case BrokerMsgType::SUBSCRIBE:
|
||||
_real_broker.subscribe(m->broker_id, m->sub_id, m->stream_id);
|
||||
break;
|
||||
case BrokerMsgType::UNSUBSCRIBE:
|
||||
_real_broker.unsubscribe(m->broker_id, m->sub_id, m->stream_id);
|
||||
break;
|
||||
case BrokerMsgType::NOTIFY: {
|
||||
LOG(Latency, Diag, "Notify broker {}. Cycles: {}", _id.value(),
|
||||
_cycles);
|
||||
auto scoped_do_work =
|
||||
trace::log_scope(log, {}, "BrokerProxy::notify");
|
||||
_real_broker.notify(
|
||||
m->broker_id, m->stream_id, m->sub_id, m->notification);
|
||||
} break;
|
||||
case BrokerMsgType::LOCAL_SUBSCRIBE:
|
||||
_real_broker.local_subscribe(
|
||||
m->local_sub_id, m->filter, m->local_handler);
|
||||
break;
|
||||
case BrokerMsgType::LOCAL_UNSUBSCRIBE:
|
||||
_real_broker.local_unsubscribe(m->local_sub_id);
|
||||
break;
|
||||
case BrokerMsgType::EXECUTE_ACTION: {
|
||||
auto start = Timer::now();
|
||||
LOG(Latency, Diag, "Executing lambda on broker {}", _id.value());
|
||||
m->action();
|
||||
auto stop = Timer::now();
|
||||
LOG(Latency, Diag,
|
||||
"Finished executing lambda on broker {}. Took: {}us",
|
||||
_id.value(), us_diff(stop, start));
|
||||
break;
|
||||
}
|
||||
default:
|
||||
bts_assert(false);
|
||||
break;
|
||||
}
|
||||
_incoming.drain();
|
||||
_incoming.push(m);
|
||||
});
|
||||
}
|
||||
void do_work(trace::PerfLog &log);
|
||||
|
||||
private:
|
||||
ILocalBroker &_real_broker;
|
||||
@@ -142,7 +88,7 @@ class BrokerProxy : public ILocalBroker {
|
||||
uint64_t _cycles;
|
||||
utils::GrowableSPSCQueue<BrokerMsg *> _outgoing;
|
||||
utils::GrowableSPSCQueue<BrokerMsg *> _incoming;
|
||||
boost::object_pool<BrokerMsg> _action_pool;
|
||||
bts::utils::object_pool<BrokerMsg> _action_pool;
|
||||
|
||||
void send(BrokerMsg &m) {
|
||||
_outgoing.push(&m);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "lib/api/services/OrderService.h"
|
||||
#include "lib/core/FMT_IMPL.h"
|
||||
#include "lib/domain_model/Security.h"
|
||||
#include "lib/domain_model/common.h"
|
||||
@@ -24,6 +25,25 @@ struct CreateOrderMsg {
|
||||
api::orderman::RoutingRecord const *routing_record;
|
||||
};
|
||||
|
||||
/**
|
||||
* A CreateOrderMsg wrapper with a custom fmt::formatter that doesn't access the
|
||||
* raw security pointer. This is mainly used for logging purposes (because the
|
||||
* security pointer may be invalid by the time the fmt::formatter is invoked).
|
||||
*/
|
||||
struct CreateOrderMsg_JsonData {
|
||||
CreateOrderMsg_JsonData() = default;
|
||||
CreateOrderMsg_JsonData(CreateOrderMsg source) : source(std::move(source)) {
|
||||
if (this->source.security != nullptr) {
|
||||
sec_name = securities::descriptor(*this->source.security);
|
||||
}
|
||||
}
|
||||
|
||||
CreateOrderMsg source;
|
||||
std::string sec_name;
|
||||
};
|
||||
|
||||
void to_json(nlohmann::json &j, const CreateOrderMsg &msg);
|
||||
|
||||
inline std::ostream &operator<<(std::ostream &out, CreateOrderMsg const &msg) {
|
||||
out << "key: " << msg.key.value() << " price: " << msg.price.value()
|
||||
<< " qty: " << msg.quantity.full_value() << " security: "
|
||||
@@ -43,6 +63,29 @@ inline std::ostream &operator<<(std::ostream &out, CreateOrderMsg const &msg) {
|
||||
return out;
|
||||
}
|
||||
|
||||
inline void to_json(
|
||||
nlohmann::json &j, const CreateOrderMsg_JsonData &msg_data) {
|
||||
const auto &msg = msg_data.source;
|
||||
j["key"] = msg.key.value();
|
||||
j["price"] = msg.price.value();
|
||||
j["quantity"] = msg.quantity.value();
|
||||
if (msg.display_quantity.value() != 0) {
|
||||
j["display_quantity"] = msg.quantity.value();
|
||||
}
|
||||
if (!msg_data.sec_name.empty()) {
|
||||
j["security"] = msg_data.sec_name;
|
||||
}
|
||||
j["side"] = msg.side;
|
||||
if (msg.stop_px.value() != 0) {
|
||||
j["stop_px"] = msg.stop_px.value();
|
||||
}
|
||||
j["time_in_force"] = msg.time_in_force;
|
||||
j["manual_order_indicator"] = msg.manual_order_indicator;
|
||||
if (msg.routing_record != nullptr) {
|
||||
j["routing_record"] = *msg.routing_record;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace bts::orderman
|
||||
|
||||
FMT_IMPL_OSTREAM(bts::orderman::CreateOrderMsg);
|
||||
@@ -8,6 +8,7 @@
|
||||
#include "lib/config/config.h"
|
||||
#include "lib/config/protos/jetfix.pb.h"
|
||||
#include "lib/config/protos/orderman_cfg.pb.h"
|
||||
#include "lib/core/FMT.h"
|
||||
#include "lib/domain_model/Security.pb.h"
|
||||
#include "lib/domain_model/common.h"
|
||||
#include "lib/logging/logging.h"
|
||||
@@ -29,11 +30,11 @@
|
||||
#include "lib/orderman/actors/order_manager/order_gateway_definition.h"
|
||||
#include "lib/orderman/events/ExchangePositionEvent.h"
|
||||
#include "lib/orderman/events/OrderOpResultEvent.h"
|
||||
#include "lib/orderman/gateways/execution_gateway_provider.h"
|
||||
#include "lib/orderman/gateways/i_execution_gateway_provider.h"
|
||||
#include "lib/orderman/order_factory/DynamicOrderRegistry.h"
|
||||
#include "lib/orderman/orderman_utils/ExchangeOpenOrders.h"
|
||||
#include "lib/orderman/orders/Order.h"
|
||||
#include "lib/os/os.h"
|
||||
#include "lib/os/spark_data_directory.h"
|
||||
#include "lib/plugin/SparkServices.h"
|
||||
#include "lib/plugin/plugin.h"
|
||||
@@ -41,7 +42,6 @@
|
||||
#include "lib/utils/bts_chrono.h"
|
||||
#include "lib/utils/thread_manager.h"
|
||||
#include "lib/utils/type_name.h"
|
||||
#include "lib_binance_execution/binance_execution/execution_gateway_provider.h"
|
||||
|
||||
#include <bits/stdint-uintn.h>
|
||||
#include <boost/algorithm/string/predicate.hpp>
|
||||
@@ -73,16 +73,58 @@ struct ActorThread {
|
||||
void start(trace::PerfLog &log) {
|
||||
if (cfg.poll())
|
||||
thread = threading::make_thread(cfg.thread_name(), [this, &log]() {
|
||||
for (;;)
|
||||
const core::TimeSpan log_interval =
|
||||
core::TimeSpan::from_seconds(30);
|
||||
core::TimeStamp last_log_time{0};
|
||||
core::TimeStamp now;
|
||||
core::TimeSpan elapsed;
|
||||
std::uint32_t last_count = 0;
|
||||
for (;;) {
|
||||
now = core::TimeStamp::now();
|
||||
elapsed = now - last_log_time;
|
||||
if (elapsed > log_interval) {
|
||||
LOG(OrderMan, Info,
|
||||
"OrderServiceServer ActorThread work speed is {:.2f} "
|
||||
"cycles/sec",
|
||||
static_cast<float>(last_count) / elapsed.total_seconds());
|
||||
last_log_time = now;
|
||||
last_count = 0;
|
||||
}
|
||||
executor.do_work(log);
|
||||
last_count++;
|
||||
}
|
||||
});
|
||||
else {
|
||||
thread = threading::make_thread(cfg.thread_name(), [this, &log]() {
|
||||
// for logging purposes
|
||||
const std::string t_name = bts::os::get_thread_name();
|
||||
const std::string actors_desc = FMT_JOIN(cfg.hosted_actor(), ", ");
|
||||
const core::TimeSpan log_interval =
|
||||
core::TimeSpan::from_seconds(30);
|
||||
|
||||
const auto inner_iter = 1000;
|
||||
bts::core::TimeStamp last_log_time{0};
|
||||
core::TimeStamp now;
|
||||
core::TimeSpan elapsed;
|
||||
std::uint32_t last_count = 0;
|
||||
while (!done) {
|
||||
now = core::TimeStamp::now();
|
||||
elapsed = now - last_log_time;
|
||||
auto log_elapsed = now - last_log_time;
|
||||
if (log_elapsed > log_interval) {
|
||||
LOG(OrderMan, Info,
|
||||
"OrderServiceServer ActorThread {} work speed is {:.2f} "
|
||||
"cycles/sec ({})",
|
||||
t_name,
|
||||
static_cast<float>(last_count) / elapsed.total_seconds(),
|
||||
actors_desc);
|
||||
last_log_time = now;
|
||||
last_count = 0;
|
||||
}
|
||||
auto start = Timer::now();
|
||||
for (auto i = 0; i < inner_iter; i++)
|
||||
executor.do_work(log);
|
||||
last_count += inner_iter;
|
||||
auto stop = Timer::now();
|
||||
loop_metric->observe(ns_diff(stop, start) / inner_iter);
|
||||
std::this_thread::sleep_for(10ms);
|
||||
|
||||
@@ -24,6 +24,12 @@
|
||||
namespace bts {
|
||||
namespace orderman {
|
||||
|
||||
#define LOG_FN(level, fn, ...) \
|
||||
LOG(OrderMan, level, "FillStore::" fn ". " __VA_ARGS__)
|
||||
#define INFO_FN(fn, ...) LOG_FN(Info, fn, __VA_ARGS__)
|
||||
#define WARN_FN(fn, ...) LOG_FN(Warn, fn, __VA_ARGS__)
|
||||
#define ERROR_FN(fn, ...) LOG_FN(Error, fn, __VA_ARGS__)
|
||||
|
||||
auto const fills_ns = "fills";
|
||||
|
||||
struct FillStore::UnresolvedFills {
|
||||
@@ -160,7 +166,7 @@ void FillStore::on_execution_report(
|
||||
ev.data->to_string());
|
||||
auto &er = *ev.data;
|
||||
auto fk = fmt::format(
|
||||
"{}:{}", er.order_key.value(), to_str(er.exchange_fill_id));
|
||||
"{}:{}", er.order_key.value(), to_strn(er.exchange_fill_id));
|
||||
auto is_known = me->known_fills.count(fk);
|
||||
if (er.exec_type == api::orderman::EXEC_TYPE_PARTIAL_FILL ||
|
||||
er.exec_type == api::orderman::EXEC_TYPE_FILL) {
|
||||
@@ -176,7 +182,7 @@ void FillStore::on_execution_report(
|
||||
fill.set_fill_kind(api::orderman::Fill::FILL_KIND_STANDARD);
|
||||
fill.set_security_key(er.security->id());
|
||||
}
|
||||
fill.set_account(to_str(er.account_name));
|
||||
fill.set_account(to_strn(er.account_name));
|
||||
fill.set_last_price(er.last_price.value());
|
||||
fill.set_avg_price(er.avg_price.value());
|
||||
fill.set_side(er.side);
|
||||
@@ -207,7 +213,15 @@ void FillStore::on_execution_report(
|
||||
auto it = me->known_orders.find(er.order_key);
|
||||
if (it != me->known_orders.end()) {
|
||||
auto &order_def = it->second;
|
||||
fill.set_account(order_def.account());
|
||||
if (fill.account() != order_def.account()) {
|
||||
WARN_FN(
|
||||
"on_execution_report",
|
||||
"ExecutionReport account '{}' does not match "
|
||||
"OrderDefinition account '{}' (order_key: {}). "
|
||||
"Setting fill account to the latter",
|
||||
fill.account(), order_def.account(), er.order_key);
|
||||
fill.set_account(order_def.account());
|
||||
}
|
||||
fill.set_internal_account_id(order_def.account_id());
|
||||
} else
|
||||
me->fills_with_unresolved_accounts[er.order_key]
|
||||
@@ -215,6 +229,7 @@ void FillStore::on_execution_report(
|
||||
|
||||
auto fv = utils::pb::to_string(fill);
|
||||
me->store->insert(fills_ns, fk, fv);
|
||||
LOG(OrderMan, Info, "Publishing fill. {}", fv);
|
||||
publish(me->fill_stream, fill, *er.security);
|
||||
} else
|
||||
LOG(OrderMan, Info,
|
||||
|
||||
@@ -155,7 +155,7 @@ void OrderStore::store_and_publish_if_complete(
|
||||
auto &rec_info = me->known_orders[k];
|
||||
auto &rec = rec_info.rec;
|
||||
if (rec.has_def() && rec.has_state()) {
|
||||
LOG(OrderMan, Info,
|
||||
LOG(OrderMan, Diag,
|
||||
"OrderStore::store_and_publish_if_complete rec: {}",
|
||||
utils::pb::to_string(rec));
|
||||
publish(me->order_stream, rec);
|
||||
|
||||
@@ -37,6 +37,12 @@
|
||||
namespace bts {
|
||||
namespace orderman {
|
||||
|
||||
#define _LOG_FN(level, fn, ...) \
|
||||
LOG(OrderMan, level, "ServiceBridge::" fn ". " __VA_ARGS__)
|
||||
#define INFO_FN(fn, ...) _LOG_FN(Info, fn, __VA_ARGS__)
|
||||
#define WARN_FN(fn, ...) _LOG_FN(Warn, fn, __VA_ARGS__)
|
||||
#define ERROR_FN(fn, ...) _LOG_FN(Error, fn, __VA_ARGS__)
|
||||
|
||||
struct ServiceBridge::PImpl {
|
||||
PImpl(
|
||||
OnReady const &on_ready,
|
||||
@@ -173,56 +179,72 @@ void ServiceBridge::init() {
|
||||
} // namespace orderman
|
||||
|
||||
void ServiceBridge::on(mercury::Update<StoredFillEvent> const &update) {
|
||||
if (update.events) {
|
||||
std::vector<api::orderman::Fill> fills;
|
||||
fills.reserve(update.events->size());
|
||||
for (auto ev : *(update.events))
|
||||
fills.push_back(ev.fill);
|
||||
me->on_fills(fills);
|
||||
if (!update.events) {
|
||||
WARN_FN("on StoredFillEvent", "no event data");
|
||||
return;
|
||||
}
|
||||
INFO_FN("on StoredFillEvent", "n_events: {}", update.events->size());
|
||||
std::vector<api::orderman::Fill> fills;
|
||||
fills.reserve(update.events->size());
|
||||
for (auto ev : *(update.events))
|
||||
fills.push_back(ev.fill);
|
||||
me->on_fills(fills);
|
||||
}
|
||||
|
||||
void ServiceBridge::on(mercury::Update<StoredOrderEvent> const &update) {
|
||||
if (update.events) {
|
||||
bool was_ready = is_ready();
|
||||
me->have_orders = true;
|
||||
|
||||
std::vector<api::orderman::OrderRecord> orders;
|
||||
orders.reserve(update.events->size());
|
||||
for (auto ev : *(update.events)) {
|
||||
me->known_orders.insert(OrderKey(ev.order_record.key()));
|
||||
orders.push_back(ev.order_record);
|
||||
}
|
||||
me->on_orders(orders);
|
||||
maybe_signal_ready(was_ready);
|
||||
if (!update.events) {
|
||||
WARN_FN("on StoredOrderEvent", "no event data");
|
||||
return;
|
||||
}
|
||||
INFO_FN("on StoredOrderEvent", "n_events: {}", update.events->size());
|
||||
bool was_ready = is_ready();
|
||||
me->have_orders = true;
|
||||
|
||||
std::vector<api::orderman::OrderRecord> orders;
|
||||
orders.reserve(update.events->size());
|
||||
for (auto ev : *(update.events)) {
|
||||
me->known_orders.insert(OrderKey(ev.order_record.key()));
|
||||
orders.push_back(ev.order_record);
|
||||
}
|
||||
me->on_orders(orders);
|
||||
maybe_signal_ready(was_ready);
|
||||
}
|
||||
|
||||
void ServiceBridge::on(mercury::Update<OrderManagerReadyEvent> const &update) {
|
||||
if (update.events) {
|
||||
bool was_ready = is_ready();
|
||||
me->order_manager_ready = true;
|
||||
maybe_signal_ready(was_ready);
|
||||
if (!update.events) {
|
||||
WARN_FN("on OrderManagerReadyEvent", "no event data");
|
||||
return;
|
||||
}
|
||||
INFO_FN("on OrderManagerReadyEvent", "n_events: {}", update.events->size());
|
||||
bool was_ready = is_ready();
|
||||
me->order_manager_ready = true;
|
||||
maybe_signal_ready(was_ready);
|
||||
}
|
||||
|
||||
void ServiceBridge::on(mercury::Update<NewAdminDataEvent> const &update) {
|
||||
if (update.events)
|
||||
for (auto ev : *(update.events))
|
||||
me->order_router.update(*ev.admin_data);
|
||||
if (!update.events) {
|
||||
WARN_FN("on NewAdminDataEvent", "no event data");
|
||||
return;
|
||||
}
|
||||
INFO_FN("on NewAdminDataEvent", "n_events: {}", update.events->size());
|
||||
for (auto ev : *(update.events)) {
|
||||
me->order_router.update(*ev.admin_data);
|
||||
}
|
||||
}
|
||||
|
||||
void ServiceBridge::on(mercury::Update<OrderOpResultEvent> const &update) {
|
||||
if (update.events)
|
||||
for (auto ev : *(update.events))
|
||||
me->on_order_op_result(ev);
|
||||
if (!update.events) {
|
||||
WARN_FN("on OrderOpResultEvent", "no event data");
|
||||
return;
|
||||
}
|
||||
INFO_FN("on OrderOpResultEvent", "n_events: {}", update.events->size());
|
||||
for (auto ev : *(update.events)) {
|
||||
me->on_order_op_result(ev);
|
||||
}
|
||||
}
|
||||
|
||||
void ServiceBridge::create_order(uint32_t rid, api::orderman::Create const &c) {
|
||||
|
||||
LOG(OrderMan, Info, "Received new order from client: {}",
|
||||
utils::pb::to_string(c));
|
||||
|
||||
INFO_FN("create_order", "rid: {}; req: {}", rid, utils::pb::to_string(c));
|
||||
me->service_call_marshaller.schedule_action([this, c, rid]() {
|
||||
if (is_ready()) {
|
||||
auto &order =
|
||||
@@ -239,9 +261,7 @@ void ServiceBridge::create_order(uint32_t rid, api::orderman::Create const &c) {
|
||||
}
|
||||
|
||||
void ServiceBridge::update_order(uint32_t rid, api::orderman::Update const &c) {
|
||||
LOG(OrderMan, Info, "ServiceBridge::update_order update: {}",
|
||||
utils::pb::to_string(c));
|
||||
|
||||
INFO_FN("update_order", "rid: {}; req: {}", rid, utils::pb::to_string(c));
|
||||
me->service_call_marshaller.schedule_action([this, c, rid]() {
|
||||
auto ok = OrderKey(c.order_key());
|
||||
if (is_ready()) {
|
||||
@@ -259,9 +279,7 @@ void ServiceBridge::update_order(uint32_t rid, api::orderman::Update const &c) {
|
||||
}
|
||||
|
||||
void ServiceBridge::cancel_order(uint32_t rid, api::orderman::Cancel const &c) {
|
||||
LOG(OrderMan, Info, "Received order cancel from client: {}",
|
||||
utils::pb::to_string(c));
|
||||
|
||||
INFO_FN("cancel_order", "rid: {}; req: {}", rid, utils::pb::to_string(c));
|
||||
me->service_call_marshaller.schedule_action([this, c, rid]() {
|
||||
auto ok = OrderKey(c.order_key());
|
||||
if (is_ready()) {
|
||||
@@ -279,9 +297,7 @@ void ServiceBridge::cancel_order(uint32_t rid, api::orderman::Cancel const &c) {
|
||||
}
|
||||
|
||||
void ServiceBridge::detach_order(uint32_t rid, api::orderman::Detach const &c) {
|
||||
LOG(OrderMan, Info, "Received order detach from client: {}",
|
||||
utils::pb::to_string(c));
|
||||
|
||||
INFO_FN("detach_order", "rid: {}; req: {}", rid, utils::pb::to_string(c));
|
||||
me->service_call_marshaller.schedule_action([this, c, rid]() {
|
||||
auto ok = OrderKey(c.order_key());
|
||||
if (is_ready()) {
|
||||
@@ -300,15 +316,13 @@ void ServiceBridge::detach_order(uint32_t rid, api::orderman::Detach const &c) {
|
||||
|
||||
void ServiceBridge::manually_bust_all_fills(
|
||||
const api::orderman::ManualBustParameters &c) {
|
||||
LOG(OrderMan, Info, "ServiceBridge::manually_bust_all_fills: {}",
|
||||
utils::pb::to_string(c));
|
||||
INFO_FN("manually_bust_all_fills", "req: {}", utils::pb::to_string(c));
|
||||
me->service_call_marshaller.schedule_action(
|
||||
[this, c]() { publish(me->manually_bust_fills_pub, c.account()); });
|
||||
}
|
||||
|
||||
void ServiceBridge::send_rfq(api::orderman::RFQRequest const &rfq) {
|
||||
LOG(OrderMan, Info, "ServiceBridge::send_rfq rfq: {}",
|
||||
utils::pb::to_string(rfq));
|
||||
INFO_FN("send_rfq", "req: {}", utils::pb::to_string(rfq));
|
||||
me->service_call_marshaller.schedule_action(
|
||||
[this, rfq]() { publish(me->rfq_pub, rfq); });
|
||||
}
|
||||
@@ -398,6 +412,11 @@ std::optional<FilledPositionEvent> make_pos_event(
|
||||
|
||||
void ServiceBridge::update_position(
|
||||
api::position::Position_dataUpdate const &pos) {
|
||||
INFO_FN(
|
||||
"update_position",
|
||||
"n_added: {}; n_changed: {}; n_removed: {}; n_total: {}",
|
||||
pos.added().size(), pos.changed().size(), pos.removed().size(),
|
||||
pos.all().size());
|
||||
std::vector<FilledPositionEvent> evs;
|
||||
for (auto const &pu : pos.added()) {
|
||||
auto ev = make_pos_event(*pu, me->sec_db);
|
||||
@@ -430,6 +449,9 @@ bool ServiceBridge::is_ready() const {
|
||||
}
|
||||
|
||||
void ServiceBridge::maybe_signal_ready(bool old_ready) {
|
||||
INFO_FN(
|
||||
"maybe_signal_ready", "old_ready: {}; is_ready: {}", old_ready,
|
||||
is_ready());
|
||||
if (!old_ready && is_ready())
|
||||
me->on_ready();
|
||||
}
|
||||
|
||||
@@ -40,6 +40,13 @@
|
||||
namespace bts {
|
||||
namespace orderman {
|
||||
|
||||
#define _LOG_FN(level, fn, ...) \
|
||||
LOG(OrderMan, level, "OrderManager::" fn ". " __VA_ARGS__)
|
||||
#define DIAG_FN(fn, ...) _LOG_FN(Diag, fn, __VA_ARGS__)
|
||||
#define INFO_FN(fn, ...) _LOG_FN(Info, fn, __VA_ARGS__)
|
||||
#define WARN_FN(fn, ...) _LOG_FN(Warn, fn, __VA_ARGS__)
|
||||
#define ERROR_FN(fn, ...) _LOG_FN(Error, fn, __VA_ARGS__)
|
||||
|
||||
using namespace mercury;
|
||||
|
||||
enum class OrderManager::State {
|
||||
@@ -80,6 +87,23 @@ struct OrderManager::PImpl {
|
||||
mercury::Stream<OrderManagerReadyEvent> pub_ready;
|
||||
std::thread background_thread;
|
||||
std::atomic<bool> shutdown = false;
|
||||
|
||||
constexpr std::string state_name() {
|
||||
switch (state) {
|
||||
case State::WAIT_ADMIN:
|
||||
return "WAIT_ADMIN";
|
||||
case State::WAIT_SEC:
|
||||
return "WAIT_SEC";
|
||||
case State::WAIT_FILLED_POSITION:
|
||||
return "WAIT_FILLED_POSITION";
|
||||
case State::WAIT_ORDERS:
|
||||
return "WAIT_ORDERS";
|
||||
case State::READY:
|
||||
return "READY";
|
||||
default:
|
||||
return "UNKNOWN";
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
OrderManager::OrderManager(
|
||||
@@ -108,6 +132,7 @@ OrderManager::~OrderManager() {
|
||||
}
|
||||
|
||||
void OrderManager::init() {
|
||||
INFO_FN("init", "state: {}", me->state_name());
|
||||
subscribe<NewAdminDataEvent>(
|
||||
[this](auto const &admin_update) { this->on_admin_data(admin_update); });
|
||||
subscribe<AccountPnLUpdateEvent>(
|
||||
@@ -119,300 +144,365 @@ void OrderManager::init() {
|
||||
}
|
||||
|
||||
void OrderManager::on_admin_data(const mercury::Update<NewAdminDataEvent> &m) {
|
||||
if (m.events) {
|
||||
if (me->state == State::WAIT_ADMIN) {
|
||||
LOG(OrderMan, Info,
|
||||
"Order manager received admin data. Subscribing to security list "
|
||||
"now ");
|
||||
for (auto &osd : me->services_data.sessions)
|
||||
osd->gw->on_admin_data(m);
|
||||
if (!m.events) {
|
||||
WARN_FN("on_admin_data", "no event data. state: {}", me->state_name());
|
||||
return;
|
||||
}
|
||||
INFO_FN("on_admin_data", "state: {}", me->state_name());
|
||||
if (me->state == State::WAIT_ADMIN) {
|
||||
for (auto &osd : me->services_data.sessions)
|
||||
osd->gw->on_admin_data(m);
|
||||
|
||||
me->state = State::WAIT_SEC;
|
||||
subscribe<NewSecuritiesEvent>([this](auto const &sec_update) {
|
||||
this->on_securities(sec_update);
|
||||
});
|
||||
}
|
||||
me->state = State::WAIT_SEC;
|
||||
INFO_FN("on_admin_data", "state: {}", me->state_name());
|
||||
subscribe<NewSecuritiesEvent>(
|
||||
[this](auto const &sec_update) { this->on_securities(sec_update); });
|
||||
}
|
||||
|
||||
for (auto ev : *m.events) {
|
||||
me->services_data.order_router.update(*ev.admin_data);
|
||||
me->services_data.limiter.update(*ev.admin_data);
|
||||
}
|
||||
for (auto ev : *m.events) {
|
||||
me->services_data.order_router.update(*ev.admin_data);
|
||||
me->services_data.limiter.update(*ev.admin_data);
|
||||
}
|
||||
}
|
||||
|
||||
void OrderManager::on_pnl_update(
|
||||
const mercury::Update<AccountPnLUpdateEvent> &u) {
|
||||
if (u.events)
|
||||
for (auto ev : *u.events)
|
||||
me->services_data.limiter.set_day_pnl(ev.account_id, ev.day_pnl);
|
||||
if (!u.events) {
|
||||
WARN_FN("on_pnl_update", "no event data. state: {}", me->state_name());
|
||||
return;
|
||||
}
|
||||
// This log is too noisy. It fires regularly on an interval (sometimes when
|
||||
// the pnl hasn't actually changed).
|
||||
// INFO_FN(
|
||||
// "on_pnl_update", "state: {}; n_events: {}", me->state_name(),
|
||||
// u.events->size());
|
||||
for (auto ev : *u.events) {
|
||||
me->services_data.limiter.set_day_pnl(ev.account_id, ev.day_pnl);
|
||||
}
|
||||
}
|
||||
|
||||
void OrderManager::on_excess_liquidity_update(
|
||||
const mercury::Update<ExcessLiquidityUpdateEvent> &u) {
|
||||
if (u.events)
|
||||
for (auto ev : *u.events)
|
||||
me->services_data.limiter.set_excess_liquidity(
|
||||
ev.account_id, ev.excess_liquidity);
|
||||
if (!u.events) {
|
||||
WARN_FN(
|
||||
"on_excess_liquidity_update", "no event data. state: {}",
|
||||
me->state_name());
|
||||
return;
|
||||
}
|
||||
INFO_FN(
|
||||
"on_excess_liquidity_update", "state: {}; n_events: {}",
|
||||
me->state_name(), u.events->size());
|
||||
for (auto ev : *u.events) {
|
||||
me->services_data.limiter.set_excess_liquidity(
|
||||
ev.account_id, ev.excess_liquidity);
|
||||
}
|
||||
}
|
||||
|
||||
void OrderManager::on_rfq_send(const mercury::Update<RFQEvent> &rfq) {
|
||||
if (rfq.events) {
|
||||
std::vector<OrderMsg> msgs = {};
|
||||
for (auto ev : *rfq.events) {
|
||||
RFQMsg rfq_msg = RFQMsg(ev);
|
||||
msgs.emplace_back(rfq_msg);
|
||||
}
|
||||
trace::CorID cid = {};
|
||||
LOG(OrderMan, Info, "OrderManager::on_rfq_send n_events: {}",
|
||||
rfq.events->size());
|
||||
for (auto &osd : me->services_data.sessions)
|
||||
osd->gw->send_msgs(msgs, cid);
|
||||
if (!rfq.events) {
|
||||
WARN_FN("on_rfq_send", "no event data. state: {}", me->state_name());
|
||||
return;
|
||||
}
|
||||
INFO_FN(
|
||||
"on_rfq_send", "state: {}; n_events: {}", me->state_name(),
|
||||
rfq.events->size());
|
||||
std::vector<OrderMsg> msgs = {};
|
||||
for (auto ev : *rfq.events) {
|
||||
RFQMsg rfq_msg = RFQMsg(ev);
|
||||
msgs.emplace_back(rfq_msg);
|
||||
}
|
||||
trace::CorID cid = {};
|
||||
for (auto &osd : me->services_data.sessions) {
|
||||
osd->gw->send_msgs(msgs, cid);
|
||||
}
|
||||
}
|
||||
|
||||
void OrderManager::on_securities(
|
||||
const mercury::Update<NewSecuritiesEvent> &update) {
|
||||
|
||||
if (!update.events) {
|
||||
WARN_FN("on_securities", "no event data. state: {}", me->state_name());
|
||||
return;
|
||||
}
|
||||
INFO_FN(
|
||||
"on_securities", "state: {}; n_events: {}", me->state_name(),
|
||||
update.events->size());
|
||||
// forward to execution gateways
|
||||
for (auto &osd : me->services_data.sessions)
|
||||
osd->gw->on_securities(update);
|
||||
|
||||
if (update.events) {
|
||||
// update our security db and accumulate for the sake of the limiter
|
||||
std::vector<api::Security const *> secs;
|
||||
for (auto const &ev : *(update.events)) {
|
||||
auto sec_ptr = ev.security;
|
||||
secs.push_back(sec_ptr);
|
||||
}
|
||||
me->services_data.limiter.add_securities(secs);
|
||||
|
||||
// update our security db and accumulate for the sake of the limiter
|
||||
std::vector<api::Security const *> secs;
|
||||
for (auto const &ev : *(update.events)) {
|
||||
auto sec_ptr = ev.security;
|
||||
secs.push_back(sec_ptr);
|
||||
}
|
||||
me->services_data.limiter.add_securities(secs);
|
||||
|
||||
if (me->state == State::WAIT_SEC) {
|
||||
LOG(OrderMan, Info,
|
||||
"Order manager received initial set of {} "
|
||||
"securities. Waiting for the filled position now",
|
||||
update.events->size());
|
||||
me->state = State::WAIT_FILLED_POSITION;
|
||||
me->filled_position_sub = subscribe<FilledPositionEvent>(
|
||||
[this](auto const &pos) { this->on_filled_position(pos); });
|
||||
}
|
||||
if (me->state == State::WAIT_SEC) {
|
||||
me->state = State::WAIT_FILLED_POSITION;
|
||||
INFO_FN(
|
||||
"on_securities", "received initial set of {} securities. state: {}",
|
||||
update.events->size(), me->state_name());
|
||||
me->filled_position_sub = subscribe<FilledPositionEvent>(
|
||||
[this](auto const &pos) { this->on_filled_position(pos); });
|
||||
}
|
||||
}
|
||||
|
||||
void OrderManager::on_filled_position(
|
||||
const mercury::Update<FilledPositionEvent> &update) {
|
||||
if (update.events) {
|
||||
if (me->state == State::WAIT_FILLED_POSITION) {
|
||||
LOG(OrderMan, Info,
|
||||
"Order manager received initial set of {} "
|
||||
"stored filled position records. Waiting for stored orders now",
|
||||
update.events->size());
|
||||
me->state = State::WAIT_ORDERS;
|
||||
me->stored_order_sub = subscribe<StoredOrderEvent>(
|
||||
[this](auto const &orders) { this->on_order_recovery(orders); });
|
||||
}
|
||||
if (!update.events) {
|
||||
WARN_FN(
|
||||
"on_filled_position", "no event data. state: {}", me->state_name());
|
||||
return;
|
||||
}
|
||||
INFO_FN(
|
||||
"on_filled_position", "state: {}; n_events: {}", me->state_name(),
|
||||
update.events->size());
|
||||
if (me->state == State::WAIT_FILLED_POSITION) {
|
||||
me->state = State::WAIT_ORDERS;
|
||||
INFO_FN("on_filled_position", "new_state: {}", me->state_name());
|
||||
me->stored_order_sub = subscribe<StoredOrderEvent>(
|
||||
[this](auto const &orders) { this->on_order_recovery(orders); });
|
||||
}
|
||||
|
||||
for (auto const &ev : *(update.events)) {
|
||||
me->services_data.position.update(ev);
|
||||
me->services_data.limiter.set_filled_position(
|
||||
*ev.security, ev.account_id, ev.long_quantity, ev.short_quantity);
|
||||
}
|
||||
for (auto const &ev : *(update.events)) {
|
||||
me->services_data.position.update(ev);
|
||||
me->services_data.limiter.set_filled_position(
|
||||
*ev.security, ev.account_id, ev.long_quantity, ev.short_quantity);
|
||||
}
|
||||
}
|
||||
|
||||
void OrderManager::on_order_recovery(
|
||||
const mercury::Update<StoredOrderEvent> &update) {
|
||||
if (update.events)
|
||||
if (me->state == State::WAIT_ORDERS) {
|
||||
if (!update.events) {
|
||||
WARN_FN(
|
||||
"on_order_recovery", "no event data. state: {}", me->state_name());
|
||||
return;
|
||||
}
|
||||
if (me->state != State::WAIT_ORDERS) {
|
||||
WARN_FN(
|
||||
"on_order_recovery",
|
||||
"Expected state WAIT_ORDERS. state: {}; n_events: {}",
|
||||
me->state_name(), update.events->size());
|
||||
return;
|
||||
}
|
||||
// Got the first batch of persisted orders which has to match the orders
|
||||
// stored on disk (since we are the only ones producing new orders)
|
||||
INFO_FN(
|
||||
"on_order_recovery", "state: {}; n_events: {}", me->state_name(),
|
||||
update.events->size());
|
||||
// Don't need to monitor persisted orders anymore, all the new orders we care
|
||||
// about will be generated by us.
|
||||
unsubscribe(me->stored_order_sub);
|
||||
|
||||
// Got the first batch of persisted orders which has to match
|
||||
// the orders stored on disk (since we are the only ones
|
||||
// producing new orders)
|
||||
LOG(OrderMan, Info,
|
||||
"Order manager received initial set of {} persisted orders.",
|
||||
update.events->size());
|
||||
init_order_services();
|
||||
|
||||
// Don't need to monitor persisted orders anymore, all the
|
||||
// new orders we care about will be generated by us.
|
||||
unsubscribe(me->stored_order_sub);
|
||||
// recover orders from disk
|
||||
for (auto const &ev : *(update.events)) {
|
||||
auto &order = me->services_data.order_factory.recover(ev.order_record);
|
||||
auto const &def = order.def();
|
||||
publish_order_def_update(OrderKey(ev.key()), def);
|
||||
order.init_order_services(me->services_data);
|
||||
|
||||
init_order_services();
|
||||
auto r = me->orders.emplace(order.key(), &order);
|
||||
bts_assert(r.second);
|
||||
auto const &ostate = ev.order_record.state();
|
||||
order.on_activate(
|
||||
Price(ostate.price()), Quantity(ostate.quantity()), *order._services);
|
||||
}
|
||||
|
||||
// recover orders from disk
|
||||
for (auto const &ev : *(update.events)) {
|
||||
auto &order =
|
||||
me->services_data.order_factory.recover(ev.order_record);
|
||||
auto const &def = order.def();
|
||||
publish_order_def_update(OrderKey(ev.key()), def);
|
||||
order.init_order_services(me->services_data);
|
||||
|
||||
auto r = me->orders.emplace(order.key(), &order);
|
||||
bts_assert(r.second);
|
||||
auto const &ostate = ev.order_record.state();
|
||||
order.on_activate(
|
||||
Price(ostate.price()), Quantity(ostate.quantity()),
|
||||
*order._services);
|
||||
}
|
||||
|
||||
// signal that we are ready
|
||||
LOG(OrderMan, Info, "Order manager is ready");
|
||||
publish(me->pub_ready);
|
||||
me->state = State::READY;
|
||||
}
|
||||
// signal that we are ready
|
||||
INFO_FN("on_order_recovery", "OrderManager is ready");
|
||||
publish(me->pub_ready);
|
||||
me->state = State::READY;
|
||||
}
|
||||
|
||||
void OrderManager::on_execution_report(
|
||||
const mercury::Update<NewExecutionReportEvent> &m) {
|
||||
if (m.events)
|
||||
for (auto const &report : *m.events)
|
||||
if (report.data->leg_security == nullptr) {
|
||||
auto k = report.data->order_key;
|
||||
auto it = me->orders.find(k);
|
||||
if (it == me->orders.end()) {
|
||||
LOG(OrderMan, Error,
|
||||
"Received an execution report for an unknown order with key "
|
||||
"{}. Order state will be reconstructed from this report but "
|
||||
"some "
|
||||
"information will be lost, including CustomerOrFirm,CTI "
|
||||
"code "
|
||||
"and "
|
||||
"parent order id",
|
||||
k.value());
|
||||
auto order =
|
||||
&me->services_data.order_factory.recreate_from_exe_report(
|
||||
*report.data);
|
||||
auto const &def = order->def();
|
||||
publish_order_def_update(OrderKey(k), def);
|
||||
order->init_order_services(me->services_data);
|
||||
auto r = me->orders.emplace(order->key(), order);
|
||||
bts_assert(r.second);
|
||||
order->on_activate(
|
||||
report.data->price, report.data->quantity,
|
||||
*order->_services);
|
||||
}
|
||||
}
|
||||
if (!m.events) {
|
||||
WARN_FN("on_execution_report", "no event data");
|
||||
return;
|
||||
}
|
||||
INFO_FN(
|
||||
"on_execution_report", "state: {}; n_events", me->state_name(),
|
||||
m.events->size());
|
||||
for (auto const &report : *m.events) {
|
||||
if (report.data->leg_security != nullptr) {
|
||||
continue;
|
||||
}
|
||||
auto k = report.data->order_key;
|
||||
auto it = me->orders.find(k);
|
||||
if (it == me->orders.end()) {
|
||||
ERROR_FN(
|
||||
"on_execution_report",
|
||||
"Received an execution report for an unknown order with key {}. "
|
||||
"Order state USED TO be reconstructed from this report but some "
|
||||
"information will be lost, including CustomerOrFirm,CTI code and "
|
||||
"parent order id but now these are IGNORED",
|
||||
k.value());
|
||||
continue;
|
||||
// auto order = &me->services_data.order_factory.recreate_from_exe_report(
|
||||
// *report.data);
|
||||
// auto const &def = order->def();
|
||||
// publish_order_def_update(OrderKey(k), def);
|
||||
// order->init_order_services(me->services_data);
|
||||
// auto r = me->orders.emplace(order->key(), order);
|
||||
// bts_assert(r.second);
|
||||
// order->on_activate(
|
||||
// report.data->price, report.data->quantity, *order->_services);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void OrderManager::on_activate_order(
|
||||
const mercury::Update<ActivateOrderEvent> &m) {
|
||||
if (m.events)
|
||||
for (auto const &ev : *m.events) {
|
||||
LOG(OrderMan, Info,
|
||||
"OrderManager::on_activate_order req_id: {} p: {} q: {}",
|
||||
ev.req_id, ev.price, ev.quantity);
|
||||
auto order = ev.order;
|
||||
auto const &def = order->def();
|
||||
publish_order_def_update(order->key(), def);
|
||||
order->init_order_services(me->services_data);
|
||||
auto r = me->orders.emplace(order->key(), order);
|
||||
bts_assert(r.second);
|
||||
if (!m.events) {
|
||||
WARN_FN("on_activate_order", "no event data");
|
||||
return;
|
||||
}
|
||||
INFO_FN(
|
||||
"on_activate_order", "state: {}; n_events: {}", me->state_name(),
|
||||
m.events->size());
|
||||
for (auto const &ev : *m.events) {
|
||||
LOG(OrderMan, Info,
|
||||
"OrderManager::on_activate_order req_id: {} p: {} q: {}", ev.req_id,
|
||||
ev.price, ev.quantity);
|
||||
auto order = ev.order;
|
||||
auto const &def = order->def();
|
||||
publish_order_def_update(order->key(), def);
|
||||
order->init_order_services(me->services_data);
|
||||
auto r = me->orders.emplace(order->key(), order);
|
||||
bts_assert(r.second);
|
||||
|
||||
auto rid = ev.req_id;
|
||||
auto k = order->key();
|
||||
auto on_response = [this, rid, k](auto const &order_op_result) {
|
||||
publish(
|
||||
me->services_data.report_order_op_result_pub, k, rid,
|
||||
order_op_result);
|
||||
};
|
||||
order->_services->activate(ev.price, ev.quantity, on_response);
|
||||
}
|
||||
auto rid = ev.req_id;
|
||||
auto k = order->key();
|
||||
auto on_response = [this, rid, k](auto const &order_op_result) {
|
||||
publish(
|
||||
me->services_data.report_order_op_result_pub, k, rid,
|
||||
order_op_result);
|
||||
};
|
||||
order->_services->activate(ev.price, ev.quantity, on_response);
|
||||
}
|
||||
}
|
||||
|
||||
void OrderManager::on_update_order(const mercury::Update<UpdateOrderEvent> &m) {
|
||||
if (m.events)
|
||||
for (auto const &ev : *m.events) {
|
||||
auto k = ev.key();
|
||||
auto it = me->orders.find(k);
|
||||
auto rid = ev.req_id();
|
||||
if (!m.events) {
|
||||
WARN_FN("on_update_order", "no event data");
|
||||
return;
|
||||
}
|
||||
INFO_FN(
|
||||
"on_update_order", "state: {}; n_events: {}", me->state_name(),
|
||||
m.events->size());
|
||||
for (auto const &ev : *m.events) {
|
||||
auto k = ev.key();
|
||||
auto it = me->orders.find(k);
|
||||
auto rid = ev.req_id();
|
||||
|
||||
auto on_order_op_result = [this, rid, k](auto const &order_op_result) {
|
||||
publish(
|
||||
me->services_data.report_order_op_result_pub, k, rid,
|
||||
order_op_result);
|
||||
};
|
||||
auto on_order_op_result = [this, rid, k](auto const &order_op_result) {
|
||||
publish(
|
||||
me->services_data.report_order_op_result_pub, k, rid,
|
||||
order_op_result);
|
||||
};
|
||||
|
||||
if (it != me->orders.end()) {
|
||||
if (ev.is_full_update())
|
||||
it->second->_services->update(
|
||||
ev.full_update(), on_order_op_result);
|
||||
else
|
||||
it->second->_services->update(
|
||||
Price(ev.price()), Quantity(ev.quantity()),
|
||||
on_order_op_result);
|
||||
if (it != me->orders.end()) {
|
||||
if (ev.is_full_update()) {
|
||||
it->second->_services->update(ev.full_update(), on_order_op_result);
|
||||
} else {
|
||||
on_order_op_result(OrderOpResult{
|
||||
api::orderman::ORDER_ERROR_UNKNOWN_ORDER, "Unknown order"});
|
||||
LOG(OrderMan, Error,
|
||||
"Received an update request for an unknown order {}. "
|
||||
"This should not happen and likely indicates a serious bug. ",
|
||||
ev.key().value());
|
||||
it->second->_services->update(
|
||||
Price(ev.price()), Quantity(ev.quantity()), on_order_op_result);
|
||||
}
|
||||
} else {
|
||||
on_order_op_result(OrderOpResult{
|
||||
api::orderman::ORDER_ERROR_UNKNOWN_ORDER, "Unknown order"});
|
||||
ERROR_FN(
|
||||
"on_update_order",
|
||||
"order not found (id={}). This likely indicates a serious bug.",
|
||||
ev.key().value());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void OrderManager::on_cancel_order(const mercury::Update<CancelOrderEvent> &m) {
|
||||
if (m.events)
|
||||
for (auto const &ev : *m.events) {
|
||||
auto k = ev.key();
|
||||
auto it = me->orders.find(k);
|
||||
auto rid = ev.req_id;
|
||||
if (!m.events) {
|
||||
WARN_FN("on_cancel_order", "no event data");
|
||||
return;
|
||||
}
|
||||
INFO_FN(
|
||||
"on_cancel_order", "state: {}; n_events: {}", me->state_name(),
|
||||
m.events->size());
|
||||
for (auto const &ev : *m.events) {
|
||||
auto k = ev.key();
|
||||
auto it = me->orders.find(k);
|
||||
auto rid = ev.req_id;
|
||||
|
||||
auto on_order_op_result = [this, rid, k](auto const &order_op_result) {
|
||||
publish(
|
||||
me->services_data.report_order_op_result_pub, k, rid,
|
||||
order_op_result);
|
||||
};
|
||||
auto on_order_op_result = [this, rid, k](auto const &order_op_result) {
|
||||
publish(
|
||||
me->services_data.report_order_op_result_pub, k, rid,
|
||||
order_op_result);
|
||||
};
|
||||
|
||||
if (it != me->orders.end())
|
||||
it->second->_services->cancel(on_order_op_result);
|
||||
else {
|
||||
on_order_op_result(OrderOpResult{
|
||||
api::orderman::ORDER_ERROR_UNKNOWN_ORDER, "Unknown order"});
|
||||
LOG(OrderMan, Error,
|
||||
"Received a cancel request for an unknown order {}. "
|
||||
"This should not happen and likely indicates a serious bug. ",
|
||||
ev.key().value());
|
||||
}
|
||||
if (it != me->orders.end()) {
|
||||
it->second->_services->cancel(on_order_op_result);
|
||||
} else {
|
||||
on_order_op_result(OrderOpResult{
|
||||
api::orderman::ORDER_ERROR_UNKNOWN_ORDER, "Unknown order"});
|
||||
ERROR_FN(
|
||||
"on_cancel_order",
|
||||
"order not found (id={}). This likely indicates a serious bug.",
|
||||
ev.key().value());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void OrderManager::on_detach_order(const mercury::Update<DetachOrderEvent> &m) {
|
||||
if (m.events)
|
||||
for (auto const &ev : *m.events) {
|
||||
auto k = ev.key();
|
||||
auto it = me->orders.find(k);
|
||||
auto rid = ev.req_id;
|
||||
if (!m.events) {
|
||||
WARN_FN("on_detach_order", "no event data");
|
||||
return;
|
||||
}
|
||||
INFO_FN(
|
||||
"on_detach_order", "state: {}; n_events: {}", me->state_name(),
|
||||
m.events->size());
|
||||
for (auto const &ev : *m.events) {
|
||||
auto k = ev.key();
|
||||
auto it = me->orders.find(k);
|
||||
auto rid = ev.req_id;
|
||||
|
||||
auto on_order_op_result = [this, rid, k](auto const &order_op_result) {
|
||||
publish(
|
||||
me->services_data.report_order_op_result_pub, k, rid,
|
||||
order_op_result);
|
||||
};
|
||||
auto on_order_op_result = [this, rid, k](auto const &order_op_result) {
|
||||
publish(
|
||||
me->services_data.report_order_op_result_pub, k, rid,
|
||||
order_op_result);
|
||||
};
|
||||
|
||||
if (it != me->orders.end())
|
||||
it->second->_services->detach(on_order_op_result);
|
||||
else {
|
||||
on_order_op_result(OrderOpResult{
|
||||
api::orderman::ORDER_ERROR_UNKNOWN_ORDER, "Unknown Order"});
|
||||
if (it != me->orders.end()) {
|
||||
it->second->_services->detach(on_order_op_result);
|
||||
} else {
|
||||
on_order_op_result(OrderOpResult{
|
||||
api::orderman::ORDER_ERROR_UNKNOWN_ORDER, "Unknown Order"});
|
||||
|
||||
LOG(OrderMan, Error,
|
||||
"Received a detach request for an unknown order {}. "
|
||||
"This should not happen and likely indicates a serious bug. ",
|
||||
ev.key().value());
|
||||
}
|
||||
ERROR_FN(
|
||||
"on_detach_order",
|
||||
"order not found (id={}). This likely indicates a serious bug.",
|
||||
ev.key().value());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void OrderManager::on_gateway_state(
|
||||
const mercury::Update<GatewayStatusChangeEvent> &m) {
|
||||
if (m.events)
|
||||
for (auto const &ev : *m.events) {
|
||||
LOG(OrderMan, Info,
|
||||
" OrderManager::on_gateway_state gw: {} connected: {}", ev.gw_name,
|
||||
ev.is_connected);
|
||||
auto &sr = me->services_data.get_session(ev.gw_name.value());
|
||||
sr.gw_connected = ev.is_connected;
|
||||
}
|
||||
if (!m.events) {
|
||||
WARN_FN("on_gateway_state", "no event data");
|
||||
return;
|
||||
}
|
||||
INFO_FN(
|
||||
"on_gateway_state", "state: {}; n_events: {}", me->state_name(),
|
||||
m.events->size());
|
||||
for (auto const &ev : *m.events) {
|
||||
LOG(OrderMan, Info,
|
||||
" OrderManager::on_gateway_state gw: {} connected: {}", ev.gw_name,
|
||||
ev.is_connected);
|
||||
auto &sr = me->services_data.get_session(ev.gw_name.value());
|
||||
sr.gw_connected = ev.is_connected;
|
||||
}
|
||||
}
|
||||
|
||||
void OrderManager::add_child_order(OrderServices &parent, Order &child) {
|
||||
@@ -470,9 +560,12 @@ void OrderManager::init_order_services() {
|
||||
return s->session;
|
||||
}),
|
||||
", "));
|
||||
LOG(OrderMan, Info, "init_order_services. sessions: {}", session_names);
|
||||
INFO_FN(
|
||||
"init_order_services", "state: {}; sessions: {}", me->state_name(),
|
||||
session_names);
|
||||
|
||||
for (auto &s : me->services_data.sessions) {
|
||||
INFO_FN("init_order_services", "Initializing session {}", s->session);
|
||||
auto gw_name = GatewayName(s->session);
|
||||
|
||||
// Now we are ready to subscribe to the execution gateway
|
||||
@@ -535,9 +628,11 @@ void OrderManager::init_order_services() {
|
||||
// reject evets
|
||||
auto s_ptr = s.get();
|
||||
s->exe_report_pub = make_stream<NewExecutionReportEvent>(gw_name);
|
||||
s->gw->on_exe_report() = [s_ptr, this](auto const &er, auto const &t) {
|
||||
publish(s_ptr->exe_report_pub, &er, t);
|
||||
};
|
||||
s->gw->on_exe_report() =
|
||||
[s_ptr,
|
||||
this](std::shared_ptr<const ExecutionReport> er, auto const &t) {
|
||||
publish(s_ptr->exe_report_pub, std::move(er), t);
|
||||
};
|
||||
s->gw_status_change_pub = make_stream<GatewayStatusChangeEvent>(gw_name);
|
||||
s->gw->on_gw_status_change() = [s_ptr, this, gw_name](bool b) {
|
||||
publish(s_ptr->gw_status_change_pub, gw_name, b);
|
||||
@@ -563,9 +658,10 @@ void OrderManager::init_order_services() {
|
||||
s->gw->on_cancel_reject() = [s_ptr, this](auto const &cr) {
|
||||
publish(s_ptr->cancel_reject_pub, cr);
|
||||
};
|
||||
INFO_FN("init_order_services", "Starting session {}", s->session);
|
||||
// handlers are ready so we can start processing fix events
|
||||
s->gw->start();
|
||||
|
||||
INFO_FN("init_order_services", "Started session {}", s->session);
|
||||
reload_fix_buffer(*s);
|
||||
}
|
||||
|
||||
@@ -676,9 +772,10 @@ void OrderManager::after_event() {
|
||||
if (queued_ops.size()) {
|
||||
auto &corid = osd->current_trace;
|
||||
auto now = utils::TimeStamp::now();
|
||||
LOG(Latency, Info,
|
||||
"entry_type:after_event channel:{} sn:{} app_arrival:{} now:{} "
|
||||
"delta:{}",
|
||||
INFO_FN(
|
||||
"after_event",
|
||||
"channel: {} sn: {} app_arrival: {} now: {} "
|
||||
"delta: {}",
|
||||
std::string{corid.channel}, corid.sn,
|
||||
corid.app_arrival.to_uint64(), now.to_uint64(),
|
||||
(now - corid.app_arrival).nanoseconds());
|
||||
@@ -694,9 +791,9 @@ void OrderManager::reload_fix_buffer(SessionServicesData &osd) {
|
||||
osd.buffered_fix_msgs = osd.free_buffers.back();
|
||||
osd.free_buffers.pop_back();
|
||||
} else {
|
||||
LOG(OrderMan, Warn,
|
||||
"Ran out of fix buffers in {}. Will allocate directly. "
|
||||
"This will have an impact on performance",
|
||||
WARN_FN(
|
||||
"reload_fix_buffer",
|
||||
"Allocating new fix buffers in {}. This will impact performance",
|
||||
osd.cfg.name());
|
||||
osd.buffered_fix_msgs = std::make_shared<jetfix::OutFIX>(osd.cfg);
|
||||
}
|
||||
|
||||
@@ -1,10 +1,6 @@
|
||||
#include "OrderServicesData.h"
|
||||
#include "lib/config/config.h"
|
||||
#include "lib/config/protos/jetfix.pb.h"
|
||||
#include "lib/md/SecurityOracle.h"
|
||||
#include "lib/orderman/actors/order_manager/order_gateway_definition.h"
|
||||
#include "lib/orderman/gateways/ExecutionGatewayFactory.h"
|
||||
#include "lib/os/spark_data_directory.h"
|
||||
#include "lib/tracing/PerfLog.h"
|
||||
|
||||
namespace bts {
|
||||
@@ -26,8 +22,6 @@ OrderServicesData::OrderServicesData(
|
||||
for (auto &g : gateways) {
|
||||
sessions.push_back(std::make_unique<SessionServicesData>(std::move(g)));
|
||||
}
|
||||
// spark::SparkDataDirectory data_dir;
|
||||
// auto binance_j = data_dir.load_config_json("binance_execution");
|
||||
}
|
||||
|
||||
} // namespace orderman
|
||||
|
||||
@@ -34,7 +34,6 @@
|
||||
#include "lib/orderman/events/TickerUpdateEvent.h"
|
||||
#include "lib/orderman/events/TradeSignalEvent.h"
|
||||
#include "lib/orderman/events/UpdateOrderEvent.h"
|
||||
#include "lib/orderman/gateways/ExecutionGatewayFactory.h"
|
||||
#include "lib/orderman/gateways/IExecutionGateway.h"
|
||||
#include "lib/orderman/limiter/Limiter.h"
|
||||
#include "lib/orderman/order_factory/OrderFactory.h"
|
||||
|
||||
@@ -10,7 +10,6 @@
|
||||
#include "lib/orderman/events/ExchangePositionEvent.h"
|
||||
#include "lib/orderman/events/GatewayStatusChangeEvent.h"
|
||||
#include "lib/orderman/events/NewExecutionReportEvent.h"
|
||||
#include "lib/orderman/gateways/ExecutionGatewayFactory.h"
|
||||
#include "lib/orderman/gateways/IExecutionGateway.h"
|
||||
#include "lib/utils/PBUtils.h"
|
||||
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
#include "lib/utils/bts_assert.h"
|
||||
#include <bits/stdint-uintn.h>
|
||||
#include <boost/lockfree/spsc_queue.hpp>
|
||||
#include <ccapi_cpp/bts_session_options.h>
|
||||
#include <ccapi_cpp/ccapi_element.h>
|
||||
#include <ccapi_cpp/ccapi_event.h>
|
||||
#include <ccapi_cpp/ccapi_macro.h>
|
||||
@@ -255,7 +256,8 @@ struct CCAPIDeribitOrderMux::PImpl {
|
||||
{CCAPI_DERIBIT_CLIENT_ID, d_conn_config.client_id},
|
||||
{CCAPI_DERIBIT_CLIENT_SECRET, d_conn_config.client_secret},
|
||||
});
|
||||
session = new ccapi::Session({}, session_configs, &event_handler);
|
||||
session = new ccapi::Session(
|
||||
{}, {ccapi::BtsVenues::DERIBIT}, session_configs, &event_handler);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -953,4 +955,4 @@ void CCAPIDeribitOrderMux::_send_buffered_orders(uint32_t sec_id) {
|
||||
"n_buffered_orders: {}",
|
||||
me->buffered_creates.size());
|
||||
}
|
||||
} // namespace bts::orderman
|
||||
} // namespace bts::orderman
|
||||
@@ -20,12 +20,13 @@ class NewExecutionReportEvent : public mercury::Event<NewExecutionReportEvent> {
|
||||
public:
|
||||
using Tag = GatewayName;
|
||||
using SynopsisPolicy = mercury::CacheAll<NewExecutionReportEvent>;
|
||||
ExecutionReport const *data;
|
||||
std::shared_ptr<const ExecutionReport> data;
|
||||
trace::CorID trace;
|
||||
// used by ContentSubscription
|
||||
OrderKey key() const { return data->order_key; }
|
||||
NewExecutionReportEvent(const ExecutionReport *dt, trace::CorID const &trace)
|
||||
: data(dt), trace(trace){};
|
||||
NewExecutionReportEvent(
|
||||
std::shared_ptr<const ExecutionReport> dt, trace::CorID const &trace)
|
||||
: data(std::move(dt)), trace(trace){};
|
||||
};
|
||||
|
||||
} // namespace orderman
|
||||
|
||||
@@ -40,7 +40,7 @@
|
||||
#include <bits/stdint-uintn.h>
|
||||
//#include <boost/container/small_vector.hpp>
|
||||
#include "lib/orderman/ccapi/CCAPIConnectionConfig.h"
|
||||
#include <boost/pool/object_pool.hpp>
|
||||
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <optional>
|
||||
@@ -138,7 +138,6 @@ struct CCAPIExecutionGateway::PImpl {
|
||||
OnExchangeOpenOrdersState on_exchange_open_orders;
|
||||
// orders are sent out and callbacks received via IEMOrderMuxer
|
||||
size_t msg_seq_num = 0;
|
||||
boost::object_pool<ExecutionReport> exe_report_pool;
|
||||
std::unordered_map<OrderKey, CreateOrderMsg> order_map = {};
|
||||
// this is updated as we get exchange positions
|
||||
ExchangePosition exchange_position;
|
||||
@@ -339,8 +338,7 @@ void CCAPIExecutionGateway::_on_order_cb(const CCAPIOrderState &s) {
|
||||
const auto &cr_msg = it->second;
|
||||
|
||||
// cant create one on the stack
|
||||
ExecutionReport *er_p = me->exe_report_pool.construct();
|
||||
ExecutionReport &er = *er_p;
|
||||
ExecutionReport er;
|
||||
// length of these have to be <= 12
|
||||
to_array(cr_msg.routing_record->account(), &er.account_name.value());
|
||||
to_array(cr_msg.routing_record->operator_id(), &er.operator_id.value());
|
||||
@@ -390,7 +388,7 @@ void CCAPIExecutionGateway::_on_order_cb(const CCAPIOrderState &s) {
|
||||
LOG(OrderMan, Info,
|
||||
"CCAPIExecutionGateway::_process_cb_and_fire_er firing er: {}",
|
||||
er.to_string());
|
||||
me->on_exe_report(er, {});
|
||||
me->on_exe_report(std::make_shared<ExecutionReport>(std::move(er)), {});
|
||||
// _fire_leg_er(em_cb, er);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,61 +0,0 @@
|
||||
#include "ExecutionGatewayFactory.h"
|
||||
#include "lib/domain_model/Security.pb.h"
|
||||
#include "lib/md/SecurityOracle.h"
|
||||
#include "lib/orderman/gateways/CCAPIExecutionGateway.h"
|
||||
// #include "lib/orderman/gateways/EMExecutionGateway.h"
|
||||
#include "lib/orderman/gateways/FakeExecutionGateway.h"
|
||||
#include "lib/orderman/gateways/GeneralExecutionGateway.h"
|
||||
#include "lib/orderman/gateways/GLBXExecutionGateway.h"
|
||||
#include "lib/orderman/gateways/GLBXiLink3ExecutionGateway.h"
|
||||
#include "lib/orderman/gateways/ICEExecutionGateway.h"
|
||||
#include "lib/orderman/gateways/IExecutionGenericAdapter.h"
|
||||
#include "lib/orderman/gateways/OkxExecutionAdapter.h"
|
||||
#include "lib/orderman/gateways/OnixFIXAdapter.h"
|
||||
#include "lib/orderman/gateways/sim/SimExecutionGatewayFactory.h"
|
||||
#include "lib/orderman/gateways/sim/SimExecutionGatewayUniqueWrapper.h"
|
||||
#include "lib/tracing/PerfLog.h"
|
||||
#include "lib/utils/PBUtils.h"
|
||||
#include "lib/utils/ValueWrapper.h"
|
||||
#include <memory>
|
||||
#include <variant>
|
||||
|
||||
namespace bts::orderman {
|
||||
|
||||
std::unique_ptr<IExecutionGateway> ExecutionGatewayFactory::create_gateway(
|
||||
const config::JetFIX::Connection &conn,
|
||||
trace::PerfLog &pl,
|
||||
md::SecurityOracle o) {
|
||||
LOG(OrderMan, Info, "ExecutionGatewayFactory::create_gateway conn: {}",
|
||||
utils::pb::to_string(conn));
|
||||
if (SimExecutionGatewayFactory::is_sim_execution_on(conn.venue())) {
|
||||
return std::make_unique<SimExecutionGatewayUniqueWrapper>(conn.venue());
|
||||
}
|
||||
LOG(OrderMan, Info, "ExecutionGatewayFactory::create_gateway NOT SIM conn: {}",
|
||||
utils::pb::to_string(conn));
|
||||
switch(conn.venue()){
|
||||
case bts::api::VENUE_ICE:
|
||||
case bts::api::VENUE_SIMICE:
|
||||
return std::make_unique<ICEExecutionGateway>(conn, pl, o);
|
||||
case bts::api::VENUE_GLBX:{
|
||||
if (conn.use_ilink3())
|
||||
return std::make_unique<ilink3::GLBXiLink3ExecutionGateway>( conn, pl, o);
|
||||
return std::make_unique<GLBXExecutionGateway>(conn, pl, o);
|
||||
}
|
||||
case bts::api::VENUE_DERIBIT:
|
||||
return std::make_unique<CCAPIExecutionGateway>(conn.venue(), o);
|
||||
case bts::api::VENUE_CBOE:
|
||||
{
|
||||
std::unique_ptr<IExecutionGenericAdapter> adapter = std::make_unique<OnixFIXAdapter>(o);
|
||||
return std::make_unique<GeneralExecutionGateway>(conn.venue(),std::move(adapter),o);
|
||||
}
|
||||
case bts::api::VENUE_OKX:
|
||||
{
|
||||
std::unique_ptr<IExecutionGenericAdapter> adapter = std::make_unique<OkxExecutionAdapter>(o);
|
||||
return std::make_unique<GeneralExecutionGateway>(conn.venue(),std::move(adapter),o);
|
||||
}
|
||||
default:
|
||||
return std::make_unique<FakeExecutionGateway>(o);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace bts::orderman
|
||||
@@ -1,25 +0,0 @@
|
||||
#pragma once
|
||||
#include "lib/config/protos/jetfix.pb.h"
|
||||
#include <memory>
|
||||
|
||||
namespace bts::trace {
|
||||
class PerfLog;
|
||||
}
|
||||
|
||||
namespace bts::md {
|
||||
class SecurityOracle;
|
||||
}
|
||||
|
||||
namespace bts::orderman {
|
||||
|
||||
class IExecutionGateway;
|
||||
|
||||
class ExecutionGatewayFactory {
|
||||
public:
|
||||
static std::unique_ptr<IExecutionGateway> create_gateway(
|
||||
const config::JetFIX::Connection &,
|
||||
trace::PerfLog &,
|
||||
md::SecurityOracle);
|
||||
};
|
||||
|
||||
} // namespace bts::orderman
|
||||
@@ -25,7 +25,6 @@
|
||||
#include "server/sparkd/md/platforms/emumba/IEMOrderMuxer.h"
|
||||
|
||||
#include <atomic>
|
||||
#include <boost/pool/object_pool.hpp>
|
||||
#include <cstdint>
|
||||
#include <set>
|
||||
#include <string>
|
||||
@@ -54,7 +53,7 @@ struct FakeExecutionGateway::PImpl {
|
||||
md::SecurityOracle sec_oracle;
|
||||
std::vector<OrderMsg> buffered_msgs = {};
|
||||
unsigned long exchange_oid = 0;
|
||||
std::vector<ExecutionReport> exe_reports = {};
|
||||
std::vector<std::shared_ptr<ExecutionReport>> exe_reports = {};
|
||||
IEMOrderMuxer *_order_mux = nullptr;
|
||||
};
|
||||
|
||||
@@ -84,7 +83,9 @@ void FakeExecutionGateway::dispatch_incoming_fix_messages() {
|
||||
"FakeExecutionGateway::dispatch_incoming_fix_message key: {} sec: "
|
||||
"{} p: {} q: {}",
|
||||
cr_msg.key, cr_msg.security->id(), cr_msg.price, cr_msg.quantity);
|
||||
ExecutionReport er = {};
|
||||
me->exe_reports.push_back(std::make_shared<ExecutionReport>());
|
||||
std::shared_ptr<ExecutionReport> &er_p = me->exe_reports.back();
|
||||
ExecutionReport &er = *er_p;
|
||||
std::string account_name = "ACCA1";
|
||||
std::array<char, 12> an_array = {};
|
||||
size_t i = 0;
|
||||
@@ -115,19 +116,18 @@ void FakeExecutionGateway::dispatch_incoming_fix_messages() {
|
||||
er.msg = "Message Placed";
|
||||
er.exchange = api::VENUE_SIM;
|
||||
trace::CorID cid = {};
|
||||
me->exe_reports.push_back(er);
|
||||
const auto &er_pheap = me->exe_reports.back();
|
||||
LOG(OrderMan, Info,
|
||||
"FakeExecutionGateway::dispatch_incoming_fix_messages er: {}",
|
||||
er_pheap.to_string());
|
||||
me->on_exe_report(er_pheap, cid);
|
||||
er.to_string());
|
||||
me->on_exe_report(er_p, cid);
|
||||
} else if (std::holds_alternative<CancelOrderMsg>(m)) {
|
||||
CancelOrderMsg cxl_m = std::get<CancelOrderMsg>(m);
|
||||
for (auto &er : me->exe_reports) {
|
||||
for (std::shared_ptr<ExecutionReport> &er_p : me->exe_reports) {
|
||||
auto &er = *er_p;
|
||||
if (er.order_key == cxl_m.key) {
|
||||
er.order_status = api::orderman::NATIVE_ORDER_STATUS_CANCELED;
|
||||
er.msg = "Cancelled";
|
||||
me->on_exe_report(er, {});
|
||||
me->on_exe_report(er_p, {});
|
||||
LOG(OrderMan, Info,
|
||||
"FakeExecutionGateway::dispatch_incoming_fix_messages er: "
|
||||
"{}",
|
||||
@@ -138,13 +138,14 @@ void FakeExecutionGateway::dispatch_incoming_fix_messages() {
|
||||
} else if (std::holds_alternative<CancelReplaceOrderMsg>(m)) {
|
||||
CancelReplaceOrderMsg cxl_r_msg = std::get<CancelReplaceOrderMsg>(m);
|
||||
CreateOrderMsg cr_msg = cxl_r_msg.create_fields;
|
||||
for (auto &er : me->exe_reports) {
|
||||
for (std::shared_ptr<ExecutionReport> &er_p : me->exe_reports) {
|
||||
auto &er = *er_p;
|
||||
if (er.order_key == cr_msg.key) {
|
||||
er.order_status = api::orderman::NATIVE_ORDER_STATUS_REPLACED;
|
||||
er.msg = "Replaced";
|
||||
er.price = cr_msg.price;
|
||||
er.quantity = cr_msg.quantity;
|
||||
me->on_exe_report(er, {});
|
||||
me->on_exe_report(er_p, {});
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -196,4 +197,4 @@ OnExchangeOpenOrdersState &FakeExecutionGateway::on_exchange_open_orders() {
|
||||
}
|
||||
|
||||
} // namespace orderman
|
||||
} // namespace bts
|
||||
} // namespace bts
|
||||
Loaded 100 of 288 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user