Addedd SingleVolSurfaceTheoSource

This commit is contained in:
Taha Afzal authored and John Colvin committed 2021-12-09 11:40:49 -05:00
1 parent 4af73414cf
commit 7403155a88
1 file changed
+145
@@ -0,0 +1,145 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using mbts.analytics;
using bts;
namespace mbts.spark
{
/// Publishes the committed and candidate greeks in the
/// form of VolSurfaceTheoData
/// Subs :
/// 1. Comm Vol Output
/// 2. Cand Calc Response
/// 3. Is Dirty (ie a pending change?)
/// 4. Detailed Window State
/// Pubs :
/// 1. VolSurfaceTheoData (cand and comm theos)
public class SingleVolSurfaceTheoSource : IDisposable
{
private readonly int _volsurface_id;
private readonly ThreadedBlackboard _threaded_bb;
private readonly object _in_committed_vol_output_tag, _in_candidate_vol_output_tag, _in_input_dirty_tag, _in_window_state_tag;
private readonly HighPerformanceTimer _timer = new HighPerformanceTimer();
private bool _is_dirty = true;
public SingleVolSurfaceTheoSource(int volsurface_id, ThreadedBlackboard threaded_bb)
{
_volsurface_id = volsurface_id;
_threaded_bb = threaded_bb;
_in_committed_vol_output_tag = SparkTags.VolSurfaceCommittedOutputTag(_volsurface_id);
_in_candidate_vol_output_tag = SparkTags.VolSurfaceCalculationResponseTag(_volsurface_id);
_in_input_dirty_tag = SparkTags.VolSurfaceDirtyStateTag(_volsurface_id);
_in_window_state_tag = SparkTags.VolSurfaceTheoWindowStateTag(_volsurface_id);
_threaded_bb.add_to_subscription_list(_in_committed_vol_output_tag, _in_candidate_vol_output_tag, _in_input_dirty_tag, _in_window_state_tag);
_threaded_bb.data_callback += _on_data;
_threaded_bb.timer_callback += _on_timer;
}
#region Blackboard
private void _on_data(object tag, object data)
{
if (_in_candidate_vol_output_tag.Equals(tag) || _in_committed_vol_output_tag.Equals(tag) || _in_input_dirty_tag.Equals (tag) || _in_window_state_tag.Equals (tag))
_is_dirty = true;
}
private void _on_timer()
{
if (_is_dirty)
{
/// publishes new values when there is an
/// 1. theos window up
/// 2. waiting candidate change
_compute_and_publish(_is_active_candidate(), _is_active_theos_window());
_is_dirty = false;
}
}
#endregion
private static readonly IDictionary<VolSurfaceExpiration, IEnumerable<Theos>> _empty_theos = new Dictionary<VolSurfaceExpiration, IEnumerable<Theos>>();
private VolSurfaceTheoData _last_data = new VolSurfaceTheoData(new Dictionary<VolSurfaceExpiration, HashSet<double>>(), _empty_theos, _empty_theos, false);
private void _compute_and_publish(bool is_active_candidate, bool is_active_theos_window)
{
VolSurfaceCalculationResponse calc_resp = _threaded_bb.sec_thread_retrieve_from_cache(_in_candidate_vol_output_tag) as VolSurfaceCalculationResponse;
VolSurfaceOutput candidate_output = calc_resp != null ? calc_resp.volsurface_calculation_output : null,
committed_output = _threaded_bb.sec_thread_retrieve_from_cache(_in_committed_vol_output_tag) as VolSurfaceOutput;
IDictionary<VolSurfaceExpiration, HashSet<double>> secs = _get_securities(committed_output, candidate_output);
IDictionary<VolSurfaceExpiration, IEnumerable<Theos>> comm_theo_map = _empty_theos, cand_theo_map = _empty_theos;
if (is_active_theos_window)
{
comm_theo_map = _compute_theos(committed_output, secs);
if (is_active_candidate)
cand_theo_map = _compute_theos(candidate_output, secs);
}
else
{
if (is_active_candidate)
cand_theo_map = _compute_theos(candidate_output, secs);
}
var new_data = new VolSurfaceTheoData(secs, comm_theo_map, cand_theo_map, is_active_candidate);
if (_is_valid_update(new_data))
_threaded_bb.sec_thread_write(SparkTags.VolSurfaceTheoOutputTag(_volsurface_id), new_data);
}
private bool _is_valid_update(VolSurfaceTheoData new_data)
{
bool is_spurious = _last_data.committed_theo_map.Count == 0 && _last_data.candidate_theo_map.Count == 0 &&
new_data.committed_theo_map.Count == 0 && new_data.candidate_theo_map.Count == 0;
_last_data = new_data;
return !is_spurious;
}
private IDictionary<VolSurfaceExpiration, IEnumerable<Theos>> _compute_theos(VolSurfaceOutput vs_output, IDictionary<VolSurfaceExpiration, HashSet<double>> exp_strikes)
{
if (vs_output != null)
{
_timer.start();
IDictionary<VolSurfaceExpiration, IEnumerable<mbts.analytics.VolMatrix>> exp_volmat_map =
VolSurfaceVolMatrixCalculator.generate_volmatrix(vs_output.volsurface_input, exp_strikes.ToDictionary(kv => kv.Key, kv => kv.Value as IEnumerable<double>), vs_output.aux_data_map,
new double[] { 0.0 }, false, true, false, false, false);
_timer.stop();
Log.Gen.info(String.Format("SingleVolSurfaceTheoSource._compute_theos vs {0} vol comp time {1} msecs", _volsurface_id, _timer.lastIntervalTime));
_timer.start();
IDictionary<VolSurfaceExpiration, IEnumerable<Theos>> theo_map = VolSurfaceTheoCalculator.compute_standard_greeks(vs_output.volsurface_input.surface_settings, exp_volmat_map, true);
_timer.stop();
Log.Gen.info(String.Format("SingleVolSurfaceTheoSource._compute_theos vs {0} theos comp time {1} msecs", _volsurface_id, _timer.lastIntervalTime));
return theo_map;
}
return new Dictionary<VolSurfaceExpiration, IEnumerable<Theos>>();
}
private IDictionary<VolSurfaceExpiration, HashSet<double>> _get_securities(VolSurfaceOutput commited_output, VolSurfaceOutput candidate_output)
{
if (candidate_output != null)
/// candidate output wins if its active
return VolSurfaceUtilityFunctions.get_contracts(candidate_output);
else if (commited_output != null)
return VolSurfaceUtilityFunctions.get_contracts(commited_output);
else
return new Dictionary<VolSurfaceExpiration, HashSet<double>>();
}
/// If the details window is not active, the calculations are suspended
private bool _is_active_theos_window()
{
object rv_obj = _threaded_bb.sec_thread_retrieve_from_cache(_in_window_state_tag);
return rv_obj != null && (bool)rv_obj;
}
private bool _is_active_candidate()
{
object rv_obj = _threaded_bb.sec_thread_retrieve_from_cache(_in_input_dirty_tag);
return rv_obj != null && (bool)rv_obj;
}
#region IDisposable
public void Dispose()
{
_threaded_bb.data_callback -= _on_data;
_threaded_bb.timer_callback -= _on_timer;
}
#endregion
}
}