Compare commits

..
Author SHA1 Message Date
Mohammad Arham 8620839692 changes to make system not lag due to number of contracts 2024-05-29 11:17:15 -04:00
Taha Afzal 2e0e0658c6 Fixing merging 2024-05-23 10:34:40 -04:00
Taha Afzal bbdb50a41e FIX bridge for Trafix 2024-05-23 10:34:40 -04:00
John Colvin 90705cac83 Integrate theos into Algo Testing Framework
(For Laguna)
2024-05-13 14:28:16 +00:00
John Colvin b91cdd6d91 Expose get/set test secs fn for Algo Testing Framework
Laguna needs this. Historically, the test securities were extracted from
coconut md replay files. Eventually, we'll extract them from the new
replay file format, but until then, this gives Laguna a way to manually
set securities for algo tests (I hardcoded 1 ES sec for convenience by
default).
2024-05-13 14:23:31 +00:00
Mohammad Arham 64f2bd7a88 removed quoting algo so can be loaded dynamically 2024-04-04 20:38:15 -04:00
Mohammad Arham 3e32f684e3 removed redundant code 2024-04-03 16:56:31 -04:00
Mohammad Arham ff73756a55 removed assert if delete order that is completed 2024-04-03 16:55:29 -04:00
Mohammad Arham 1e64811d93 changed error code 2024-04-03 16:54:51 -04:00
12 changed files with 246 additions and 72 deletions

No files matched your search

