feat: Add axis-fit behavior setting for vs graph

This commit is contained in:
John Colvin committed 2022-02-14 14:01:51 -05:00
1 parent 4cf8959b13
commit 110faefc96
8 files changed
+232 -135

No files matched your search

@@ -0,0 +1,23 @@
namespace mbts.spark {
using System.ComponentModel;
enum VolSurfaceGraphAxisRetension {
/// <summary>
/// When an expiration is selected, the zoom level and scroll position do not change (i.e.,
/// these values are 'shared' across expirations)
/// </summary>
None,
/// <summary>
/// When an expiration is selected, the zoom level and scroll position are adjusted to fit all
/// points in the expiration series (plus a bit of padding).
/// </summary>
[Description("Refit when selection changes")]
AutoFit,
/// <summary>
/// When an expiration is selected, the zoom level and scroll position are reset to the values
/// they held when the expiration was last selected
/// </summary>
[Description("Restore when selection changes")]
Expiration,
}
}
@@ -57,7 +57,8 @@ namespace mbts.spark {
color = BTSColors.VolSurfaceGraph.implied_call
},
vol_path = get_default_vol_path_settings(),
exp_bounds = new ExpGraphBounds[0]
exp_bounds = Array.Empty<ExpGraphBounds>(),
axis_scale_retension = VolSurfaceGraphAxisRetension.None,
};
}
@@ -96,6 +97,8 @@ namespace mbts.spark {
[ProtoMember(11)]
public ExpGraphBounds[] exp_bounds;
[ProtoMember(12)]
public VolSurfaceGraphAxisRetension axis_scale_retension { get; set; }
//private LineSeriesSettings _vol_path;
//public LineSeriesSettings vol_path {
// get {
@@ -121,7 +124,8 @@ namespace mbts.spark {
atm.safe_equals(other.atm) &&
implied_atm.safe_equals(other.implied_atm) &&
vol_path.safe_equals(other.vol_path) &&
exp_bounds.safe_sequence_equals(other.exp_bounds);
exp_bounds.safe_sequence_equals(other.exp_bounds) &&
axis_scale_retension == other.axis_scale_retension;
public override bool Equals(object obj) =>
Equals(obj as VolSurfaceGraphSettings);
+25 -2
View File
@@ -11,6 +11,7 @@
xmlns:cv="clr-namespace:mbts.ui.converters"
xmlns:shell="clr-namespace:mbts.ui.shell"
xmlns:graph="clr-namespace:mbts.ui.controls.graph"
xmlns:oxy="clr-namespace:OxyPlot.Wpf;assembly=OxyPlot.Wpf"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase"
Background="{StaticResource CommonBackground}"
@@ -203,6 +204,11 @@
</Setter.Value>
</Setter>
</Style>
<c:RadioButtonGroupManager
x:Key="VolSurfaceGraphAxisRetensionRadioButtonGroupManager"
SelectedItem="{Binding DataContext.axis_retension, Source={u:DirectReference vol_curve_plot_view}, Mode=TwoWay}"/>
</UserControl.Resources>
<AdornerDecorator>
@@ -476,12 +482,29 @@
Show Legend
</CheckBox>
<graph:BTSPlotView
<oxy:PlotView
x:Name="vol_curve_plot_view"
Grid.Row="2"
Grid.ColumnSpan="3"
Model="{Binding graph_vm.plot_model}"
MouseDoubleClick="_on_graph_mouse_double_click" />
MouseDoubleClick="_on_graph_mouse_double_click">
<oxy:PlotView.ContextMenu>
<ContextMenu>
<MenuItem Header="Pan" Command="{Binding x_axis.pan_cmd}"/>
<MenuItem Header="Replot" Command="{Binding graph_vm.replot_cmd}"/>
<MenuItem
Header="Axis Behavior" ItemsSource="{u:EnumBindingSource {x:Type spark:VolSurfaceGraphAxisRetension}}">
<MenuItem.ItemContainerStyle>
<Style TargetType="MenuItem">
<Setter Property="IsCheckable" Value="True"/>
<Setter Property="HeaderTemplate" Value="{StaticResource EnumDescriptionTemplate}"/>
<Setter Property="c:RadioButtonGroupManager.Manager" Value="{StaticResource VolSurfaceGraphAxisRetensionRadioButtonGroupManager}"/>
</Style>
</MenuItem.ItemContainerStyle>
</MenuItem>
</ContextMenu>
</oxy:PlotView.ContextMenu>
</oxy:PlotView>
</Grid>
</TabItem>
@@ -4,11 +4,8 @@
using mbts.ui.controls;
using mbts.ui.controls.adorners;
using mbts.ui.converters;
using mbts.ui.extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
@@ -56,45 +53,8 @@
}
#endregion
public void fit_axes_to_series() {
if (_vol_curve_graph_vm?.axes?.Count != 2) {
return;
}
var vis_series = _vol_curve_graph_vm.flat_visible_series.Where(s => s.points.Any()).ToList();
if (!vis_series.Any()) {
return;
}
Type tx = vis_series.Select(s => s.points.First().x.GetType()).Distinct().SingleOrDefault();
if (tx != typeof(double)) {
return;
}
Type ty = vis_series.Select(s => s.points.First().y.GetType()).Distinct().SingleOrDefault();
if (ty != typeof(double)) {
return;
}
if (_vol_curve_graph_vm.axes.TryGetFirst(a => a.position.is_horizontal(), out var x_axis) &&
_vol_curve_graph_vm.axes.TryGetFirst(a => a.position.is_vertical(), out var y_axis)) {
var points = vis_series.SelectMany(vs => vs.points);
var x_points = points.Select(p => (double)p.x);
if (x_points.Except(double.NaN).MinMax(out var x_min, out var x_max) && x_min < x_max) {
double padding = (x_max - x_min) * 0.05;
x_axis.minimum = x_min - padding;
x_axis.maximum = x_max + padding;
}
var y_points = points.Select(p => (double)p.y);
if (y_points.Except(double.NaN).MinMax(out var y_min, out var y_max) && y_min < y_max) {
double padding = (y_max - y_min) * 0.05;
y_axis.minimum = y_min - padding;
y_axis.maximum = y_max + padding;
}
}
_vol_curve_graph_vm?.reset_all_axes();
}
public void fit_axes_to_series() =>
_vol_curve_graph_vm?.fit_axes_to_bounds();
void _on_is_legend_visible_changed(bool value) {
Disposable.dispose(ref _legend_popup);
@@ -24,8 +24,8 @@ namespace mbts.spark {
readonly VolSurfaceDisplayModel _model;
readonly VolSurfaceEventDemux _demux;
readonly Action<bool, VolSurfaceDisplayGridExpiration> _publish_show_implied_vol;
readonly LinearAxisVM _x_axis = new();
readonly LinearAxisVM _y_axis = new();
readonly LinearAxisVM _x_axis = new() { position = AxisPosition.BottomLeftToRight };
readonly LinearAxisVM _y_axis = new() { position = AxisPosition.LeftBottomToTop };
readonly Dictionary<SecurityExpiration, ExpGraphBounds> _expiration_graph_bounds;
readonly VolSurfaceGraphSettings _settings;
readonly ObservableCollection<VolSurfaceDisplayGridExpiration> _expirations = new();
@@ -136,6 +136,13 @@ namespace mbts.spark {
public bool show_vol_path { get { return _show_vol_path; } set { _show_vol_path = value; _show_vol_path_changed(); } }
public IDialogCommand settings_command { get; private set; }
public VolSurfaceDisplayGridExpiration earliest_selected_expiration { get; set; }
public VolSurfaceGraphAxisRetension axis_retension {
get => _settings.axis_scale_retension;
set {
using var _ = detect_property_change();
_settings.axis_scale_retension = value;
}
}
public void set_visible_expiration(VolSurfaceDisplayGridExpiration expiration) {
_earliest_selected_expiration = expiration;
@@ -215,97 +222,69 @@ namespace mbts.spark {
}
IEnumerable<GraphPointVM> _get_points(VolSurfaceDisplayGridExpiration exp, VolSurfaceGraphSeriesType type) {
if (type == VolSurfaceGraphSeriesType.CandidateCallWing
|| type == VolSurfaceGraphSeriesType.CandidatePutWing
|| type == VolSurfaceGraphSeriesType.CandidateInnerCurve
|| type == VolSurfaceGraphSeriesType.CandidateControlPoints
|| type == VolSurfaceGraphSeriesType.CandidateSpecialCalls
|| type == VolSurfaceGraphSeriesType.CandidateSpecialPuts
|| type == VolSurfaceGraphSeriesType.CandidateATM) {
var disp_graph = _model.get_candidate_exp_vol_graph(exp);
if (disp_graph != null) {
return type switch {
VolSurfaceGraphSeriesType.CandidateCallWing => disp_graph.all_points
.Where(t => t.Item1 >= disp_graph.call_wing_start)
.Select(t => new GraphPointVM(t.Item1, t.Item2))
.ToList(),
VolSurfaceGraphSeriesType.CandidatePutWing => disp_graph.all_points
.Where(t => t.Item1 <= disp_graph.put_wing_start)
.Select(t => new GraphPointVM(t.Item1, t.Item2))
.ToList(),
VolSurfaceGraphSeriesType.CandidateInnerCurve => disp_graph.all_points
.Where(t => t.Item1.is_within_range(disp_graph.put_wing_start, disp_graph.call_wing_start))
.Select(t => new GraphPointVM(t.Item1, t.Item2))
.ToList(),
VolSurfaceGraphSeriesType.CandidateControlPoints => Enumerable.Empty<GraphPointVM>(),
VolSurfaceGraphSeriesType.CandidateSpecialCalls => disp_graph.special_call_points
.Select(t => new GraphPointVM(t.Item1, t.Item2))
.ToList(),
VolSurfaceGraphSeriesType.CandidateSpecialPuts => disp_graph.special_put_points
.Select(t => new GraphPointVM(t.Item1, t.Item2))
.ToList(),
VolSurfaceGraphSeriesType.CandidateATM => new GraphPointVM[] {
new(disp_graph.atm_point.Item1, disp_graph.atm_point.Item2)
InvalidOperationException _unexpected_series_type_ex() =>
new($"Unexpected graph series type '{type}'");
if (type.is_vol_path()) {
return type switch {
VolSurfaceGraphSeriesType.CommittedVolPath =>
_model.get_committed_volpath(exp).Select(tup => new GraphPointVM(tup.Item1, tup.Item2)),
VolSurfaceGraphSeriesType.CandidateVolPath =>
_model.get_candidate_volpath(exp).Select(tup => new GraphPointVM(tup.Item1, tup.Item2)),
_ => throw _unexpected_series_type_ex(),
};
}
if (type.is_candidate()) {
var graph = _model.get_candidate_exp_vol_graph(exp);
if (graph is not null) {
var pts = type switch {
VolSurfaceGraphSeriesType.CandidateCallWing => graph.real_call_wing_points(),
VolSurfaceGraphSeriesType.CandidatePutWing => graph.real_put_wing_points(),
VolSurfaceGraphSeriesType.CandidateInnerCurve => graph.real_inner_curve_points(),
VolSurfaceGraphSeriesType.CandidateControlPoints => graph.real_control_points(),
VolSurfaceGraphSeriesType.CandidateSpecialCalls => graph.real_special_call_points(),
VolSurfaceGraphSeriesType.CandidateSpecialPuts => graph.real_special_put_points(),
VolSurfaceGraphSeriesType.CandidateATM => new (double x, double y)[] {
new(graph.atm_point.Item1, graph.atm_point.Item2)
},
_ => Enumerable.Empty<GraphPointVM>(),
_ => throw _unexpected_series_type_ex(),
};
return pts.Select(pt => new GraphPointVM(pt.x, pt.y)).ToList();
}
}
if (type == VolSurfaceGraphSeriesType.CommittedCallWing
|| type == VolSurfaceGraphSeriesType.CommittedPutWing
|| type == VolSurfaceGraphSeriesType.CommittedInnerCurve
|| type == VolSurfaceGraphSeriesType.CommittedControlPoints
|| type == VolSurfaceGraphSeriesType.CommittedSpecialCalls
|| type == VolSurfaceGraphSeriesType.CommittedSpecialPuts
|| type == VolSurfaceGraphSeriesType.CommittedATM) {
var disp_graph = _model.get_committed_exp_vol_graph(exp);
if (disp_graph != null) {
return type switch {
VolSurfaceGraphSeriesType.CommittedCallWing => disp_graph.all_points
.Where(t => t.Item1 >= disp_graph.call_wing_start)
.Select(t => new GraphPointVM(t.Item1, t.Item2))
.ToList(),
VolSurfaceGraphSeriesType.CommittedPutWing => disp_graph.all_points
.Where(t => t.Item1 <= disp_graph.put_wing_start)
.Select(t => new GraphPointVM(t.Item1, t.Item2))
.ToList(),
VolSurfaceGraphSeriesType.CommittedInnerCurve => disp_graph.all_points
.Where(t => t.Item1.is_within_range(disp_graph.put_wing_start, disp_graph.call_wing_start))
.Select(t => new GraphPointVM(t.Item1, t.Item2))
.ToList(),
VolSurfaceGraphSeriesType.CommittedControlPoints => Enumerable.Empty<GraphPointVM>(),
VolSurfaceGraphSeriesType.CommittedSpecialCalls => disp_graph.special_call_points
.Select(t => new GraphPointVM(t.Item1, t.Item2)).ToList(),
VolSurfaceGraphSeriesType.CommittedSpecialPuts => disp_graph.special_put_points
.Select(t => new GraphPointVM(t.Item1, t.Item2)).ToList(),
VolSurfaceGraphSeriesType.CommittedATM => new GraphPointVM[] {
new(disp_graph.atm_point.Item1, disp_graph.atm_point.Item2)
if (type.is_committed()) {
var graph = _model.get_committed_exp_vol_graph(exp);
if (graph is not null) {
var pts = type switch {
VolSurfaceGraphSeriesType.CommittedCallWing => graph.real_call_wing_points(),
VolSurfaceGraphSeriesType.CommittedPutWing => graph.real_put_wing_points(),
VolSurfaceGraphSeriesType.CommittedInnerCurve => graph.real_inner_curve_points(),
VolSurfaceGraphSeriesType.CommittedControlPoints => graph.real_control_points(),
VolSurfaceGraphSeriesType.CommittedSpecialCalls => graph.real_special_call_points(),
VolSurfaceGraphSeriesType.CommittedSpecialPuts => graph.real_special_put_points(),
VolSurfaceGraphSeriesType.CommittedATM => new (double x, double y)[] {
new(graph.atm_point.Item1, graph.atm_point.Item2)
},
_ => Enumerable.Empty<GraphPointVM>(),
_ => throw _unexpected_series_type_ex(),
};
return pts.Select(pt => new GraphPointVM(pt.x, pt.y)).ToList();
}
}
if (type == VolSurfaceGraphSeriesType.ImpliedVolATM
|| type == VolSurfaceGraphSeriesType.ImpliedVolCalls
|| type == VolSurfaceGraphSeriesType.ImpliedVolPuts) {
if (type.is_implied()) {
var implied_vol_data = _model.get_market_market_implied_vols(exp);
if (implied_vol_data != null) {
switch (type) {
case VolSurfaceGraphSeriesType.ImpliedVolATM: return new GraphPointVM[] { new(implied_vol_data.atm_strike, implied_vol_data.atm_vol) };
case VolSurfaceGraphSeriesType.ImpliedVolCalls: return implied_vol_data.call_rawk_vols.Select(kvp => new GraphPointVM(kvp.Key, kvp.Value)).ToList();
case VolSurfaceGraphSeriesType.ImpliedVolPuts: return implied_vol_data.put_rawk_vols.Select(kvp => new GraphPointVM(kvp.Key, kvp.Value)).ToList();
}
if (implied_vol_data is not null) {
return type switch {
VolSurfaceGraphSeriesType.ImpliedVolATM => new GraphPointVM[] { new(implied_vol_data.atm_strike, implied_vol_data.atm_vol) },
VolSurfaceGraphSeriesType.ImpliedVolCalls => implied_vol_data.call_rawk_vols.Select(kvp => new GraphPointVM(kvp.Key, kvp.Value)).ToList(),
VolSurfaceGraphSeriesType.ImpliedVolPuts => implied_vol_data.put_rawk_vols.Select(kvp => new GraphPointVM(kvp.Key, kvp.Value)).ToList(),
_ => throw _unexpected_series_type_ex(),
};
}
}
if (type == VolSurfaceGraphSeriesType.CommittedVolPath) {
return _model.get_committed_volpath(exp).Select(tup => new GraphPointVM(tup.Item1, tup.Item2));
}
if (type == VolSurfaceGraphSeriesType.CandidateVolPath) {
return _model.get_candidate_volpath(exp).Select(tup => new GraphPointVM(tup.Item1, tup.Item2));
}
return Enumerable.Empty<GraphPointVM>();
}
@@ -328,8 +307,14 @@ namespace mbts.spark {
var sec_exp = vsd_exp.ToSecurityExp();
_expiration_graph_bounds[sec_exp] = new ExpGraphBounds {
exp = new SecurityExpirationProto { security = sec_exp.security_tuple, expiration_oa_date = sec_exp.expiration.ToOADate() },
x_bounds = new AxisBounds { min = _x_axis.minimum, max = _x_axis.maximum },
y_bounds = new AxisBounds { min = _y_axis.minimum, max = _y_axis.maximum }
x_bounds = new AxisBounds {
min = _x_axis.minimum,
max = _x_axis.maximum,
},
y_bounds = new AxisBounds {
min = _y_axis.minimum,
max = _y_axis.maximum,
},
};
}
}
@@ -339,12 +324,20 @@ namespace mbts.spark {
foreach (var ss in graph_vm.series_sets) {
ss.is_visible = exp_to_make_visible.Equals(ss.args);
}
if (_expiration_graph_bounds.TryGetValue(exp_to_make_visible.ToSecurityExp(), out var egb)) {
_x_axis.minimum = egb.x_bounds.min;
_x_axis.maximum = egb.x_bounds.max;
_y_axis.minimum = egb.y_bounds.min;
_y_axis.maximum = egb.y_bounds.max;
graph_vm.reset_all_axes();
switch (axis_retension) {
case VolSurfaceGraphAxisRetension.Expiration:
if (_expiration_graph_bounds.TryGetValue(exp_to_make_visible.ToSecurityExp(), out var egb)) {
_x_axis.minimum = egb.x_bounds.min;
_x_axis.maximum = egb.x_bounds.max;
_y_axis.minimum = egb.y_bounds.min;
_y_axis.maximum = egb.y_bounds.max;
graph_vm.plot_model.ResetAllAxes();
graph_vm.plot_model.InvalidatePlot(false);
}
break;
case VolSurfaceGraphAxisRetension.AutoFit:
graph_vm.fit_axes_to_bounds();
break;
}
}
@@ -1,11 +1,13 @@
namespace mbts.spark {
using bts.api;
using bts.api.analytics;
using bts.utils;
using mbts.analytics;
using mbts.spark.VolSurface;
using mbts.ui.controls;
using System;
using System.Collections.Generic;
using System.Linq;
@@ -68,6 +70,12 @@
_ => false,
};
public static bool is_vol_path(this VolSurfaceGraphSeriesType series_type) =>
series_type switch {
VolSurfaceGraphSeriesType.CandidateVolPath or VolSurfaceGraphSeriesType.CommittedVolPath => true,
_ => false,
};
public static IEnumerable<VolSurfaceExpiration> select_exp(this IEnumerable<VolSurfaceRowContext> rows) =>
rows.Select(r => r.expiration.ToVSExp());
@@ -79,5 +87,29 @@
public static IEnumerable<VolSurfaceDisplayGridExpiration> where_slave(this IEnumerable<VolSurfaceDisplayGridExpiration> rows) =>
rows.Where(r => !r.is_slave);
public static IEnumerable<(double x, double y)> real_call_wing_points(this VolCurveDisplayGraph graph) =>
graph.real_all_points().Where(pt => pt.x >= graph.call_wing_start);
public static IEnumerable<(double x, double y)> real_put_wing_points(this VolCurveDisplayGraph graph) =>
graph.real_all_points().Where(pt => pt.x <= graph.put_wing_start);
public static IEnumerable<(double x, double y)> real_inner_curve_points(this VolCurveDisplayGraph graph) =>
graph.real_all_points().Where(pt => pt.x.is_within_range(graph.put_wing_start, graph.call_wing_start));
public static IEnumerable<(double x, double y)> real_control_points(this VolCurveDisplayGraph graph) =>
Enumerable.Empty<(double x, double y)>();
public static IEnumerable<(double x, double y)> real_special_call_points(this VolCurveDisplayGraph graph) =>
_select_real_points(graph.special_call_points);
public static IEnumerable<(double x, double y)> real_special_put_points(this VolCurveDisplayGraph graph) =>
_select_real_points(graph.special_put_points);
public static IEnumerable<(double x, double y)> real_all_points(this VolCurveDisplayGraph graph) =>
_select_real_points(graph.all_points);
static IEnumerable<(double x, double y)> _select_real_points(IEnumerable<Tuple<double, double>> points) =>
points.Where(pt => pt.Item1.is_real() && pt.Item2.is_real()).Select(t => (t.Item1, t.Item2));
}
}
@@ -128,7 +128,7 @@ namespace mbts.spark {
}
public double minimum {
get => _model.Minimum;
get => _model.ActualMinimum;
set {
using var _ = detect_property_change();
_model.Minimum = value;
@@ -136,7 +136,7 @@ namespace mbts.spark {
}
public double maximum {
get => _model.Maximum;
get => _model.ActualMaximum;
set {
using var _ = detect_property_change();
_model.Maximum = value;
@@ -1,6 +1,7 @@
namespace mbts.spark {
using mbts.ui.controls;
using mbts.ui.controls.graph;
using mbts.ui.extensions;
using mbts.ui.shell;
using mbts.ui.utility;
@@ -66,6 +67,67 @@
public IEnumerable<object> horizontal_grid_lines { get; set; }
public IEnumerable<object> vertical_grid_lines { get; set; }
public AxisVM x_axis => axes.FirstOrDefault(a => a.position.is_horizontal());
public AxisVM y_axis => axes.FirstOrDefault(a => a.position.is_vertical());
public (IComparable x_min, IComparable x_max, IComparable y_min, IComparable y_max) get_axes_bounds() {
var x_bounds = get_axes_bounds(p => (IComparable)p.x);
var y_bounds = get_axes_bounds(p => (IComparable)p.y);
var (x_min, x_max) = x_bounds ?? (null, null);
var (y_min, y_max) = y_bounds ?? (null, null);
return (x_min, x_max, y_min, y_max);
}
public (IComparable min, IComparable max)? get_axes_bounds(Func<GraphPointVM, IComparable> select_scalar) =>
get_axes_bounds<IComparable>(select_scalar);
public (T min, T max)? get_axes_bounds<T>(Func<GraphPointVM, T> select_scalar, Func<GraphPointVM, bool> predicate = null) where T : IComparable {
var vis_series = flat_visible_series.Where(s => s.points.Any()).ToList();
if (!vis_series.Any()) {
return null;
}
var pts = vis_series.SelectMany(vs => vs.points);
if (predicate is not null) {
pts = pts.Where(predicate);
}
var scalar_vals = pts.Select(select_scalar);
if (scalar_vals.MinMax(out var min, out var max) && min.CompareTo(max) < 0) {
return (min, max);
}
return null;
}
public bool fit_axes_to_bounds() {
var x_bounds = get_axes_bounds(p => (double)p.x);
if (!x_bounds.HasValue) {
return false;
}
var y_bounds = get_axes_bounds(p => (double)p.y);
if (!y_bounds.HasValue) {
return false;
}
var (x_min, x_max) = x_bounds.Value;
const double padding_pct = 0.05;
double x_padding = (x_max - x_min) * padding_pct;
var x_axis = this.x_axis;
if (x_axis is not null) {
x_axis.minimum = x_min - x_padding;
x_axis.maximum = x_max + x_padding;
}
var (y_min, y_max) = y_bounds.Value;
double y_padding = (y_max - y_min) * padding_pct;
var y_axis = this.y_axis;
if (y_axis is not null) {
y_axis.minimum = y_min - y_padding;
y_axis.maximum = y_max + y_padding;
}
reset_all_axes();
return true;
}
public Color background {
get => _background.Value;
set => _background.Value = value;