Added VolSurfaceDisplayModel
This commit is contained in:
1 parent
9f28ef19ef
commit
f2d000a91a
2 files changed
+607
No files matched your search
@@ -0,0 +1,604 @@
|
||||
using bts.api;
|
||||
|
||||
using mbts;
|
||||
using mbts.analytics;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using mbts.ui.controls;
|
||||
using bts.utils;
|
||||
using System.Reactive.Linq;
|
||||
using System.Collections;
|
||||
|
||||
namespace mbts.spark {
|
||||
public class VolSurfaceDisplayModel : IDisposable {
|
||||
public enum DisplayModelState {
|
||||
/// Start state, as soon as the first
|
||||
/// server state is received, it gets
|
||||
/// out from here to Idle
|
||||
WaitingForCommittedState = 0,
|
||||
/// When there is nothing to commit/revert
|
||||
/// (ie no local changes)
|
||||
Idle,
|
||||
/// When the UI makes a calculation request
|
||||
/// (either through a data change, action
|
||||
/// or settings change)
|
||||
WaitingForCalcResponse,
|
||||
/// Once the calculation response is received
|
||||
/// it goes to this (from ResponseWaiting)
|
||||
ResponseReady
|
||||
}
|
||||
|
||||
public class DisplayModelData : ICloneable {
|
||||
private VolSurfaceDisplayModel _model;
|
||||
public VolSurfaceOutput vs_output = null;
|
||||
public VolSurfaceDisplayGlobalGrid global_grid = null;
|
||||
public VolSurfaceDisplayGrid exp_grid = null;
|
||||
public IDictionary<VolSurfaceDisplayGridExpiration, VolCurveDisplayGraph> exp_graph_map = null;
|
||||
|
||||
public DisplayModelData(VolSurfaceDisplayModel model) { _model = model; }
|
||||
|
||||
public object Clone() {
|
||||
DisplayModelData rv = new DisplayModelData(_model);
|
||||
rv.vs_output = vs_output != null ? vs_output.Clone() as VolSurfaceOutput : null;
|
||||
VolSurfaceUIAdapter.copy_output_to_ui(vs_output, _model._grid_settings, out rv.exp_grid, out rv.global_grid, out rv.exp_graph_map);
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
|
||||
private readonly int _vs_id;
|
||||
private readonly IBus _bus;
|
||||
private readonly ITag<mbts.analytics.VolSurfaceOutput> _in_bb_committed_output_tag;
|
||||
private readonly ITag<VolSurfaceCalculationResponse> _in_bb_calculation_response_tag;
|
||||
private readonly ITag<IDictionary<VolSurfaceExpiration, Tuple<VolSurfaceExpiration, VolCurveMarketImpliedVolData>>> _in_bb_impl_vol_tag;
|
||||
private readonly ITag<IEnumerable<VolSurfaceInputSnapshot>> _in_bb_state_list_tag;
|
||||
private readonly bool _publish_lifetime;
|
||||
|
||||
internal void global_vol_shift(object is_up) {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
private readonly VolSurfaceDisplayGrid _empty_grid = new VolSurfaceDisplayGrid(new VolSurfaceDisplayGridExpiration[] { },
|
||||
new VolSurfaceDisplayGridColumn[] { },
|
||||
new Dictionary<VolSurfaceDisplayGridExpiration, IDictionary<VolSurfaceDisplayGridColumn, object>>());
|
||||
private DisplayModelState _state = DisplayModelState.WaitingForCommittedState;
|
||||
private DisplayModelData _committed_data, _candidate_data;
|
||||
private int _request_version_no = 0;
|
||||
private IDictionary<VolSurfaceExpiration, Tuple<VolSurfaceExpiration, VolCurveMarketImpliedVolData>> _impl_vol_map = new Dictionary<VolSurfaceExpiration, Tuple<VolSurfaceExpiration, VolCurveMarketImpliedVolData>>();
|
||||
private IEnumerable<VolSurfaceInputSnapshot> _input_snapshot_list = new List<VolSurfaceInputSnapshot>();
|
||||
|
||||
#region Events
|
||||
public event Action on_committed_grid_change, on_candidate_grid_change = () => { };
|
||||
public event Action on_graph_change;
|
||||
public event Action on_dirty_change;
|
||||
#endregion
|
||||
|
||||
public VolSurfaceDisplayModel(int vs_id, IBus bus, bool publish_lifetime = false) {
|
||||
_vs_id = vs_id;
|
||||
_bus = bus;
|
||||
_publish_lifetime = publish_lifetime;
|
||||
_committed_data = new DisplayModelData(this);
|
||||
_candidate_data = new DisplayModelData(this);
|
||||
_in_bb_committed_output_tag = SparkTags.VolSurfaceCommittedOutputTag(vs_id);
|
||||
_in_bb_calculation_response_tag = SparkTags.VolSurfaceCalculationResponseTag(vs_id);
|
||||
_in_bb_impl_vol_tag = SparkTags.VolSurfaceMarketImpliedVolTag(vs_id);
|
||||
_in_bb_state_list_tag = SparkTags.VolSurfaceInputSnapshotListTag(vs_id);
|
||||
_bus.post(SparkTags.VolSurfaceActivationTag(vs_id), true);
|
||||
|
||||
_subscriptions = Disposable.Create(
|
||||
_bus.subscribe(_in_bb_committed_output_tag).OnDispatcher().Subscribe(_on_server_update),
|
||||
_bus.subscribe(_in_bb_calculation_response_tag).OnDispatcher().Subscribe(_on_calculation_response),
|
||||
_bus.subscribe(_in_bb_impl_vol_tag).OnDispatcher().Subscribe(_on_impl_vol_update),
|
||||
_bus.subscribe(_in_bb_state_list_tag).OnDispatcher().Subscribe(_on_state_list_update));
|
||||
if (_publish_lifetime)
|
||||
_bus.post(SparkTags.VolSurfaceActiveUITag(vs_id), true);
|
||||
}
|
||||
|
||||
|
||||
private IDisposable _subscriptions;
|
||||
|
||||
#region Expiration Grid Getters/Setters
|
||||
|
||||
public VolSurfaceDisplayGrid get_committed_grid() { return _committed_data.exp_grid != null ? _committed_data.exp_grid.Clone() as VolSurfaceDisplayGrid : _empty_grid; }
|
||||
|
||||
public VolSurfaceDisplayGrid get_active_grid() { return is_dirty ? (_candidate_data.exp_grid != null ? _candidate_data.exp_grid.Clone() as VolSurfaceDisplayGrid : _empty_grid) : get_committed_grid(); }
|
||||
|
||||
public void set_candidate_grid(VolSurfaceDisplayGrid exp_grid) {
|
||||
if (_state == DisplayModelState.Idle || _state == DisplayModelState.ResponseReady) {
|
||||
_candidate_data.exp_grid = exp_grid.Clone() as VolSurfaceDisplayGrid;
|
||||
DisplayModelData curr_data = _get_current_data();
|
||||
VolSurfaceInput vs_inp = VolSurfaceUIAdapter.copy_ui_to_input(_candidate_data.exp_grid, curr_data.vs_output.volsurface_input.surface_settings, _grid_settings);
|
||||
vs_inp.surface_parameters.vol_time = curr_data.vs_output.volsurface_input.surface_parameters.vol_time;
|
||||
_copy_time_parameters(curr_data.vs_output.volsurface_input, ref vs_inp);
|
||||
_change_state(DisplayModelState.WaitingForCalcResponse);
|
||||
_bus.post(SparkTags.VolSurfaceCalculationRequestTag(_vs_id),
|
||||
new VolSurfaceCalculationRequest(vs_inp, _request_version_no++));
|
||||
} else
|
||||
_log_bad_transition(_state, "set candidate grid");
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Globals
|
||||
public VolSurfaceEnums.VolSurfaceTimeMode get_active_vol_time_mode() {
|
||||
var curr_data = _get_current_data();
|
||||
if (curr_data != null) {
|
||||
var ts = curr_data.vs_output.volsurface_input.surface_settings.core_settings.time_settings;
|
||||
if (ts != null) return ts.time_mode;
|
||||
}
|
||||
return VolSurfaceEnums.VolSurfaceTimeMode.Manual;
|
||||
}
|
||||
public DateTime get_active_vol_time() {
|
||||
DisplayModelData curr_data = _get_current_data();
|
||||
return (curr_data != null && curr_data.vs_output != null) ? curr_data.vs_output.get_vol_time() : DateTime.MinValue;
|
||||
}
|
||||
public DateTime get_committed_vol_time() {
|
||||
return (_committed_data != null && _committed_data.vs_output != null) ? _committed_data.vs_output.volsurface_input.surface_parameters.vol_time : DateTime.MinValue;
|
||||
}
|
||||
public void set_candidate_vol_time(DateTime vol_time) {
|
||||
if (_state == DisplayModelState.Idle || _state == DisplayModelState.ResponseReady) {
|
||||
DisplayModelData curr_data = _get_current_data();
|
||||
if (curr_data != null) {
|
||||
VolSurfaceInput vs_input = VolSurfaceUtilityFunctions.clone_proto(curr_data.vs_output.volsurface_input);
|
||||
vs_input.surface_parameters.vol_time = vol_time;
|
||||
VolSurfaceCalculationRequest req = new VolSurfaceCalculationRequest(vs_input, _request_version_no++);
|
||||
_change_state(DisplayModelState.WaitingForCalcResponse);
|
||||
_bus.post(SparkTags.VolSurfaceCalculationRequestTag(_vs_id), req);
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Gets the Auto Decay Points for this VolSurface.
|
||||
/// </summary>
|
||||
/// <returns>The AutoDecay Points for this VolSurface, or null if the VolSurfaceTimeMode (found in VolSurfaceSettings.core_settings.time_settings) is not set to AutoDecay.</returns>
|
||||
public IEnumerable<Tuple<DateTime, double>> get_active_time_decay_points() {
|
||||
DisplayModelData curr_data = _get_current_data();
|
||||
if (curr_data == null || curr_data.vs_output == null) return null;
|
||||
var ts = curr_data.vs_output.volsurface_input.surface_settings.core_settings.time_settings;
|
||||
if (ts == null || ts.time_mode != VolSurfaceEnums.VolSurfaceTimeMode.AutoDecay) return null;
|
||||
var tp = curr_data.vs_output.volsurface_input.surface_parameters.time_parameters;
|
||||
return tp == null ? Enumerable.Empty<Tuple<DateTime, double>>() : tp.auto_decay_points ?? Enumerable.Empty<Tuple<DateTime, double>>();
|
||||
}
|
||||
public void set_candidate_time_decay_parameters(IEnumerable<Tuple<DateTime, double>> auto_decay_points) {
|
||||
if (_state == DisplayModelState.Idle || _state == DisplayModelState.ResponseReady) {
|
||||
DisplayModelData curr_data = _get_current_data();
|
||||
if (curr_data != null) {
|
||||
VolSurfaceInput vs_input = VolSurfaceUtilityFunctions.clone_proto(curr_data.vs_output.volsurface_input);
|
||||
if (vs_input.surface_parameters.time_parameters == null) vs_input.surface_parameters.time_parameters = new VolSurfaceTimeParameters();
|
||||
vs_input.surface_parameters.time_parameters.auto_decay_points = auto_decay_points.ToList();
|
||||
VolSurfaceCalculationRequest req = new VolSurfaceCalculationRequest(vs_input, _request_version_no++);
|
||||
_change_state(DisplayModelState.WaitingForCalcResponse);
|
||||
_bus.post(SparkTags.VolSurfaceCalculationRequestTag(_vs_id), req);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Settings
|
||||
/// Will always be called from the UI when the wizard changes
|
||||
/// a setting
|
||||
public void set_candidate_settings(mbts.analytics.VolSurfaceSettings settings) {
|
||||
if (_state == DisplayModelState.Idle || _state == DisplayModelState.ResponseReady) {
|
||||
DisplayModelData curr_data = _get_current_data();
|
||||
if (curr_data != null && settings != null) {
|
||||
VolSurfaceInput vs_input = VolSurfaceUtilityFunctions.clone_proto(curr_data.vs_output.volsurface_input);
|
||||
vs_input.surface_settings = settings;
|
||||
VolSurfaceCalculationRequest req = new VolSurfaceCalculationRequest(vs_input, _request_version_no++);
|
||||
_change_state(DisplayModelState.WaitingForCalcResponse);
|
||||
_bus.post(SparkTags.VolSurfaceCalculationRequestTag(_vs_id), req);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public VolSurfaceSettings get_active_settings() {
|
||||
var curr_data = _get_current_data();
|
||||
return curr_data == null ? null : curr_data.vs_output.volsurface_input.surface_settings;
|
||||
}
|
||||
|
||||
private VolSurfaceMacroGridSettings _grid_settings = null;
|
||||
public void publish_grid_settings(VolSurfaceMacroGridSettings settings) {
|
||||
_grid_settings = settings;
|
||||
_bus.post(SparkTags.VolSurfaceMacroGridSettingsTag(_vs_id), settings);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Graph Getters
|
||||
|
||||
public IEnumerable<VolSurfaceDisplayGridExpiration> get_ordered_expirations() {
|
||||
VolSurfaceDisplayGrid current_grid = get_active_grid();
|
||||
return current_grid != null && current_grid.get_ordered_expirations() != null ? current_grid.get_ordered_expirations() : new List<VolSurfaceDisplayGridExpiration>();
|
||||
}
|
||||
|
||||
private readonly VolCurveDisplayGraph _empty_graph = new VolCurveDisplayGraph(new List<Tuple<double, double>>(), new List<Tuple<double, double>>(), new List<Tuple<double, double>>(), new Tuple<double, double>(Double.NaN, Double.NaN), Double.NaN, Double.NaN);
|
||||
public VolCurveDisplayGraph get_committed_exp_vol_graph(VolSurfaceDisplayGridExpiration exp) {
|
||||
VolCurveDisplayGraph disp_graph = null;
|
||||
return _committed_data.exp_graph_map != null && _committed_data.exp_graph_map.TryGetValue(exp, out disp_graph) ? disp_graph : _empty_graph;
|
||||
}
|
||||
|
||||
public IEnumerable<Tuple<double, double>> get_committed_volpath(VolSurfaceDisplayGridExpiration exp) {
|
||||
if (_committed_data != null && _committed_data.vs_output != null) {
|
||||
VolCurveOutput exp_out = _committed_data.vs_output[new VolSurfaceExpiration(exp.symbol, exp.expiration)];
|
||||
return VolSurfaceUtilityFunctions.get_vol_path_points(exp_out);
|
||||
} else
|
||||
return Enumerable.Empty<Tuple<double, double>>();
|
||||
}
|
||||
|
||||
public IEnumerable<Tuple<double, double>> get_candidate_volpath(VolSurfaceDisplayGridExpiration exp) {
|
||||
if (is_dirty && _candidate_data != null && _candidate_data.vs_output != null) {
|
||||
VolCurveOutput exp_out = _candidate_data.vs_output[new VolSurfaceExpiration(exp.symbol, exp.expiration)];
|
||||
return VolSurfaceUtilityFunctions.get_vol_path_points(exp_out);
|
||||
} else
|
||||
return Enumerable.Empty<Tuple<double, double>>();
|
||||
}
|
||||
|
||||
public VolCurveDisplayGraph get_candidate_exp_vol_graph(VolSurfaceDisplayGridExpiration exp) {
|
||||
VolCurveDisplayGraph disp_graph = null;
|
||||
return is_dirty && _candidate_data.exp_graph_map != null && _candidate_data.exp_graph_map.TryGetValue(exp, out disp_graph) ? disp_graph : _empty_graph;
|
||||
}
|
||||
|
||||
public Dictionary<VolSurfaceExpiration, double> get_committed_term_structure_map() {
|
||||
return _committed_data == null || _committed_data.vs_output == null ? new Dictionary<VolSurfaceExpiration, double>() : _committed_data.vs_output.exp_output_map.ToDictionary(kv => kv.Key, kv => kv.Value.atm_vol);
|
||||
}
|
||||
|
||||
public Dictionary<VolSurfaceExpiration, double> get_candidate_term_structure_map() {
|
||||
return !is_dirty || _candidate_data == null || _candidate_data.vs_output == null ? new Dictionary<VolSurfaceExpiration, double>() : _candidate_data.vs_output.exp_output_map.ToDictionary(kv => kv.Key, kv => kv.Value.atm_vol);
|
||||
}
|
||||
|
||||
public VolCurveMarketImpliedVolData get_market_market_implied_vols(VolSurfaceDisplayGridExpiration exp) {
|
||||
Tuple<VolSurfaceExpiration, VolCurveMarketImpliedVolData> rv = null;
|
||||
return _impl_vol_map.TryGetValue(new VolSurfaceExpiration() { symbol = exp.symbol, expiration = exp.expiration }, out rv) ? rv.Item2 : null;
|
||||
}
|
||||
|
||||
public VolSurfaceExpiration get_market_implied_vol_source(VolSurfaceDisplayGridExpiration exp) {
|
||||
Tuple<VolSurfaceExpiration, VolCurveMarketImpliedVolData> rv = null;
|
||||
return _impl_vol_map.TryGetValue(new VolSurfaceExpiration() { symbol = exp.symbol, expiration = exp.expiration }, out rv) ? rv.Item1 : null;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Detail View
|
||||
public void show_details() {
|
||||
_bus.post(SparkTags.VolSurfaceLaunchTheoWindow, _vs_id);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region State History
|
||||
|
||||
public event Action on_previous_state_list_change = () => { };
|
||||
public IEnumerable<string> get_previous_states() {
|
||||
if (_input_snapshot_list != null) {
|
||||
IEnumerable<VolSurfaceInputSnapshot> input_list = _input_snapshot_list.OrderBy(s => s.save_time_utc);
|
||||
input_list = input_list.OrderBy(vss => vss.save_time_utc).Reverse();
|
||||
return input_list.Select(s => s.save_marker);
|
||||
} else
|
||||
return new string[] { };
|
||||
}
|
||||
|
||||
public void load_previous_state(string state_name) {
|
||||
//mbts.Logger.log(bts.log.Level.Info, bts.log.Subsystem.ui, String.Format("VolSurfaceDisplayModel load_previous_state called with state {0}", state_name));
|
||||
bts.Log.Ui.info(String.Format("VolSurfaceDisplayModel load_previous_state called with state {0}", state_name));
|
||||
if (_state == DisplayModelState.Idle || _state == DisplayModelState.ResponseReady) {
|
||||
bool succ = false;
|
||||
VolSurfaceInputSnapshot vs_sn = _input_snapshot_list.ToList().Find(v => v.save_marker == state_name);
|
||||
if (vs_sn != null) {
|
||||
VolSurfaceInput vs_inp = VolSurfaceUtilityFunctions.deserialize_proto<VolSurfaceInput>(vs_sn.data);
|
||||
if (vs_inp != null) {
|
||||
_change_state(DisplayModelState.WaitingForCalcResponse);
|
||||
_bus.post(SparkTags.VolSurfaceCalculationRequestTag(_vs_id),
|
||||
new VolSurfaceCalculationRequest(vs_inp, _request_version_no++));
|
||||
succ = true;
|
||||
}
|
||||
}
|
||||
if (!succ)
|
||||
_bus.post(SparkTags.MessageDisplayTag, new MessageDisplayData("Load Vol Surface Error", ("Could not load previously saved Vol Surface")));
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region File operation
|
||||
|
||||
/// Returns the bytes for the current Vol Surface
|
||||
/// (ie candidate if dirty, else committed).
|
||||
/// Returns null if there is no data
|
||||
public byte[] get_current_state() {
|
||||
DisplayModelData curr_data = _get_current_data();
|
||||
if (curr_data != null && curr_data.vs_output != null && curr_data.vs_output.volsurface_input != null)
|
||||
return VolSurfaceUtilityFunctions.serialize_proto(curr_data.vs_output.volsurface_input);
|
||||
else
|
||||
return null;
|
||||
}
|
||||
|
||||
/// Sets the state that is passed in bytes form. Returns
|
||||
/// true is state is set successfully
|
||||
public bool set_state(byte[] file_data) {
|
||||
VolSurfaceInput vs_input = VolSurfaceUtilityFunctions.deserialize_proto<VolSurfaceInput>(file_data);
|
||||
if (vs_input != null) {
|
||||
if (_state == DisplayModelState.Idle || _state == DisplayModelState.ResponseReady) {
|
||||
_change_state(DisplayModelState.WaitingForCalcResponse);
|
||||
_bus.post(SparkTags.VolSurfaceCalculationRequestTag(_vs_id),
|
||||
new VolSurfaceCalculationRequest(vs_input, _request_version_no++));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool csv_importing_enabled() { return true; }
|
||||
|
||||
public void import_csv_file(string file_path) {
|
||||
try {
|
||||
System.IO.StreamReader reader = new System.IO.StreamReader(file_path);
|
||||
string header = reader.ReadLine();
|
||||
string[] col_names = header.Split(',').Select(cn => cn.Trim()).ToArray();
|
||||
IDictionary<int, string> file_col_map = new Dictionary<int, string>();
|
||||
/// first 2 cols are always symbol and expiration
|
||||
for (int i = 2; i < col_names.Length; i++)
|
||||
file_col_map.Add(i, col_names[i]);
|
||||
VolSurfaceDisplayGrid grid = get_active_grid().Clone() as VolSurfaceDisplayGrid;
|
||||
while (!reader.EndOfStream) {
|
||||
string line = reader.ReadLine();
|
||||
string[] data_items = line.Split(',').Select(cn => cn.Trim()).ToArray();
|
||||
if (data_items.Length != col_names.Length)
|
||||
throw new ApplicationException("Data is not well formed");
|
||||
VolSurfaceDisplayGridExpiration grid_exp = new VolSurfaceDisplayGridExpiration(data_items[0], DateTime.Parse(data_items[1]), VolSurfaceEnums.VolSurfaceExpirationCalculationMethod.Master);
|
||||
for (int i = 2; i < col_names.Length; i++) {
|
||||
string col_name = file_col_map[i];
|
||||
string data_text = data_items[i];
|
||||
if (!String.IsNullOrEmpty(data_text))
|
||||
grid.set_grid_data_text(grid_exp, col_name, data_text);
|
||||
}
|
||||
}
|
||||
set_candidate_grid(grid);
|
||||
} catch (Exception ex) {
|
||||
_bus.post(SparkTags.MessageDisplayTag, new MessageDisplayData("Import Error", ex.ToString(), false));
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Syncing Slaves
|
||||
|
||||
public void sync_slave_ref_forwards(VolSurfaceExpiration vs_exp) {
|
||||
DisplayModelData curr_data = _get_current_data();
|
||||
if (curr_data != null && curr_data.vs_output != null && curr_data.vs_output.volsurface_input != null) {
|
||||
IEnumerable<VolSurfaceExpiration> slave_exps = VolSurfaceUtilityFunctions.get_slave_expirations(curr_data.vs_output.volsurface_input);
|
||||
_send_curve_action_request(slave_exps.ToDictionaryDupes(e => e, e => new VolCurveAction(VolCurveAction.ActionType.SyncSlaveRefFwds)), false, false);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Blackboard response
|
||||
private void _on_server_update(VolSurfaceOutput vs_output) {
|
||||
if (vs_output != null) {
|
||||
vs_output = vs_output.Clone() as VolSurfaceOutput;
|
||||
if (_state == DisplayModelState.WaitingForCommittedState) {
|
||||
//mbts.Logger.log(bts.log.Level.Info, bts.log.Subsystem.ui, String.Format("Received first committed update for vol surface id {0}", _vs_id));
|
||||
bts.Log.Ui.info(String.Format("Received first committed update for vol surface id {0}", _vs_id));
|
||||
_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();
|
||||
if (on_graph_change != null)
|
||||
on_graph_change();
|
||||
} else if (_state == DisplayModelState.Idle || _state == DisplayModelState.WaitingForCalcResponse || _state == DisplayModelState.ResponseReady) {
|
||||
//mbts.Logger.log(bts.log.Level.Diag, bts.log.Subsystem.ui, String.Format("Received committed update for vol surface id {0} state {1}", _vs_id, _state.ToString()));
|
||||
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();
|
||||
if (on_graph_change != null)
|
||||
on_graph_change();
|
||||
} else
|
||||
_log_bad_transition(_state, "On Committed State Update");
|
||||
}
|
||||
}
|
||||
|
||||
private void _on_calculation_response(VolSurfaceCalculationResponse data) {
|
||||
if (_state == DisplayModelState.WaitingForCalcResponse || _state == DisplayModelState.ResponseReady) {
|
||||
//Logger.log(bts.log.Level.Info, bts.log.Subsystem.ui, String.Format("Received calculation response for vol surface id {0} state {1}", _vs_id, _state.ToString()));
|
||||
bts.Log.Ui.info(String.Format("Received calculation response for vol surface id {0} state {1}", _vs_id, _state.ToString()));
|
||||
VolSurfaceCalculationResponse calc_response = (data).Clone() as VolSurfaceCalculationResponse;
|
||||
_candidate_data.vs_output = calc_response.volsurface_calculation_output;
|
||||
VolSurfaceUIAdapter.copy_output_to_ui(_candidate_data.vs_output, _grid_settings, out _candidate_data.exp_grid, out _candidate_data.global_grid, out _candidate_data.exp_graph_map);
|
||||
if (_state == DisplayModelState.WaitingForCalcResponse)
|
||||
_change_state(DisplayModelState.ResponseReady);
|
||||
if (on_candidate_grid_change != null)
|
||||
on_candidate_grid_change();
|
||||
if (on_graph_change != null)
|
||||
on_graph_change();
|
||||
}
|
||||
}
|
||||
|
||||
private void _on_impl_vol_update(IDictionary<VolSurfaceExpiration, Tuple<VolSurfaceExpiration, VolCurveMarketImpliedVolData>> data) {
|
||||
_impl_vol_map = data;
|
||||
if (on_graph_change != null)
|
||||
on_graph_change();
|
||||
}
|
||||
|
||||
private void _on_state_list_update(IEnumerable<VolSurfaceInputSnapshot> data) {
|
||||
_input_snapshot_list = new List<VolSurfaceInputSnapshot>(data);
|
||||
if (on_previous_state_list_change != null)
|
||||
on_previous_state_list_change();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Actions originating from the UI
|
||||
public void put_rotation(IEnumerable<VolSurfaceDisplayGridExpiration> exps, bool is_up, bool cascade_up, bool cascade_down) {
|
||||
DisplayModelData curr_data = _get_current_data();
|
||||
if (curr_data != null)
|
||||
_send_curve_operation_request(exps, curr_data.vs_output.volsurface_input.surface_settings.core_settings.put_rotation, is_up, cascade_up, cascade_down);
|
||||
}
|
||||
|
||||
public void call_rotation(IEnumerable<VolSurfaceDisplayGridExpiration> exps, bool is_up, bool cascade_up, bool cascade_down) {
|
||||
DisplayModelData curr_data = _get_current_data();
|
||||
if (curr_data != null)
|
||||
_send_curve_operation_request(exps, curr_data.vs_output.volsurface_input.surface_settings.core_settings.call_rotation, is_up, cascade_up, cascade_down);
|
||||
}
|
||||
|
||||
public void full_rotation(IEnumerable<VolSurfaceDisplayGridExpiration> exps, bool is_up, bool cascade_up, bool cascade_down) {
|
||||
DisplayModelData curr_data = _get_current_data();
|
||||
if (curr_data != null)
|
||||
_send_curve_operation_request(exps, curr_data.vs_output.volsurface_input.surface_settings.core_settings.full_rotation, is_up, cascade_up, cascade_down);
|
||||
}
|
||||
|
||||
public void strangle(IEnumerable<VolSurfaceDisplayGridExpiration> exps, bool is_up, bool cascade_up, bool cascade_down) {
|
||||
DisplayModelData curr_data = _get_current_data();
|
||||
if (curr_data != null)
|
||||
_send_curve_operation_request(exps, curr_data.vs_output.volsurface_input.surface_settings.core_settings.strangle, is_up, cascade_up, cascade_down);
|
||||
}
|
||||
|
||||
public void atm_shift(IEnumerable<VolSurfaceDisplayGridExpiration> exps, bool is_up, bool cascade_up, bool cascade_down) {
|
||||
DisplayModelData curr_data = _get_current_data();
|
||||
if (curr_data != null)
|
||||
_send_curve_operation_request(exps, curr_data.vs_output.volsurface_input.surface_settings.core_settings.atm_shift, is_up, cascade_up, cascade_down);
|
||||
}
|
||||
|
||||
public void fit_to_market(IEnumerable<VolSurfaceDisplayGridExpiration> exp_list, bool is_all_curve, bool is_live_market,
|
||||
bool use_weighted_regression_fit, bool cascade_up, bool cascade_down) {
|
||||
DisplayModelData curr_data = _get_current_data();
|
||||
IEnumerable<VolSurfaceExpiration> vs_exps = exp_list.Select(i => i.ToVSExp());
|
||||
_send_curve_action_request(vs_exps.ToDictionary(e => e,
|
||||
e => {
|
||||
VolCurveMarketFitAction.ActionType action_type = is_all_curve ? VolCurveMarketFitAction.ActionType.FitWholeCurve : VolCurveMarketFitAction.ActionType.FitATM;
|
||||
return new VolCurveAction(new VolCurveMarketFitAction(action_type, null, use_weighted_regression_fit));
|
||||
}),
|
||||
cascade_up, cascade_down);
|
||||
}
|
||||
|
||||
public void rebuild(IEnumerable<VolSurfaceDisplayGridExpiration> exp_list, bool cascade_up, bool cascade_down) {
|
||||
_send_curve_action_request(exp_list.ToDictionary(e => e.ToVSExp(), e => new VolCurveAction(VolCurveAction.ActionType.Rebuild)), cascade_up, cascade_down);
|
||||
}
|
||||
|
||||
public void publish_new_grid_selection(string window_id, VolSurfaceDisplayGridExpiration selected_expiration, bool show_implied_vol_checked) {
|
||||
_bus.post(SparkTags.VolSurfaceGridSelectionTag(_vs_id), new Tuple<VolSurfaceDisplayGridExpiration, bool>(selected_expiration, show_implied_vol_checked));
|
||||
}
|
||||
|
||||
|
||||
public void set_impliedvol_market_side(VolSurfaceEnums.MarketSide market_side) {
|
||||
_bus.post(SparkTags.VolSurfaceMarketImpliedVolSide(_vs_id), market_side);
|
||||
}
|
||||
|
||||
public void publish_ignore_implied_vol_settings(string window_id, bool ignore_implied_vol_settings) {
|
||||
_bus.post(SparkTags.VolSurfaceIgnoreImpliedVolSettingsTag(_vs_id), ignore_implied_vol_settings);
|
||||
}
|
||||
|
||||
public void offset_slaves_atm(IEnumerable<VolSurfaceDisplayGridExpiration> exps, double offset_dollars) {
|
||||
// TODO
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Commit/Revert
|
||||
public void revert() {
|
||||
if (_state == DisplayModelState.WaitingForCalcResponse || _state == DisplayModelState.ResponseReady) {
|
||||
_candidate_data = _committed_data.Clone() as DisplayModelData;
|
||||
_change_state(DisplayModelState.Idle);
|
||||
on_candidate_grid_change();
|
||||
on_graph_change();
|
||||
} else
|
||||
_log_bad_transition(_state, "revert");
|
||||
}
|
||||
|
||||
public void commit() {
|
||||
if (_state == DisplayModelState.ResponseReady) {
|
||||
_bus.post(SparkTags.VolSurfaceCommitTag(_vs_id), _candidate_data.vs_output.volsurface_input);
|
||||
_change_state(DisplayModelState.Idle);
|
||||
} else
|
||||
_log_bad_transition(_state, "commit");
|
||||
}
|
||||
#endregion
|
||||
|
||||
/// Put Rot, Call Rot, RR, Strangle and ATM
|
||||
/// operation go call this
|
||||
private void _send_curve_operation_request(IEnumerable<VolSurfaceDisplayGridExpiration> exp_list, VolCurveMovementOperation curve_operation,
|
||||
bool is_up, bool cascade_up, bool cascade_down) {
|
||||
_send_curve_action_request(exp_list.ToDictionary(e => e.ToVSExp(), e => new VolCurveAction(VolSurfaceUtilityFunctions.add_side_to_curve_operation(curve_operation, is_up))),
|
||||
cascade_up, cascade_down);
|
||||
}
|
||||
|
||||
private void _send_curve_action_request(IDictionary<VolSurfaceExpiration, VolCurveAction> exp_action_map,
|
||||
bool cascade_up, bool cascade_down) {
|
||||
if (_state == DisplayModelState.Idle || _state == DisplayModelState.ResponseReady) {
|
||||
DisplayModelData curr_data = _get_current_data();
|
||||
VolSurfaceCalculationRequest req = new VolSurfaceCalculationRequest(curr_data.vs_output.volsurface_input, _request_version_no++, exp_action_map,
|
||||
cascade_up, cascade_down);
|
||||
_change_state(DisplayModelState.WaitingForCalcResponse);
|
||||
_bus.post(SparkTags.VolSurfaceCalculationRequestTag(_vs_id), req);
|
||||
} else
|
||||
_log_bad_transition(_state, "set candidate grid");
|
||||
}
|
||||
|
||||
|
||||
private void _change_state(DisplayModelState new_state) {
|
||||
bool dirty_before = _translate_to_dirty(_state),
|
||||
dirty_after = _translate_to_dirty(new_state);
|
||||
// mbts.Logger.log(bts.log.Level.Info, bts.log.Subsystem.ui,
|
||||
// String.Format("State change {0}->{1}", _state.ToString(), new_state.ToString()));
|
||||
bts.Log.Ui.info(String.Format("State change {0}->{1}", _state.ToString(), new_state.ToString()));
|
||||
_state = new_state;
|
||||
if (dirty_before != dirty_after && on_dirty_change != null) {
|
||||
_bus.post(SparkTags.VolSurfaceDirtyStateTag(_vs_id), dirty_after);
|
||||
on_dirty_change();
|
||||
}
|
||||
}
|
||||
|
||||
public bool is_dirty { get { return _translate_to_dirty(_state); } }
|
||||
|
||||
private DisplayModelData _get_current_data() {
|
||||
if (_state == DisplayModelState.Idle)
|
||||
return _committed_data;
|
||||
else if (_state == DisplayModelState.ResponseReady)
|
||||
return _candidate_data;
|
||||
else
|
||||
return null;
|
||||
}
|
||||
|
||||
private void _copy_time_parameters(VolSurfaceInput source, ref VolSurfaceInput target) {
|
||||
target.surface_parameters = target.surface_parameters ?? new VolSurfaceParameters();
|
||||
target.surface_parameters.time_parameters = target.surface_parameters.time_parameters ?? new VolSurfaceTimeParameters() { input_time = DateTime.MinValue, auto_decay_points = new List<Tuple<DateTime, double>>() };
|
||||
if (source != null && source.surface_parameters != null) {
|
||||
target.surface_parameters.vol_time = source.surface_parameters.vol_time;
|
||||
if (source.surface_parameters.time_parameters != null) {
|
||||
target.surface_parameters.time_parameters.input_time = source.surface_parameters.time_parameters.input_time;
|
||||
target.surface_parameters.time_parameters.auto_decay_points = source.surface_parameters.time_parameters.auto_decay_points == null ? target.surface_parameters.time_parameters.auto_decay_points : source.surface_parameters.time_parameters.auto_decay_points.ToList();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#region Stateless Helpers
|
||||
private static void _log_graphs(bool is_committed, IDictionary<VolSurfaceDisplayGridExpiration, IEnumerable<Tuple<double, double>>> graph_map) {
|
||||
foreach (KeyValuePair<VolSurfaceDisplayGridExpiration, IEnumerable<Tuple<double, double>>> p in graph_map) {
|
||||
StringBuilder sb = new StringBuilder(p.Key.ToString());
|
||||
sb = sb.AppendLine(is_committed ? "Committed" : "Candidate");
|
||||
foreach (Tuple<double, double> t in p.Value)
|
||||
sb = sb.AppendLine(String.Format("x:{0} y:{1}", t.Item1, t.Item2));
|
||||
//mbts.Logger.log(bts.log.Level.Info, bts.log.Subsystem.gen, sb.ToString());
|
||||
bts.Log.Gen.info(String.Format("{0}", sb.ToString()));
|
||||
}
|
||||
}
|
||||
|
||||
private static void _log_bad_transition(DisplayModelState state, string action) {
|
||||
//mbts.Logger.log(bts.log.Level.Error, bts.log.Subsystem.ui, String.Format("{0}:{1}", state, action));
|
||||
bts.Log.Ui.error(String.Format("{0}:{1}", state, action));
|
||||
}
|
||||
|
||||
private static bool _translate_to_dirty(DisplayModelState state) {
|
||||
return state == DisplayModelState.WaitingForCalcResponse || state == DisplayModelState.ResponseReady;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
public void Dispose() {
|
||||
if (_publish_lifetime)
|
||||
_bus.post(SparkTags.VolSurfaceActiveUITag(_vs_id), true);
|
||||
Disposable.dispose(ref _subscriptions);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,7 @@ using System.Threading.Tasks;
|
||||
using bts.api.analytics;
|
||||
using mbts.analytics;
|
||||
using mbts.ui.converters;
|
||||
using mbts.spark.filters;
|
||||
|
||||
namespace mbts.spark {
|
||||
|
||||
@@ -289,6 +290,8 @@ namespace mbts.spark {
|
||||
public static ITag<MessageDisplayData> MessageDisplayTag { get; } =
|
||||
new SparkTag<MessageDisplayData>("MessageDisplayTag");
|
||||
|
||||
public static ITag<Int32> VolSurfaceLaunchTheoWindow { get; } =
|
||||
new SparkTag<Int32>("VolSurfaceLaunchTheoWindow");
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
Reference in new issue
Block a user