+23 -16
View File
@@ -20,6 +20,7 @@ using System.Composition;
using System.Linq;
using System.Reactive.Linq;
using System.Threading.Tasks;
using System.Diagnostics;
using SecurityKey = System.UInt32;
@@ -112,25 +113,31 @@ namespace bts
}
private void _merge_and_post()
{
Func<SecurityMD, bool> is_active = (sec_md) =>
{
{
// Stopwatch stopwatch = Stopwatch.StartNew();
Func<SecurityMD, bool> is_active = (sec_md) =>
{
var sec = sec_md.security;
return SecurityExpirationHelper.is_unexpired(sec);
};
var to_pub = _server_sec_map.Where(kv => is_active(kv.Value)).ToDictionary(kv => kv.Key, kv => kv.Value);
foreach (var kv in _fake_sec_map)
{
};
var to_pub = _server_sec_map.Where(kv => is_active(kv.Value)).ToDictionary(kv => kv.Key, kv => kv.Value);
foreach (var kv in _fake_sec_map)
{
to_pub[kv.Key] = kv.Value;
}
bts.Log.Gen.info(String.Format("MergedMDDaemon._merge_and_post sz: {0}", to_pub.Count()));
_bus.post(SparkTags.SecuritiesTag, to_pub);
SystemContractData sc_data = new SystemContractData(to_pub);
_bus.post(SparkTags.SystemContractsTag, sc_data);
ExchangeSecurityMap exs_map = new ExchangeSecurityMap(sc_data);
// backwards compat for TC2
_bus.post(SparkTags.TC2Tags.ExchangeSecurityMapTag, exs_map);
}
}
// look here
bts.Log.Gen.info(String.Format("MergedMDDaemon._merge_and_post sz: {0}", to_pub.Count()));
_bus.post(SparkTags.SecuritiesTag, to_pub);
SystemContractData sc_data = new SystemContractData(to_pub);
// _bus.post(SparkTags.SystemContractsTag, sc_data);
ExchangeSecurityMap exs_map = new ExchangeSecurityMap(sc_data);
// backwards compat for TC2
_bus.post(SparkTags.TC2Tags.ExchangeSecurityMapTag, exs_map);
// stopwatch.Stop();
// bts.Log.Gen.info($"MergedMDDaemon._merge_and_post took {stopwatch.ElapsedMilliseconds} ms");
}
public void Dispose() =>
Disposable.dispose(ref _dispose);
@@ -30,7 +30,7 @@ namespace mbts.spark {
using System.Windows;
using System.Windows.Media;
[Export(typeof(IShellModule))]
// [Export(typeof(IShellModule))]
class AnalyticsShellModule : IShellModule, IShellModuleViewFactory {
readonly IBus _bus;
readonly IWindowManager _wm;
@@ -83,12 +83,18 @@ namespace mbts.spark {
ulong _prices_updated;
NativePriceSize[] _bids = new NativePriceSize[NativePriceHelpers.COCONUT_MAX_DEPTH], _asks = new NativePriceSize[NativePriceHelpers.COCONUT_MAX_DEPTH];
bool _price_dirty = false, _piq_dirty = false;
LadderPriceList _price_list = new() {
length = _initial_row_count,
};
LadderPriceList _price_list_backing;
LadderPriceList _price_list=>_price_list_backing ??= new LadderPriceList {
length = _initial_row_count,
security = security
};
LadderPriceList _pending_price_list = null;
IComparer<double> _price_comparer;
double _last_origin_price;
IComparer<double> _price_comparer_backing;
IComparer<double> _price_comparer => _price_comparer_backing
??= Comparer<double>.Create((x, y) => _spec.tick_equals(_normalize_price(x), _normalize_price(y)) ? 0 : y.CompareTo(x));
double _last_origin_price;
readonly MarketBreakLines _mkt_break_lines;
NativePriceSize _last_trade;
Func<NativePriceSize, string> _fmt_mkt_depth_item_impl = MDDisplayHelper.get_formatter_nps(
@@ -296,7 +302,7 @@ namespace mbts.spark {
nameof(TopTick),
FrameworkPropertyMetadataOptions.AffectsRender,
(o, v) => {
if (o._price_list is null) {
if (o._price_list_backing is null) {
return;
}
o._set_scrollbar_range(high_tick: v);
@@ -591,7 +597,7 @@ namespace mbts.spark {
busy = false;
void ensure_tick_padding() {
if (_price_list is null || _settings is null) {
if (_price_list_backing is null || _settings is null) {
return;
}
var pct = _scroll_viewer.VerticalOffset / _scroll_viewer.ScrollableHeight;
@@ -950,7 +956,6 @@ namespace mbts.spark {
// this serves as a redundancy/fail-safe; it ensures that the vm's security's
// contract spec will be used, even if the settings sequence is empty.
_invalidate_spec();
_price_comparer = Comparer<double>.Create((x, y) => _spec.tick_equals(_normalize_price(x), _normalize_price(y)) ? 0 : y.CompareTo(x));
_price_dirty = true;
_piq_dirty = true;
// **use local vm to protect against re-assignment,
@@ -1047,7 +1052,7 @@ namespace mbts.spark {
#endregion
void _refresh_frozen_mkt_lines() {
if (_price_list is null)
if (_price_list_backing is null)
return;
_mkt_break_lines.refresh_frozen();
_refresh_bid_mkt_break();
@@ -1109,9 +1114,9 @@ namespace mbts.spark {
bool last_trade_changed = _vm.md.get_last_trade(ref unused, out _last_trade, out DateTime _, out ulong __);
bool depth_changed = _vm.md.get_depth(ref _prices_updated, ref _bids, ref _asks, NativePriceHelpers.COCONUT_MAX_DEPTH);
_mkt_break_lines.refresh();
if (_price_list is null) {
if (_price_list_backing is null) {
Log.assert(_price_dirty);
_price_list = new LadderPriceList {
_price_list_backing = new LadderPriceList {
length = _initial_row_count,
security = security
};
@@ -1126,9 +1131,9 @@ namespace mbts.spark {
_price_dirty = true;
}
if (_price_dirty) {
if (_pending_price_list != null && !_pending_price_list.Equals(_price_list)) {
var prev_price_list = _price_list;
_price_list = _pending_price_list;
if (_pending_price_list != null && !_pending_price_list.Equals(_price_list_backing)) {
var prev_price_list = _price_list_backing;
_price_list_backing = _pending_price_list;
_pending_price_list = null;
if (prev_price_list.Count != _price_list.Count) {
_reset_height();
@@ -1359,11 +1364,6 @@ namespace mbts.spark {
void _clear_visuals() {
if (_settings == null)
return;
if (_price_list is null) {
_price_list = new LadderPriceList {
security = security
};
}
_price_list.reset(null, null, _initial_row_count);
Array.Clear(_bids, 0, _bids.Length);
Array.Clear(_asks, 0, _asks.Length);
@@ -1429,8 +1429,11 @@ namespace mbts.spark {
LadderRender.render_single_grid_line(vis, row, _settings.row_height, color, _price_column.x_left, _settings.price_column.width);
#endregion
void _invalidate_spec() =>
void _invalidate_spec(){
_spec_backing = null;
_price_comparer_backing = null;
_price_list_backing = null;
}
void _settings_updated(LadderSettings s) {
double prev_row_height;
+46
View File
@@ -8,6 +8,7 @@ using System.Collections.Generic;
using System.Composition;
using System.Reactive.Subjects;
// using System.Diagnostics;
namespace bts.core {
[Export(typeof(IBus))]
public class SimpleBus : IBus {
@@ -28,10 +29,55 @@ namespace bts.core {
return result;
}
}
// class TimedSubscriptionCallbackWrapper<T> : ISubject<T> {
// readonly ISubject<T> subject;
// public TimedSubscriptionCallbackWrapper(ISubject<T> subject) =>
// this.subject = subject;
// public void OnCompleted() =>
// subject.OnCompleted();
// public void OnError(Exception error) =>
// subject.OnError(error);
// public void OnNext(T value) =>
// subject.OnNext(value);
// public IDisposable Subscribe(IObserver<T> observer) {
// return subject.Subscribe(onNext, observer.OnError, observer.OnCompleted);
// void onNext(T value) {
// var timer = Stopwatch.StartNew();
// observer.OnNext(value);
// timer.Stop();
// bts.Log.Gen.info($"SimpleBus.onNext took {timer.ElapsedMilliseconds} ms {value}");
// }
// }
// }
// private ISubject<T> subject<T>(object tag) {
// var inst = SubjectSingleton<T>.instance;
// lock (inst) {
// if (inst.TryGetValue(tag, out ISubject<T> result)) {
// return result;
// }
// Log.Ui.info($"SimpleBus, publishing '{tag}' for the first time");
// result = new ReplaySubject<T>(1);
// if (tag == mbts.spark.SparkTags.SystemContractsTag) {
// result = new TimedSubscriptionCallbackWrapper<T>(result);
// }
// inst[tag] = result;
// return result;
// }
// }
public void post<T>(ITag<T> tag, T data) {
try {
// Stopwatch stopwatch = Stopwatch.StartNew();
subject<T>(tag).OnNext(data);
// stopwatch.Stop();
// if(stopwatch.ElapsedMilliseconds > 50)
// bts.Log.Gen.info($"SimpleBus.post took {stopwatch.ElapsedMilliseconds} ms {tag} {data}");
// TODO: for debugging purposes, remove it later
if (tag != mbts.spark.SparkTags.SparkTimer) {
var tid = System.Threading.Thread.CurrentThread.ManagedThreadId;
+10 -7
View File
@@ -131,8 +131,9 @@ namespace Spark.Api.Orders {
});
return r.value;
} catch {
var r = new Response();
r.ErrorCode = OrderError.Undefined;
var r = new Response {
ErrorCode = OrderError.Undefined
};
return r;
}
}
@@ -143,7 +144,7 @@ namespace Spark.Api.Orders {
server_theos.Theos.AddRange(ptp_list);
var r = await _order_service.set_precomputed_theos_async(server_theos);
var r2 = new Response {
ErrorCode = OrderError.Undefined,
ErrorCode = OrderError.Ok,
Msg = r.ToString()
};
return r2;
@@ -167,8 +168,9 @@ namespace Spark.Api.Orders {
});
return r.value;
} catch {
var r = new Response();
r.ErrorCode = OrderError.Undefined;
var r = new Response {
ErrorCode = OrderError.Undefined
};
return r;
}
}
@@ -180,8 +182,9 @@ namespace Spark.Api.Orders {
});
return r.value;
} catch {
var r = new Response();
r.ErrorCode = OrderError.Undefined;
var r = new Response {
ErrorCode = OrderError.Undefined
};
return r;
}
}
@@ -0,0 +1,3 @@
#include "IFIXGenericAdapter.h"
namespace bts::orderman {}
@@ -0,0 +1,61 @@
#pragma once
#include "lib/api/services/OrderService.pb.h"
#include "lib/config/protos/jetfix.pb.h"
#include "lib/domain_model/Security.pb.h"
#include "lib/jetfix/codec/MessageDefinitions.h"
#include "lib/jetfix/common/OutFIXPtr.h"
#include "lib/md/SecurityOracle.h"
#include "lib/mercury/events/Update.h"
#include "lib/orderman/CreateOrderMsg.h"
#include "lib/orderman/OrderMsg.h"
#include "lib/orderman/orderman_utils/ExchangeOpenOrders.h"
#include "lib/orderman/orderman_utils/ExchangePosition.h"
#include "lib/orderman/orderman_utils/orderman_common.h"
#include "lib/utils/bts_no_copy.h"
#include <functional>
#include <memory>
#include <variant>
namespace bts::orderman {
struct OrderModification {};
struct OrderCancellation {};
class NewSecuritiesEvent;
class NewAdminDataEvent;
class ExecutionReport;
class CancelReject;
struct OrderStateWithFills {
api::orderman::OrderState ordr_state;
std::vector<api::orderman::Fill> fills = {};
};
using OnOrderUpdate = std::function<void(OrderStateWithFillsgit const &)>;
// fix execution gateway for a single FIX session
class IFIXGenericAdapter {
public:
IFIXGenericAdapter() = default;
virtual ~IFIXGenericAdapter() {}
virtual void start() = 0;
// this callback should be added in a list, and on receving an update from the other side, it should be passed here
virtual void register_callback(const OnOrderUpdate &);
virtual void launch_new_order(const CreateOrderMsg &);
virtual void modify_order(const CancelReplaceOrderMsg &);
virtual void cancel_order(const CancelOrderMsg &);
// as new securities are added
virtual void update_securities(
const mercury::Update<NewSecuritiesEvent> &) = 0;
virtual void update_admin_data(
const mercury::Update<NewAdminDataEvent> &) = 0;
private:
BTS_NO_COPY(IFIXGenericAdapter);
};
} // namespace bts::orderman
@@ -228,13 +228,19 @@ struct SEGSecurityData {
const CancelReplaceOrderMsg &cxlr_msg =
std::get<CancelReplaceOrderMsg>(msg);
SEGOrderState *seg_os = find_order(cxlr_msg.create_fields.key);
bts_assert(seg_os != nullptr);
if(seg_os == nullptr){
LOG(OrderMan, Info, "SimExecutionGateway::apply_order_action tried updating unkown order");
return {ActionResult::NoOp, nullptr};
}
auto action_result = seg_os->apply_change(cxlr_msg);
return {action_result, seg_os};
} else if (std::holds_alternative<CancelOrderMsg>(msg)) {
const CancelOrderMsg &cxl_msg = std::get<CancelOrderMsg>(msg);
SEGOrderState *seg_os = find_order(cxl_msg.key);
bts_assert(seg_os != nullptr);
if(seg_os == nullptr){
LOG(OrderMan, Info, "SimExecutionGateway::apply_order_action tried canceling unkown order");
return {ActionResult::NoOp, nullptr};
}
auto action_result = seg_os->apply_cxl(cxl_msg);
return {action_result, seg_os};
}
@@ -247,11 +253,12 @@ struct SEGSecurityData {
}
}
void remove_inactive_orders() {
orders.erase(
std::remove_if(
orders.begin(), orders.end(),
[](const auto &ord) { return !ord.active(); }),
orders.end());
return;
// orders.erase(
// std::remove_if(
// orders.begin(), orders.end(),
// [](const auto &ord) { return !ord.active(); }),
// orders.end());
}
void record_trade(const SEGTradeEntry &seg_te) { trades.push_back(seg_te); }
};
@@ -283,13 +283,6 @@ void QuotingAlgo::on_book(SecurityKey sec_key, Book const &book) {
const md::PriceSize &bid = me->sec_book->bid().at(0);
const md::PriceSize &ask = me->sec_book->ask().at(0);
//infers if limit order got filled and if it did does not try to update it
if (me->child_order_key != null_order_key() &&
((me->algo_params->side == api::SIDE_BUY && ask.price() <= me->child_order_price.value()) ||
(me->algo_params->side != api::SIDE_BUY && bid.price() >= me->child_order_price.value()))) {
return;
}
double mid_price=(bid.price() + ask.price())/2;
quote(mid_price);
@@ -373,7 +373,7 @@ extern "C" void init_spark_plugin(bts::SparkServices &srv) {
// init_sniper_plugin(srv);
init_task_eye_plugin(srv);
init_vol_algo_plugin(srv);
init_quoting_algo_plugin(srv);
// init_quoting_algo_plugin(srv);
}
} // namespace dorman
@@ -3,6 +3,7 @@
#include "lib/api/services/OrderService.pb.h"
#include "lib/logging/logging.h"
#include "lib/md/relay/RelayRecorder.h"
#include "lib/orderman/calculations/ITheoCalculator.h"
#include "lib/orderman/orderman_utils/Book.h"
#include <optional>
@@ -17,14 +18,43 @@ constexpr bool has_flag(ATDFlags prop, ATDFlags flag) {
return ((uint32_t)prop & (uint32_t)flag) == (uint32_t)flag;
}
const std::map<SecurityKey, api::Security> &get_some_securities() {
static std::map<SecurityKey, api::Security> results;
if (results.empty()) {
md::get_securities_from_replay([&](const api::Security &s) {
results.insert(std::make_pair(SecurityKey(s.id()), s));
});
static std::map<SecurityKey, api::Security> test_securities;
static std::map<SecurityKey, api::Security> get_dummy_securities() {
// TODO: Get securities from recorded market data
std::map<SecurityKey, api::Security> dummy_secs;
api::Security sec;
sec.set_id(7);
*sec.mutable_symbol() = "ES";
auto &glbx = *sec.mutable_glbx();
*glbx.mutable_cme_symbol() = "ES";
*glbx.mutable_cfi_code() = "FFICSO";
*glbx.mutable_security_desc() = "ESZ5";
auto &fut = *sec.mutable_future();
fut.set_expiration_year(2025);
fut.set_expiration_month(11);
fut.set_expiration_day(20);
auto &spec = *sec.mutable_contract_spec();
spec.set_cash_per_point(50);
spec.set_high_tick(0.25);
spec.set_low_tick(0.25);
spec.set_price_multiplier(1);
spec.set_price_display_format(2);
spec.set_main_fraction(100);
spec.set_sub_fraction(0);
dummy_secs[SecurityKey{sec.id()}] = std::move(sec);
return dummy_secs;
}
const std::map<SecurityKey, api::Security> &get_test_securities() {
if (test_securities.empty()) {
test_securities = get_dummy_securities();
}
return results;
return test_securities;
}
void set_test_securities(std::map<SecurityKey, api::Security> securities) {
test_securities = std::move(securities);
}
struct RecursionCounter {
@@ -64,6 +94,7 @@ struct AlgoTestDriverBase::Impl : public RecursionCounter {
std::vector<ChildOrder> detached_orders;
std::map<SecurityKey, Book> books;
std::shared_ptr<analytics::ITheoCalculator> theo_calc{nullptr};
std::map<SecurityKey, Price> settlements;
std::map<SecurityKey, bool> book_subs;
std::map<SecurityKey, bool> settlement_subs;
@@ -71,6 +102,7 @@ struct AlgoTestDriverBase::Impl : public RecursionCounter {
std::map<SecurityKey, bool> status_subs;
bool sub_timer = false;
bool sub_position = false;
bool is_subscribed_to_theos = false;
// Callbacks
std::vector<std::function<void(OrderKey, Quantity, Price)>>
@@ -130,7 +162,7 @@ AlgoTestDriverBase::AlgoTestDriverBase(
me->now = Timer::now();
me->state = AlgoState::CREATED;
auto securities = get_some_securities();
auto securities = get_test_securities();
REQUIRE(securities.size() > 0);
me->security = securities.begin()->second;
@@ -174,6 +206,14 @@ void AlgoTestDriverBase::update_book(const Book &book) {
}
}
void AlgoTestDriverBase::update_theos(
std::shared_ptr<analytics::ITheoCalculator> theo_calc) {
me->theo_calc = theo_calc;
if (me->is_subscribed_to_theos && theo_calc != nullptr) {
me->algo.on_theos(me->theo_calc.get());
}
}
void AlgoTestDriverBase::update_settlement(SecurityKey key, Price price) {
me->settlements[key] = price;
auto iter = me->settlement_subs.find(key);
@@ -362,7 +402,7 @@ OrderKey AlgoTestDriverBase::order_key() const { return me->order_key; }
OrderKey AlgoTestDriverBase::parent_order_key() const { return me->parent_key; }
const api::Security *AlgoTestDriverBase::lookup_security(
SecurityKey key) const {
const auto &s = get_some_securities();
const auto &s = get_test_securities();
auto iter = s.find(key);
if (iter != s.end()) {
return &iter->second;
@@ -372,7 +412,7 @@ const api::Security *AlgoTestDriverBase::lookup_security(
std::vector<api::Security const *>
AlgoTestDriverBase::known_securities() const {
std::vector<api::Security const *> result;
auto s = get_some_securities();
auto s = get_test_securities();
for (auto const &p : s) {
result.push_back(&p.second);
}
@@ -464,8 +504,16 @@ void AlgoTestDriverBase::unsubscribe_timer() { me->sub_timer = false; }
void AlgoTestDriverBase::subscribe_position() { me->sub_position = true; }
void AlgoTestDriverBase::unsubscribe_position() { me->sub_position = false; }
void AlgoTestDriverBase::subscribe_theos() {}
void AlgoTestDriverBase::unsubscribe_theos() {}
void AlgoTestDriverBase::subscribe_theos() {
me->is_subscribed_to_theos = true;
if (me->theo_calc != nullptr) {
me->algo.on_theos(me->theo_calc.get());
}
}
void AlgoTestDriverBase::unsubscribe_theos() {
me->is_subscribed_to_theos = false;
}
OrderKey AlgoTestDriverBase::launch_child(
ChildTemplate &, Price price, Quantity qty, bool sub) {
@@ -1,5 +1,6 @@
#pragma once
#include "lib/md/IntPrice.h"
#include "lib/orderman/calculations/ITheoCalculator.h"
#include "lib/orderman/events/TickerUpdateEvent.h"
#include "lib/orderman/orders/algo/IAlgoOrder.h"
#include "lib/orderman/orders/algo/IAlgoServices.h"
@@ -12,7 +13,8 @@ namespace bts {
namespace orderman {
namespace test {
const std::map<SecurityKey, api::Security> &get_some_securities();
const std::map<SecurityKey, api::Security> &get_test_securities();
void set_test_securities(std::map<SecurityKey, api::Security> securities);
enum class ATDFlags {
NONE = 0,
@@ -79,6 +81,7 @@ class AlgoTestDriverBase : private IAlgoServices {
void update_settlement(SecurityKey, Price);
void update_ticker(SecurityKey, const TradeVector &);
void update_status(SecurityKey, co2::TradingStatus);
void update_theos(std::shared_ptr<analytics::ITheoCalculator> theo_calc);
void advance_time(Duration, uint32_t steps = 1);
IAlgoOrder::OnCancelResponse cancel();