From dee32dead4e1b93af7aed394986eaa2182e53e50 Mon Sep 17 00:00:00 2001 From: Taha Date: Tue, 17 Sep 2024 22:17:23 -0400 Subject: [PATCH] Fixing implied vols for SPX (market data was getting divide dby the fwd) --- .../SingleVolSurfaceMarketImpliedVolSource.cs | 92 +++++++++++++------ .../AW2/Bus/VolSurfaceBlackboardManager.cs | 33 +++++-- src/client/spark/AW2/LiveTheoPublisher.cs | 3 +- src/lib/config/profiles/default/toggles.conf | 6 ++ 4 files changed, 93 insertions(+), 41 deletions(-) diff --git a/src/client/spark/AW2/Bus/SingleVolSurfaceMarketImpliedVolSource.cs b/src/client/spark/AW2/Bus/SingleVolSurfaceMarketImpliedVolSource.cs index 5ec5f9c45..6b79d3925 100644 --- a/src/client/spark/AW2/Bus/SingleVolSurfaceMarketImpliedVolSource.cs +++ b/src/client/spark/AW2/Bus/SingleVolSurfaceMarketImpliedVolSource.cs @@ -1,4 +1,5 @@ -namespace mbts.spark { +namespace mbts.spark +{ using bts; using bts.api; @@ -13,29 +14,37 @@ /// This class publishes the market implied vols for the /// selected expiration. - public class SingleVolSurfaceMarketImpliedVolSource : IDisposable { + public class SingleVolSurfaceMarketImpliedVolSource : IDisposable + { // This keeps the last computed exp and its bbo #region MD caching - private class CachedExpMD { + private class CachedExpMD + { public SecurityExpiration exp = SecurityExpiration.empty; public IDictionary> k_md_map = new Dictionary>(); private bool _has_changed( SecurityExpiration new_exp, - IDictionary> new_k_md_map) { - if (new_exp != exp) { + IDictionary> new_k_md_map) + { + if (new_exp != exp) + { return true; } - if (k_md_map.Count() != new_k_md_map.Count) { + if (k_md_map.Count() != new_k_md_map.Count) + { return true; } - foreach (var kv in k_md_map) { + foreach (var kv in k_md_map) + { Tuple k_md; - if (!new_k_md_map.TryGetValue(kv.Key, out k_md)) { + if (!new_k_md_map.TryGetValue(kv.Key, out k_md)) + { return true; } if (!kv.Value.Item1.Equals(k_md.Item1) || - !kv.Value.Item2.Equals(k_md.Item2)) { + !kv.Value.Item2.Equals(k_md.Item2)) + { return true; } } @@ -43,9 +52,11 @@ } public bool set( SecurityExpiration new_exp, - IDictionary> new_k_md_map) { + IDictionary> new_k_md_map) + { bool has_changed = _has_changed(new_exp, new_k_md_map); - if (has_changed) { + if (has_changed) + { exp = new_exp; k_md_map = new_k_md_map; } @@ -69,7 +80,8 @@ private TimeSpan _compute_period = TimeSpan.FromMilliseconds(333); private bool _is_dirty = true; - public SingleVolSurfaceMarketImpliedVolSource(int vs_id, ThreadedBlackboard bb) { + public SingleVolSurfaceMarketImpliedVolSource(int vs_id, ThreadedBlackboard bb) + { bts.Log.Gen.info($"SingleVolSurfaceMarketImpliedVolSource cons vs_id:{vs_id}"); _vs_id = vs_id; _bb = bb; @@ -91,7 +103,8 @@ _bb.timer_callback += _on_bb_timer; } - private void _on_bb_data(object tag, object data) { + private void _on_bb_data(object tag, object data) + { if (tag != null) if (_in_bb_committed_output_tag.Equals(tag) || _in_bb_calc_resp_tag.Equals(tag) || @@ -101,11 +114,13 @@ _is_dirty = true; } - private void _check_bbo_change() { + private void _check_bbo_change() + { var sc_data = _bb.read_state(SparkTags.SystemContractsTag); Tuple sel_exp_tup = _bb.read_state(_in_bb_selected_exp_tag); - if (sc_data == null || sel_exp_tup == null) { + if (sc_data == null || sel_exp_tup == null) + { return; } VolSurfaceExpiration new_sel_exp = sel_exp_tup.Item1.ToVSExp(SecurityType.Option); @@ -115,8 +130,10 @@ } private DateTime _last_publish_time = DateTime.MinValue; - private void _on_bb_timer() { - if (DateTime.Now - _last_publish_time >= _compute_period) { + private void _on_bb_timer() + { + if (DateTime.Now - _last_publish_time >= _compute_period) + { _check_bbo_change(); if (_is_dirty) _recompute_and_publish(); @@ -125,13 +142,15 @@ } } - private void _recompute_and_publish() { + private void _recompute_and_publish() + { Tuple sel_exp = _bb.read_state(_in_bb_selected_exp_tag); VolSurfaceOutput vs_output = _resolve_curr_cand(); if (vs_output != null && vs_output.volsurface_input != null && vs_output.aux_data_map != null && - sel_exp != null && sel_exp.Item1 != null) { + sel_exp != null && sel_exp.Item1 != null) + { var impliedvol_side = _get_market_impliedvol_side(_bb, _vs_id); var orig_exp = sel_exp.Item1.ToVSExp(SecurityType.Option); var timer = Stopwatch.StartNew(); @@ -149,7 +168,8 @@ "SingleVolSurfaceMarketImpliedVolSource", "SingleVolSurfaceMarketImpliedVolSource", timer.Elapsed.TotalMilliseconds); - if (exp_impl_vol_map is null) { + if (exp_impl_vol_map is null) + { return; } bts.Log.Gen.info( @@ -164,17 +184,21 @@ } // Returns the candidate or committed VolSurface depending - private VolSurfaceOutput _resolve_curr_cand() { + private VolSurfaceOutput _resolve_curr_cand() + { bool is_dirty = _bb.read_state(_in_bb_inp_dirty_tag, false); - if (is_dirty) { + if (is_dirty) + { VolSurfaceCalculationResponse vs_resp = _bb.read_state(_in_bb_calc_resp_tag); return vs_resp != null ? vs_resp.volsurface_calculation_output : null; - } else + } + else return _bb.read_state(_in_bb_committed_output_tag); } private IDictionary> - _get_bbo(VolSurfaceExpiration vs_exp) { + _get_bbo(VolSurfaceExpiration vs_exp) + { return get_option_bbo(_bb, vs_exp); } @@ -186,10 +210,14 @@ // option prices are in BTC internal static double get_option_price_mult( ThreadedBlackboard bb, - VolSurfaceExpiration option_exp) { + VolSurfaceExpiration option_exp) + { + if (option_exp.venue != Venue.Deribit) + return 1.0; var driver_map = bb.read_state(SparkTags.AllOptionDriverBBOTag); if (driver_map != null && - driver_map.TryGetValue((option_exp.symbol, option_exp.venue), out var sym_map)) { + driver_map.TryGetValue((option_exp.symbol, option_exp.venue), out var sym_map)) + { var vs_md = sym_map.get_driver_md(option_exp.expiration); return vs_md.get_driver(); } @@ -197,7 +225,8 @@ } internal static IDictionary> - get_option_bbo(ThreadedBlackboard bb, VolSurfaceExpiration vs_exp) { + get_option_bbo(ThreadedBlackboard bb, VolSurfaceExpiration vs_exp) + { var sc_data = bb.read_state(SparkTags.SystemContractsTag); IDictionary> rv = sc_data.get_option_bbo_vsexp(vs_exp).Item2; @@ -210,13 +239,15 @@ } internal static bool _ignore_market_implied_vol_source_settings( - ThreadedBlackboard bb, int vs_id) { + ThreadedBlackboard bb, int vs_id) + { return bb.read_state( SparkTags.VolSurfaceIgnoreImpliedVolSettingsTag(vs_id), false); } internal static VolSurfaceEnums.MarketSide - _get_market_impliedvol_side(ThreadedBlackboard bb, int vs_id) { + _get_market_impliedvol_side(ThreadedBlackboard bb, int vs_id) + { return bb.read_state( SparkTags.VolSurfaceMarketImpliedVolSide(vs_id), VolSurfaceEnums.MarketSide.Mid); @@ -225,7 +256,8 @@ #endregion #region IDisposable - public void Dispose() { + public void Dispose() + { _bb.data_callback -= _on_bb_data; _bb.timer_callback -= _on_bb_timer; } diff --git a/src/client/spark/AW2/Bus/VolSurfaceBlackboardManager.cs b/src/client/spark/AW2/Bus/VolSurfaceBlackboardManager.cs index bfdb61fba..143ec82c4 100644 --- a/src/client/spark/AW2/Bus/VolSurfaceBlackboardManager.cs +++ b/src/client/spark/AW2/Bus/VolSurfaceBlackboardManager.cs @@ -39,11 +39,14 @@ namespace mbts.spark this.bus = bus; _bb = bb; var contracts_source = bus.subscribe(SparkTags.SystemContractsTag); - if (contracts_throttle.HasValue) { - contracts_source = contracts_source.Sample(contracts_throttle.Value); + if (contracts_throttle.HasValue) + { + contracts_source = contracts_source.Sample(contracts_throttle.Value); } - var s = contracts_source.Subscribe(contracts => { - lock (_lock) { + var s = contracts_source.Subscribe(contracts => + { + lock (_lock) + { _on_data(contracts); } }); @@ -63,7 +66,8 @@ namespace mbts.spark } public void Dispose() { - lock (_lock) { + lock (_lock) + { _disps.ForEach(d => d.Dispose()); _option_driver_map.Values.DisposeAll(); _option_driver_map.Clear(); @@ -153,7 +157,12 @@ namespace mbts.spark Log.Ui.info($"VolSurfaceUsageList vs_usage acount: {vs_usage.Items.Count}"); foreach (var u in vs_usage.Items) { - if (!validate_vs_usage_unit(u, out var error)) { + if (string.Compare(u.OptionName, "SPX", 0) == 0 || string.Compare(u.OptionName, "SPXW", 0) == 0 && u.OptionVenue == Venue.Unset) + { + u.OptionVenue = Venue.Cboe; + } + if (!validate_vs_usage_unit(u, out var error)) + { Log.Gen.error($"Invalid VolSurfaceUsageUnit config. error: {error}; value: {u}"); continue; } @@ -199,16 +208,20 @@ namespace mbts.spark ); _disps.Add(theo_sub); - static bool validate_vs_usage_unit(VolSurfaceUsageUnit vs_usage_unit, [NotNullWhen(false)]out string error) { - if (vs_usage_unit.OptionVenue == Venue.Unset) { + static bool validate_vs_usage_unit(VolSurfaceUsageUnit vs_usage_unit, [NotNullWhen(false)] out string error) + { + if (vs_usage_unit.OptionVenue == Venue.Unset) + { error = "OptionVenue is unspecified"; return false; } - if (string.IsNullOrEmpty(vs_usage_unit.OptionName)) { + if (string.IsNullOrEmpty(vs_usage_unit.OptionName)) + { error = "OptionName is null/empty"; return false; } - if (string.IsNullOrEmpty(vs_usage_unit.VsName)) { + if (string.IsNullOrEmpty(vs_usage_unit.VsName)) + { error = "VsName is null/empty"; return false; } diff --git a/src/client/spark/AW2/LiveTheoPublisher.cs b/src/client/spark/AW2/LiveTheoPublisher.cs index a5f2c6cf3..073b78d19 100644 --- a/src/client/spark/AW2/LiveTheoPublisher.cs +++ b/src/client/spark/AW2/LiveTheoPublisher.cs @@ -10,7 +10,8 @@ using System.Windows.Automation; using bts.collections; namespace mbts.spark { - // Publishes theos for all options (on the side, also publishes the theos for TC2) + // Publishes theos for all options (on the side, also publishes the theos for TC2). + // Listens to driver and interp theos, on a change, calls _interp_and_publish public class LiveTheoPublisher : IDisposable { private ThreadedBlackboard _bb; private readonly ITag> diff --git a/src/lib/config/profiles/default/toggles.conf b/src/lib/config/profiles/default/toggles.conf index c6962739c..b5ebf2442 100644 --- a/src/lib/config/profiles/default/toggles.conf +++ b/src/lib/config/profiles/default/toggles.conf @@ -111,5 +111,11 @@ VolSurfaceUsage { option_name: "BTC" vs_name: "BTC_moneyness" } + items: { + option_name: "SPX" + option_venue: VENUE_CBOE + vs_name: "SPX_moneyness" + } + } }