diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 000000000..1484e76be --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,62 @@ +{ + "configurations": [ + { + "name": "Linux", + "includePath": [ + "/usr/include", + "/usr/local/include", + "${workspaceRoot}" + ], + "defines": [], + "intelliSenseMode": "clang-x64", + "browse": { + "path": [ + "/usr/include", + "/usr/local/include", + "${workspaceRoot}" + ], + "limitSymbolsToIncludedHeaders": true, + "databaseFilename": "" + } + }, + { + "name": "Win32", + "includePath": [ + "C:/Program Files (x86)/Microsoft Visual Studio/2017/Professional/VC/Tools/MSVC/14.11.25503/include/*", + "C:/Program Files (x86)/Windows Kits/10/Include/10.0.15063.0/um", + "C:/Program Files (x86)/Windows Kits/10/Include/10.0.15063.0/ucrt", + "C:/Program Files (x86)/Windows Kits/10/Include/10.0.15063.0/shared", + "C:/Program Files (x86)/Windows Kits/10/Include/10.0.15063.0/winrt", + "${workspaceRoot}/src", + "${workspaceRoot}/externals/windows/include", + "${workspaceRoot}/externals/linux/include", + "${localappdata}/lxss/rootfs/usr/include/c++/5", + "${localappdata}/lxss/rootfs/usr/include/x86_64-linux-gnu/c++/5", + "${localappdata}/lxss/rootfs/usr/include/c++/5/backward", + "${localappdata}/lxss/rootfs/usr/lib/gcc/x86_64-linux-gnu/5/include", + "${localappdata}/lxss/rootfs/usr/local/include", + "${localappdata}/lxss/rootfs/usr/lib/gcc/x86_64-linux-gnu/5/include-fixed", + "${localappdata}/lxss/rootfs/usr/include/x86_64-linux-gnu", + "${localappdata}/lxss/rootfs/usr/include" + ], + "defines": [ + "_DEBUG", + "UNICODE" + ], + "intelliSenseMode": "msvc-x64", + "browse": { + "path": [ + "C:/Program Files (x86)/Microsoft Visual Studio/2017/Professional/VC/Tools/MSVC/14.11.25503/include/*", + "C:/Program Files (x86)/Windows Kits/10/Include/10.0.15063.0/um", + "C:/Program Files (x86)/Windows Kits/10/Include/10.0.15063.0/ucrt", + "C:/Program Files (x86)/Windows Kits/10/Include/10.0.15063.0/shared", + "C:/Program Files (x86)/Windows Kits/10/Include/10.0.15063.0/winrt", + "${workspaceRoot}" + ], + "limitSymbolsToIncludedHeaders": true, + "databaseFilename": "" + } + } + ], + "version": 3 +} \ No newline at end of file diff --git a/src/client/spark/mspark/Shell/MarketModule.cs b/src/client/spark/mspark/Shell/MarketModule.cs index 3c0684ef8..88ded8c09 100644 --- a/src/client/spark/mspark/Shell/MarketModule.cs +++ b/src/client/spark/mspark/Shell/MarketModule.cs @@ -159,7 +159,7 @@ namespace mbts.spark synthetic = SyntheticDataHelper.Defaults.Security(find_unique_synthetic_name(), s); strategy = _synthetic_strategy_map.TryGetValue(s.Id, out SyntheticExecutionStrategy ses) ? ses.ExecutionStrategy.Clone() : SyntheticDataHelper.Defaults.ExecutionStrategy(s); } else { - synthetic = SyntheticDataHelper.Defaults.Security(find_unique_synthetic_name(), securities); + synthetic = SyntheticDataHelper.Defaults.Security(find_unique_synthetic_name(), securities.Ordered()); strategy = SyntheticDataHelper.Defaults.ExecutionStrategy(securities); } show_synthetic_editor(synthetic, strategy); diff --git a/src/client/spark/mspark/Shell/Securities/Synthetic/SyntheticEditorModel.cs b/src/client/spark/mspark/Shell/Securities/Synthetic/SyntheticEditorModel.cs index 6048a4751..13147fe37 100644 --- a/src/client/spark/mspark/Shell/Securities/Synthetic/SyntheticEditorModel.cs +++ b/src/client/spark/mspark/Shell/Securities/Synthetic/SyntheticEditorModel.cs @@ -19,9 +19,9 @@ namespace mbts.spark /// /// Creates a new Security if synthetic's Id is 0, otherwise modifies the already existing synthetic's execution strategy. /// - /// synthetic parameter is not a synthetic security. - /// - /// + /// The synthetic parameter is not a synthetic security. + /// The new synthetic security to create, or existing synthetic security who's strategy to modify + /// The new strategy to create, or existing strategy modify public async void save(Security synthetic, ExecutionStrategy strategy) { if (synthetic.ContractCase != Security.ContractOneofCase.Synthetic) throw new ArgumentException("Must be a synthetic security.", nameof(synthetic)); var map = await _bus.subscribe(SparkTags.SecuritiesTag).FirstAsync(); @@ -30,6 +30,7 @@ namespace mbts.spark var curr_secmap = new HashSet(map.Keys); uint id = uint.MaxValue; while (curr_secmap.Contains(id)) id--; + synthetic.Id = id; var sec_strategy = new SyntheticExecutionStrategy { Security = synthetic, ExecutionStrategy = strategy }; _bus.post(SparkTags.PersistCommandTag, new SparkBusData.PersistMessage(1, "SyntheticSecurity", String.Empty, sec_strategy.ToByteArray())); } else { diff --git a/src/client/spark/mspark/Shell/Securities/Synthetic2/ParameterRow.cs b/src/client/spark/mspark/Shell/Securities/Synthetic2/ParameterRow.cs index ec25b1f02..d5e8cde89 100644 --- a/src/client/spark/mspark/Shell/Securities/Synthetic2/ParameterRow.cs +++ b/src/client/spark/mspark/Shell/Securities/Synthetic2/ParameterRow.cs @@ -1,4 +1,6 @@ -using mbts.ui.spark; +using bts.api; +using mbts.ui.spark; +using System; using System.Collections.Generic; namespace mbts.spark @@ -10,8 +12,22 @@ namespace mbts.spark void set_value(LegSpreaderParamsTuple leg_tup, T value); } - internal static class SectionNames { public const string LegDef = "Leg", Quote = "Quote", PreHedge = "PreHedge", PostHedge = "PostHedge"; } - internal static class SubsectionNames { public const string Mute = "Mute", InsideMarketDistance = "Mute Inside Mkt", InsideMarketDistanceWithTravel = "Mute Inside Mkt & Move"; } + internal static class SectionNames { + public const string + LegDef = "Leg", + Quote = "Quote", + PreHedge = "PreHedge", + PostHedge = "PostHedge"; + } + + internal static class SubsectionNames { + public const string + Mute = "Mute", + InsideMarketDistance = "Mute Inside Mkt", + InsideMarketDistanceWithTravel = "Mute Inside Mkt & Move", + MarketForcedCross = "Outside Mkt Forced Cross", + TimeForcedCross = "Time Forced Cross"; + } internal abstract class ParameterBase : IParameter { protected ParameterBase() { } @@ -23,9 +39,12 @@ namespace mbts.spark public object create_cell(LegSpreaderParamsTuple leg_tup) { return new ParameterRowCell(this, leg_tup); } } - internal class SizeRowModel : ParameterBase { - public override uint get_value(LegSpreaderParamsTuple leg_tup) { return leg_tup.leg.Quantity; } - public override void set_value(LegSpreaderParamsTuple leg_tup, uint value) { leg_tup.leg.Quantity = value; } + internal class SizeRowModel : ParameterBase { + public override int get_value(LegSpreaderParamsTuple leg_tup) { return leg_tup.leg.Side == BuySell.Buy ? (int)leg_tup.leg.Quantity : -(int)leg_tup.leg.Quantity; } + public override void set_value(LegSpreaderParamsTuple leg_tup, int value) { + leg_tup.leg.Side = value > 0 ? BuySell.Buy : BuySell.Sell; + leg_tup.leg.Quantity = (uint)Math.Abs(value); + } public override string display_name { get; } = "Size"; public override string[] section_path { get; } = new[] { SectionNames.LegDef }; } @@ -59,14 +78,14 @@ namespace mbts.spark public override void set_value(LegSpreaderParamsTuple leg_tup, uint value) { leg_tup.leg_params.QuoteParams.QuoteMaxOrderMove.MaxOrderMoveThreshold = value; } } - internal class QuoteMuteZoneInsideTicksRoleModel : ParameterBase { + internal class QuoteMuteZoneInsideTicksRowModel : ParameterBase { public override string display_name { get; } = "Inside Ticks"; public override string[] section_path { get; } = new[] { SectionNames.Quote, SubsectionNames.Mute }; public override uint get_value(LegSpreaderParamsTuple leg_tup) { return leg_tup.leg_params.QuoteParams.QuoteMuteZone.InsideTicks; } public override void set_value(LegSpreaderParamsTuple leg_tup, uint value) { leg_tup.leg_params.QuoteParams.QuoteMuteZone.InsideTicks = value; } } - internal class QuoteMuteZoneOutsideTicksRoleModel : ParameterBase { + internal class QuoteMuteZoneOutsideTicksRowModel : ParameterBase { public override string display_name { get; } = "Outside Ticks"; public override string[] section_path { get; } = new[] { SectionNames.Quote, SubsectionNames.Mute }; public override uint get_value(LegSpreaderParamsTuple leg_tup) { return leg_tup.leg_params.QuoteParams.QuoteMuteZone.OutsideTicks; } @@ -83,8 +102,8 @@ namespace mbts.spark internal class QuoteInsideMarketDistanceWithTravelTicksRowModel : ParameterBase { public override string display_name { get; } = "Ticks"; public override string[] section_path { get; } = new[] { SectionNames.Quote, SubsectionNames.InsideMarketDistanceWithTravel }; - public override uint get_value(LegSpreaderParamsTuple leg_tup) { return leg_tup.leg_params.QuoteParams.QuoteInsideMarketDistance.TicksFromInside; } - public override void set_value(LegSpreaderParamsTuple leg_tup, uint value) { leg_tup.leg_params.QuoteParams.QuoteInsideMarketDistance.TicksFromInside = value; } + public override uint get_value(LegSpreaderParamsTuple leg_tup) { return leg_tup.leg_params.QuoteParams.QuoteInsideMarketDistanceWithTravel.TicksFromInside; } + public override void set_value(LegSpreaderParamsTuple leg_tup, uint value) { leg_tup.leg_params.QuoteParams.QuoteInsideMarketDistanceWithTravel.TicksFromInside = value; } } internal class QuoteInsideMarketDistanceWithTravelLimitRowModel : ParameterBase { @@ -92,7 +111,7 @@ namespace mbts.spark public override string[] section_path { get; } = new[] { SectionNames.Quote, SubsectionNames.InsideMarketDistanceWithTravel }; public override uint get_value(LegSpreaderParamsTuple leg_tup) { return leg_tup.leg_params.QuoteParams.QuoteInsideMarketDistanceWithTravel.Limit; } public override void set_value(LegSpreaderParamsTuple leg_tup, uint value) { leg_tup.leg_params.QuoteParams.QuoteInsideMarketDistanceWithTravel.Limit = value; } - } + } #endregion #region PreHedge @@ -110,28 +129,49 @@ namespace mbts.spark public override void set_value(LegSpreaderParamsTuple leg_tup, uint value) { leg_tup.leg_params.PreHedgeParams.QuantityBasedPaylessTicks.PaylessTicks = value; } } - internal class MaxTicksInsideMarket : ParameterBase { + internal class MaxTicksInsideMarketRowModel : ParameterBase { public override string display_name { get; } = "Max Inside Ticks"; public override string[] section_path { get; } = new[] { SectionNames.PreHedge }; public override uint get_value(LegSpreaderParamsTuple leg_tup) { return leg_tup.leg_params.PreHedgeParams.MaxPriceFromInsideMarket.HedgeTickLimit; } public override void set_value(LegSpreaderParamsTuple leg_tup, uint value) { leg_tup.leg_params.PreHedgeParams.MaxPriceFromInsideMarket.HedgeTickLimit = value; } } + + internal class SendHedgeAsMarketOrderRowModel : ParameterBase { + public override string display_name { get; } = "Send Mkt Order Hedge"; + public override string[] section_path { get; } = new[] { SectionNames.PreHedge }; + public override bool get_value(LegSpreaderParamsTuple leg_tup) { return leg_tup.leg_params.PreHedgeParams.SendHedgeAsMarketOrder.SendHedgeAsMarketOrder; } + public override void set_value(LegSpreaderParamsTuple leg_tup, bool value) { leg_tup.leg_params.PreHedgeParams.SendHedgeAsMarketOrder.SendHedgeAsMarketOrder = value; } + } #endregion #region PostHedge internal class PostHedgePriceBasedCrossTicks : ParameterBase { - public override string display_name { get; } = "Price Cross Ticks"; - public override string[] section_path { get; } = new[] { SectionNames.PostHedge }; + public override string display_name { get; } = "Max Outside Ticks"; + public override string[] section_path { get; } = new[] { SectionNames.PostHedge, SubsectionNames.MarketForcedCross }; public override uint get_value(LegSpreaderParamsTuple leg_tup) { return leg_tup.leg_params.PostHedgeParams.PriceBasedCross.TicksThreshold; } public override void set_value(LegSpreaderParamsTuple leg_tup, uint value) { leg_tup.leg_params.PostHedgeParams.PriceBasedCross.TicksThreshold = value; } } internal class PostHedgePriceBasedCrossMarketTicks : ParameterBase { - public override string display_name { get; } = "Market Cross Ticks"; - public override string[] section_path { get; } = new[] { SectionNames.PostHedge }; + public override string display_name { get; } = "Mkt Cross Ticks"; + public override string[] section_path { get; } = new[] { SectionNames.PostHedge, SubsectionNames.MarketForcedCross }; public override uint get_value(LegSpreaderParamsTuple leg_tup) { return leg_tup.leg_params.PostHedgeParams.PriceBasedCross.MarketCrossTicks; } public override void set_value(LegSpreaderParamsTuple leg_tup, uint value) { leg_tup.leg_params.PostHedgeParams.PriceBasedCross.MarketCrossTicks = value; } } + + internal class PostHedgeTimeBasedCrossTicks : ParameterBase { + public override string display_name { get; } = "Max Outside Time(ms)"; + public override string[] section_path { get; } = new[] { SectionNames.PostHedge, SubsectionNames.TimeForcedCross }; + public override uint get_value(LegSpreaderParamsTuple leg_tup) { return leg_tup.leg_params.PostHedgeParams.TimeBasedForcedCross.TimeInMsThreshold; } + public override void set_value(LegSpreaderParamsTuple leg_tup, uint value) { leg_tup.leg_params.PostHedgeParams.TimeBasedForcedCross.TimeInMsThreshold = value; } + } + + internal class PostHedgeTimeBasedCrossMarketTicks : ParameterBase { + public override string display_name { get; } = "Mkt Cross Ticks"; + public override string[] section_path { get; } = new[] { SectionNames.PostHedge, SubsectionNames.TimeForcedCross }; + public override uint get_value(LegSpreaderParamsTuple leg_tup) { return leg_tup.leg_params.PostHedgeParams.TimeBasedForcedCross.MarketCrossTicks; } + public override void set_value(LegSpreaderParamsTuple leg_tup, uint value) { leg_tup.leg_params.PostHedgeParams.TimeBasedForcedCross.MarketCrossTicks = value; } + } #endregion internal class ParameterRowCell { diff --git a/src/client/spark/mspark/Shell/Securities/Synthetic2/SyntheticEditor2.xaml b/src/client/spark/mspark/Shell/Securities/Synthetic2/SyntheticEditor2.xaml index 991e81e84..42ac9733d 100644 --- a/src/client/spark/mspark/Shell/Securities/Synthetic2/SyntheticEditor2.xaml +++ b/src/client/spark/mspark/Shell/Securities/Synthetic2/SyntheticEditor2.xaml @@ -16,6 +16,11 @@ + + + + + @@ -33,6 +38,11 @@ + + + + + @@ -147,7 +157,7 @@ HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalAlignment}" HeadersVisibility="All" - RowHeaderWidth="160" + RowHeaderWidth="180" RowStyle="{StaticResource DataGridRowStyle}" ItemsSource="{Binding ParameterRowsView, RelativeSource={RelativeSource TemplatedParent}}"> diff --git a/src/client/spark/mspark/Shell/Securities/Synthetic2/SyntheticEditor2.xaml.cs b/src/client/spark/mspark/Shell/Securities/Synthetic2/SyntheticEditor2.xaml.cs index c6d22fb80..28e595831 100644 --- a/src/client/spark/mspark/Shell/Securities/Synthetic2/SyntheticEditor2.xaml.cs +++ b/src/client/spark/mspark/Shell/Securities/Synthetic2/SyntheticEditor2.xaml.cs @@ -28,8 +28,12 @@ namespace mbts.ui.spark object create_cell(LegSpreaderParamsTuple leg_tup); } - public static readonly DependencyProperty SyntheticNameProperty = DependencyProperty.Register("SyntheticName", typeof(string), typeof(SyntheticEditor2), new PropertyMetadata(string.Empty)); + #region SyntheticName + public static readonly DependencyProperty SyntheticNameProperty = DependencyProperty.Register("SyntheticName", typeof(string), typeof(SyntheticEditor2), + new FrameworkPropertyMetadata(string.Empty, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault)); + public string SyntheticName { get => GetValue(SyntheticNameProperty) as string; set => SetValue(SyntheticNameProperty, value); } + #endregion #region LegTuples public static readonly DependencyProperty LegTuplesProperty = DependencyProperty.Register("LegTuples", typeof(IReadOnlyCollection), typeof(SyntheticEditor2), @@ -148,6 +152,7 @@ namespace mbts.ui.spark { public DataTemplate DoubleTemplate { get; set; } public DataTemplate BooleanTemplate { get; set; } + public DataTemplate IntTemplate { get; set; } public DataTemplate UIntTemplate { get; set; } public override DataTemplate SelectTemplate(object item, DependencyObject container) { @@ -157,6 +162,7 @@ namespace mbts.ui.spark Type gt = t.GenericTypeArguments[0]; if (gt == typeof(bool)) return BooleanTemplate; if (gt == typeof(double)) return DoubleTemplate; + if (gt == typeof(int)) return IntTemplate; if (gt == typeof(uint)) return UIntTemplate; } } diff --git a/src/client/spark/mspark/Shell/Securities/Synthetic2/SyntheticEditorVM2.cs b/src/client/spark/mspark/Shell/Securities/Synthetic2/SyntheticEditorVM2.cs index 0b2c4bdfd..ffdc94bb1 100644 --- a/src/client/spark/mspark/Shell/Securities/Synthetic2/SyntheticEditorVM2.cs +++ b/src/client/spark/mspark/Shell/Securities/Synthetic2/SyntheticEditorVM2.cs @@ -8,14 +8,17 @@ using System.Windows.Input; namespace mbts.spark { + /// + /// The view model designed to serve as the DataContext for the SyntheticEditor2 control + /// internal partial class SyntheticEditorVM2 { /// /// Initializes a new instance of SyntheticEditorVM2 class. The commit command will create a new Synthetic if synthetic's Id is 0, otherwise it will modify the existing synthetic's strategy. /// - /// - /// the synthetic security to create, or existing strategy modify, on commit - /// the new or existing strategy to modify + /// The application bus + /// The new synthetic security to create, or existing synthetic security who's strategy to modify, on commit + /// The new strategy to create, or existing strategy to modify, on commit public SyntheticEditorVM2(IBus bus, Security synthetic, ExecutionStrategy strategy) { _synthetic = synthetic; leg_tuples = _synthetic.Synthetic.Legs.Select(l => new LegSpreaderParamsTuple(l, strategy.LegParamsMap[l.Security.Id])).ToList(); @@ -24,18 +27,24 @@ namespace mbts.spark new PriceMultiplierRowModel(), new IsQuoteRowModel(), new MaxOrderMoveRowModel(), - new QuoteMuteZoneInsideTicksRoleModel(), - new QuoteMuteZoneOutsideTicksRoleModel(), + new QuoteMuteZoneInsideTicksRowModel(), + new QuoteMuteZoneOutsideTicksRowModel(), new QuoteInsideMarketDistanceTicksRowModel(), new QuoteInsideMarketDistanceWithTravelTicksRowModel(), new QuoteInsideMarketDistanceWithTravelLimitRowModel(), new PayupRowModel(), new PaylessRowModel(), new LeanRatioRowModel(), - new MaxTicksInsideMarket() + new MaxTicksInsideMarketRowModel(), + new SendHedgeAsMarketOrderRowModel(), + new PostHedgePriceBasedCrossTicks(), + new PostHedgePriceBasedCrossMarketTicks(), + new PostHedgeTimeBasedCrossTicks(), + new PostHedgeTimeBasedCrossMarketTicks() }; commit = new DelegateCommand(() => new SyntheticEditorModel(bus).save(_synthetic, strategy)); } + private readonly Security _synthetic; public string name { get => _synthetic.Synthetic.Name; set => _synthetic.Synthetic.Name = value; } diff --git a/src/client/spark/mspark/Shell/TimeAndSales/TimeAndSalesModel.cs b/src/client/spark/mspark/Shell/TimeAndSales/TimeAndSalesModel.cs index 938679736..e87527822 100644 --- a/src/client/spark/mspark/Shell/TimeAndSales/TimeAndSalesModel.cs +++ b/src/client/spark/mspark/Shell/TimeAndSales/TimeAndSalesModel.cs @@ -35,7 +35,7 @@ namespace mbts.spark public void set_securities(IEnumerable securities) { foreach (var r in _channels.Keys.Except(securities).ToList()) _channels.Remove(r); - foreach (var a in securities.Except(_channels.Keys).ToList()) _channels.Add(a, Co2RelayControl.instance.consumer.get_channel(a.symbol, get_type(a.ContractCase), get_exchange(a.Exchange))); + foreach (var a in securities.Except(_channels.Keys).ToList()) _channels.Add(a, Co2RelayControl.instance.consumer.get_channel(a.Symbol, get_type(a.ContractCase), get_exchange(a.Exchange))); } public void Dispose() { _work_loop.Dispose(); } diff --git a/src/client/spark/view/Controls/SecuritySelectorControl/SecurityItem.cs b/src/client/spark/view/Controls/SecuritySelectorControl/SecurityItem.cs index 385572c49..57af36734 100644 --- a/src/client/spark/view/Controls/SecuritySelectorControl/SecurityItem.cs +++ b/src/client/spark/view/Controls/SecuritySelectorControl/SecurityItem.cs @@ -35,7 +35,7 @@ namespace mbts.ui.spark public static string get_display_name(Security s) { return s.ContractCase == ContractOneofCase.Synthetic ? "{0} ({1:q/n:./m:c})".fmt(s.Synthetic.Name, s) : s.ToString("q/n:./m:c"); } private static string _get_symbol(Security s) { if (s.ContractCase == ContractOneofCase.Synthetic) return _get_symbol(s.Synthetic.Legs.First().Security); - else return s.symbol; + else return s.Symbol; } } } diff --git a/src/client/spark_api/security/DateFormatter.cs b/src/client/spark_api/security/DateFormatter.cs index 8ce5139fb..f98c0185d 100644 --- a/src/client/spark_api/security/DateFormatter.cs +++ b/src/client/spark_api/security/DateFormatter.cs @@ -55,8 +55,8 @@ namespace bts.api #region Extensions public static string SecurityName(this Security security) { switch (security.ContractCase) { - case Security.ContractOneofCase.Future: return security.Future.Symbol; - case Security.ContractOneofCase.Spread: return security.Spread.Legs.Count > 0 ? SecurityName(security.Spread.Legs[0].Security) : string.Empty; + case Security.ContractOneofCase.Future: + case Security.ContractOneofCase.Spread: return security.Symbol; case Security.ContractOneofCase.Synthetic: return security.Synthetic.Legs.Count > 0 ? SecurityName(security.Synthetic.Legs[0].Security) : string.Empty; case Security.ContractOneofCase.None: return "N/A"; default: return "Unknown"; diff --git a/src/client/spark_api/security/Security.cs b/src/client/spark_api/security/Security.cs index 5b555b97d..fdd82af16 100644 --- a/src/client/spark_api/security/Security.cs +++ b/src/client/spark_api/security/Security.cs @@ -7,7 +7,7 @@ using static bts.api.Security; using O2Security=bts.security_model.OldSecurity; namespace bts.api { - public partial class Security : IComparable, IComparable, IFormattable { + public partial class Security : IFormattable { internal Security(O2Security other) { // Logger.assert(other != null); @@ -33,8 +33,8 @@ namespace bts.api { ExpirationDay = other.future.expiration_day, ExpirationMonth = other.future.expiration_month, ExpirationYear = other.future.expiration_year, - Symbol = other.future.symbol }; + Symbol = other.future.symbol; break; case security_model.SecurityType.type.SPREAD: Spread = new SpreadContract(); @@ -54,22 +54,6 @@ namespace bts.api { } } - public string symbol { - get { - switch (ContractCase) { - case ContractOneofCase.Future: - return Future.Symbol; - case ContractOneofCase.Spread: - return Spread.Legs.First().Security.symbol; - case ContractOneofCase.Synthetic: - return Synthetic.Name; - default: - case ContractOneofCase.None: - return "Invalid"; - } - } - } - private static BuySell ofSide(security_model.Side.type side) { switch (side) { case security_model.Side.type.BUY: return BuySell.Buy; @@ -169,10 +153,6 @@ namespace bts.api { get_base_exchange_securities (leg.Security, resolve_side (side, leg.Side), leg.Quantity * mult, ref legs); } - public int CompareTo(Security other) { return SecurityComparer.Compare(this, other); } - - public int CompareTo(object obj) { return CompareTo(obj as Security); } - public static readonly Security Empty = new Security { Exchange = Venue.Bts, ContractSpec = new ContractSpecification { CashPerPoint = double.NaN, HighTick = double.NaN, LowTick = double.NaN, PriceBreak = double.NaN, PriceMultiplier = double.NaN } diff --git a/src/client/spark_api/security/SecurityComparer.cs b/src/client/spark_api/security/SecurityComparer.cs deleted file mode 100644 index 10e62265c..000000000 --- a/src/client/spark_api/security/SecurityComparer.cs +++ /dev/null @@ -1,37 +0,0 @@ -using System; -using System.Linq; -using static bts.api.Security; - -namespace bts.api -{ - public static class SecurityComparer - { - public static int Compare(Security x, Security y) { - if (x == null) return y == null ? 0 : -1; - if (y == null) return 1; - if (x.ContractCase == ContractOneofCase.Future) return y.ContractCase == ContractOneofCase.Future ? Compare(x.Future, y.Future) : -1; - if (y.ContractCase == ContractOneofCase.Future) return 1; - var xlegs = SecurityHelper.get_flat_future_legs(x).ToArray(); - var ylegs = SecurityHelper.get_flat_future_legs(y).ToArray(); - int max = Math.Min(xlegs.Length, ylegs.Length); - for(int i = 0; i < max; i++) { - int rv = Compare(xlegs[i].Item2, ylegs[i].Item2); - if (rv != 0) return rv; - } - for (int i = 0; i < max; i++) { - int rv = xlegs[i].Item3.CompareTo(ylegs[i].Item3); - if (rv != 0) return rv; - } - for (int i = 0; i < max; i++) { - int rv = xlegs[i].Item1.CompareTo(ylegs[i].Item1); - if (rv != 0) return rv; - } - return xlegs.Length.CompareTo(ylegs.Length); - } - - public static int Compare(FutureContract x, FutureContract y) { - int sym_comp = x.Symbol.CompareTo(y.Symbol); - return sym_comp == 0 ? x.RawExpDate().CompareTo(y.RawExpDate()) : sym_comp; - } - } -} \ No newline at end of file diff --git a/src/client/spark_api/security/SecurityFormatter.cs b/src/client/spark_api/security/SecurityFormatter.cs index b268c1b19..5305d17ca 100644 --- a/src/client/spark_api/security/SecurityFormatter.cs +++ b/src/client/spark_api/security/SecurityFormatter.cs @@ -24,7 +24,7 @@ namespace bts.api if (part_formatters.Count > 0 && part_formatters[0].separator == "!") { if (part_formatters[0].add_part(s, out string result)) return result; } - return string.Join("", SecurityHelper.get_flat_future_legs(s).Select(flt => _format_future_leg(flt, part_formatters))); + return string.Join("", SecurityHelper.get_flat_future_legs(s).Select(flt => _format_future_leg(flt, s.Symbol, part_formatters))); } } @@ -52,18 +52,18 @@ namespace bts.api } } - private static string _format_future(Security s, IEnumerable part_formatters) { return _format(s, part_formatters, _add_future); } - private static string _format_future_leg(FutureLeg l, IEnumerable part_formatters) { return _format(l, part_formatters, _add_future_leg); } + private static string _format_future(Security s, IEnumerable part_formatters) { return _format(s, s.Symbol, part_formatters, _add_future); } + private static string _format_future_leg(FutureLeg l, string symbol, IEnumerable part_formatters) { return _format(l, symbol, part_formatters, _add_future_leg); } - private delegate bool AddPart(T t, IPartFormatter part_formatter, out string result); - private static bool _add_future(Security s, IPartFormatter part_formatter, out string result) { return part_formatter.add_part(s, out result); } - private static bool _add_future_leg(FutureLeg l, IPartFormatter part_formatter, out string result) { return part_formatter.add_part(l, out result); } + private delegate bool AddPart(string symbol, T t, IPartFormatter part_formatter, out string result); + private static bool _add_future(string symbol, Security s, IPartFormatter part_formatter, out string result) { return part_formatter.add_part(s, out result); } + private static bool _add_future_leg(string symbol, FutureLeg l, IPartFormatter part_formatter, out string result) { return part_formatter.add_part(l, symbol, out result); } - private static string _format(T t, IEnumerable part_formatters, AddPart add_part) { + private static string _format(T t, string symbol, IEnumerable part_formatters, AddPart add_part) { var adds = new List>(); foreach (var pf in part_formatters) { string p; - if(add_part(t, pf, out p)) adds.Add(Tuple.Create(p, pf)); + if(add_part(symbol, t, pf, out p)) adds.Add(Tuple.Create(p, pf)); } string result = string.Empty; if (adds.Count > 0) { @@ -76,7 +76,7 @@ namespace bts.api private interface IPartFormatter { string separator { get; } - bool add_part(FutureLeg fl, out string result); + bool add_part(FutureLeg fl, string symbol, out string result); bool add_part(Security s, out string result); } @@ -118,7 +118,7 @@ namespace bts.api this.separator = separator; } public string separator { get; } - public bool add_part(FutureLeg fl, out string result) { + public bool add_part(FutureLeg fl, string symbol, out string result) { result = null; return false; } @@ -138,7 +138,7 @@ namespace bts.api } private bool _first_leg = true; public string separator { get; } - public bool add_part(FutureLeg fl, out string result) { + public bool add_part(FutureLeg fl, string symbol, out string result) { if (_first_leg) { if (fl.Item3 > 0) result = fl.Item3 == 1 ? string.Empty : fl.Item3.ToString(); else result = fl.Item3 == -1 ? "-" : fl.Item3.ToString("-0"); @@ -160,8 +160,8 @@ namespace bts.api this.separator = separator; } public string separator { get; } - public bool add_part(FutureLeg fl, out string result) { result = fl.Item2.Symbol; return true; } - public bool add_part(Security s, out string result) { result = s.Future.Symbol; return true; } + public bool add_part(FutureLeg fl, string symbol, out string result) { result = symbol; return true; } + public bool add_part(Security s, out string result) { result = s.Symbol; return true; } } private class Formatter : IPartFormatter { public Formatter(Func get_sec_value, Func get_leg_value, string separator) : this(get_sec_value, get_leg_value, t => t.ToString(), separator) { } @@ -188,7 +188,7 @@ namespace bts.api return false; } } - public bool add_part(FutureLeg fl, out string result) { + public bool add_part(FutureLeg fl, string symbol, out string result) { T val = _get_leg_value(fl); if (!val.Equals(_last_val)) { result = _format(_last_val = val); diff --git a/src/client/spark_core/Co2/Co2Tests.cs b/src/client/spark_core/Co2/Co2Tests.cs index a1694105b..2fedf6daa 100644 --- a/src/client/spark_core/Co2/Co2Tests.cs +++ b/src/client/spark_core/Co2/Co2Tests.cs @@ -174,7 +174,7 @@ namespace mbts.co2.tests //Assert.That(s.security.numeric_id == s.handle.numeric_id); } Assert.That(securities.Count() == 24); - var dict = securities.Select(s => s.security).GroupBy(s => s.symbol).ToDictionary(group => group.Key, group => group.ToList()); + var dict = securities.Select(s => s.security).GroupBy(s => s.Symbol).ToDictionary(group => group.Key, group => group.ToList()); Assert.That(dict.Count == 2); Assert.That(dict.All(kvp => kvp.Value.Count == 12)); } @@ -200,8 +200,8 @@ namespace mbts.co2.tests consumer.scan_securities(); System.Threading.Thread.Sleep(100); Assert.AreEqual(24, securities.Count); - Assert.That(securities.Find(s => s.symbol == "FOOM") != null); - Assert.That(securities.Find(s => s.symbol == "FOOXM") != null); + Assert.That(securities.Find(s => s.Symbol == "FOOM") != null); + Assert.That(securities.Find(s => s.Symbol == "FOOXM") != null); foreach (var sec in securities) { var handle = consumer.try_get_handle(sec.Id); diff --git a/src/client/spark_core/Co2/MDConsumer.cs b/src/client/spark_core/Co2/MDConsumer.cs index b47adfe68..444ca60fa 100644 --- a/src/client/spark_core/Co2/MDConsumer.cs +++ b/src/client/spark_core/Co2/MDConsumer.cs @@ -1,4 +1,5 @@ -using bts.security_model; +using bts.api; +using bts.security_model; using System; using System.Collections.Generic; using System.IO; @@ -34,12 +35,11 @@ namespace mbts.co2 unsafe void init() { _raw_sec_cb = (key, buf, len, handle) => { - var security = Co2NativeMethods.deserialize(buf, len); - var next = new SecurityAndHandle() { security = new bts.api.Security(security), handle = new MDConsumerHandle(handle) }; - _securities.Add(next); - var cb = _on_security; - if (cb != null) cb(new List(1) { next }); - }; + var security = Co2NativeMethods.deserialize(buf, len); + var next = new SecurityAndHandle() { security = security, handle = new MDConsumerHandle(handle) }; + _securities.Add(next); + _on_security?.Invoke(new List(1) { next }); + }; Co2NativeMethods.md_consumer_set_on_security(_consumer, _raw_sec_cb); on_security = Observable.Create>(observer => { @@ -75,7 +75,7 @@ namespace mbts.co2 public unsafe IEnumerable all_securities() { List result = new List(); Co2NativeMethods.raw_security_callback cb = (key, buf, len, handle) => { - var security = Co2NativeMethods.deserialize(buf, len); + var security = Co2NativeMethods.deserialize(buf, len); result.Add(new SecurityAndHandle() { security = new bts.api.Security(security), handle = new MDConsumerHandle(handle) }); }; Co2NativeMethods.md_consumer_walk_security_list(_consumer, cb); @@ -105,7 +105,7 @@ namespace mbts.co2 struct SecurityAndHandle { - public bts.api.Security security; + public Security security; public MDConsumerHandle handle; public override string ToString() { return security.ToString(); @@ -129,7 +129,7 @@ namespace mbts.co2 return "{0}-{1}".fmt(symbol, type); } - + public static List get_all() { List symbols = new List(); Co2NativeMethods.md_consumer_get_system_md_symbols(us => symbols.Add(us)); diff --git a/src/client/spark_core/Co2/MDConsumerHandle.cs b/src/client/spark_core/Co2/MDConsumerHandle.cs index 287404744..b93ddcbfe 100644 --- a/src/client/spark_core/Co2/MDConsumerHandle.cs +++ b/src/client/spark_core/Co2/MDConsumerHandle.cs @@ -9,6 +9,7 @@ using System.Text; using System.Threading; using System.Threading.Tasks; using security_key = System.UInt32; +using bts.api; namespace mbts.co2 { @@ -22,10 +23,10 @@ namespace mbts.co2 // The exchange key can change at CBOE. This numeric_id will always be correct public ulong numeric_id { get { return _cold->numeric_id; } } - public bts.api.Security get_security() { - bts.security_model.OldSecurity result = null; - Co2NativeMethods.md_consumer_get_single_security(_cold, (key, buffer, length) => result = ProtoBuf.Serializer.Deserialize(new UnmanagedMemoryStream(buffer, (long)length))); - return new bts.api.Security(result); + public Security get_security() { + Security result = null; + Co2NativeMethods.md_consumer_get_single_security(_cold, (key, buffer, length) => result = ProtoBuf.Serializer.Deserialize(new UnmanagedMemoryStream(buffer, (long)length))); + return result; } // This property exists for specific performance optimization purposes diff --git a/src/lib/CMakeDeps.cmake b/src/lib/CMakeDeps.cmake index b20180187..d83664ced 100644 --- a/src/lib/CMakeDeps.cmake +++ b/src/lib/CMakeDeps.cmake @@ -1,2 +1 @@ -# generated by make process -set(sources api/apicore.proto;api/apioptions.proto;api/core/ApiClientInterop.cpp;api/core/ApiServerInterop.cpp;api/public/ApiServer.cpp;api/public/ClientChannel.cpp;api/public/QuickbusClientChannel.cpp;api/public/ServerChannel.cpp;api/public/ServiceContext.cpp;api/services/ApiUnitTestService.proto;api/services/HelloService.proto;api/services/MarketDataRelay.proto;api/services/OrderService.proto;api/services/OutOfProcessURLListener.proto;api/services/PersistenceService.proto;api/services/UserService.proto;api/test/ApiTests.cpp;api/test/HelloServiceServer.cpp;config/ConfigCheck.cpp;config/ConfigDownloader.cpp;config/ConfigErrorCollector.cpp;config/ConfigLog.cpp;config/ConfigPropertyEvaluator.cpp;config/ConfigValueQueue.cpp;config/ConfigurationException.cpp;config/ConfigurationState.cpp;config/PreprocessInputStream.cpp;config/config.cpp;config/defaults/Account.conf;config/defaults/Executor.conf;config/defaults/MarketData.conf;config/defaults/Symbols.conf;config/defaults/User.conf;config/defaults/edge.conf;config/defaults/expiration_display_offset.conf;config/defaults/logging.conf;config/defaults/testkeys.conf;config/extensions/extensions.proto;config/invalid/invalid.proto;config/protos/Account.proto;config/protos/Executor.proto;config/protos/MarketData.proto;config/protos/Ports.proto;config/protos/Symbols.proto;config/protos/UI.proto;config/protos/User.proto;config/protos/environment.proto;config/protos/expiration_display_offset.proto;config/protos/jetfix.proto;config/protos/logging.proto;config/protos/quickbus_cfg.proto;config/protos/system.proto;config/protos/testkeys.proto;config/protos/toggles.proto;config/tests/HTTPServer.cpp;config/tests/config_ref_tests.cpp;config/tests/config_tests.cpp;config/text_format/text_format.cpp;domain_model/Security.proto;expression/BooleanGrammar.cpp;expression/ExpressionEvaluator.cpp;expression/ParserExceptions.cpp;expression/Value.cpp;expression/tests/expression_parser_tests.cpp;logging/LoggerHub.cpp;logging/Sink.cpp;logging/logging.cpp;logging/tests/logging_tests.cpp;md/Diag/MDPriceLantencyTester.cpp;md/Diag/ShmDumper.cpp;md/MDConsumer.cpp;md/MDConsumerHandle.cpp;md/MDConsumerInterop.cpp;md/MDProducer.cpp;md/MDProducerHandle.cpp;md/MDRecoverableConsumer.cpp;md/MemorySegmentUtils.cpp;md/PlatformLoader.cpp;md/SecurityIDRecorder.cpp;md/SettlementRecorder.cpp;md/Structure/FixedSecurity.cpp;md/Structure/PriceSize.cpp;md/common.cpp;md/protos/co2.proto;md/relay/QueryServer.cpp;md/relay/RelayClient.cpp;md/relay/RelayRecorder.cpp;md/test/Co2TestInstrument.cpp;md/test/md2_tests.cpp;order_man/protos/OrderMan.proto;os/os.cpp;os/tests/os_tests.cpp;quickbus/core/LocalNode.cpp;quickbus/core/Quickbus.cpp;quickbus/core/SubscriptionID.cpp;quickbus/core/TableID.cpp;quickbus/message_model/Update.cpp;quickbus/message_model/merge_updates.cpp;quickbus/network/QBConnection.cpp;quickbus/network/QBConnectionManager.cpp;quickbus/network/endpoint_description.cpp;quickbus/network/quickbus_msgs.proto;quickbus/pubsub/TablePublisher.cpp;quickbus/pubsub/TableSubscriber.cpp;quickbus/pubsub/tests/qb_pubsub_tests.cpp;quickbus/routing/INode.cpp;quickbus/routing/NodeID.cpp;quickbus/routing/Router.cpp;quickbus/rpc/RPCClient.cpp;quickbus/rpc/RPCServer.cpp;quickbus/rpc/RPCSessionID.cpp;quickbus/rpc/rpc_utils.cpp;quickbus/rpc/tests/qb_rpc_tests.cpp;quickbus/tests/quickbus_tests.cpp;security_model/SecurityHelper.cpp;security_model/old_security.proto;sparkproto/SparkProtos.proto;testing/windows_test_driver.cpp;utils/Dispatcher.cpp;utils/HTTPStream.cpp;utils/PBUtils.cpp;utils/Puid.cpp;utils/SharedMemory.cpp;utils/Sqlite_utils.cpp;utils/Uuid.cpp;utils/datetime.cpp;utils/file_utils.cpp;utils/init_spark_lib.cpp;utils/is_unittest_environment.cpp) +set(sources api/apicore.proto;api/apioptions.proto;api/core/ApiClientInterop.cpp;api/core/ApiServerInterop.cpp;api/public/ApiServer.cpp;api/public/ClientChannel.cpp;api/public/QuickbusClientChannel.cpp;api/public/ServerChannel.cpp;api/public/ServiceContext.cpp;api/services/ApiUnitTestService.proto;api/services/HelloService.proto;api/services/MarketDataRelay.proto;api/services/OrderService.proto;api/services/OutOfProcessURLListener.proto;api/services/PersistenceService.proto;api/services/UserService.proto;api/test/ApiTests.cpp;api/test/HelloServiceServer.cpp;config/ConfigCheck.cpp;config/ConfigDownloader.cpp;config/ConfigErrorCollector.cpp;config/ConfigLog.cpp;config/ConfigPropertyEvaluator.cpp;config/ConfigValueQueue.cpp;config/ConfigurationException.cpp;config/ConfigurationState.cpp;config/PreprocessInputStream.cpp;config/config.cpp;config/defaults/Account.conf;config/defaults/Executor.conf;config/defaults/MarketData.conf;config/defaults/Symbols.conf;config/defaults/User.conf;config/defaults/edge.conf;config/defaults/expiration_display_offset.conf;config/defaults/logging.conf;config/defaults/testkeys.conf;config/extensions/extensions.proto;config/invalid/invalid.proto;config/protos/Account.proto;config/protos/Executor.proto;config/protos/MarketData.proto;config/protos/Ports.proto;config/protos/Symbols.proto;config/protos/UI.proto;config/protos/User.proto;config/protos/environment.proto;config/protos/expiration_display_offset.proto;config/protos/jetfix.proto;config/protos/logging.proto;config/protos/quickbus_cfg.proto;config/protos/system.proto;config/protos/testkeys.proto;config/protos/toggles.proto;config/tests/HTTPServer.cpp;config/tests/config_ref_tests.cpp;config/tests/config_tests.cpp;config/text_format/text_format.cpp;domain_model/Security.proto;expression/BooleanGrammar.cpp;expression/ExpressionEvaluator.cpp;expression/ParserExceptions.cpp;expression/Value.cpp;expression/tests/expression_parser_tests.cpp;logging/LoggerHub.cpp;logging/Sink.cpp;logging/logging.cpp;logging/tests/logging_tests.cpp;md/Diag/MDPriceLantencyTester.cpp;md/Diag/ShmDumper.cpp;md/MDConsumer.cpp;md/MDConsumerHandle.cpp;md/MDConsumerInterop.cpp;md/MDProducer.cpp;md/MDProducerHandle.cpp;md/MDRecoverableConsumer.cpp;md/MemorySegmentUtils.cpp;md/PlatformLoader.cpp;md/SecurityIDRecorder.cpp;md/SettlementRecorder.cpp;md/Structure/FixedSecurity.cpp;md/Structure/PriceSize.cpp;md/common.cpp;md/protos/co2.proto;md/relay/QueryServer.cpp;md/relay/RelayClient.cpp;md/relay/RelayRecorder.cpp;md/test/Co2TestInstrument.cpp;md/test/md2_tests.cpp;os/os.cpp;os/tests/os_tests.cpp;quickbus/core/LocalNode.cpp;quickbus/core/Quickbus.cpp;quickbus/core/SubscriptionID.cpp;quickbus/core/TableID.cpp;quickbus/message_model/Update.cpp;quickbus/message_model/merge_updates.cpp;quickbus/network/QBConnection.cpp;quickbus/network/QBConnectionManager.cpp;quickbus/network/endpoint_description.cpp;quickbus/network/quickbus_msgs.proto;quickbus/pubsub/TablePublisher.cpp;quickbus/pubsub/TableSubscriber.cpp;quickbus/pubsub/tests/qb_pubsub_tests.cpp;quickbus/routing/INode.cpp;quickbus/routing/NodeID.cpp;quickbus/routing/Router.cpp;quickbus/rpc/RPCClient.cpp;quickbus/rpc/RPCServer.cpp;quickbus/rpc/RPCSessionID.cpp;quickbus/rpc/rpc_utils.cpp;quickbus/rpc/tests/qb_rpc_tests.cpp;quickbus/tests/quickbus_tests.cpp;security_model/SecurityHelper.cpp;security_model/old_security.proto;sparkproto/SparkProtos.proto;testing/windows_test_driver.cpp;utils/Dispatcher.cpp;utils/HTTPStream.cpp;utils/PBUtils.cpp;utils/Puid.cpp;utils/SharedMemory.cpp;utils/Sqlite_utils.cpp;utils/Uuid.cpp;utils/datetime.cpp;utils/file_utils.cpp;utils/init_spark_lib.cpp;utils/is_unittest_environment.cpp) \ No newline at end of file diff --git a/src/lib/domain_model/Security.proto b/src/lib/domain_model/Security.proto index 980e02ca2..61911e691 100644 --- a/src/lib/domain_model/Security.proto +++ b/src/lib/domain_model/Security.proto @@ -53,7 +53,6 @@ message ICEExchangeData { } message FutureContract { - string symbol = 10; uint32 expiration_year = 20; uint32 expiration_month = 30; uint32 expiration_day = 40; @@ -85,6 +84,7 @@ message ContractSpecification { message Security { uint32 id = 10 [(bts.api.key)=true]; + string symbol = 11; Venue exchange = 20; ContractSpecification contract_spec = 30; Currency currency = 40; diff --git a/src/lib/domain_model/common.h b/src/lib/domain_model/common.h index 8fe6997a4..933528f3a 100644 --- a/src/lib/domain_model/common.h +++ b/src/lib/domain_model/common.h @@ -9,7 +9,6 @@ namespace bts { using SecurityKey = utils::ValueWrapper; using Price = utils::ValueWrapper; using Quantity = utils::ValueWrapper; -using AccountName = utils::ValueWrapper; using UserLogin = utils::ValueWrapper; } diff --git a/src/lib/md/MDConsumer.cpp b/src/lib/md/MDConsumer.cpp index 05d9195c8..68522c6da 100644 --- a/src/lib/md/MDConsumer.cpp +++ b/src/lib/md/MDConsumer.cpp @@ -22,7 +22,7 @@ namespace md2 { /* * Implementation notes: - * SlowHeader keeps a security count to know when a list has changed + * ColdHeader keeps a security count to know when a list has changed * When a security is made visible increment count (non-atomic) * When user hits walk_security_list just walk all of them * When scanning for new securities we can short circuit the search if the security_count hasn't incremented diff --git a/src/lib/md/MDConsumerHandle.h b/src/lib/md/MDConsumerHandle.h index ae82c8720..cc11681f0 100644 --- a/src/lib/md/MDConsumerHandle.h +++ b/src/lib/md/MDConsumerHandle.h @@ -33,7 +33,8 @@ public: CoconutSecurityStatus::type status() const; uint16_t sequence() const; bool has_depth() const { return _cheader && _cheader->has_depth; } - security_key key() const { return _cdata ? _cdata->security.key : invalid_security_key; } + // security_key key() const { return _cdata ? _cdata->security.key : invalid_security_key; } + security_key key() const { return has_depth() ? hotd()->key : hot()->key; } OldSecurity get_normalized_security() const { bts_assert(is_initialized()); return convert_security(_cdata->security); } diff --git a/src/lib/md/MDConsumerInterop.cpp b/src/lib/md/MDConsumerInterop.cpp index d2a14a77d..d58afee34 100644 --- a/src/lib/md/MDConsumerInterop.cpp +++ b/src/lib/md/MDConsumerInterop.cpp @@ -63,10 +63,10 @@ typedef void(*security_cb)(security_key, const char*, size_t, MDConsumerHandle& CAPI size_t md_consumer_walk_security_list(MDConsumer* consumer, security_cb cb) { bts_assert(consumer); std::string buffer; - return consumer->walk_security_list([&](const OldSecurity& security, MDConsumerHandle handle) { + return consumer->walk_security_list([&](const Security& security, MDConsumerHandle handle) { buffer.clear(); security.SerializePartialToString(&buffer); - cb(security.security_key(), buffer.c_str(), buffer.size(), handle); + cb(security.id(), buffer.c_str(), buffer.size(), handle); }); } @@ -93,10 +93,10 @@ void safe_call_clr_callback(security_cb cb, security_key key, const char* buffer CAPI void md_consumer_set_on_security(MDConsumer* consumer, security_cb cb) { bts_assert(consumer); - consumer->on_security([=](const OldSecurity& security, MDConsumerHandle handle) { + consumer->on_security([=](const Security& security, MDConsumerHandle handle) { std::string buffer; security.SerializePartialToString(&buffer); - safe_call_clr_callback(cb, security.security_key(), buffer.c_str(), buffer.size(), handle); + safe_call_clr_callback(cb, security.id(), buffer.c_str(), buffer.size(), handle); }); } #endif @@ -104,10 +104,13 @@ CAPI void md_consumer_set_on_security(MDConsumer* consumer, security_cb cb) { typedef void(*just_security_cb)(security_key, const char*, size_t); CAPI void md_consumer_get_single_security(const ColdEntry* entry, just_security_cb cb) { bts_assert(entry); - auto security = convert_security(entry->security); + bts::api::Security security; + // fix + auto old = convert_security(entry->security); + convert_security(old, security); std::string buffer; security.SerializePartialToString(&buffer); - cb(security.security_key(), buffer.c_str(), buffer.size()); + cb(security.id(), buffer.c_str(), buffer.size()); } bool is_heart_beat_ok(const ColdHeader &header); diff --git a/src/lib/md/MDRecoverableConsumer.cpp b/src/lib/md/MDRecoverableConsumer.cpp index 1848da828..918024ae6 100644 --- a/src/lib/md/MDRecoverableConsumer.cpp +++ b/src/lib/md/MDRecoverableConsumer.cpp @@ -48,13 +48,14 @@ unique_ptr MDRecoverableConsumer::init(s return result; } -size_t MDRecoverableConsumer::walk_security_list(OldSecurityCallback callback) { +size_t MDRecoverableConsumer::walk_security_list(SecurityCallback callback) { return _current->consumer.walk_security_list(callback); } -void MDRecoverableConsumer::on_security(OldSecurityCallback cb) { + +void MDRecoverableConsumer::on_security(SecurityCallback cb) { // Add a filter to the callback to avoid calling the on_security callback with duplicates - auto filtered_cb = [this,cb](const OldSecurity& security, MDConsumerHandle handle) { - auto key = security.security_key(); + auto filtered_cb = [this,cb](const Security& security, MDConsumerHandle handle) { + auto key = security.id(); if (_seen.find(key) == _seen.end()) { _seen.insert(key); cb(security, handle); @@ -153,3 +154,4 @@ set get_symbols(Exchange::type exchange) { }}//namespace #endif + diff --git a/src/lib/md/MDRecoverableConsumer.h b/src/lib/md/MDRecoverableConsumer.h index 1bb535f4b..8076a89da 100644 --- a/src/lib/md/MDRecoverableConsumer.h +++ b/src/lib/md/MDRecoverableConsumer.h @@ -23,8 +23,8 @@ public: // // Security functions - size_t walk_security_list(md2::OldSecurityCallback callback); - void on_security(md2::OldSecurityCallback cb); + size_t walk_security_list(md2::SecurityCallback callback); + void on_security(md2::SecurityCallback cb); // Scans for added securities - updates managed handles size_t scan_securities(); @@ -53,7 +53,7 @@ private: std::unique_ptr _current; std::vector> _old; - std::vector _callbacks; + std::vector _callbacks; std::set _seen; typedef tbb::mutex Mutex; Mutex _lock; diff --git a/src/lib/md/Structure/FixedSecurity.cpp b/src/lib/md/Structure/FixedSecurity.cpp index 7bbc167cc..6104d2454 100644 --- a/src/lib/md/Structure/FixedSecurity.cpp +++ b/src/lib/md/Structure/FixedSecurity.cpp @@ -360,9 +360,10 @@ void convert_security(const OldSecurity& source, Security& dest) { dest.mutable_future()->set_expiration_day(source.future().expiration_day()); dest.mutable_future()->set_expiration_month(source.future().expiration_month()); dest.mutable_future()->set_expiration_year(source.future().expiration_year()); - dest.mutable_future()->set_symbol(source.future().symbol()); + dest.set_symbol(source.future().symbol()); break; case security_model::SecurityType::SPREAD: { + dest.set_symbol(source.spread().symbol()); for(const auto& oleg : source.spread().legs()) { auto* nleg = dest.mutable_spread()->add_legs(); nleg->set_quantity(oleg.leg_quantity()); diff --git a/src/lib/md/Structure/structure.h b/src/lib/md/Structure/structure.h index 0b4dde140..52514301e 100644 --- a/src/lib/md/Structure/structure.h +++ b/src/lib/md/Structure/structure.h @@ -49,16 +49,15 @@ struct ALIGNED_(32) HotEntry { const uint32_t COCONUT_MAX_DEPTH = 10; struct ALIGNED_(CACHE_LINE) HotDepthEntry { HotDepthEntry() : - timestamp(), sequence(0), status(CoconutSecurityStatus::OK), book(), key(invalid_security_key) + timestamp(), key(invalid_security_key), status(CoconutSecurityStatus::OK), book(), sequence(0) { } uint32_t size() const { return sizeof(HotDepthEntry); } TimeStamp timestamp; // 8 - uint16_t sequence; // 2 - //md::CoconutSecurityStatus::type status; // 4 + security_key key; // NOTE: key is at a different offset than it is in HotEntry uint8_t status; // 1 // Interleave bid/ask values so the top of book is always on the first line SingleWriterPriceSizeStore book[COCONUT_MAX_DEPTH*2]; - security_key key; // NOTE: key is at a different offset than it is in HotEntry + uint16_t sequence; // 2 SingleWriterPriceSizeStore& bid(uint32_t depth) { return book[depth*2]; } const SingleWriterPriceSizeStore& bid(uint32_t depth) const { return book[depth*2]; } diff --git a/src/lib/md/test/md2_tests.cpp b/src/lib/md/test/md2_tests.cpp index eefbd4bb0..0f8ae855d 100644 --- a/src/lib/md/test/md2_tests.cpp +++ b/src/lib/md/test/md2_tests.cpp @@ -1,1361 +1,144 @@ -#ifndef NO_CPP0X - -#include "catch/catch.hpp" -#include "../SettlementRecorder.h" -#include "../coconut2.h" -#include "../relay/QueryServer.h" -#include "Co2TestInstrument.h" -#include "lib/config/config.h" -#include "lib/os/os.h" -#include "lib/security_model/SecurityHelper.h" -#include "lib/utils/Dispatcher.h" -#include "lib/utils/defer.h" -#include "lib/utils/warn_ctrl.h" -#include "lib/utils/bts_chrono.h" - -#include "lib/config/protos/MarketData.pb.h" - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - - -using namespace bts::md2; -using namespace bts::md; -using namespace std; - -namespace bts { -namespace md2 { -namespace test { - -// -// Helper functions for Producer/Consumer tests -// -vector create_test_securities(MDProducer& producer, const char* postfix ="") { - FixedOptionSecurity fos; - FixedFutureSecurity ffs; - ffs.symbol("FOO"+string(postfix)); - fos.symbol("FOOX"+string(postfix)); - ffs.key = invalid_security_key; - fos.key = invalid_security_key; - ffs.expiration_year = fos.expiration_year = 2015; - ffs.expiration_day = fos.expiration_day = 27; - fos.strike = 1000; - fos.call_put = PutOrCall::CALL; - // Add 12 expirations to 2 different shared memory regions - vector result; - for (uint32_t x = 1; x <= 12; ++x) { - ffs.expiration_month = fos.expiration_month = x; - result.push_back(producer.add_security(ffs)); - result.push_back(producer.add_security(fos)); - // In a real app these securities would now be passed to the SecurityIDRecorder - } - return result; -} - -set get_test_symbols(const char* postfix="") { - set symbols; - UserSymbol future_symbol(string("FOO")+postfix, Exchange::TESTEXCHANGE, SecurityType::FUTURE); - symbols.insert(future_symbol); - UserSymbol option_symbol(string("FOOX")+postfix, Exchange::TESTEXCHANGE, SecurityType::OPTION); - symbols.insert(option_symbol); - return symbols; -} - -OldSecurity mkfuture(string symbol, security_key key, int year, int month, int day) { - OldSecurity result; - result.set_security_type(SecurityType::FUTURE); - SecurityHelper::set_symbol(result, symbol); - result.set_exchange(Exchange::TESTEXCHANGE); - result.set_security_key(key); - result.mutable_future()->set_expiration_day(day); - result.mutable_future()->set_expiration_month(month); - result.mutable_future()->set_expiration_year(year); - return result; -} -OldSecurity mkoption(string symbol, security_key key, int year, int month, int day, double strike) { - OldSecurity result; - result.set_security_type(SecurityType::OPTION); - SecurityHelper::set_symbol(result, symbol); - result.set_exchange(Exchange::TESTEXCHANGE); - result.set_security_key(key); - result.mutable_option()->set_expiration_day(day); - result.mutable_option()->set_expiration_month(month); - result.mutable_option()->set_expiration_year(year); - result.mutable_option()->set_strike(strike); - result.mutable_option()->set_call_put(PutOrCall::PUT); - return result; -} -FixedOptionSecurity mkfixedoption(string symbol, security_key key, int year, int month, int day, double strike, PutOrCall::type callput) { - FixedOptionSecurity fos; - fos.type = SecurityType::OPTION; - fos.exchange = Exchange::TESTEXCHANGE; - fos.key = key; - fos.strike = strike; - fos.expiration_day = day; - fos.expiration_month = month; - fos.expiration_year = year; - fos.call_put = callput; - fos.symbol(symbol); - return fos; -} -OldSecurity mkspread(string symbol, security_key key, const vector& legs) { - OldSecurity result; - result.set_security_type(SecurityType::SPREAD); - SecurityHelper::set_symbol(result, symbol); - result.set_exchange(Exchange::TESTEXCHANGE); - result.set_security_key(key); - for (const auto& leg : legs) { - auto* pleg = result.mutable_spread()->add_legs(); - pleg->mutable_security()->CopyFrom(leg); - } - return result; -} -OldSecurity mkspread(string symbol, security_key key, OldSecurity leg1, OldSecurity leg2) { - OldSecurity legs[] = { leg1, leg2 }; - vector vlegs(begin(legs), end(legs)); - return mkspread(symbol, key, vlegs); -} -OldSecurity mkspread(string symbol, security_key key, OldSecurity leg1, OldSecurity leg2, OldSecurity leg3) { - OldSecurity legs[] = { leg1, leg2, leg3 }; - vector vlegs(begin(legs), end(legs)); - return mkspread(symbol, key, vlegs); -} - -void assign_ids(vector::iterator begin, vector::iterator end, security_key start_key = 1000) { - for (; begin != end; ++begin) { - (*begin).set_key(++start_key); - } -} - -class Co2Tests -{ -public: - void setup() { - config::internal::initialize_for_test( - "PlatformMap {" - " value {" - " sim {" - " exchange: TESTEXCHANGE" - " }" - " }" - "}" - "PublisherOptions {" - " value {" - " heartbeat_ms: 100" - " heartbeat_boot_seconds: 1" - " }" - "}" - ); - } - -#ifndef WIN32 - void test_security_id_recorder() { - // Verify callbacks happen after work complete - SecurityIDRecorder idsvc, idsvc2; - security_key result = invalid_security_key; - std::string desc = "FOOX 1"; - idsvc.get_key(desc, SecurityType::FUTURE, [](void* arg, security_key key) { - security_key* presult = (security_key*)arg; - *presult = key; - }, &result); - idsvc.do_work(); - REQUIRE(result != invalid_security_key); - security_key result2 = invalid_security_key; - idsvc2.get_key(desc, SecurityType::OPTION, [](void* arg, security_key key) { - *((security_key*)arg) = key; - }, &result2); - idsvc2.do_work(); - REQUIRE(result == result2); - } -#endif - - void test_price_size() { - // PriceSize violates some type punning rules - // So it is important to have this test work correctly on the target platform - PriceSize p, p2; - p = PriceSize::from_double<2>(4.5, 14); - SingleWriterPriceSizeStore memory; - memory.store(p); - p2 = memory.load(); - REQUIRE(p.price() == 4.5); - REQUIRE(p.size() == 14); - REQUIRE(p2.price() == 4.5); - REQUIRE(p2.size() == 14); - - p = PriceSize::from_double<2>(4.0, 5); - memory.store(p); - p2 = memory.load(); - REQUIRE(p2.price() == 4.0); - REQUIRE(p2.size() == 5); - - p = PriceSize::from_double<2>(-4.2, 6); - memory.store(p); - p2 = memory.load(); - REQUIRE(p2.price() == -4.2); - REQUIRE(p2.size() == 6); - - p = PriceSize(4, -1, 2); - memory.store(p); - p2 = memory.load(); - REQUIRE(p2.price() == 0.4); - REQUIRE(p2.size() == 2); - - // Test requirement for more than 32 bit mantissa - double bigval = -9999.9921875; - p = PriceSize::from_double<7>(bigval, 42); - memory.store(p); - p2 = memory.load(); - REQUIRE(p2.price() == bigval); - REQUIRE(p2.size() == 42); - } - - void test_slow_double_conversion() { - double values[] = { 1.5, 0.5, 0.2155, 0.216, 42.1 }; - int expected[] = { -1, -1, -4, -3, -1 }; - static_assert(BTS_ARRAYSIZE(values) == BTS_ARRAYSIZE(expected), ""); - for (size_t x = 0; x < BTS_ARRAYSIZE(values); ++x) { - REQUIRE(to_exponent(values[x]) == expected[x]); - } - - // Tests powers of 10. Important for display_factor conversion - for (int x = 8; x > -8; --x) { - double d = pow10(x); - REQUIRE(to_exponent(d) == x); - } - } - - struct ExchangeDecimal { - int64_t mantissa; - int32_t exponent; - }; - bool close_enough(double a, double b) { - // We need at least 7 places but we're testing with 10 just in case - const double acceptable_error = 1E-12; - double delta = abs(a - b); - // Equal if the delta is less than error - return delta < acceptable_error; - } - void test_compact_exponent() { - ExchangeDecimal test_values[] = { - { 2030000000000, -9 }, // mantissa > pow(2,40) - { -2030000000000, -9 }, // mantissa > pow(2,40) - { 2022750000000, -9 },// mantissa > pow(2,40) - { -2022750000000, -9 },// mantissa > pow(2,40) - { 20227500000000, -10 },// mantissa > pow(2,40) - { 202275000000000, -11 },// mantissa > pow(2,40) - { 2022750000000000, -12 },// mantissa > pow(2,40) - { 20227500000000000, -13 },// mantissa > pow(2,40) - { -20227500000000000, -13 },// mantissa > pow(2,40) - { 203, 1}, // control - { -14000000000, -8 }, // control - { -14, 1 }, // control - { 20227500, -4 }, // control - }; - for (auto p : test_values) { - double expected = p.mantissa * pow10(p.exponent); - compact_mantissa(p.mantissa, p.exponent); - double compacted = p.mantissa * pow10(p.exponent); - REQUIRE(close_enough(expected, compacted)); - } - } - - void test_producer_consumer() { - MDProducer producer(Platform::SIM); - vector producers = create_test_securities(producer); - - // Add bbo data for those expirations - auto write_time = TimeStamp::now(); - for (auto& p : producers) { - p.update_bid(write_time, PriceSize::from_double<2>(5.0, 5)); - p.update_ask(write_time, PriceSize::from_double<2>(5.0, 5)); - } - - // Symbol list for the consumer - set symbols; - UserSymbol future_symbol( "FOO", Exchange::TESTEXCHANGE, SecurityType::FUTURE ); - symbols.insert(future_symbol); - UserSymbol option_symbol( "FOOX", Exchange::TESTEXCHANGE, SecurityType::OPTION ); - symbols.insert(option_symbol); - MDConsumer consumer(symbols); - - vector consumer_securities; - // Poll for security list - none visible until they have keys - size_t count = consumer.walk_security_list([&](const OldSecurity& security, MDConsumerHandle) {consumer_securities.push_back(security);}); - // Since no security has an id none are "public" - REQUIRE(consumer_securities.size() == count); - REQUIRE(count == 0); - // Assign a unique ID to each security (from the producer) - assign_ids(producers.begin(), producers.end()); - // Now securities can be enumerated by the client - count = consumer.walk_security_list([&](const OldSecurity& security, MDConsumerHandle) {consumer_securities.push_back(security);}); - REQUIRE(consumer_securities.size() == count); - REQUIRE(count == 24); - - vector consumers; - for(const auto& security : consumer_securities) { - consumers.push_back(consumer.get_handle(security.security_key())); - } - - for (const auto& c : consumers) { - PriceSize bid, ask; - TimeStamp read_time; - // get_boo returns true becuase this time is after the change - REQUIRE(c.get_bbo(read_time, bid, ask) == HandleReadResult::SUCCESS); - REQUIRE((read_time - TimeStamp()).seconds() > 0); - REQUIRE(bid.price() == 5.0); - REQUIRE(ask.price() == 5.0); - REQUIRE(bid.size() == 5); - REQUIRE(ask.size() == 5); - } - - // Second query before a write has happend finds no updates - auto read_time = TimeStamp::now(); - for (const auto& c : consumers) { - PriceSize bid, ask; - REQUIRE(c.get_bbo(read_time, bid, ask) != HandleReadResult::SUCCESS); - } - - // update everything once more - write_time = TimeStamp::now(); - for (auto& p : producers) { - // Update bid/ask with one call - p.update_bbo(write_time, PriceSize::from_double<2>(6.55, 6), PriceSize::from_double<2>(6.55, 6)); - } - // Now read_time is < write_time - for (const auto& c : consumers) { - TimeStamp modified_time = read_time; - PriceSize bid, ask; - // get_bbo returns true becuase this time is after the change - REQUIRE(c.get_bbo(modified_time, bid, ask) == HandleReadResult::SUCCESS); - REQUIRE( (modified_time - read_time).seconds() > 0); - REQUIRE(bid.price() == 6.55); - REQUIRE(ask.price() == 6.55); - REQUIRE(bid.size() == 6); - REQUIRE(ask.size() == 6); - } - } - - // When a security is added the consumer should provide callback - void test_consumer_callback_on_security_added() { - MDProducer producer(Platform::SIM); - vector producers = create_test_securities(producer); - - // Symbol list for the consumer - set symbols; - UserSymbol future_symbol( "FOO", Exchange::TESTEXCHANGE, SecurityType::FUTURE ); - symbols.insert(future_symbol); - UserSymbol option_symbol( "FOOX", Exchange::TESTEXCHANGE, SecurityType::OPTION ); - symbols.insert(option_symbol); - MDConsumer consumer(symbols); - - // Add the on_security callback - vector securities; - consumer.on_security([&](const OldSecurity& security, MDConsumerHandle handle) { - REQUIRE(security.security_key() != invalid_security_key); - REQUIRE(security.security_key() == handle.key()); - securities.push_back(security); - }); - // No securities have ids yet - REQUIRE(securities.size() == 0); - // Check for new securities - consumer.scan_securities(); - // Still none - REQUIRE(securities.size() == 0); - producers.front().set_key(1001); - consumer.scan_securities(); - REQUIRE(securities.size() == 1); - - // Assign the rest of the ids - assign_ids(producers.begin() + 1, producers.end(), producers.front().security().key); - consumer.scan_securities(); - REQUIRE(securities.size() == producers.size()); - } - - // If an MDConsumer is asked for a handle to a security which does not exist in the system - // The consumer should return a handle which always returns false on all calls but wakes up - // when the security is added. - // * Case 1 - the shared memory has been created but the security has not been discovered - // * Case 2 - The producer has never been created - void test_consumer_defer_handle1() { - MDProducer producer(Platform::SIM); - vector producers = create_test_securities(producer); - - set symbols; - UserSymbol future_symbol( "FOO", Exchange::TESTEXCHANGE, SecurityType::FUTURE ); - symbols.insert(future_symbol); - UserSymbol option_symbol( "FOOX", Exchange::TESTEXCHANGE, SecurityType::OPTION ); - symbols.insert(option_symbol); - MDConsumer consumer(symbols); - - // Get a handle to 24 consumers which haven't been created yet - vector consumers; - vector> managed_consumers; - security_key key = 1; - for(; key < 24; ++key) { - consumers.push_back(consumer.get_handle(key+1000)); - managed_consumers.push_back(consumer.get_managed_handle(key+1000)); - } - - // Set bbo on the producers - still not public - TimeStamp write_time = TimeStamp::now(); - for (auto& p : producers) { - p.update_bbo(write_time, PriceSize::from_double<3>(4.5, 5), PriceSize::from_double<3>(6.75, 7)); - } - // Even though bbo is available it is not given out because the producing security does not have an id - PriceSize bid, ask; - for(auto& c : consumers) { - TimeStamp zerotime; - REQUIRE(c.get_bbo(zerotime, bid, ask) != HandleReadResult::SUCCESS); - } - for(auto& mc : managed_consumers) { - TimeStamp zerotime; - REQUIRE(mc->get_bbo(zerotime, bid, ask) != HandleReadResult::SUCCESS); - } - - assign_ids(producers.begin(), producers.end()); - consumer.scan_securities(); - size_t bbo_count = 0; - // Existing old handles remain invalid - for(auto& c : consumers) { - TimeStamp zerotime; - if (c.get_bbo(zerotime, bid, ask) == HandleReadResult::SUCCESS) { - bbo_count++; - } - } - REQUIRE(bbo_count == 0); - // Now, magically, the existing (managed) handles are valid - for(auto& mc : managed_consumers) { - TimeStamp zerotime; - if (mc->get_bbo(zerotime, bid, ask) == HandleReadResult::SUCCESS) { - bbo_count++; - } - } - REQUIRE(bbo_count > 0); - } - - - // It is legal and valid to create a consumer before the producer process has started - // Handles given out by that consumer will become valid once the securities are discovered - void test_consumer_defer_handle2() { - set symbols; - UserSymbol future_symbol( "FOO", Exchange::TESTEXCHANGE, SecurityType::FUTURE ); - symbols.insert(future_symbol); - UserSymbol option_symbol( "FOOX", Exchange::TESTEXCHANGE, SecurityType::OPTION ); - symbols.insert(option_symbol); - MDConsumer consumer(symbols); - - // Get a handle to 24 consumers which haven't been created yet - vector consumers; - vector> managed_consumers; - security_key key = 1; - for(; key < 24; ++key) { - consumers.push_back(consumer.get_handle(key+1000)); - managed_consumers.push_back(consumer.get_managed_handle(key+1000)); - } - - // Even though bbo is available it is not given out because the producing security does not have an id - PriceSize bid, ask; - for(auto& c : consumers) { - TimeStamp zerotime; - REQUIRE(c.get_bbo(zerotime, bid, ask) != HandleReadResult::SUCCESS); - } - for(auto& mc : managed_consumers) { - TimeStamp zerotime; - REQUIRE(mc->get_bbo(zerotime, bid, ask) != HandleReadResult::SUCCESS); - } - - // Only NOW create the producers (and the shared memory) - MDProducer producer(Platform::SIM); - vector producers = create_test_securities(producer); - // Set bbo on the producers - still not public - TimeStamp write_time = TimeStamp::now(); - for (auto& p : producers) { - p.update_bbo(write_time, PriceSize::from_double<3>( 4.5, 5 ), PriceSize::from_double<3>(6.75, 7)); - } - - assign_ids(producers.begin(), producers.end()); - consumer.scan_securities(); - size_t bbo_count = 0; - // Existing old handles remain invalid - for(auto& c : consumers) { - TimeStamp zerotime; - if (c.get_bbo(zerotime, bid, ask) == HandleReadResult::SUCCESS) { - bbo_count++; - } - } - REQUIRE(bbo_count == 0); - // Now, magically, the existing (managed) handles are valid - for(auto& mc : managed_consumers) { - TimeStamp zerotime; - if (mc->get_bbo(zerotime, bid, ask) == HandleReadResult::SUCCESS) { - bbo_count++; - } - } - REQUIRE(bbo_count > 0); - } - - void test_spread_resolver() { - SpreadResolver resolver; - vector legs; - legs.push_back("1"); - legs.push_back("2"); - legs.push_back("3"); - legs.push_back("4"); - resolver.add("1234", legs); - legs.clear(); - legs.push_back("4"); - legs.push_back("5"); - resolver.add("45", legs); - auto key_map = [](string& key) -> security_key { - if (key == "1") return 1; - if (key == "2") return 2; - if (key == "3") return 3; - if (key == "4") return 4; - if (key == "5") return invalid_security_key; - REQUIRE(false); - return 0; - }; - auto key_map2 = [](string& key) -> security_key { - if (key == "4") return 4; - if (key == "5") return 5; - REQUIRE(false); - return 0; - }; - - // 2 unresolved spreads - REQUIRE(resolver.count() == 2); - resolver.resolve(key_map, [&](const string& key, const vector& legs) { - REQUIRE(key == "1234"); - REQUIRE(legs.size() == 4); - }); - // 1 spread remains - REQUIRE(resolver.count() == 1); - resolver.resolve(key_map2, [&](const string& key, const vector& legs) { - REQUIRE(key == "45"); - REQUIRE(legs.size() == 2); - }); - REQUIRE(resolver.count() == 0); - } - - - void test_no_double_security_callback() { - MDProducer producer(Platform::SIM); - vector producers = create_test_securities(producer); - // Assign ids so securities are already public in the producer - assign_ids(producers.begin(), producers.end()); - - // Symbol list for the consumer - auto syms = get_test_symbols(); - MDConsumer consumer = MDConsumer(syms); - - // Add the on_security callback - callback fires for existing securities - vector securities; - consumer.on_security([&](const OldSecurity& security, MDConsumerHandle handle) { - REQUIRE(security.security_key() != invalid_security_key); - REQUIRE(security.security_key() == handle.key()); - securities.push_back(security); - }); - // Found all securities - REQUIRE(securities.size() == producers.size()); - // Check for new securities - consumer.scan_securities(); - // Expect no duplicates - REQUIRE(securities.size() == producers.size()); - } - - void test_recovery() { - auto producer1 = make_shared(Platform::SIM); - vector producers = create_test_securities(*producer1); - assign_ids(producers.begin(), producers.end()); - producer1->get_channel("FOO", SecurityType::FUTURE, Exchange::TESTEXCHANGE).heartbeat(); - producer1->get_channel("FOOX", SecurityType::OPTION, Exchange::TESTEXCHANGE).heartbeat(); - for (auto& pro : producers) { pro.update_bbo(TimeStamp::now(), PriceSize(1, 0, 1), PriceSize(2, 0, 2)); } - - // Symbol list for the consumer - MDRecoverableConsumer consumer(get_test_symbols()); - map> products; - consumer.on_security([&](const OldSecurity& security, MDConsumerHandle handle) { - REQUIRE(security.security_key() == handle.key()); - products.insert(make_pair(security.security_key(), consumer.get_managed_handle(security.security_key()))); - }); - consumer.scan_securities(); - REQUIRE(products.size() == producers.size()); - - // Choose a handle - for (auto& pair : products) { - auto& handle = pair.second; - REQUIRE(handle->is_initialized()); - REQUIRE(handle->is_heart_beating()); - } - - // Delete the producer - try { - producers.clear(); - producer1.reset(); - } catch(std::exception&) { - LOG(MarketData,Error,"Failed test trying to destroy producer."); - REQUIRE(false); - } - - // Wait out the heartbeat - auto options = config::get(); - auto ms = std::max(options.heartbeat_ms(), options.heartbeat_boot_seconds()*1000)+10; - - std::this_thread::sleep_for(std::chrono::duration(ms)); - - // Make sure heartbeat is done - try { - for (auto& pair : products) { - auto& handle = pair.second; - REQUIRE(handle->is_initialized()); - REQUIRE(handle->is_heart_beating() == false); - } - } catch(std::exception&) { - LOG(MarketData,Error,"Failed to validate heartbeat while producer is down."); - REQUIRE(false); - } - - // Create a new publisher and attempt to recover - shared_ptr producer2; - try { - producer2 = make_shared(Platform::SIM); - } catch(std::exception&) { - LOG(MarketData,Error,"Failed to create new producer"); - REQUIRE(false); - } - producers = create_test_securities(*producer2); - REQUIRE(products.size() == producers.size()); - assign_ids(producers.begin(), producers.end()); - producer2->get_channel("FOO", SecurityType::FUTURE, Exchange::TESTEXCHANGE).heartbeat(); - producer2->get_channel("FOOX", SecurityType::OPTION, Exchange::TESTEXCHANGE).heartbeat(); - - // set bbo to a different value - for (auto& pro : producers) { - pro.update_bbo(TimeStamp::now(), PriceSize(4, 0, 5), PriceSize(6, 0, 7)); - } - - // Attempt to recover the managed handles - REQUIRE(consumer.try_recover()); - - // Since we know that both securities exist they should have been updated to the new section - for (auto& pair : products) { - auto& handle = pair.second; - REQUIRE(handle->is_initialized()); - REQUIRE(handle->is_heart_beating()); - TimeStamp tzero; - PriceSize bid, ask; - REQUIRE(handle->get_bbo(tzero, bid, ask) == HandleReadResult::SUCCESS); - REQUIRE(bid.price() == 4.0); - REQUIRE(bid.size() == 5); - REQUIRE(ask.price() == 6.0); - REQUIRE(ask.size() == 7); - } - } - - - void test_no_block_forever_on_publisher_crash() { -#ifdef _WIN32 -#pragma warning(push, 0) -#endif - // OK if this isn't aligned for testing - auto src = std::make_unique ("Foo", SecurityType::FUTURE); -#ifdef _WIN32 -#pragma warning(pop) -#endif - TimeStamp read = TimeStamp::now(); - src->producer().update_bbo(TimeStamp::invalid(), PriceSize::from_double<3>(100.0, 100), PriceSize::from_double<3>(101.0, 101)); - PriceSize bid, ask; - TimeStamp start = TimeStamp::now(); - auto result = src->consumer().get_bbo(read, bid, ask); - TimeStamp end = TimeStamp::now(); - REQUIRE(result != HandleReadResult::SUCCESS); - REQUIRE((end - start).seconds() < 5); - } - - void test_get_handles() { - MDProducer producer(Platform::SIM); - vector producers = create_test_securities(producer); - // Add entries to the shared memory that we aren't looking for - vector p2_no_id = create_test_securities(producer, "2"); - vector p3 = create_test_securities(producer, "3"); - - for (auto& p : producers) { p.update_bbo(TimeStamp::now(), PriceSize::from_double<3>(5.0, 5), PriceSize::from_double<3>(5.0, 5)); } - assign_ids(producers.begin(), producers.end()); - assign_ids(p3.begin(), p3.end()); - - // Symbol list for the consumer - only search half of the symbols - set symbols; - UserSymbol future_symbol( "FOO", Exchange::TESTEXCHANGE, SecurityType::FUTURE ); - symbols.insert(future_symbol); - UserSymbol option_symbol( "FOOX", Exchange::TESTEXCHANGE, SecurityType::OPTION ); - symbols.insert(option_symbol); - MDConsumer consumer(symbols); - - set keys; - consumer.walk_security_list([&](const OldSecurity& security, MDConsumerHandle) { keys.insert(security.security_key()); }); - REQUIRE(keys.size() == producers.size()); - auto map = consumer.get_handles(keys); - REQUIRE(map.size() == keys.size()); - for (const auto& entry : map) { - REQUIRE(entry.second.is_initialized()); - } - } - - void test_denormalization() { - // By using a different symbol for each security - // we put them all on separate shared mem sections (co2 channels) - security_key key = 999; - OldSecurity f1 = mkfuture("F1", ++key, 2016, 5, 1); - OldSecurity o1 = mkoption("O1", ++key, 2016, 5, 1, 100); - OldSecurity o2 = mkoption("O2", ++key, 2016, 5, 2, 200); - OldSecurity o3 = mkoption("O3", ++key, 2016, 5, 3, 300); - OldSecurity s4 = mkspread("S3", ++key, o2, o3); - OldSecurity s3 = mkspread("S4", ++key, o1, s4); - OldSecurity s2 = mkspread("S2", ++key, o1, s4, s3); - OldSecurity s1 = mkspread("S1", ++key, f1, s2); - OldSecurity all_secs_array[] = { f1, o1, o2, o3, s1, s2, s3, s4 }; - - MDProducer producer(Platform::SIM); - vector producers; - for (auto& sec : all_secs_array) { - with_security(sec.security_type(), sec.spread().legs_size(), [&](FixedSecurity& fsec) { - convert_security(sec, fsec, true); - producers.push_back(producer.add_security(fsec)); - }); - } - - set csymbols; - csymbols.insert(UserSymbol("F1", Exchange::TESTEXCHANGE, SecurityType::FUTURE)); - csymbols.insert(UserSymbol("O1", Exchange::TESTEXCHANGE, SecurityType::OPTION)); - csymbols.insert(UserSymbol("O2", Exchange::TESTEXCHANGE, SecurityType::OPTION)); - csymbols.insert(UserSymbol("O3", Exchange::TESTEXCHANGE, SecurityType::OPTION)); - csymbols.insert(UserSymbol("S1", Exchange::TESTEXCHANGE, SecurityType::SPREAD)); - csymbols.insert(UserSymbol("S2", Exchange::TESTEXCHANGE, SecurityType::SPREAD)); - csymbols.insert(UserSymbol("S3", Exchange::TESTEXCHANGE, SecurityType::SPREAD)); - csymbols.insert(UserSymbol("S4", Exchange::TESTEXCHANGE, SecurityType::SPREAD)); - MDConsumer consumer(csymbols); - - auto check_sec = [&](const OldSecurity& security, MDConsumerHandle handle) { - REQUIRE(security.security_key() == handle.key()); - auto iter = find_if(begin(all_secs_array), end(all_secs_array), [&](const OldSecurity& sec) { return sec.security_key() == security.security_key(); }); - REQUIRE(iter != end(all_secs_array)); - REQUIRE(SecurityHelper::symbol(*iter) == SecurityHelper::symbol(security)); - if (security.security_type() == SecurityType::SPREAD) { - REQUIRE(security.spread().normalized() == false); - REQUIRE(security.spread().has_symbol()); - REQUIRE(security.spread().legs_size() == iter->spread().legs_size()); - for (int x = 0; x < security.spread().legs_size(); ++x) { - REQUIRE(SecurityHelper::symbol(security.spread().legs(x).security()) == SecurityHelper::symbol(iter->spread().legs(x).security())); - } - } - }; - - consumer.walk_security_list(check_sec); - consumer.on_security(check_sec); - consumer.scan_securities(); - } - void test_denormalization2() { - // Just like test_denormailzation but with a different memory layout - security_key key = 999; - OldSecurity f1 = mkfuture("FF", ++key, 2016, 5, 1); - OldSecurity o1 = mkoption("OO", ++key, 2016, 5, 1, 100); - OldSecurity o2 = mkoption("OO", ++key, 2016, 5, 2, 200); - OldSecurity o3 = mkoption("OO", ++key, 2016, 5, 3, 300); - OldSecurity s4 = mkspread("SS", ++key, o2, o3); - OldSecurity s3 = mkspread("SS", ++key, o1, s4); - OldSecurity s2 = mkspread("SS", ++key, o1, s4, s3); - OldSecurity s1 = mkspread("SS", ++key, f1, s2); - OldSecurity all_secs_array[] = { f1, o1, o2, o3, s1, s2, s3, s4 }; - - MDProducer producer(Platform::SIM); - vector producers; - for (auto& sec : all_secs_array) { - with_security(sec.security_type(), sec.spread().legs_size(), [&](FixedSecurity& fsec) { - convert_security(sec, fsec, true); - producers.push_back(producer.add_security(fsec)); - }); - } - - set csymbols; - csymbols.insert(UserSymbol("FF", Exchange::TESTEXCHANGE, SecurityType::FUTURE)); - csymbols.insert(UserSymbol("OO", Exchange::TESTEXCHANGE, SecurityType::OPTION)); - csymbols.insert(UserSymbol("SS", Exchange::TESTEXCHANGE, SecurityType::SPREAD)); - MDConsumer consumer(csymbols); - - auto check_sec = [&](const OldSecurity& security, MDConsumerHandle handle) { - REQUIRE(security.security_key() == handle.key()); - auto iter = find_if(begin(all_secs_array), end(all_secs_array), [&](const OldSecurity& sec) { return sec.security_key() == security.security_key(); }); - REQUIRE(iter != end(all_secs_array)); - REQUIRE(SecurityHelper::symbol(*iter) == SecurityHelper::symbol(security)); - if (security.security_type() == SecurityType::SPREAD) { - REQUIRE(security.spread().normalized() == false); - REQUIRE(security.spread().has_symbol()); - REQUIRE(security.spread().legs_size() == iter->spread().legs_size()); - for (int x = 0; x < security.spread().legs_size(); ++x) { - REQUIRE(SecurityHelper::symbol(security.spread().legs(x).security()) == SecurityHelper::symbol(iter->spread().legs(x).security())); - } - } - }; - - consumer.walk_security_list(check_sec); - consumer.on_security(check_sec); - consumer.scan_securities(); - } - - void test_retry_unresolved_spread() { - // Make 2 spreads with option legs - // Leave out one leg for hte other spread - // The other spread only shows up once its option leg is produced - - OldSecurity o1 = mkoption("OO", 400, 2016, 5, 1, 100); - OldSecurity o2 = mkoption("OO", 401, 2016, 5, 2, 200); - OldSecurity o3 = mkoption("OO", 402, 2016, 5, 3, 300); - OldSecurity s1 = mkspread("SS", 500, o1, o2); - OldSecurity s2 = mkspread("SS", 501, o2, o3); - - MDProducer producer(Platform::SIM); - map handles; - auto add_security = [&](const OldSecurity& sec) { - with_security(sec.security_type(), sec.spread().legs_size(), [&](FixedSecurity& fsec) { - convert_security(sec, fsec, true); - handles.insert(make_pair(fsec.key, producer.add_security(fsec))); - }); - }; - // "options publisher has not started yet" - //add_security(o1); - //add_security(o2); - //add_security(o3); - add_security(s1); - add_security(s2); - - // Create consumer - set consumer_symbols; - consumer_symbols.insert(UserSymbol("OO", Exchange::TESTEXCHANGE, SecurityType::OPTION)); - consumer_symbols.insert(UserSymbol("SS", Exchange::TESTEXCHANGE, SecurityType::SPREAD)); - MDConsumer consumer(consumer_symbols); - - size_t security_count = 0; - // Indirection for check function - function check_func; - check_func = [&](const OldSecurity&) { - REQUIRE(false); - }; - consumer.on_security([&](const OldSecurity& sec, MDConsumerHandle) { ++security_count; check_func(sec); }); - consumer.scan_securities(); - REQUIRE(security_count == 0); - - // OK, now add enough options to resolve s1 but not s2 - add_security(o1); - add_security(o2); - check_func = [&](const OldSecurity& security) { - if (security.security_type() == SecurityType::SPREAD) { - set legs; - for (const auto& leg : security.spread().legs()) { - legs.insert(leg.security().security_key()); - } - REQUIRE(security.security_key() == 500); - REQUIRE(legs.size() == 2); - REQUIRE(legs.count(400) == 1); - REQUIRE(legs.count(401) == 1); - } - }; - consumer.scan_securities(); // finds o1,o2,s1 - consumer.scan_securities(); // finds nothing - REQUIRE(security_count == 3); - - // Then add the last security and we should see s2 - add_security(o3); - check_func = [&](const OldSecurity& security) { - if (security.security_type() == SecurityType::SPREAD) { - set legs; - for (const auto& leg : security.spread().legs()) { - legs.insert(leg.security().security_key()); - } - REQUIRE(security.security_key() == 501); - REQUIRE(legs.size() == 2); - REQUIRE(legs.count(401) == 1); - REQUIRE(legs.count(402) == 1); - } - }; - consumer.scan_securities(); - REQUIRE(security_count == 5); - } - - #ifndef WIN32 - void test_settlement_recorder() { - using namespace datetime; - security_key k1 = 401, k2 = 402; - OldSecurity o1 = mkoption("OO", k1, 2016, 5, 1, 100); - OldSecurity o2 = mkoption("OO", k2, 2016, 5, 2, 200); - MDProducer producer(Platform::SIM); - auto add_security = [&](const OldSecurity& sec) { - unique_ptr phandle; - with_security(sec.security_type(), sec.spread().legs_size(), [&](FixedSecurity& fsec) { - convert_security(sec, fsec, true); - phandle.reset(new MDProducerHandle(producer.add_security(fsec))); - }); - return phandle; - }; - auto ph1 = *add_security(o1); - auto ph2 = *add_security(o2); - set consumer_symbols; - consumer_symbols.insert(UserSymbol("OO", Exchange::TESTEXCHANGE, SecurityType::OPTION)); - MDConsumer consumer(consumer_symbols); - auto h1 = consumer.get_handle(k1); - REQUIRE(h1.is_initialized()); - auto h2 = consumer.get_handle(k2); - - // Pick 2 valid dates in the future - auto today = datetime::today(); - today.year += 1; - auto yesterday = datetime::add_days(today, -1); - - { - co2::SettlementRecorder recorder(Platform::UNSET); - recorder.add_settlement(ph1, 101, yesterday); - recorder.add_settlement(ph1, 102, today); - recorder.add_settlement(ph2, 199, yesterday); - recorder.add_settlement(ph2, 188, today); - recorder.do_work(); - recorder.do_work(); - } - REQUIRE(h1.misc().settlement_price == 102); - REQUIRE(h1.misc().settlement_date == today); - REQUIRE(h1.misc().previous_settlement_price == 101); - REQUIRE(h1.misc().previous_settlement_date == yesterday); - REQUIRE(h2.misc().settlement_price == 188); - REQUIRE(h2.misc().settlement_date == today); - REQUIRE(h2.misc().previous_settlement_price == 199); - REQUIRE(h2.misc().previous_settlement_date == yesterday); - - // Update but skip persist - auto tomorrow = datetime::add_days(today, 1); - ph1.update_settlement(TimeStamp::now(), 103, tomorrow); - REQUIRE(h1.misc().settlement_price == 103); - REQUIRE(h1.misc().settlement_date == tomorrow); - REQUIRE(h1.misc().previous_settlement_price == 102); - REQUIRE(h1.misc().previous_settlement_date == today); - ph1.update_previous_settlement(TimeStamp::now(), 0, Date()); - REQUIRE(h1.misc().settlement_price == 103); - REQUIRE(h1.misc().settlement_date == tomorrow); - ph1.update_settlement(TimeStamp::now(), 0, Date()); - REQUIRE(h1.misc().settlement_price == 0); - REQUIRE(h1.misc().settlement_date.day == 0); - - // pushing a new settlement for today doesn't invalidate yesterday - ph2.update_settlement(TimeStamp::now(), 201, today); - REQUIRE(h2.misc().settlement_price == 201); - REQUIRE(h2.misc().settlement_date == today); - REQUIRE(h2.misc().previous_settlement_price == 199); - REQUIRE(h2.misc().previous_settlement_date == yesterday); - - // Restore persisted state - { - co2::SettlementRecorder recorder(Platform::UNSET); - recorder.restore_settlement(ph1); - recorder.restore_settlement(ph2); - recorder.do_work(); - } - REQUIRE(h1.misc().settlement_price == 102); - REQUIRE(h1.misc().settlement_date == today); - REQUIRE(h1.misc().previous_settlement_price == 101); - REQUIRE(h1.misc().previous_settlement_date == yesterday); - REQUIRE(h2.misc().settlement_price == 188); - REQUIRE(h2.misc().settlement_date == today); - REQUIRE(h2.misc().previous_settlement_price == 199); - REQUIRE(h2.misc().previous_settlement_date == yesterday); - } - #endif - - void teardown() {} -} ; -std::unique_ptr co2_tests; - -TEST_CASE("CO2Tests","[md]") { - - if (!co2_tests) - co2_tests = std::make_unique(); - - co2_tests->setup(); - - #ifndef WIN32 - SECTION("test_security_id_recorder") { - co2_tests->test_security_id_recorder(); - } - #endif - SECTION("test_price_size") { - co2_tests->test_price_size(); - } - SECTION("test_slow_double_conversion") { - co2_tests->test_slow_double_conversion(); - } - SECTION("test_compact_exponent") { - co2_tests->test_compact_exponent(); - } - SECTION("test_producer_consumer") { - co2_tests->test_producer_consumer(); - } - SECTION("test_consumer_callback_on_security_added") { - co2_tests->test_consumer_callback_on_security_added(); - } - SECTION("test_consumer_defer_handle1") { - co2_tests->test_consumer_defer_handle1(); - } - SECTION("test_consumer_defer_handle2") { - co2_tests->test_consumer_defer_handle2(); - } - SECTION("test_spread_resolver") { - co2_tests->test_spread_resolver(); - } - SECTION("test_no_double_security_callback") { - co2_tests->test_no_double_security_callback(); - } - SECTION("test_recovery") { - co2_tests->test_recovery(); - } - SECTION("test_no_block_forever_on_publisher_crash") { - co2_tests->test_no_block_forever_on_publisher_crash(); - } - SECTION("test_get_handles") { - co2_tests->test_get_handles(); - } - SECTION("test_denormalization") { - co2_tests->test_denormalization(); - } - SECTION("test_denormalization2") { - co2_tests->test_denormalization2(); - } - SECTION("test_retry_unresolved_spread") { - co2_tests->test_retry_unresolved_spread(); - } - #ifndef WIN32 - SECTION("test_settlement_recorder") { - co2_tests->test_settlement_recorder(); - } - #endif -} - -struct Co2IndexTests { - vector random_ids; - set used_ids; - const int iterations; - tbb::task_scheduler_init _tbb; - - Co2IndexTests() : - random_ids(), - used_ids(), - iterations(50), - _tbb(tbb::task_scheduler_init::automatic) - {} - - void setup() { - used_ids.clear(); - random_ids.clear(); - - std::uniform_int_distribution rand_distribution(1, 0x7FFFFFFF); - std::mt19937 random_number_engine; - auto genrandom = std::bind(rand_distribution, random_number_engine); - set temp_ids; - for (int x = 0; x < 9999; ++x) { - auto r = genrandom(); - if (x < 1000) { used_ids.insert(r); } - temp_ids.insert(r); - } - for (uint64_t id : temp_ids) { - random_ids.push_back(id); - } - REQUIRE(random_ids.size() > 1000); - } - - struct Handle { - Handle() : _key(0) {} - uint64_t _key; - security_key key() const { return _key; } - void key(security_key key) { _key = key; } - }; - - struct SecurityData { - uint64_t exchange_id; - OldSecurity security; - Handle handle; - }; - - template - void gen_securities(Index& index) { - unique_ptr temp; - uint32_t count = 0; - for (auto& id : used_ids) { - ++count; - temp.reset(new SecurityData()); - temp->security.Clear(); - temp->security.set_security_type(SecurityType::OPTION); - temp->security.mutable_option()->set_symbol("FOOX"); - temp->security.set_numeric_id(id); - temp->exchange_id = id; - temp->handle.key(count); - index.add_security(id, move(temp)); - } - } - - - template - void test_random_search(Index& index) { - vector temp(random_ids); - std::random_shuffle(temp.begin(), temp.end()); - for (auto& id : temp) { - if (used_ids.find(id) != used_ids.end()) { - REQUIRE(index.has_security(id)); - } - else { - REQUIRE(index.get_security(id) == nullptr); - } - } - } - - template - void test_walk(Index& index) { - int count = 0; - index.walk_security_list([&count](const SecurityData&) { ++count; }); - REQUIRE(count == (int) used_ids.size()); - } - - typedef SimpleSecurityIndex MySimpleIndex; - void test_simple_index_search() { - MySimpleIndex index; - gen_securities(index); - for (int x = 0; x < iterations; ++x) { - test_random_search(index); - } - } - - void test_simple_index_parallel_search() { - MySimpleIndex index; - gen_securities(index); - tbb::parallel_for(0, iterations, [&](int) { - test_random_search(index); - }); - } - - void test_simple_index_iterate() { - MySimpleIndex index; - gen_securities(index); - test_walk(index); - } - - typedef ConcurrentSecurityIndex MyConcurrentIndex; - void test_concurrent_index_search() { - MyConcurrentIndex index; - gen_securities(index); - for (int x = 0; x < iterations; ++x) { - test_random_search(index); - } - } - void test_concurrent_index_parallel_search() { - MyConcurrentIndex index; - gen_securities(index); - tbb::parallel_for(0, iterations, [&](int) { - test_random_search(index); - }); - } - void test_concurrent_index_iterate() { - MyConcurrentIndex index; - gen_securities(index); - test_walk(index); - } -} ; - -std::unique_ptr co2_index_tests; - -TEST_CASE ("Co2IndexTests","[md]") -{ - if (!co2_index_tests) - co2_index_tests = std::make_unique (); - - co2_index_tests->setup(); - - SECTION("test_simple_index_search") { - co2_index_tests->test_simple_index_search(); - } - - SECTION("test_simple_index_parallel_search") { - co2_index_tests->test_simple_index_parallel_search(); - } - - SECTION("test_simple_index_iterate") { - co2_index_tests->test_simple_index_iterate(); - } - SECTION("test_concurrent_index_search") { - co2_index_tests->test_concurrent_index_search(); - } - SECTION("test_concurrent_index_iterate") { - co2_index_tests->test_concurrent_index_iterate(); - } - SECTION("test_concurrent_index_parallel_search") { - co2_index_tests->test_concurrent_index_parallel_search(); - } -} - -struct Co2PerfTests { - static const size_t instrument_count = 2048; - vector> test_instruments; - vector producers; - vector consumers; - - Co2PerfTests() : - test_instruments(), - producers(), - consumers() - { -#ifdef _WIN32 -#pragma warning(push, 0) -#endif - // OK if this isn't aligned for testing - for (size_t x = 0; x < instrument_count; ++x) { - test_instruments.push_back( - std::unique_ptr( - new Co2TestInstrument("Foo", SecurityType::OPTION))); - } -#ifdef _WIN32 -#pragma warning(pop) -#endif - for (size_t x = 0; x < test_instruments.size(); ++x) { - auto producer = test_instruments[x]->producer(); - producer.set_key(x + 1); - producers.push_back(producer); - auto consumer = test_instruments[x]->consumer(); - REQUIRE(consumer.key() == x + 1); - consumers.push_back(consumer); - } - - std::srand(std::time(0)); - //std::random_shuffle(producers.begin(), producers.end(), [](int i) { return std::rand() % i; }); - std::random_shuffle(consumers.begin(), consumers.end(), [](int i) { return std::rand() % i; }); - } - void setup() { } - void teardown() {}; - void test_write_bbo_perf() { - REQUIRE(producers.size() == instrument_count); - - // Warmup - for (size_t x = 0; x < producers.size(); ++x) { - auto& p = producers[x]; - // p.update_bbo(TimeStamp::now(), PriceSize::from_double<3>(100.0 + x, x), PriceSize::from_double<3>(200 + x, 2 * x)); - p.update_bbo(TimeStamp::now(), PriceSize(100+x,0,x), PriceSize(200+x,0,2*x)); - } - - // Test - TimeStamp start = TimeStamp::now(); - size_t count = 0; - const size_t iterations = 1000; - for (size_t loop = 0; loop < iterations; ++loop) { - for (size_t x = 0; x < producers.size(); ++x) { - ++count; - auto& p = producers[x]; - TimeStamp t = TimeStamp::now(); - // p.update_bbo(t, PriceSize::from_double<3>(100.0 + count, count), PriceSize::from_double<3>(200 + count, 2 * count)); - p.update_bbo(t, PriceSize(100+x,0,x), PriceSize(200+x,0,2*x)); - } - } - TimeStamp end = TimeStamp::now(); - std::stringstream ss; - // Of which approximately 1/3 is generating the timestamp on lab-lx4 - ss << "\t" << (end - start).nanoseconds() / iterations / instrument_count << " ns per update *\n"; - std::cout << ss.str(); - } - - void test_read_bbo_perf() { - // Warmup - const auto& write_all = [](vector& producers) { - // Offset each price by a random amount each iteration to ensure that writes actually happen - size_t rand_factor = std::rand() % 1024; - for (size_t x = 0; x < producers.size(); ++x) { - auto& p = producers[x]; - // p.update_bbo(TimeStamp::now(), PriceSize::from_double<3>(100.0 + x + rand_factor, x+rand_factor), PriceSize::from_double<3>(200 + x+rand_factor, 2 * x + rand_factor)); - p.update_bbo(TimeStamp::now(), PriceSize(100+x+rand_factor,0,x+rand_factor), PriceSize(200+x+rand_factor,0,2*x+rand_factor)); - } - }; - write_all(producers); - - for (auto& c : consumers) { - PriceSize bid, ask; - TimeStamp ts; - REQUIRE(c.get_bbo(ts, bid, ask) == HandleReadResult::SUCCESS); - } - - // Start a thread which loops and writes to the producers until terminated - utils::Dispatcher& writer = utils::Dispatcher::create_new("bbo_writer"); - DEFER(writer.terminate()); - tbb::queuing_mutex producer_mutex; - auto* pproducers = &producers; - function write_all_repeat = [&]() { - tbb::queuing_mutex::scoped_lock lock(producer_mutex); - if (pproducers != nullptr) { - write_all(*pproducers); - writer.post(write_all_repeat); - } - }; - write_all_repeat(); - - // Perform read test - TimeStamp start = TimeStamp::now(); - size_t read_count = 0; - const size_t iterations = 1000; - for (size_t loop = 0; loop < iterations; ++loop) { - TimeStamp ts = TimeStamp::now(); - for (size_t x = 0; x < consumers.size(); ++x) { - PriceSize bid, ask; - TimeStamp ts_temp = ts; - if (consumers[x].get_bbo(ts_temp, bid, ask) == HandleReadResult::SUCCESS) { - ++read_count; - } - } - } - REQUIRE(read_count > 0); - - TimeStamp end = TimeStamp::now(); - std::stringstream ss; - ss << "\t" << (end - start).nanoseconds() / iterations / instrument_count << " ns per update. Actual reads = " << read_count << " / " << instrument_count*iterations << " * \n"; - std::cout << ss.str(); - - writer.terminate(); - - tbb::queuing_mutex::scoped_lock lock(producer_mutex); - pproducers = nullptr; - std::this_thread::sleep_for(2s); - - } - -} ; - -std::unique_ptr co2_perf_tests; - -const size_t Co2PerfTests::instrument_count ; -TEST_CASE ("Co2PerfTests","[md]") -{ - - if (!co2_perf_tests) - co2_perf_tests = std::make_unique(); - - co2_perf_tests->setup(); - - SECTION("test_write_bbo_perf") { - // co2_perf_tests->test_write_bbo_perf(); - } - SECTION("test_read_bbo_perf") { - // co2_perf_tests->test_read_bbo_perf(); - } -} - -// struct Co2RelayTests { +// #ifndef NO_CPP0X + +// #include "catch/catch.hpp" +// #include "../SettlementRecorder.h" +// #include "../coconut2.h" +// #include "../relay/QueryServer.h" +// #include "Co2TestInstrument.h" +// #include "lib/config/config.h" +// #include "lib/os/os.h" +// #include "lib/security_model/SecurityHelper.h" +// #include "lib/utils/Dispatcher.h" +// #include "lib/utils/defer.h" +// #include "lib/utils/warn_ctrl.h" +// #include "lib/utils/bts_chrono.h" + +// #include "lib/config/protos/MarketData.pb.h" + +// #include +// #include +// #include +// #include +// #include +// #include +// #include +// #include +// #include +// #include +// #include + + +// using namespace bts::md2; +// using namespace bts::md; +// using namespace std; + +// namespace bts { +// namespace md2 { +// namespace test { + +// // +// // Helper functions for Producer/Consumer tests +// // +// vector create_test_securities(MDProducer& producer, const char* postfix ="") { +// FixedOptionSecurity fos; +// FixedFutureSecurity ffs; +// ffs.symbol("FOO"+string(postfix)); +// fos.symbol("FOOX"+string(postfix)); +// ffs.key = invalid_security_key; +// fos.key = invalid_security_key; +// ffs.expiration_year = fos.expiration_year = 2015; +// ffs.expiration_day = fos.expiration_day = 27; +// fos.strike = 1000; +// fos.call_put = PutOrCall::CALL; +// // Add 12 expirations to 2 different shared memory regions +// vector result; +// for (uint32_t x = 1; x <= 12; ++x) { +// ffs.expiration_month = fos.expiration_month = x; +// result.push_back(producer.add_security(ffs)); +// result.push_back(producer.add_security(fos)); +// // In a real app these securities would now be passed to the SecurityIDRecorder +// } +// return result; +// } + +// set get_test_symbols(const char* postfix="") { +// set symbols; +// UserSymbol future_symbol(string("FOO")+postfix, Exchange::TESTEXCHANGE, SecurityType::FUTURE); +// symbols.insert(future_symbol); +// UserSymbol option_symbol(string("FOOX")+postfix, Exchange::TESTEXCHANGE, SecurityType::OPTION); +// symbols.insert(option_symbol); +// return symbols; +// } + +// OldSecurity mkfuture(string symbol, security_key key, int year, int month, int day) { +// OldSecurity result; +// result.set_security_type(SecurityType::FUTURE); +// SecurityHelper::set_symbol(result, symbol); +// result.set_exchange(Exchange::TESTEXCHANGE); +// result.set_security_key(key); +// result.mutable_future()->set_expiration_day(day); +// result.mutable_future()->set_expiration_month(month); +// result.mutable_future()->set_expiration_year(year); +// return result; +// } +// OldSecurity mkoption(string symbol, security_key key, int year, int month, int day, double strike) { +// OldSecurity result; +// result.set_security_type(SecurityType::OPTION); +// SecurityHelper::set_symbol(result, symbol); +// result.set_exchange(Exchange::TESTEXCHANGE); +// result.set_security_key(key); +// result.mutable_option()->set_expiration_day(day); +// result.mutable_option()->set_expiration_month(month); +// result.mutable_option()->set_expiration_year(year); +// result.mutable_option()->set_strike(strike); +// result.mutable_option()->set_call_put(PutOrCall::PUT); +// return result; +// } +// FixedOptionSecurity mkfixedoption(string symbol, security_key key, int year, int month, int day, double strike, PutOrCall::type callput) { +// FixedOptionSecurity fos; +// fos.type = SecurityType::OPTION; +// fos.exchange = Exchange::TESTEXCHANGE; +// fos.key = key; +// fos.strike = strike; +// fos.expiration_day = day; +// fos.expiration_month = month; +// fos.expiration_year = year; +// fos.call_put = callput; +// fos.symbol(symbol); +// return fos; +// } +// OldSecurity mkspread(string symbol, security_key key, const vector& legs) { +// OldSecurity result; +// result.set_security_type(SecurityType::SPREAD); +// SecurityHelper::set_symbol(result, symbol); +// result.set_exchange(Exchange::TESTEXCHANGE); +// result.set_security_key(key); +// for (const auto& leg : legs) { +// auto* pleg = result.mutable_spread()->add_legs(); +// pleg->mutable_security()->CopyFrom(leg); +// } +// return result; +// } +// OldSecurity mkspread(string symbol, security_key key, OldSecurity leg1, OldSecurity leg2) { +// OldSecurity legs[] = { leg1, leg2 }; +// vector vlegs(begin(legs), end(legs)); +// return mkspread(symbol, key, vlegs); +// } +// OldSecurity mkspread(string symbol, security_key key, OldSecurity leg1, OldSecurity leg2, OldSecurity leg3) { +// OldSecurity legs[] = { leg1, leg2, leg3 }; +// vector vlegs(begin(legs), end(legs)); +// return mkspread(symbol, key, vlegs); +// } + +// void assign_ids(vector::iterator begin, vector::iterator end, security_key start_key = 1000) { +// for (; begin != end; ++begin) { +// (*begin).set_key(++start_key); +// } +// } + +// class Co2Tests +// { +// public: // void setup() { // config::internal::initialize_for_test( // "PlatformMap {" @@ -1364,177 +147,1394 @@ TEST_CASE ("Co2PerfTests","[md]") // " exchange: TESTEXCHANGE" // " }" // " }" -// "}"); +// "}" +// "PublisherOptions {" +// " value {" +// " heartbeat_ms: 100" +// " heartbeat_boot_seconds: 1" +// " }" +// "}" +// ); // } -// void teardown() {} -// MDConsumer create_consumer() { +// #ifndef WIN32 +// void test_security_id_recorder() { +// // Verify callbacks happen after work complete +// SecurityIDRecorder idsvc, idsvc2; +// security_key result = invalid_security_key; +// std::string desc = "FOOX 1"; +// idsvc.get_key(desc, SecurityType::FUTURE, [](void* arg, security_key key) { +// security_key* presult = (security_key*)arg; +// *presult = key; +// }, &result); +// idsvc.do_work(); +// REQUIRE(result != invalid_security_key); +// security_key result2 = invalid_security_key; +// idsvc2.get_key(desc, SecurityType::OPTION, [](void* arg, security_key key) { +// *((security_key*)arg) = key; +// }, &result2); +// idsvc2.do_work(); +// REQUIRE(result == result2); +// } +// #endif + +// void test_price_size() { +// // PriceSize violates some type punning rules +// // So it is important to have this test work correctly on the target platform +// PriceSize p, p2; +// p = PriceSize::from_double<2>(4.5, 14); +// SingleWriterPriceSizeStore memory; +// memory.store(p); +// p2 = memory.load(); +// REQUIRE(p.price() == 4.5); +// REQUIRE(p.size() == 14); +// REQUIRE(p2.price() == 4.5); +// REQUIRE(p2.size() == 14); + +// p = PriceSize::from_double<2>(4.0, 5); +// memory.store(p); +// p2 = memory.load(); +// REQUIRE(p2.price() == 4.0); +// REQUIRE(p2.size() == 5); + +// p = PriceSize::from_double<2>(-4.2, 6); +// memory.store(p); +// p2 = memory.load(); +// REQUIRE(p2.price() == -4.2); +// REQUIRE(p2.size() == 6); + +// p = PriceSize(4, -1, 2); +// memory.store(p); +// p2 = memory.load(); +// REQUIRE(p2.price() == 0.4); +// REQUIRE(p2.size() == 2); + +// // Test requirement for more than 32 bit mantissa +// double bigval = -9999.9921875; +// p = PriceSize::from_double<7>(bigval, 42); +// memory.store(p); +// p2 = memory.load(); +// REQUIRE(p2.price() == bigval); +// REQUIRE(p2.size() == 42); +// } + +// void test_slow_double_conversion() { +// double values[] = { 1.5, 0.5, 0.2155, 0.216, 42.1 }; +// int expected[] = { -1, -1, -4, -3, -1 }; +// static_assert(BTS_ARRAYSIZE(values) == BTS_ARRAYSIZE(expected), ""); +// for (size_t x = 0; x < BTS_ARRAYSIZE(values); ++x) { +// REQUIRE(to_exponent(values[x]) == expected[x]); +// } + +// // Tests powers of 10. Important for display_factor conversion +// for (int x = 8; x > -8; --x) { +// double d = pow10(x); +// REQUIRE(to_exponent(d) == x); +// } +// } + +// struct ExchangeDecimal { +// int64_t mantissa; +// int32_t exponent; +// }; +// bool close_enough(double a, double b) { +// // We need at least 7 places but we're testing with 10 just in case +// const double acceptable_error = 1E-12; +// double delta = abs(a - b); +// // Equal if the delta is less than error +// return delta < acceptable_error; +// } +// void test_compact_exponent() { +// ExchangeDecimal test_values[] = { +// { 2030000000000, -9 }, // mantissa > pow(2,40) +// { -2030000000000, -9 }, // mantissa > pow(2,40) +// { 2022750000000, -9 },// mantissa > pow(2,40) +// { -2022750000000, -9 },// mantissa > pow(2,40) +// { 20227500000000, -10 },// mantissa > pow(2,40) +// { 202275000000000, -11 },// mantissa > pow(2,40) +// { 2022750000000000, -12 },// mantissa > pow(2,40) +// { 20227500000000000, -13 },// mantissa > pow(2,40) +// { -20227500000000000, -13 },// mantissa > pow(2,40) +// { 203, 1}, // control +// { -14000000000, -8 }, // control +// { -14, 1 }, // control +// { 20227500, -4 }, // control +// }; +// for (auto p : test_values) { +// double expected = p.mantissa * pow10(p.exponent); +// compact_mantissa(p.mantissa, p.exponent); +// double compacted = p.mantissa * pow10(p.exponent); +// REQUIRE(close_enough(expected, compacted)); +// } +// } + +// void test_producer_consumer() { +// MDProducer producer(Platform::SIM); +// vector producers = create_test_securities(producer); + +// // Add bbo data for those expirations +// auto write_time = TimeStamp::now(); +// for (auto& p : producers) { +// p.update_bid(write_time, PriceSize::from_double<2>(5.0, 5)); +// p.update_ask(write_time, PriceSize::from_double<2>(5.0, 5)); +// } + +// // Symbol list for the consumer // set symbols; -// UserSymbol future_symbol("FOO", Exchange::TESTEXCHANGE, SecurityType::FUTURE); +// UserSymbol future_symbol( "FOO", Exchange::TESTEXCHANGE, SecurityType::FUTURE ); // symbols.insert(future_symbol); -// UserSymbol option_symbol("FOOX", Exchange::TESTEXCHANGE, SecurityType::OPTION); +// UserSymbol option_symbol( "FOOX", Exchange::TESTEXCHANGE, SecurityType::OPTION ); // symbols.insert(option_symbol); // MDConsumer consumer(symbols); -// return consumer; + +// vector consumer_securities; +// // Poll for security list - none visible until they have keys +// size_t count = consumer.walk_security_list([&](const OldSecurity& security, MDConsumerHandle) {consumer_securities.push_back(security);}); +// // Since no security has an id none are "public" +// REQUIRE(consumer_securities.size() == count); +// REQUIRE(count == 0); +// // Assign a unique ID to each security (from the producer) +// assign_ids(producers.begin(), producers.end()); +// // Now securities can be enumerated by the client +// count = consumer.walk_security_list([&](const OldSecurity& security, MDConsumerHandle) {consumer_securities.push_back(security);}); +// REQUIRE(consumer_securities.size() == count); +// REQUIRE(count == 24); + +// vector consumers; +// for(const auto& security : consumer_securities) { +// consumers.push_back(consumer.get_handle(security.security_key())); +// } + +// for (const auto& c : consumers) { +// PriceSize bid, ask; +// TimeStamp read_time; +// // get_boo returns true becuase this time is after the change +// REQUIRE(c.get_bbo(read_time, bid, ask) == HandleReadResult::SUCCESS); +// REQUIRE((read_time - TimeStamp()).seconds() > 0); +// REQUIRE(bid.price() == 5.0); +// REQUIRE(ask.price() == 5.0); +// REQUIRE(bid.size() == 5); +// REQUIRE(ask.size() == 5); +// } + +// // Second query before a write has happend finds no updates +// auto read_time = TimeStamp::now(); +// for (const auto& c : consumers) { +// PriceSize bid, ask; +// REQUIRE(c.get_bbo(read_time, bid, ask) != HandleReadResult::SUCCESS); +// } + +// // update everything once more +// write_time = TimeStamp::now(); +// for (auto& p : producers) { +// // Update bid/ask with one call +// p.update_bbo(write_time, PriceSize::from_double<2>(6.55, 6), PriceSize::from_double<2>(6.55, 6)); +// } +// // Now read_time is < write_time +// for (const auto& c : consumers) { +// TimeStamp modified_time = read_time; +// PriceSize bid, ask; +// // get_bbo returns true becuase this time is after the change +// REQUIRE(c.get_bbo(modified_time, bid, ask) == HandleReadResult::SUCCESS); +// REQUIRE( (modified_time - read_time).seconds() > 0); +// REQUIRE(bid.price() == 6.55); +// REQUIRE(ask.price() == 6.55); +// REQUIRE(bid.size() == 6); +// REQUIRE(ask.size() == 6); +// } // } - -// template -// T simulate_wire_transfer(const T& request) { -// T temp; -// std::string buffer; -// request.SerializeToString(&buffer); -// temp.ParseFromString(buffer); -// return temp; -// } - -// PriceSize convert(const co2::PriceSizeMessage& psm) { -// return PriceSize(psm.mantissa(), psm.exponent(), psm.size()); -// } - -// void relay_nans() { +// // When a security is added the consumer should provide callback +// void test_consumer_callback_on_security_added() { // MDProducer producer(Platform::SIM); -// UserSymbol symbol("FOO", Exchange::TESTEXCHANGE, SecurityType::OPTION); - -// // initialize data -// vector producers; -// for (size_t x = 0; x < 100; ++x) { -// auto strike = 1000 + (100 * x); -// producers.push_back( -// producer.add_security( -// mkfixedoption("FOO", invalid_security_key, 2017, 1, 28, strike, PutOrCall::PUT))); -// producers.push_back( -// producer.add_security( -// mkfixedoption("FOO", invalid_security_key, 2017, 1, 28, strike, PutOrCall::CALL))); -// } -// assign_ids(producers.begin(), producers.end(), 1000); -// PriceSize price35 = PriceSize::from_double<2>(3.5, 5); -// PriceSize price5 = PriceSize::from_double<2>(5, 5); -// for(auto p : producers) { -// p.update_bbo(TimeStamp::now(), price35, price35); -// } +// vector producers = create_test_securities(producer); -// // Read using QueryServer +// // Symbol list for the consumer // set symbols; -// symbols.insert(symbol); -// auto server = create_query_server(symbols); -// REQUIRE(server); -// server->scan_securities(); +// UserSymbol future_symbol( "FOO", Exchange::TESTEXCHANGE, SecurityType::FUTURE ); +// symbols.insert(future_symbol); +// UserSymbol option_symbol( "FOOX", Exchange::TESTEXCHANGE, SecurityType::OPTION ); +// symbols.insert(option_symbol); +// MDConsumer consumer(symbols); -// co2::MDRequest request; -// auto* chan = request.add_channels(); -// chan->set_type(symbol.type()); -// chan->set_symbol(symbol.symbol()); -// chan->set_exchange(symbol.exchange()); -// chan->set_flags(co2::MDRequest::BBO | co2::MDRequest::SECURITY); +// // Add the on_security callback +// vector securities; +// consumer.on_security([&](const OldSecurity& security, MDConsumerHandle handle) { +// REQUIRE(security.security_key() != invalid_security_key); +// REQUIRE(security.security_key() == handle.key()); +// securities.push_back(security); +// }); +// // No securities have ids yet +// REQUIRE(securities.size() == 0); +// // Check for new securities +// consumer.scan_securities(); +// // Still none +// REQUIRE(securities.size() == 0); +// producers.front().set_key(1001); +// consumer.scan_securities(); +// REQUIRE(securities.size() == 1); -// // This request should include everything -// co2::MDReply reply; -// server->query(request, &reply); -// reply = simulate_wire_transfer(reply); -// REQUIRE((size_t)reply.channels().size() == symbols.size()); -// REQUIRE((size_t)reply.channels(0).updates().size() == producers.size()); -// // update the request to match the server time -// for (auto& update : reply.channels(0).updates()) { -// REQUIRE(update.has_security()); -// REQUIRE(update.has_book()); -// REQUIRE(update.book().asks_size() > 0); -// REQUIRE(update.book().bids_size() > 0); -// REQUIRE(convert(update.book().bids(0)) == price35); -// REQUIRE(convert(update.book().asks(0)) == price35); +// // Assign the rest of the ids +// assign_ids(producers.begin() + 1, producers.end(), producers.front().security().key); +// consumer.scan_securities(); +// REQUIRE(securities.size() == producers.size()); +// } + +// // If an MDConsumer is asked for a handle to a security which does not exist in the system +// // The consumer should return a handle which always returns false on all calls but wakes up +// // when the security is added. +// // * Case 1 - the shared memory has been created but the security has not been discovered +// // * Case 2 - The producer has never been created +// void test_consumer_defer_handle1() { +// MDProducer producer(Platform::SIM); +// vector producers = create_test_securities(producer); + +// set symbols; +// UserSymbol future_symbol( "FOO", Exchange::TESTEXCHANGE, SecurityType::FUTURE ); +// symbols.insert(future_symbol); +// UserSymbol option_symbol( "FOOX", Exchange::TESTEXCHANGE, SecurityType::OPTION ); +// symbols.insert(option_symbol); +// MDConsumer consumer(symbols); + +// // Get a handle to 24 consumers which haven't been created yet +// vector consumers; +// vector> managed_consumers; +// security_key key = 1; +// for(; key < 24; ++key) { +// consumers.push_back(consumer.get_handle(key+1000)); +// managed_consumers.push_back(consumer.get_managed_handle(key+1000)); // } + +// // Set bbo on the producers - still not public +// TimeStamp write_time = TimeStamp::now(); +// for (auto& p : producers) { +// p.update_bbo(write_time, PriceSize::from_double<3>(4.5, 5), PriceSize::from_double<3>(6.75, 7)); +// } +// // Even though bbo is available it is not given out because the producing security does not have an id +// PriceSize bid, ask; +// for(auto& c : consumers) { +// TimeStamp zerotime; +// REQUIRE(c.get_bbo(zerotime, bid, ask) != HandleReadResult::SUCCESS); +// } +// for(auto& mc : managed_consumers) { +// TimeStamp zerotime; +// REQUIRE(mc->get_bbo(zerotime, bid, ask) != HandleReadResult::SUCCESS); +// } + +// assign_ids(producers.begin(), producers.end()); +// consumer.scan_securities(); +// size_t bbo_count = 0; +// // Existing old handles remain invalid +// for(auto& c : consumers) { +// TimeStamp zerotime; +// if (c.get_bbo(zerotime, bid, ask) == HandleReadResult::SUCCESS) { +// bbo_count++; +// } +// } +// REQUIRE(bbo_count == 0); +// // Now, magically, the existing (managed) handles are valid +// for(auto& mc : managed_consumers) { +// TimeStamp zerotime; +// if (mc->get_bbo(zerotime, bid, ask) == HandleReadResult::SUCCESS) { +// bbo_count++; +// } +// } +// REQUIRE(bbo_count > 0); +// } + + +// // It is legal and valid to create a consumer before the producer process has started +// // Handles given out by that consumer will become valid once the securities are discovered +// void test_consumer_defer_handle2() { +// set symbols; +// UserSymbol future_symbol( "FOO", Exchange::TESTEXCHANGE, SecurityType::FUTURE ); +// symbols.insert(future_symbol); +// UserSymbol option_symbol( "FOOX", Exchange::TESTEXCHANGE, SecurityType::OPTION ); +// symbols.insert(option_symbol); +// MDConsumer consumer(symbols); + +// // Get a handle to 24 consumers which haven't been created yet +// vector consumers; +// vector> managed_consumers; +// security_key key = 1; +// for(; key < 24; ++key) { +// consumers.push_back(consumer.get_handle(key+1000)); +// managed_consumers.push_back(consumer.get_managed_handle(key+1000)); +// } + +// // Even though bbo is available it is not given out because the producing security does not have an id +// PriceSize bid, ask; +// for(auto& c : consumers) { +// TimeStamp zerotime; +// REQUIRE(c.get_bbo(zerotime, bid, ask) != HandleReadResult::SUCCESS); +// } +// for(auto& mc : managed_consumers) { +// TimeStamp zerotime; +// REQUIRE(mc->get_bbo(zerotime, bid, ask) != HandleReadResult::SUCCESS); +// } + +// // Only NOW create the producers (and the shared memory) +// MDProducer producer(Platform::SIM); +// vector producers = create_test_securities(producer); +// // Set bbo on the producers - still not public +// TimeStamp write_time = TimeStamp::now(); +// for (auto& p : producers) { +// p.update_bbo(write_time, PriceSize::from_double<3>( 4.5, 5 ), PriceSize::from_double<3>(6.75, 7)); +// } + +// assign_ids(producers.begin(), producers.end()); +// consumer.scan_securities(); +// size_t bbo_count = 0; +// // Existing old handles remain invalid +// for(auto& c : consumers) { +// TimeStamp zerotime; +// if (c.get_bbo(zerotime, bid, ask) == HandleReadResult::SUCCESS) { +// bbo_count++; +// } +// } +// REQUIRE(bbo_count == 0); +// // Now, magically, the existing (managed) handles are valid +// for(auto& mc : managed_consumers) { +// TimeStamp zerotime; +// if (mc->get_bbo(zerotime, bid, ask) == HandleReadResult::SUCCESS) { +// bbo_count++; +// } +// } +// REQUIRE(bbo_count > 0); +// } + +// void test_spread_resolver() { +// SpreadResolver resolver; +// vector legs; +// legs.push_back("1"); +// legs.push_back("2"); +// legs.push_back("3"); +// legs.push_back("4"); +// resolver.add("1234", legs); +// legs.clear(); +// legs.push_back("4"); +// legs.push_back("5"); +// resolver.add("45", legs); +// auto key_map = [](string& key) -> security_key { +// if (key == "1") return 1; +// if (key == "2") return 2; +// if (key == "3") return 3; +// if (key == "4") return 4; +// if (key == "5") return invalid_security_key; +// REQUIRE(false); +// return 0; +// }; +// auto key_map2 = [](string& key) -> security_key { +// if (key == "4") return 4; +// if (key == "5") return 5; +// REQUIRE(false); +// return 0; +// }; + +// // 2 unresolved spreads +// REQUIRE(resolver.count() == 2); +// resolver.resolve(key_map, [&](const string& key, const vector& legs) { +// REQUIRE(key == "1234"); +// REQUIRE(legs.size() == 4); +// }); +// // 1 spread remains +// REQUIRE(resolver.count() == 1); +// resolver.resolve(key_map2, [&](const string& key, const vector& legs) { +// REQUIRE(key == "45"); +// REQUIRE(legs.size() == 2); +// }); +// REQUIRE(resolver.count() == 0); +// } + + +// void test_no_double_security_callback() { +// MDProducer producer(Platform::SIM); +// vector producers = create_test_securities(producer); +// // Assign ids so securities are already public in the producer +// assign_ids(producers.begin(), producers.end()); + +// // Symbol list for the consumer +// auto syms = get_test_symbols(); +// MDConsumer consumer = MDConsumer(syms); + +// // Add the on_security callback - callback fires for existing securities +// vector securities; +// consumer.on_security([&](const OldSecurity& security, MDConsumerHandle handle) { +// REQUIRE(security.security_key() != invalid_security_key); +// REQUIRE(security.security_key() == handle.key()); +// securities.push_back(security); +// }); +// // Found all securities +// REQUIRE(securities.size() == producers.size()); +// // Check for new securities +// consumer.scan_securities(); +// // Expect no duplicates +// REQUIRE(securities.size() == producers.size()); +// } + +// void test_recovery() { +// auto producer1 = make_shared(Platform::SIM); +// vector producers = create_test_securities(*producer1); +// assign_ids(producers.begin(), producers.end()); +// producer1->get_channel("FOO", SecurityType::FUTURE, Exchange::TESTEXCHANGE).heartbeat(); +// producer1->get_channel("FOOX", SecurityType::OPTION, Exchange::TESTEXCHANGE).heartbeat(); +// for (auto& pro : producers) { pro.update_bbo(TimeStamp::now(), PriceSize(1, 0, 1), PriceSize(2, 0, 2)); } + +// // Symbol list for the consumer +// MDRecoverableConsumer consumer(get_test_symbols()); +// map> products; +// consumer.on_security([&](const OldSecurity& security, MDConsumerHandle handle) { +// REQUIRE(security.security_key() == handle.key()); +// products.insert(make_pair(security.security_key(), consumer.get_managed_handle(security.security_key()))); +// }); +// consumer.scan_securities(); +// REQUIRE(products.size() == producers.size()); + +// // Choose a handle +// for (auto& pair : products) { +// auto& handle = pair.second; +// REQUIRE(handle->is_initialized()); +// REQUIRE(handle->is_heart_beating()); +// } + +// // Delete the producer +// try { +// producers.clear(); +// producer1.reset(); +// } catch(std::exception&) { +// LOG(MarketData,Error,"Failed test trying to destroy producer."); +// REQUIRE(false); +// } + +// // Wait out the heartbeat +// auto options = config::get(); +// auto ms = std::max(options.heartbeat_ms(), options.heartbeat_boot_seconds()*1000)+10; -// // Second request with updated time should include nothing -// request.mutable_channels(0)->set_time(reply.channels(0).channel().time()); -// reply.Clear(); -// server->query(request, &reply); -// reply = simulate_wire_transfer(reply); -// REQUIRE(reply.channels().size() == 1); -// REQUIRE(reply.channels(0).updates().size() == 0); +// std::this_thread::sleep_for(std::chrono::duration(ms)); -// // Update a single price and make sure we get that back -// producers[0].update_bbo(TimeStamp::now(), price5, price5, 1); -// request.mutable_channels(0)->set_time(reply.channels(0).channel().time()); -// reply.Clear(); -// server->query(request, &reply); -// reply = simulate_wire_transfer(reply); -// REQUIRE(reply.channels().size() == 1); -// REQUIRE(reply.channels(0).updates().size() == 1); -// REQUIRE(reply.channels(0).updates(0).has_book()); -// REQUIRE(convert(reply.channels(0).updates(0).book().bids(0)) == price5); -// REQUIRE(convert(reply.channels(0).updates(0).book().asks(0)) == price5); - -// // NaN out all the prices - expect to get a full set of updates -// for(auto p : producers) { -// p.update_bbo(TimeStamp::now(), PriceSize::NO_MARKET, PriceSize::NO_MARKET, 2); -// } -// request.mutable_channels(0)->set_time(reply.channels(0).channel().time()); -// reply.Clear(); -// server->query(request, &reply); -// reply = simulate_wire_transfer(reply); -// REQUIRE(reply.channels().size() == 1); -// REQUIRE((size_t)reply.channels(0).updates().size() == producers.size()); -// for (auto& update : reply.channels(0).updates()) { -// REQUIRE(update.has_book()); -// REQUIRE(update.book().bids_size() > 0); -// REQUIRE(update.book().asks_size() > 0); -// REQUIRE(convert(update.book().bids(0)) == PriceSize::NO_MARKET); -// REQUIRE(convert(update.book().asks(0)) == PriceSize::NO_MARKET); +// // Make sure heartbeat is done +// try { +// for (auto& pair : products) { +// auto& handle = pair.second; +// REQUIRE(handle->is_initialized()); +// REQUIRE(handle->is_heart_beating() == false); +// } +// } catch(std::exception&) { +// LOG(MarketData,Error,"Failed to validate heartbeat while producer is down."); +// REQUIRE(false); // } -// // Fill up the markets again -// for(auto p : producers) { -// p.update_bbo(TimeStamp::now(), price35, price35, 3); +// // Create a new publisher and attempt to recover +// shared_ptr producer2; +// try { +// producer2 = make_shared(Platform::SIM); +// } catch(std::exception&) { +// LOG(MarketData,Error,"Failed to create new producer"); +// REQUIRE(false); // } -// request.mutable_channels(0)->set_time(reply.channels(0).channel().time()); -// reply.Clear(); -// server->query(request, &reply); -// reply = simulate_wire_transfer(reply); -// REQUIRE(reply.channels().size() == 1); -// REQUIRE((size_t)reply.channels(0).updates().size() == producers.size()); -// for (auto& update : reply.channels(0).updates()) { -// REQUIRE(update.has_book()); -// REQUIRE(update.book().bids_size() > 0); -// REQUIRE(update.book().asks_size() > 0); -// REQUIRE(convert(update.book().bids(0)) == price35); -// REQUIRE(convert(update.book().asks(0)) == price35); +// producers = create_test_securities(*producer2); +// REQUIRE(products.size() == producers.size()); +// assign_ids(producers.begin(), producers.end()); +// producer2->get_channel("FOO", SecurityType::FUTURE, Exchange::TESTEXCHANGE).heartbeat(); +// producer2->get_channel("FOOX", SecurityType::OPTION, Exchange::TESTEXCHANGE).heartbeat(); + +// // set bbo to a different value +// for (auto& pro : producers) { +// pro.update_bbo(TimeStamp::now(), PriceSize(4, 0, 5), PriceSize(6, 0, 7)); // } -// // NaN out the bids -// for(auto p : producers) { -// p.update_bbo(TimeStamp::now(), PriceSize::NO_MARKET, price35, 4); +// // Attempt to recover the managed handles +// REQUIRE(consumer.try_recover()); + +// // Since we know that both securities exist they should have been updated to the new section +// for (auto& pair : products) { +// auto& handle = pair.second; +// REQUIRE(handle->is_initialized()); +// REQUIRE(handle->is_heart_beating()); +// TimeStamp tzero; +// PriceSize bid, ask; +// REQUIRE(handle->get_bbo(tzero, bid, ask) == HandleReadResult::SUCCESS); +// REQUIRE(bid.price() == 4.0); +// REQUIRE(bid.size() == 5); +// REQUIRE(ask.price() == 6.0); +// REQUIRE(ask.size() == 7); // } -// request.mutable_channels(0)->set_time(reply.channels(0).channel().time()); -// reply.Clear(); -// server->query(request, &reply); -// reply = simulate_wire_transfer(reply); -// REQUIRE(reply.channels().size() == 1); -// REQUIRE((size_t)reply.channels(0).updates().size() == producers.size()); -// for (auto& update : reply.channels(0).updates()) { -// REQUIRE(update.has_book()); -// REQUIRE(update.book().bids_size() > 0); -// REQUIRE(update.book().asks_size() > 0); -// REQUIRE(convert(update.book().bids(0)) == PriceSize::NO_MARKET); -// REQUIRE(convert(update.book().asks(0)) == price35); +// } + + +// void test_no_block_forever_on_publisher_crash() { +// #ifdef _WIN32 +// #pragma warning(push, 0) +// #endif +// // OK if this isn't aligned for testing +// auto src = std::make_unique ("Foo", SecurityType::FUTURE); +// #ifdef _WIN32 +// #pragma warning(pop) +// #endif +// TimeStamp read = TimeStamp::now(); +// src->producer().update_bbo(TimeStamp::invalid(), PriceSize::from_double<3>(100.0, 100), PriceSize::from_double<3>(101.0, 101)); +// PriceSize bid, ask; +// TimeStamp start = TimeStamp::now(); +// auto result = src->consumer().get_bbo(read, bid, ask); +// TimeStamp end = TimeStamp::now(); +// REQUIRE(result != HandleReadResult::SUCCESS); +// REQUIRE((end - start).seconds() < 5); +// } + +// void test_get_handles() { +// MDProducer producer(Platform::SIM); +// vector producers = create_test_securities(producer); +// // Add entries to the shared memory that we aren't looking for +// vector p2_no_id = create_test_securities(producer, "2"); +// vector p3 = create_test_securities(producer, "3"); + +// for (auto& p : producers) { p.update_bbo(TimeStamp::now(), PriceSize::from_double<3>(5.0, 5), PriceSize::from_double<3>(5.0, 5)); } +// assign_ids(producers.begin(), producers.end()); +// assign_ids(p3.begin(), p3.end()); + +// // Symbol list for the consumer - only search half of the symbols +// set symbols; +// UserSymbol future_symbol( "FOO", Exchange::TESTEXCHANGE, SecurityType::FUTURE ); +// symbols.insert(future_symbol); +// UserSymbol option_symbol( "FOOX", Exchange::TESTEXCHANGE, SecurityType::OPTION ); +// symbols.insert(option_symbol); +// MDConsumer consumer(symbols); + +// set keys; +// consumer.walk_security_list([&](const OldSecurity& security, MDConsumerHandle) { keys.insert(security.security_key()); }); +// REQUIRE(keys.size() == producers.size()); +// auto map = consumer.get_handles(keys); +// REQUIRE(map.size() == keys.size()); +// for (const auto& entry : map) { +// REQUIRE(entry.second.is_initialized()); // } // } + +// void test_denormalization() { +// // By using a different symbol for each security +// // we put them all on separate shared mem sections (co2 channels) +// security_key key = 999; +// OldSecurity f1 = mkfuture("F1", ++key, 2016, 5, 1); +// OldSecurity o1 = mkoption("O1", ++key, 2016, 5, 1, 100); +// OldSecurity o2 = mkoption("O2", ++key, 2016, 5, 2, 200); +// OldSecurity o3 = mkoption("O3", ++key, 2016, 5, 3, 300); +// OldSecurity s4 = mkspread("S3", ++key, o2, o3); +// OldSecurity s3 = mkspread("S4", ++key, o1, s4); +// OldSecurity s2 = mkspread("S2", ++key, o1, s4, s3); +// OldSecurity s1 = mkspread("S1", ++key, f1, s2); +// OldSecurity all_secs_array[] = { f1, o1, o2, o3, s1, s2, s3, s4 }; + +// MDProducer producer(Platform::SIM); +// vector producers; +// for (auto& sec : all_secs_array) { +// with_security(sec.security_type(), sec.spread().legs_size(), [&](FixedSecurity& fsec) { +// convert_security(sec, fsec, true); +// producers.push_back(producer.add_security(fsec)); +// }); +// } + +// set csymbols; +// csymbols.insert(UserSymbol("F1", Exchange::TESTEXCHANGE, SecurityType::FUTURE)); +// csymbols.insert(UserSymbol("O1", Exchange::TESTEXCHANGE, SecurityType::OPTION)); +// csymbols.insert(UserSymbol("O2", Exchange::TESTEXCHANGE, SecurityType::OPTION)); +// csymbols.insert(UserSymbol("O3", Exchange::TESTEXCHANGE, SecurityType::OPTION)); +// csymbols.insert(UserSymbol("S1", Exchange::TESTEXCHANGE, SecurityType::SPREAD)); +// csymbols.insert(UserSymbol("S2", Exchange::TESTEXCHANGE, SecurityType::SPREAD)); +// csymbols.insert(UserSymbol("S3", Exchange::TESTEXCHANGE, SecurityType::SPREAD)); +// csymbols.insert(UserSymbol("S4", Exchange::TESTEXCHANGE, SecurityType::SPREAD)); +// MDConsumer consumer(csymbols); + +// auto check_sec = [&](const OldSecurity& security, MDConsumerHandle handle) { +// REQUIRE(security.security_key() == handle.key()); +// auto iter = find_if(begin(all_secs_array), end(all_secs_array), [&](const OldSecurity& sec) { return sec.security_key() == security.security_key(); }); +// REQUIRE(iter != end(all_secs_array)); +// REQUIRE(SecurityHelper::symbol(*iter) == SecurityHelper::symbol(security)); +// if (security.security_type() == SecurityType::SPREAD) { +// REQUIRE(security.spread().normalized() == false); +// REQUIRE(security.spread().has_symbol()); +// REQUIRE(security.spread().legs_size() == iter->spread().legs_size()); +// for (int x = 0; x < security.spread().legs_size(); ++x) { +// REQUIRE(SecurityHelper::symbol(security.spread().legs(x).security()) == SecurityHelper::symbol(iter->spread().legs(x).security())); +// } +// } +// }; + +// consumer.walk_security_list(check_sec); +// consumer.on_security(check_sec); +// consumer.scan_securities(); +// } +// void test_denormalization2() { +// // Just like test_denormailzation but with a different memory layout +// security_key key = 999; +// OldSecurity f1 = mkfuture("FF", ++key, 2016, 5, 1); +// OldSecurity o1 = mkoption("OO", ++key, 2016, 5, 1, 100); +// OldSecurity o2 = mkoption("OO", ++key, 2016, 5, 2, 200); +// OldSecurity o3 = mkoption("OO", ++key, 2016, 5, 3, 300); +// OldSecurity s4 = mkspread("SS", ++key, o2, o3); +// OldSecurity s3 = mkspread("SS", ++key, o1, s4); +// OldSecurity s2 = mkspread("SS", ++key, o1, s4, s3); +// OldSecurity s1 = mkspread("SS", ++key, f1, s2); +// OldSecurity all_secs_array[] = { f1, o1, o2, o3, s1, s2, s3, s4 }; + +// MDProducer producer(Platform::SIM); +// vector producers; +// for (auto& sec : all_secs_array) { +// with_security(sec.security_type(), sec.spread().legs_size(), [&](FixedSecurity& fsec) { +// convert_security(sec, fsec, true); +// producers.push_back(producer.add_security(fsec)); +// }); +// } + +// set csymbols; +// csymbols.insert(UserSymbol("FF", Exchange::TESTEXCHANGE, SecurityType::FUTURE)); +// csymbols.insert(UserSymbol("OO", Exchange::TESTEXCHANGE, SecurityType::OPTION)); +// csymbols.insert(UserSymbol("SS", Exchange::TESTEXCHANGE, SecurityType::SPREAD)); +// MDConsumer consumer(csymbols); + +// auto check_sec = [&](const OldSecurity& security, MDConsumerHandle handle) { +// REQUIRE(security.security_key() == handle.key()); +// auto iter = find_if(begin(all_secs_array), end(all_secs_array), [&](const OldSecurity& sec) { return sec.security_key() == security.security_key(); }); +// REQUIRE(iter != end(all_secs_array)); +// REQUIRE(SecurityHelper::symbol(*iter) == SecurityHelper::symbol(security)); +// if (security.security_type() == SecurityType::SPREAD) { +// REQUIRE(security.spread().normalized() == false); +// REQUIRE(security.spread().has_symbol()); +// REQUIRE(security.spread().legs_size() == iter->spread().legs_size()); +// for (int x = 0; x < security.spread().legs_size(); ++x) { +// REQUIRE(SecurityHelper::symbol(security.spread().legs(x).security()) == SecurityHelper::symbol(iter->spread().legs(x).security())); +// } +// } +// }; + +// consumer.walk_security_list(check_sec); +// consumer.on_security(check_sec); +// consumer.scan_securities(); +// } + +// void test_retry_unresolved_spread() { +// // Make 2 spreads with option legs +// // Leave out one leg for hte other spread +// // The other spread only shows up once its option leg is produced + +// OldSecurity o1 = mkoption("OO", 400, 2016, 5, 1, 100); +// OldSecurity o2 = mkoption("OO", 401, 2016, 5, 2, 200); +// OldSecurity o3 = mkoption("OO", 402, 2016, 5, 3, 300); +// OldSecurity s1 = mkspread("SS", 500, o1, o2); +// OldSecurity s2 = mkspread("SS", 501, o2, o3); + +// MDProducer producer(Platform::SIM); +// map handles; +// auto add_security = [&](const OldSecurity& sec) { +// with_security(sec.security_type(), sec.spread().legs_size(), [&](FixedSecurity& fsec) { +// convert_security(sec, fsec, true); +// handles.insert(make_pair(fsec.key, producer.add_security(fsec))); +// }); +// }; +// // "options publisher has not started yet" +// //add_security(o1); +// //add_security(o2); +// //add_security(o3); +// add_security(s1); +// add_security(s2); + +// // Create consumer +// set consumer_symbols; +// consumer_symbols.insert(UserSymbol("OO", Exchange::TESTEXCHANGE, SecurityType::OPTION)); +// consumer_symbols.insert(UserSymbol("SS", Exchange::TESTEXCHANGE, SecurityType::SPREAD)); +// MDConsumer consumer(consumer_symbols); + +// size_t security_count = 0; +// // Indirection for check function +// function check_func; +// check_func = [&](const OldSecurity&) { +// REQUIRE(false); +// }; +// consumer.on_security([&](const OldSecurity& sec, MDConsumerHandle) { ++security_count; check_func(sec); }); +// consumer.scan_securities(); +// REQUIRE(security_count == 0); + +// // OK, now add enough options to resolve s1 but not s2 +// add_security(o1); +// add_security(o2); +// check_func = [&](const OldSecurity& security) { +// if (security.security_type() == SecurityType::SPREAD) { +// set legs; +// for (const auto& leg : security.spread().legs()) { +// legs.insert(leg.security().security_key()); +// } +// REQUIRE(security.security_key() == 500); +// REQUIRE(legs.size() == 2); +// REQUIRE(legs.count(400) == 1); +// REQUIRE(legs.count(401) == 1); +// } +// }; +// consumer.scan_securities(); // finds o1,o2,s1 +// consumer.scan_securities(); // finds nothing +// REQUIRE(security_count == 3); + +// // Then add the last security and we should see s2 +// add_security(o3); +// check_func = [&](const OldSecurity& security) { +// if (security.security_type() == SecurityType::SPREAD) { +// set legs; +// for (const auto& leg : security.spread().legs()) { +// legs.insert(leg.security().security_key()); +// } +// REQUIRE(security.security_key() == 501); +// REQUIRE(legs.size() == 2); +// REQUIRE(legs.count(401) == 1); +// REQUIRE(legs.count(402) == 1); +// } +// }; +// consumer.scan_securities(); +// REQUIRE(security_count == 5); +// } + +// #ifndef WIN32 +// void test_settlement_recorder() { +// using namespace datetime; +// security_key k1 = 401, k2 = 402; +// OldSecurity o1 = mkoption("OO", k1, 2016, 5, 1, 100); +// OldSecurity o2 = mkoption("OO", k2, 2016, 5, 2, 200); +// MDProducer producer(Platform::SIM); +// auto add_security = [&](const OldSecurity& sec) { +// unique_ptr phandle; +// with_security(sec.security_type(), sec.spread().legs_size(), [&](FixedSecurity& fsec) { +// convert_security(sec, fsec, true); +// phandle.reset(new MDProducerHandle(producer.add_security(fsec))); +// }); +// return phandle; +// }; +// auto ph1 = *add_security(o1); +// auto ph2 = *add_security(o2); +// set consumer_symbols; +// consumer_symbols.insert(UserSymbol("OO", Exchange::TESTEXCHANGE, SecurityType::OPTION)); +// MDConsumer consumer(consumer_symbols); +// auto h1 = consumer.get_handle(k1); +// REQUIRE(h1.is_initialized()); +// auto h2 = consumer.get_handle(k2); + +// // Pick 2 valid dates in the future +// auto today = datetime::today(); +// today.year += 1; +// auto yesterday = datetime::add_days(today, -1); + +// { +// co2::SettlementRecorder recorder(Platform::UNSET); +// recorder.add_settlement(ph1, 101, yesterday); +// recorder.add_settlement(ph1, 102, today); +// recorder.add_settlement(ph2, 199, yesterday); +// recorder.add_settlement(ph2, 188, today); +// recorder.do_work(); +// recorder.do_work(); +// } +// REQUIRE(h1.misc().settlement_price == 102); +// REQUIRE(h1.misc().settlement_date == today); +// REQUIRE(h1.misc().previous_settlement_price == 101); +// REQUIRE(h1.misc().previous_settlement_date == yesterday); +// REQUIRE(h2.misc().settlement_price == 188); +// REQUIRE(h2.misc().settlement_date == today); +// REQUIRE(h2.misc().previous_settlement_price == 199); +// REQUIRE(h2.misc().previous_settlement_date == yesterday); + +// // Update but skip persist +// auto tomorrow = datetime::add_days(today, 1); +// ph1.update_settlement(TimeStamp::now(), 103, tomorrow); +// REQUIRE(h1.misc().settlement_price == 103); +// REQUIRE(h1.misc().settlement_date == tomorrow); +// REQUIRE(h1.misc().previous_settlement_price == 102); +// REQUIRE(h1.misc().previous_settlement_date == today); +// ph1.update_previous_settlement(TimeStamp::now(), 0, Date()); +// REQUIRE(h1.misc().settlement_price == 103); +// REQUIRE(h1.misc().settlement_date == tomorrow); +// ph1.update_settlement(TimeStamp::now(), 0, Date()); +// REQUIRE(h1.misc().settlement_price == 0); +// REQUIRE(h1.misc().settlement_date.day == 0); + +// // pushing a new settlement for today doesn't invalidate yesterday +// ph2.update_settlement(TimeStamp::now(), 201, today); +// REQUIRE(h2.misc().settlement_price == 201); +// REQUIRE(h2.misc().settlement_date == today); +// REQUIRE(h2.misc().previous_settlement_price == 199); +// REQUIRE(h2.misc().previous_settlement_date == yesterday); + +// // Restore persisted state +// { +// co2::SettlementRecorder recorder(Platform::UNSET); +// recorder.restore_settlement(ph1); +// recorder.restore_settlement(ph2); +// recorder.do_work(); +// } +// REQUIRE(h1.misc().settlement_price == 102); +// REQUIRE(h1.misc().settlement_date == today); +// REQUIRE(h1.misc().previous_settlement_price == 101); +// REQUIRE(h1.misc().previous_settlement_date == yesterday); +// REQUIRE(h2.misc().settlement_price == 188); +// REQUIRE(h2.misc().settlement_date == today); +// REQUIRE(h2.misc().previous_settlement_price == 199); +// REQUIRE(h2.misc().previous_settlement_date == yesterday); +// } +// #endif + +// void teardown() {} +// } ; +// std::unique_ptr co2_tests; + +// TEST_CASE("CO2Tests","[md]") { + +// if (!co2_tests) +// co2_tests = std::make_unique(); + +// co2_tests->setup(); + +// #ifndef WIN32 +// SECTION("test_security_id_recorder") { +// co2_tests->test_security_id_recorder(); +// } +// #endif +// SECTION("test_price_size") { +// co2_tests->test_price_size(); +// } +// SECTION("test_slow_double_conversion") { +// co2_tests->test_slow_double_conversion(); +// } +// SECTION("test_compact_exponent") { +// co2_tests->test_compact_exponent(); +// } +// SECTION("test_producer_consumer") { +// co2_tests->test_producer_consumer(); +// } +// SECTION("test_consumer_callback_on_security_added") { +// co2_tests->test_consumer_callback_on_security_added(); +// } +// SECTION("test_consumer_defer_handle1") { +// co2_tests->test_consumer_defer_handle1(); +// } +// SECTION("test_consumer_defer_handle2") { +// co2_tests->test_consumer_defer_handle2(); +// } +// SECTION("test_spread_resolver") { +// co2_tests->test_spread_resolver(); +// } +// SECTION("test_no_double_security_callback") { +// co2_tests->test_no_double_security_callback(); +// } +// SECTION("test_recovery") { +// co2_tests->test_recovery(); +// } +// SECTION("test_no_block_forever_on_publisher_crash") { +// co2_tests->test_no_block_forever_on_publisher_crash(); +// } +// SECTION("test_get_handles") { +// co2_tests->test_get_handles(); +// } +// SECTION("test_denormalization") { +// co2_tests->test_denormalization(); +// } +// SECTION("test_denormalization2") { +// co2_tests->test_denormalization2(); +// } +// SECTION("test_retry_unresolved_spread") { +// co2_tests->test_retry_unresolved_spread(); +// } +// #ifndef WIN32 +// SECTION("test_settlement_recorder") { +// co2_tests->test_settlement_recorder(); +// } +// #endif +// } + +// struct Co2IndexTests { +// vector random_ids; +// set used_ids; +// const int iterations; +// tbb::task_scheduler_init _tbb; + +// Co2IndexTests() : +// random_ids(), +// used_ids(), +// iterations(50), +// _tbb(tbb::task_scheduler_init::automatic) +// {} + +// void setup() { +// used_ids.clear(); +// random_ids.clear(); + +// std::uniform_int_distribution rand_distribution(1, 0x7FFFFFFF); +// std::mt19937 random_number_engine; +// auto genrandom = std::bind(rand_distribution, random_number_engine); +// set temp_ids; +// for (int x = 0; x < 9999; ++x) { +// auto r = genrandom(); +// if (x < 1000) { used_ids.insert(r); } +// temp_ids.insert(r); +// } +// for (uint64_t id : temp_ids) { +// random_ids.push_back(id); +// } +// REQUIRE(random_ids.size() > 1000); +// } + +// struct Handle { +// Handle() : _key(0) {} +// uint64_t _key; +// security_key key() const { return _key; } +// void key(security_key key) { _key = key; } +// }; + +// struct SecurityData { +// uint64_t exchange_id; +// OldSecurity security; +// Handle handle; +// }; + +// template +// void gen_securities(Index& index) { +// unique_ptr temp; +// uint32_t count = 0; +// for (auto& id : used_ids) { +// ++count; +// temp.reset(new SecurityData()); +// temp->security.Clear(); +// temp->security.set_security_type(SecurityType::OPTION); +// temp->security.mutable_option()->set_symbol("FOOX"); +// temp->security.set_numeric_id(id); +// temp->exchange_id = id; +// temp->handle.key(count); +// index.add_security(id, move(temp)); +// } +// } + + +// template +// void test_random_search(Index& index) { +// vector temp(random_ids); +// std::random_shuffle(temp.begin(), temp.end()); +// for (auto& id : temp) { +// if (used_ids.find(id) != used_ids.end()) { +// REQUIRE(index.has_security(id)); +// } +// else { +// REQUIRE(index.get_security(id) == nullptr); +// } +// } +// } + +// template +// void test_walk(Index& index) { +// int count = 0; +// index.walk_security_list([&count](const SecurityData&) { ++count; }); +// REQUIRE(count == (int) used_ids.size()); +// } + +// typedef SimpleSecurityIndex MySimpleIndex; +// void test_simple_index_search() { +// MySimpleIndex index; +// gen_securities(index); +// for (int x = 0; x < iterations; ++x) { +// test_random_search(index); +// } +// } + +// void test_simple_index_parallel_search() { +// MySimpleIndex index; +// gen_securities(index); +// tbb::parallel_for(0, iterations, [&](int) { +// test_random_search(index); +// }); +// } + +// void test_simple_index_iterate() { +// MySimpleIndex index; +// gen_securities(index); +// test_walk(index); +// } + +// typedef ConcurrentSecurityIndex MyConcurrentIndex; +// void test_concurrent_index_search() { +// MyConcurrentIndex index; +// gen_securities(index); +// for (int x = 0; x < iterations; ++x) { +// test_random_search(index); +// } +// } +// void test_concurrent_index_parallel_search() { +// MyConcurrentIndex index; +// gen_securities(index); +// tbb::parallel_for(0, iterations, [&](int) { +// test_random_search(index); +// }); +// } +// void test_concurrent_index_iterate() { +// MyConcurrentIndex index; +// gen_securities(index); +// test_walk(index); +// } // } ; -//std::unique_ptr co2_relay_tests; - +// std::unique_ptr co2_index_tests; -// TEST_CASE ("Co2RelayTests","[md]") +// TEST_CASE ("Co2IndexTests","[md]") // { -// if (!co2_relay_tests) -// co2_relay_tests = std::make_unique(); -// co2_relay_tests->setup(); +// if (!co2_index_tests) +// co2_index_tests = std::make_unique (); + +// co2_index_tests->setup(); -// SECTION("relay_nans") { -// co2_relay_tests->relay_nans(); +// SECTION("test_simple_index_search") { +// co2_index_tests->test_simple_index_search(); +// } + +// SECTION("test_simple_index_parallel_search") { +// co2_index_tests->test_simple_index_parallel_search(); +// } + +// SECTION("test_simple_index_iterate") { +// co2_index_tests->test_simple_index_iterate(); +// } +// SECTION("test_concurrent_index_search") { +// co2_index_tests->test_concurrent_index_search(); +// } +// SECTION("test_concurrent_index_iterate") { +// co2_index_tests->test_concurrent_index_iterate(); +// } +// SECTION("test_concurrent_index_parallel_search") { +// co2_index_tests->test_concurrent_index_parallel_search(); // } // } +// struct Co2PerfTests { +// static const size_t instrument_count = 2048; +// vector> test_instruments; +// vector producers; +// vector consumers; -}}} +// Co2PerfTests() : +// test_instruments(), +// producers(), +// consumers() +// { +// #ifdef _WIN32 +// #pragma warning(push, 0) +// #endif +// // OK if this isn't aligned for testing +// for (size_t x = 0; x < instrument_count; ++x) { +// test_instruments.push_back( +// std::unique_ptr( +// new Co2TestInstrument("Foo", SecurityType::OPTION))); +// } +// #ifdef _WIN32 +// #pragma warning(pop) +// #endif +// for (size_t x = 0; x < test_instruments.size(); ++x) { +// auto producer = test_instruments[x]->producer(); +// producer.set_key(x + 1); +// producers.push_back(producer); +// auto consumer = test_instruments[x]->consumer(); +// REQUIRE(consumer.key() == x + 1); +// consumers.push_back(consumer); +// } -#endif +// std::srand(std::time(0)); +// //std::random_shuffle(producers.begin(), producers.end(), [](int i) { return std::rand() % i; }); +// std::random_shuffle(consumers.begin(), consumers.end(), [](int i) { return std::rand() % i; }); +// } +// void setup() { } +// void teardown() {}; +// void test_write_bbo_perf() { +// REQUIRE(producers.size() == instrument_count); + +// // Warmup +// for (size_t x = 0; x < producers.size(); ++x) { +// auto& p = producers[x]; +// // p.update_bbo(TimeStamp::now(), PriceSize::from_double<3>(100.0 + x, x), PriceSize::from_double<3>(200 + x, 2 * x)); +// p.update_bbo(TimeStamp::now(), PriceSize(100+x,0,x), PriceSize(200+x,0,2*x)); +// } + +// // Test +// TimeStamp start = TimeStamp::now(); +// size_t count = 0; +// const size_t iterations = 1000; +// for (size_t loop = 0; loop < iterations; ++loop) { +// for (size_t x = 0; x < producers.size(); ++x) { +// ++count; +// auto& p = producers[x]; +// TimeStamp t = TimeStamp::now(); +// // p.update_bbo(t, PriceSize::from_double<3>(100.0 + count, count), PriceSize::from_double<3>(200 + count, 2 * count)); +// p.update_bbo(t, PriceSize(100+x,0,x), PriceSize(200+x,0,2*x)); +// } +// } +// TimeStamp end = TimeStamp::now(); +// std::stringstream ss; +// // Of which approximately 1/3 is generating the timestamp on lab-lx4 +// ss << "\t" << (end - start).nanoseconds() / iterations / instrument_count << " ns per update *\n"; +// std::cout << ss.str(); +// } + +// void test_read_bbo_perf() { +// // Warmup +// const auto& write_all = [](vector& producers) { +// // Offset each price by a random amount each iteration to ensure that writes actually happen +// size_t rand_factor = std::rand() % 1024; +// for (size_t x = 0; x < producers.size(); ++x) { +// auto& p = producers[x]; +// // p.update_bbo(TimeStamp::now(), PriceSize::from_double<3>(100.0 + x + rand_factor, x+rand_factor), PriceSize::from_double<3>(200 + x+rand_factor, 2 * x + rand_factor)); +// p.update_bbo(TimeStamp::now(), PriceSize(100+x+rand_factor,0,x+rand_factor), PriceSize(200+x+rand_factor,0,2*x+rand_factor)); +// } +// }; +// write_all(producers); + +// for (auto& c : consumers) { +// PriceSize bid, ask; +// TimeStamp ts; +// REQUIRE(c.get_bbo(ts, bid, ask) == HandleReadResult::SUCCESS); +// } + +// // Start a thread which loops and writes to the producers until terminated +// utils::Dispatcher& writer = utils::Dispatcher::create_new("bbo_writer"); +// DEFER(writer.terminate()); +// tbb::queuing_mutex producer_mutex; +// auto* pproducers = &producers; +// function write_all_repeat = [&]() { +// tbb::queuing_mutex::scoped_lock lock(producer_mutex); +// if (pproducers != nullptr) { +// write_all(*pproducers); +// writer.post(write_all_repeat); +// } +// }; +// write_all_repeat(); + +// // Perform read test +// TimeStamp start = TimeStamp::now(); +// size_t read_count = 0; +// const size_t iterations = 1000; +// for (size_t loop = 0; loop < iterations; ++loop) { +// TimeStamp ts = TimeStamp::now(); +// for (size_t x = 0; x < consumers.size(); ++x) { +// PriceSize bid, ask; +// TimeStamp ts_temp = ts; +// if (consumers[x].get_bbo(ts_temp, bid, ask) == HandleReadResult::SUCCESS) { +// ++read_count; +// } +// } +// } +// REQUIRE(read_count > 0); + +// TimeStamp end = TimeStamp::now(); +// std::stringstream ss; +// ss << "\t" << (end - start).nanoseconds() / iterations / instrument_count << " ns per update. Actual reads = " << read_count << " / " << instrument_count*iterations << " * \n"; +// std::cout << ss.str(); + +// writer.terminate(); + +// tbb::queuing_mutex::scoped_lock lock(producer_mutex); +// pproducers = nullptr; +// std::this_thread::sleep_for(2s); + +// } + +// } ; + +// std::unique_ptr co2_perf_tests; + +// const size_t Co2PerfTests::instrument_count ; +// TEST_CASE ("Co2PerfTests","[md]") +// { + +// if (!co2_perf_tests) +// co2_perf_tests = std::make_unique(); + +// co2_perf_tests->setup(); + +// SECTION("test_write_bbo_perf") { +// // co2_perf_tests->test_write_bbo_perf(); +// } +// SECTION("test_read_bbo_perf") { +// // co2_perf_tests->test_read_bbo_perf(); +// } +// } + +// // struct Co2RelayTests { +// // void setup() { +// // config::internal::initialize_for_test( +// // "PlatformMap {" +// // " value {" +// // " sim {" +// // " exchange: TESTEXCHANGE" +// // " }" +// // " }" +// // "}"); +// // } +// // void teardown() {} + +// // MDConsumer create_consumer() { +// // set symbols; +// // UserSymbol future_symbol("FOO", Exchange::TESTEXCHANGE, SecurityType::FUTURE); +// // symbols.insert(future_symbol); +// // UserSymbol option_symbol("FOOX", Exchange::TESTEXCHANGE, SecurityType::OPTION); +// // symbols.insert(option_symbol); +// // MDConsumer consumer(symbols); +// // return consumer; +// // } + + +// // template +// // T simulate_wire_transfer(const T& request) { +// // T temp; +// // std::string buffer; +// // request.SerializeToString(&buffer); +// // temp.ParseFromString(buffer); +// // return temp; +// // } + +// // PriceSize convert(const co2::PriceSizeMessage& psm) { +// // return PriceSize(psm.mantissa(), psm.exponent(), psm.size()); +// // } + +// // void relay_nans() { +// // MDProducer producer(Platform::SIM); +// // UserSymbol symbol("FOO", Exchange::TESTEXCHANGE, SecurityType::OPTION); + +// // // initialize data +// // vector producers; +// // for (size_t x = 0; x < 100; ++x) { +// // auto strike = 1000 + (100 * x); +// // producers.push_back( +// // producer.add_security( +// // mkfixedoption("FOO", invalid_security_key, 2017, 1, 28, strike, PutOrCall::PUT))); +// // producers.push_back( +// // producer.add_security( +// // mkfixedoption("FOO", invalid_security_key, 2017, 1, 28, strike, PutOrCall::CALL))); +// // } +// // assign_ids(producers.begin(), producers.end(), 1000); +// // PriceSize price35 = PriceSize::from_double<2>(3.5, 5); +// // PriceSize price5 = PriceSize::from_double<2>(5, 5); +// // for(auto p : producers) { +// // p.update_bbo(TimeStamp::now(), price35, price35); +// // } + +// // // Read using QueryServer +// // set symbols; +// // symbols.insert(symbol); +// // auto server = create_query_server(symbols); +// // REQUIRE(server); +// // server->scan_securities(); + +// // co2::MDRequest request; +// // auto* chan = request.add_channels(); +// // chan->set_type(symbol.type()); +// // chan->set_symbol(symbol.symbol()); +// // chan->set_exchange(symbol.exchange()); +// // chan->set_flags(co2::MDRequest::BBO | co2::MDRequest::SECURITY); + +// // // This request should include everything +// // co2::MDReply reply; +// // server->query(request, &reply); +// // reply = simulate_wire_transfer(reply); +// // REQUIRE((size_t)reply.channels().size() == symbols.size()); +// // REQUIRE((size_t)reply.channels(0).updates().size() == producers.size()); +// // // update the request to match the server time +// // for (auto& update : reply.channels(0).updates()) { +// // REQUIRE(update.has_security()); +// // REQUIRE(update.has_book()); +// // REQUIRE(update.book().asks_size() > 0); +// // REQUIRE(update.book().bids_size() > 0); +// // REQUIRE(convert(update.book().bids(0)) == price35); +// // REQUIRE(convert(update.book().asks(0)) == price35); +// // } + +// // // Second request with updated time should include nothing +// // request.mutable_channels(0)->set_time(reply.channels(0).channel().time()); +// // reply.Clear(); +// // server->query(request, &reply); +// // reply = simulate_wire_transfer(reply); +// // REQUIRE(reply.channels().size() == 1); +// // REQUIRE(reply.channels(0).updates().size() == 0); + +// // // Update a single price and make sure we get that back +// // producers[0].update_bbo(TimeStamp::now(), price5, price5, 1); +// // request.mutable_channels(0)->set_time(reply.channels(0).channel().time()); +// // reply.Clear(); +// // server->query(request, &reply); +// // reply = simulate_wire_transfer(reply); +// // REQUIRE(reply.channels().size() == 1); +// // REQUIRE(reply.channels(0).updates().size() == 1); +// // REQUIRE(reply.channels(0).updates(0).has_book()); +// // REQUIRE(convert(reply.channels(0).updates(0).book().bids(0)) == price5); +// // REQUIRE(convert(reply.channels(0).updates(0).book().asks(0)) == price5); + +// // // NaN out all the prices - expect to get a full set of updates +// // for(auto p : producers) { +// // p.update_bbo(TimeStamp::now(), PriceSize::NO_MARKET, PriceSize::NO_MARKET, 2); +// // } +// // request.mutable_channels(0)->set_time(reply.channels(0).channel().time()); +// // reply.Clear(); +// // server->query(request, &reply); +// // reply = simulate_wire_transfer(reply); +// // REQUIRE(reply.channels().size() == 1); +// // REQUIRE((size_t)reply.channels(0).updates().size() == producers.size()); +// // for (auto& update : reply.channels(0).updates()) { +// // REQUIRE(update.has_book()); +// // REQUIRE(update.book().bids_size() > 0); +// // REQUIRE(update.book().asks_size() > 0); +// // REQUIRE(convert(update.book().bids(0)) == PriceSize::NO_MARKET); +// // REQUIRE(convert(update.book().asks(0)) == PriceSize::NO_MARKET); +// // } + +// // // Fill up the markets again +// // for(auto p : producers) { +// // p.update_bbo(TimeStamp::now(), price35, price35, 3); +// // } +// // request.mutable_channels(0)->set_time(reply.channels(0).channel().time()); +// // reply.Clear(); +// // server->query(request, &reply); +// // reply = simulate_wire_transfer(reply); +// // REQUIRE(reply.channels().size() == 1); +// // REQUIRE((size_t)reply.channels(0).updates().size() == producers.size()); +// // for (auto& update : reply.channels(0).updates()) { +// // REQUIRE(update.has_book()); +// // REQUIRE(update.book().bids_size() > 0); +// // REQUIRE(update.book().asks_size() > 0); +// // REQUIRE(convert(update.book().bids(0)) == price35); +// // REQUIRE(convert(update.book().asks(0)) == price35); +// // } + +// // // NaN out the bids +// // for(auto p : producers) { +// // p.update_bbo(TimeStamp::now(), PriceSize::NO_MARKET, price35, 4); +// // } +// // request.mutable_channels(0)->set_time(reply.channels(0).channel().time()); +// // reply.Clear(); +// // server->query(request, &reply); +// // reply = simulate_wire_transfer(reply); +// // REQUIRE(reply.channels().size() == 1); +// // REQUIRE((size_t)reply.channels(0).updates().size() == producers.size()); +// // for (auto& update : reply.channels(0).updates()) { +// // REQUIRE(update.has_book()); +// // REQUIRE(update.book().bids_size() > 0); +// // REQUIRE(update.book().asks_size() > 0); +// // REQUIRE(convert(update.book().bids(0)) == PriceSize::NO_MARKET); +// // REQUIRE(convert(update.book().asks(0)) == price35); +// // } +// // } +// // } ; + +// //std::unique_ptr co2_relay_tests; + + +// // TEST_CASE ("Co2RelayTests","[md]") +// // { +// // if (!co2_relay_tests) +// // co2_relay_tests = std::make_unique(); +// // co2_relay_tests->setup(); + +// // SECTION("relay_nans") { +// // co2_relay_tests->relay_nans(); +// // } +// // } + + +// }}} + +// #endif diff --git a/src/lib/order_man/protos/OrderMan.proto b/src/lib/order_man/protos/OrderMan.proto deleted file mode 100644 index d1e6e34ed..000000000 --- a/src/lib/order_man/protos/OrderMan.proto +++ /dev/null @@ -1,7 +0,0 @@ -syntax='proto3' ; -package bts.orderman ; - - -message B{} - -message C{} diff --git a/src/lib/utils/ValueWrapper.h b/src/lib/utils/ValueWrapper.h index c9100e99a..474282b23 100644 --- a/src/lib/utils/ValueWrapper.h +++ b/src/lib/utils/ValueWrapper.h @@ -23,6 +23,7 @@ public: bool operator<(ValueWrapper const&v) const {return _v < v._v;} ValueT const& value() const {return _v;} + ValueT & value() {return _v;} private: ValueT _v; diff --git a/src/server/sparkd/main.cpp b/src/server/sparkd/main.cpp index 8ea581366..c2098a826 100644 --- a/src/server/sparkd/main.cpp +++ b/src/server/sparkd/main.cpp @@ -9,7 +9,6 @@ #include "fabric/Domain.h" #include "testing/catch_test_runner.h" - int main( int argc, char*argv [] ) { using namespace bts; diff --git a/src/server/sparkd/order_man/OrderMan.cpp b/src/server/sparkd/order_man/OrderMan.cpp index 94235dddb..a6738fded 100644 --- a/src/server/sparkd/order_man/OrderMan.cpp +++ b/src/server/sparkd/order_man/OrderMan.cpp @@ -1,189 +1,20 @@ -// #include "OrderMan.h" +#include "OrderMan.h" +#include "server/sparkd/grid/Node.h" +#include "events/NewSecurities.h" -// namespace bts { -// namespace orderman { +namespace bts { +namespace orderman { -// OrderMan::OrderMan(IOrderManObserver &o, -// IFIXGateway &gw, -// OrderKeyGenerator const &key_gen) : -// state_(State::IN_RECOVERY), -// observer_(o), -// fix_gw_(gw), -// orders_(), -// key_gen_(key_gen), -// keys_left_(0), -// next_key_(), -// outgoing_fix_msgs_(), -// time_observers_() -// { -// gen_key(); -// } - -// OrderMan::~OrderMan() {} - -// void OrderMan::on_end_recovery() -// { -// bts_assert(state_ == State::IN_RECOVERY); -// transition(State::DISCONNECTED); -// } - -// void OrderMan::send_pending_messages() -// { -// if (outgoing_fix_msgs_->first_stream().n_msgs()) { -// if (state_ == State::READY) { -// outgoing_fix_msgs_->first_stream().close(); -// fix_gw_.send(outgoing_fix_msgs_); -// } -// outgoing_fix_msgs_ = fix_gw_.make_outfix_buffer(); -// } -// } - -// void OrderMan::on_venue_ready() { -// bts_assert(state_ == State::DISCONNECTED); -// transition(State::READY); -// } - -// void OrderMan::on_venue_disconnect() { -// bts_assert(state_ == State::READY); -// transition(State::DISCONNECTED); -// } - -// void OrderMan::on_begin_replay() -// { -// bts_assert(state_ == State::DISCONNECTED); -// transition(State::REPLAYING); -// } - -// void OrderMan::on_end_replay() -// { -// bts_assert(state_ == State::REPLAYING); -// transition(State::DISCONNECTED); -// } - -// void OrderMan::on_exe_report(api::Security const &sec, -// OrderKey order_key, -// jetfix::fix::ExecutionReportMsg const&exe_report) -// { -// using namespace jetfix; - -// LOG(OrderMan,Info, -// "Received execution report {} for order {} on {}", -// exe_report.get(), -// order_key.value(), -// fix_gw_.name()); - -// auto it =orders_.find(order_key); -// bool is_new_order = it == orders_.end(); - -// // is this an order we know about? -// if (is_new_order) { - -// // we have never seen this order so we must have crashed -// // before we could persist it. see if we can recover the -// // order from the execution report - -// if (exe_report.get() == api::orderman::OT_LIMIT && -// (exe_report.get() == api::orderman::ET_NEW || -// exe_report.get() == api::orderman::ET_REJECTED)) { - -// // recoverable -// LOG(OrderMan,Error, -// "Received execution report for unknown limit order {} on {}. " -// "This should be investigated." -// " Will try to recreate the order from the execution report", -// order_key.value(),fix_gw_.name()); - -// it = orders_.emplace(order_key, -// std::unique_ptr -// (new LimitOrder(order_key,*this,exe_report))).first; -// } -// else { -// // unrecoverable -// LOG(OrderMan,Error, -// "Received execution report for unknown order {} on {}. " -// "Will not be able to recover " -// "order state. This should never happen. Please investigate", -// order_key.value(),fix_gw_.name()); -// return; -// } -// } - -// it->second->on_exe_report(exe_report); -// observer_.on_order_change(*(it->second)); -// if (exe_report.get () == api::orderman::ET_FILL || -// exe_report.get () == api::orderman::ET_PARTIAL_FILL) { -// OrderFill fill; -// extract_fill(sec,exe_report,&fill); -// observer_.on_fill(*(it->second),fill); -// } -// } - -// void OrderMan::on_cancel_reject(api::Security const&, -// OrderKey order_key, -// jetfix::fix::CancelRejectMsg const&msg) -// { -// using namespace jetfix; - -// LOG(OrderMan,Error, "Received cancel reject {} for order {} on {}", -// msg.get(), -// order_key.value(),fix_gw_.name()); - -// auto it = orders_.find(order_key); -// if (it == orders_.end()) { -// LOG(OrderMan,Error, -// "Received cancel rejection for unknown order {} on {}. " -// " This should never happen. Please investigate", -// order_key.value(),fix_gw_.name()); -// return; -// } -// else { -// it->second->on_cancel_reject(msg); -// observer_.on_order_change(*(it->second)); -// } -// } +struct OrderMan::PImpl +{ + PImpl(OnOrderState const&s,OnFill const&f) : on_state(s), on_fills(f) {} + OnOrderState on_state; + OnFill on_fills; -// void OrderMan::on_time( const OrderManTime&t) -// { -// for (auto &o : time_observers_) -// o(t); -// } +}; -// OrderKey OrderMan::gen_key() -// { -// const auto KEY_BATCH_SIZE = 1000; +OrderMan::OrderMan(OnOrderState const&s,OnFill const&f) : me (new PImpl(s,f)) {} +OrderMan::~OrderMan () {} - -// if (keys_left_ == 0) { -// keys_left_ = KEY_BATCH_SIZE - 1; -// next_key_ = key_gen_(KEY_BATCH_SIZE); -// } -// auto result = next_key_; -// keys_left_--; -// next_key_ = OrderKey(next_key_.value()+1); -// return result; -// } - -// const char* OrderMan::state_name(State state) -// { -// switch (state) { -// case State::IN_RECOVERY: -// return "IN_RECOVERY"; -// case State::DISCONNECTED: -// return "DISCONNECTED"; -// case State::REPLAYING: -// return "REPLAYING"; -// case State::READY: -// return "READY"; -// } -// } - -// void OrderMan::transition(State new_state) { -// LOG(OrderMan,Info, "Order man for {} transitioning from {} to {}", -// fix_gw_.name(),state_name(state_),state_name(new_state)); - -// state_ = new_state; - -// } - -// }} +}} diff --git a/src/server/sparkd/order_man/OrderMan.h b/src/server/sparkd/order_man/OrderMan.h index 1c50003ab..27cff286c 100644 --- a/src/server/sparkd/order_man/OrderMan.h +++ b/src/server/sparkd/order_man/OrderMan.h @@ -1,186 +1,37 @@ #pragma once #include - -#include "lib/api/services/OrderService.pb.h" -#include "lib/logging/logging.h" -#include "lib/utils/bts_assert.h" - -#include "server/sparkd/jetfix/codec/MessageDefinitions.h" -#include "server/sparkd/jetfix/common/OutFIX.h" - -#include "order_man_common.h" -#include "IFIXGateway.h" -#include "fix_order_codec.h" - -#include +#include "server/sparkd/order_man/order_man_utils.h" namespace bts { +namespace api { namespace orderman { +class OrderState; +class Fill; +class CreateOrderRequest; +class UpdateOrderRequest; +class CancelOrderRequest; +}} -class OrderManTime; -class IMDSource; -class OrderFill {}; -using TimeObserver = std::function; - -class Order { -public: - virtual void on_exe_report(jetfix::fix::ExecutionReportMsg const&) {} - virtual void on_cancel_reject(jetfix::fix::CancelRejectMsg const&) {} -}; - -class IOrderManObserver { -public: - virtual void on_order_change(Order const &order) = 0; - virtual void on_fill(Order const&order, OrderFill const &rf) = 0; -}; - - -class IFIXGateway; -class CreateOrder; -class UpdateOrder; -class CancelOrder; - - -class OrderMan; - -class LimitOrder : public Order{ -public: - LimitOrder(OrderKey, OrderMan &,jetfix::fix::ExecutionReportMsg const&); -}; - +namespace orderman { class OrderMan { public: - struct Result {api::orderman::OrderError error;OrderKey key;}; - - OrderMan(IOrderManObserver &o, - IFIXGateway &gw, OrderKeyGenerator const &key_gen) ; + using OnOrderState = std::function; + using OnFill = std::function; + + OrderMan(OnOrderState const&,OnFill const&); ~OrderMan(); - template - Result create(typename OrderType::Create const &create_arg) - { - if (state_!= State::READY) { - LOG(OrderMan,Info, "Cannot create order because venue {} is not ready", fix_gw_.name()); - return Result {api::orderman::OE_VENUE_NOT_READY,OrderKey(0)}; - } - - auto k = gen_key(); - auto order_ptr = new OrderType (k,*this,create_arg); - auto &order_slot = orders_[k]; - bts_assert(!order_slot); - order_slot = std::unique_ptr(order_ptr); - return {api::orderman::OE_OK,k}; - } - - template - Result update(OrderKey k, typename OrderType::Update const& update_arg) - { - if (state_!= State::READY) { - LOG(OrderMan,Info, "Cannot update order {} because venue {} is not ready", k.value(),fix_gw_.name()); - return Result {api::orderman::OE_VENUE_NOT_READY,k}; - } - - auto it = orders_.find(k); - if (it == orders_.end()) { - LOG(OrderMan,Error, - "Cannot update order: {} on venue {} because no such order exists", - k.value(),fix_gw_.name()); - return Result {api::orderman::OE_UNKNOWN_ORDER,k}; - } - - auto abstract_order_ptr = it->second.get(); - auto concrete_order_ptr = dynamic_cast (abstract_order_ptr); - if (!concrete_order_ptr) { - LOG(OrderMan,Error, - "Cannot update order: {} on venue {} because its type does not match the requested type. " - "This is a serious problem. Contact development immediately", - k.value(),fix_gw_.name()); - return Result {api::orderman::OE_ORDER_TYPE_MISMATCH,k}; - } - - concrete_order_ptr->update(update_arg); - return Result {api::orderman::OE_OK,k}; - } - - template - Result cancel(OrderKey k, typename OrderType::Cancel const& cancel_arg) - { - if (state_!= State::READY) { - LOG(OrderMan,Info, "Cannot cancel order {} because venue {} is not ready", k.value(),fix_gw_.name()); - return Result {api::orderman::OE_VENUE_NOT_READY,k}; - } - - auto it = orders_.find(k); - if (it == orders_.end()) { - LOG(OrderMan,Error, - "Cannot cancel order: {} on venue {} because no such order exists", - k.value(),fix_gw_.name()); - return Result {api::orderman::OE_UNKNOWN_ORDER,k}; - } - - auto abstract_order_ptr = it->second.get(); - auto concrete_order_ptr = dynamic_cast (abstract_order_ptr); - if (!concrete_order_ptr) { - LOG(OrderMan,Error, - "Cannot cancel order: {} on venue {} because its type does not match the requested type. " - "This is a serious problem. Contact development immediately", - k.value(),fix_gw_.name()); - return Result {api::orderman::OE_ORDER_TYPE_MISMATCH,k}; - } - - concrete_order_ptr->cancel(cancel_arg); - return Result {api::orderman::OE_OK,k}; - } - - template - Result recover(OrderKey k,typename OrderType::State const&recovered_state) - { - bts_assert(state_ == State::IN_RECOVERY); - auto order_ptr = new OrderType (k,*this,recovered_state); - auto &order_slot = orders_[k]; - bts_assert(!order_slot); - order_slot = std::unique_ptr(order_ptr); - return {api::orderman::OE_OK,k}; - } - - void on_end_recovery(); - void send_pending_messages(); - void on_venue_ready(); - void on_venue_disconnect(); - void on_begin_replay(); - void on_end_replay(); - void on_exe_report(api::Security const &sec, OrderKey order_key, - jetfix::fix::ExecutionReportMsg const&exe_report); - void on_cancel_reject(api::Security const&, OrderKey order_key, - jetfix::fix::CancelRejectMsg const&msg); - void on_time( const OrderManTime&t); + void start(); + void create_order(OrderKey key,api::orderman::CreateOrderRequest const&r); + void update_order(api::orderman::UpdateOrderRequest const&r); + void cancel_order(api::orderman::CancelOrderRequest const&r); private: - - enum class State {IN_RECOVERY,DISCONNECTED, REPLAYING, READY }; - using OrderMap = boost::container::flat_map> ; - - OrderKey gen_key(); - const char* state_name(State state); - void transition(State new_state) ; - - State state_; - // IMDSource &md_src_; - IOrderManObserver &observer_; - IFIXGateway &fix_gw_; - - OrderMap orders_; - OrderKeyGenerator key_gen_; - size_t keys_left_; - OrderKey next_key_; - - jetfix::OutFIXPtr outgoing_fix_msgs_; - std::vector time_observers_; + struct PImpl; + std::unique_ptr me; }; }} - - diff --git a/src/server/sparkd/order_man/OrderManSetup.h b/src/server/sparkd/order_man/OrderManSetup.h new file mode 100644 index 000000000..537d35c4d --- /dev/null +++ b/src/server/sparkd/order_man/OrderManSetup.h @@ -0,0 +1,18 @@ +#pragma once + +#include + +namespace bts { +namespace orderman { + +class OrderManSetup +{ +public: + OrderManSetup(); + ~OrderManSetup(); +private: + struct PImpl; + std::unique_ptr me; +}; + +}} diff --git a/src/server/sparkd/order_man/OrderState.cpp b/src/server/sparkd/order_man/OrderState.cpp index b75e5b594..a3250e38d 100644 --- a/src/server/sparkd/order_man/OrderState.cpp +++ b/src/server/sparkd/order_man/OrderState.cpp @@ -13,8 +13,8 @@ std::string OrderState::to_string () const os.set_order_key(order_key.value()); auto &proto = *os.mutable_native(); - proto.set_account_name(account_name.value()); - proto.set_operator_id(operator_id.value()); + // proto.set_account_name(account_name.value()); + // proto.set_operator_id(operator_id.value()); proto.set_security_key(security_key.value()); proto.set_side(side); proto.set_time_in_force(time_in_force); @@ -22,7 +22,7 @@ std::string OrderState::to_string () const proto.set_customer_or_firm(customer_or_firm); proto.set_cti_code(cti_code); proto.set_user_login(user_login.value()); - proto.set_exchange_order_id(exchange_order_id.value()); + // proto.set_exchange_order_id(exchange_order_id.value()); proto.set_manual_order_indicator(manual_order_indicator); proto.set_price(price.value()); proto.set_quantity(quantity.value()); @@ -48,8 +48,8 @@ void OrderState::from_string(std::string const&str) bts_assert(proto.transact_time().size() == ts.size()); std::copy(proto.transact_time().begin(),proto.transact_time().end(), &ts[0]); *this = OrderState { - AccountName(proto.account_name()), - OperatorID(proto.operator_id()), + AccountName(), + OperatorID(), OrderKey(os.order_key()), SecurityKey(proto.security_key()), proto.side(), @@ -58,7 +58,7 @@ void OrderState::from_string(std::string const&str) proto.customer_or_firm(), proto.cti_code(), UserLogin(proto.user_login()), - ExchangeOrderID(proto.exchange_order_id()), + ExchangeOrderID(), proto.manual_order_indicator(), Price(proto.price()), Quantity(proto.quantity()), diff --git a/src/server/sparkd/order_man/OrderState.h b/src/server/sparkd/order_man/OrderState.h index 4b7ebf86e..36a2ad029 100644 --- a/src/server/sparkd/order_man/OrderState.h +++ b/src/server/sparkd/order_man/OrderState.h @@ -2,7 +2,6 @@ #include #include "order_man_utils.h" -#include "order_man_utils.h" #include "lib/security_model/SecurityHelper.h" #include "lib/domain_model/Security.pb.h" diff --git a/src/server/sparkd/order_man/WorkingOrder.cpp b/src/server/sparkd/order_man/WorkingOrder.cpp index bf93c0e8f..92ff1ecb6 100644 --- a/src/server/sparkd/order_man/WorkingOrder.cpp +++ b/src/server/sparkd/order_man/WorkingOrder.cpp @@ -133,8 +133,8 @@ WorkingOrder::WorkingOrder(fix::ExecutionReportMsg const&msg) decode_clorid(msg.get(),&ok, &sk); me->order_data = OrderState { - AccountName(msg.get()), - OperatorID(msg.get()), + to(msg.get()), + to(msg.get()), ok, sk, api::BuySell(msg.get()), @@ -143,7 +143,7 @@ WorkingOrder::WorkingOrder(fix::ExecutionReportMsg const&msg) COF_UNDEFINED, CTI_UNDEFINED, UserLogin(), - ExchangeOrderID(msg.get()), + to(msg.get()), ManualOrderIndicator(msg.get()), Price (msg.get()), Quantity (msg.get()), @@ -180,8 +180,8 @@ OrderError WorkingOrder::encode_create(OrderCreate const&create,jetfix::FIXEncod encoding.add (scale_price(*create.security,create.price).value(), PriceFieldSz); encoding.add (create.quantity.value(),SizeFieldSz); encoding.add(&create.transact_time[0],create.transact_time.size()); - encoding.add (create.account_name.value()); - encoding.add (create.operator_id.value()); + encoding.add (to_str(create.account_name)); + encoding.add (to_str(create.operator_id)); std::string clordid(clordid_size(),' '); encode_clorid(create.order_key,create.security_key,&clordid); @@ -264,8 +264,8 @@ OrderError WorkingOrder::encode_cancel(OrderCancel const&cancel,jetfix::FIXEncod encoding.add (clordid); encoding.add (clordid); encoding.add (clordid); - encoding.add (state.operator_id.value()); - encoding.add (state.exchange_order_id.value()); + encoding.add (to_str(state.operator_id)); + encoding.add (to_str(state.exchange_order_id)); encoding.add (state.side); encoding.add(&state.transact_time[0],state.transact_time.size()); encoding.add (state.manual_order_indicator); @@ -294,8 +294,8 @@ void WorkingOrder::init_encoding() me->transact_time_bm = encoding.add (datetime::FIX_timestamp(datetime::now())); - encoding.add (state.account_name.value()); - encoding.add (state.operator_id.value()); + encoding.add (to_str(state.account_name)); + encoding.add (to_str(state.operator_id)); std::string clordid(clordid_size(),' '); encode_clorid(state.order_key,state.security_key,&clordid); @@ -308,7 +308,7 @@ void WorkingOrder::init_encoding() encoding.add (state.order_type); encoding.add (state.customer_or_firm); encoding.add (state.cti_code); - encoding.add (state.exchange_order_id.value()); + encoding.add (to_str(state.exchange_order_id)); encoding.add (me->security->glbx().security_desc()); } @@ -374,7 +374,7 @@ bool WorkingOrder::on_exe_report (jetfix::fix::ExecutionReportMsg const&msg) auto const &ts_str = msg.get(); std::copy(ts_str.begin(),ts_str.end(),&(me->order_data.transact_time[0])); - me->order_data.exchange_order_id = ExchangeOrderID(msg.get()); + me->order_data.exchange_order_id = to(msg.get()); switch (me->internal_state) { case InternalState::UNACKNOWLEDGED: diff --git a/src/server/sparkd/order_man/components/Gateway.h b/src/server/sparkd/order_man/components/Gateway.h new file mode 100644 index 000000000..3c537dce8 --- /dev/null +++ b/src/server/sparkd/order_man/components/Gateway.h @@ -0,0 +1,29 @@ +#pragma once + +#include +#include "server/sparkd/jetfix/jetfix.h" + +namespace bts { +namespace orderman { + +class Gateway +{ +public: + using On + Gateway(config::JetFIX::Connection const&c, + + + ) : _gw(c) {} + void on(OutGatewayMsg const&m) {_gw.send(m.msg);} + + + ~Gateway(); +private: + using FIXGw = jetfix::JetFIXGateway; + FIXGw _gw; +}; + +}} diff --git a/src/server/sparkd/order_man/components/MDSource.cpp b/src/server/sparkd/order_man/components/MDSource.cpp new file mode 100644 index 000000000..1751156b6 --- /dev/null +++ b/src/server/sparkd/order_man/components/MDSource.cpp @@ -0,0 +1 @@ +#include "MDSource.h" diff --git a/src/server/sparkd/order_man/components/MDSource.h b/src/server/sparkd/order_man/components/MDSource.h new file mode 100644 index 000000000..c3fa3425c --- /dev/null +++ b/src/server/sparkd/order_man/components/MDSource.h @@ -0,0 +1,47 @@ +#pragma once + +#include "server/sparkd/order_man/events/NewSecurities.h" +#include "server/sparkd/order_man/events/BookUpdate.h" +#include "lib/utils/TimeStamp.h" +#include "lib/md/MDConsumerHandle.h" + +namespace bts { +namespace orderman { + +class MDSource +{ +public: + using OnBook = std::function; + + MDSource(OnBook const&cb) : _on_book(cb), _handles() {} + + void on(event::NewSecurities const&secs) + { + for (auto const &s : secs.value) + _handles.push_back({utils::TimeStamp(),s.handle}); + } + + + void poll_md() { + event::BookUpdate book; + for (auto &h : _handles) + if (h.handle->get_depth(h.last, + std::tuple_size(), + &book.bid()[0], + &book.ask()[0]) == md2::HandleReadResult::SUCCESS) { + book.security_key = SecurityKey(h.handle->key()); + _on_book(book); + } + } + +private: + OnBook _on_book; + struct HandleRecord { + utils::TimeStamp last; + md2::MDConsumerHandle* handle; + }; + + std::vector _handles; +}; + +}} diff --git a/src/server/sparkd/order_man/components/SecuritySource.cpp b/src/server/sparkd/order_man/components/SecuritySource.cpp new file mode 100644 index 000000000..fcbdcfa3b --- /dev/null +++ b/src/server/sparkd/order_man/components/SecuritySource.cpp @@ -0,0 +1,52 @@ +#include "SecuritySource.h" + +#include "lib/domain_model/common.h" +#include "lib/md/MDRecoverableConsumer.h" +#include "lib/utils/bts_assert.h" +#include "server/sparkd/order_man/events/NewSecurities.h" +#include "server/sparkd/order_man/order_man_utils.h" + +#include + +namespace bts { +namespace orderman { + +struct SecuritySource::PImpl +{ + PImpl(OnSecurities const&cb) + : on_securities(cb),md_consumer(),known_securities(),pending_event(), + keep_alive() + {} + + OnSecurities on_securities; + md::MDRecoverableConsumer md_consumer; + std::map> known_securities; + event::NewSecurities pending_event; + std::vector> keep_alive; +}; + +SecuritySource::SecuritySource (OnSecurities const&cb) : me (new PImpl(cb)) +{ + me->md_consumer.on_security ([this] (api::Security const &sec, md2::MDConsumerHandle ) { + auto &ptr = me->known_securities[SecurityKey(sec.id())]; + bts_assert(!ptr); + ptr = std::make_unique(sec); + auto handle = me->md_consumer.get_managed_handle(sec.id()); + me->keep_alive.push_back(handle); + me->pending_event.value.push_back({ptr.get(),handle.get()}); + }); +} + + +void SecuritySource::discover() +{ + me->md_consumer.scan_securities (); + if (me->pending_event.value.size()) { + me->on_securities(me->pending_event); + me->pending_event.value.clear(); + } +} + +SecuritySource::~SecuritySource () {} + +}} diff --git a/src/server/sparkd/order_man/components/SecuritySource.h b/src/server/sparkd/order_man/components/SecuritySource.h new file mode 100644 index 000000000..43bfcacff --- /dev/null +++ b/src/server/sparkd/order_man/components/SecuritySource.h @@ -0,0 +1,23 @@ +#pragma once + +#include + +namespace bts { +namespace orderman { + +namespace event {class NewSecurities;} + +class SecuritySource +{ +public: + using OnSecurities = std::function; + SecuritySource(OnSecurities const&cb); + void discover(); + + ~SecuritySource(); +private: + struct PImpl; + std::unique_ptr me; +}; + +}} diff --git a/src/server/sparkd/order_man/components/TimeSource.h b/src/server/sparkd/order_man/components/TimeSource.h new file mode 100644 index 000000000..835e35115 --- /dev/null +++ b/src/server/sparkd/order_man/components/TimeSource.h @@ -0,0 +1,36 @@ +#pragma once + +#include "server/sparkd/order_man/events/NewSecurities.h" +#include "server/sparkd/order_man/events/BookUpdate.h" +#include "lib/utils/TimeStamp.h" +#include "lib/md/MDConsumerHandle.h" + +namespace bts { +namespace orderman { + +class TimeSource +{ +public: + using OnTime = std::function; + + MDSource(OnTime const&cb) : _on_time(cb),_last() {} + + void check_time() { + auto now = Timer::now(); + if (ms_diff(now,_last) > 10) { + _last = now; + event::TimeChange change; + auto ts_str = datetime::FIX_timestamp(datetime::now()); + bts_assert(ts_str.size() == std::tuple_size()); + std::copy(ts_str.begin(),ts_str.end(),&change.ts_str[0]); + _on_time(change); + } + } + +private: + OnTime _on_time; + TimeStamp _last; +}; + +}} + diff --git a/src/server/sparkd/order_man/doc/order_man_notes.org b/src/server/sparkd/order_man/doc/order_man_notes.org index f52b77ebd..dd1cc2c3f 100644 --- a/src/server/sparkd/order_man/doc/order_man_notes.org +++ b/src/server/sparkd/order_man/doc/order_man_notes.org @@ -34,7 +34,6 @@ | remaining_quantity | N | N | N | Y | Y | ~const,null | | reject_msg | N | N | N | Y | Y | ~const,null | - * Order Chain trace ** NewOrder =-=-=-=-=-=-==-=-=-=-=-=-==-=-=-=-=-=-= diff --git a/src/server/sparkd/order_man/events/BookUpdate.h b/src/server/sparkd/order_man/events/BookUpdate.h new file mode 100644 index 000000000..8ec82db4f --- /dev/null +++ b/src/server/sparkd/order_man/events/BookUpdate.h @@ -0,0 +1,30 @@ +#pragma once + +#include +#include "lib/domain_model/common.h" +#include "lib/config/protos/MarketData.pb.h" +#include "lib/md/Structure/PriceSize.h" + +namespace bts { +namespace orderman { +namespace event { + +using BookSide = std::array ; + +class BookUpdate { +public: + SecurityKey security_key; + BookSide &bid () {return _book[0];} + BookSide &ask () {return _book[1];} + + BookSide& get_side(api::BuySell bs) + { + bts_assert_d(bs != api::UNDEFINED); + return _book[bs - api::BUY]; + } + +private: + std::array _book; +}; + +}}} diff --git a/src/server/sparkd/order_man/events/ExecutionReport.h b/src/server/sparkd/order_man/events/ExecutionReport.h new file mode 100644 index 000000000..f46026b26 --- /dev/null +++ b/src/server/sparkd/order_man/events/ExecutionReport.h @@ -0,0 +1,32 @@ +#pragma once + +#include +#include "lib/api/services/OrderService.pb.h" + +namespace bts { +namespace orderman { +namespace event { + +class ExecutionReport +{ +public: + AccountName account_name; + OperatorID operator_id; + OrderKey order_key; + SecurityKey security_key; + api::BuySell side; + TimeInForce::Type time_in_force; + api::orderman::OrderType order_type; + api::orderman::ExecType exec_type; + api::orderman::OrderStatus order_status; + ExchangeOrderID exchange_order_id; + OrderPrice price; + Quantity order_quantity; + Quantity filled_quantity; + Quantity last_shares; + OrderPrice last_px; + std::string reject_msg; +}; + +}}} + diff --git a/src/server/sparkd/order_man/events/GatewayStatusChange.h b/src/server/sparkd/order_man/events/GatewayStatusChange.h new file mode 100644 index 000000000..df28e18d0 --- /dev/null +++ b/src/server/sparkd/order_man/events/GatewayStatusChange.h @@ -0,0 +1,16 @@ +#pragma once + +#include +#include "server/sparkd/order_man/order_man_utils.h" + +namespace bts { +namespace orderman { +namespace event { + +class GatewayStatusChange { +public: + GatewayName name; + bool is_connected; +}; + +}}} diff --git a/src/server/sparkd/order_man/events/NewSecurities.h b/src/server/sparkd/order_man/events/NewSecurities.h new file mode 100644 index 000000000..0c4960bfb --- /dev/null +++ b/src/server/sparkd/order_man/events/NewSecurities.h @@ -0,0 +1,23 @@ +#pragma once + +#include + +namespace bts { + +namespace api {class Security;} +namespace md2 {class MDConsumerHandle;} + +namespace orderman { +namespace event { + +class NewSecurities +{ +public: + struct SecRec { + api::Security const* sec; + md2::MDConsumerHandle* handle; + }; + std::vector value; +}; + +}}} diff --git a/src/server/sparkd/order_man/events/OutGatewayMsg.h b/src/server/sparkd/order_man/events/OutGatewayMsg.h new file mode 100644 index 000000000..9d76f0b3c --- /dev/null +++ b/src/server/sparkd/order_man/events/OutGatewayMsg.h @@ -0,0 +1,13 @@ +#include "OutGatewayMsg.h" +#include "server/sparkd/jetfix/common/OutFIXPtr.h" + +namespace bts { +namespace orderman { +namespace event { + +class OutGatewayMsg { +public: + jetfix::OutFIXPtr msg; +}; + +}}} diff --git a/src/server/sparkd/order_man/events/TimeChange.cpp b/src/server/sparkd/order_man/events/TimeChange.cpp new file mode 100644 index 000000000..53ebedeea --- /dev/null +++ b/src/server/sparkd/order_man/events/TimeChange.cpp @@ -0,0 +1 @@ +#include "TimeChange.h" diff --git a/src/server/sparkd/order_man/events/TimeChange.h b/src/server/sparkd/order_man/events/TimeChange.h new file mode 100644 index 000000000..8adc128d7 --- /dev/null +++ b/src/server/sparkd/order_man/events/TimeChange.h @@ -0,0 +1,18 @@ +#pragma once + +#include +#include "lib/utils/bts_chrono.h" +#include "server/sparkd/order_man/order_man_utils.h" + +namespace bts { +namespace orderman { +namespace event { + +class TimeChange +{ +public: + TimeStamp ts; + OrderTimeStamp ts_str; +}; + +}}} diff --git a/src/server/sparkd/order_man/fix_order_codec.h b/src/server/sparkd/order_man/fix_order_codec.h index 214a8b4ac..7febb86fb 100644 --- a/src/server/sparkd/order_man/fix_order_codec.h +++ b/src/server/sparkd/order_man/fix_order_codec.h @@ -1,52 +1,52 @@ -#pragma once +// #pragma once -#include -#include +// #include +// #include -#include "order_man_common.h" -#include "server/sparkd/jetfix/codec/i32_toa.h" -#include "server/sparkd/jetfix/codec/MessageDefinitions.h" -#include "lib/utils/bts_assert.h" -#include "order_man_common.h" +// #include "order_man_common.h" +// #include "server/sparkd/jetfix/codec/i32_toa.h" +// #include "server/sparkd/jetfix/codec/MessageDefinitions.h" +// #include "lib/utils/bts_assert.h" +// #include "order_man_common.h" -namespace bts { -namespace api {class Security;} +// namespace bts { +// namespace api {class Security;} -namespace orderman { +// namespace orderman { -class OrderFill; +// class OrderFill; -void extract_fill(api::Security const &sec,jetfix::fix::ExecutionReportMsg const&, OrderFill *); +// void extract_fill(api::Security const &sec,jetfix::fix::ExecutionReportMsg const&, OrderFill *); -constexpr size_t clordid_size() {return 20;} +// constexpr size_t clordid_size() {return 20;} -inline void encode_clorid(OrderKey ok, SecurityKey sk,std::string *out) { - u32_toa(sk.value(),&((*out)[0]),clordid_size()/2); - u32_toa(ok.value(),&((*out)[clordid_size()/2]),clordid_size()/2); -} +// inline void encode_clorid(OrderKey ok, SecurityKey sk,std::string *out) { +// u32_toa(sk.value(),&((*out)[0]),clordid_size()/2); +// u32_toa(ok.value(),&((*out)[clordid_size()/2]),clordid_size()/2); +// } -inline void decode_clorid(std::string const&in, OrderKey *ok, SecurityKey *sk) -{ - bts_assert(in.size() == clordid_size()); - char buffer[clordid_size()/2+1]; - buffer[clordid_size()/2] = 0; +// inline void decode_clorid(std::string const&in, OrderKey *ok, SecurityKey *sk) +// { +// bts_assert(in.size() == clordid_size()); +// char buffer[clordid_size()/2+1]; +// buffer[clordid_size()/2] = 0; - std::memcpy(buffer,&in[0],clordid_size()/2); - *sk = SecurityKey(std::atoi(buffer)); +// std::memcpy(buffer,&in[0],clordid_size()/2); +// *sk = SecurityKey(std::atoi(buffer)); - std::memcpy(buffer,&in[clordid_size()/2],clordid_size()/2); - *ok = OrderKey(std::atoi(buffer)); -} +// std::memcpy(buffer,&in[clordid_size()/2],clordid_size()/2); +// *ok = OrderKey(std::atoi(buffer)); +// } -template -OrderKey get_order_key(MSG const &msg) -{ +// template +// OrderKey get_order_key(MSG const &msg) +// { - bts_assert(msg.template has ()); - OrderKey result; - SecurityKey dummy; - decode_clorid(msg.template get(),&result,&dummy); - return result; -} +// bts_assert(msg.template has ()); +// OrderKey result; +// SecurityKey dummy; +// decode_clorid(msg.template get(),&result,&dummy); +// return result; +// } -}} +// }} diff --git a/src/server/sparkd/order_man/order_man_common.cpp b/src/server/sparkd/order_man/order_man_common.cpp deleted file mode 100644 index 299713f84..000000000 --- a/src/server/sparkd/order_man/order_man_common.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "order_man_common.h" diff --git a/src/server/sparkd/order_man/order_man_common.h b/src/server/sparkd/order_man/order_man_common.h deleted file mode 100644 index cf6af3ab6..000000000 --- a/src/server/sparkd/order_man/order_man_common.h +++ /dev/null @@ -1,23 +0,0 @@ - -#pragma once - -#include -#include -#include -#include - -#include "lib/utils/ValueWrapper.h" -#include "lib/utils/bts_assert.h" -#include "lib/domain_model/common.h" - -namespace bts { -namespace orderman { -using OrderKey = utils::ValueWrapper; -using FIXSessionName = utils::ValueWrapper; -using OperatorID = utils::ValueWrapper; -using ExchangeOrderID = utils::ValueWrapper; -using OrderTimeStamp = std::array ; -using FillID = utils::ValueWrapper; -using OrderKeyGenerator = std::function; - -}} diff --git a/src/server/sparkd/order_man/order_man_utils.h b/src/server/sparkd/order_man/order_man_utils.h index 196095aa3..2399fd8b5 100644 --- a/src/server/sparkd/order_man/order_man_utils.h +++ b/src/server/sparkd/order_man/order_man_utils.h @@ -3,17 +3,54 @@ #include #include #include +#include #include "lib/utils/ValueWrapper.h" #include "lib/domain_model/common.h" +#include "lib/utils/bts_assert.h" namespace bts { +namespace api {class Security;} namespace orderman { using OrderKey = utils::ValueWrapper; using FIXSessionName = utils::ValueWrapper; -using OperatorID = utils::ValueWrapper; -using ExchangeOrderID = utils::ValueWrapper; +using GatewayName = utils::ValueWrapper; +using OperatorID = utils::ValueWrapper>; +using ExchangeOrderID = utils::ValueWrapper>; using OrderTimeStamp = std::array ; +using SecurityPtr = const api::Security *; +using AccountName = utils::ValueWrapper>; + +template void to_array(std::string const&str,std::array*out ) +{ + auto &result = *out; + bts_assert(str.size() <= N); + memset(&result[0],' ', N); + memcpy(&result[0],str.data(),str.size()); +} + +template void to_str(std::array const&in ,std::string *out) +{ + auto &result = *out; + result.resize(N); + memcpy(&result[0],in.data(),N); +} + +template +T to(std::string const&str) +{ + T result; + to_array(str,&result.value()); + return result; +} + +template +inline std::string to_str (T const&t) +{ + std::string result; + to_str(t.value(),&result); + return result; +} }} diff --git a/src/server/sparkd/risk_limits/RiskLimitManager.cpp b/src/server/sparkd/risk_limits/RiskLimitManager.cpp index 5b303a579..6d94e398d 100644 --- a/src/server/sparkd/risk_limits/RiskLimitManager.cpp +++ b/src/server/sparkd/risk_limits/RiskLimitManager.cpp @@ -1,88 +1,88 @@ -#include "RiskLimitManager.h" - -#include "SharedRiskLimiter.h" -#include "RiskLimitState.h" -#include "IRiskLimitObserver.h" - -#include "lib/config/config.h" -#include "lib/config/protos/Account.pb.h" -#include "lib/config/protos/User.pb.h" -#include "lib/domain_model/Security.pb.h" - -#include "lib/utils/bts_assert.h" -#include "lib/logging/logging.h" - -#include -#include - -using namespace bts::orderman; - -namespace bts { -namespace risklimits { - -using OperatorMap = boost::container::flat_map; - -using SymbolKey = std::tuple; -using KeyType = std::tuple; -using SharedLimiterMap = boost::container::flat_map>; - -using StateKeyType = std::tuple; -using PBLimitsMap = const google::protobuf::Map; - -struct RiskLimitManager::PImpl -{ - RiskLimitManager& this_ptr; - OperatorMap operator_map; - SharedLimiterMap limiter_map; - std::vector observers; - boost::container::flat_map aggregate_state; - - PImpl(RiskLimitManager& rm) : - this_ptr(rm), - operator_map(), limiter_map(), observers(), aggregate_state() - { - auto users = config::get(); - for(int i = 0; i < users.user_size(); i++) - { - auto user = users.user(i); - for(int j = 0; i < user.operators_size(); j++) - operator_map[user.operators(j).operator_id().id()] = user.operators(j); - } - - auto accts = config::get(); - for(int i = 0; i < accts.accounts_with_limits_size(); i++) - { - const auto& acct = accts.accounts_with_limits(i).account(); - const auto& future_limit = accts.accounts_with_limits(i).future_limits(); - update_limits(future_limit, acct, api::Security::kFuture); - const auto& spread_limit = accts.accounts_with_limits(i).spread_limits(); - update_limits(spread_limit, acct, api::Security::kSpread); - } +#include "RiskLimitManager.h" + +#include "SharedRiskLimiter.h" +#include "RiskLimitState.h" +#include "IRiskLimitObserver.h" + +#include "lib/config/config.h" +#include "lib/config/protos/Account.pb.h" +#include "lib/config/protos/User.pb.h" +#include "lib/domain_model/Security.pb.h" + +#include "lib/utils/bts_assert.h" +#include "lib/logging/logging.h" + +#include +#include + +using namespace bts::orderman; + +namespace bts { +namespace risklimits { + +using OperatorMap = boost::container::flat_map; + +using SymbolKey = std::tuple; +using KeyType = std::tuple; +using SharedLimiterMap = boost::container::flat_map>; + +using StateKeyType = std::tuple; +using PBLimitsMap = const google::protobuf::Map; + +struct RiskLimitManager::PImpl +{ + RiskLimitManager& this_ptr; + OperatorMap operator_map; + SharedLimiterMap limiter_map; + std::vector observers; + boost::container::flat_map aggregate_state; + + PImpl(RiskLimitManager& rm) : + this_ptr(rm), + operator_map(), limiter_map(), observers(), aggregate_state() + { + auto users = config::get(); + for(int i = 0; i < users.user_size(); i++) + { + auto user = users.user(i); + for(int j = 0; i < user.operators_size(); j++) + operator_map[user.operators(j).operator_id().id()] = user.operators(j); + } + + auto accts = config::get(); + for(int i = 0; i < accts.accounts_with_limits_size(); i++) + { + const auto& acct = accts.accounts_with_limits(i).account(); + const auto& future_limit = accts.accounts_with_limits(i).future_limits(); + update_limits(future_limit, acct, api::Security::kFuture); + const auto& spread_limit = accts.accounts_with_limits(i).spread_limits(); + update_limits(spread_limit, acct, api::Security::kSpread); + } } void update_limits(PBLimitsMap & map, const bts::config::Account & acct, api::Security::ContractCase contract_type) { - for(auto j = map.begin(); j != map.end(); j++) - { - for(int k = 0; k < acct.allowed_operators_size(); k++) - { - auto allowed = operator_map.find(acct.allowed_operators(k).id()); - if(allowed != operator_map.end()) - { - OperatorID opid(allowed->second.operator_id().id()); - SharedRiskLimiter& lim = find_limiter(to_accountname(acct), opid, to_symbolkey(j->first, contract_type)); - lim.on_account_update(acct); - lim.on_operator_update(allowed->second); - lim.on_risk_limit_update(j->second); - } - } + for(auto j = map.begin(); j != map.end(); j++) + { + for(int k = 0; k < acct.allowed_operators_size(); k++) + { + auto allowed = operator_map.find(acct.allowed_operators(k).id()); + if(allowed != operator_map.end()) + { + OperatorID opid(allowed->second.operator_id().id()); + SharedRiskLimiter& lim = find_limiter(to_accountname(acct), opid, to_symbolkey(j->first, contract_type)); + lim.on_account_update(acct); + lim.on_operator_update(allowed->second); + lim.on_risk_limit_update(j->second); + } + } } - } - - SymbolKey to_symbolkey(const api::Security& security) - { - return std::make_tuple("ES", security.Contract_case()); - } + } + + SymbolKey to_symbolkey(const api::Security& security) + { + return std::make_tuple("ES", security.Contract_case()); + } SymbolKey to_symbolkey(const std::string& symbol, api::Security::ContractCase type) { return std::make_tuple(symbol, type); @@ -90,8 +90,8 @@ struct RiskLimitManager::PImpl AccountName to_accountname(const config::Account& key) { return AccountName(key.account_id()); - } - + } + SharedRiskLimiter& find_limiter(AccountName acctname, OperatorID opid, SymbolKey symkey) { KeyType key(acctname, opid, symkey); @@ -105,10 +105,10 @@ struct RiskLimitManager::PImpl limiter_map[key] = ptr; return *ptr; } - } -}; - - + } +}; + + RiskLimitManager::RiskLimitManager() : me(new PImpl(*this)) {} RiskLimitManager::~RiskLimitManager() {} diff --git a/src/server/sparkd/risk_limits/RiskLimiter.cpp b/src/server/sparkd/risk_limits/RiskLimiter.cpp index 262bb781d..b590d7071 100644 --- a/src/server/sparkd/risk_limits/RiskLimiter.cpp +++ b/src/server/sparkd/risk_limits/RiskLimiter.cpp @@ -94,42 +94,42 @@ struct RiskLimiter::PImpl }; -RiskLimiter::RiskLimiter(SharedRiskLimiter& sh, orderman::OrderKey key) - : me(new PImpl(sh, key)) -{ -} - -RiskLimiter::~RiskLimiter() {} - -RiskLimiter::RiskLimitResult RiskLimiter::check(Quantity qty, api::BuySell side) const -{ - RiskLimiter::RiskLimitResult res; - res.success = me->check_can_trade(res.message); - if(!res.success) - return res; - res.success = me->check_clip_size(qty, res.message); - if(!res.success) - return res; - res.success = me->check_position_limit(qty, side, res.message); - - return res; -} - - -void RiskLimiter::on_order_state(const orderman::OrderState& state) -{ - int working_diff = state.remaining_quantity.value() - me->remaining_quantity.value(); - if(state.side == api::BUY) - me->shared.position_info.long_working += working_diff; - else - me->shared.position_info.short_working += working_diff; - - int filled_diff = state.filled_quantity.value() - me->filled_quantity.value(); - if(state.side == api::BUY) - me->shared.position_info.long_position += filled_diff; - else - me->shared.position_info.short_position += filled_diff; -} - +RiskLimiter::RiskLimiter(SharedRiskLimiter& sh, orderman::OrderKey key) + : me(new PImpl(sh, key)) +{ +} + +RiskLimiter::~RiskLimiter() {} + +RiskLimiter::RiskLimitResult RiskLimiter::check(Quantity qty, api::BuySell side) const +{ + RiskLimiter::RiskLimitResult res; + res.success = me->check_can_trade(res.message); + if(!res.success) + return res; + res.success = me->check_clip_size(qty, res.message); + if(!res.success) + return res; + res.success = me->check_position_limit(qty, side, res.message); + + return res; +} + + +void RiskLimiter::on_order_state(const orderman::OrderState& state) +{ + int working_diff = state.remaining_quantity.value() - me->remaining_quantity.value(); + if(state.side == api::BUY) + me->shared.position_info.long_working += working_diff; + else + me->shared.position_info.short_working += working_diff; + + int filled_diff = state.filled_quantity.value() - me->filled_quantity.value(); + if(state.side == api::BUY) + me->shared.position_info.long_position += filled_diff; + else + me->shared.position_info.short_position += filled_diff; +} + } } \ No newline at end of file diff --git a/src/server/sparkd/spreader/SimpleSpreader.cpp b/src/server/sparkd/spreader/SimpleSpreader.cpp index 9c78d37c0..9c2efc97e 100644 --- a/src/server/sparkd/spreader/SimpleSpreader.cpp +++ b/src/server/sparkd/spreader/SimpleSpreader.cpp @@ -27,7 +27,7 @@ namespace spreader class SimpleSpreaderCreate : public SpreaderCreate { public: - AccountName account_name; + orderman::AccountName account_name; orderman::OperatorID operator_id; api::BuySell side; diff --git a/src/server/sparkd/spreader/SpreaderBase.h b/src/server/sparkd/spreader/SpreaderBase.h index df3ed0954..f58cf7645 100644 --- a/src/server/sparkd/spreader/SpreaderBase.h +++ b/src/server/sparkd/spreader/SpreaderBase.h @@ -2,7 +2,8 @@ #include #include "lib/domain_model/Security.pb.h" -#include "server/sparkd/order_man/order_man_common.h" +#include "server/sparkd/order_man/order_man_utils.h" + #include "lib/api/services/OrderService.pb.h" #include "lib/utils/bts_chrono.h" @@ -49,7 +50,7 @@ class SpreaderUpdate {}; struct SpreaderSubscriptions { std::vector securities_for_md; - AccountName account_for_limit_check; + orderman::AccountName account_for_limit_check; orderman::OperatorID operator_for_limit_check; }; @@ -115,7 +116,7 @@ protected: // use this to accelarate the creation of new orders OrderTemplate const& make_order_template(api::Security const&, - AccountName const &, + orderman::AccountName const &, orderman::OperatorID const &, api::BuySell , api::orderman::CustomerOrFirm , diff --git a/tools/ensure_lhome.cmd b/tools/ensure_lhome.cmd index cd106b143..0f5a1aa15 100644 --- a/tools/ensure_lhome.cmd +++ b/tools/ensure_lhome.cmd @@ -1,5 +1,7 @@ @set linuxhome=%homedrive%%homepath%\linux_home -@if not exist %linuxhome% ( +@if not exist %linuxhome%\www\config ( mkdir %linuxhome%\www\config - mkdir %linuxhome%\.bts\ +) +@if not exist %linuxhome%\.bts\conf ( + mkdir %linuxhome%\.bts\conf ) diff --git a/tools/get_system_includes.sh b/tools/get_system_includes.sh index 3fcddd858..0a14f7a3a 100755 --- a/tools/get_system_includes.sh +++ b/tools/get_system_includes.sh @@ -7,3 +7,5 @@ cd $ROOT # external includes find externals/linux/include/ -name "*.h" -or -name "*.hpp" | sed s/externals\\/linux\\/include\\/// +# stdlib includes +find /usr/include/c++/6 -maxdepth 1 | sed s/\\/usr\\/include\\/c\+\+\\/6\\/// diff --git a/tools/safe_dep_glob.cmake b/tools/safe_dep_glob.cmake index da0cb9c45..e4a8e28e4 100644 --- a/tools/safe_dep_glob.cmake +++ b/tools/safe_dep_glob.cmake @@ -21,7 +21,7 @@ function(update_deps_file deps) list(REMOVE_DUPLICATES deps) list(SORT deps) # Update the deps file - set(content "# generated by make process\nset(sources ${deps})\n") + set(content "set(sources ${deps})") update_file(${deps_file} "${content}") # Include the file so it's tracked as a generation dependency we don't # need the content.