feat: Add volsurface graph settings dialog

A lot of the vm code was already there, but I cleaned it up a bit. The view was
ported over from edge and also cleaned up/tweaked a bit.
This commit is contained in:
John Colvin committed 2022-10-10 14:46:34 -04:00
1 parent c0289719e7
commit 6d56f67697
9 files changed
+400 -80

No files matched your search

@@ -10,7 +10,9 @@ namespace mbts.spark {
using mbts.ui.shell;
using mbts.ui.utility;
using Spark.Analytics.View;
using Spark.Core.Disposables;
using Spark.Core.Protobuf;
using System;
using System.Collections;
@@ -29,13 +31,13 @@ namespace mbts.spark {
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();
readonly Dictionary<VolSurfaceDisplayGridExpiration, SeriesSetVM> _expiration_series_cache = new();
VolSurfaceExpiration _md_exp;
VolSurfaceDisplayGridExpiration _expiration;
VolSurfaceDisplayGridExpiration _earliest_selected_expiration;
VolSurfaceGraphSettings _settings;
object _implied_vol_market_side = ImpliedVolHelper.None;
bool _show_vol_path;
@@ -82,13 +84,17 @@ namespace mbts.spark {
if (_model.get_active_settings() is not VolSurfaceSettings vss) {
return null;
}
// return new VolSurfaceGraphSettingsEditor {
// DataContext = new VolSurfaceGraphSettingsEditorVM(vss.core_settings.vol_surface_name, ProtoBufSerializationHelper.clone_protobuf(_settings), settings => {
// _settings = settings;
// _recreate_series();
// })
// };
return null;
var vm = new VolSurfaceGraphSettingsVM(vss.core_settings.vol_surface_name);
vm.load_from(_settings.Clone());
vm.request_commit += commit;
return new VolSurfaceGraphSettingsWindow {
DataContext = vm
};
void commit(VolSurfaceGraphSettings settings) {
_settings = settings;
_recreate_series();
}
});
graph_vm = new BTSGraphVM {
plot_area_background = Colors.Transparent,
@@ -185,6 +191,20 @@ namespace mbts.spark {
static (double call_wing_start, double put_wing_start) _get_actual_wing_starts(VolCurveDisplayGraph disp_graph) =>
disp_graph.all_points.Select(p => p.Item1).MinMax() ?? (double.NaN, double.NaN);
void _recreate_series() {
/// 'graph_vm.series_set' is bound to '_expirations', so clearing and re-adding expirations
/// in turn regenerates the series. '_expiration_series_cache' must
/// be cleared as well, otherwise cached series vms would just be back to the collection
/// ('graph_vm.series_set').
var visible_exp = _earliest_selected_expiration;
_expiration_series_cache.Clear();
_expirations.SetItems(_model.get_ordered_expirations());
foreach (var svm in graph_vm.series.OfType<SeriesSetVM>()) {
svm.is_visible = svm.args.Equals(visible_exp);
}
_update();
}
void _expirations_changed(IEnumerable<VolSurfaceDisplayGridExpiration> added, IEnumerable<VolSurfaceDisplayGridExpiration> removed) {
_expirations.sync_with(_model.get_ordered_expirations());
_update();
@@ -1,72 +0,0 @@
using mbts.ui;
using mbts.ui.controls.graph;
using System;
using System.Windows.Input;
using System.Windows.Media;
namespace mbts.spark
{
class VolSurfaceGraphSettingsEditorVM
{
public VolSurfaceGraphSettingsEditorVM(string vol_surface_name, VolSurfaceGraphSettings settings, Action<VolSurfaceGraphSettings> commit) {
this.vol_surface_name = vol_surface_name;
_settings = settings;
this.commit = new DelegateCommand(() => commit(_settings));
}
private readonly VolSurfaceGraphSettings _settings;
public string vol_surface_name { get; private set; }
public VolSurfaceScatterSeriesSettingsVM implied_puts { get { return new VolSurfaceScatterSeriesSettingsVM(_settings.implied_puts); } }
public VolSurfaceScatterSeriesSettingsVM implied_atm { get { return new VolSurfaceScatterSeriesSettingsVM(_settings.implied_atm); } }
public VolSurfaceScatterSeriesSettingsVM implied_calls { get { return new VolSurfaceScatterSeriesSettingsVM(_settings.implied_calls); } }
public VolSurfaceScatterSeriesSettingsVM put_control_points { get { return new VolSurfaceScatterSeriesSettingsVM(_settings.put_control_points); } }
public VolSurfaceScatterSeriesSettingsVM atm { get { return new VolSurfaceScatterSeriesSettingsVM(_settings.atm); } }
public VolSurfaceScatterSeriesSettingsVM call_control_points { get { return new VolSurfaceScatterSeriesSettingsVM(_settings.call_control_points); } }
public VolSurfaceLineSeriesSettingsVM put_wing { get { return new VolSurfaceLineSeriesSettingsVM(_settings.put_wing); } }
public VolSurfaceLineSeriesSettingsVM inner_curve { get { return new VolSurfaceLineSeriesSettingsVM(_settings.inner_curve); } }
public VolSurfaceLineSeriesSettingsVM call_wing { get { return new VolSurfaceLineSeriesSettingsVM(_settings.call_wing); } }
public VolSurfaceLineSeriesSettingsVM vol_path { get { return new VolSurfaceLineSeriesSettingsVM(_settings.vol_path); } }
public ICommand commit { get; private set; }
}
class VolSurfaceScatterSeriesSettingsVM
{
public VolSurfaceScatterSeriesSettingsVM(ScatterSeriesSettings settings) {
_settings = settings;
}
private readonly ScatterSeriesSettings _settings;
public MarkerShape shape { get { return _settings.shape; } set { _settings.shape = value; } }
public double size { get { return _settings.size; } set { _settings.size = value; } }
public Color fill {
get => _settings.fill;
set => _settings.fill = value;
}
public Color stroke {
get => _settings.stroke;
set => _settings.stroke = value;
}
public Array shapes { get { return Enum.GetValues(typeof(MarkerShape)); } }
}
class VolSurfaceLineSeriesSettingsVM
{
public VolSurfaceLineSeriesSettingsVM(LineSeriesSettings settings) {
_settings = settings;
}
private readonly LineSeriesSettings _settings;
public Color color { get { return (Color)_settings.color; } set { _settings.color = (BTSColor)value; } }
public double thickness { get { return _settings.stroke_thickness; } set { _settings.stroke_thickness = value; } }
}
}
@@ -0,0 +1,161 @@
<UserControl
x:Class="Spark.Analytics.View.VolSurfaceGraphSettingsView"
x:ClassModifier="internal"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:u="clr-namespace:mbts.ui"
xmlns:c="clr-namespace:mbts.ui.controls"
Grid.IsSharedSizeScope="True">
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/view/Controls/ExpandedContentSelector.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style TargetType="GroupBox">
<Setter Property="Margin" Value="8" />
<Setter Property="Padding" Value="5" />
<Setter Property="HeaderTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock Text="{Binding}" FontWeight="Bold" />
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="GroupBox" x:Key="inner" BasedOn="{StaticResource {x:Type GroupBox}}">
<Setter Property="HeaderTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock Text="{Binding}" FontWeight="Regular" />
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
<ControlTemplate x:Key="ScatterSeriesTemplate">
<Grid HorizontalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" SharedSizeGroup="label" />
<ColumnDefinition Width="*" SharedSizeGroup="value" />
</Grid.ColumnDefinitions>
<TextBlock Text="Shape" VerticalAlignment="Center" Margin="7,4,7,4" />
<ComboBox
Grid.Column="1"
ItemsSource="{Binding shapes}"
Margin="3"
SelectedValue="{Binding shape}" />
<TextBlock Grid.Row="1" VerticalAlignment="Center" Text="Size" Margin="7,4,7,4" />
<c:TickUpDownDouble Grid.Row="1" Grid.Column="1" Margin="3" Value="{Binding size}" />
<TextBlock Grid.Row="2" VerticalAlignment="Center" Text="Color" Margin="7,4,7,4" />
<c:ColorPickerPopup
Grid.Row="2"
Grid.Column="1"
Margin="3"
DataContext="{Binding fill_vm}"
SelectedColor="{Binding color}"
Palettes="{Binding palettes}" />
</Grid>
</ControlTemplate>
<DataTemplate x:Key="LineSeriesTemplate">
<Grid HorizontalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" SharedSizeGroup="label" />
<ColumnDefinition Width="*" SharedSizeGroup="value" />
</Grid.ColumnDefinitions>
<TextBlock Text="Thickness" VerticalAlignment="Center" Margin="7,4,7,4" />
<c:TickUpDownDouble Grid.Column="1" Margin="3" Value="{Binding stroke_thickness}" />
<TextBlock Grid.Row="1" VerticalAlignment="Top" Text="Color" Margin="7,4,7,4" />
<c:ColorPickerPopup
Grid.Row="1"
Grid.Column="1"
VerticalAlignment="Top"
Margin="3"
DataContext="{Binding color_vm}"
SelectedColor="{Binding color}"
Palettes="{Binding palettes}" />
</Grid>
</DataTemplate>
</ResourceDictionary>
</UserControl.Resources>
<c:ExpandedContentSelector>
<c:ExpandedContentSelectorItem Header="Control Points">
<StackPanel Orientation="Horizontal">
<GroupBox
Header="Puts"
DataContext="{Binding put_control_points}"
Style="{StaticResource inner}">
<ContentControl Template="{StaticResource ScatterSeriesTemplate}" />
</GroupBox>
<GroupBox Header="ATM" DataContext="{Binding atm}" Style="{StaticResource inner}">
<ContentControl Template="{StaticResource ScatterSeriesTemplate}" />
</GroupBox>
<GroupBox
Header="Calls"
DataContext="{Binding call_control_points}"
Style="{StaticResource inner}">
<ContentControl Template="{StaticResource ScatterSeriesTemplate}" />
</GroupBox>
</StackPanel>
</c:ExpandedContentSelectorItem>
<c:ExpandedContentSelectorItem Header="Main Curve">
<StackPanel Orientation="Horizontal">
<GroupBox
Header="Put Wing"
DataContext="{Binding put_wing}"
Content="{Binding}"
ContentTemplate="{StaticResource LineSeriesTemplate}"
Style="{StaticResource inner}" />
<GroupBox
Header="Inner Curve"
DataContext="{Binding inner_curve}"
Content="{Binding}"
ContentTemplate="{StaticResource LineSeriesTemplate}"
Style="{StaticResource inner}" />
<GroupBox
Header="Call Wing"
DataContext="{Binding call_wing}"
Content="{Binding}"
ContentTemplate="{StaticResource LineSeriesTemplate}"
Style="{StaticResource inner}" />
</StackPanel>
</c:ExpandedContentSelectorItem>
<c:ExpandedContentSelectorItem Header="Implied Vol">
<StackPanel Orientation="Horizontal">
<GroupBox
Header="Puts" DataContext="{Binding implied_puts}" Style="{StaticResource inner}">
<ContentControl Template="{StaticResource ScatterSeriesTemplate}" />
</GroupBox>
<GroupBox
Header="ATM" DataContext="{Binding implied_atm}" Style="{StaticResource inner}">
<ContentControl Template="{StaticResource ScatterSeriesTemplate}" />
</GroupBox>
<GroupBox
Header="Calls" DataContext="{Binding implied_calls}" Style="{StaticResource inner}">
<ContentControl Template="{StaticResource ScatterSeriesTemplate}" />
</GroupBox>
</StackPanel>
</c:ExpandedContentSelectorItem>
<c:ExpandedContentSelectorItem Header="Vol Path">
<ContentControl
Margin="16,8"
DataContext="{Binding vol_path}"
Content="{Binding}"
ContentTemplate="{StaticResource LineSeriesTemplate}" />
</c:ExpandedContentSelectorItem>
</c:ExpandedContentSelector>
</UserControl>
@@ -0,0 +1,9 @@
#nullable enable
namespace Spark.Analytics.View {
using System.Windows.Controls;
partial class VolSurfaceGraphSettingsView : UserControl {
public VolSurfaceGraphSettingsView() =>
InitializeComponent();
}
}
@@ -0,0 +1,31 @@
<Window
x:Class="Spark.Analytics.View.VolSurfaceGraphSettingsWindow"
x:ClassModifier="internal"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:analytics="clr-namespace:Spark.Analytics.View"
xmlns:u="clr-namespace:mbts.ui"
xmlns:c="clr-namespace:mbts.ui.controls"
Title="{Binding vol_surface_name, StringFormat='{}Graph Settings \&apos;{0}\&apos;'}"
Style="{StaticResource BTSDialog}"
u:WindowStyle.SizeToContent="WidthAndHeight"
u:WindowStyle.AllowResize="True"
Grid.IsSharedSizeScope="True">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<ScrollViewer
VerticalScrollBarVisibility="Auto"
HorizontalScrollBarVisibility="Auto"
VerticalAlignment="Stretch"
HorizontalAlignment="Stretch">
<analytics:VolSurfaceGraphSettingsView />
</ScrollViewer>
<c:DialogSkirtOKApplyCancel
Grid.Row="1" OKCommand="{Binding commit_cmd}" ApplyCommand="{Binding apply_cmd}" />
</Grid>
</Window>
@@ -0,0 +1,9 @@
#nullable enable
namespace Spark.Analytics.View {
using System.Windows;
partial class VolSurfaceGraphSettingsWindow : Window {
public VolSurfaceGraphSettingsWindow() =>
InitializeComponent();
}
}
@@ -0,0 +1,85 @@
#nullable enable
namespace mbts.spark {
using mbts.ui;
using System;
using System.Windows.Input;
class VolSurfaceGraphSettingsVM : IRequestViewClose {
/// <summary>
/// A reference to the settings obj last loaded (<see cref="load_from"/>). It contains props
/// that aren't edited by this class. It is kept because a settings obj generated by this
/// class from scratch would lose some information from the original settings obj.
/// </summary>
VolSurfaceGraphSettings? _settings;
public VolSurfaceGraphSettingsVM(string vol_surface_name) {
this.vol_surface_name = vol_surface_name;
apply_cmd = new Command(apply);
commit_cmd = new Command(commit);
void apply() {
if (request_commit is null) {
return;
}
var res = _settings?.Clone() ?? new();
assign_to(res);
request_commit(res);
}
void commit() {
apply();
RequestClose?.Invoke(this, EventArgs.Empty);
}
}
public event EventHandler? RequestClose;
public event Action<VolSurfaceGraphSettings>? request_commit;
public string vol_surface_name { get; }
public VolSurfaceScatterSeriesSettingsVM implied_puts { get; } = new();
public VolSurfaceScatterSeriesSettingsVM implied_atm { get; } = new();
public VolSurfaceScatterSeriesSettingsVM implied_calls { get; } = new();
public VolSurfaceScatterSeriesSettingsVM put_control_points { get; } = new();
public VolSurfaceScatterSeriesSettingsVM atm { get; } = new();
public VolSurfaceScatterSeriesSettingsVM call_control_points { get; } = new();
public VolSurfaceLineSeriesSettingsVM put_wing { get; } = new();
public VolSurfaceLineSeriesSettingsVM inner_curve { get; } = new();
public VolSurfaceLineSeriesSettingsVM call_wing { get; } = new();
public VolSurfaceLineSeriesSettingsVM vol_path { get; } = new();
public ICommand apply_cmd { get; }
public ICommand commit_cmd { get; }
public void assign_to(VolSurfaceGraphSettings settings) {
implied_puts.assign_to(settings.implied_puts);
implied_atm.assign_to(settings.implied_atm);
implied_calls.assign_to(settings.implied_calls);
put_control_points.assign_to(settings.put_control_points);
atm.assign_to(settings.atm);
call_control_points.assign_to(settings.call_control_points);
put_wing.assign_to(settings.put_wing);
inner_curve.assign_to(settings.inner_curve);
call_wing.assign_to(settings.call_wing);
vol_path.assign_to(settings.vol_path);
}
public void load_from(VolSurfaceGraphSettings settings) {
_settings = settings;
implied_puts.load_from(settings.implied_puts);
implied_atm.load_from(settings.implied_atm);
implied_calls.load_from(settings.implied_calls);
put_control_points.load_from(settings.put_control_points);
atm.load_from(settings.atm);
call_control_points.load_from(settings.call_control_points);
put_wing.load_from(settings.put_wing);
inner_curve.load_from(settings.inner_curve);
call_wing.load_from(settings.call_wing);
vol_path.load_from(settings.vol_path);
}
}
}
@@ -0,0 +1,30 @@
#nullable enable
namespace mbts.spark {
using mbts.ui.controls;
using mbts.ui.shell;
class VolSurfaceLineSeriesSettingsVM : ViewModelBase {
readonly Property<double> _stroke_thickness;
public VolSurfaceLineSeriesSettingsVM() {
_stroke_thickness = backing(nameof(stroke_thickness), 2.0);
}
public double stroke_thickness {
get => _stroke_thickness.Value;
set => _stroke_thickness.set(value);
}
public ColorPickerPopupVM color_vm { get; } = new() { bind_opacity_to_alpha = true };
public void load_from(LineSeriesSettings settings) {
color_vm.color = settings.color;
stroke_thickness = settings.stroke_thickness;
}
public void assign_to(LineSeriesSettings settings) {
settings.color = color_vm.color;
settings.stroke_thickness = stroke_thickness;
}
}
}
@@ -0,0 +1,47 @@
#nullable enable
namespace mbts.spark {
using mbts.ui.controls;
using mbts.ui.shell;
using mbts.ui.utility;
using System.Collections.Generic;
class VolSurfaceScatterSeriesSettingsVM : ViewModelBase {
readonly Property<MarkerShape> _shape;
readonly Property<double> _size;
public VolSurfaceScatterSeriesSettingsVM() {
_shape = backing(nameof(shape), MarkerShape.Circle);
_size = backing(nameof(size), 2.0);
}
public MarkerShape shape {
get => _shape.Value;
set => _shape.set(value);
}
public double size {
get => _size.Value;
set => _size.set(value);
}
public IReadOnlyList<MarkerShape> shapes =>
EnumHelper.get_values<MarkerShape>();
public ColorPickerPopupVM fill_vm { get; } = new() { bind_opacity_to_alpha = true };
public ColorPickerPopupVM stroke_vm { get; } = new() { bind_opacity_to_alpha = true };
public void load_from(ScatterSeriesSettings settings) {
shape = settings.shape;
size = settings.size;
fill_vm.color = settings.fill;
stroke_vm.color = settings.stroke;
}
public void assign_to(ScatterSeriesSettings settings) {
settings.fill = fill_vm.color;
settings.stroke = stroke_vm.color;
settings.shape = shape;
}
}
}