Compare commits

..
Author SHA1 Message Date
John Colvin da1c35bb85 fill simulator (a work in progress) 2020-04-09 13:18:28 -05:00
26 changed files with 757 additions and 138 deletions

No files matched your search

+1 -10
View File
@@ -115,7 +115,7 @@ namespace mbts.ui.shell
{
try
{
string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\BTS\\stunnel\\";
var path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\BTS\\stunnel\\";
if (File.Exists(path + "config\\stunnel.conf"))
{
uint job_error = BTSUINativeMethods.setup_process_job_object();
@@ -124,15 +124,6 @@ namespace mbts.ui.shell
kill_stunnel();
start_stunnel(path, job_error);
}
path = AppDomain.CurrentDomain.BaseDirectory + "\\..\\stunnel\\";
if (File.Exists(path + "config\\stunnel.conf"))
{
uint job_error = BTSUINativeMethods.setup_process_job_object();
//if we can't put this process and stunnel into a job, try to stop stunnel then restart
if (job_error != 0)
kill_stunnel();
start_stunnel(path, job_error);
}
}
catch(Exception ex)
{
@@ -534,7 +534,8 @@ namespace mbts.spark {
_size_from_row(row, _price_list, _asks, _vm.security.ContractSpec);
uint _volume_from_row(int row) {
if (_price_list.SafeTryGetAt(row, out var price)) {
if (_price_list != null) {
var price = _price_list[row];
var vapls = _volume_container.read_new_value();
if (vapls != null) {
return vapls.SelectFirstOrDefault(v => MathUtils.epsilon_equals(v.Price, price), v => v.Volume);
@@ -2,7 +2,6 @@ using bts.api;
using bts.api.core;
using bts.api.position;
using bts.collections;
using bts.utils;
using mbts;
using mbts.spark;
using mbts.ui;
+5 -18
View File
@@ -2,7 +2,6 @@
using bts.api;
using bts.shell;
using bts.spark;
using bts.utils;
using mbts.config;
using mbts.persist;
using mbts.spark;
@@ -115,7 +114,6 @@ namespace mbts.ui {
void dispose_core() {
Disposable.dispose(ref _settings_sub);
Disposable.dispose(ref _toast_controller);
Disposable.dispose(ref _sound_player);
if (_dispatcher != null && !_dispatcher.HasShutdownStarted) {
_dispatcher.BeginInvokeShutdown(DispatcherPriority.Background);
}
@@ -160,25 +158,15 @@ namespace mbts.ui {
}
}
class SoundNotificationPlayer : IDisposable {
IReadOnlyDictionary<SoundNotification.SystemSound, NAudioPlayer> _players;
class SoundNotificationPlayer {
readonly IReadOnlyDictionary<SoundNotification.SystemSound, NAudioPlayer> _players;
public static SoundNotificationPlayer create() => new SoundNotificationPlayer(_all_players());
SoundNotificationPlayer(IReadOnlyDictionary<SoundNotification.SystemSound, NAudioPlayer> players) =>
_players = players;
public static SoundNotificationPlayer create() =>
new SoundNotificationPlayer(_all_players());
public void Dispose() {
if (_players != null) {
_players.Values.DisposeAll();
_players = null;
}
}
public void play_sounds(IEnumerable<SoundNotification> sounds) {
if (_players == null)
return;
foreach (var s in sounds) {
switch (s.sound_type_case) {
case SoundNotification.SoundTypeOneofCase.System: _players.GetValueOrDefault(s.system_sound.Value)?.play(); break;
@@ -189,8 +177,7 @@ namespace mbts.ui {
}
static IReadOnlyDictionary<SoundNotification.SystemSound, NAudioPlayer> _all_players() =>
EnumHelper.get_values<SoundNotification.SystemSound>()
.ToReadOnlyDictionaryWhere<SoundNotification.SystemSound, NAudioPlayer>(NAudioPlayer.try_create);
EnumHelper.get_values<SoundNotification.SystemSound>().ToReadOnlyDictionary(s => s, NAudioPlayer.create);
}
// This class adopts to role of view-controller in the mvc paradigm, rather than view-model in mvvm.
+22 -75
View File
@@ -1,5 +1,4 @@
using bts;
using bts.shell;
using bts.shell;
using mbts.ui.utility;
using NAudio.Wave;
using System;
@@ -7,71 +6,29 @@ using System.IO;
using System.Threading.Tasks;
namespace mbts.ui {
class NAudioPlayer : IDisposable {
class NAudioPlayer {
// number of milliseconds to delay playing sound,
// during which time all subsequent requests to play the same sound will be ignored.
const int _delay_ms = 75;
readonly int _start_position;
IDisposable _stream_in;
WaveOutEvent _wave_out = new WaveOutEvent();
WaveFileReader _wave_reader;
bool _is_playing, _is_play_requested;
/// <paramref name="stream_in"/> should not be disposed by the caller,
/// as NAudioPlayer handles that in its own Dispose method;
/// this constructor is private is to prevent such misuse.
NAudioPlayer(Stream stream_in, int start_position = 0) {
_stream_in = stream_in;
_wave_reader = new WaveFileReader(stream_in);
public NAudioPlayer(Stream uwave_stream, int start_position = 0) {
var ms = new MemoryStream();
uwave_stream.CopyTo(ms);
ms.Position = 0;
_wave_reader = new WaveFileReader(ms);
_wave_reader.Position = _start_position = (start_position < _wave_reader.Length ? start_position : 0);
_wave_out.Init(_wave_reader);
_wave_out.PlaybackStopped += _on_playback_stopped;
}
/// <see cref="_playback_stopped"/> is a proxy/buffer for the _wave_out.PlaybackStopped event.
/// It serves a couple purposes:
/// 1) The main callback (<see cref="_on_playback_stopped(object, StoppedEventArgs)"/>)
/// is guarenteed to be executed first (though it would be anyways, since that
/// callback is hooked up in the constructor, the proxy event makes it obvious).
/// 2) <see cref="_playback_stopped"/> is only raised after playback is truly complete,
/// meaning there are no pending requests to repeat the sound.
event Action _playback_stopped;
public static bool try_create(SoundNotification.SystemSound system_sound, out NAudioPlayer result) {
if (_try_get_stream(system_sound, out var stream)) {
var sb = EnumHelper.get_attribute<SoundNotification.SystemSound, SilenceBufferAttribute>(system_sound);
result = sb != null ? new NAudioPlayer(stream, sb.start_position) : new NAudioPlayer(stream);
return true;
}
result = null;
return false;
}
public static void play(SoundNotification.SystemSound system_sound) {
if (try_create(system_sound, out var player)) {
try {
var temp = player;
temp._playback_stopped += temp.Dispose;
player.play();
player = null;
} catch (Exception ex) {
Log.Ui.warn($"Encountered exception playing the system sound '{system_sound}': {ex}.");
} finally {
player?.Dispose();
}
} else {
Log.Ui.warn($"Unable to play the system sound '{system_sound}': unexpected sound type (did not find an audio stream that corresponds to this enum value).");
}
}
public void Dispose() {
if (_wave_out != null) {
_wave_out.PlaybackStopped -= _on_playback_stopped;
_wave_out.Dispose();
_wave_out = null;
}
Disposable.dispose(ref _wave_reader);
Disposable.dispose(ref _stream_in);
public static NAudioPlayer create(SoundNotification.SystemSound system_sound) {
var stream = _get_stream(system_sound);
var sb = EnumHelper.get_attribute<SoundNotification.SystemSound, SilenceBufferAttribute>(system_sound);
return sb != null ? new NAudioPlayer(stream, sb.start_position) : new NAudioPlayer(stream);
}
public void play() {
@@ -89,8 +46,6 @@ namespace mbts.ui {
if (_is_play_requested) {
_is_play_requested = false;
_play_core();
} else {
_playback_stopped?.Invoke();
}
}
@@ -101,26 +56,18 @@ namespace mbts.ui {
_is_playing = false;
}
static bool _try_get_stream(SoundNotification.SystemSound system_sound, out Stream stream) {
UnmanagedMemoryStream um_stream;
static Stream _get_stream(SoundNotification.SystemSound system_sound) {
switch (system_sound) {
case SoundNotification.SystemSound.GameCorrect: um_stream = bts.Properties.Resources.game_sound_correct; break;
case SoundNotification.SystemSound.GameIncorrect: um_stream = bts.Properties.Resources.game_sound_wrong; break;
case SoundNotification.SystemSound.GameOverArcade: um_stream = bts.Properties.Resources.game_over_arcade; break;
case SoundNotification.SystemSound.Whistle: um_stream = bts.Properties.Resources.whistle; break;
case SoundNotification.SystemSound.BellCounter: um_stream = bts.Properties.Resources.bell_counter; break;
case SoundNotification.SystemSound.Powerup: um_stream = bts.Properties.Resources.powerup; break;
case SoundNotification.SystemSound.CashRegister: um_stream = bts.Properties.Resources.cash_register_purchase; break;
case SoundNotification.SystemSound.Reload: um_stream = bts.Properties.Resources.reload_1911; break;
default:
stream = null;
return false;
case SoundNotification.SystemSound.GameCorrect: return bts.Properties.Resources.game_sound_correct;
case SoundNotification.SystemSound.GameIncorrect: return bts.Properties.Resources.game_sound_wrong;
case SoundNotification.SystemSound.GameOverArcade: return bts.Properties.Resources.game_over_arcade;
case SoundNotification.SystemSound.Whistle: return bts.Properties.Resources.whistle;
case SoundNotification.SystemSound.BellCounter: return bts.Properties.Resources.bell_counter;
case SoundNotification.SystemSound.Powerup: return bts.Properties.Resources.powerup;
case SoundNotification.SystemSound.CashRegister: return bts.Properties.Resources.cash_register_purchase;
case SoundNotification.SystemSound.Reload: return bts.Properties.Resources.reload_1911;
default: throw new NotImplementedException($"Unknown {nameof(SoundNotification.SystemSound)}: {system_sound}.");
}
stream = new MemoryStream();
um_stream.CopyTo(stream);
um_stream.Dispose();
stream.Position = 0;
return true;
}
}
}
}
+2 -5
View File
@@ -215,11 +215,8 @@ namespace mbts.spark {
FillTuple.key_fn);
on_fill_tuples_with_root = fill_order_tuples_with_root_join.Cache();
// manual fills
on_manual_fills_securities = admin_model
.permission_filter(
_position_service.subscribei_manual_fills()
.Naggle(interval, scheduler),
f => f.AccountId)
on_manual_fills_securities = _position_service.subscribei_manual_fills()
.Naggle(interval, scheduler)
.Filter(f => f.FillType == ManualFill.Types.FillType.Local)
.join(sstore, mf => mf.SecurityKey)
.Table().Cache();
@@ -1,11 +1,11 @@
using System;
using System.Globalization;
namespace bts.utils {
public delegate bool TryGetValue<T>(out T value);
public delegate bool TryGetValue<TKey, TValue>(TKey key, out TValue value);
namespace mbts.utils {
delegate bool TryGetValue<T>(out T value);
delegate bool TryGetValue<TKey, TValue>(TKey key, out TValue value);
public static class LogicalDelegates {
static class LogicalDelegates {
public static readonly Func<bool, bool, bool>
And = (a, b) => a && b,
Or = (a, b) => a || b,
@@ -13,7 +13,7 @@ namespace bts.utils {
None = (a, b) => !a && !b;
}
public static class FormattingDelegates {
static class FormattingDelegates {
public static class Price {
public static readonly Func<double, string> Value =
p => double.IsNaN(p) ? "" : p.ToString("#,0.00");
@@ -26,4 +26,4 @@ namespace bts.utils {
p => p.ToString("#,0.00", CultureInfo.CurrentCulture);
}
}
}
}
@@ -38,7 +38,11 @@ namespace mbts.ui.controls {
}
public static ICommand PreviewSoundCommand =>
new DelegateCommand<SoundNotification.SystemSound>(NAudioPlayer.play);
new DelegateCommand<SoundNotification.SystemSound>(s => {
try {
NAudioPlayer.create(s).play();
} catch (NAudio.MmException) { }
});
#region SoundTypes
public static readonly DependencyProperty SoundTypesProperty =
@@ -418,15 +418,6 @@ namespace System.Collections.Generic {
public static ReadOnlyDictionary<TKey, TElement> ToReadOnlyDictionary<T, TKey, TElement>(this IEnumerable<T> enumerable, Func<T, TKey> key_selector, Func<T, TElement> element_selector, IEqualityComparer<TKey> comparer) =>
new ReadOnlyDictionary<TKey, TElement>(enumerable.ToDictionary(key_selector, element_selector, comparer));
public static ReadOnlyDictionary<TKey, TElement> ToReadOnlyDictionaryWhere<TKey, TElement>(this IEnumerable<TKey> keys, TryGetValue<TKey, TElement> value_selector) {
var mdict = new Dictionary<TKey, TElement>();
foreach (var k in keys) {
if (value_selector(k, out var val))
mdict[k] = val;
}
return new ReadOnlyDictionary<TKey, TElement>(mdict);
}
public static ILookup<TKey, TElement> ToFlatLookup<T, TKey, TElement>(this IEnumerable<T> source, Func<T, TKey> key_selector, Func<T, IEnumerable<TElement>> selector) =>
source.SelectMany(v => {
var k = key_selector(v);
@@ -103,15 +103,6 @@ namespace bts.collections {
return array;
}
public static bool SafeTryGetAt<T>(this IList<T> list, int index, out T value) {
if (list != null && index >= 0 && index < list.Count) {
value = list[index];
return true;
}
value = default;
return false;
}
public static bool TryGetAt<T>(this IList<T> list, int index, out T value) {
if (index >= 0 && index < list.Count) {
value = list[index];
Binary file not shown.
+6 -3
View File
@@ -1,10 +1,13 @@
#nusers: 1
#allow concurrent : true
#expires: 2021-Jan-01 00:00:00 637450560000000000
#generation_time: 2020-Apr-03 18:39:31.010863 5248901378137496534
#expires: 2020-Mar-31 00:00:00 637212096000000000
#generation_time: 2020-Jan-06 18:56:23.406931 5248825356261457214
EncryptedKeyCode {
value {
keycode: "BJBQWsbdTXWa/iLmOf3ekE8vP/NYkf0Q/nUsp/rJ78Ze3W9pNXICCMabmmsr1xOM7aqRIclPgZd955uSSfvFkdNWFTxwPQ5IfnDGopj3QdhUVxhzgVLaXHN+vC0H/MWrDQ5K4WY5J/6VXgUn1LSN/2HNb5iR2sKXeh3Alamg87YtCWEi+scelffE7HK5ERT/3A3Y/e0pqUFhP6xouIjHRhC2FRBb6OoJQVvFwVDADSuKZNo9B6GhC3H0YdHqjFhGDoky5ecgzeKIX8uWJEQNnQXQJ0iTgQ95+YR5IMU+XWdlVfSrc0fuTmj3YWcBQ8Kl2ItEcobyZzJYega3tu6Ftw=="
keycode: "Ixb/z6hwFQmojpm4GwNEpQmsqpYT9OTHbcKtROIqj38GwGqyVn54JMDB04V048quO+NAI78DKJXsUWgjDH0y5HQlwN6Lq48s/bdwKqR1OTBPr6xoTImtCb+l5b+8fUGlg7t3X8gfMdxohHgcUi9k9n7ZrnNvqcjdYKoP0CaTZT8zHuBQx3f4Lqrz32gq9rJ5skRjH6ZqNblNdlsF+kOAqXCkvI5p0QfyKgtCodr33qsW2cbqY5gUD9KwsnmFVa8/zuFBucsoM7dHB47yRaIeOFFu7xGayAdAHA8SwLyoxE5y+adIX1SYnAiqAu1raZpeXgRE1LNQk3c7uhUjt0coag=="
}
}
@@ -0,0 +1,70 @@
#include "BookUtils.h"
namespace bts {
namespace orderman {
// todo: use existing price epsilon (if any), or verify this is appropriate
const double price_epsilon = 1e-8;
Size BookUtils::volume_at_or_better(const Book &book, Side side, Price price) {
Size volume = 0;
for (const auto &plevel : BookSideImpl<side>::get_depth(book)) {
if (BookSideImpl<side>::(price, plevel.price()))
break;
volume += plevel.size();
}
return volume;
}
const BookSide &BookUtils::get_depth(const Book &book, Side side) {
return BookSideImpl<side>::get_depth(side);
}
const BookSide &BookUtils::get_opposite_depth(const Book &book, Side side) {
return BookSideImpl<side>::get_opposite_depth(book);
}
md::PriceSize BookUtils::get_top(const Book &book, Side side) {
return *BookSideImpl<side>::get_depth(book).begin();
}
md::PriceSize BookUtils::get_opposite_top(const Book &book, Side side) {
return *BookSideImpl<side>::get_opposite_depth(book).begin();
}
bool BookUtils::is_price_better(Side side, double price, double comparand) {
return BookSideImpl<side>::(price, comparand);
}
bool BookUtils::is_price_worse(Side side, double price, double comparand) {
return BookSideImpl<side>::(comparand, price);
}
bool BookUtils::is_top_better(Side side, double price) {
auto top = BookUtils::get_top(side);
return !top.is_nan() && BookUtils::is_price_better(side, price, top.price());
}
bool BookUtils::is_top_better_or_equal(Side side, double price) {
auto top = BookUtils::get_top(side);
return !top.is_nan() &&
(((top - price) < epsilon && (top - price) > -epsilon) ||
BookUtils::is_price_better(side, price, top.price()));
}
bool BookUtils::is_top_worse(Side side, double price) {
auto top = BookUtils::get_top(side);
return !top.is_nan() && BookUtils::is_price_worse(side, price, top.price());
}
bool BookUtils::is_top_worse_or_equal(Side side, double price) {
auto top = BookUtils::get_top(side);
return !top.is_nan() &&
(((top - price) < epsilon && (top - price) > -epsilon) ||
BookUtils::is_price_worse(side, price, top.price()));
}
Side BookUtils::to_opposite_side(Side side) {
return BookSideImpl<side>::to_opposite_side();
}
} // namespace orderman
} // namespace bts
@@ -0,0 +1,89 @@
#pragma once
#include "Book.h"
#include "lib/api/services/OrderService.pb.h"
#include "lib/config/protos/MarketData.pb.h"
#include "lib/domain_model/common.h"
#include "lib/md/PriceSize.h"
#include <memory>
namespace bts {
namespace orderman {
using Size = bts::md::Size;
using Side = bts::api::Side;
struct BookUtils {
static Size volume_at_or_better(const Book &book, Side side, Price price);
static const BookSide &get_depth(const Book &book, Side side);
static const BookSide &get_opposite_depth(const Book &book, Side side);
static md::PriceSize get_top(const Book &book, Side side);
static md::PriceSize get_opposite_top(const Book &book, Side side);
static bool is_price_better(Side side, double price, double comparand);
static bool is_price_worse(Side side, double price, double comparand);
static bool BookUtils::is_top_better(Side side, double price);
static bool BookUtils::is_top_better_or_equal(Side side, double price);
static bool BookUtils::is_top_worse(Side side, double price);
static bool BookUtils::is_top_worse_or_equal(Side side, double price);
static Side to_opposite_side(Side side);
private:
// put side-dependent logic in the SIDE_BUY/SIDE_ASK impls
template <bts::api::Side side> struct BookSideImpl {
static inline BookSide get_depth(const Book &book) {
// todo: format message
bts_assert(false, "Unexpected side argument '{}' (not buy or sell)");
}
static inline const BookSide &get_opposite_depth(const Book &book) {
// todo: format message
bts_assert(false, "Unexpected side argument '{}' (not buy or sell)");
}
bool operator()(const Price &lhs, const Price &rhs) {
// since this is just a comparison operator,
// it's probably okay to use the default comparison
// rather than assert
return lhs.price() > rhs.price();
}
bool operator()(const double &lhs, const double &rhs) {
// since this is just a comparison operator,
// it's probably okay to use the default comparison
// rather than assert
return lhs > rhs;
}
};
template <> struct BookSideImpl<bts::api::SIDE_BUY> {
static inline const BookSide &get_depth(const Book &book) {
return book.bid();
}
static inline const BookSide &get_opposite_depth(const Book &book) {
return book.ask();
}
static inline std::string name() { return "Buy"; }
static inline Side to_opposite_side() { return bts::api::SIDE_SELL; }
bool operator()(const Price &lhs, const Price &rhs) {
return lhs.price() > rhs.price();
}
bool operator()(const double &lhs, const double &rhs) {
return lhs > rhs;
}
};
template <> struct BookSideImpl<bts::api::SIDE_SELL> {
static inline const BookSide &get_depth(const Book &book) {
return book.ask();
}
static inline const BookSide &get_opposite_depth(const Book &book) {
return book.bid();
}
static inline Side to_opposite_side() { return bts::api::SIDE_BUY; }
static inline std::string name() { return "Sell"; }
bool operator()(const Price &lhs, const Price &rhs) {
return lhs.price() < rhs.price();
}
bool operator()(const double &lhs, const double &rhs) {
return lhs < rhs;
}
};
};
} // namespace orderman
} // namespace bts
+63
View File
@@ -0,0 +1,63 @@
#pragma once
#include "lib/utils/TimeStamp.h"
#include "server/sparkd/fill_models/OrderStatus.h"
#include "server/sparkd/fill_models/ExchangeOrderSnapshot.h"
namespace bts {
namespace md {
template <bts::api::Side side> struct PriceComp {
bool operator()(const double &lhs, const double &rhs) {
bts_assert(false);
}
};
template <> struct PriceComp<bts::api::SIDE_BUY> {
bool operator()(const double &lhs, const double &rhs) {
return lhs > rhs;
}
};
template <> struct PriceComp<bts::api::SIDE_SELL> {
bool operator()(const double &lhs, const double &rhs) {
return lhs < rhs;
}
};
struct TransactionTimeCmp {
// bool operator()(const OrderStatus& lhs, const OrderStatus& rhs) {
// return lhs.transaction_time > rhs.transaction_time;
// }
// bool cmp(const std::pair<const bts::utils::TimeStamp, bts::md::MBOUpdate>&
// , const bts::utils::TimeStamp& )
//{
// return true;
//}
bool comp_member(bts::utils::TimeStamp, bts::utils::TimeStamp) {
return true;
}
bool operator()(const bts::utils::TimeStamp &lhs,
const bts::utils::TimeStamp &rhs) {
return lhs.to_uint64() > rhs.to_uint64();
}
bool operator()(const ExchangeOrderSnapshot &lhs, const ExchangeOrderSnapshot &rhs) {
return lhs.transaction_time.to_uint64() > rhs.transaction_time.to_uint64();
}
bool operator()(const OrderStatus &lhs, const OrderStatus &rhs) {
return lhs.transaction_time > rhs.transaction_time;
}
bool operator()(const google::protobuf::Timestamp &lhs,
const google::protobuf::Timestamp &rhs) {
if (lhs.seconds() > rhs.seconds()) {
return true;
} else if (lhs.seconds() < rhs.seconds()) {
return false;
} else {
return lhs.nanos() > rhs.nanos();
}
}
};
} // namespace md
} // namespace bts
@@ -0,0 +1,18 @@
#pragma once
#include "lib/md/MBOProcessor.h"
#include "lib/utils/TimeStamp.h"
namespace bts {
namespace md {
struct ExchangeOrderSnapshot {
SnapshotOrder *state;
bts::utils::TimeStamp transaction_time;
ExchangeOrderSnapshot() {}
ExchangeOrderSnapshot(SnapshotOrder *state, TimeStamp transaction_time)
: state(state), transaction_time(transaction_time) {}
};
} // namespace md
} // namespace bts
@@ -0,0 +1,148 @@
#include "FIFOFillModel.h"
#include "lib/config/config.h"
#include <atomic>
#include <functional>
#include <map>
#include <sstream>
#include <thread>
#include <vector>
namespace bts {
namespace md {
void FIFOFillModel::BTSOrderCache::on_order(const OrderStatus& order) {
auto id_it = by_id.find(order.id);
if (id_it == by_id.end()) {
auto pair = std::make_pair(order.id, BTSOrderHistory());
pair.second.insert(order.transaction_time, order);
by_id.insert(pair);
_compute_fills(order);
} else {
auto pos_in_hist = id_it->second.lower_bound(order.transaction_time);
if (pos_in_hist == id_it->second.end())
_compute_fills(order);
id_it->second.insert_or_assign(order.transaction_time, order);
}
by_time.insert(order.transaction_time, order);
}
void FIFOFillModel::on_mbo(SecurityKey id, TimeStamp transaction_time, const MBOSnapshot &snapshot) {
for (auto o : snapshot.bids) {
auto it = _exch_orders.find(o.id);
ExchOrderHistory &hist;
if (it == _exch_orders.end()) {
auto pair = std::make_pair(o.id, ExchOrderHistory());
hist = pair.second;
_exch_orders.insert(pair);
} else {
hist = it->second;
}
hist.insert_or_assign(transaction_time, ExchangeOrderSnapshot(&o, transaction_time));
}
// flush crosses
_compute_fills();
}
void FIFOFillModel::on_exch_order(const ExchangeOrderSnapshot &order) {}
void FIFOFillModel::on_order(const OrderStatus &order) {
_bts_orders.on_order(order);
}
void FIFOFillModel::on_trade(TimeStamp transaction_time, IntPrice price,
co2::AggressorSide aggressor_side,
std::vector<TradeDetail> trades) {
bts_assert(!price.is_nan());
std::unordered_set<ExchangeOrderKey> processed;
for (auto trade : trades) {
bts_assert(trade.last_qty > 0);
Size rem_trade_qty = trade.last_qty;
ExchOrderHistory exch_order_hist;
if (find_exch_order(trade.order_id, exch_order_hist)) {
auto bts_order_it = _bts_orders.by_time.lower_bound(transaction_time);
bts_order_it != _bts_orders.by_time.end();
while (bts_order_it != _bts_orders.by_time.end()) {
auto bts_order = bts_order_it->second;
if (!bts_order.is_active) {
_terminal_orders.insert(
std::make_pair(bts_order.transaction_time, bts_order));
} else if (bts_order.transaction_time > transaction_time ||
!FillModelUtils::can_fill_at_price(bts_order,
price.price()) ||
is_terminal(bts_order) ||
processed.find(bts_order.id) != processed.end()) {
//
bts_order_it++;
} else if (FillModelUtils::can_fill_at_price(bts_order,
price.price())) {
processed.insert(bts_order.id);
Size fill_qty =
std::min(rem_trade_qty, bts_order.working_quantity());
bts_assert(fill_qty > 0);
auto fill = _realize_fill(transaction_time, bts_order,
price.price(), fill_qty);
fill.order_key = trade.order_id;
if (FillModelUtils::normalize_aggressor_side(aggressor_side) ==
bts_order.side) {
fill.flags |= FillDataFlags::SameSideAggressor;
}
rem_trade_qty -= fill_qty;
}
}
}
}
}
void FIFOFillModel::on_book(SecurityKey sec, TimeStamp transaction_time,
const Book &book) {
_books.insert_or_assign(sec, book);
_compute_fills();
}
void FIFOFillModel::_compute_fills() {
for (auto order : _bts_orders.by_time) {
_compute_fills(order.second);
}
}
void FIFOFillModel::_compute_fills(OrderStatus &order) {
if (!is_terminal(order))
_assert_compute_fills(order);
}
std::vector<FillData> FIFOFillModel::_assert_compute_fills(OrderStatus &order) {
std::vector<FillData> fills;
auto book = _get_book(order.security);
auto depth = bts::orderman::BookUtils::get_opposite_depth(book);
for (int level = 0;
!order.is_filled() && level < depth.size() &&
FillModelUtils::can_fill_at_price(order, depth[level].price.value());
level++) {
auto ps = depth[level];
auto fill_qty =
std::max(static_cast<int>(order.working_quantity()), ps.size());
bts_assert(fill_qty > 0);
fills.push_back(_realize_fill(order.transaction_time, order, ps.price(), fill_qty);
}
return fills;
}
FillData FIFOFillModel::_realize_fill(bts::utils::TimeStamp transaction_time,
OrderStatus &order, double fill_price,
Size fill_qty) {
// todo
return FillData();
}
bool FIFOFillModel::find_exch_order(ExchangeOrderKey key,
ExchOrderHistory &history) {
// todo
return true;
}
bool FIFOFillModel::is_terminal(const OrderStatus &order) {
return !order.is_active ||
_terminal_orders.find(order.id) != _terminal_orders.end();
}
} // namespace md
} // namespace bts
@@ -0,0 +1,57 @@
#pragma once
#include <stdlib.h>
#include "server/sparkd/fill_models/IFillModel.h"
#include "server/sparkd/fill_models/Comparers.h"
#include "lib/md/ExchangeOrderKey.h"
#include "lib/md/MBOProcessor.h"
#include "lib/domain_model/common.h"
#include "lib/md/IntPrice.h"
#include "server/sparkd/fill_models/FillData.h"
#include "lib/orderman/orderman_utils/BookUtils.h"
#include "server/sparkd/fill_models/FillModelUtils.h"
#include <unordered_map>
#include <map>
#include <set>
#include <unordered_set>
#include "server/sparkd/exchange_sim/book/OrderMatcher.h"
using Book = bts::orderman::Book;
using TimeStamp = bts::utils::TimeStamp;
using ExchOrderHistory = std::map<TimeStamp, bts::md::ExchangeOrderSnapshot>;
using BTSOrderHistory = std::map<TimeStamp, bts::md::OrderStatus>;
namespace bts {
namespace md {
struct FIFOFillModel : public IFillModel {
void on_order(const OrderStatus &order) override;
void on_book(SecurityKey, TimeStamp, const Book&) override;
void on_mbo(SecurityKey, TimeStamp, const MBOSnapshot &) override;
void on_exch_order(const ExchangeOrderSnapshot &update) override;
void on_trade(TimeStamp transaction_time, IntPrice price, co2::AggressorSide aggressor_side, std::vector<TradeDetail> real_trades) override;
// keeps a cache of order history per id
// and a flat history of all orders
struct BTSOrderCache {
std::unordered_map<ExchangeOrderKey, BTSOrderHistory> by_id;
std::multimap<TimeStamp, OrderStatus> by_time;
void on_order(const OrderStatus &order);
};
private:
std::map<SecurityKey, Book> _books;
std::map<ExchangeOrderKey, ExchOrderHistory> _exch_orders;
BTSOrderCache _bts_orders;
std::unordered_set<ExchangeOrderKey> _terminal_orders;
FillData _realize_fill(TimeStamp transaction_time,
OrderStatus &order, double fill_price, Size fill_qty);
// computes fills for all working/non-terminal orders
void _compute_fills();
// checks if the order is stil working/non terminal
// before calling _compute_fills_for (this call should never assert)
void _compute_fills(OrderStatus &order);
// asserts that the order is still working/non-terminal
std::vector<FillData> _assert_compute_fills(OrderStatus& order);
bool find_exch_order(ExchangeOrderKey key, ExchOrderHistory &history);
bool is_terminal(const OrderStatus &order);
};
} // namespace md
} // namespace bts
+24
View File
@@ -0,0 +1,24 @@
#pragma once
#include <stdlib.h>
namespace bts {
namespace md {
enum FillDataFlags {
None = 0,
SameSideAggressor = 1
};
struct FillData {
uint32_t working_qty;
uint32_t filled_qty;
ExchangeOrderKey order_key;
FillDataFlags flags;
FillData(){}
FillData(uint64_t working_qty, uint64_t filled_qty)
: working_qty(working_qty), filled_qty(filled_qty) {}
FillData(uint64_t working_qty, uint64_t filled_qty, ExchangeOrderKey order_key, FillDataFlags flags)
: working_qty(working_qty), filled_qty(filled_qty), order_key(order_key), flags(flags) {}
};
} // namespace md
} // namespace bts
@@ -0,0 +1,16 @@
#include "FillModelUtils.h"
namespace bts {
namespace md {
bool FillModelUtils::can_fill_at_price(const OrderStatus &order,
double price) {
return !os::is_nan(price) && (order.is_market_order ||
!bts::orderman::BookUtils::is_price_better(order.side, price, order.price.price()));
}
Side FillModelUtils::normalize_aggressor_side(
co2::AggressorSide aggressor_side) {
// todo
return bts::api::SIDE_SELL;
}
} // namespace md
} // namespace bts
@@ -0,0 +1,29 @@
#pragma once
#include "lib/md/protos/Co2.pb.h"
#include "lib/orderman/orderman_utils/Book.h"
#include "lib/orderman/orderman_utils/BookUtils.h"
#include "server/sparkd/fill_models/OrderStatus.h"
#include "lib/api/services/OrderService.pb.h"
#include "lib/config/protos/MarketData.pb.h"
#include "lib/domain_model/common.h"
#include "lib/md/PriceSize.h"
#include "server/sparkd/fill_models/Comparers.h"
#include <memory>
using bts::orderman;
using Book = bts::orderman::Book;
namespace bts {
namespace md {
using Size = bts::md::Size;
using Side = bts::api::Side;
struct FillModelUtils {
static bool can_fill_at_price(const OrderStatus& order, double price);
static Side normalize_aggressor_side(co2::AggressorSide aggressor_side);
//static bool can_fill_at_price(bts::api::Side order_side, double order_price, double fill_price);
};
} // namespace orderman
} // namespace bts
@@ -0,0 +1,38 @@
#pragma once
#include "TradeDetail.h"
#include "lib/md/IntPrice.h"
#include "lib/md/protos/Co2.pb.h"
#include "server/sparkd/fill_models/OrderStatus.h"
#include <stdlib.h>
//#include "lib/domain_model/Security.pb.h"
#include "lib/md/MBOProcessor.h"
#include "lib/utils/PBUtils.h"
#include "lib/utils/TimeStamp.h"
#include <google/protobuf/timestamp.pb.h>
//#include "lib/api/services/OrderService.pb.h"
//#include "lib/orderman/orders/Order.h"
//#include "server/sparkd/exchange_sim/book/Order.h"
// using bts::ex_sim::Order = OrderStatus;
// using Side = bts::api::Side;
namespace bts {
namespace md {
using Timestamp = bts::utils::TimeStamp;
using TimeStamp = bts::utils::TimeStamp;
struct PriceCmp;
struct TransactionTimeCmp;
class IFillModel {
public:
virtual void on_mbo(SecurityKey, TimeStamp, const MBOSnapshot &) {}
virtual void on_book(SecurityKey, TimeStamp, const Book &) {}
virtual void on_exch_order(const ExchangeOrderSnapshot &update) {}
virtual void on_order(const OrderStatus &) {}
virtual void on_trade(TimeStamp transaction_time, IntPrice price,
co2::AggressorSide aggressor_side,
std::vector<TradeDetail> real_trades) {}
};
} // namespace md
} // namespace bts
+46
View File
@@ -0,0 +1,46 @@
#include "server/sparkd/fill_models/MBOBook.h"
namespace bts {
namespace md {
//uint32_t MBOBook::_assign_fills(const OrderStatus& target_order, uint32_t filled_qty, OrderAtPriceIter order_it, OrderAtPriceIter order_end, PriceNodeIter price_node_it, PriceNodeIter price_node_end) {
//if (order_it == order_end) {
//if (price_node_it == price_node_end)
//return filled_qty;
//
//}
//int32_t working_qty = target_order.working_quantity();
//while (filled_qty < working_qty) {
//if (order_it == order_end) {
//if (++price_node_it == price_node_end)
//break;
//price_node_it->second.orders.begin();
//}
//}
//return filled_qty;
//}
void MBOBook::assign_fills(TimeStamp transaction_time, std::map<TimeStamp, OrderStatus>::iterator working_orders_it, std::map<TimeStamp, OrderStatus>::iterator working_orders_end, std::vector<std::pair<ExchangeOrderKey, FillData>>& fills_out, std::vector<TradeDetail> trades){
if (working_orders_it == working_orders_it)
return;
FillData curr_fill;
for (auto tit = trades.begin(); tit != trades.end(); tit++) {
auto exch_order_it = _cache.find(tit->order_id);
if (exch_order_it != _cache.end()) {
SnapshotOrder state;
TimeStamp exch_order_transaction_time;
if (exch_order_it->second.try_match_order(transaction_time, *state, *exch_order_transaction_time)) {
if (exch_order_transaction_time.to_uint64() > working_orders_it->first) {
uint32_t wkg_qty = working_orders_it->second.working_quantity();
}
}
}
}
}
void MBOBook::_assign_fills(const OrderStatus& target_order, const std::vector<TradeDetail>& trades, std::vector<std::pair<IntPrice, uint32_t>>& fills) {
auto exch_order_match = _cache.find(target_order.id);
exch_order_match->second.lower_bound(target_order)
if (exch_order_match == _cache.end()) return;
exch_order_match->first.value
//while (exch_order_match != _cache.begin() && exch_order_match->second.transaction_time)
}
}
} // namespace bts
+60
View File
@@ -0,0 +1,60 @@
#pragma once
#include "lib/md/ExchangeOrderKey.h"
#include "lib/md/IntPrice.h"
#include "lib/md/MBOProcessor.h"
#include "server/sparkd/fill_models/ExchangeOrderSnapshot.h"
#include "server/sparkd/fill_models/Comparers.h"
#include "server/sparkd/fill_models/FillData.h"
#include "server/sparkd/fill_models/OrderStatus.h"
#include <map>
#include <stdlib.h>
#include <unordered_map>
namespace bts {
namespace md {
using TimeStamp = bts::utils::TimeStamp;
using OrderAtPriceIter =
std::map<TimeStamp, SnapshotOrder, TransactionTimeCmp>::iterator;
// std::multiset<ExchangeOrderSnapshot, TransactionTimeCmp>::iterator;
using MarketOrderHistory = std::multiset<ExchangeOrderSnapshot, TransactionTimeCmp>;
struct MarketOrdersOfPrice {
IntPrice price;
std::map<TimeStamp, SnapshotOrder, TransactionTimeCmp> orders;
MarketOrdersOfPrice() {}
MarketOrdersOfPrice(IntPrice price) : price(price), orders() {}
MarketOrdersOfPrice(const OrdersOfPrice &other)
: price(other.price), orders(other.orders) {}
bool try_match_order(TimeStamp transaction_time, SnapshotOrder &result, TimeStamp& result_transaction_time) {
auto it = orders.lower_bound(transaction_time);
if (it == orders.begin()) {
return false;
}
do {
it--;
} while (it->first.to_uint64() > transaction_time);
result_transaction_time = it->first;
result = it->second;
return true;
}
};
using PriceNodes = std::map<IntPrice, MarketOrdersOfPrice>;
using PriceNodeIter = std::map<IntPrice, MarketOrdersOfPrice>::iterator;
struct MBOBook {
void on_mbo_update(MBOUpdate);
void assign_fills(TimeStamp transaction_time, std::vector<std::pair<ExchangeOrderKey, FillData>>& fills_out, std::vector<TradeDetail> trades);
bool try_get_history();
private:
std::unordered_map<ExchangeOrderKey, MarketOrderHistory> _cache;
//void MBOBook::_assign_fills(const OrderStatus& target_order, std::vector<std::pair<IntPrice, uint32_t>>& fills);
//uint32_t _assign_fills(const OrderStatus &target_order, uint32_t filled_qty);
};
} // namespace md
} // namespace bts
@@ -0,0 +1,31 @@
#pragma once
#include "lib/md/IntPrice.h"
#include "lib/md/ExchangeOrderKey.h"
#include <google/protobuf/timestamp.pb.h>
#include "lib/utils/PBUtils.h"
#include "lib/utils/TimeStamp.h"
namespace bts {
namespace md {
struct OrderStatus {
ExchangeOrderKey id;
SecurityKey security;
bts::utils::TimeStamp transaction_time;
IntPrice price;
int32_t quantity;
int32_t filled_quantity;
bool is_active, is_market_order;
bts::api::Side side;
OrderStatus()
: id(0), price(IntPrice(0, 0)), quantity(0),
filled_quantity(0), is_active(false), is_bid(false) {}
int32_t working_quantity() { return quantity - filled_quantity; }
bool is_filled() { return filled_quantity >= quantity; }
};
} // namespace md
} // namespace bts
@@ -0,0 +1,19 @@
#pragma once
#include "lib/md/ExchangeOrderKey.h"
namespace bts {
namespace md {
enum class AggressorSide {
Buy, Sell, None
};
struct TradeDetail {
const ExchangeOrderKey& order_id;
const uint& last_qty;
TradeDetail(ExchangeOrderKey order_id, uint last_qty)
: order_id(order_id), last_qty(last_qty) { }
};
} // namespace md
} // namespace bts