From 0a6f2d5adb24601bc85780cb8a5aec9fffc53b0d Mon Sep 17 00:00:00 2001 From: Taha Afzal Date: Fri, 4 Feb 2022 12:39:26 -0500 Subject: [PATCH] Normalizing options market data form coins to USD --- .../SingleVolSurfaceMarketImpliedVolSource.cs | 30 ++++++++++++++++--- .../Bus/SingleVolSurfaceRequestCalculator.cs | 2 +- .../MarketImpliedVol/VolSurfaceBBOUnit.cs | 16 ++++++++++ 3 files changed, 43 insertions(+), 5 deletions(-) diff --git a/src/client/spark/AW2/Bus/SingleVolSurfaceMarketImpliedVolSource.cs b/src/client/spark/AW2/Bus/SingleVolSurfaceMarketImpliedVolSource.cs index 4cb3e1243..fdee9ab35 100644 --- a/src/client/spark/AW2/Bus/SingleVolSurfaceMarketImpliedVolSource.cs +++ b/src/client/spark/AW2/Bus/SingleVolSurfaceMarketImpliedVolSource.cs @@ -179,15 +179,37 @@ private IDictionary> _get_bbo(VolSurfaceExpiration vs_exp) { - return _get_bbo(_bb, vs_exp); + return get_option_bbo(_bb, vs_exp); } + private static bool _normalize_bbo = true; #region Static helpers + + // for some markets (crypto ie), the options prices are not in + // the same terms as strikes, ie strikes are in USD whereras the + // option prices are in BTC + internal static double get_option_price_mult( + ThreadedBlackboard bb, + VolSurfaceExpiration option_exp) { + var driver_map = bb.read_state(SparkTags.AllOptionDriverBBOTag); + if (driver_map != null && + driver_map.TryGetValue(option_exp.symbol, out var sym_map)) { + var vs_bbo = sym_map.get_driver_bbo(option_exp.expiration); + return vs_bbo.get_weighted_mid_price(); + } + return Double.NaN; + } internal static IDictionary> - _get_bbo(ThreadedBlackboard bb, VolSurfaceExpiration vs_exp) { + get_option_bbo(ThreadedBlackboard bb, VolSurfaceExpiration vs_exp) { var sc_data = bb.read_state(SparkTags.SystemContractsTag); - var rv = sc_data.get_option_bbo_vsexp(vs_exp); - return rv.Item2; + IDictionary> rv = + sc_data.get_option_bbo_vsexp(vs_exp).Item2; + double mult = get_option_price_mult(bb, vs_exp); + if (_normalize_bbo) + return rv.ToDictionary(kv => kv.Key, kv => + Tuple.Create(kv.Value.Item1.mult_price(mult), + kv.Value.Item2.mult_price(mult))); + return rv; } internal static bool _ignore_market_implied_vol_source_settings( diff --git a/src/client/spark/AW2/Bus/SingleVolSurfaceRequestCalculator.cs b/src/client/spark/AW2/Bus/SingleVolSurfaceRequestCalculator.cs index 69add7513..ff8d75c03 100644 --- a/src/client/spark/AW2/Bus/SingleVolSurfaceRequestCalculator.cs +++ b/src/client/spark/AW2/Bus/SingleVolSurfaceRequestCalculator.cs @@ -146,7 +146,7 @@ namespace mbts.spark { VolSurfaceExpiration md_exp; VolCurveMarketImpliedVolData exp_impl_data = VolSurfaceCoreFunctions.compute_implied_vols(vs_inp, vs_exp, aux_data_map, - arg_vs_exp => SingleVolSurfaceMarketImpliedVolSource._get_bbo(_bb, arg_vs_exp), + arg_vs_exp => SingleVolSurfaceMarketImpliedVolSource.get_option_bbo(_bb, arg_vs_exp), impliedvol_side, SingleVolSurfaceMarketImpliedVolSource._ignore_market_implied_vol_source_settings( _bb, _vs_id), diff --git a/src/client/spark/Analytics/MarketImpliedVol/VolSurfaceBBOUnit.cs b/src/client/spark/Analytics/MarketImpliedVol/VolSurfaceBBOUnit.cs index 7ddd96f11..2e1ce0d1e 100644 --- a/src/client/spark/Analytics/MarketImpliedVol/VolSurfaceBBOUnit.cs +++ b/src/client/spark/Analytics/MarketImpliedVol/VolSurfaceBBOUnit.cs @@ -25,6 +25,11 @@ namespace mbts.analytics { ask_size = this_ask_size; } + public VolSurfaceBBOUnit mult_price(double mult) { + return new VolSurfaceBBOUnit(bid_price * mult, bid_size, + ask_price * mult, ask_size); + } + public VolSurfaceBBOUnit(NativePriceSize bid, NativePriceSize ask) : this(bid.price, bid.size, ask.price, ask.size) { } @@ -38,6 +43,17 @@ namespace mbts.analytics { return mid_price; } + public double get_weighted_mid_price(bool get_one_sided_mid = true) { + uint sum_q = bid_size + ask_size; + if (sum_q == 0) { + return Double.NaN; + } + double w_mid_price = (bid_price * bid_size + ask_price * ask_size) / sum_q; + if (Double.IsNaN(w_mid_price)) + w_mid_price = !Double.IsNaN(bid_price) ? bid_price : ask_price; + return w_mid_price; + } + public double get_bid_ask_spread() { return ask_price - bid_price; } public static VolSurfaceBBOUnit empty { get { return _empty; } }