ladder - corner cases for one-sided/empty markets
This commit is contained in:
1 parent
13cf14d859
commit
d19c6894b6
6 files changed
+129
-79
No files matched your search
@@ -18,7 +18,8 @@ namespace mbts.spark {
|
||||
new { tick_unit = 0.25, tick_factors = new TickFactors(100, 25), jump_quantities = new[] { 1, 2, 4, 8, 20, 40 } },
|
||||
new { tick_unit = 0.0025, tick_factors = new TickFactors(10000, 25), jump_quantities = new[] { 1, 2, 4, 20, 40, 200 } },
|
||||
new { tick_unit = 0.0050, tick_factors = new TickFactors(10000, 50), jump_quantities = new[] { 1, 2, 10, 20, 100, 500 } },
|
||||
new { tick_unit = 0.015625, tick_factors = new TickFactors(64, 1), jump_quantities = new[] { 1, 4, 8, 16, 32, 64 } }
|
||||
new { tick_unit = 0.015625, tick_factors = new TickFactors(64, 1), jump_quantities = new[] { 1, 4, 8, 16, 32, 64 } },
|
||||
new { tick_unit = 0.0078125, tick_factors = new TickFactors(640, 5), jump_quantities = new[] { 1, 5, 10, 20, 100, 320 } }
|
||||
}.ToDictionary(obj => obj.tick_unit, obj => (obj.tick_factors, obj.jump_quantities)));
|
||||
#endregion
|
||||
|
||||
@@ -63,11 +64,11 @@ namespace mbts.spark {
|
||||
return mid;
|
||||
}
|
||||
|
||||
public static double atm_tick(ContractSpecificationImmutable spec, double bid, double ask, ATMLine atm_line) {
|
||||
public static double atm_tick(ContractSpecificationImmutable spec, double bid, double ask, MarketBreakType atm_line) {
|
||||
switch (atm_line) {
|
||||
case ATMLine.Bid: return double.IsNaN(bid) ? ask : bid;
|
||||
case ATMLine.Ask: return double.IsNaN(ask) ? bid : ask;
|
||||
case ATMLine.Mid:
|
||||
case MarketBreakType.Bid: return double.IsNaN(bid) ? ask : bid;
|
||||
case MarketBreakType.Ask: return double.IsNaN(ask) ? bid : ask;
|
||||
case MarketBreakType.Mid:
|
||||
default: return mid_tick(spec, bid, ask);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ namespace mbts.spark {
|
||||
bool _price_dirty = false;
|
||||
double[] _price_list = null;
|
||||
IComparer<double> _price_comparer;
|
||||
double _last_mid;
|
||||
double _last_market_break;
|
||||
NativePriceSize _last_trade;
|
||||
|
||||
public LadderGridHost() {
|
||||
@@ -163,8 +163,10 @@ namespace mbts.spark {
|
||||
else return row <= price_list.Length ? _size_from_price(price_list[row], depth, spec) : null;
|
||||
}
|
||||
|
||||
static ushort? _size_from_price(double price, NativePriceSize[] depth, ContractSpecificationImmutable spec) =>
|
||||
depth.FirstOrDefault(nps => spec.tick_equals(price, nps.price), nps => new ushort?(nps.size));
|
||||
static ushort? _size_from_price(double price, NativePriceSize[] depth, ContractSpecificationImmutable spec) {
|
||||
if (double.IsNaN(price)) return null;
|
||||
else return depth.FirstOrDefault(nps => spec.tick_equals(price, nps.price), nps => nps.size);
|
||||
}
|
||||
|
||||
int _row_for_point(Point point) {
|
||||
var row_offset = (_scrollViewer.ContentVerticalOffset / _settings.row_height);
|
||||
@@ -216,7 +218,7 @@ namespace mbts.spark {
|
||||
_render_market_col(column, _bids);
|
||||
break;
|
||||
case ColumnType.Price:
|
||||
LadderRender.render_prices(column.visual, _last_mid, _row_count, _settings.row_height, _price_list, column.settings, security.ContractSpec);
|
||||
LadderRender.render_prices(column.visual, _row_count, _settings.row_height, _price_list, column.settings, security.ContractSpec);
|
||||
break;
|
||||
case ColumnType.Ask:
|
||||
_render_market_col(column, _asks);
|
||||
@@ -260,7 +262,7 @@ namespace mbts.spark {
|
||||
if (_scrollViewer != null && _settings != null) {
|
||||
double target_position = _row_count * _settings.row_height / 2 - (ActualHeight / 2);
|
||||
if (_price_list != null) {
|
||||
var mid = TickUtils.atm_tick(security.ContractSpec, _bids[0].price, _asks[0].price, _settings.atm_line);
|
||||
var mid = TickUtils.atm_tick(security.ContractSpec, _bids[0].price, _asks[0].price, _settings.market_break_type);
|
||||
var orig_mid = _price_list[_price_list.Length / 2];
|
||||
var drift = _settings.row_height * TickUtils.ticks_between(mid, orig_mid, _vm.security.ContractSpec);
|
||||
target_position += drift;
|
||||
@@ -283,34 +285,59 @@ namespace mbts.spark {
|
||||
|
||||
void _tick() {
|
||||
if (_settings != null) {
|
||||
if (_vm.md.get_depth(ref _prices_updated, ref _bids, ref _asks)) {
|
||||
var mid = TickUtils.atm_tick(security.ContractSpec, _bids[0].price, _asks[0].price, _settings.atm_line);
|
||||
if (_price_dirty || _prices_out_of_range()) {
|
||||
_price_list = LadderRender.render_prices(_pricevis, mid, _row_count, _settings.row_height, _settings.price_column, security.ContractSpec);
|
||||
_ordershost.update_settings(_price_list, _price_comparer, _settings.row_height, _settings.orders_column);
|
||||
LadderRender.render_grid(_gridvis, _price_list, _row_count, _settings.row_height, _settings.grid_line_color);
|
||||
}
|
||||
Logger.assert_d(_price_list != null);
|
||||
var midIndex = LadderRender.index_of(_price_list, mid, _price_comparer);
|
||||
LadderRender.render_sizes(_bidvis, midIndex, _price_list, _bids, _settings.row_height, security.ContractSpec, _settings.bid_column);
|
||||
LadderRender.render_sizes(_askvis, midIndex, _price_list, _asks, _settings.row_height, security.ContractSpec, _settings.ask_column);
|
||||
if (mid != _last_mid) {
|
||||
_last_mid = mid;
|
||||
LadderRender.render_mid(_midvis, midIndex, _settings.row_height);
|
||||
}
|
||||
ulong unused = 0;
|
||||
_vm.md.get_last_trade(ref unused, out NativePriceSize last, out DateTime _, out ulong __);
|
||||
if (_price_dirty || _last_trade != last) {
|
||||
_last_trade = last;
|
||||
var last_index = LadderRender.index_of(_price_list, last.price, _price_comparer);
|
||||
if (last_index > 0) {
|
||||
LadderRender.render_last(_lastvis, last, last_index, _settings.row_height, _settings.last_column);
|
||||
}
|
||||
}
|
||||
_price_dirty = false;
|
||||
ulong unused = 0;
|
||||
_vm.md.get_last_trade(ref unused, out NativePriceSize last, out DateTime _, out ulong __);
|
||||
double market_break = _get_market_break();
|
||||
bool depth_changed = _vm.md.get_depth(ref _prices_updated, ref _bids, ref _asks);
|
||||
bool update_prices = _price_dirty || _prices_out_of_range();
|
||||
if (update_prices) {
|
||||
_price_list = LadderRender.render_prices(_pricevis, market_break, _row_count, _settings.row_height, _settings.price_column, security.ContractSpec);
|
||||
_ordershost.update_settings(_price_list, _price_comparer, _settings.row_height, _settings.orders_column);
|
||||
LadderRender.render_grid(_gridvis, _price_list, _row_count, _settings.row_height, _settings.grid_line_color);
|
||||
}
|
||||
int market_break_index = LadderRender.index_of(_price_list, market_break, _price_comparer);
|
||||
if (update_prices || market_break != _last_market_break) {
|
||||
_last_market_break = market_break;
|
||||
LadderRender.render_mid(_midvis, market_break_index, _settings.row_height);
|
||||
}
|
||||
if (update_prices || _last_trade != last) {
|
||||
_last_trade = last;
|
||||
var last_index = LadderRender.index_of(_price_list, last.price, _price_comparer);
|
||||
if (last_index > 0) {
|
||||
LadderRender.render_last(_lastvis, last, last_index, _settings.row_height, _settings.last_column);
|
||||
}
|
||||
}
|
||||
if (update_prices || depth_changed) {
|
||||
Logger.assert_d(_price_list != null);
|
||||
LadderRender.render_sizes(_bidvis, market_break_index, _price_list, _bids, _settings.row_height, security.ContractSpec, _settings.bid_column);
|
||||
LadderRender.render_sizes(_askvis, market_break_index, _price_list, _asks, _settings.row_height, security.ContractSpec, _settings.ask_column);
|
||||
}
|
||||
_price_dirty = false;
|
||||
}
|
||||
}
|
||||
|
||||
double _get_market_break() {
|
||||
double best_bid = _get_top_price(_bids);
|
||||
double best_ask = _get_top_price(_asks);
|
||||
switch (_settings.market_break_type) {
|
||||
case MarketBreakType.Bid:
|
||||
return double.IsNaN(best_bid) ?
|
||||
double.IsNaN(best_ask) ? _vm.low : security.ContractSpec.tickdown(best_ask) :
|
||||
best_bid;
|
||||
case MarketBreakType.Ask:
|
||||
return double.IsNaN(best_ask) ?
|
||||
double.IsNaN(best_bid) ? security.ContractSpec.tickdown(_vm.high) : best_bid :
|
||||
security.ContractSpec.tickdown(best_ask);
|
||||
case MarketBreakType.Mid:
|
||||
return double.IsNaN(best_bid) ?
|
||||
double.IsNaN(best_ask) ? 0.5 * (_vm.low + _vm.high) : security.ContractSpec.tickdown(best_ask) :
|
||||
double.IsNaN(best_ask) ? best_bid : TickUtils.mid_tick(security.ContractSpec, best_bid, best_ask);
|
||||
default: throw new NotImplementedException($"Unknown MarketBreakType ({_settings.market_break_type})");
|
||||
}
|
||||
}
|
||||
|
||||
double _get_top_price(NativePriceSize[] prices) =>
|
||||
prices != null && prices.Length > 0 ? prices[0].price : double.NaN;
|
||||
|
||||
void _settings_updated(LadderSettings s) {
|
||||
_settings = s;
|
||||
@@ -335,7 +362,7 @@ namespace mbts.spark {
|
||||
}
|
||||
|
||||
void _render_market_col(Column column, NativePriceSize[] depth) {
|
||||
var mid = LadderRender.index_of(_price_list, _last_mid, _price_comparer);
|
||||
var mid = LadderRender.index_of(_price_list, _last_market_break, _price_comparer);
|
||||
LadderRender.render_sizes(column.visual, mid, _price_list, depth, _settings.row_height, security.ContractSpec, column.settings);
|
||||
}
|
||||
|
||||
@@ -346,8 +373,8 @@ namespace mbts.spark {
|
||||
}
|
||||
|
||||
bool _prices_out_of_range() {
|
||||
double low = TickUtils.low_tick(_bids, _asks);
|
||||
double high = TickUtils.high_tick(_bids, _asks);
|
||||
double low = _bids == null ? double.NaN : TickUtils.low_tick(_bids, _asks);
|
||||
double high = _asks == null ? double.NaN : TickUtils.high_tick(_bids, _asks);
|
||||
return
|
||||
(!double.IsNaN(low) && _price_list[_price_list.Length - 1] > low) ||
|
||||
(!double.IsNaN(high) && _price_list[0] < high);
|
||||
|
||||
@@ -3,7 +3,7 @@ using mbts.ui;
|
||||
using ProtoBuf;
|
||||
|
||||
namespace mbts.spark {
|
||||
public enum ATMLine { Bid, Ask, Mid }
|
||||
public enum MarketBreakType { Bid, Ask, Mid }
|
||||
|
||||
[ProtoContract]
|
||||
public class LadderColumnSettings {
|
||||
@@ -49,7 +49,7 @@ namespace mbts.spark {
|
||||
return new LadderSettings {
|
||||
row_height = 20,
|
||||
grid_line_color = 0x535353,
|
||||
atm_line = ATMLine.Bid,
|
||||
market_break_type = MarketBreakType.Bid,
|
||||
orders_column = orders_column,
|
||||
bid_column = bid_column,
|
||||
ask_column = ask_column,
|
||||
@@ -71,7 +71,7 @@ namespace mbts.spark {
|
||||
[ProtoMember(2)]
|
||||
public uint grid_line_color { get; set; }
|
||||
[ProtoMember(3)]
|
||||
public ATMLine atm_line { get; set; }
|
||||
public MarketBreakType market_break_type { get; set; }
|
||||
|
||||
// Column settings
|
||||
[ProtoMember(4)]
|
||||
|
||||
@@ -29,8 +29,7 @@ namespace mbts.spark {
|
||||
class LadderVM : OrderPropertiesEditorVM, IKeyListener {
|
||||
readonly IBus _bus;
|
||||
readonly LadderModel _model;
|
||||
readonly Property<string> _high, _low, _change, _open;
|
||||
readonly Property<double> _change_double;
|
||||
readonly Property<double> _high, _low, _change, _open;
|
||||
readonly Property<ulong> _volume;
|
||||
readonly Property<bool> _revert;
|
||||
LadderSettings _settings;
|
||||
@@ -47,11 +46,10 @@ namespace mbts.spark {
|
||||
: base(model.order_gateway, Observable.Return(model.security), settings) {
|
||||
_bus = bus;
|
||||
_model = model;
|
||||
_high = backing(nameof(high), "");
|
||||
_low = backing(nameof(low), "");
|
||||
_change = backing(nameof(change), "");
|
||||
_change_double = backing(nameof(change_double), 0.00);
|
||||
_open = backing(nameof(open), "");
|
||||
_high = backing(nameof(high), double.NaN);
|
||||
_low = backing(nameof(low), double.NaN);
|
||||
_change = backing(nameof(change), double.NaN);
|
||||
_open = backing(nameof(open), double.NaN);
|
||||
_volume = backing(nameof(volume), 0ul);
|
||||
_revert = backing(nameof(revert), false);
|
||||
log_categories = new[] { order_category, cancel_category, modify_category, fill_category, warn_category };
|
||||
@@ -82,11 +80,10 @@ namespace mbts.spark {
|
||||
public bool is_synthetic => _model.security.ContractCase == Security.ContractOneofCase.Synthetic;
|
||||
public LogViewerMessageCollection log_messages { get; } = new LogViewerMessageCollection();
|
||||
public LadderSettings current_settings => _settings;
|
||||
public string high => _high.Value;
|
||||
public string low => _low.Value;
|
||||
public string change => _change.Value;
|
||||
public double change_double => _change_double.Value;
|
||||
public string open => _open.Value;
|
||||
public double high => _high.Value;
|
||||
public double low => _low.Value;
|
||||
public double change => _change.Value;
|
||||
public double open => _open.Value;
|
||||
public ulong volume => _volume.Value;
|
||||
public bool revert { get => _revert.Value; set => _revert.set(value); }
|
||||
public ObservableCollection<OrderVM> orders { get; } = new ObservableCollection<OrderVM>();
|
||||
@@ -159,20 +156,19 @@ namespace mbts.spark {
|
||||
void _update_header() {
|
||||
if (_misc_time != _model.md.misc_timestamp) {
|
||||
var spec = security.ContractSpec;
|
||||
_high.set(spec.format(md.session_high));
|
||||
_low.set(spec.format(md.session_low));
|
||||
_open.set(spec.format(md.open_price));
|
||||
_high.set(md.session_high);
|
||||
_low.set(md.session_low);
|
||||
_open.set(md.open_price);
|
||||
ulong unused = 0;
|
||||
md.get_last_trade(ref unused, out NativePriceSize last, out DateTime time, out ulong volume);
|
||||
if (!double.IsNaN(last.price)) {
|
||||
_change_double.set(last.price - md.settlement_price);
|
||||
_change.set(last.price - md.settlement_price);
|
||||
} else {
|
||||
unused = 0;
|
||||
_model.md.get_bbo(ref unused, out NativePriceSize bid, out NativePriceSize ask);
|
||||
double mid = TickUtils.mid_tick(spec, bid.price, ask.price);
|
||||
_change_double.set(mid - md.settlement_price);
|
||||
_change.set(mid - md.settlement_price);
|
||||
}
|
||||
_change.set(spec.format(_change_double.Value));
|
||||
_volume.set(volume);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -104,10 +104,38 @@
|
||||
<TextBlock Grid.Column="0" Grid.Row="6" Text="Open"/>
|
||||
<TextBlock Grid.Column="0" Grid.Row="8" Text="Volume"/>
|
||||
|
||||
<TextBlock Grid.Column="2" Grid.Row="0" Text="{Binding change}" Foreground="{Binding change_double, Converter={StaticResource NumericSignBrushConverter}}" FontWeight="Bold"/>
|
||||
<TextBlock Grid.Column="2" Grid.Row="2" Text="{Binding high, Mode=OneWay}" Foreground="{mbts:BTSColor UI.default_text}" FontWeight="Bold" />
|
||||
<TextBlock Grid.Column="2" Grid.Row="4" Text="{Binding low, Mode=OneWay}" Foreground="{mbts:BTSColor UI.default_text}" FontWeight="Bold" />
|
||||
<TextBlock Grid.Column="2" Grid.Row="6" Text="{Binding open, Mode=OneWay}" FontWeight="Bold" />
|
||||
<TextBlock Grid.Column="2" Grid.Row="0" Foreground="{Binding change, Converter={StaticResource NumericSignBrushConverter}}" FontWeight="Bold">
|
||||
<TextBlock.Text>
|
||||
<MultiBinding Converter="{StaticResource TickDisplayConverter}" Mode="OneWay">
|
||||
<Binding Path="change"/>
|
||||
<Binding Path="security.ContractSpec"/>
|
||||
</MultiBinding>
|
||||
</TextBlock.Text>
|
||||
</TextBlock>
|
||||
<TextBlock Grid.Column="2" Grid.Row="2" Foreground="{mbts:BTSColor UI.default_text}" FontWeight="Bold">
|
||||
<TextBlock.Text>
|
||||
<MultiBinding Converter="{StaticResource TickDisplayConverter}" Mode="OneWay">
|
||||
<Binding Path="high"/>
|
||||
<Binding Path="security.ContractSpec"/>
|
||||
</MultiBinding>
|
||||
</TextBlock.Text>
|
||||
</TextBlock>
|
||||
<TextBlock Grid.Column="2" Grid.Row="4" Foreground="{mbts:BTSColor UI.default_text}" FontWeight="Bold">
|
||||
<TextBlock.Text>
|
||||
<MultiBinding Converter="{StaticResource TickDisplayConverter}" Mode="OneWay">
|
||||
<Binding Path="low"/>
|
||||
<Binding Path="security.ContractSpec"/>
|
||||
</MultiBinding>
|
||||
</TextBlock.Text>
|
||||
</TextBlock>
|
||||
<TextBlock Grid.Column="2" Grid.Row="6" FontWeight="Bold">
|
||||
<TextBlock.Text>
|
||||
<MultiBinding Converter="{StaticResource TickDisplayConverter}" Mode="OneWay">
|
||||
<Binding Path="open"/>
|
||||
<Binding Path="security.ContractSpec"/>
|
||||
</MultiBinding>
|
||||
</TextBlock.Text>
|
||||
</TextBlock>
|
||||
<TextBlock Grid.Column="2" Grid.Row="8" Text="{Binding volume, Mode=OneWay}" FontWeight="Bold" />
|
||||
<!--<spark:RecenterCountdown Grid.ColumnSpan="3" Grid.Row="9"
|
||||
Width="16" Height="16" Margin="6,6,6,0"
|
||||
|
||||
@@ -60,11 +60,11 @@ namespace mbts.spark {
|
||||
public static double[] render_prices(DrawingVisual price, double mid, int row_count, double row_height,
|
||||
LadderColumnSettings settings, ContractSpecificationImmutable spec) {
|
||||
var dprices = gen_prices(mid, row_count, spec);
|
||||
render_prices(price, mid, row_count, row_height, dprices, settings, spec);
|
||||
render_prices(price, row_count, row_height, dprices, settings, spec);
|
||||
return dprices;
|
||||
}
|
||||
|
||||
public static void render_prices(DrawingVisual price, double mid, int row_count, double row_height, double[] dprices, LadderColumnSettings settings, ContractSpecificationImmutable spec) {
|
||||
public static void render_prices(DrawingVisual price, int row_count, double row_height, double[] dprices, LadderColumnSettings settings, ContractSpecificationImmutable spec) {
|
||||
var foreground = new SolidColorBrush(settings.foreground.rgb_color());
|
||||
var background = new SolidColorBrush(settings.background.rgb_color());
|
||||
var fprices = format_prices(dprices, settings.font_family, settings.font_size, foreground, spec);
|
||||
@@ -102,26 +102,25 @@ namespace mbts.spark {
|
||||
|
||||
public static void render_sizes(
|
||||
DrawingVisual dv,
|
||||
int mid_index,
|
||||
int market_break_index,
|
||||
double[] prices,
|
||||
NativePriceSize[] rungs,
|
||||
double row_height,
|
||||
ContractSpecificationImmutable spec,
|
||||
LadderColumnSettings settings
|
||||
) {
|
||||
if (rungs.Length > 1) {
|
||||
double start_px = mid_index * row_height;
|
||||
//todo: cache these values {
|
||||
var background = new SolidColorBrush(settings.background.rgb_color());
|
||||
var foreground = new SolidColorBrush(settings.foreground.rgb_color());
|
||||
var font = new Typeface(settings.font_family);
|
||||
//}
|
||||
//todo: cache these values {
|
||||
var background = new SolidColorBrush(settings.background.rgb_color());
|
||||
var foreground = new SolidColorBrush(settings.foreground.rgb_color());
|
||||
var font = new Typeface(settings.font_family);
|
||||
//}
|
||||
|
||||
using (var dc = dv.RenderOpen()) {
|
||||
dc.DrawRectangle(background, null, new Rect(0, 0, settings.width, prices.Length * row_height));
|
||||
//dc.DrawEllipse(Brushes.Purple, null, new Point(settings.width / 2, start_px + (row_height/2)), 5, 5);
|
||||
using (var dc = dv.RenderOpen()) {
|
||||
dc.DrawRectangle(background, null, new Rect(0, 0, settings.width, prices.Length * row_height));
|
||||
//dc.DrawEllipse(Brushes.Purple, null, new Point(settings.width / 2, start_px + (row_height/2)), 5, 5);
|
||||
|
||||
var dest = mid_index;
|
||||
if (rungs != null && rungs.Length > 0) {
|
||||
var dest = market_break_index;
|
||||
int direction = rungs[0].price < rungs[1].price ? -1 : 1;
|
||||
for (int x = 0; x < rungs.Length; ++x) {
|
||||
if (rungs[x].is_nan) break;
|
||||
@@ -136,8 +135,7 @@ namespace mbts.spark {
|
||||
double y = (row_height * dest) + top_margin(ft, row_height);
|
||||
double left_margin = (settings.width - ft.Width) / 2;
|
||||
dc.DrawText(ft, new Point(left_margin, y));
|
||||
}
|
||||
else { break; }
|
||||
} else { break; }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user