From 96cd807720514f30d32d4504dba4a1de40dca4fa Mon Sep 17 00:00:00 2001 From: John Colvin Date: Sun, 27 Mar 2022 21:49:39 -0400 Subject: [PATCH] Add fake slide report data --- .../SupportingData/VolSurfaceNonVolData.cs | 4 +- .../spark/Daemons/FakeSlideDataDaemon.cs | 94 +++++++++++++++++++ src/client/spark/Risk/FakeTradeLogRow.cs | 55 +++++++++++ .../spark/Risk/Slide/SlideReportCalculator.cs | 4 +- .../spark/Risk/VolSurfaceRiskCalculator.cs | 14 +-- 5 files changed, 160 insertions(+), 11 deletions(-) create mode 100644 src/client/spark/Daemons/FakeSlideDataDaemon.cs create mode 100644 src/client/spark/Risk/FakeTradeLogRow.cs diff --git a/src/client/spark/Analytics/SupportingData/VolSurfaceNonVolData.cs b/src/client/spark/Analytics/SupportingData/VolSurfaceNonVolData.cs index c868d9ef3..af6a85ca8 100644 --- a/src/client/spark/Analytics/SupportingData/VolSurfaceNonVolData.cs +++ b/src/client/spark/Analytics/SupportingData/VolSurfaceNonVolData.cs @@ -41,7 +41,7 @@ namespace mbts.analytics { } public IDictionary get_cpp_map() { - return input.get_cpp_list().ToDictionary(i => new mbts.SecurityTuple(i.Item1, i.Item2, Venue.Unset), i => i.Item3); + return input.get_cpp_list().ToDictionaryCrushed(i => new mbts.SecurityTuple(i.Item1, i.Item2, Venue.Sim), i => i.Item3); } private static mbts.SecurityExpiration _unset = mbts.SecurityExpiration.empty; @@ -59,4 +59,4 @@ namespace mbts.analytics { return rv; } } -} \ No newline at end of file +} diff --git a/src/client/spark/Daemons/FakeSlideDataDaemon.cs b/src/client/spark/Daemons/FakeSlideDataDaemon.cs new file mode 100644 index 000000000..80e1e5371 --- /dev/null +++ b/src/client/spark/Daemons/FakeSlideDataDaemon.cs @@ -0,0 +1,94 @@ +#nullable enable +namespace bts.daemons { + using bts.api; + using bts.utils; + + using mbts; + using mbts.spark; + using mbts.ui; + + using System; + using System.Collections.Generic; + using System.Collections.Immutable; + using System.Composition; + using System.Linq; + using System.Reactive.Linq; + using System.Threading; + + [Export(typeof(IShellDaemon))] + public sealed class FakeSymbolDriverDaemon : IShellDaemon, IDisposable { + IDisposable _dispose; + + public FakeSymbolDriverDaemon(IBus bus) { + int tid_counter = 1; + IDictionary vol_surfaces = new Dictionary(); + var sec_trades = new Dictionary>(); + _dispose = Disposable.Create( + bus.subscribe(SparkTags.VolSurfaceListTag).Subscribe(vss => vol_surfaces = vss), + bus.subscribe(SparkTags.SecuritiesTag).SubscribeEach(secs => { + var btc_secs = secs.Values.Where( + s => s.security.Symbol == "BTC" && + s.security.ContractCase == Security.ContractOneofCase.Future) + .ToList(); + var fake_trades = btc_secs.Select( + s => get_fake_trade(to_sec_exp(s.security), 4200)).ToList(); + bus.post(SparkTags.TradeLogTag, fake_trades); + var bbo_upd_refs = btc_secs.ToDictionary(s => s.security.Id, _ => 0UL); + var driver_sym_prices = btc_secs.ToImmutableDictionary( + s => to_sec_exp(s.security), get_driver_price); + + double get_driver_price(SecurityMD smd) { + var upd_ref = bbo_upd_refs[smd.security.Id]; + var rv = double.NaN; + if (smd.md.get_bbo(ref upd_ref, out var bid, out var ask)) { + rv = bid.price.if_nan(ask.price); + } + bbo_upd_refs[smd.security.Id] = upd_ref; + return rv; + } + + return Observable.Interval(TimeSpan.FromSeconds(2)).Subscribe(_ => { + var bd = driver_sym_prices.ToBuilder(); + foreach (var sec in btc_secs) { + var sec_exp = to_sec_exp(sec.security); + bd[sec_exp] = get_driver_price(sec).if_nan(driver_sym_prices[sec_exp]); + } + driver_sym_prices = bd.ToImmutable(); + foreach (var vsid in vol_surfaces.Keys) { + bus.post(SparkTags.VolSurfaceDriverSymbolPriceTag(vsid), driver_sym_prices); + } + }); + })); + + static SecurityExpiration to_sec_exp(SecurityImmutable sec) => + new(sec.Exchange, sec.ContractCase, sec.Symbol, sec.security_expiration() ?? DateTime.MinValue, sec.Id); + + ITradeLogRow get_fake_trade( + SecurityExpiration sec_exp, + double price, + int size = 1, + string account = "ACC A", + double? strike = null, + PutOrCall? cp = null, + double? theo = null + ) { + var t1 = new FakeTradeLogRow( + Interlocked.Increment(ref tid_counter), + sec_exp, + strike, + cp, + size, + price, + theo ?? price + 10 * new Random().NextDouble(), + account, + true, + DateTime.Now, + "FakeTrader"); + return t1; + } + } + + public void Dispose() => + Disposable.dispose(ref _dispose); + } +} diff --git a/src/client/spark/Risk/FakeTradeLogRow.cs b/src/client/spark/Risk/FakeTradeLogRow.cs new file mode 100644 index 000000000..6f86a7c37 --- /dev/null +++ b/src/client/spark/Risk/FakeTradeLogRow.cs @@ -0,0 +1,55 @@ +#nullable enable +namespace mbts { + using bts.api; + + using System; + + public class FakeTradeLogRow : ITradeLogRow { + public FakeTradeLogRow( + int trade_id, + SecurityExpiration security_expiration, + double? strike, + PutOrCall? call_put, + int size, + double price, + double theo, + string account, + bool is_day_trade, + DateTime time_entered, + string trader + ) { + this.trade_id = trade_id; + security_type = security_expiration.security_type; + symbol = security_expiration.symbol; + expiration = security_expiration.expiration; + this.strike = strike ?? double.NaN; + this.security_expiration = security_expiration; + this.call_put = call_put ?? PutOrCall.Call; + security_strike = new(security_expiration, this.strike, null); + this.size = size; + this.price = price; + this.theo = theo; + this.account = account; + this.is_day_trade = is_day_trade; + this.time_entered = time_entered; + this.trader = trader; + } + + public int trade_id { get; } + public SecurityType security_type { get; } + public string symbol { get; } + public DateTime? expiration { get; } + public double strike { get; } + public SecurityExpiration? security_expiration { get; } + public SecurityStrike? security_strike { get; } + public PutOrCall? call_put { get; } + public int size { get; } + public double price { get; } + public double theo { get; } + public string account { get; } + public bool is_day_trade { get; } + public DateTime time_entered { get; } + public string trader { get; } + } +} + diff --git a/src/client/spark/Risk/Slide/SlideReportCalculator.cs b/src/client/spark/Risk/Slide/SlideReportCalculator.cs index dcacca9bf..39928ef04 100644 --- a/src/client/spark/Risk/Slide/SlideReportCalculator.cs +++ b/src/client/spark/Risk/Slide/SlideReportCalculator.cs @@ -257,7 +257,7 @@ namespace mbts.analytics { var cpp_map = _vs_nonvol_data.get_cpp_map(); var fut_theo_map = VolSurfaceUtilityFunctions.compute_future_theos( account_fut_pos_map.Keys.Select( - kv => new SecurityExpiration(Venue.Unset, SecurityType.Future, kv.symbol, kv.expiration)), + kv => new SecurityExpiration(Venue.Sim, SecurityType.Future, kv.symbol, kv.expiration)), _vs_nonvol_data.input.exp_input_map, _future_prices_map); var fut_theo_map_vsexp = fut_theo_map.ToDictionary( @@ -265,7 +265,7 @@ namespace mbts.analytics { kv => kv.Value); var delta_terms_symbol = new mbts.SecurityTuple( - _report_params.RiskTermsType, _report_params.RiskTermsName, Venue.Unset); + _report_params.RiskTermsType, _report_params.RiskTermsName, Venue.Sim); var option_exp_slide_greeks = VolSurfaceRiskCalculator.compute_option_slide( _report_params.Greeks, diff --git a/src/client/spark/Risk/VolSurfaceRiskCalculator.cs b/src/client/spark/Risk/VolSurfaceRiskCalculator.cs index 19163e9d6..c6d6dc8c8 100644 --- a/src/client/spark/Risk/VolSurfaceRiskCalculator.cs +++ b/src/client/spark/Risk/VolSurfaceRiskCalculator.cs @@ -267,9 +267,9 @@ namespace mbts.analytics foreach (KeyValuePair>> kv in pos_map) { IEnumerable exp_theos = theo_map.TryGetValue(kv.Key, out exp_theos) ? exp_theos : null; - double cpp = cpp_map.TryGetValue(new SecurityTuple(SecurityType.Option, kv.Key.symbol, Venue.Unset), out cpp) ? cpp : Double.NaN, + double cpp = cpp_map.TryGetValue(new SecurityTuple(SecurityType.Option, kv.Key.symbol, Venue.Sim), out cpp) ? cpp : Double.NaN, delta_terms_sym_cpp = cpp_map.TryGetValue(delta_terms_symbol, out delta_terms_sym_cpp) ? delta_terms_sym_cpp : Double.NaN, - oev_terms_sym_cpp = cpp_map.TryGetValue(new SecurityTuple(SecurityType.Option, oev_terms_option_symbol, Venue.Unset), out oev_terms_sym_cpp) ? oev_terms_sym_cpp : Double.NaN; + oev_terms_sym_cpp = cpp_map.TryGetValue(new SecurityTuple(SecurityType.Option, oev_terms_option_symbol, Venue.Sim), out oev_terms_sym_cpp) ? oev_terms_sym_cpp : Double.NaN; rv[kv.Key] = _compute_single_exp_option_slide(ordered_greeks, perc_move_handles, kv.Value, VolSurfaceUtilityFunctions.hash_strikes(exp_theos), cpp, delta_terms_sym_cpp, oev_terms_sym_cpp); } return rv; @@ -376,7 +376,7 @@ namespace mbts.analytics IDictionary> rv = new Dictionary>(); foreach (string future_symbol in new HashSet(fut_pos_map.Keys.Select(e => e.symbol))) { - double cpp = cpp_map.TryGetValue(new mbts.SecurityTuple(SecurityType.Future, future_symbol, Venue.Unset), out cpp) ? cpp : Double.NaN, + double cpp = cpp_map.TryGetValue(new mbts.SecurityTuple(SecurityType.Future, future_symbol, Venue.Sim), out cpp) ? cpp : Double.NaN, delta_terms_symbol_cpp = cpp_map.TryGetValue(delta_terms_symbol, out delta_terms_symbol_cpp) ? delta_terms_symbol_cpp : Double.NaN; IDictionary> sym_rv = _compute_single_symbol_future_slide(ordered_greeks, handles, future_symbol, fut_theo_map, fut_pos_map, cpp, delta_terms_symbol_cpp); sym_rv.ToList().ForEach(kv => rv.Add(kv)); @@ -476,7 +476,7 @@ namespace mbts.analytics VolSurfaceExpiration exp = kv_exp.Key; double exp_pos = kv_exp.Value; VolSurfaceDriverTheo exp_theo = theo_map.TryGetValue(exp, out exp_theo) ? exp_theo : VolSurfaceDriverTheo.empty; - double cpp = cpp_map.TryGetValue(new SecurityTuple(SecurityType.Future, exp.symbol, Venue.Unset), out cpp) ? cpp : Double.NaN; + double cpp = cpp_map.TryGetValue(new SecurityTuple(SecurityType.Future, exp.symbol, Venue.Sim), out cpp) ? cpp : Double.NaN; IEnumerable> exp_handle_theos = _compute_future_slide_theos(perc_handles, exp_theo.theo, exp_theo.delta, exp_theo.roll); SlideContractUnit exp_val = new SlideContractUnit(n_greeks, exp_handle_theos.Select(t => t.Item3)); slide_expiration_map.Add(VolSurfaceRiskCalculator._to_sec_exp(exp, false), exp_val); @@ -513,7 +513,7 @@ namespace mbts.analytics { slide_strike_map = new Dictionary(); double delta_terms_cpp = cpp_map.TryGetValue (delta_terms_symbol, out delta_terms_cpp) ? delta_terms_cpp : Double.NaN, - oev_terms_cpp = cpp_map.TryGetValue(new SecurityTuple(SecurityType.Option, oev_terms_option_symbol,Venue.Unset), out oev_terms_cpp) ? oev_terms_cpp : Double.NaN; + oev_terms_cpp = cpp_map.TryGetValue(new SecurityTuple(SecurityType.Option, oev_terms_option_symbol,Venue.Sim), out oev_terms_cpp) ? oev_terms_cpp : Double.NaN; int n_greeks = ordered_greeks.Count(), zero_handle_index = perc_handles.find_first_index(0.0), pnl_index = ordered_greeks.find_first_index(VolSurfaceGreeks.Pnl); @@ -523,7 +523,7 @@ namespace mbts.analytics { SecurityExpiration exp = _to_sec_exp(kv_exp.Key, true); IDictionary> exp_pos_map = kv_exp.Value; - double cpp = cpp_map.TryGetValue(new SecurityTuple(SecurityType.Option, exp.symbol, Venue.Unset), out cpp) ? cpp : Double.NaN; + double cpp = cpp_map.TryGetValue(new SecurityTuple(SecurityType.Option, exp.symbol, Venue.Sim), out cpp) ? cpp : Double.NaN; /* strike */ foreach (var kv_k in exp_pos_map) { @@ -569,7 +569,7 @@ namespace mbts.analytics internal static mbts.SecurityExpiration _to_sec_exp(VolSurfaceExpiration exp, bool is_option) { - return new SecurityExpiration(Venue.Unset, is_option ? SecurityType.Option : SecurityType.Future, exp.symbol, exp.expiration); + return new SecurityExpiration(Venue.Sim, is_option ? SecurityType.Option : SecurityType.Future, exp.symbol, exp.expiration); } internal static IDictionary _to_strike_map(IDictionary> exp_map)