OrderUtils.working_quantity() returns an int (not susceptible to overfill-overflow)

This commit is contained in:
John Colvin committed 2018-02-23 17:26:00 -05:00
1 parent c218f2ef23
commit e2c90bebe2
10 files changed
+64 -54

No files matched your search

@@ -13,10 +13,8 @@ using System.Linq;
using System.Reactive.Linq;
using System.Windows.Input;
namespace mbts.spark
{
partial class MarketViewVM
{
namespace mbts.spark {
partial class MarketViewVM {
public class Row : SecurityMDRow, IDisposable {
readonly IBus _bus;
readonly OrdersModel _orders_model;
@@ -28,7 +26,7 @@ namespace mbts.spark
readonly Property<ushort> _bid_size, _ask_size, _last_size;
readonly Property<ulong> _volume;
readonly Property<bool> _inside_bids, _inside_asks;
readonly DelegateProperty<uint> _working_bid_volume, _working_ask_volume;
readonly DelegateProperty<int> _working_bid_volume, _working_ask_volume;
readonly DelegateProperty<double> _working_best_bid, _working_best_ask;
ulong _bbo_ref, _last_ref;
@@ -88,8 +86,8 @@ namespace mbts.spark
public double day_change => _day_change.Value;
public double session_low => _session_low.Value;
public double session_high => _session_high.Value;
public uint working_bid_volume => _working_bid_volume.Value;
public uint working_ask_volume => _working_ask_volume.Value;
public int working_bid_volume => _working_bid_volume.Value;
public int working_ask_volume => _working_ask_volume.Value;
public string working_bids_text => OrderUtils.order_collection_summary(_working_bids, (x, y) => x.CompareTo(y), security.ContractSpec);
public string working_asks_text => OrderUtils.order_collection_summary(_working_asks, (x, y) => y.CompareTo(x), security.ContractSpec);
public double working_best_bid => _working_best_bid.Value;
@@ -63,8 +63,8 @@ namespace mbts.spark
public IDataGridColumnPersistentFilter<Row, DoubleMinMaxState> session_low_filter { get; } = new DoubleMinMax<Row>(r => r.session_low, nameof(Row.session_low));
public IDataGridColumnPersistentFilter<Row, DoubleMinMaxState> session_high_filter { get; } = new DoubleMinMax<Row>(r => r.session_high, nameof(Row.session_high));
public IDataGridColumnPersistentFilter<Row, IntMinMaxState> position_filter { get; } = new IntMinMax<Row>(r => r.position, nameof(Row.position));
public IDataGridColumnPersistentFilter<Row, UIntMinMaxState> working_bid_volume_filter { get; } = new UIntMinMax<Row>(r => r.working_bid_volume, nameof(Row.working_bid_volume));
public IDataGridColumnPersistentFilter<Row, UIntMinMaxState> working_ask_volume_filter { get; } = new UIntMinMax<Row>(r => r.working_ask_volume, nameof(Row.working_ask_volume));
public IDataGridColumnPersistentFilter<Row, IntMinMaxState> working_bid_volume_filter { get; } = new IntMinMax<Row>(r => r.working_bid_volume, nameof(Row.working_bid_volume));
public IDataGridColumnPersistentFilter<Row, IntMinMaxState> working_ask_volume_filter { get; } = new IntMinMax<Row>(r => r.working_ask_volume, nameof(Row.working_ask_volume));
public IDataGridColumnPersistentFilter<Row, ULongMinMaxState> volume_filter { get; } = new ULongMinMax<Row>(r => r.volume, nameof(Row.volume));
public IDataGridColumnPersistentFilter<Row, string> security_key_filter { get; } = new RegexFinder<Row>(r => r.security_key.ToString(), nameof(Row.security_key));
@@ -9,10 +9,8 @@ using System.Reactive.Linq;
using System.Threading.Tasks;
using static mbts.spark.DataGridColumnFilters;
namespace bts.shell
{
class MarketViewWindowFactory : ShellWindowFactory<MarketViewWindowFactory.StartupData>
{
namespace bts.shell {
class MarketViewWindowFactory : ShellWindowFactory<MarketViewWindowFactory.StartupData> {
readonly IBus _bus;
readonly OrdersModel _orders_model;
@@ -76,9 +74,9 @@ namespace bts.shell
[ProtoMember(12)]
public IntMinMaxState position;
[ProtoMember(13)]
public UIntMinMaxState working_bid_volume;
public IntMinMaxState working_bid_volume;
[ProtoMember(14)]
public UIntMinMaxState working_ask_volume;
public IntMinMaxState working_ask_volume;
[ProtoMember(15)]
public ULongMinMaxState volume;
[ProtoMember(16)]
@@ -3,32 +3,42 @@ using System.Globalization;
using System.Windows.Controls;
using System.Windows.Data;
namespace mbts.ui.converters
{
internal class IntTextConverter : IValueConverter
{
namespace mbts.ui.converters {
class IntTextConverter : IValueConverter {
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
if (!(value is int)) return new ValidationResult(false, null);
else return value.ToString();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
var text = value as string;
if (string.IsNullOrWhiteSpace(text) || !int.TryParse(text, out int result)) return new ValidationResult(false, null);
else return result;
}
}
[ValueConversion(typeof(int?), typeof(string))]
class NullableIntTextConverter : IValueConverter {
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) =>
value?.ToString();
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
var text = value as string;
if (string.IsNullOrWhiteSpace(text) || !int.TryParse(text, out int result)) return null;
else return result;
}
}
[ValueConversion(typeof(int?), typeof(int))]
class NullableIntConverter : IValueConverter {
public int DefaultValue { get; set; } = 0;
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
if (!(value is int)) return new ValidationResult(false, null);
else return value.ToString();
var ni = value as int?;
return ni.HasValue ? ni.Value : DefaultValue;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
var text = value as string;
if (string.IsNullOrWhiteSpace(text) || !int.TryParse(text, out int result)) return new ValidationResult(false, null);
else return result;
}
}
internal class NullableIntTextConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
return value?.ToString();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
var text = value as string;
if (string.IsNullOrWhiteSpace(text) || !int.TryParse(text, out int result)) return null;
else return result;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) =>
value is int ? new int?((int)value) : null;
}
}
@@ -39,9 +39,10 @@
<cv:NullableUShortTextConverter x:Key="NullableUShortTextConverter"/>
<cv:NullableULongTextConverter x:Key="NullableULongTextConverter"/>
<cv:NullableDoubleTextConverter x:Key="NullableDoubleTextConverter"/>
<cv:NullableIntConverter x:Key="NullableIntConverter"/>
<cv:NullableUIntConverter x:Key="NullableUIntConverter"/>
<cv:NullableDoubleConverter x:Key="NullableDoubleConverter"/>
<cv:NullableDoubleConverter x:Key="NullableDoubleZeroConverter" DefaultValue="0"/>
<cv:NullableUIntConverter x:Key="NullableUIntConverter"/>
<cv:ValueMap x:Key="DoubleNotZeroConverter" Default="{u:True}">
<cv:KeyValue Key="{u:Double 0.0}" Value="{u:False}" />
</cv:ValueMap>
@@ -17,8 +17,8 @@ namespace mbts.spark {
return filled(order) / (double)size;
}
public static uint working_quantity(this OrderRecordImmutable order) =>
order.State.Quantity > order.State.FilledQuantity ? order.State.Quantity - order.State.FilledQuantity : 0;
public static int working_quantity(this OrderRecordImmutable order) =>
(int)order.State.Quantity - (int)order.State.FilledQuantity;
public static string full_description(this OrderRecordImmutable order, SecurityImmutable security) {
StringBuilder sb = new StringBuilder();
@@ -68,7 +68,7 @@ namespace mbts.spark {
var best_price_volume = best_price.Sum(o => o.working_quantity());
return price_lookup.Count == 1 ?
$"{spec.format(best_price.Key)} @ {best_price_volume}" :
$"{spec.format(best_price.Key)} @ {best_price_volume} ({best_price_volume + price_lookup.Skip(1).SelectMany(g => g).Sum(o => o.working_quantity())})";
$"{spec.format(best_price.Key)} @ {best_price_volume} ({best_price_volume + price_lookup.Where(g => g.Key != best_price.Key).SelectMany(g => g).Sum(o => o.working_quantity())})";
}
}
}
@@ -57,7 +57,7 @@ namespace mbts.spark {
}
public void update(OrderRecordImmutable neworder) {
Logger.assert(neworder.Key == id, $"OrderVM.update was called with an order with a different Key({neworder.Key}) than original({id})");
Logger.assert(neworder.Key == id, $"OrderVM.update was called with an order with a different Key({neworder.Key}) from original({id})");
using (detect_property_changes())
_init(neworder);
}
@@ -70,7 +70,7 @@ namespace mbts.spark {
public string text => "";
string _buy_sell_indicator => _order.Def.Side == Side.Buy ? "B" : "S";
public string line1 => $"{_buy_sell_indicator}:{OrderUtils.filled(_order)}";
public string line2 => $"W:{_order.State.Quantity - OrderUtils.filled(_order)}";
public string line2 => $"W:{_order.working_quantity()}";
public string summary => _order.short_description(_parent.security.ContractSpec);
public string description => _order.full_description(_parent.security);
@@ -60,7 +60,7 @@
</Grid.ColumnDefinitions>
<usp:PriceEntryControl Grid.ColumnSpan="2" ContractSpec="{Binding security.ContractSpec}" Value="{Binding price}" TickDefaultValue="{Binding working_price, Converter={StaticResource NullableDoubleZeroConverter}}"/>
<TextBlock Grid.Column="1" Text="{Binding working_price, Converter={StaticResource NullableDoubleTextConverter}}" Style="{StaticResource TextPromptStyle}"/>
<c:TickUpDownNullableUInt Grid.Row="2" Grid.ColumnSpan="2" Value="{Binding size_calculator.value}" TickDefaultValue="{Binding working_size, Converter={StaticResource NullableUIntConverter}}"/>
<c:TickUpDownNullableUInt Grid.Row="2" Grid.ColumnSpan="2" Value="{Binding size_calculator.value}" TickDefaultValue="{Binding working_size, Converter={StaticResource NullableIntConverter}}"/>
<TextBlock Grid.Row="2" Grid.Column="1" Text="{Binding working_size, Converter={StaticResource NullableUIntTextConverter}}" Style="{StaticResource TextPromptStyle}"/>
</Grid>
<Grid Grid.Column="2">
@@ -101,7 +101,7 @@ namespace mbts.spark
public double price { get; set; } = double.NaN;
public double? working_price { get; set; }
public CalculatorLogic size_calculator { get; }
public uint? working_size { get; set; }
public int? working_size { get; set; }
public IEnumerable<TimeInForce> time_in_forces => EnumHelper.get_values<TimeInForce>();
public TimeInForce selected_time_in_force { get => _def.TimeInForce; set => _def = _def.with(time_in_force: value); }
public string[] accounts { get; }
@@ -135,9 +135,10 @@ namespace mbts.spark
public readonly OrderDefinitionImmutable def;
public readonly uint key;
public readonly double? price, wprice;
public readonly uint? size, wsize;
public readonly uint? size;
public readonly int? wsize;
public OrderParams(SecurityMD smd, OrderDefinitionImmutable def, uint key=0, double? price=null, double? wprice=null, uint? size=null, uint? wsize=null) {
public OrderParams(SecurityMD smd, OrderDefinitionImmutable def, uint key=0, double? price=null, double? wprice=null, uint? size=null, int? wsize=null) {
this.smd = smd;
this.def = def;
this.key = key;
@@ -277,8 +277,8 @@ namespace mbts.spark
if (lookup.success) {
var o = lookup.result;
if (o.State.Price == price) return true;
uint wq = o.working_quantity();
if (wq != 0) return await orders_model.update_order(order, price, wq);
int wq = o.working_quantity();
if (wq > 0) return await orders_model.update_order(order, price, (uint)wq);
}
return false;
}
@@ -287,9 +287,11 @@ namespace mbts.spark
var lookup = await order_from_key(orders_model, order);
if (lookup.success) {
var o = lookup.result;
uint wq = o.working_quantity();
if (wq == size) return true;
return await orders_model.update_order(order, o.State.Price, wq);
int wq = o.working_quantity();
if (wq > 0) {
if (wq == size) return true;
return await orders_model.update_order(order, o.State.Price, (uint)wq);
}
}
return false;
}