diff --git a/src/client/spark/NMSheets/DataProviders/NMSheetSingleOptionMainDataSource.cs b/src/client/spark/NMSheets/DataProviders/NMSheetSingleOptionMainDataSource.cs index ec982fd87..8161c8282 100644 --- a/src/client/spark/NMSheets/DataProviders/NMSheetSingleOptionMainDataSource.cs +++ b/src/client/spark/NMSheets/DataProviders/NMSheetSingleOptionMainDataSource.cs @@ -8,13 +8,15 @@ using bts.api; using mbts.analytics; using mbts; -namespace BCG { +namespace BCG +{ /// For each NMSheetSymbol in an NMSheet gets all the /// data together (theos, position, vol change etc) /// and fires a callback. /// This is where all the data is gathered, the rest /// is all access to this - public class NMSheetSingleOptionMainDataSource : IDisposable { + public class NMSheetSingleOptionMainDataSource : IDisposable + { private ThreadedBlackboard _threaded_bb; private readonly ITag _in_bb_main_sheet_request_tag; private readonly ITag _in_bb_secmap_tag = @@ -24,6 +26,7 @@ namespace BCG { private readonly ITag _in_bb_precomputed_sparse_theos_tag; private readonly ITag> _in_bb_vol_shift_tag; private readonly ITag _in_bb_sheet_settings_tag; + private readonly ITag> _in_bb_trade_log_tag = SparkTags.TradeLogTag; private readonly ITag _in_exp_param_tag = SparkTags.VolSurfaceNonVolInputTag; private readonly ITag _in_bb_main_sheet_visibility_tag; private ITag _in_bb_onefive_value_tag = SparkTags.TC2Tags.VoidTag, @@ -31,7 +34,6 @@ namespace BCG { _in_bb_symbol_theo_shift_tag = SparkTags.TC2Tags.VoidTag, _in_bb_rollover_data_tag = SparkTags.TC2Tags.VoidTag, _in_bb_presaved_aw2_theos_tag = SparkTags.TC2Tags.VoidTag, - _in_bb_position_tag = SparkTags.TC2Tags.VoidTag, _in_bb_option_driver_detailed_price_tag = SparkTags.TC2Tags.VoidTag; private readonly NMSheetSymbol _nmsheet_symbol; private Action> _cb; @@ -40,7 +42,8 @@ namespace BCG { private string _electronic_equivelant_symbol; private NMSheetArchivedVolProvider _archived_vol_provider; - public NMSheetSingleOptionMainDataSource(object req_id, ThreadedBlackboard threaded_bb, NMSheetSymbol option, Action> callback) { + public NMSheetSingleOptionMainDataSource(object req_id, ThreadedBlackboard threaded_bb, NMSheetSymbol option, Action> callback) + { _threaded_bb = threaded_bb; _nmsheet_symbol = option; _electronic_equivelant_symbol = option.option_name; @@ -56,7 +59,8 @@ namespace BCG { _in_bb_sheet_settings_tag = SparkTags.TC2Tags.NMSheetSettingsTag(req_id.ToString()); _in_bb_main_sheet_visibility_tag = SparkTags.TC2Tags.NMSheetVisibilityTag(req_id.ToString()); _archived_vol_provider = new NMSheetArchivedVolProvider(req_id.ToString(), _threaded_bb, option, - () => { + () => + { _rollover_data = null; _compute_and_fire(); }); @@ -76,7 +80,7 @@ namespace BCG { _threaded_bb.subscribe(_in_bb_rollover_data_tag); _threaded_bb.subscribe(_in_bb_presaved_aw2_theos_tag); _threaded_bb.subscribe(_in_exp_param_tag); - _threaded_bb.subscribe(_in_bb_position_tag); + _threaded_bb.subscribe(_in_bb_trade_log_tag); _threaded_bb.subscribe(_in_bb_sheet_settings_tag); _threaded_bb.subscribe(_in_bb_onefive_value_tag); _threaded_bb.subscribe(_in_bb_qs_tag); @@ -84,15 +88,17 @@ namespace BCG { _threaded_bb.subscribe(_in_bb_option_driver_detailed_price_tag); } - private void _react_to_bb(object tag, object value) { + private void _react_to_bb(object tag, object value) + { if (_in_bb_main_sheet_request_tag.Equals(tag) || _in_bb_secmap_tag.Equals(tag) || _in_bb_live_theos_tag.Equals(tag) || /*_in_bb_live_driver_bbo_tag.Equals(tag) ||*/ _in_bb_precomputed_sparse_theos_tag.Equals(tag) || _in_bb_vol_shift_tag.Equals(tag) || _in_bb_symbol_theo_shift_tag.Equals(tag) || _in_exp_param_tag.Equals(tag) || _in_bb_presaved_aw2_theos_tag.Equals(tag) || - _in_bb_position_tag.Equals(tag) || _in_bb_rollover_data_tag.Equals(tag) || _in_bb_sheet_settings_tag.Equals(tag) || _in_bb_qs_tag.Equals(tag) || + _in_bb_trade_log_tag.Equals(tag) || _in_bb_rollover_data_tag.Equals(tag) || _in_bb_sheet_settings_tag.Equals(tag) || _in_bb_qs_tag.Equals(tag) || _in_bb_main_sheet_visibility_tag.Equals(tag)) _compute_and_fire(); } - private NMSheetDriverData _get_driver(DriverMD d) { + private NMSheetDriverData _get_driver(DriverMD d) + { double dr_pr = d.get_driver(); if (d.driver_security.HasValue) return new NMSheetDriverData(d.driver_security.symbol_or_default(), @@ -102,8 +108,10 @@ namespace BCG { return new NMSheetDriverData(String.Empty, Venue.Unset, DateTime.MinValue, dr_pr, dr_pr, false); } - private void _compute_and_fire() { - if (_is_main_sheet_active()) { + private void _compute_and_fire() + { + if (_is_main_sheet_active()) + { var timer = Stopwatch.StartNew(); NMSheetDataRequest data_request = _threaded_bb.read_state(_in_bb_main_sheet_request_tag) ?? new NMSheetDataRequest(new List>()); data_request = NMSheetDataRequest.restrict_for_symbol(data_request, _nmsheet_symbol); @@ -112,7 +120,8 @@ namespace BCG { NMSheetSettings sheet_settings = _threaded_bb.read_state(_in_bb_sheet_settings_tag) ?? new NMSheetSettings(); ExpStrikeKeyedData shared_live_theo_data_map = _threaded_bb.read_state(_in_bb_live_theos_tag); - if (data_request != null && live_driver_map != null && shared_live_theo_data_map != null) { + if (data_request != null && live_driver_map != null && shared_live_theo_data_map != null) + { ExpStrikeKeyedData output_theo_data_map = _limit_to_requested_securities(shared_live_theo_data_map, data_request, sheet_settings); output_theo_data_map = _adjust_for_freezes(output_theo_data_map, data_request); _add_vol_shift(output_theo_data_map); @@ -138,14 +147,17 @@ namespace BCG { } } - private bool _is_main_sheet_active() { + private bool _is_main_sheet_active() + { return _threaded_bb.read_state(_in_bb_main_sheet_visibility_tag, true); } - private ExpStrikeKeyedData _limit_to_requested_securities(ExpStrikeKeyedData theo_data, NMSheetDataRequest data_request, NMSheetSettings sheet_settings) { + private ExpStrikeKeyedData _limit_to_requested_securities(ExpStrikeKeyedData theo_data, NMSheetDataRequest data_request, NMSheetSettings sheet_settings) + { HashSet selected_exp_map = new HashSet(); HashSet selected_strike_map = new HashSet(sheet_settings.setting_unselected_strikes); - foreach (Triplet t in data_request.expiration_request_list) { + foreach (Triplet t in data_request.expiration_request_list) + { NMSheetExpiration nm_exp = t.first; if (nm_exp != null && nm_exp.option_name == _nmsheet_symbol.option_name) selected_exp_map.Add(nm_exp.option_expiration); @@ -153,12 +165,14 @@ namespace BCG { return theo_data.clone_filtered_set(selected_exp_map, true, selected_strike_map, false); } - private ExpStrikeKeyedData> _attach_sec_keys(ExpStrikeKeyedData theo_map) { + private ExpStrikeKeyedData> _attach_sec_keys(ExpStrikeKeyedData theo_map) + { theo_map = theo_map ?? new ExpStrikeKeyedData(); ExchangeSecurityMap sec_map = _threaded_bb.read_state(_in_bb_secmap_tag); if (sec_map != null) return sec_map.attach_sec_keys(theo_map, _electronic_equivelant_symbol, _nmsheet_symbol.option_exchange); - else { + else + { ExpStrikeKeyedData> rv = new ExpStrikeKeyedData>(); foreach (Triplet t in theo_map.get_data_linearly()) rv.set_data(t.first, t.second, new Triplet(t.third, 0, 0)); @@ -166,7 +180,8 @@ namespace BCG { } } - private ExpStrikeKeyedData _adjust_for_freezes(ExpStrikeKeyedData live_theo_map, NMSheetDataRequest data_request) { + private ExpStrikeKeyedData _adjust_for_freezes(ExpStrikeKeyedData live_theo_map, NMSheetDataRequest data_request) + { return live_theo_map; // TODO // ExpStrikeKeyedData rv = new ExpStrikeKeyedData(); @@ -192,9 +207,11 @@ namespace BCG { private Dictionary _get_option_exp_driver_data_map(NMSheetDataRequest data_request, ExpStrikeKeyedData output_theo_data_map, ExpStrikeKeyedData shared_live_theo_data_map, - Dictionary> live_driver_map) { + Dictionary> live_driver_map) + { Dictionary rv = new Dictionary(); - foreach (Triplet theo_request in data_request.expiration_request_list) { + foreach (Triplet theo_request in data_request.expiration_request_list) + { DateTime option_expiration = theo_request.first.option_expiration; /// live driver var live_driver_info = live_driver_map.GetValueOrDefault(option_expiration) @@ -208,9 +225,11 @@ namespace BCG { private Dictionary _get_option_exp_driver_data_map(NMSheetDataRequest data_request, - OptionDriverPriceMap drivers) { + OptionDriverPriceMap drivers) + { var rv = new Dictionary(); - foreach (var tr in data_request.expiration_request_list) { + foreach (var tr in data_request.expiration_request_list) + { NMSheetExpiration nm_exp = tr.first; SecurityExpiration exp = tr.first.to_sec_exp(); drivers.exp_price_map.TryGetValue(exp, out DriverMD dr_md); @@ -222,7 +241,8 @@ namespace BCG { return rv; } - private static double _get_driverprice_for_expiration(ExpStrikeKeyedData theo_data_map, DateTime option_expiration) { + private static double _get_driverprice_for_expiration(ExpStrikeKeyedData theo_data_map, DateTime option_expiration) + { IDictionary exp_data = theo_data_map.get_data_for_single_exp(option_expiration); if (exp_data != null) foreach (mbts.spark.TheoPoint tp in exp_data.Values) @@ -230,7 +250,8 @@ namespace BCG { return Double.NaN; } - private IDictionary _get_atm_data(ExpStrikeKeyedData theo_data) { + private IDictionary _get_atm_data(ExpStrikeKeyedData theo_data) + { NMSheetSettings sheet_settings = _threaded_bb.read_state(_in_bb_sheet_settings_tag) ?? new NMSheetSettings(); VolSurfaceNonVolInput vs_nvinp = _threaded_bb.read_state(_in_exp_param_tag); var option_exp_roll_data = vs_nvinp @@ -248,17 +269,43 @@ namespace BCG { } OptionContractKeyedData _rollover_data = null; - private OptionContractKeyedData _get_rollover_data() { + private OptionContractKeyedData _get_rollover_data() + { if (_rollover_data == null) _rollover_data = _archived_vol_provider.get_all_data(); return _rollover_data; } - private ExpStrikeKeyedData> _get_position_map() { - return new ExpStrikeKeyedData>(); + private ExpStrikeKeyedData> _get_position_map() + { + var sheet_settings = _threaded_bb.read_state(_in_bb_sheet_settings_tag); + var risk_account_text = sheet_settings?.setting_layout?.risk_account ?? string.Empty; + var risk_accounts = risk_account_text + .Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries) + .Select(a => a.Trim()) + .Where(a => !string.IsNullOrEmpty(a)) + .ToHashSet(StringComparer.OrdinalIgnoreCase); + var trades = _threaded_bb.read_state(_in_bb_trade_log_tag) ?? Enumerable.Empty(); + var option_trades = trades.Where(t => + t.is_active && + t.security_type == SecurityType.Option && + t.symbol == _nmsheet_symbol.option_name && + t.venue == _nmsheet_symbol.option_exchange && + (risk_accounts.Count == 0 || risk_accounts.Contains(t.account))); + var pos_data = SecurityPositionData.from_trades(option_trades); + var rv = new ExpStrikeKeyedData>(); + foreach (var (sec_exp, strike_map) in pos_data.option_pos_map) + { + foreach (var (strike, strike_pos) in strike_map) + { + rv.set_data(sec_exp.expiration, strike, Tuple.Create(strike_pos.call, strike_pos.put)); + } + } + return rv; } - private double _get_onefive() { + private double _get_onefive() + { // object dp_obj = _threaded_bb.sec_thread_retrieve_from_cache(_in_bb_onefive_value_tag); // if (dp_obj != null) { // DataPacket dp = dp_obj as DataPacket; @@ -270,22 +317,29 @@ namespace BCG { #region Vol Shift - private void _add_vol_shift(ExpStrikeKeyedData theo_data) { + private void _add_vol_shift(ExpStrikeKeyedData theo_data) + { var vshift_map = _threaded_bb.read_state(_in_bb_vol_shift_tag); if (vshift_map != null) add_vol_shift(_nmsheet_symbol, theo_data, vshift_map); } - internal static void add_vol_shift(NMSheetSymbol apply_to_symbol, ExpStrikeKeyedData theo_data, IDictionary exp_shift_map) { - foreach (KeyValuePair p in exp_shift_map) { - if (p.Key.nmsheet_symbol.Equals(apply_to_symbol)) { + internal static void add_vol_shift(NMSheetSymbol apply_to_symbol, ExpStrikeKeyedData theo_data, IDictionary exp_shift_map) + { + foreach (KeyValuePair p in exp_shift_map) + { + if (p.Key.nmsheet_symbol.Equals(apply_to_symbol)) + { DateTime option_expiration = p.Key.option_expiration; double shift = p.Value; - if (shift != 0) { + if (shift != 0) + { IDictionary exp_theos = theo_data.get_data_for_single_exp(option_expiration); - if (exp_theos != null) { + if (exp_theos != null) + { Dictionary shifted_exp_theos = new Dictionary(); - foreach (KeyValuePair strike_tp in exp_theos) { + foreach (KeyValuePair strike_tp in exp_theos) + { mbts.spark.TheoPoint new_tp = (mbts.spark.TheoPoint)strike_tp.Value.Clone(); add_vol_shift(new_tp, shift); shifted_exp_theos[strike_tp.Key] = new_tp; @@ -297,7 +351,8 @@ namespace BCG { } } - internal static void add_vol_shift(mbts.spark.TheoPoint v, double shift) { + internal static void add_vol_shift(mbts.spark.TheoPoint v, double shift) + { v.call += v.cvega * shift; v.put += v.pvega * shift; } @@ -305,7 +360,8 @@ namespace BCG { #endregion #region IDisposable - public void Dispose() { + public void Dispose() + { _threaded_bb.data_callback -= _react_to_bb; } #endregion