From 7b04a7849bab0fb3962bfe213ca2ce8cf19eabb7 Mon Sep 17 00:00:00 2001 From: Taha Afzal Date: Wed, 8 Jun 2022 00:25:15 -0400 Subject: [PATCH] Future delta fixed in Slide --- .../spark/Risk/VolSurfaceSlideCalculator.cs | 35 ++++++++++++------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/src/client/spark/Risk/VolSurfaceSlideCalculator.cs b/src/client/spark/Risk/VolSurfaceSlideCalculator.cs index 58dbe08d7..53d2aaffa 100644 --- a/src/client/spark/Risk/VolSurfaceSlideCalculator.cs +++ b/src/client/spark/Risk/VolSurfaceSlideCalculator.cs @@ -303,6 +303,8 @@ namespace mbts.analytics { return handle_greeks; } + + public static IDictionary> compute_future_slide(IEnumerable ordered_greeks, IEnumerable handles, mbts.SecurityTuple delta_terms_symbol, @@ -316,12 +318,6 @@ namespace mbts.analytics { 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)); } - //rv = rv.ToDictionary(kv => new VolSurfaceExpiration() - //{ - // symbol = kv.Key.symbol, - // expiration = mbts.ui.utility.ExpirationFormatter.convert_real_to_mapped(SecurityType.type.FUTURE, kv.Key.symbol, kv.Key.expiration) - //}, - //kv => kv.Value); return rv; } @@ -335,7 +331,9 @@ namespace mbts.analytics { IDictionary> rv = new Dictionary>(); foreach (KeyValuePair kv in future_pos_map.Where(kv => kv.Key.symbol == fut_sym)) { VolSurfaceDriverTheo theo_tup = fut_theo_map.TryGetValue(kv.Key, out theo_tup) ? theo_tup : null; - IEnumerable exp_pos_greeks = _compute_single_exp_future_slide(ordered_greeks, handles, + IEnumerable exp_pos_greeks = _compute_single_exp_future_slide( + kv.Key, + ordered_greeks, handles, theo_tup != null ? theo_tup.effective_theo : Double.NaN, theo_tup != null ? theo_tup.effective_delta : Double.NaN, theo_tup != null ? theo_tup.roll : Double.NaN, @@ -348,8 +346,18 @@ namespace mbts.analytics { } /// Computes the handles for one exp - private static IEnumerable _compute_single_exp_future_slide(IEnumerable ordered_greeks, IEnumerable handles, - double theo, double delta, double roll, double pos) { + private static IEnumerable _compute_single_exp_future_slide( + VolSurfaceExpiration vs_exp, + IEnumerable ordered_greeks, IEnumerable handles, + double theo, double delta, double roll, double pos) { + Func normalize_delta = (delta, handle_theo) => { + // TODO: + bool qty_in_usd = vs_exp.symbol == "BTC" || vs_exp.symbol == "ETH"; + if (qty_in_usd) { + return handle_theo == 0 ? Double.NaN : delta / handle_theo; + } + return delta; + }; IEnumerable> handle_theos = _compute_future_slide_theos(handles, theo, delta, roll); int pnl_index = Array.FindIndex(ordered_greeks.ToArray(), g => g == VolSurfaceGreeks.Pnl), delta_index = Array.FindIndex(ordered_greeks.ToArray(), g => g == VolSurfaceGreeks.Delta), @@ -358,17 +366,18 @@ namespace mbts.analytics { flat_vol_delta_index = Array.FindIndex(ordered_greeks.ToArray(), g => g == VolSurfaceGreeks.FlatVolDelta); List rv_handle_pos_greeks = new List(); foreach (Tuple t in handle_theos) { + double handle_theo = t.Item1; double[] position_greeks = Enumerable.Repeat(0.0, ordered_greeks.Count()).ToArray(); if (pnl_index >= 0) position_greeks[pnl_index] = t.Item1 * pos; if (delta_index >= 0) - position_greeks[delta_index] = t.Item2 * pos; + position_greeks[delta_index] = normalize_delta(t.Item2 * pos, handle_theo); if (bsm_delta_index >= 0) - position_greeks[bsm_delta_index] = t.Item2 * pos; + position_greeks[bsm_delta_index] = normalize_delta(t.Item2 * pos, handle_theo); if (flat_vol_delta_index >= 0) - position_greeks[flat_vol_delta_index] = t.Item2 * pos; + position_greeks[flat_vol_delta_index] = normalize_delta(t.Item2 * pos, handle_theo); if (exp_delta_index >= 0) - position_greeks[exp_delta_index] = t.Item2 * pos; + position_greeks[exp_delta_index] = normalize_delta(t.Item2 * pos, handle_theo); rv_handle_pos_greeks.Add(new SlideGreekUnit(t.Item3, position_greeks)); } return rv_handle_pos_greeks;