diff --git a/src/client/spark/AW2/Bus/SingleVolSurfaceManager.cs b/src/client/spark/AW2/Bus/SingleVolSurfaceManager.cs index c5356b93f..f3335f476 100644 --- a/src/client/spark/AW2/Bus/SingleVolSurfaceManager.cs +++ b/src/client/spark/AW2/Bus/SingleVolSurfaceManager.cs @@ -28,9 +28,10 @@ namespace mbts.spark { syms => new SingleVolSurfaceOptionDriverSource(vs_id, syms, t_bb)), new SingleVolSurfaceDataSource(SingleVolSurfaceDataSource.SymbolListType.Combined, vs_id, t_bb, syms => new SingleVolSurfaceNonVolDataSource(vs_id, syms, t_bb)), - new SingleVolSurfaceDataSource(SingleVolSurfaceDataSource.SymbolListType.Committed, vs_id, 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, a_syms => new SingleVolSurfaceRequestCalculator(vs_id, a_syms, t_bb)), + new SingleVolSurfaceDataSource(SingleVolSurfaceDataSource.SymbolListType.Candidate, 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), // new SingleVolSurfaceHeatmapManager (vs_id, t_bb), diff --git a/src/client/spark/AW2/Bus/SingleVolSurfaceRequestCalculator.cs b/src/client/spark/AW2/Bus/SingleVolSurfaceRequestCalculator.cs index 50169ed3a..65a66a146 100644 --- a/src/client/spark/AW2/Bus/SingleVolSurfaceRequestCalculator.cs +++ b/src/client/spark/AW2/Bus/SingleVolSurfaceRequestCalculator.cs @@ -9,20 +9,18 @@ using bts.api; using mbts.analytics; namespace mbts.spark { - /// - /// All local changes to the VolSurface that trigger calculations - /// are handled here. - /// Subs : - /// 1. VS Calc Req - /// 2. VS Nonvol Data - /// 3. Dirty - /// Pubs : - /// 1. Calc Resp - /// + // All local changes to the VolSurface that trigger calculations + // are handled here. + // Subs : + // 1. VS Calc Req + // 2. VS Nonvol Data + // 3. Dirty + // Pubs : + // 1. Calc Resp public class SingleVolSurfaceRequestCalculator : IDisposable { private readonly int _vs_id; private readonly HashSet _symbols; - private readonly ThreadedBlackboard _threaded_bb; + private readonly ThreadedBlackboard _bb; private readonly ITag _in_bb_request_tag; private readonly ITag _in_bb_nonvol_data_tag; private readonly ITag _in_bb_vs_standard_time_tag; @@ -33,20 +31,21 @@ namespace mbts.spark { private VolSurfaceOutput _last_output; 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); - _threaded_bb = threaded_bb; + _bb = threaded_bb; _in_bb_request_tag = SparkTags.VolSurfaceCalculationRequestTag(vs_id); _in_bb_nonvol_data_tag = SparkTags.VolSurfaceNonVolDataTag(vs_id); _in_bb_vs_standard_time_tag = SparkTags.VolSurfaceStandardTimeTag(vs_id); _in_bb_market_impliedvol_side_tag = SparkTags.VolSurfaceMarketImpliedVolSide(vs_id); _in_bb_dirty_tag = SparkTags.VolSurfaceDirtyStateTag(vs_id); - _threaded_bb.subscribe(_in_bb_request_tag); - _threaded_bb.subscribe(_in_bb_nonvol_data_tag); - _threaded_bb.subscribe(_in_bb_vs_standard_time_tag); - _threaded_bb.subscribe(_in_bb_vs_standard_time_tag); - _threaded_bb.subscribe(_in_bb_dirty_tag); - _threaded_bb.data_callback += _on_bb_data; + _bb.subscribe(_in_bb_request_tag); + _bb.subscribe(_in_bb_nonvol_data_tag); + _bb.subscribe(_in_bb_vs_standard_time_tag); + _bb.subscribe(_in_bb_vs_standard_time_tag); + _bb.subscribe(_in_bb_dirty_tag); + _bb.data_callback += _on_bb_data; _recompute_and_publish(true); } @@ -59,13 +58,13 @@ namespace mbts.spark { } private void _recompute_and_publish(bool is_new_request) { - object is_dirty_obj = _threaded_bb.sec_thread_retrieve_from_cache(_in_bb_dirty_tag); + object is_dirty_obj = _bb.sec_thread_retrieve_from_cache(_in_bb_dirty_tag); bool is_dirty = is_dirty_obj is bool && (bool)is_dirty_obj; - VolSurfaceNonVolData nonvol_data = _threaded_bb.sec_thread_retrieve_from_cache(_in_bb_nonvol_data_tag) as VolSurfaceNonVolData; - VolSurfaceStandardTime vs_standard_time = _threaded_bb.sec_thread_retrieve_from_cache(_in_bb_vs_standard_time_tag) as VolSurfaceStandardTime; + VolSurfaceNonVolData nonvol_data = _bb.read_state(_in_bb_nonvol_data_tag); + VolSurfaceStandardTime vs_standard_time = _bb.read_state(_in_bb_vs_standard_time_tag); if (is_dirty && nonvol_data != null) { _timer.start(); - VolSurfaceCalculationRequest curr_req = _threaded_bb.sec_thread_retrieve_from_cache(_in_bb_request_tag) as VolSurfaceCalculationRequest, + VolSurfaceCalculationRequest curr_req = _bb.read_state(_in_bb_request_tag), effective_req = null; if (is_new_request) effective_req = curr_req; @@ -74,7 +73,9 @@ namespace mbts.spark { 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 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); _last_output = GeneralVolSurfaceCalculator.compute(effective_req.volsurface_input, nonvol_data.contracts, @@ -83,14 +84,17 @@ namespace mbts.spark { null, 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", nonvol_data.contracts.Count, _timer.lastIntervalTime)); - _threaded_bb.post(SparkTags.VolSurfaceCalculationResponseTag(_vs_id), new VolSurfaceCalculationResponse(_last_output, effective_req.request_id)); + bts.Log.Gen.info(String.Format("SingleVolSurfaceRequestCalculator. 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)); } } } /// Computes the impl vol if the action is fit to market - private IDictionary _post_process_action(VolSurfaceCalculationRequest req, IDictionary aux_data_map) { + 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 /// all exps have the same action, but the structure doesnt. So here the UI @@ -98,16 +102,20 @@ namespace mbts.spark { if (rv != null && rv.Count > 0) { 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); - 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))); + IDictionary impl_vol_map = + _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))); } } return rv; } - private IDictionary _fill_in_implied_vols(VolSurfaceInput vs_inp, IDictionary action_map, - IDictionary aux_data_map, - bool cascade_up, bool cascade_down) { + 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(); VolCurveAction rep_action = action_map.ElementAt(0).Value; HashSet master_exps = VolSurfaceUtilityFunctions.get_master_expirations(vs_inp, true); @@ -119,10 +127,15 @@ namespace mbts.spark { 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(_threaded_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 = VolSurfaceCoreFunctions.compute_implied_vols(vs_inp, vs_exp, aux_data_map, arg_vs_exp => SingleVolSurfaceMarketImpliedVolSource._get_bbo(_threaded_bb, arg_vs_exp), impliedvol_side, SingleVolSurfaceMarketImpliedVolSource._ignore_market_implied_vol_source_settings(_threaded_bb, _vs_id), out 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), + impliedvol_side, + SingleVolSurfaceMarketImpliedVolSource._ignore_market_implied_vol_source_settings(_bb, _vs_id), + out md_exp); rv[vs_exp] = exp_impl_data; } return rv; @@ -133,7 +146,7 @@ namespace mbts.spark { public void Dispose() { if (!_disposed) { _disposed = true; - _threaded_bb.data_callback -= _on_bb_data; + _bb.data_callback -= _on_bb_data; } } #endregion