89 lines
3.9 KiB
C#
89 lines
3.9 KiB
C#
using bts.api;
|
|
using bts.spark;
|
|
using mbts;
|
|
using mbts.spark;
|
|
using mbts.ui;
|
|
using mbts.ui.spark;
|
|
using mbts.ui.utility;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel.Composition;
|
|
using System.Linq;
|
|
using System.Reactive.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace bts.shell {
|
|
[Export(typeof(IInitializable))]
|
|
partial class ShowSyntheticEditorHandler : IInitializable, IDisposable {
|
|
readonly SpreaderModel _model;
|
|
readonly IBus _bus;
|
|
|
|
IDisposable _dispose;
|
|
|
|
[ImportingConstructor]
|
|
public ShowSyntheticEditorHandler(IBus bus, SpreaderModel model) {
|
|
_bus = bus;
|
|
_model = model;
|
|
}
|
|
|
|
static void _show_synthetic_editor(SyntheticEditorVM vm) {
|
|
var v = new SyntheticEditorView { DataContext = vm };
|
|
WindowUtilities.listen_for_close_requests(v);
|
|
v.Show(-250, -200, true);
|
|
}
|
|
|
|
async void _show_synthetic_create(SecurityCriteria security_criteria) =>
|
|
_show_synthetic_create((await security_criteria.securities(_bus)).ToList());
|
|
|
|
void _show_synthetic_create(IReadOnlyCollection<SecurityImmutable> securities) {
|
|
if (securities.Count == 1) {
|
|
var sec = securities.First();
|
|
switch (sec.ContractCase) {
|
|
case Security.ContractOneofCase.Future: _show_synthetic_create(new ContractLeg[] { SyntheticDataHelper.Defaults.ContractLeg(sec) }); break;
|
|
case Security.ContractOneofCase.Spread: _show_synthetic_create(sec.Spread.Legs.Select(l => l.CloneToMutable())); break;
|
|
case Security.ContractOneofCase.Synthetic: _show_synthetic_create(sec.Synthetic.Legs.Select(l => l.CloneToMutable())); break;
|
|
default: Logger.warn(log.Subsystem.Ui, $"{GetType()} unable to process security: {sec}"); break;
|
|
}
|
|
} else
|
|
_show_synthetic_create(securities.Select(s => new ContractLeg(SyntheticDataHelper.Defaults.ContractLeg(s))));
|
|
}
|
|
|
|
void _show_synthetic_strategy(SecurityImmutable security) {
|
|
if (security.ContractCase != Security.ContractOneofCase.Synthetic)
|
|
Logger.warn(log.Subsystem.Ui, $"{GetType()} observed a non-synthetic-security posted on {nameof(SparkTags.ShowSyntheticStrategyTag)}: {security}");
|
|
else
|
|
_show_synthetic_strategy_async(security).ignore();
|
|
}
|
|
|
|
async Task _show_synthetic_strategy_async(SecurityImmutable verified_synthetic) {
|
|
var lookup = await _model.lookup_strategy_async(verified_synthetic);
|
|
if (lookup.success) _show_synthetic_editor(new SyntheticEditorVM(_model, verified_synthetic, lookup.result));
|
|
}
|
|
|
|
void _show_synthetic_create(IEnumerable<ContractLeg> legs) {
|
|
var syn = SyntheticDataHelper.Defaults.SyntheticContract("Synthetic", legs);
|
|
var sec = new SecurityImmutable(new Security {
|
|
Synthetic = syn,
|
|
Symbol = legs.FirstOrDefault()?.Security.Symbol ?? string.Empty,
|
|
ContractSpec = SecurityDataHelper.Defaults.ContractSpecification
|
|
});
|
|
_show_synthetic_editor(new SyntheticEditorVM(_bus, _model, sec, new ExecutionStrategyImmutable(SyntheticDataHelper.Defaults.ExecutionStrategy(sec)),
|
|
s => new LegSpreaderParamsTuple(SyntheticDataHelper.Defaults.ContractLeg(s), SyntheticDataHelper.Defaults.LegSpreaderParams)));
|
|
}
|
|
|
|
#region IInitializable
|
|
int IInitializable.priority => int.MaxValue;
|
|
|
|
void IInitializable.initialize() {
|
|
Disposable.dispose(ref _dispose);
|
|
_dispose = Disposable.Create(
|
|
_bus.subscribe(SparkTags.ShowSyntheticCreateTag).ObserveOnDispatcher().Subscribe(_show_synthetic_create),
|
|
_bus.subscribe(SparkTags.ShowSyntheticStrategyTag).ObserveOnDispatcher().Subscribe(_show_synthetic_strategy)
|
|
);
|
|
}
|
|
|
|
void IDisposable.Dispose() =>
|
|
Disposable.dispose(ref _dispose);
|
|
#endregion
|
|
}
|
|
} |