diff --git a/src/client/spark/AW2/Bus/SingleVolSurfaceGateway.cs b/src/client/spark/AW2/Bus/SingleVolSurfaceGateway.cs index 0cd23d73c..375245d60 100644 --- a/src/client/spark/AW2/Bus/SingleVolSurfaceGateway.cs +++ b/src/client/spark/AW2/Bus/SingleVolSurfaceGateway.cs @@ -45,12 +45,8 @@ namespace mbts.spark { } private void _log_basic_vs_info(VolSurfaceInput vs_inp) { - string exp_str = "expirations: "; - foreach (var kv in vs_inp.surface_parameters.curve_parameters_map) { - exp_str += " " + kv.Item1.to_security_expiration().ToString(); - } - bts.Log.Gen.info(String.Format("SingleVolSurfaceGateway vs_id:{0} {1}", - vs_inp.surface_settings.core_settings.vol_surface_id, exp_str)); + bts.Log.Gen.info(String.Format("SingleVolSurfaceGateway vs_id:{0} vs:{1}", + vs_inp.surface_settings.core_settings.vol_surface_id, vs_inp.log_string())); } private void _on_vss(bts.api.core.Update update) { diff --git a/src/client/spark/AW2/Bus/SingleVolSurfaceManager.cs b/src/client/spark/AW2/Bus/SingleVolSurfaceManager.cs index c7d70917b..fc78c5db4 100644 --- a/src/client/spark/AW2/Bus/SingleVolSurfaceManager.cs +++ b/src/client/spark/AW2/Bus/SingleVolSurfaceManager.cs @@ -30,7 +30,7 @@ namespace mbts.spark { vs_id, t_bb, syms => new SingleVolSurfaceNonVolDataSource(vs_id, syms, t_bb)), new SingleVolSurfaceDataSource(SingleVolSurfaceDataSource.SymbolListType.Committed, vs_id, t_bb, a_syms => new SingleVolSurfaceCommittedOutputRetriever(vs_id, t_bb, a_syms)), - new SingleVolSurfaceDataSource(SingleVolSurfaceDataSource.SymbolListType.Candidate, vs_id, t_bb, + new SingleVolSurfaceDataSource(SingleVolSurfaceDataSource.SymbolListType.Combined, vs_id, t_bb, a_syms => new SingleVolSurfaceRequestCalculator(vs_id, a_syms, t_bb)), new SingleVolSurfaceMarketImpliedVolSource(vs_id, t_bb), new SingleVolSurfaceTheoSource(vs_id, t_bb), diff --git a/src/client/spark/AW2/Bus/SingleVolSurfaceRequestCalculator.cs b/src/client/spark/AW2/Bus/SingleVolSurfaceRequestCalculator.cs index 65a66a146..9e2fa472a 100644 --- a/src/client/spark/AW2/Bus/SingleVolSurfaceRequestCalculator.cs +++ b/src/client/spark/AW2/Bus/SingleVolSurfaceRequestCalculator.cs @@ -30,7 +30,8 @@ namespace mbts.spark { /// data private VolSurfaceOutput _last_output; - public SingleVolSurfaceRequestCalculator(int vs_id, IEnumerable symbols, ThreadedBlackboard threaded_bb) { + public SingleVolSurfaceRequestCalculator(int vs_id, IEnumerable symbols, + ThreadedBlackboard threaded_bb) { bts.Log.Gen.info($"SingleVolSurfaceRequestCalculator cons vs_id:{vs_id}"); _vs_id = vs_id; _symbols = new HashSet(symbols); @@ -70,21 +71,27 @@ namespace mbts.spark { effective_req = curr_req; else { if (_last_output != null) - effective_req = new VolSurfaceCalculationRequest(_last_output.volsurface_input, curr_req.request_id); + effective_req = + new VolSurfaceCalculationRequest(_last_output.volsurface_input, curr_req.request_id); } if (effective_req != null) { IDictionary aux_data_map = VolSurfaceUtilityFunctions.compute_aux_data(effective_req.volsurface_input, - nonvol_data.input, nonvol_data.driver_price_map, vs_standard_time, true); - IDictionary action_map = _post_process_action(effective_req, aux_data_map); + nonvol_data.input, + nonvol_data.driver_price_map, + vs_standard_time, true); + IDictionary action_map = + _post_process_action(effective_req, aux_data_map); _last_output = GeneralVolSurfaceCalculator.compute(effective_req.volsurface_input, nonvol_data.contracts, aux_data_map, action_map, null, - effective_req.cascade_up, effective_req.cascade_down); + effective_req.cascade_up, + effective_req.cascade_down); _timer.stop(); - bts.Log.Gen.info(String.Format("SingleVolSurfaceRequestCalculator. Time to compute vol for {0} exps {1} msecs", + bts.Log.Gen.info(String.Format( + "SingleVolSurfaceRequestCalculator._recompute_and_publish Time to compute vol for {0} exps {1} msecs", nonvol_data.contracts.Count, _timer.lastIntervalTime)); _bb.post(SparkTags.VolSurfaceCalculationResponseTag(_vs_id), new VolSurfaceCalculationResponse(_last_output, effective_req.request_id)); @@ -93,7 +100,8 @@ namespace mbts.spark { } /// Computes the impl vol if the action is fit to market - private IDictionary _post_process_action(VolSurfaceCalculationRequest req, + private IDictionary _post_process_action( + VolSurfaceCalculationRequest req, IDictionary aux_data_map) { IDictionary rv = req.action_map; /// TODO : VolCurveAction needs to be structured better. The UI enforces that @@ -103,7 +111,8 @@ namespace mbts.spark { VolCurveAction rep_action = rv.ElementAt(0).Value; if (rep_action.action_type == VolCurveAction.ActionType.FitToMarket) { IDictionary impl_vol_map = - _fill_in_implied_vols(req.volsurface_input, req.action_map, aux_data_map, req.cascade_up, req.cascade_down); + _fill_in_implied_vols(req.volsurface_input, req.action_map, aux_data_map, + req.cascade_up, req.cascade_down); rv = impl_vol_map.ToDictionary(kv => kv.Key, kv => new VolCurveAction( new VolCurveMarketFitAction(rep_action.market_fit_action.fit_action, kv.Value, rep_action.market_fit_action.use_weighted_regression))); @@ -112,22 +121,28 @@ namespace mbts.spark { return rv; } - private IDictionary _fill_in_implied_vols(VolSurfaceInput vs_inp, + private IDictionary _fill_in_implied_vols( + VolSurfaceInput vs_inp, IDictionary action_map, IDictionary aux_data_map, bool cascade_up, bool cascade_down) { - IDictionary rv = new Dictionary(); + IDictionary rv = + new Dictionary(); VolCurveAction rep_action = action_map.ElementAt(0).Value; - HashSet master_exps = VolSurfaceUtilityFunctions.get_master_expirations(vs_inp, true); + HashSet master_exps = + VolSurfaceUtilityFunctions.get_master_expirations(vs_inp, true); IEnumerable applicable_exps; if (cascade_up || cascade_down) - applicable_exps = VolSurfaceUtilityFunctions.get_exp_list_from_cascade(vs_inp.surface_parameters.curve_parameters_map.Select(t => t.Item1), - action_map.Keys, cascade_up, cascade_down); + applicable_exps = + VolSurfaceUtilityFunctions.get_exp_list_from_cascade( + vs_inp.surface_parameters.curve_parameters_map.Select(t => t.Item1), + action_map.Keys, cascade_up, cascade_down); else applicable_exps = action_map.Keys; applicable_exps = applicable_exps.Where(vs_exp => master_exps.Contains(vs_exp)); - VolSurfaceEnums.MarketSide impliedvol_side = SingleVolSurfaceMarketImpliedVolSource._get_market_impliedvol_side(_bb, _vs_id); + VolSurfaceEnums.MarketSide impliedvol_side = + SingleVolSurfaceMarketImpliedVolSource._get_market_impliedvol_side(_bb, _vs_id); foreach (VolSurfaceExpiration vs_exp in applicable_exps) { VolSurfaceExpiration md_exp; VolCurveMarketImpliedVolData exp_impl_data = diff --git a/src/client/spark/AW2/Grid/VolSurfaceDisplayGrid.cs b/src/client/spark/AW2/Grid/VolSurfaceDisplayGrid.cs index 7faeb1034..9249ce37f 100644 --- a/src/client/spark/AW2/Grid/VolSurfaceDisplayGrid.cs +++ b/src/client/spark/AW2/Grid/VolSurfaceDisplayGrid.cs @@ -10,11 +10,9 @@ using mbts.ui.shell; using mbts.spark; using bts.utils; -namespace mbts.ui.controls -{ +namespace mbts.ui.controls { /// Encapsulates the vol data that would show up in the top grid - public class VolSurfaceDisplayGrid : ICloneable, IGridDisplayModel, ICellDataStore - { + public class VolSurfaceDisplayGrid : ICloneable, IGridDisplayModel, ICellDataStore { private readonly IEnumerable _ordered_expirations = null; private readonly IEnumerable _ordered_columns = null; private readonly IDictionary _column_map; @@ -30,35 +28,36 @@ namespace mbts.ui.controls }).ToArray()); } + public IDictionary> data_map { + get { + return _data_map; + } + } public VolSurfaceDisplayGrid(IEnumerable ordered_expirations, IEnumerable ordered_columns, - IDictionary> data) - { + IDictionary> data) { _ordered_expirations = ordered_expirations; _ordered_columns = ordered_columns; _data_map = data; _column_map = ordered_columns.ToDictionary(k => k.unique_col_name, k => k); } - public object get_grid_data(VolSurfaceDisplayGridExpiration exp, VolSurfaceDisplayGridColumn col, object default_value) - { + public object get_grid_data(VolSurfaceDisplayGridExpiration exp, VolSurfaceDisplayGridColumn col, object default_value) { object rv = default_value; return get_grid_data(exp, col, out rv) ? rv : default_value; } - public bool get_grid_data(VolSurfaceDisplayGridExpiration exp, VolSurfaceDisplayGridColumn col, out object data) - { + public bool get_grid_data(VolSurfaceDisplayGridExpiration exp, VolSurfaceDisplayGridColumn col, out object data) { data = null; IDictionary exp_data; if (_data_map.TryGetValue(exp, out exp_data)) if (exp_data.TryGetValue(col, out data)) return true; return false; - } + } public bool copy(VolSurfaceDisplayGridExpiration source_exp, VolSurfaceDisplayGridTextInputColumn source_col, - VolSurfaceDisplayGridExpiration target_exp, VolSurfaceDisplayGridTextInputColumn target_col) - { + VolSurfaceDisplayGridExpiration target_exp, VolSurfaceDisplayGridTextInputColumn target_col) { object data; if (get_grid_data(source_exp, source_col, out data)) return set_grid_data(target_exp, target_col, data); @@ -68,18 +67,16 @@ namespace mbts.ui.controls #region Comparison Helpers /// Returns the expiration differences public static void compare_exps(VolSurfaceDisplayGrid new_grid, VolSurfaceDisplayGrid old_grid, - out IEnumerable deleted, out IEnumerable added) - { + out IEnumerable deleted, out IEnumerable added) { HashSet new_grid_map = new HashSet(new_grid.get_ordered_expirations()), old_grid_map = new HashSet(old_grid.get_ordered_expirations()), del, add; - CollectionHelper.compare_hashsets(new_grid_map, old_grid_map, out del, out add); + CollectionHelper.compare_hashsets(new_grid_map, old_grid_map, out del, out add); deleted = del; added = add; } - public static bool cols_changed(VolSurfaceDisplayGrid d1, VolSurfaceDisplayGrid d2) - { + public static bool cols_changed(VolSurfaceDisplayGrid d1, VolSurfaceDisplayGrid d2) { IEnumerable d1_extra, d2_extra; compare_cols(d1, d2, out d1_extra, out d2_extra); return d1_extra.Count() > 0 || d2_extra.Count() > 0; @@ -88,8 +85,7 @@ namespace mbts.ui.controls /// Returns the col name differences (it will generally be very rare, but for instance when a point gets added /// to a polynomial or the spline model, d1 or d2 wont be empty) public static void compare_cols(VolSurfaceDisplayGrid d1, VolSurfaceDisplayGrid d2, - out IEnumerable d1_extra, out IEnumerable d2_extra) - { + out IEnumerable d1_extra, out IEnumerable d2_extra) { HashSet d1_exp_map = new HashSet(d1.get_ordered_columns()), d2_exp_map = new HashSet(d2.get_ordered_columns()), del, add; @@ -104,12 +100,10 @@ namespace mbts.ui.controls /// Returns the differernce between two VolSurfaceDisplay's. /// It only returns the elements that are present in both /// and are different. - public static IEnumerable> compare_data(VolSurfaceDisplayGrid d1, VolSurfaceDisplayGrid d2) - { - List> rv = new List> (); - foreach (VolSurfaceDisplayGridExpiration exp in CollectionHelper.append_arrays (d1.get_ordered_expirations(), d2.get_ordered_expirations())) - foreach (VolSurfaceDisplayGridColumn col in CollectionHelper.append_arrays(d1.get_ordered_columns(), d2.get_ordered_columns())) - { + public static IEnumerable> compare_data(VolSurfaceDisplayGrid d1, VolSurfaceDisplayGrid d2) { + List> rv = new List>(); + foreach (VolSurfaceDisplayGridExpiration exp in CollectionHelper.append_arrays(d1.get_ordered_expirations(), d2.get_ordered_expirations())) + foreach (VolSurfaceDisplayGridColumn col in CollectionHelper.append_arrays(d1.get_ordered_columns(), d2.get_ordered_columns())) { object d1_data, d2_data; if (d1.get_grid_data(exp, col, out d1_data) && d2.get_grid_data(exp, col, out d2_data) && d1 != d2) rv.Add(Tuple.Create(exp, col)); @@ -119,11 +113,10 @@ namespace mbts.ui.controls #endregion #region ICloneable - public object Clone() - { - Dictionary> data = new Dictionary> (); + public object Clone() { + Dictionary> data = new Dictionary>(); foreach (KeyValuePair> p in _data_map) - data.Add (p.Key, new Dictionary (p.Value)); + data.Add(p.Key, new Dictionary(p.Value)); return new VolSurfaceDisplayGrid(new List(_ordered_expirations), new List(_ordered_columns), data); @@ -137,12 +130,10 @@ namespace mbts.ui.controls #endregion #region ICellDataStore - public string read(VolSurfaceDisplayGridExpiration row, IDataGridDisplayModelColumn col) - { + public string read(VolSurfaceDisplayGridExpiration row, IDataGridDisplayModelColumn col) { VolSurfaceDisplayGridColumn grid_col = col as VolSurfaceDisplayGridColumn; grid_col = grid_col != null ? (_column_map.TryGetValue(grid_col.unique_col_name, out grid_col) ? grid_col : null) : null; - if (grid_col != null) - { + if (grid_col != null) { object value; if (get_grid_data(row, grid_col, out value)) return grid_col.format(value); @@ -150,29 +141,24 @@ namespace mbts.ui.controls return string.Empty; } - public bool write(VolSurfaceDisplayGridExpiration row, IDataGridDisplayModelColumn col, string value) - { + public bool write(VolSurfaceDisplayGridExpiration row, IDataGridDisplayModelColumn col, string value) { VolSurfaceDisplayGridTextInputColumn grid_col = col as VolSurfaceDisplayGridTextInputColumn; return grid_col != null ? set_grid_data(row, grid_col, grid_col.parse(value)) : false; } - public object get_display_info(VolSurfaceDisplayGridExpiration row, IDataGridDisplayModelColumn col) - { + public object get_display_info(VolSurfaceDisplayGridExpiration row, IDataGridDisplayModelColumn col) { return null; // ?? //return read(row, col); } - public void handle_tick(VolSurfaceDisplayGridExpiration row, IDataGridDisplayModelColumn col, TickDirection tick, out string new_value) - { + public void handle_tick(VolSurfaceDisplayGridExpiration row, IDataGridDisplayModelColumn col, TickDirection tick, out string new_value) { VolSurfaceDisplayGridColumn mapped_col = col as VolSurfaceDisplayGridColumn; mapped_col = mapped_col != null ? (_column_map.TryGetValue(mapped_col.unique_col_name, out mapped_col) ? mapped_col : null) : null; VolSurfaceDisplayGridTextInputColumn grid_col = mapped_col as VolSurfaceDisplayGridTextInputColumn; - new_value = String.Empty; - if (grid_col != null) - { + new_value = String.Empty; + if (grid_col != null) { object data; - if (get_grid_data(row, grid_col, out data)) - { + if (get_grid_data(row, grid_col, out data)) { data = grid_col.tick_move(data, tick == TickDirection.UP); new_value = grid_col.format(data); set_grid_data(row, grid_col, data); @@ -180,29 +166,49 @@ namespace mbts.ui.controls } } - public bool can_tick(VolSurfaceDisplayGridExpiration row, IDataGridDisplayModelColumn col, TickDirection tick) - { + public bool can_tick(VolSurfaceDisplayGridExpiration row, IDataGridDisplayModelColumn col, TickDirection tick) { return true; } #endregion - internal void set_grid_data_text(VolSurfaceDisplayGridExpiration exp, string col_name, string formatted_data) - { + internal void set_grid_data_text(VolSurfaceDisplayGridExpiration exp, string col_name, string formatted_data) { VolSurfaceDisplayGridColumn grid_col; if (_column_map.TryGetValue(col_name, out grid_col)) write(exp, grid_col, formatted_data); } - internal bool set_grid_data(VolSurfaceDisplayGridExpiration exp, VolSurfaceDisplayGridColumn col, object data) - { + internal bool set_grid_data(VolSurfaceDisplayGridExpiration exp, VolSurfaceDisplayGridColumn col, object data) { IDictionary exp_data; if (_data_map.TryGetValue(exp, out exp_data)) - if (exp_data.ContainsKey(col)) - { + if (exp_data.ContainsKey(col)) { exp_data[col] = data; return true; } return false; } + + // internal string log_string { + // get { + // Func trim_leading_comma = (s) => { + // if (!s.IsNullOrEmpty() && s.EndsWith(",")) { + // return s.Remove(s.Length - 1, 1); + // } + // return s; + // }; + // Func, string> log_exp = (map) => { + // string rv = String.Empty; + // foreach (var kv in map) { + // string val_string = kv.Value == null ? "Null" : kv.Value.ToString(); + // rv += $"{kv.Key.display_name}:{val_string},"; + // } + // return trim_leading_comma(rv); + // }; + // string rv = String.Empty; + // foreach (var kv in _data_map) { + // rv += $"Exp:{kv.Key.expiration.ToShortDateString()} Data:{log_exp(kv.Value)},"; + // } + // return trim_leading_comma(rv); + // } + // } } } \ No newline at end of file diff --git a/src/client/spark/AW2/Misc/VolSurfaceTroubleshootingFunctions.cs b/src/client/spark/AW2/Misc/VolSurfaceTroubleshootingFunctions.cs new file mode 100644 index 000000000..d9473cad7 --- /dev/null +++ b/src/client/spark/AW2/Misc/VolSurfaceTroubleshootingFunctions.cs @@ -0,0 +1,74 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using mbts; +using mbts.analytics; +using mbts.ui.controls; +using mbts.ui.shell; +using mbts.spark; +using bts.utils; + +namespace mbts.spark { + /// This contains the list of global values for the vol + /// surface (whereas VolSurfaceDisplayGrid has exp keyed + /// values) + public static class VolSurfaceTroubleshootingFunctions { + public static string log_string(this VolSurfaceInput vs_inp) { + string rv = "Null"; + if (vs_inp.surface_parameters != null && vs_inp.surface_parameters.curve_parameters_map != null) { + rv = String.Empty; + foreach (var kv in vs_inp.surface_parameters.curve_parameters_map) { + var exp = kv.Item1; + var exp_data = kv.Item2; + double atm_vol = VolSurfaceDataFunctions.get_atm_vol(vs_inp.surface_settings, exp_data); + rv += $"exp:{exp.expiration.ToShortDateString()} atm_vol:{atm_vol} \n"; + } + } + return rv; + } + + internal static string log_string(this VolSurfaceDisplayModel.DisplayModelData dm_data) { + string rv = String.Empty; + string global_grid_str = dm_data.global_grid == null ? "Null" : dm_data.global_grid.log_string(); + rv += $"global_grid: {global_grid_str} \n"; + string exp_grid_str = dm_data.exp_grid == null ? "Null" : dm_data.exp_grid.log_string(); + rv += $"exp_grid: {exp_grid_str} \n"; + return rv; + } + + public static string log_string(this VolSurfaceDisplayGlobalGrid ggrid) { + string rv = String.Empty; + foreach (var col in ggrid.get_ordered_columns()) { + object col_data = null; + ggrid.get_grid_data(col, out col_data); + string col_data_str = col_data == null ? "Null" : col_data.ToString(); + rv += $"{col.display_name}:{col_data_str} "; + } + return rv; + } + + internal static string log_string(this VolSurfaceDisplayGrid dgrid) { + Func trim_leading_comma = (s) => { + if (!s.IsNullOrEmpty() && s.EndsWith(",")) { + return s.Remove(s.Length - 1, 1); + } + return s; + }; + Func, string> log_exp = (map) => { + string rv = String.Empty; + foreach (var kv in map) { + string val_string = kv.Value == null ? "Null" : kv.Value.ToString(); + rv += $"{kv.Key.display_name}:{val_string},"; + } + return trim_leading_comma(rv); + }; + string rv = String.Empty; + foreach (var kv in dgrid.data_map) { + rv += $"Exp:{kv.Key.expiration.ToShortDateString()} Data:{log_exp(kv.Value)},"; + } + return trim_leading_comma(rv); + } + } +} diff --git a/src/client/spark/AW2/VolSurfaceDisplayModel.cs b/src/client/spark/AW2/VolSurfaceDisplayModel.cs index 8f6d9b775..44e32c26b 100644 --- a/src/client/spark/AW2/VolSurfaceDisplayModel.cs +++ b/src/client/spark/AW2/VolSurfaceDisplayModel.cs @@ -371,6 +371,12 @@ #region Blackboard response private void _on_server_update(VolSurfaceOutput vs_output) { + Action fire_committed_change = () => { + bool event_hooked = on_committed_grid_change != null; + bts.Log.Ui.info($"VolSurfaceDiplayModel._on_server_update firing on_committed_grid_change event_hooked: {event_hooked} committed_data: {_committed_data.log_string()}"); + if (event_hooked) + on_committed_grid_change(); + }; if (vs_output != null) { vs_output = vs_output.Clone() as VolSurfaceOutput; if (_state == DisplayModelState.WaitingForCommittedState) { @@ -379,8 +385,7 @@ _committed_data.vs_output = vs_output; VolSurfaceUIAdapter.copy_output_to_ui(_committed_data.vs_output, _grid_settings, out _committed_data.exp_grid, out _committed_data.global_grid, out _committed_data.exp_graph_map); _change_state(DisplayModelState.Idle); - if (on_committed_grid_change != null) - on_committed_grid_change(); + fire_committed_change(); if (on_graph_change != null) on_graph_change(); } else if (_state == DisplayModelState.Idle || _state == DisplayModelState.WaitingForCalcResponse || _state == DisplayModelState.ResponseReady) { @@ -388,8 +393,7 @@ bts.Log.Ui.diag(String.Format("Received committed update for vol surface id {0} state {1}", _vs_id, _state.ToString())); _committed_data.vs_output = vs_output; VolSurfaceUIAdapter.copy_output_to_ui(_committed_data.vs_output, _grid_settings, out _committed_data.exp_grid, out _committed_data.global_grid, out _committed_data.exp_graph_map); - if (on_committed_grid_change != null) - on_committed_grid_change(); + fire_committed_change(); if (on_graph_change != null) on_graph_change(); } else @@ -510,7 +514,7 @@ } else _log_bad_transition(_state, "commit"); } - #endregion + #endregion /// Put Rot, Call Rot, RR, Strangle and ATM /// operation go call this