Contract picker - View, Criteria Clause, and auto-generating name clause on select

This commit is contained in:
Jake Norbut committed 2017-11-29 15:16:32 -06:00
1 parent 23b0b637f9
commit 512d907d54
4 files changed
+163 -182

No files matched your search

@@ -18,17 +18,20 @@ using ProtoBuf;
using mbts.ui;
using System.IO;
using System.Windows.Input;
using System.Windows;
namespace mbts.spark {
internal class ContractPickerModel : ViewModelBase {
public ContractPickerModel(IBus bus) {
public ContractPickerModel(IBus bus)
{
_bus = bus;
_subs.Add(_bus.subscribe(SecuritiesTag).Subscribe(on_securities_changed));
add_criteria_command = new DelegateCommand(_ => {
security_criteria.Add(new SecurityCriteria()
add_criteria_command = new DelegateCommand(_ =>
{
security_criteria.Add(new SecurityCriteriaClause()
{
pretty_name = criteria_input_name,
exchanges = selected_exchanges.ToArray(),
@@ -41,16 +44,45 @@ namespace mbts.spark {
remove_criteria_command = new DelegateCommand(_ =>
{
security_criteria.Remove(selected_security_criteria);
security_criteria.Remove(selected_security_criteria_clause);
});
}
private readonly IBus _bus;
private IList<IDisposable> _subs = new List<IDisposable>();
private void _compose_criteria_clause_name()
{
string proposed_name = string.Empty;
if (selected_securities.Count > 0)
{
proposed_name = all_securities.Where(s => selected_securities.Contains(s.Key)).Select(sec => sec.Value.security.ToString()).Aggregate((i, j) => $"{i} and {j}");
} else if (selected_symbols.Count > 0)
{
proposed_name = $"{selected_symbols.Aggregate((i, j) => $"{i} and {j}")}";
if (selected_security_types.Count > 0)
{
proposed_name += $" {selected_security_types.Select(sec_type => sec_type.ToString()).Aggregate((i,j) => $"{i}s and {j}s")}";
}
if (selected_exchanges.Count > 0)
{
proposed_name += $" at {selected_exchanges.Select(exch => exch.ToString()).Aggregate((i,j) => $"{i} and {j}")}";
}
} else if (selected_security_types.Count > 0)
{
proposed_name = $"{selected_security_types.Select(sec_type => sec_type.ToString()).Aggregate((i, j) => $"{i}s and {j}s")}";
if (selected_exchanges.Count > 0)
{
proposed_name += $" at {selected_exchanges.Select(exch => exch.ToString()).Aggregate((i, j) => $"{i} and {j}")}";
}
} else if (selected_exchanges.Count > 0)
{
proposed_name = $"{selected_exchanges.Select(exch => exch.ToString()).Aggregate((i, j) => $"{i} and {j}")}";
}
criteria_input_name = proposed_name;
}
internal void exchange_selection_changed(IEnumerable<Venue> exchanges)
{
@@ -61,35 +93,32 @@ namespace mbts.spark {
.Distinct()
.ToList());
_update_securities();
// test on click -- verify our func works
var results = all_securities.Select(all => all.Value.security).Where(x => _security_in_criteria(x)).ToList();
_compose_criteria_clause_name();
}
internal void security_type_selection_changed(IEnumerable<SecurityType> security_types)
{
selected_security_types = security_types.ToList();
_update_securities();
// test on click -- verify our func works
var results = all_securities.Select(all => all.Value.security).Where(x => _security_in_criteria(x)).ToList();
_compose_criteria_clause_name();
}
internal void symbol_selection_changed(IEnumerable<string> symbols)
{
selected_symbols = symbols.ToList();
_update_securities();
// test on click -- verify our func works
var results = all_securities.Select(all => all.Value.security).Where(x => _security_in_criteria(x)).ToList();
_compose_criteria_clause_name();
}
internal void security_selection_changed(IEnumerable<SecurityImmutable> secs)
{
selected_securities = secs.Select(s => s.Id).ToList();
_compose_criteria_clause_name();
// _security_in_criteria is our fun to be returned to our callers
// test on click -- verify our func works
var results = all_securities.Select(all => all.Value.security).Where( x => _security_in_criteria(x)).ToList();
// var results = all_securities.Select(all => all.Value.security).Where( x => _security_in_criteria(x)).ToList();
}
private void on_securities_changed(IReadOnlyDictionary<uint, SecurityMD> changed_securities)
@@ -100,29 +129,28 @@ namespace mbts.spark {
);
_update_securities();
}
internal void criteria_selection_changed(SecurityCriteria changed_criteria)
internal void criteria_selection_changed(SecurityCriteriaClause changed_clause)
{
// this is currently not changing the selected item in the main listboxes, need to investigate
if (changed_criteria != null)
selected_security_criteria_clause = changed_clause;
if (changed_clause != null)
{
// need security key to description lookup here
SecurityCriteriaChangedEventsArgs args = new SecurityCriteriaChangedEventsArgs();
args.securities = all_securities.Where(s => changed_criteria.security_keys.Contains(s.Key)).Select(sec => sec.Value.security).ToArray();
args.criteria = changed_criteria;
SecurityCriteriaClauseChangedEventsArgs args = new SecurityCriteriaClauseChangedEventsArgs();
args.securities = all_securities.Where(s => changed_clause.security_keys.Contains(s.Key)).Select(sec => sec.Value.security).ToArray();
args.clause = changed_clause;
on_security_criteria_changed(args);
on_security_criteria_clause_changed(args);
}
}
protected virtual void on_security_criteria_changed(SecurityCriteriaChangedEventsArgs e)
protected virtual void on_security_criteria_clause_changed(SecurityCriteriaClauseChangedEventsArgs e)
{
security_criteria_changed?.Invoke(this, e);
security_criteria_clause_changed?.Invoke(this, e);
}
public List<Venue> all_exchanges { get; } = new List<Venue> { Venue.Glbx, Venue.Cboe };//UserSymbol.get_all().Select(x => x.exchange).Distinct().ToList();
public List<Venue> selected_exchanges = new List<Venue>();
public IEnumerable<SecurityType> security_types { get; } = EnumHelper.get_values_except(SecurityType.Option, SecurityType.Unset);
public IEnumerable<SecurityType> security_types { get; } = EnumHelper.get_values_except(SecurityType.Option, SecurityType.Stock, SecurityType.Unset);
public List<SecurityType> selected_security_types = new List<SecurityType>();
public ObservableCollection<string> symbols { get; } = new ObservableCollection<string>(UserSymbol.get_all().Select(s => s.symbol).Distinct());
@@ -134,16 +162,16 @@ namespace mbts.spark {
public ICommand add_criteria_command { get; }
public ICommand remove_criteria_command { get; }
public ObservableCollection<SecurityCriteria> security_criteria { get; } = new ObservableCollection<SecurityCriteria>();
public ObservableCollection<SecurityCriteriaClause> security_criteria { get; } = new ObservableCollection<SecurityCriteriaClause>();
private SecurityCriteria _selected_security_criteria = new SecurityCriteria();
public SecurityCriteria selected_security_criteria
private SecurityCriteriaClause _selected_security_criteria_clause = new SecurityCriteriaClause();
public SecurityCriteriaClause selected_security_criteria_clause
{
get { return _selected_security_criteria; }
get { return _selected_security_criteria_clause; }
set
{
_selected_security_criteria = value;
RaisePropertyChanged("selected_security_criteria");
_selected_security_criteria_clause = value;
RaisePropertyChanged("selected_security_criteria_clause");
}
}
@@ -155,10 +183,9 @@ namespace mbts.spark {
}
}
public event EventHandler<SecurityCriteriaChangedEventsArgs> security_criteria_changed;
public event EventHandler<SecurityCriteriaClauseChangedEventsArgs> security_criteria_clause_changed;
private void _update_securities()
{
securities.sync_with(all_securities
@@ -173,39 +200,32 @@ namespace mbts.spark {
.Where(sec => selected_symbols.Count == 0 || selected_symbols
.Any(sym => sym == sec.Symbol))
);
// test of serializing/de to/from protobuf
MemoryStream test = new MemoryStream();
get_state(test);
test.Position = 0;
try_deserialize(test, out ShellSerializationHeader header);
try_deserialize(test, out StartupData data);
}
private bool _security_in_criteria(SecurityImmutable sec_item)
{
// Mock criteria to test against
var item1 = new SecurityCriteria()
var item1 = new SecurityCriteriaClause()
{
exchanges = new Venue[] { },
security_types = new SecurityType[] { },
symbols = new string[] { },
security_keys = new uint[] { 176868 }
};
var item2 = new SecurityCriteria()
var item2 = new SecurityCriteriaClause()
{
exchanges = new Venue[] { },
security_types = new SecurityType[] { SecurityType.Future },
symbols = new string[] {"GE"},
security_keys = new uint[] { }
};
SecurityCriteria[] a = new SecurityCriteria[] { item1, item2 };
SecurityCriteria a = new SecurityCriteria();
a.clauses.AddRange(new SecurityCriteriaClause[] { item1, item2 } );
List<bool> criteria_results = new List<bool>(a.Length + 1);
List<bool> criteria_results = new List<bool>(a.clauses.Length + 1);
criteria_results.Add(false);
foreach (SecurityCriteria x in a)
// need to add another arg to class constructor to accept SecurityCriteria[]
foreach (SecurityCriteriaClause x in a.clauses)
{
bool iteration_result = (x.exchanges.Length == 0 || x.exchanges.Any(exchange => exchange == sec_item.Exchange))
&& (x.security_types.Length == 0 || x.security_types.Any(sec_type => sec_type == sec_item.security_type()))
@@ -216,76 +236,20 @@ namespace mbts.spark {
bool final_result = criteria_results.Any(r => r);
return final_result;
//var overall_predicate = PredicateBuilder.True<SecurityImmutable>();
//foreach (SecurityCriteria entry in a)
//{
// var inner_exchange_predicate = PredicateBuilder.False<SecurityImmutable>();
// foreach (Venue v in entry.exchanges)
// {
// Venue temp_v = v;
// inner_exchange_predicate = inner_exchange_predicate.Or(p => p.Exchange == temp_v);
// }
// //predicate = predicate.And(exchange_predicate);
// var inner_type_predicate = PredicateBuilder.False<SecurityImmutable>();
// foreach (SecurityType s_type in entry.security_types)
// {
// SecurityType temp_s = s_type;
// inner_type_predicate = inner_type_predicate.Or(p => p.security_type() == temp_s);
// }
// var entry_predicate = PredicateBuilder.True<SecurityImmutable>();
// entry_predicate = entry_predicate.And(inner_exchange_predicate).And(inner_type_predicate);
// overall_predicate = overall_predicate.Or(entry_predicate);
//}
//return entry_predicate.Compile();
}
public void get_state(Stream save_here)
{
// Mock data to be saved
var x = new SecurityCriteria()
{
exchanges = selected_exchanges.ToArray(),
security_types = selected_security_types.ToArray(),
symbols = selected_symbols.ToArray(),
security_keys = selected_securities.ToArray()
};
SecurityCriteria[] a = new SecurityCriteria[] { x };
//ProtoBufSerializationHelper.serialize(save_here, new ShellSerializationHeader { name = SAVE_NAME, version = VERSION });
//ProtoBufSerializationHelper.serialize(save_here, new StartupData(a));
}
public void Dispose()
{
Disposable.dispose(_subs);
}
}
internal class SecurityCriteriaChangedEventsArgs : EventArgs
internal class SecurityCriteriaClauseChangedEventsArgs : EventArgs
{
public SecurityCriteria criteria { get; set; }
public SecurityCriteriaClause clause { get; set; }
public SecurityImmutable[] securities { get; set; }
}
[ProtoContract]
public class StartupData
{
public const int Version = 1;
public StartupData() { }
public StartupData(SecurityCriteria[] security_criteria)
{
this.security_criteria = security_criteria;
}
[ProtoMember(1)]
public SecurityCriteria[] security_criteria;
}
}
@@ -4,49 +4,50 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
x:ClassModifier="internal"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="600">
mc:Ignorable="d" d:DesignWidth="561.489">
<DockPanel>
<Grid Grid.IsSharedSizeScope="True" DockPanel.Dock="Top" ShowGridLines="True" Background="Bisque">
<Grid Grid.IsSharedSizeScope="True" DockPanel.Dock="Top" Margin="12,0,12,0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="250"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="share_col_width" Width="Auto" />
<ColumnDefinition SharedSizeGroup="share_col_width" Width="Auto" />
<ColumnDefinition SharedSizeGroup="share_col_width" Width="Auto"/>
<ColumnDefinition Width="Auto" />
<ColumnDefinition SharedSizeGroup="share_col_width" Width="Auto"/>
<ColumnDefinition SharedSizeGroup="share_col_width" Width="Auto"/>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0" Content="Exchanges"/>
<Label Grid.Row="0" Grid.Column="1" Content="Types"/>
<Label Grid.Row="0" Grid.Column="2" Content="Symbols"/>
<Label Grid.Row="0" Grid.Column="3" Content="Instruments"/>
<ListBox x:Name="_exchanges" Grid.Row ="1" Grid.Column="0" SelectionMode="Extended" ItemsSource="{Binding all_exchanges}" Height="200" VerticalAlignment="Top" ></ListBox>
<ListBox x:Name="_security_types" Grid.Row ="1" Grid.Column="1" SelectionMode="Extended" ItemsSource="{Binding security_types}" Height="200" VerticalAlignment="Top"></ListBox>
<ListBox x:Name="_symbols" Grid.Row ="1" Grid.Column="2" SelectionMode="Extended" ItemsSource="{Binding symbols}" Height="200" VerticalAlignment="Top"></ListBox>
<ListBox x:Name="_securities" Grid.Row ="1" Grid.Column="3" SelectionMode="Extended" ItemsSource="{Binding securities}" Height="200" Width="200" VerticalAlignment="Top" ></ListBox>
<Grid Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="3">
<GroupBox Grid.Row="0" Grid.Column="0" Header="Exchanges">
<ListBox x:Name="_exchanges" Grid.Row ="1" Grid.Column="0" SelectionMode="Extended" ItemsSource="{Binding all_exchanges}" Margin="2" ></ListBox>
</GroupBox>
<GroupBox Grid.Row="0" Grid.Column="1" Header="Types">
<ListBox x:Name="_security_types" Grid.Row ="1" Grid.Column="1" SelectionMode="Extended" ItemsSource="{Binding security_types}" Margin="2"></ListBox>
</GroupBox>
<GroupBox Grid.Row="0" Grid.Column="2" Header="Symbols">
<ListBox x:Name="_symbols" Grid.Row ="1" Grid.Column="2" SelectionMode="Extended" ItemsSource="{Binding symbols}" Margin="2"></ListBox>
</GroupBox>
<GroupBox Grid.Row="0" Grid.Column="3" Header="Instruments">
<ListBox x:Name="_securities" Grid.Row ="1" Grid.Column="3" SelectionMode="Extended" ItemsSource="{Binding securities}" Margin="2" ></ListBox>
</GroupBox>
</Grid>
<UniformGrid HorizontalAlignment="Right" Rows="1" Columns="2" DockPanel.Dock="Bottom" Margin="0,12,12,12">
<Button Content="Ok" Width="50" Margin="12,0,0,0" />
<Button Content="Cancel" Width="50" Margin="12,0,0,0" />
</UniformGrid>
<GroupBox Header="Picked instruments" DockPanel.Dock="Bottom" Margin="12,0,12,0">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*" MinHeight="100"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="300" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBox Grid.Row="0" Grid.Column="0" Text="{Binding criteria_input_name}" Width="100"></TextBox>
<Button Grid.Row="0" Grid.Column="1" Command="{Binding add_criteria_command}" HorizontalAlignment="Right">Add</Button>
<ListBox x:Name="_criteria" Grid.Row="1" Grid.Column="0" ItemsSource="{Binding security_criteria}" SelectedItem="{Binding selected_security_criteria}" Height="50"></ListBox>
<Button Grid.Row="1" Grid.Column="1" Command="{Binding remove_criteria_command}" HorizontalAlignment="Right">Remove</Button>
<TextBox Grid.Row="0" Grid.Column="0" Text="{Binding criteria_input_name}" Margin="2" IsReadOnly="True"></TextBox>
<Button Grid.Row="0" Grid.Column="1" Command="{Binding add_criteria_command}" Margin="2" HorizontalAlignment="Left" Width="50">Add</Button>
<ListBox x:Name="_criteria" Grid.Row="1" Grid.Column="0" ItemsSource="{Binding security_criteria}" SelectedItem="{Binding selected_security_criteria_clause}" Margin="2"></ListBox>
<Button Grid.Row="1" Grid.Column="1" Command="{Binding remove_criteria_command}" Margin="2" HorizontalAlignment="Left" VerticalAlignment="Top" Width="50">Remove</Button>
</Grid>
<UniformGrid Grid.Row="3" Rows="1" Columns="2" DockPanel.Dock="Bottom">
<Button>OK</Button>
<Button>Cancel</Button>
</UniformGrid>
</Grid>
</GroupBox>
</DockPanel>
</UserControl>
@@ -19,80 +19,86 @@ using System.ComponentModel.Composition;
namespace mbts.spark
{
/// <summary>
/// Interaction logic for ContractPickerView.xaml
/// </summary>
partial class ContractPickerView : UserControl
{
/// <summary>
/// Interaction logic for ContractPickerView.xaml
/// </summary>
partial class ContractPickerView : UserControl
{
public ContractPickerView(ContractPickerModel context)
{
InitializeComponent();
this.DataContext = context;
public ContractPickerView(ContractPickerModel context)
{
InitializeComponent();
this.DataContext = context;
_exchanges.SelectionChanged += exchanges_changed;
_security_types.SelectionChanged += security_types_changed;
_symbols.SelectionChanged += symbols_changed;
_securities.SelectionChanged += securities_changed;
_criteria.SelectionChanged += criteria_changed;
context.security_criteria_changed += security_criteria_changed;
}
_exchanges.SelectionChanged += exchanges_changed;
_security_types.SelectionChanged += security_types_changed;
_symbols.SelectionChanged += symbols_changed;
_securities.SelectionChanged += securities_changed;
_criteria.SelectionChanged += criteria_changed;
context.security_criteria_clause_changed += security_criteria_clause_changed;
}
private void security_criteria_changed(object sender, SecurityCriteriaChangedEventsArgs e)
private void security_criteria_clause_changed(object sender, SecurityCriteriaClauseChangedEventsArgs e)
{
if (e != null)
{
_exchanges.SelectedItems.Clear();
foreach (var exch in e.criteria.exchanges)
foreach (var exch in e.clause.exchanges)
{
_exchanges.SelectedItems.Add(exch);
}
_exchanges.ScrollIntoView(e.clause.exchanges.FirstOrDefault());
_security_types.SelectedItems.Clear();
foreach (var sec_type in e.criteria.security_types)
foreach (var sec_type in e.clause.security_types)
{
_security_types.SelectedItems.Add(sec_type);
}
_security_types.ScrollIntoView(e.clause.security_types.FirstOrDefault());
_symbols.SelectedItems.Clear();
foreach (var sym in e.criteria.symbols)
foreach (var sym in e.clause.symbols)
{
_symbols.SelectedItems.Add(sym);
}
_symbols.ScrollIntoView(e.clause.symbols.FirstOrDefault());
_securities.SelectedItems.Clear();
foreach (var sec in e.securities)
{
_securities.SelectedItems.Add(sec);
}
_securities.ScrollIntoView(e.securities.FirstOrDefault());
}
}
private void exchanges_changed(object sender, SelectionChangedEventArgs e)
{
((ContractPickerModel)DataContext).exchange_selection_changed(_exchanges.SelectedItems.Cast<Venue>());
}
private void security_types_changed(object sender, SelectionChangedEventArgs e)
{
((ContractPickerModel)DataContext).security_type_selection_changed(_security_types.SelectedItems.Cast<SecurityType>());
}
private void securities_changed(object sender, SelectionChangedEventArgs e)
{
((ContractPickerModel)DataContext).security_selection_changed(_securities.SelectedItems.Cast<SecurityImmutable>());
}
private void symbols_changed(object sender, SelectionChangedEventArgs e)
{
((ContractPickerModel)DataContext).symbol_selection_changed(_symbols.SelectedItems.Cast<string>());
}
{
((ContractPickerModel)DataContext).symbol_selection_changed(_symbols.SelectedItems.Cast<string>());
}
private void security_types_changed(object sender, SelectionChangedEventArgs e)
{
((ContractPickerModel)DataContext).security_type_selection_changed(_security_types.SelectedItems.Cast<SecurityType>());
}
private void exchanges_changed(object sender, SelectionChangedEventArgs e)
{
((ContractPickerModel)DataContext).exchange_selection_changed(_exchanges.SelectedItems.Cast<Venue>());
}
private void securities_changed(object sender, SelectionChangedEventArgs e)
{
((ContractPickerModel)DataContext).security_selection_changed(_securities.SelectedItems.Cast<SecurityImmutable>());
}
private void criteria_changed(object sender, SelectionChangedEventArgs e)
{
((ContractPickerModel)DataContext).criteria_selection_changed(_criteria.SelectedItem as SecurityCriteria);
}
private void criteria_changed(object sender, SelectionChangedEventArgs e)
{
((ContractPickerModel)DataContext).criteria_selection_changed(_criteria.SelectedItem as SecurityCriteriaClause);
}
}
//[Export (typeof(IShellModule))]
[Export (typeof(IShellModule))]
public class TempModule : IShellModule {
private IBus _bus;
@@ -133,6 +139,7 @@ namespace mbts.spark
var x = new Window {
Content = new ContractPickerView(new ContractPickerModel(_bus))
};
x.SizeToContent = SizeToContent.WidthAndHeight;
x.Show();
}
}
@@ -10,6 +10,15 @@ namespace mbts.spark
{
public SecurityCriteria() { }
[ProtoMember(1)]
public SecurityCriteriaClause[] clauses;
}
[ProtoContract]
public class SecurityCriteriaClause
{
public SecurityCriteriaClause() { }
[ProtoMember(1)]
public string pretty_name;