Merged in SPARK-1456 (pull request #44)

SPARK-1456 Centralized ladder re-center

Approved-by: Jake Norbut <jake.norbut@bluetradesys.com>
This commit is contained in:
Eric Harding committed 2018-07-16 11:23:19 +00:00
commit 8490151844
8 files changed
+84 -121

No files matched your search

-1
View File
@@ -36,7 +36,6 @@ namespace bts {
_deleted_synthetics = new HashSet<uint>(t.Where(kv => kv.Value.Deleted).Select(kv => kv.Key));
_bus.post(SparkTags.SyntheticstrategiesTag, t);
}));
new DataPersistor(_bus);
Task.Run(run);
}
@@ -0,0 +1,48 @@
using bts.api;
using mbts.spark;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace mbts.ui {
[Export(typeof(IShellDaemon))]
class RecenterDaemon : IShellDaemon, IDisposable {
bool _run = true;
readonly IBus _bus;
readonly TimeSpan _time_to_center;
DateTime _last_recenter;
IDisposable _dispose;
[ImportingConstructor]
public RecenterDaemon(IBus bus) {
_bus = bus;
_time_to_center = TimeSpan.FromSeconds(config.Configuration.get<bts.config.LadderKnobs>().seconds_to_recenter);
_last_recenter = DateTime.UtcNow;
_dispose = _bus.subscribe(SparkTags.RequestRecenterLadder).Subscribe(_ => trigger_recenter(SparkTags.RecenterLadderNow));
run();
}
void trigger_recenter(ITag<DateTime> tag) {
_last_recenter = DateTime.UtcNow;
_bus.post(tag, _last_recenter);
}
async void run() {
_run = true;
while (_run) {
await Task.Delay(TimeSpan.FromSeconds(5));
if (_last_recenter + _time_to_center < DateTime.UtcNow) {
trigger_recenter(SparkTags.RecenterLadderBegin);
}
}
}
public void Dispose() {
Disposable.dispose(ref _dispose);
}
}
}
+5 -3
View File
@@ -67,9 +67,11 @@ namespace mbts.spark
public static ITag<SecurityCriteria> ShowTimeAndSalesTag { get; } = new SparkTag<SecurityCriteria>("Show TimeAndSales");
#endregion
#region Persist
public static ITag<PersistMessage> PersistCommandTag { get; } = new SparkTag<PersistMessage>("PersistCommandTag");
public static ITag<PersistedItems> PersistedItemsTag { get; } = new SparkTag<PersistedItems>("PersistedItemsTag");
#region Ladder
public static ITag<Nothing> RequestRecenterLadder { get; } = new SparkTag<Nothing>(nameof(RequestRecenterLadder));
public static ITag<DateTime> RecenterLadderBegin { get; } = new SparkTag<DateTime>(nameof(RecenterLadderBegin));
public static ITag<DateTime> RecenterLadderNow { get; } = new SparkTag<DateTime>(nameof(RecenterLadderNow));
#endregion
public static ITag<uint> SyntheticDeleteTag { get; } = new SparkTag<uint>(nameof(SyntheticDeleteTag));
@@ -136,7 +136,6 @@ namespace bts.api.diagnostic {
}
public override Task<ScriptResultMessage> run(ScriptLines request) {
// append lines to _pending
foreach (var line in request.Lines) {
_pending.AppendLine(line);
@@ -1,63 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using bts.api;
using System.IO;
using mbts.ui.shell;
namespace mbts.spark
{
public class DataPersistor
{
private List<IDisposable> _disp_list = new List<IDisposable>();
private readonly IBus _bus;
private readonly string _loc = @"C:\ProgramData\bts\temp_securities.bin"; /// TODO
private SparkBusData.PersistedItems _persisted_items = new SparkBusData.PersistedItems();
public DataPersistor(IBus bus)
{
_bus = bus;
var sub = _bus.subscribe<SparkBusData.PersistMessage>(SparkTags.PersistCommandTag);
_disp_list.Add(sub.Subscribe(p => _on_new_persist_message(p)));
_persisted_items = _read_data_from_file();
_bus.post(SparkTags.PersistedItemsTag, _persisted_items);
}
private void _on_new_persist_message(SparkBusData.PersistMessage p)
{
_persisted_items.items.Add(p);
_write_to_file(_persisted_items);
_bus.post(SparkTags.PersistedItemsTag, new SparkBusData.PersistedItems(_persisted_items.items));
}
private void _write_to_file(SparkBusData.PersistedItems items)
{
byte[] data = SparkUtils.serialize_proto(items);
try
{
File.WriteAllBytes(_loc, data);
}
catch (Exception ex)
{
mbts.Logger.log(bts.log.Level.Error, bts.log.Subsystem.gen, ex.ToString());
}
}
private SparkBusData.PersistedItems _read_data_from_file()
{
var rv = new SparkBusData.PersistedItems();
try
{
if (File.Exists(_loc)) {
byte[] data = File.ReadAllBytes(_loc);
rv = SparkUtils.deserialize_proto<SparkBusData.PersistedItems>(data);
}
}
catch { }
return rv;
}
}
}
@@ -143,6 +143,8 @@ namespace mbts.spark {
);
}
public IBus bus => _bus;
public event Action on_tick;
public MDConsumerHandle md => _model.md;
@@ -483,6 +483,7 @@
Background="{x:Null}" BorderBrush="{x:Null}"
PieBackground="Transparent"
SliceBackground="#a000"
Bus="{Binding bus}"
Command="{Binding RecenterCommand, ElementName=_ladder}">
<spark:RecenterCountdown.SliceStroke>
<Pen Thickness="1" Brush="Black" />
@@ -2,80 +2,55 @@
using mbts.co2;
using mbts.ui.controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reactive.Linq;
using System.Windows;
using System.Windows.Threading;
namespace mbts.spark {
// Implement the recenter logic in a UI control
// Kind of a hack but it makes receiving input from the UI easier
public class RecenterCountdown : CountDownTimer {
DispatcherTimer _timer = new DispatcherTimer(DispatcherPriority.Background) {
Interval = TimeSpan.FromSeconds(1)
};
SecurityImmutable? _security;
MDConsumerHandle _handle;
DateTime _last_recenter = DateTime.MaxValue;
double _last_mid = 0.0;
IDisposable _dispose;
readonly TimeSpan _seconds_to_center;
IBus _bus;
public RecenterCountdown() {
DataContextChanged += RecenterCountdown_DataContextChanged;
Unloaded += (o,e) => on_unload(false);
_timer.Tick += _timer_Tick;
_timer.Start();
_seconds_to_center = TimeSpan.FromSeconds(config.Configuration.get<bts.config.LadderKnobs>().seconds_to_recenter);
Unloaded += (o, e) => on_unload(false);
}
static (double,double) tick_range(double price, int tick_count, ContractSpecificationImmutable spec) {
return (TickUtils.tickdown(spec, price, tick_count), TickUtils.tickup(spec, price, tick_count));
public IBus Bus {
get { return (IBus)GetValue(busProperty); }
set { SetValue(busProperty, value); }
}
private void _timer_Tick(object sender, EventArgs e) {
if (_security != null) {
var mid = _handle.mid();
if (//mid < low || mid > high ||
(_last_recenter + _seconds_to_center < DateTime.UtcNow)) {
trigger_recenter(mid);
}
public static readonly DependencyProperty busProperty =
DependencyProperty.Register("Bus", typeof(IBus), typeof(RecenterCountdown), new PropertyMetadata(null, bus_changed));
static void bus_changed(object sender, DependencyPropertyChangedEventArgs args) {
if (sender is RecenterCountdown self) {
self.setbus(args.NewValue as IBus);
}
}
void setbus(IBus value) {
if (value != null) {
Disposable.dispose(ref _dispose);
_bus = value;
_dispose =
Disposable.Create(
_bus?.subscribe(SparkTags.RecenterLadderBegin).OnDispatcher().Subscribe(begin_recenter),
_bus?.subscribe(SparkTags.RecenterLadderNow).OnDispatcher().Subscribe(_ => base.OnClick())
);
}
}
void begin_recenter(DateTime _) => start(null);
private void on_unload(bool _) {
Disposable.dispose(ref _dispose);
_timer.Stop();
}
private void RecenterCountdown_DataContextChanged(object sender, System.Windows.DependencyPropertyChangedEventArgs e) {
Disposable.dispose(ref _dispose);
var vm = e.NewValue as LadderVM;
if (vm != null) {
_security = vm.security;
_handle = vm.md;
reset_counters();
vm.OnDisposed += on_unload;
_dispose =
Disposable.Create(
Disposable.Create(() => vm.OnDisposed -= on_unload));
}
}
void trigger_recenter(double mid) {
reset_counters();
start(null);
}
protected override void OnClick() {
reset_counters();
base.OnClick();
}
void reset_counters() {
_last_recenter = DateTime.UtcNow;
_last_mid = _handle.mid();
_bus?.post(SparkTags.RequestRecenterLadder, null);
}
}
}