Merge branch 'master' into algo-templates-v1

This commit is contained in:
John Colvin committed 2019-02-02 10:47:53 -06:00
commit f76e862b8a
96 files changed
+3521 -1468

No files matched your search

+21
View File
@@ -1,4 +1,25 @@
# Release Notes
## 1.4.10
### Bug Fixes
- Removed duplicate entries in performance log
## 1.4.9
### SDK
- Added ticker events to the order API
### Performance Improvements
- Significantly improved hedging speed
### Bug Fixes
- Fixed a visual oddity in the order book UI when using High DPI displays
## 1.4.8
### Marketdata
- Additional trade details for CME ticker
## 1.4.7
### Bug Fixes
- Corrected FIX tag 1028, ManualOrderIndicator, when orders are triggered by algos
## 1.4.6
### Ladder Usability Improvements
- The ladder can now be zoomed in/out according to a user's preference
+8 -21
View File
@@ -59,7 +59,9 @@ namespace mbts.ui {
}
private static ImageSource load_resource_and_freeze(string resource_name) {
return ((DrawingImage)_resources[resource_name]).frozen();
lock (_resources) {
return ((DrawingImage)_resources[resource_name]).frozen();
}
}
#pragma warning disable CS0618 // Type or member is obsolete
@@ -193,7 +195,6 @@ namespace mbts.ui {
public static Drawing create_drawing(IEnumerable<string> paths, Brush brush, Pen pen = null, Transform transform = null) {
var drawing_group = new DrawingGroup();
foreach (string p in paths) {
var gd = new GeometryDrawing(brush, pen, Geometry.Parse(p));
drawing_group.Children.Add(new GeometryDrawing(brush, pen, Geometry.Parse(p)));
}
if (transform != null)
@@ -202,7 +203,10 @@ namespace mbts.ui {
}
public static IEnumerable<string> get_geometric_path(string geometric_path_key) {
object obj = _resources[geometric_path_key];
object obj;
lock (_resources) {
obj = _resources[geometric_path_key];
}
if (obj is string path) {
if (string.IsNullOrEmpty(path)) {
Log.Api.error($"Resource lookup of '{geometric_path_key}' did not return a valid string.", nameof(geometric_path_key));
@@ -213,22 +217,5 @@ namespace mbts.ui {
return paths.Where(p => !string.IsNullOrEmpty(p)).ToArray();
} else return null;
}
public static Drawing create_disk_document_icon(IEnumerable<string> paths, Brush brush, Pen pen = null, Transform transform = null) {
var drawing_group = new DrawingGroup();
var plist = paths.ToList();
int c = 0;
foreach (string p in paths) {
var gd = new GeometryDrawing(brush, pen, Geometry.Parse(p));
if (c == plist.Count - 1) {
//gd.Bounds = new Rect(20, 20, 8, 7);
//gd.
}
drawing_group.Children.Add(new GeometryDrawing(brush, pen, Geometry.Parse(p)));
}
if (transform != null)
drawing_group.Transform = transform;
return drawing_group;
}
}
}
}
@@ -62,7 +62,8 @@
<Setter Property="ColumnHeaderStyle">
<Setter.Value>
<Style TargetType="GridViewColumnHeader">
<Setter Property="Background" Value="{mbts:BTSColor header}"/>
<!-- Background - note that we're settings this property on the containing stackpanel in which the GridViewHeaderRowPresenter resides -->
<Setter Property="Background" Value="{x:Null}"/>
<Setter Property="Foreground" Value="{mbts:BTSColor header_text}"/>
<Setter Property="BorderBrush" Value="{mbts:BTSColor header}"/>
<Setter Property="BorderThickness" Value="0,1,1,0"/>
@@ -301,7 +302,9 @@
HorizontalScrollBarVisibility="Hidden"
VerticalScrollBarVisibility="Hidden"
x:Name="_header_scroll_viewer">
<StackPanel>
<!-- setting the bg color for this stackpanel is a work-around for the vertical white lines that appear on high-dpi displays between some columns -->
<!-- in reality, there is some high dpi rounding thing going on, but that's a battle for another day -->
<StackPanel Background="{mbts:BTSColor Name=header}">
<!-- We don't need super headers over the fixed columns -->
<StackPanel Orientation="Horizontal" x:Name="_super_headers" />
<GridViewHeaderRowPresenter x:Name="_header_scroll" AllowsColumnReorder="False" ColumnHeaderContainerStyle="{TemplateBinding ColumnHeaderStyle}" UseLayoutRounding="True" />
+1
View File
@@ -5,6 +5,7 @@ using System.Runtime.InteropServices;
namespace bts {
enum Level {
NoLog,
Diag,
Info,
Warn,
@@ -9,12 +9,12 @@ using System.Threading.Tasks;
namespace bts.test {
public static class InProcessTestRunner {
public static void run_test_syncronously(string v, TestEnumerator enumerator, Action<TestResult> report) {
public static void run_test_synchronously(string v, TestEnumerator enumerator, Action<TestResult> report) {
foreach (var t in enumerator.find(v)) {
run_test_syncronously(t, report);
run_test_synchronously(t, report);
}
}
public static void run_test_syncronously(TestMethod test, Action<TestResult> report) {
public static void run_test_synchronously(TestMethod test, Action<TestResult> report) {
var result = new TestResult() {
TestName = test.full_name,
Status = TestResult.Types.Step.Setup,
@@ -2,8 +2,16 @@
namespace bts.test {
public enum TestType {
Unit, System, Shell
}
[AttributeUsage(AttributeTargets.Class)]
public class TestGroupAttribute : Attribute {
public TestGroupAttribute() {
this.TestType = TestType.Unit;
}
public TestType TestType;
}
[AttributeUsage(AttributeTargets.Method)]
public class TestAttribute : Attribute {
@@ -7,23 +7,34 @@ namespace bts.test {
public class TestEnumerator {
public TestEnumerator(params string[] assemblies) {
TestGroups =
TestGroups =
assemblies
.Select(Assembly.Load)
.SelectMany(a => a.GetTypes())
.Where(t => t.GetCustomAttribute<TestGroupAttribute>() != null)
.Select(t =>
new TestClass(t,
new TestClass(t,
t.GetCustomAttribute<TestGroupAttribute>().TestType,
t.GetMethods()
.Where(m => m.GetCustomAttribute<TestAttribute>() != null)
.Select(m => new TestMethod(t, m))
.ToList()))
.ToList();
.ToList();
}
public IReadOnlyCollection<TestClass> TestGroups { get; }
public IEnumerable<TestClass> get_groups_of_type(TestType tt) {
return TestGroups.Where(wt => wt.test_type == tt);
}
public IEnumerable<TestMethod> Tests => TestGroups.SelectMany(t => t.methods);
public IEnumerable<TestMethod> get_of_type(TestType tt) {
return TestGroups.Where(wt => wt.test_type == tt).SelectMany(t => t.methods);
}
public IEnumerable<TestMethod> find(string name) {
foreach (var g in TestGroups) {
foreach (var t in g.methods) {
@@ -41,13 +52,15 @@ namespace bts.test {
public class TestClass {
public TestClass(Type type, IEnumerable<TestMethod> methods) {
public TestClass(Type type, TestType test_type, IEnumerable<TestMethod> methods) {
this.type = type;
this.test_type = test_type;
this.methods = methods;
}
public string name => type.Name;
public Assembly assembly => type.Assembly;
public Type type { get; }
public TestType test_type { get; }
public IEnumerable<TestMethod> methods { get; }
public override string ToString() {
return $"[TestGroup:{name}]";
+280
View File
@@ -0,0 +1,280 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;
using System.Text;
using System.Threading.Tasks;
using bts.api;
using bts.api.admin;
using bts.api.core;
using bts.api.test;
using bts.test;
using bts.api.orderman;
using mbts;
using mbts.tests;
using bts.co2;
namespace bts.tests
{
[TestGroup(TestType = TestType.System)]
public class LimitsTests : IDisposable {
ServiceContext _context;
OrderService _order_service;
AdminService _admin_service;
public LimitsTests (){
_context = new ServiceContext();
_order_service = _context.locate<OrderService>();
_admin_service = _context.locate<AdminService>();
var auth = _context.login("bts","");
auth.Wait(3000);
}
public void Dispose() {
Disposable.dispose(ref _order_service);
Disposable.dispose(ref _admin_service);
Disposable.dispose(ref _context);
}
async Task<IEnumerable<SecurityImmutable>> all_futures(string symbol) {
var request = new MDRequest() {
Host = "system_test",
Channels = {
new SecurityChannel() {
Time = 0,
Flags = 0,
Type = SecurityType.Future,
Exchange = Venue.Sim,
Symbol = symbol,
TradeCursor = 0,
RfqCursor = 0
}
}
};
var relay = _context.locate<MarketDataRelay>();
await Task.Delay(100);
var result = await relay.requestmd_async(request);
List<SecurityImmutable> securities = new List<SecurityImmutable>();
if (result.success) {
foreach (var ch in result.value.Channels) {
foreach (var update in ch.Updates) {
if (update.Security != null) {
securities.Add(new SecurityImmutable(update.Security));
}
}
}
}
return securities;
}
[Test]
public async Task test_clip_limits() {
//Get and set limit values from the admin service
uint test_limit_value = 10u;
string test_symbol = "CL";
var test_user = (name: "bts", id: 1u);
var test_account = (name: "ACC A", id: 1u);
var account_data = await AdminUtilities.get_account_data(_admin_service, test_account.name);
Assert.IsTrue(account_data.HasValue);
var old_clip_limit = AdminUtilities.get_clip_limit(account_data, test_symbol);
//create Orders Here
var all = await all_futures(test_symbol);
SecurityImmutable? sec = all.FirstOrDefault();
Assert.IsTrue(sec.HasValue);
uint under_limit_order_key;
uint over_limit_order_key;
uint no_limit_order_key;
var order_def = create_order_def(sec.Value.Id, Side.Buy, test_account, test_user);
Assert.IsTrue(sec.HasValue, "Unable to create order without security");
await AdminUtilities.remove_clip_limit(_admin_service, test_account.name, test_symbol);
Result<Response> no_limit_order_result = await _order_service.create_order_async(new Create() {
NewOrder = order_def,
Price = 5,
Quantity = test_limit_value
});
var set_success = await AdminUtilities.set_clip_limit(_admin_service, test_limit_value, account_data, test_symbol);
Assert.IsTrue(set_success, "Failed to set limit");
Result<Response> under_limit_order_result = await _order_service.create_order_async(new Create() {
NewOrder = order_def,
Price = 5,
Quantity = test_limit_value
});
Result<Response> over_limit_order_result = await _order_service.create_order_async(new Create() {
NewOrder = order_def,
Price = 5,
Quantity = test_limit_value + 1
});
under_limit_order_key = under_limit_order_result.value.OrderKey;
over_limit_order_key = over_limit_order_result.value.OrderKey;
no_limit_order_key = no_limit_order_result.value.OrderKey;
var orders = _order_service.subscribei_orders().Subscribe(ords =>
{
IEnumerable<OrderRecordImmutable> filtered_orders = ords.all.Where(x => x.Key == under_limit_order_key || x.Key == over_limit_order_key || x.Key == no_limit_order_key);
foreach(var o in filtered_orders)
{
if(o.Key == under_limit_order_key) {
Assert.IsTrue(o.State.Status == OrderStatus.Working, "Under Limit Order is not 'Working'");
}
else if(o.Key == over_limit_order_key) {
Assert.IsTrue(o.State.Status == OrderStatus.RejectedBySpark, "Over Limit Order is not 'RejectedBySpark'");
}
else if(o.Key == no_limit_order_key) {
Assert.IsTrue(o.State.Status == OrderStatus.RejectedBySpark, "No Limit Order is not 'RejectedBySpark'");
}
}
});
// Restore the Limits to the original values
bool success = false;
if(old_clip_limit.HasValue) {
success = await AdminUtilities.set_clip_limit(_admin_service, old_clip_limit.Value, test_account.name, test_symbol);
}
else {
success = await AdminUtilities.remove_clip_limit(_admin_service, test_account.name, test_symbol);
}
Assert.IsTrue(success, "Failed to restore Limit");
}
public OrderDefinition create_order_def(uint security_id, Side side, (String name, uint id) account, (String name, uint id) user){
return new OrderDefinition {
SecurityKey = security_id,
Side = Side.Buy,
TimeInForce = TimeInForce.Day,
CustomDef = new CustomOrderDefinition { Limit = new LimitOrderDefinition { } },
CtiCode = CTICode.CtiCode1,
CustomerOrFirm = CustomerOrFirm.Firm,
ManualOrderIndicator = ManualOrderIndicator.Yes,
Account = account.name,
AccountId = account.id,
User = user.name,
UserId = user.id
};
}
}
public class AdminUtilities {
public static async Task<BTSAccountImmutable?> get_account_data (AdminService admin_service, string account_name)
{
Update<AdminDataImmutable, int> update = await admin_service.subscribei_admin_data().FirstAsync();
foreach (var acc in update.all.First().Accounts) {
if(acc.Name == account_name) {
return acc;
}
}
return null;
}
public static AccountSecurityClipLimitUnitImmutable? get_clip_limit_row(BTSAccountImmutable? account_data, string symbol, SecurityType security_type = SecurityType.Future, Venue venue = Venue.Sim) {
IEnumerable<AccountSecurityClipLimitUnitImmutable> clip_limit_rows = account_data.Value.SecurityClipLimits.Limit.Where(
r => r.Security.SecuritySymbol == symbol
&& r.Security.Exchange == venue
&& r.Security.SecurityType == security_type);
return clip_limit_rows.Any() ? clip_limit_rows.First() : (AccountSecurityClipLimitUnitImmutable?) null;
}
public static uint? get_clip_limit(BTSAccountImmutable? account_data, string symbol, SecurityType security_type = SecurityType.Future, Venue venue = Venue.Sim) {
var row = get_clip_limit_row(account_data,symbol,security_type,venue);
return row.HasValue ? row.Value.ClipSize : (uint?) null;
}
public static async Task<uint?> get_clip_limit(AdminService admin_service, string account_name, string symbol, SecurityType security_type = SecurityType.Future, Venue venue = Venue.Sim) {
var account_data = await get_account_data(admin_service, account_name);
return get_clip_limit(account_data, symbol, security_type, venue);
}
public static async Task<bool> set_clip_limit(AdminService admin_service, uint clip_value, string account_name, string symbol, SecurityType security_type = SecurityType.Future, Venue venue = Venue.Sim) {
var account_data = await get_account_data(admin_service, account_name);
return await set_clip_limit(admin_service, clip_value,account_data, symbol,security_type, venue);
}
public static async Task<bool> set_clip_limit(AdminService admin_service, uint clip_value, BTSAccountImmutable? account_data, string symbol, SecurityType security_type = SecurityType.Future, Venue venue = Venue.Sim) {
AccountSecurityClipLimitUnitImmutable? origin_clip_limit_row = get_clip_limit_row(account_data,symbol,security_type,venue);
AccountSecurityClipLimitUnitImmutable new_clip_limit_row;
List<AccountSecurityClipLimitUnitImmutable> clip_limits_list = account_data.Value.SecurityClipLimits.Limit.ToList();
if(origin_clip_limit_row.HasValue) {
clip_limits_list.Remove(origin_clip_limit_row.Value);
}
new_clip_limit_row = (AccountSecurityClipLimitUnitImmutable) new AccountSecurityClipLimitUnit {
Security = new SecurityScope(){ SecuritySymbol = symbol, Exchange = venue, SecurityType = security_type },
ClipSize = clip_value
};
clip_limits_list.Add(new_clip_limit_row);
AccountSecurityClipLimits temp_scl = new AccountSecurityClipLimits();
temp_scl.Limit.AddRange(clip_limits_list.Select(x => x.CloneToMutable()));
var new_account_data = account_data.Value.with(security_clip_limits: temp_scl);
var update_ret = await admin_service.update_account_async(new AccountUpdateAPIArgument {
CompleteData = new_account_data.CloneToMutable()
});
return update_ret.success;
}
public static async Task<bool> remove_clip_limit(AdminService admin_service, string account_name, string symbol, SecurityType security_type = SecurityType.Future, Venue venue = Venue.Sim) {
var account_data = await get_account_data(admin_service, account_name);
return await remove_clip_limit(admin_service, account_data, symbol, security_type, venue);
}
public static async Task<bool> remove_clip_limit(AdminService admin_service, BTSAccountImmutable? account_data, string symbol, SecurityType security_type = SecurityType.Future, Venue venue = Venue.Sim) {
AccountSecurityClipLimitUnitImmutable? origin_clip_limit_row = get_clip_limit_row(account_data,symbol,security_type,venue);
List<AccountSecurityClipLimitUnitImmutable> clip_limits_list = account_data.Value.SecurityClipLimits.Limit.ToList();
if(origin_clip_limit_row.HasValue) {
clip_limits_list.Remove(origin_clip_limit_row.Value);
AccountSecurityClipLimits temp_scl = new AccountSecurityClipLimits();
temp_scl.Limit.AddRange(clip_limits_list.Select(x => x.CloneToMutable()));
var new_account_data = account_data.Value.with(security_clip_limits: temp_scl);
var update_ret = await admin_service.update_account_async(new AccountUpdateAPIArgument {
CompleteData = new_account_data.CloneToMutable()
});
return update_ret.success;
}
else
{
return false;
}
}
}
}
+1 -1
View File
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net471</TargetFramework>
<TargetFramework>netstandard2.0</TargetFramework>
<Platform>x64</Platform>
<PlatformTarget>x64</PlatformTarget>
</PropertyGroup>
+9 -4
View File
@@ -15,12 +15,17 @@ namespace sparkcmd {
uint _parallel = 1;
bool _list;
TestType test_type = TestType.Unit;
public TestCommand() {
IsCommand("test", "Run managed unit tests");
// i comes thorugh as "t" which seems daft.
// i comes thorugh as "t" which seems daft.
HasOption("t|timeout", "Timeout unit tests", i => _timeout = 10);
HasOption<uint>("n|parallel", "How many tests to run in parallel", i => _parallel = i);
HasOption("l|list", "List all tests", _ => _list = true);
HasOption("unit","Run unit tests", _ => test_type = TestType.Unit);
HasOption("system","Run system tests", _ => test_type = TestType.System);
HasOption("shell","Run shell tests", _ => test_type = TestType.Shell);
HasAdditionalArguments(null, " [Test List]");
}
@@ -35,7 +40,7 @@ namespace sparkcmd {
if (_timeout == 0 && tests.Length == 1) {
//System.Threading.Thread.Sleep(5000);
using (var trr = new TestResultReceiver(true)) {
InProcessTestRunner.run_test_syncronously(tests[0], Enumerator, trr.get_reporter());
InProcessTestRunner.run_test_synchronously(tests[0], Enumerator, trr.get_reporter());
trr.print_summary();
return trr.ErrorCount;
}
@@ -44,7 +49,7 @@ namespace sparkcmd {
return run_tests(Enumerator.match(tests).Select(t => t.name));
}
else {
return run_tests(Enumerator.Tests.Select(t => t.full_name));
return run_tests(Enumerator.get_of_type(test_type).Select(t => t.full_name));
}
}
@@ -59,7 +64,7 @@ namespace sparkcmd {
void list_tests() {
Console.WriteLine("All tests: ");
foreach (var t in Enumerator.TestGroups) {
foreach (var t in Enumerator.get_groups_of_type(test_type)) {
ConsoleEx.WriteLine(t.name, ConsoleColor.Green);
foreach (var test in t.methods) {
Console.Write($" {test.name}");
+3 -3
View File
@@ -8,10 +8,10 @@ using System.Linq;
namespace sparkcmd {
class Program {
static void Main(string[] args) {
static int Main(string[] args) {
mbts.config.Configuration.initialize();
var commands = ConsoleCommandDispatcher.FindCommandsInSameAssemblyAs(typeof(Program));
ConsoleCommandDispatcher.DispatchCommand(commands, args, Console.Out);
return;
return ConsoleCommandDispatcher.DispatchCommand(commands, args, Console.Out);
}
}
}
+2 -3
View File
@@ -2,8 +2,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net471</TargetFramework>
<!--<TargetFrameworks>net471;netcoreapp2.1</TargetFrameworks>-->
<TargetFrameworks>net471;netcoreapp2.1</TargetFrameworks>
<RuntimeIdentifiers>win7-x64;ubuntu.16.04-x64</RuntimeIdentifiers>
<Authors>Blue Trading Systems</Authors>
<Company>Blue Trading Systems</Company>
@@ -18,7 +17,7 @@
<ProjectReference Include="../spark_api/spark_api.csproj" />
<!--<ProjectReference Include="..\spark\Spark.csproj" />-->
<ProjectReference Include="..\spark_core\spark_core.csproj" />
<ProjectReference Include="..\spark_test\spark_test.csproj" Condition="'$(Configuration)'=='Debug'" />
<ProjectReference Include="..\spark_test\spark_test.csproj" />
<!--<Proto3 Include="**/*.proto" />-->
</ItemGroup>
+240 -105
View File
@@ -1,3 +1,6 @@
/* -------------------------------------------------------------------------------- */
/* Main tables */
/* -------------------------------------------------------------------------------- */
create table if not exists
session (
version TEXT,
@@ -5,6 +8,7 @@ session (
build_type TEXT,
session_start TIMESTAMP PRIMARY KEY
);
create index if not exists session_idx on session(session_start);
create table if not exists
trace (
@@ -20,55 +24,46 @@ trace (
sn INTEGER,
uint32_1 INTEGER
);
drop view if exists recent_trace;
create view recent_trace as
select * from trace order by rowid desc limit 140123;
create index if not exists trace_idx on trace(session_start,channel,sn,ts);
create index if not exists trace_t_idx on trace(session_start,ts);
drop view if exists ordered_trace;
create view ordered_trace as
select
recent_trace.*,
row_number() over (order by session_start,ts) entry_rank
from
recent_trace;
/* -------------------------------------------------------------------------------- */
/* Main Views */
/* -------------------------------------------------------------------------------- */
drop view if exists delta_trace;
create view delta_trace as
/* An ordered view of the traces, with extra columns for the delta
betwen consecutive traces as well as the delta between related
(scoped) traces */
drop view if exists log_view;
create view log_view as
select
current.*,
(current.ts - previous.ts) / 1000.0 delta
from
ordered_trace current,
ordered_trace previous
where
current.entry_rank = previous.entry_rank+1;
drop view if exists log;
create view log as
select
delta_trace.*,
trace.*,
trace.rowid entry_id,
(ts-lag(ts) over by_session_ts)/1000.0 delta,
case cor_event>0 when 1 then (ts-cor_event)/1000.0 else 0 end scope_delta,
case cor_event>0 when 1 then "end of " || entry_kind else entry_kind end scoped_entry
from
delta_trace;
drop view if exists events;
create view events as
trace
window
by_session_ts as (order by session_start,ts);
/* A view identifying each high level event (requote or hedge) and
annotating it with its latency */
drop view if exists event_view;
create view event_view as
select
fix_reads.session_start,
"hedge" event_type,
fix_reads.hts hts_start,
fix_reads.entry_id event_id,
fix_reads.ts start,
fix_writes.ts stop,
(fix_writes.ts - fix_reads.ts) / 1000.0 duration,
row_number() over (order by fix_reads.session_start,fix_reads.ts,fix_writes.ts) event_rank ,
fix_reads.channel,
fix_reads.sn
from
(select session_start, channel, sn, ts from recent_trace where entry_kind='fix_msg_r') fix_reads,
(select session_start, channel, sn, ts from recent_trace where entry_kind='before_fix_socket_w') fix_writes
(select session_start, channel, sn, hts,ts,rowid entry_id from trace where entry_kind='fix_msg_r' order by ts) fix_reads,
(select session_start, channel, sn, hts,ts,rowid entry_id from trace where entry_kind='before_fix_socket_w') fix_writes
where
fix_reads.session_start = fix_writes.session_start and
fix_reads.channel = fix_writes.channel and
@@ -77,35 +72,168 @@ union
select
md_reads.session_start,
"requote" event_type,
md_reads.hts hts_start,
md_reads.rowid event_id,
md_reads.ts start,
fix_writes.ts stop,
(fix_writes.ts - md_reads.ts) / 1000.0 duration,
row_number() over (order by md_reads.session_start,md_reads.ts,fix_writes.ts) event_rank ,
md_reads.channel,
md_reads.sn
from
(select session_start, channel, sn, ts from recent_trace where entry_kind='md') md_reads,
(select session_start, channel, sn, ts from recent_trace where entry_kind='before_fix_socket_w') fix_writes
(select distinct session_start, channel, sn, hts,ts from trace where entry_kind='md' order by ts) md_reads,
(select session_start, channel, sn, hts,ts,rowid entry_id from trace where entry_kind='before_fix_socket_w') fix_writes
where
md_reads.session_start = fix_writes.session_start and
md_reads.channel = fix_writes.channel and
md_reads.sn = fix_writes.sn;
drop view if exists event_log;
create view event_log as
/* A view on hedge events, identifying each order sent as part of the event*/
drop view if exists hedge_trace_view;
create view hedge_trace_view as
select
log.*,
event_type,
event_rank,
events.duration event_duration,
events.start event_start,
events.stop event_stop
session.version,
session.host,
session.build_type,
session.session_start,
trace.hts trace_time,
uint64_1 order_key,
duration total,
event_id
from
log,
events
event_view,
trace,
session
where
log.session_start = events.session_start and
log.ts >= events.start and log.ts <= events.stop;
session.session_start = trace.session_start and
session.session_start = event_view.session_start and
event_view.channel=trace.channel and event_view.sn=trace.sn and
event_type='hedge' and
entry_kind='NativeOrder::on_activate' and
cor_event=0;
/* A view on requote events, identifying each order sent as part of the event*/
drop view if exists requote_trace_view;
create view requote_trace_view as
select
session.version,
session.host,
session.build_type,
session.session_start,
trace.hts trace_time,
uint64_1 order_key,
duration total,
event_id
from
event_view,
trace,
session
where
session.session_start = trace.session_start and
session.session_start = event_view.session_start and
event_view.channel=trace.channel and event_view.sn=trace.sn and
event_type='requote' and
entry_kind='NativeOrder::on_update' and
cor_event=0;
/* -------------------------------------------------------------------------------- */
/* Archive tables/view */
/* -------------------------------------------------------------------------------- */
/* Auxiliary tables and views, used to archive the results of the main
views to speed up analysis as data accumulates */
create table if not exists
event_archive (
session_start TIMESTAMP,
event_type TEXT,
hts_start TIMESTAMP,
event_id INTEGER,
start INTEGER,
stop INTEGER,
duration float,
channel TEXT,
sn INTEGER
);
create index if not exists event_archive_idx on event_archive(session_start,start,stop);
create table if not exists
log_archive(
session_start TIMESTAMP,
ts INTEGER,
hts timestamp,
channel TEXT,
entry_kind TEXT,
txt_1 TEXT,
uint64_1 INTEGER,
thread TEXT,
cor_event INTEGER,
sn INTEGER,
uint32_1 INTEGER,
entry_id INTEGER,
delta float,
scope_delta float,
scoped_entry TEXT
);
create index if not exists log_archive_idx on log_archive(session_start,ts);
create table if not exists
hedge_trace_archive (
version TEXT,
host TEXT,
build_type TEXT,
session_start TIMESTAMP,
trace_time TIMESTAMP,
order_key INTEGER,
total float,
event_id integer
);
drop view if exists hedge_trace;
create view hedge_trace as
select * from hedge_trace_view union select * from hedge_trace_archive;
create table if not exists
requote_trace_archive (
version TEXT,
host TEXT,
build_type TEXT,
session_start TIMESTAMP,
trace_time TIMESTAMP,
order_key INTEGER,
total float,
event_id integer
);
drop view if exists requote_trace;
create view requote_trace as
select * from requote_trace_view union select * from requote_trace_archive;
/* a synthetis of the log and events, focusing only on traces that are
part of hedge/requote events */
create table if not exists
event_log_archive (
session_start TIMESTAMP,
ts INTEGER,
hts timestamp,
channel TEXT,
entry_kind TEXT,
txt_1 TEXT,
uint64_1 INTEGER,
thread TEXT,
cor_event INTEGER,
sn INTEGER,
uint32_1 INTEGER,
entry_id INTEGER,
delta float,
scope_delta float,
scoped_entry TEXT,
event_type TEXT,
event_id INTEGER,
event_duration float,
hts_start TIMESTAMP,
event_start INTEGER,
event_stop INTEGER
);
create index if not exists event_log_archive_idx on event_log_archive(session_start);
drop view if exists pretty_log;
create view pretty_log as
@@ -115,69 +243,76 @@ select
scope_delta ,
event_duration,
event_type,
event_rank,
event_id,
hts,
ts,
session_start,
entry_rank
entry_id
from
event_log;
event_log_archive;
drop view if exists last_pretty_log;
create view last_pretty_log as
select * from pretty_log where session_start=(select max(session_start) from session);
select
*
from
pretty_log
where
session_start=(select max(session_start) from session);
-- drop view if exists summary;
-- create view summary as
-- select session_start,event_type,count(*) count, avg(duration) avg, max(duration) max, min(duration) min, median(duration) median, stdev(duration) stdev from events group by session_start,event_type;
/*
drop view if exists event_stats;
create view event_stats as
select
session_start,
event_type,
count(*) count,
avg(duration) avg,
max(duration) max,
min(duration) min,
median(duration) median,
stdev(duration) stdev
from
event_archive
group by
session_start,
event_type;
*/
drop view if exists event_log_aux;
create view event_log_aux as
select
log_archive.*,
event_type,
event_id,
event_archive.duration event_duration,
event_archive.hts_start hts_start,
event_archive.start event_start,
event_archive.stop event_stop
from
log_archive,
event_archive
where
log_archive.session_start = event_archive.session_start and
log_archive.ts >= event_archive.start and log_archive.ts <= event_archive.stop;
begin transaction;
insert into hedge_trace_archive select * from hedge_trace_view;
insert into requote_trace_archive select * from requote_trace_view;
insert into event_archive select * from event_view;
insert into log_archive select * from log_view;
insert into event_log_archive select * from event_log_aux;
delete from trace;
delete from event_log_archive where
rowid in (select rowid from event_log_archive order by rowid desc limit -1 offset 10000123);
delete from hedge_trace_archive where
rowid in (select rowid from hedge_trace_archive order by rowid desc limit -1 offset 10000123);
delete from requote_trace_archive where
rowid in (select rowid from requote_trace_archive order by rowid desc limit -1 offset 10000123);
delete from log_archive where
rowid in (select rowid from log_archive order by rowid desc limit -1 offset 10000123);
delete from event_log_archive where
rowid in (select rowid from event_log_archive order by rowid desc limit -1 offset 10000123);
commit;
drop view if exists hedge_trace;
create view hedge_trace as
select
session.version,
session.host,
session.build_type,
session.session_start,
recent_trace.hts trace_time,
recent_trace.uint64_1 order_key,
hedge_events.duration total,
hedge_events.event_rank rank
from
recent_trace,
(select * from events where event_type='hedge') hedge_events,
session
where
entry_kind='NativeOrder::on_activate' and
recent_trace.cor_event = 0 and
recent_trace.channel = hedge_events.channel and
recent_trace.session_start = hedge_events.session_start and
recent_trace.sn = hedge_events.sn and
recent_trace.session_start = session.session_start;
drop view if exists requote_trace;
create view requote_trace as
select
session.version,
session.host,
session.build_type,
session.session_start,
recent_trace.hts trace_time,
recent_trace.uint64_1 order_key,
requote_events.duration total,
requote_events.event_rank rank
from
recent_trace,
(select * from events where event_type='requote') requote_events,
session
where
entry_kind='NativeOrder::on_update' and
recent_trace.cor_event = 0 and
recent_trace.channel = requote_events.channel and
recent_trace.session_start = requote_events.session_start and
recent_trace.sn = requote_events.sn and
recent_trace.session_start = session.session_start ;
delete from trace where
rowid in (select rowid from trace order by rowid desc limit -1 offset 10000123);
+1 -1
View File
@@ -1 +1 @@
set(sources api/apicore.proto;api/apigateway.proto;api/apioptions.proto;api/core/ApiClientInterop.cpp;api/core/ApiServerInterop.cpp;api/public/ApiServer.cpp;api/public/ClientChannel.cpp;api/public/QuickbusClientChannel.cpp;api/public/ServerChannel.cpp;api/public/ServiceContext.cpp;api/service_helpers/AdminServiceUtils.cpp;api/services/AdminService.proto;api/services/AlgoDefinitionService.proto;api/services/AlgoService.proto;api/services/ApiUnitTestService.proto;api/services/AuthService.proto;api/services/ErrorReportService.proto;api/services/HelloService.proto;api/services/MarketDataAuxiliaryService.proto;api/services/MarketDataRelay.proto;api/services/OrderService.proto;api/services/OutOfProcessURLListener.proto;api/services/PersistenceService.proto;api/services/PositionService.proto;api/services/ShellDiagnosticService.proto;api/services/ShellLatencyService.proto;api/services/SyntheticSecurityService.proto;api/test/ApiTests.cpp;api/test/HelloServiceServer.cpp;config/ConfigCheck.cpp;config/ConfigDownloader.cpp;config/ConfigErrorCollector.cpp;config/ConfigLog.cpp;config/ConfigPropertyEvaluator.cpp;config/ConfigValueQueue.cpp;config/ConfigurationException.cpp;config/ConfigurationState.cpp;config/PreprocessInputStream.cpp;config/config.cpp;config/defaults/MarketData.conf;config/defaults/Ports.conf;config/defaults/Symbols.conf;config/defaults/cme_reference_data.conf;config/defaults/edge.conf;config/defaults/jetfix.conf;config/defaults/logging.conf;config/defaults/orderman_cfg.conf;config/defaults/orders_fills_cleanup.conf;config/defaults/spreader.conf;config/defaults/testkeys.conf;config/defaults/threads.conf;config/defaults/toggles.conf;config/extensions/extensions.proto;config/invalid/invalid.proto;config/protos/ExchangeSimCfg.proto;config/protos/MarketData.proto;config/protos/Ports.proto;config/protos/Symbols.proto;config/protos/UI.proto;config/protos/cme_reference_data.proto;config/protos/jetfix.proto;config/protos/logging.proto;config/protos/mercury_cfg.proto;config/protos/orderman_cfg.proto;config/protos/orders_fills_cleanup.proto;config/protos/perflog_cfg.proto;config/protos/quickbus_cfg.proto;config/protos/spreader.proto;config/protos/system.proto;config/protos/testkeys.proto;config/protos/threads.proto;config/protos/toggles.proto;config/tests/HTTPServer.cpp;config/tests/config_ref_tests.cpp;config/tests/config_tests.cpp;config/text_format/text_format.cpp;domain_model/Security.cpp;domain_model/Security.proto;domain_model/SecurityTests.cpp;domain_model/tests/domain_tests.cpp;expression/BooleanGrammar.cpp;expression/ExpressionEvaluator.cpp;expression/ParserExceptions.cpp;expression/Value.cpp;expression/tests/expression_parser_tests.cpp;jetfix/codec/FIXArray.cpp;jetfix/common/OutFIX.cpp;jetfix/connection/AsyncReader.cpp;jetfix/connection/IConnection.cpp;jetfix/connection/SocketConnection.cpp;jetfix/gateway/CoreGateway.cpp;jetfix/gateway/JetFIXGateway.cpp;jetfix/gateway/Session.cpp;jetfix/store/FIXFileStore.cpp;jetfix/store/session_name.cpp;jetfix/tests/async_reader_tests.cpp;jetfix/tests/decoding_tests.cpp;jetfix/tests/fix_encoding_stream_tests.cpp;jetfix/tests/fix_fragment_tests.cpp;jetfix/tests/jetfix_performance_tests.cpp;jetfix/tests/sample_fix.cpp;jetfix/tests/store_tests.cpp;logging/LoggerHub.cpp;logging/Sink.cpp;logging/logging.cpp;logging/tests/logging_tests.cpp;md/Diag/MDPeriodicDumper.cpp;md/Diag/ShmDumper.cpp;md/MDConsumer.cpp;md/MDConsumerHandle.cpp;md/MDConsumerInterop.cpp;md/MDProducer.cpp;md/MDProducerHandle.cpp;md/MDRecoverableConsumer.cpp;md/MemorySegmentUtils.cpp;md/PlatformLoader.cpp;md/PriceSize.cpp;md/SecurityDB.cpp;md/SecurityIDRecorder.cpp;md/SettlementRecorder.cpp;md/Shm/FixedSecurity.cpp;md/SyntheticSecurityService.cpp;md/cme/MDP3Utils.cpp;md/common.cpp;md/protos/co2.proto;md/relay/QueryServer.cpp;md/relay/RelayClient.cpp;md/relay/RelayRecorder.cpp;md/test/Co2ManagedTestHelper.cpp;md/test/Co2TestInstrument.cpp;md/test/md2_tests.cpp;md/test/synthetic_book_merge_tests.cpp;mercury/actors/IPollableActor.cpp;mercury/broker/Broker.cpp;mercury/broker/BrokerProxy.cpp;mercury/broker/ILocalBroker.cpp;mercury/broker/IRemoteBroker.cpp;mercury/common/mercury_common.cpp;mercury/events/TimerEvent.cpp;mercury/executor/Executor.cpp;mercury/topics/Topic.cpp;metrics/metrics.cpp;metrics/system_metrics.cpp;orderman/actors/AsyncFactory.cpp;orderman/actors/ExecutionGateway.cpp;orderman/actors/FillStore.cpp;orderman/actors/MDSupplementarySource.cpp;orderman/actors/OrderStore.cpp;orderman/actors/SODPosition.cpp;orderman/actors/SecuritySrc.cpp;orderman/actors/ServiceBridge.cpp;orderman/actors/order_manager/OrderBuilder.cpp;orderman/actors/order_manager/OrderManager.cpp;orderman/actors/order_manager/OrderServicesData.cpp;orderman/events/CancelRejectEvent.cpp;orderman/events/NewExecutionReportEvent.cpp;orderman/limiter/LimitStore.cpp;orderman/limiter/Limiter.cpp;orderman/limiter/ProductLimitNode.cpp;orderman/limiter/ProductLimitTree.cpp;orderman/limiter/SecurityLimitNode.cpp;orderman/limiter/SecurityLimitTreLine truncated
set(sources api/apicore.proto;api/apigateway.proto;api/apioptions.proto;api/core/ApiClientInterop.cpp;api/core/ApiServerInterop.cpp;api/public/ApiServer.cpp;api/public/ClientChannel.cpp;api/public/QuickbusClientChannel.cpp;api/public/ServerChannel.cpp;api/public/ServiceContext.cpp;api/service_helpers/AdminServiceUtils.cpp;api/service_helpers/AlgoServiceUtils.cpp;api/services/AdminService.proto;api/services/AlgoDefinitionService.proto;api/services/AlgoService.proto;api/services/ApiUnitTestService.proto;api/services/AuthService.proto;api/services/ErrorReportService.proto;api/services/HelloService.proto;api/services/MarketDataAuxiliaryService.proto;api/services/MarketDataRelay.proto;api/services/OrderService.proto;api/services/OutOfProcessURLListener.proto;api/services/PersistenceService.proto;api/services/PositionService.proto;api/services/ShellDiagnosticService.proto;api/services/ShellLatencyService.proto;api/services/SyntheticSecurityService.proto;api/test/ApiTests.cpp;api/test/HelloServiceServer.cpp;config/ConfigCheck.cpp;config/ConfigDownloader.cpp;config/ConfigErrorCollector.cpp;config/ConfigLog.cpp;config/ConfigPropertyEvaluator.cpp;config/ConfigValueQueue.cpp;config/ConfigurationException.cpp;config/ConfigurationState.cpp;config/PreprocessInputStream.cpp;config/config.cpp;config/defaults/MarketData.conf;config/defaults/Ports.conf;config/defaults/Symbols.conf;config/defaults/cme_reference_data.conf;config/defaults/edge.conf;config/defaults/jetfix.conf;config/defaults/logging.conf;config/defaults/orderman_cfg.conf;config/defaults/orders_fills_cleanup.conf;config/defaults/spreader.conf;config/defaults/testkeys.conf;config/defaults/threads.conf;config/defaults/toggles.conf;config/extensions/extensions.proto;config/invalid/invalid.proto;config/protos/ExchangeSimCfg.proto;config/protos/MarketData.proto;config/protos/Ports.proto;config/protos/Symbols.proto;config/protos/UI.proto;config/protos/cme_reference_data.proto;config/protos/jetfix.proto;config/protos/logging.proto;config/protos/mercury_cfg.proto;config/protos/orderman_cfg.proto;config/protos/orders_fills_cleanup.proto;config/protos/perflog_cfg.proto;config/protos/quickbus_cfg.proto;config/protos/spreader.proto;config/protos/system.proto;config/protos/testkeys.proto;config/protos/threads.proto;config/protos/toggles.proto;config/tests/HTTPServer.cpp;config/tests/config_ref_tests.cpp;config/tests/config_tests.cpp;config/text_format/text_format.cpp;domain_model/Security.cpp;domain_model/Security.proto;domain_model/SecurityTests.cpp;domain_model/tests/domain_tests.cpp;expression/BooleanGrammar.cpp;expression/ExpressionEvaluator.cpp;expression/ParserExceptions.cpp;expression/Value.cpp;expression/tests/expression_parser_tests.cpp;logging/LoggerHub.cpp;logging/Sink.cpp;logging/logging.cpp;logging/tests/logging_tests.cpp;md/Diag/MDPeriodicDumper.cpp;md/Diag/ShmDumper.cpp;md/MDConsumer.cpp;md/MDConsumerHandle.cpp;md/MDConsumerInterop.cpp;md/MDProducer.cpp;md/MDProducerHandle.cpp;md/MDRecoverableConsumer.cpp;md/MemorySegmentUtils.cpp;md/PlatformLoader.cpp;md/PriceSize.cpp;md/SecurityDB.cpp;md/SecurityIDRecorder.cpp;md/SettlementRecorder.cpp;md/Shm/FixedSecurity.cpp;md/SyntheticSecurityService.cpp;md/cme/MDP3Utils.cpp;md/common.cpp;md/protos/co2.proto;md/relay/QueryServer.cpp;md/relay/RelayClient.cpp;md/relay/RelayRecorder.cpp;md/test/Co2ManagedTestHelper.cpp;md/test/Co2TestInstrument.cpp;md/test/md2_tests.cpp;md/test/synthetic_book_merge_tests.cpp;os/os.cpp;os/tests/os_tests.cpp;quickbus/core/LocalNode.cpp;quickbus/core/Quickbus.cpp;quickbus/core/SubscriptionID.cpp;quickbus/core/TableID.cpp;quickbus/message_model/Update.cpp;quickbus/message_model/merge_updates.cpp;quickbus/network/QBConnection.cpp;quickbus/network/QBConnectionManager.cpp;quickbus/network/endpoint_description.cpp;quickbus/network/quickbus_msgs.proto;quickbus/pubsub/TablePublisher.cpp;quickbus/pubsub/TableSubscriber.cpp;quickbus/pubsub/tests/qb_pubsub_tests.cpp;quickbus/routing/INode.cpp;quickbus/routing/NodeID.cpp;quickbus/routing/Router.cpp;quickbus/rpc/RPCClient.cpp;quickbus/rpc/RPCServer.cpp;quickbus/rpc/RPCSessionID.cpp;quickbus/rpc/rpc_utils.cpp;quickbus/rpc/tests/qb_rpc_tests.cpp;quickbus/tests/quickbus_tests.cpp;sparkproto/SparkProtos.proto;testing/catch_test_runner.cpp;utils/AuthToken.cpp;utils/Dispatcher.cpp;utils/GrowableSPSCQueue.cpp;utils/HTTPStream.cpp;utils/PBUtils.cpp;utils/Puid.cpp;utils/SecureLogon.cpp;utils/SharedMemory.cpp;utils/Sqlite_utils.cpp;utils/Uuid.cpp;utils/datetime.cpp;utils/download.cpp;utils/file_utils.cpp;utils/init_spark_lib.cpp;utils/is_unittest_environment.cpp;utils/stats.cpp;utils/tests/AuthTokenTests.cpp;utils/thread_manager.cpp)
+1
View File
@@ -110,6 +110,7 @@ message CMEOptions
// Prevents parsing trades/rfqs/doing anything not required for quoting
optional bool quoter_mode = 6;
optional bool verbose_ticker = 7 [default=false];
}
message RecordOptions {
-1
View File
@@ -3,7 +3,6 @@ package bts.config;
message PerfLogCfg {
optional bool active = 1 [default=true];
optional uint32 recent_traces_size = 2 [default=140123];
optional uint32 max_rows = 3 [default=10000123];
optional uint32 entry_batch_size = 4 [default=200];
}
+10
View File
@@ -3,4 +3,14 @@ package bts.config;
message Spreader{
optional uint32 n_failed_iocs = 10 [default=20];
//if this is set to true, the spreader internally
//generates fake fills to keep the cache (data and
//instruction) warm
message CacheWarming{
optional bool enable_fills = 10 [default=true];
optional bool enable_child_order_activate = 15 [default=true];
optional double fill_period_low = 20 [default=10];
optional double fill_period_high = 30 [default=50];
}
optional CacheWarming cache_warming = 20;
}
@@ -109,7 +109,8 @@ int SocketConnection::write(WriteVec const &vec, size_t vec_size,
_perf_log.log(trace,"before_fix_socket_w");
auto result = ::writev(_fd, iv, (int)vec_size);
_perf_log.log(trace,"after_fix_socket_w");
_perf_log.flush();
if (trace.sn)
_perf_log.flush();
if (_log && result > 0) {
for (size_t i = 0; i < vec_size; i++)
+1 -1
View File
@@ -95,7 +95,7 @@ void configure_logging(bts::config::Logging const&cfg, bts::config::ThreadGroupC
real_cfg.logger_initial_queue_size = cfg.initial_queue_size();
real_cfg.logger_max_queue_growth = cfg.max_queue_growth();
real_cfg.level_names = { {Diag,"Diag"},{Info,"Info"},
real_cfg.level_names = { {NoLog,"NoLog"},{Diag,"Diag"},{Info,"Info"},
{Warn,"Warn"},{Error,"Error"},
{Fatal,"Fatal"},{Sys,"Sys"} };
+2 -1
View File
@@ -13,7 +13,8 @@ namespace bts {
namespace config {class Logging; class ThreadGroupCfg;}
// Please keep this list in sync with Log.cs
constexpr log::Level Diag {0};
constexpr log::Level NoLog {0};
constexpr log::Level Diag {NoLog.ordinal+1};
constexpr log::Level Info {Diag.ordinal+1};
constexpr log::Level Warn {Info.ordinal+1};
constexpr log::Level Error {Warn.ordinal+1};
+10 -2
View File
@@ -3,6 +3,8 @@
#include "lib/utils/TimeStamp.h"
#include "lib/utils/Dispatcher.h"
#include "lib/utils/datetime.h"
#include "lib/config/protos/system.pb.h"
#include "lib/utils/thread_manager.h"
#include <memory>
#include <string>
@@ -150,7 +152,11 @@ void write_header(PrintFormat format, long seq_mod, const std::vector<Entry>& en
}
}
void run_periodic_dumper(const std::set<UserSymbol>& symbols, uint32_t seq_mod, PrintFormat format) {
void run_periodic_dumper(const std::set<UserSymbol>& symbols, uint32_t seq_mod, PrintFormat format, bool monitor_all) {
auto qualifier = config::get<config::System>().qualifier();
auto thread_name = "tail-" + qualifier;
threading::configure_current_thread(thread_name);
MDConsumer consumer(symbols, nullptr);
@@ -160,7 +166,9 @@ void run_periodic_dumper(const std::set<UserSymbol>& symbols, uint32_t seq_mod,
entries.push_back({ 0, handle, std::make_unique<api::Security>(sec) });
});
entries = filter_entries(entries);
if (!monitor_all) {
entries = filter_entries(entries);
}
write_header(format, seq_mod, entries);
+2 -1
View File
@@ -10,7 +10,8 @@ enum class PrintFormat {
CSV
};
void run_periodic_dumper(const std::set<UserSymbol>&, uint32_t seq_mod, PrintFormat format);
void run_periodic_dumper(const std::set<UserSymbol>&, uint32_t seq_mod, PrintFormat format, bool monitor_all);
}}//ns
+151 -35
View File
@@ -1,6 +1,7 @@
#pragma once
#include "lib/logging/logging.h"
#include "lib/md/IntPrice.h"
#include "lib/md/MDConsumer.h"
#include "lib/md/MDConsumerHandle.h"
#include "lib/md/cme/MDP3Utils.h"
@@ -9,9 +10,13 @@
#include "lib/mercury/events/TimerEvent.h"
#include "lib/orderman/events/BookUpdateEvent.h"
#include "lib/orderman/events/MDRequestEvent.h"
#include "lib/orderman/events/TickerUpdateEvent.h"
#include "lib/tracing/PerfLog.h"
#include "lib/tracing/bts_trace.h"
#include "lib/utils/TimeStamp.h"
#include "lib/domain_model/Security.h"
#include <date/date.h>
namespace bts {
namespace orderman {
@@ -25,9 +30,10 @@ class MDSource : public mercury::Actor, public mercury::IPollableActor {
public:
MDSource(md::MDConsumer &consumer, metrics::MetricsContainer &stats,
trace::PerfLog &log)
: _handles(), _md_consumer(consumer), _book_update(), _perf_log(log),
_stats(stats), _loop_timer() {
: _handles(), _md_consumer(consumer), _book_update(), _ticker_update(),
_perf_log(log), _stats(stats), _loop_timer() {
_trades_buffer.Reserve(1000);
auto cme_channels = md::get_configured_cme_channels();
for (auto const &c : cme_channels)
for (auto const &s : c.symbols)
@@ -56,47 +62,43 @@ class MDSource : public mercury::Actor, public mercury::IPollableActor {
auto synthetic_count_metric =
std::make_shared<metrics::CounterMetric<int>>(
"spark_mdsource_synthetic_count");
auto ticker_channel_cout_metric =
std::make_shared<metrics::CounterMetric<int>>(
"spark_mdsource_ticker_channel_count");
_stats.publish(security_count_metric);
_stats.publish(synthetic_count_metric);
_stats.publish(ticker_channel_cout_metric);
_book_update = make_stream<BookUpdateEvent>();
subscribe<MDRequestEvent>([this, security_count_metric,
synthetic_count_metric](auto const &update) {
if (update.events) {
for (auto const &ev : *update.events) {
auto k = ev.security_key.value();
_ticker_update = make_stream<TickerUpdateEvent>();
subscribe<MDRequestEvent>(
[this, security_count_metric, synthetic_count_metric,
ticker_channel_cout_metric](auto const &update) {
if (update.events) {
for (auto const &ev : *update.events) {
auto k = ev.security_key;
for (auto &h : _handles)
if (h.handle.key() == k) {
// we want to resend this MD so that late joiners can
// catch up
h.last = utils::TimeStamp();
return;
}
switch (ev.md_type) {
case MDRequestType::BOOK:
on_book_sub(k, *synthetic_count_metric);
security_count_metric->set(_handles.size());
break;
case MDRequestType::TICKER:
on_ticker_sub(k, *ticker_channel_cout_metric);
break;
}
}
}
});
LOG(OrderMan, Warn,
"Starting to publish book updates for security key: {}", k);
auto handle = _md_consumer.get_handle(k);
auto &channel = _symbol_to_channel[handle.get_usersymbol()];
if (channel.empty())
channel = "sim_md";
_handles.push_back(
{utils::TimeStamp(), handle, channel.c_str()});
if (handle.is_synthetic()) {
synthetic_count_metric->increment();
}
}
security_count_metric->set(_handles.size());
}
});
security_count_metric->set(_handles.size());
}
private:
void on_poll() override {
Book book;
// Book Updates
Book book;
auto start = utils::TimeStamp::now();
for (auto &h : _handles) {
auto &handle = h.handle;
@@ -104,24 +106,138 @@ class MDSource : public mercury::Actor, public mercury::IPollableActor {
&book.bid()[0],
&book.ask()[0]) == md::HandleReadResult::SUCCESS) {
book._security_key = SecurityKey(handle.key());
trace::CorID trace{h.channel, handle.sequence()};
trace::CorID trace{h.external_event_channel, handle.sequence()};
_perf_log.log(trace, h.last, "md", handle.key());
_perf_log.log(trace, "md_r", h.last, h.symbol->c_str(),
handle.key());
publish(_book_update, book, trace);
}
}
// Ticker Updates
for (auto &c : _channels) {
auto &channel = c.channel;
_trades_buffer.Clear();
c.cursor = channel.read_trades(_trades_buffer, c.cursor);
for (auto const &trade : _trades_buffer) {
size_t trade_count = 0;
for (auto &per_sec_ev : c.securities) {
if (trade.key() == per_sec_ev.key().value()) {
trade_count++;
per_sec_ev.trades.emplace_back(TickerUpdateEvent::Trade{
datetime::utc_ll_to_timepoint(trade.time()),
Price(md::IntPrice(trade.mantissa(),
(int8_t)trade.exponent())
.price()),
Quantity(trade.size()), Quantity(trade.total_volume()),
trade.aggressor_side()});
}
}
if (trade_count)
for (auto &per_sec_ev : c.securities)
if (!per_sec_ev.trades.empty()) {
publish(_ticker_update, per_sec_ev);
per_sec_ev.trades.clear();
}
}
}
// Publish stats
auto stop = utils::TimeStamp::now();
_loop_timer->observe((start - stop).nanoseconds());
}
void on_book_sub(SecurityKey sk,
metrics::CounterMetric<int> &synthetic_count_metric) {
auto k = sk.value();
for (auto &h : _handles)
if (h.handle.key() == k) {
// we want to resend this MD so that late joiners can
// catch up
h.last = utils::TimeStamp();
return;
}
LOG(OrderMan, Warn,
"Starting to publish book updates for security key: {}", k);
auto handle = _md_consumer.get_handle(k);
_handles.push_back({utils::TimeStamp(), handle,
get_external_event_channel(handle.get_usersymbol()),
std::make_unique<std::string>(securities::descriptor(
handle.get_normalized_security()))});
if (handle.is_synthetic())
synthetic_count_metric.increment();
}
const char *get_external_event_channel(const md::UserSymbol &user_symbol) {
auto &channel = _symbol_to_channel[user_symbol];
if (channel.empty())
channel = "sim_md";
return channel.c_str();
}
void on_ticker_sub(SecurityKey sk,
metrics::CounterMetric<int> &channel_count_metric) {
auto user_symbol = _md_consumer.get_handle(sk.value()).get_usersymbol();
for (auto &ch : _channels)
if (ch.user_symbol == user_symbol) {
auto it =
std::find_if(ch.securities.begin(), ch.securities.end(),
[sk](auto const &ev) { return ev.key() == sk; });
if (it == ch.securities.end()) {
// first time we are asked for this security, we will
// publish the trades for it next time next round
LOG(OrderMan, Warn,
"Starting to publish ticker updates on user symbol {} for "
"security key: {} (sec count for the symbol: {} )",
user_symbol.to_string(), sk.value(),
ch.securities.size() + 1);
ch.securities.push_back(sk);
}
return;
}
// this is a channel we are not subscribed to
channel_count_metric.increment();
LOG(OrderMan, Warn,
"Starting to publish ticker updates on user symbol {} for security "
"key: {}",
user_symbol.to_string(), sk.value());
// We do not want to get old trades so we drain the trades buffer at
// this point. This makes the first subscription to a channel
// consistent with subsquent ones in terms of late join semantics.
ChannelRecord c{
0, _md_consumer.get_channel(user_symbol), {}, user_symbol};
_trades_buffer.Clear();
c.cursor = c.channel.read_trades(_trades_buffer, c.cursor);
c.securities.push_back(sk);
_channels.push_back(c);
}
struct HandleRecord {
utils::TimeStamp last;
md::MDConsumerHandle handle;
const char *channel;
const char *external_event_channel;
std::unique_ptr<std::string> symbol;
};
boost::container::small_vector<HandleRecord, 100> _handles;
struct ChannelRecord {
int64_t cursor;
co2::MDConsumerChannel channel;
boost::container::small_vector<TickerUpdateEvent, 5> securities;
md::UserSymbol user_symbol;
};
boost::container::small_vector<ChannelRecord, 3> _channels;
google::protobuf::RepeatedPtrField<co2::TradeMessage> _trades_buffer;
std::vector<HandleRecord> _handles;
md::MDConsumer _md_consumer;
mercury::Stream<BookUpdateEvent> _book_update;
mercury::Stream<TickerUpdateEvent> _ticker_update;
trace::PerfLog &_perf_log;
metrics::MetricsContainer &_stats;
std::shared_ptr<metrics::HistogramMetric<30>> _loop_timer;
@@ -39,6 +39,11 @@ class ChildOrder {
bool is_active() const { return _active; }
void warm_up_activate() {
bts_assert(!_active);
_parent_order_services->warm_up_activate_child(*_order);
}
void activate(Price p, Quantity q, trace::CorID const &trace = trace::CorID()) {
bts_assert(_order);
bts_assert(!_active);
@@ -407,13 +407,18 @@ void OrderManager::init_order_services() {
// md subs are peculiar in that we try to make them on demand, so
// we publish an event announcing the subscription
std::function<void(SecurityKey)> on_md_sub = [this](SecurityKey k) {
publish(me->services_data.md_request_pub, k);
publish(me->services_data.md_request_pub, k,MDRequestType::BOOK);
};
std::function<void(SecurityKey)> on_ticker_sub = [this](SecurityKey k) {
publish(me->services_data.md_request_pub, k,MDRequestType::TICKER);
};
std::function<void(SecurityKey)> on_settlement_sub = [this](SecurityKey k) {
publish(me->services_data.settlement_request_pub, k);
};
me->services_data.md_subs =
std::make_unique<ContentSubscription<BookUpdateEvent>>(*this, on_md_sub);
me->services_data.ticker_subs =
std::make_unique<ContentSubscription<TickerUpdateEvent>>(*this, on_ticker_sub);
me->services_data.settlement_subs =
std::make_unique<ContentSubscription<SettlementUpdateEvent>>(
*this, on_settlement_sub);
@@ -5,6 +5,7 @@
#include "lib/logging/logging.h"
#include "lib/orderman/actors/order_manager/OrderManager.h"
#include "lib/orderman/actors/order_manager/OrderServicesData.h"
#include "lib/orderman/events/TickerUpdateEvent.h"
#include "lib/orderman/governor/Governor.h"
#include "lib/orderman/orderman_utils/EventStream.h"
#include "lib/orderman/orders/Order.h"
@@ -108,8 +109,8 @@ class OrderServices {
OrderServices(Order &owner)
: _busy(false), _pending_calls(), _owner(&owner), _data(),
_parent(nullptr), _child_update_cb([](auto const &) {}),
_book_updates(), _settlement_updates(), _time_change(), _sessions(),
_create_order_reply() {}
_book_updates(), _ticker_updates(), _settlement_updates(),
_time_change(), _sessions(), _create_order_reply() {}
void init(OrderServicesData &data, OrderServices &parent) {
_parent = &parent;
@@ -119,10 +120,12 @@ class OrderServices {
void init(OrderServicesData &data) {
_data = &data;
_book_updates.init(*data.md_subs);
_ticker_updates.init(*data.ticker_subs);
_settlement_updates.init(*data.settlement_subs);
_time_change.init(*data.time_subs);
for (auto &sd : data.sessions)
_sessions.emplace_back(*sd);
if (_sessions.empty())
for (auto &sd : data.sessions)
_sessions.emplace_back(*sd);
_create_order_reply.init(*data.order_creation_subs);
}
@@ -147,17 +150,19 @@ class OrderServices {
ss.queue_fix(mt, f, trace);
}
void log(trace::CorID cid, const char *entry_kind,
uint64_t uint64_1 = 0,const char *txt_1 = "",uint32_t uint32_1 = 0) {
void log(trace::CorID cid, const char *entry_kind, uint64_t uint64_1 = 0,
const char *txt_1 = "", uint32_t uint32_1 = 0) {
_data->log.log(cid, entry_kind, uint64_1, txt_1, uint32_1);
}
trace::ScopedPerfLogEntry log_scope(trace::CorID cid, const char *entry_kind,
uint64_t uint64_1 = 0,const char *txt_1 = "",uint32_t uint32_1 = 0)
{
return trace::ScopedPerfLogEntry(_data->log,cid,entry_kind,uint64_1,txt_1,uint32_1);
uint64_t uint64_1 = 0,
const char *txt_1 = "",
uint32_t uint32_1 = 0) {
return trace::ScopedPerfLogEntry(_data->log, cid, entry_kind, uint64_1,
txt_1, uint32_1);
}
void report_state(const api::orderman::OrderState &state,
trace::CorID const &trace = trace::CorID()) {
@@ -172,6 +177,7 @@ class OrderServices {
_owner->key().value());
unsubscribe_from_child_updates();
_book_updates.unsub_all();
_ticker_updates.unsub_all();
_settlement_updates.unsub_all();
_time_change.unsub_all();
_create_order_reply.unsub_all();
@@ -199,7 +205,13 @@ class OrderServices {
k, [this, cb](auto const &ev) { this->on_external_event(ev, cb); });
}
void unsubscribe_from_book(BookSubID sid) { _book_updates.unsub(sid); }
// Ticker
using TickerSubID = EventStream<TickerUpdateEvent>::SubID;
template <class CB> TickerSubID subscribe_to_ticker(SecurityKey k, CB &&cb) {
return _ticker_updates.sub(
k, [this, cb](auto const &ev) { this->on_external_event(ev, cb); });
}
void unsubscribe_from_ticker(TickerSubID sid) { _ticker_updates.unsub(sid); }
// Settlement
using SettlementSubID = EventStream<SettlementUpdateEvent>::SubID;
template <class CB>
@@ -223,8 +235,8 @@ class OrderServices {
// Execution Reports
using ExeReportSubID = EventStream<NewExecutionReportEvent>::SubID;
template <class CB>
void subscribe_to_exe_report(SessionServices &ss, OrderKey k, CB &&cb) {
ss.subscribe_to_exe_report(
ExeReportSubID subscribe_to_exe_report(SessionServices &ss, OrderKey k, CB &&cb) {
return ss.subscribe_to_exe_report(
k, [this, cb](auto const &ev) { this->on_external_event(ev, cb); });
}
void unsubscribe_from_exe_report(SessionServices &ss, ExeReportSubID sid) {
@@ -321,8 +333,13 @@ class OrderServices {
OrderServices &activate_child(Order &o, Price p, Quantity q,
trace::CorID const &trace) {
return _data->owner.activate_child(*this, o, p, q, trace);
return _data->owner.activate_child(*this, o, p, q, trace);
}
void warm_up_activate_child(Order &o) {
o.warm_up_activate(*_data,*this);
}
void update(Price p, Quantity q, trace::CorID const &trace) {
on_inter_order_call([p, q, trace](OrderServices &self) {
self._owner->on_update(p, q, trace);
@@ -350,6 +367,7 @@ class OrderServices {
OrderServices *_parent;
std::function<void(OrderStateUpdateEvent const &)> _child_update_cb;
EventStream<BookUpdateEvent> _book_updates;
EventStream<TickerUpdateEvent> _ticker_updates;
EventStream<SettlementUpdateEvent> _settlement_updates;
EventStream<mercury::TimerEvent, mercury::NoKey> _time_change;
boost::container::small_vector<SessionServices, 20> _sessions;
@@ -11,6 +11,7 @@
#include "lib/orderman/actors/ExecutionGateway.h"
#include "lib/orderman/events/ActivateOrderEvent.h"
#include "lib/orderman/events/BookUpdateEvent.h"
#include "lib/orderman/events/TickerUpdateEvent.h"
#include "lib/orderman/events/CancelOrderEvent.h"
#include "lib/orderman/events/CancelRejectEvent.h"
#include "lib/orderman/events/CrackOrderEvent.h"
@@ -83,6 +84,7 @@ class OrderServicesData {
Limiter limiter;
OrderFactory order_factory;
std::unique_ptr<mercury::ContentSubscription<BookUpdateEvent>> md_subs;
std::unique_ptr<mercury::ContentSubscription<TickerUpdateEvent>> ticker_subs;
std::unique_ptr<mercury::ContentSubscription<SettlementUpdateEvent>>
settlement_subs;
std::unique_ptr<
+13 -7
View File
@@ -2,23 +2,29 @@
#include <memory>
#include "lib/orderman/orderman_utils/orderman_common.h"
#include "lib/mercury/events/Event.h"
#include "lib/orderman/orderman_utils/orderman_common.h"
namespace bts {
namespace orderman {
enum class MDRequestType { BOOK, TICKER };
class MDRequestEvent : public mercury::Event<MDRequestEvent> {
public:
public:
using SynopsisPolicy = mercury::CacheAll<MDRequestEvent>;
MDRequestEvent(SecurityKey k) : security_key(k) {}
MDRequestEvent &operator=(MDRequestEvent const&rhs)
{
MDRequestEvent(SecurityKey k, MDRequestType md_type)
: security_key(k), md_type(md_type) {}
MDRequestEvent &operator=(MDRequestEvent const &rhs) {
security_key = rhs.security_key;
md_type = rhs.md_type;
return *this;
}
SecurityKey security_key;
MDRequestType md_type;
};
}}
} // namespace orderman
} // namespace bts
@@ -0,0 +1,35 @@
#pragma once
#include <memory>
#include "lib/mercury/events/Event.h"
#include "lib/orderman/orderman_utils/orderman_common.h"
#include "lib/utils/bts_chrono.h"
#include "lib/md/protos/co2.pb.h"
namespace bts {
namespace orderman {
class TickerUpdateEvent : public mercury::Event<TickerUpdateEvent> {
SecurityKey _key;
public:
using SynopsisPolicy = mercury::NoSynopsis;
SecurityKey key() const {return _key;};
struct Trade {
TimeStamp ts;
Price price;
Quantity quantity;
Quantity total_volume;
co2::AggressorSide aggressor_side;
};
boost::container::small_vector<Trade,5> trades;
TickerUpdateEvent(SecurityKey key) : _key(key), trades() {}
};
}
}
@@ -72,8 +72,22 @@ Order &OrderFactory::create(OrderKey parent,
auto k = gen_order_id();
auto pk = parent;
auto &order = allocate(k, pk, def);
order.on_init_new(def,
PreActivationOrderServices{me->sec_db, *this, me->router});
// For non native orders we force the ManalOrderIndicator to be 'N'
// since all non native order are algos and we would rather not
// rely on the order creator (be it shell or another algo) to set
// this correctly for every single algo.
if (dynamic_cast<NativeOrder *>(&order) == nullptr &&
def.manual_order_indicator() ==
api::orderman::MANUAL_ORDER_INDICATOR_YES) {
auto corrected_def = def;
corrected_def.set_manual_order_indicator(
api::orderman::MANUAL_ORDER_INDICATOR_NO);
order.on_init_new(corrected_def, PreActivationOrderServices{
me->sec_db, *this, me->router});
} else
order.on_init_new(
def, PreActivationOrderServices{me->sec_db, *this, me->router});
return order;
}
@@ -18,7 +18,6 @@ struct EventStream
void init(CS&csub)
{
bts_assert(!cs);
cs = &csub;
}
+14 -5
View File
@@ -17,8 +17,14 @@ using namespace api::orderman;
struct IcebergOrder::ActiveChild {
ActiveChild() {}
ChildOrder order;
api::orderman::OrderState state;
void clear() { state = api::orderman::OrderState(); }
OrderKey key;
void clear() {
state = api::orderman::OrderState();
key = null_order_key();
}
};
enum class IcebergOrder::WorkingSubStatus { WORKING, PAUSED };
@@ -55,7 +61,7 @@ void IcebergOrder::on_init_new(api::orderman::OrderDefinition const &def,
auto const &iceberg_def = def.custom_def().iceberg();
me->show_quantity = Quantity(iceberg_def.show_quantity());
me->pause_between_orders =
me->pause_between_orders =
config::get<config::OrdermanCfg>().default_iceberg_pause_secs();
auto child_def = def;
@@ -248,11 +254,14 @@ void IcebergOrder::send_next_child_order() {
auto remaining = me->state.quantity() - me->state.filled_quantity();
auto to_send = std::min(remaining, me->show_quantity.value());
me->child.order = me->child_builder.build();
me->child.key = me->child.order.key();
LOG(OrderMan, Info,
"Iceberg order {} creating new child order {} with size "
"{} price {}. Filled {} so far out of {}",
key().value(), me->child.order.key().value(), to_send, me->state.price(),
key().value(), me->child.key.value(), to_send, me->state.price(),
me->state.filled_quantity(), me->state.quantity());
me->child.order.activate(Price(me->state.price()), Quantity(to_send), trace::CorID());
@@ -261,7 +270,7 @@ void IcebergOrder::send_next_child_order() {
void IcebergOrder::on_child_order_update(OrderStateUpdateEvent const &ev) {
switch (me->state.status()) {
case ORDER_STATUS_WORKING:
if (ev.key == me->child.order.key()) {
if (ev.key == me->child.key) {
me->child.state = ev.rebuild_state();
switch (ev.status) {
@@ -313,7 +322,7 @@ void IcebergOrder::on_child_order_update(OrderStateUpdateEvent const &ev) {
LOG(OrderMan, Info,
"Iceberg order {} received update for non active child order {}."
"update state = {}",
key().value(), me->child.order.key().value(),
key().value(), me->child.key.value(),
utils::pb::to_string(ev.rebuild_state()));
}
break;
+128 -106
View File
@@ -58,6 +58,9 @@ struct NativeOrder::PImpl {
jetfix::FIXFragment cancel_encoding;
jetfix::FIXFragment::Bookmark<jetfix::fix::TransactTimeStr>
cancel_transact_time_bm;
OrderServices::ExeReportSubID exe_report_sid;
OrderServices::CancelRejectSubID cancel_reject_sid;
};
NativeOrder::NativeOrder() : me(new PImpl()) {}
@@ -272,12 +275,133 @@ void NativeOrder::on_init_from_exe_report(
NativeOrder::~NativeOrder() {}
void NativeOrder::on_activate(Price p, Quantity q,
bts::orderman::OrderServices &services,
trace::CorID const &trace) {
void NativeOrder::_on_activate(Price p, Quantity q,
bts::orderman::OrderServices &services,
trace::CorID const &trace) {
auto scoped_log =
services.log_scope(trace, "NativeOrder::on_activate", key().value());
me->state.set_price(p.value());
me->state.set_quantity(q.value());
me->state.set_filled_quantity(0);
me->state.set_last_price(0);
me->state.set_avg_price(0);
me->session_services = &me->services->get_session_services(
get_native_state(me->state).routing_record().session());
me->price_encoding_precision =
me->services->price_encoding_precision(*me->session_services);
// is our gateway connected?
if (!me->services->is_gateway_connected(*me->session_services)) {
LOG(OrderMan, Warn,
"Attempt to send order to a disconnected gateway. "
"Dropping order {}",
key().value());
transition(ORDER_STATUS_REJECTED_BY_SPARK, "Exchange gateway is down");
return;
}
// can we trade?
auto lqr = me->services->limiter().can_trade(
UserID(me->definition.user_id()),
AccountID(me->definition.account_id()));
if (!lqr.ok) {
LOG(OrderMan, Warn,
"Unauthorized order attempted. User: {} on "
"Account: {}. Msg: {} Order: {}. Dropping order",
me->definition.user(), me->definition.account(), lqr.error_msg,
key().value());
transition(ORDER_STATUS_REJECTED_BY_SPARK,
fmt::format("Unauthorized order: {}", lqr.error_msg));
return;
}
// can we trade this security, with this size?
lqr = me->services->limiter().can_send_new_order(
AccountID(me->definition.account_id()), *me->sec, me->definition.side(),
q);
if (!lqr.ok) {
LOG(OrderMan, Warn,
"New order would breach limit. User: {} Account: "
"{} Msg: {} Order: {}. Dropping order",
me->definition.user(), me->definition.account(), lqr.error_msg,
key().value());
transition(ORDER_STATUS_REJECTED_BY_SPARK,
fmt::format("Order would breach limit: {}", lqr.error_msg));
return;
}
// are we under the messaging threshold?
if (!me->services->governor(*me->session_services).can_send()) {
LOG(OrderMan, Warn,
"New order would cause breach of messaging "
"limits. Dropping order {}",
key().value());
transition(ORDER_STATUS_REJECTED_BY_SPARK,
"Messaging threshold breached");
return;
}
// if we are here we are good.
// subscribe to usefull events
me->exe_report_sid = me->services->subscribe_to_exe_report(
*me->session_services, key(),
[this](auto const &r) { this->on_exe_report(r); });
me->cancel_reject_sid = me->services->subscribe_to_cancel_reject(
*me->session_services, key(),
[this](auto const &cr) { this->on_cancel_reject(cr); });
me->services->subscribe_to_gateway_status(
*me->session_services,
[this](auto const &gw) { this->on_gateway_status(gw); });
// send out order
auto &encoding = me->create_encoding;
switch (me->definition.custom_def().custom_case()) {
case CustomOrderDefinition::kLimit:
case CustomOrderDefinition::kStopLimit:
me->create_price_bm = encoding.overwrite(
me->create_price_bm,
securities::encode_exchange_price(*me->sec, p).value(),
me->price_encoding_precision, price_sz());
break;
case CustomOrderDefinition::kStop:
case CustomOrderDefinition::kMarket:
break;
default:
bts_assert(false);
break;
}
me->create_quantity_bm =
encoding.overwrite(me->create_quantity_bm, q.value(), quantity_sz());
auto const &ts = services.get_time_str();
me->create_transact_time_bm = encoding.overwrite(
me->create_transact_time_bm, &ts.value()[0], ts.value().size());
}
void NativeOrder::warm_up_activate(OrderServicesData&data, OrderServices& services) {
bts_assert(!me->services);
me->services = &services;
init_order_services(data,services);
_on_activate(Price(0),Quantity(0),services, trace::CorID{});
me->services->unsubscribe_from_exe_report(
*me->session_services,me->exe_report_sid);
me->services->unsubscribe_from_cancel_reject(
*me->session_services,me->cancel_reject_sid);
me->services->unsubscribe_from_gateway_status(
*me->session_services);
me->services = nullptr;
}
void NativeOrder::on_activate(Price p, Quantity q,
bts::orderman::OrderServices &services,
trace::CorID const &trace) {
bts_assert(!me->services);
me->services = &services;
@@ -286,109 +410,7 @@ void NativeOrder::on_activate(Price p, Quantity q,
case ORDER_STATUS_INACTIVE: {
// this is a brand new order
me->state.set_price(p.value());
me->state.set_quantity(q.value());
me->state.set_filled_quantity(0);
me->state.set_last_price(0);
me->state.set_avg_price(0);
me->session_services = &me->services->get_session_services(
get_native_state(me->state).routing_record().session());
me->price_encoding_precision =
me->services->price_encoding_precision(*me->session_services);
// is our gateway connected?
if (!me->services->is_gateway_connected(*me->session_services)) {
LOG(OrderMan, Warn,
"Attempt to send order to a disconnected gateway. "
"Dropping order {}",
key().value());
transition(ORDER_STATUS_REJECTED_BY_SPARK, "Exchange gateway is down");
break;
}
// can we trade?
auto lqr = me->services->limiter().can_trade(
UserID(me->definition.user_id()),
AccountID(me->definition.account_id()));
if (!lqr.ok) {
LOG(OrderMan, Warn,
"Unauthorized order attempted. User: {} on "
"Account: {}. Msg: {} Order: {}. Dropping order",
me->definition.user(), me->definition.account(), lqr.error_msg,
key().value());
transition(ORDER_STATUS_REJECTED_BY_SPARK,
fmt::format("Unauthorized order: {}", lqr.error_msg));
break;
}
// can we trade this security, with this size?
lqr = me->services->limiter().can_send_new_order(
AccountID(me->definition.account_id()), *me->sec,
me->definition.side(), q);
if (!lqr.ok) {
LOG(OrderMan, Warn,
"New order would breach limit. User: {} Account: "
"{} Msg: {} Order: {}. Dropping order",
me->definition.user(), me->definition.account(), lqr.error_msg,
key().value());
transition(ORDER_STATUS_REJECTED_BY_SPARK,
fmt::format("Order would breach limit: {}", lqr.error_msg));
break;
}
// are we under the messaging threshold?
if (!me->services->governor(*me->session_services).can_send()) {
LOG(OrderMan, Warn,
"New order would cause breach of messaging "
"limits. Dropping order {}",
key().value());
transition(ORDER_STATUS_REJECTED_BY_SPARK,
"Messaging threshold breached");
break;
}
// if we are here we are good.
// subscribe to usefull events
me->services->subscribe_to_exe_report(
*me->session_services, key(),
[this](auto const &r) { this->on_exe_report(r); });
me->services->subscribe_to_cancel_reject(
*me->session_services, key(),
[this](auto const &cr) { this->on_cancel_reject(cr); });
me->services->subscribe_to_gateway_status(
*me->session_services,
[this](auto const &gw) { this->on_gateway_status(gw); });
// send out order
auto &encoding = me->create_encoding;
switch (me->definition.custom_def().custom_case()) {
case CustomOrderDefinition::kLimit:
case CustomOrderDefinition::kStopLimit:
me->create_price_bm = encoding.overwrite(
me->create_price_bm,
securities::encode_exchange_price(*me->sec, p).value(),
me->price_encoding_precision, price_sz());
break;
case CustomOrderDefinition::kStop:
case CustomOrderDefinition::kMarket:
break;
default:
bts_assert(false);
break;
}
me->create_quantity_bm =
encoding.overwrite(me->create_quantity_bm, q.value(), quantity_sz());
auto const &ts = services.get_time_str();
me->create_transact_time_bm = encoding.overwrite(
me->create_transact_time_bm, &ts.value()[0], ts.value().size());
_on_activate(p,q,services,trace);
me->services->queue_fix(*me->session_services,
jetfix::MessageTypes::NewOrderSingle,
me->create_encoding, trace);
+37 -25
View File
@@ -1,9 +1,9 @@
#pragma once
#include <memory>
#include "lib/orderman/orders/Order.h"
#include "lib/api/services/OrderService.pb.h"
#include "lib/orderman/orders/Order.h"
#include "lib/tracing/PerfLog.h"
#include <memory>
namespace bts {
namespace orderman {
@@ -13,45 +13,57 @@ class CancelRejectEvent;
class GatewayStatusChangeEvent;
class ExecutionReport;
class NativeOrder : public Order
{
public:
class NativeOrder : public Order {
public:
NativeOrder();
~NativeOrder() override;
api::orderman::OrderDefinition const &definition() const override;
void on_init_new(api::orderman::OrderDefinition const&,
const PreActivationOrderServices&) override;
void on_init_recovered(api::orderman::OrderRecord const&,
void on_init_new(api::orderman::OrderDefinition const &,
const PreActivationOrderServices &) override;
void on_init_recovered(api::orderman::OrderRecord const &,
const PreActivationOrderServices &) override;
void on_activate(Price,Quantity,OrderServices &, trace::CorID const&) override ;
void on_update(Price , Quantity,trace::CorID const&) override ;
void on_crack(trace::CorID const&) override ;
void on_cancel(trace::CorID const&) override ;
void on_activate(Price, Quantity, OrderServices &,
trace::CorID const &) override;
void on_update(Price, Quantity, trace::CorID const &) override;
void on_crack(trace::CorID const &) override;
void on_cancel(trace::CorID const &) override;
void on_init_from_exe_report(ExecutionReport const&,const PreActivationOrderServices &);
private:
void on_init_from_exe_report(ExecutionReport const &,
const PreActivationOrderServices &);
protected:
void warm_up_activate(OrderServicesData &,OrderServices&) override;
private:
struct PImpl;
std::unique_ptr<PImpl> me;
void _on_activate(Price, Quantity, bts::orderman::OrderServices &,
trace::CorID const &);
void on_exe_report(const NewExecutionReportEvent &);
void on_cancel_reject(const CancelRejectEvent&);
void on_gateway_status(const GatewayStatusChangeEvent&);
void report_state(trace::CorID const&trace = trace::CorID());
void on_cancel_reject(const CancelRejectEvent &);
void on_gateway_status(const GatewayStatusChangeEvent &);
void report_state(trace::CorID const &trace = trace::CorID());
void init_fix();
void add_exchange_order_id_to_fix();
void transition(api::orderman::OrderStatus new_state,std::string const&msg);
void transition(api::orderman::OrderStatus new_state,
std::string const &msg);
void transition(api::orderman::OrderStatus new_state);
void transition_to_error(api::orderman::NativeOrderState::SubError sub_error,
std::string const&msg);
std::string const &msg);
void update_working_position();
bool has_exchange_order_id() ;
bool has_exchange_order_id();
Quantity position_contribution();
bool is_old_exe_report( api::orderman::NativeOrderState &, const NewExecutionReportEvent &) const;
bool is_old_exe_report(api::orderman::NativeOrderState &,
const NewExecutionReportEvent &) const;
bool terminal_state(api::orderman::OrderStatus status) const;
api::orderman::NativeOrderState& get_native_state(api::orderman::OrderState &);
api::orderman::NativeOrderState &
get_native_state(api::orderman::OrderState &);
};
}}
} // namespace orderman
} // namespace bts
+6 -2
View File
@@ -53,14 +53,18 @@ class Order {
virtual void on_crack(trace::CorID const&) = 0;
virtual void on_cancel(trace::CorID const&) = 0;
protected:
virtual void warm_up_activate(OrderServicesData&,OrderServices&) {}
void init_order_services(OrderServicesData &);
void init_order_services(OrderServicesData &,OrderServices &parent);
private:
OrderKey _key;
OrderKey _parent_key;
std::unique_ptr<OrderServices> _services;
void init_order_services(OrderServicesData &);
void init_order_services(OrderServicesData &,OrderServices &parent);
friend class OrderFactory;
friend class OrderManager;
friend class OrderServices;
};
}
}
@@ -12,14 +12,17 @@
namespace bts {
namespace orderman {
/**************************Cons/Dest*******************************/
SpreaderOrder::SpreaderOrder()
:
_cancelled (false),
_cancel_timeout (5000),// TODO:get it from config
_check_termination_period (250), // should also be a setting/config like above
_report_dirty(false),
_active(true),
_cancel_hedges_on_cancel(false),
_cw_helper(),
_cancel_timeout (5000),// TODO:get it from config
_check_termination_period (250), // should also be a setting/config like above
_bookkeeping_data_ptr(),
_order_services(nullptr),
_curr_timestamp(),
@@ -31,6 +34,11 @@ _waiting_for_initial_data_state_worker(this),
_working_state_worker(this),
_waiting_for_cancel_all_state_worker(this),
_end_state_worker(this){
LOG(OrderMan, Info,"SpreaderOrder:SpreaderOrder key:{} "
"cw::enable_fills:{} cw::fill_fire_period:{}",
key(),
_cw_helper.enable_fills,
_cw_helper.fill_fire_period);
_transform_to_START();
}
SpreaderOrder::~SpreaderOrder () {}
@@ -59,7 +67,11 @@ void SpreaderOrder::on_activate(Price price, Quantity quantity,
if (!_active) return;
SpreaderOrderServices spreader_order_services (&services);
_on_activate(price, quantity, spreader_order_services);
_report_state();
// the reason why this is immediatly reported (vs in the timer
// event) is that if this spreader was constructed through
// on_init_recovered, it wont subsribe to the timer event
// and hence the report wont go out
_comp_and_report_state();
}
void SpreaderOrder::on_update(Price price, Quantity quantity,trace::CorID const&){
if (!_active) return;
@@ -86,9 +98,15 @@ void SpreaderOrder::setup_new_quoter_order(size_t leg_index){
LOG(OrderMan, Info, "SpreaderOrder.setup_new_quoter_order key:{} leg:{}",
key().value(), leg_index);
SpreaderLegData& leg_data = _bookkeeping_data().get_leg_data(leg_index);
bts_assert(leg_data.leg_property_data().is_active_quoting());
bts::orderman::ChildOrder child_order = leg_data.quoter_order_builder().build();
leg_data.order_update_message().re_init(child_order);
}
ChildOrder SpreaderOrder::get_new_hedge_order(size_t leg_index){
SpreaderLegData& leg_data = _bookkeeping_data().get_leg_data(leg_index);
bts::orderman::ChildOrder child_order = leg_data.hedger_order_builder().build();
return child_order;
}
// events from orders system ////////////////////
/////////////////////////////////////////////////
void SpreaderOrder::_on_time(const bts::mercury::TimerEvent& t_event){
@@ -98,13 +116,17 @@ void SpreaderOrder::_on_time(const bts::mercury::TimerEvent& t_event){
_send_check_termination(false);
if (_any_quoter_in_irrecoverable_error())
_state_worker->on_quoter_irrecoverable_error();
_keep_hedging_path_hot();
if(_report_dirty){
_report_dirty=false;
_comp_and_report_state();
}
}
void SpreaderOrder::_on_book_update(const BookUpdateEvent& book_event){
if (!_active) return;
auto scoped = _order_services->log_scope(book_event.trace,
"SpreaderOrder::_on_book_update", key().value());
_state_worker->on_book (book_event.book,book_event.trace);
//_keep_hedging_path_hot ();
}
void SpreaderOrder::_on_settlement_update(const SettlementUpdateEvent&
settlement_event){
@@ -116,94 +138,110 @@ void SpreaderOrder::_on_settlement_update(const SettlementUpdateEvent&
settlement_event.settlement_price.price());
_state_worker->on_settlement_price(settlement_event);
}
void SpreaderOrder::_on_child_order_update(const OrderStateUpdateEvent& ord_ev){
if (!_active) return;
_perf_ts=utils::TimeStamp::now();
auto now = _perf_ts;
char cb_type='q'; //q=quoter non fill, f=quoter fill, h=hedger
auto scoped_log =
_order_services->log_scope(ord_ev.trace,
"SpreaderOrder::_on_child_order_update",
key().value());
SecurityKey sec_key = SecurityKey(ord_ev.definition->security_key());
OrderKey order_key = ord_ev.key;
bts::api::orderman::OrderStatus status = ord_ev.status;
Quantity filled_qty = Quantity (ord_ev.filled_quantity);
Price last_price = Price(ord_ev.last_price),
order_price=Price(ord_ev.price);
if (!SpreaderUtils::order_acknowledged(status)){
LOG(OrderMan, Diag, "SpreaderOrder::_on_child_order_update, unack'ed "
"callback for key:{} sec_key:{} order_key:{} "
"status:{} ignoring",
key(), sec_key, order_key, status);
bts_assert(filled_qty.value() == 0);
void SpreaderOrder::_keep_hedging_path_hot(){
bool fire=_inited() &&
_active &&
_cw_helper.enable_fills &&
(ms_diff(_curr_timestamp,_cw_helper.last_cache_warm_fill_ts) >=
_cw_helper.fill_fire_period) &&
_state_worker != nullptr &&
_state_worker->state() == SpreaderState::WORKING;
if(!fire)
return;
_cw_helper.set_forced_flag();
for(size_t i=0;i<_bookkeeping_data().n_legs();i++){
SpreaderLegData& leg=_bookkeeping_data().get_leg_data(i);
//only for quoting legs with active orders out there
if(leg.leg_property_data().is_active_quoting() &&
leg.quoter().is_working_order()){
auto& q_ord = leg.order_update_message();
bts::api::orderman::OrderState state;
state.set_filled_quantity(q_ord.current_fills().value());
state.set_last_price(q_ord.last_update_filled_price().value());
bts::api::orderman::OrderDefinition ord_def;
OrderStateUpdateEvent ord_ev (q_ord.order_key(),
key(),
ord_def,
state);
ord_ev.status=q_ord.status();
ord_ev.price=q_ord.last_reported_order_price().value();
_on_child_order_update(ord_ev);
}
if(_cw_helper.enable_child_order_activate)
leg.hedger_manager().get_hedger().keep_hedger_order_warm();
}
SpreaderLegData* leg_data_ptr = _bookkeeping_data().find_leg_from_quoter_orderkey(order_key);
if (leg_data_ptr != nullptr){
SpreaderLegData& leg_data = *leg_data_ptr;
ChildOrderError order_error = _get_order_error (ord_ev,
leg_data.quoter().expecting_cancel_update());
LOG(OrderMan, Info, "SpreaderOrder::_on_child_order_update quoter order "
"key:{} sec_key:{} state:{} order_key:{} status:{} "
"filled_qty:{} price:{} last_price:{} "
"msg:{} child_order_error:{}",
key(), sec_key,
static_cast<int>(_state_worker->state()), order_key, status,
filled_qty, order_price, last_price,
ord_ev.last_msg, static_cast<int>(order_error));
leg_data.order_update_message().on_order_update(ord_ev);
if (order_error == ChildOrderError::NONE){
if (leg_data.order_update_message().last_update_filled_quantity().value() > 0){
cb_type='f';
LOG(OrderMan,Info,"h_p f child update {0}",_diff_ns());
_state_worker->on_quoter_order_fill (leg_data,ord_ev.trace);
}
else{
cb_type='q';
LOG(OrderMan,Info,"h_p q child update {0}",_diff_ns());
_state_worker->on_quoter_order_status_update (leg_data);
}
_cw_helper.unset_forced_flag();
}
void SpreaderOrder::_on_child_order_update(const OrderStateUpdateEvent& ord_ev){
_perf_ts=utils::TimeStamp::now();
auto start = _perf_ts;
bool order_acked = SpreaderUtils::order_acknowledged(ord_ev.status);
if (!_active || !order_acked)
LOG(OrderMan,Info,"SpreaderOrder::_on_child_order_update ignoring "
"key:{} active:{} acked: {}",
key(),_active,order_acked);
else{
//q=quoter non fill, f=quoter fill, h=hedger
char cb_type='q';
SpreaderLegData* leg_data_ptr =
_bookkeeping_data().find_leg_from_quoter_orderkey(ord_ev.key);
if (leg_data_ptr == nullptr){
cb_type = 'h';
_on_non_quoter_order(ord_ev);
}
else
_state_worker->on_quoter_order_error (leg_data);
_on_quoter_order(ord_ev,*leg_data_ptr,cb_type);
LOG(OrderMan,_cw_helper.llevel,"h_p cb done {} {}",cb_type, _diff_ns());
_send_check_termination(true);
LOG(OrderMan,_cw_helper.llevel,"h_p ch_term {} {}",cb_type, _diff_ns());
_report_state(!_cw_helper.in_cache_warming_cycle);
LOG(OrderMan,_cw_helper.llevel,"h_p rep {} {}",cb_type, _diff_ns());
LOG(OrderMan, _cw_helper.llevel,"_on_child_order_update {} {}",
cb_type,
(utils::TimeStamp::now()-start).nanoseconds());
}
else{
// should be a hedge order then
SingleFillHedger* hedger = _bookkeeping_data().find_hedger_from_orderkey(order_key);
if (hedger != nullptr){
cb_type='h';
ChildOrderError order_error = _get_order_error (ord_ev,
hedger->expecting_cancel_update());
LOG(OrderMan, Info, "SpreaderOrder::_on_child_order_update hedger "
}
void SpreaderOrder::_on_non_quoter_order(const OrderStateUpdateEvent& ord_ev){
SingleFillHedger* hedger_ptr =
_bookkeeping_data().find_hedger_from_orderkey(ord_ev.key);
if (hedger_ptr != nullptr){
SingleFillHedger& hedger = *hedger_ptr;
ChildOrderError order_error = _get_order_error (ord_ev,
hedger.expecting_cancel_update());
LOG(OrderMan, Info, "SpreaderOrder::_on_child_order_update hedger "
"key:{} sec_key:{} state:{} order_key:{} status:{} "
"filled_qty:{} price:{} last_price:{} "
"msg:{} child_order_error:{}",
key(), sec_key,
static_cast<int>(_state_worker->state()), order_key, status,
filled_qty, order_price, last_price,
ord_ev.last_msg, static_cast<int>(order_error));
hedger->order_state().on_order_update(ord_ev);
if (order_error == ChildOrderError::NONE)
_state_worker->on_hedger_order_update (*hedger);
else
_state_worker->on_hedger_order_error (*hedger);
}
key(),
ord_ev.definition->security_key(),
static_cast<int>(_state_worker->state()),
ord_ev.key,
ord_ev.status,
ord_ev.filled_quantity,
ord_ev.price,
ord_ev.last_price,
ord_ev.last_msg,
static_cast<int>(order_error));
hedger.order_state().on_order_update(ord_ev);
if (order_error == ChildOrderError::NONE)
_state_worker->on_hedger_order_update (hedger);
else
LOG(OrderMan, Error, "SpreaderOrder::_on_child_order_update unknown ord "
"key:{} sec_key:{} order_key:{} status:{} "
"filled_qty:{} last_price:{} msg:{}",
key(), sec_key, order_key, status, filled_qty,
last_price, ord_ev.last_msg);
_state_worker->on_hedger_order_error (hedger);
}
LOG(OrderMan,Info,"h_p cb done {} {}",cb_type, _diff_ns());
_send_check_termination(true);
_report_state();
LOG(OrderMan, Info,"h_p end {} {}",cb_type, _diff_ns());
LOG(OrderMan, Info,"_on_child_order_update {} {}",
cb_type,
(utils::TimeStamp::now()-now).nanoseconds());
else
LOG(OrderMan, Error, "SpreaderOrder::_on_child_order_update "
"unknown ord "
"key:{} sec_key:{} order_key:{} status:{} "
"filled_qty:{} last_price:{} msg:{}",
key(),
ord_ev.definition->security_key(),
ord_ev.key,
ord_ev.status,
ord_ev.filled_quantity,
ord_ev.last_price,
ord_ev.last_msg);
}
// State Transformations ////////////////////
/////////////////////////////////////////////
@@ -265,14 +303,12 @@ void SpreaderOrder::_cancel_orders(bool cancel_quotes, bool cancel_hedges){
if (leg_data.leg_property_data().is_active_quoting())
leg_data.quoter().cancel_order();
}
if (cancel_hedges)
for (size_t h_idx = 0; h_idx < leg_data.n_hedgers(); h_idx++){
SingleFillHedger& hedger = leg_data.get_hedger(h_idx);
if (_cancel_hedges_on_cancel)
hedger.cancel_hedge();
else
hedger.terminate();
}
if (cancel_hedges){
if(_cancel_hedges_on_cancel)
leg_data.hedger_manager().cancel_hedges();
else
leg_data.hedger_manager().terminate_hedges();
}
}
}
bool SpreaderOrder::_all_quoter_orders_in_terminal_state(){
@@ -287,11 +323,8 @@ bool SpreaderOrder::_all_quoter_orders_in_terminal_state(){
bool SpreaderOrder::_all_hedger_orders_in_terminal_state(){
for (size_t q_idx = 0; q_idx < _bookkeeping_data().n_legs(); q_idx++){
SpreaderLegData& leg_data = _bookkeeping_data().get_leg_data(q_idx);
for (size_t h_idx = 0; h_idx < leg_data.n_hedgers(); h_idx++){
SingleFillHedger& hedger = leg_data.get_hedger(h_idx);
if (!hedger.is_terminal_state())
return false;
}
if (leg_data.hedger_manager().n_active_hedgers() > 0)
return false;
}
return true;
}
@@ -307,16 +340,14 @@ bool SpreaderOrder::_any_quoter_in_irrecoverable_error() const{
void SpreaderOrder::_terminate_quoters (){
for (size_t q_idx = 0; q_idx < _bookkeeping_data().n_legs(); q_idx++){
SpreaderLegData& leg_data = _bookkeeping_data().get_leg_data(q_idx);
leg_data.quoter().terminate();
if (leg_data.leg_property_data().is_active_quoting())
leg_data.quoter().terminate();
}
}
void SpreaderOrder::_terminate_hedgers (){
for (size_t q_idx = 0; q_idx < _bookkeeping_data().n_legs(); q_idx++){
SpreaderLegData& leg_data = _bookkeeping_data().get_leg_data(q_idx);
for (size_t h_idx = 0; h_idx < leg_data.n_hedgers(); h_idx++){
SingleFillHedger& hedger = leg_data.get_hedger(h_idx);
hedger.terminate ();
}
leg_data.hedger_manager().terminate_hedges();
}
}
bool SpreaderOrder::_quoters_can_accept_modification (){
@@ -331,14 +362,11 @@ bool SpreaderOrder::_quoters_can_accept_modification (){
void SpreaderOrder::_send_hedge(size_t hedge_leg_index,
Price hedge_price,
Quantity hedge_qty,
const trace::CorID trace){
SingleFillHedger& hedger = _bookkeeping_data().add_new_hedger (*this,
hedge_leg_index,
hedge_price,
hedge_qty,
_order_services,
trace);
hedger.init ();
const trace::CorID& trace){
_bookkeeping_data().add_new_hedger (hedge_leg_index,
hedge_price,
hedge_qty,
trace);
}
void SpreaderOrder::_compute_hedge_price_quantity(size_t filled_leg_index,
Price fill_price,
@@ -360,6 +388,8 @@ void SpreaderOrder::_compute_hedge_price_quantity(size_t filled_leg_index,
hedge_quantity);
else{
const QuoteLegDriver& driver_data = filled_leg_data.quoter().driver_data(quote_order_price, hedge_leg_index);
// QuoteLegDriver driver_data;
// driver_data.set(quote_order_price,Quantity(1),Quantity(1));
SpreaderUtils::compute_hedge_price_size_n_legs (filled_leg_data.leg_property_data(),
fill_price,
fill_quantity,
@@ -375,7 +405,10 @@ void SpreaderOrder::_compute_hedge_price_quantity(size_t filled_leg_index,
Price(computed_hedge_price),
Quantity(hedge_quantity));
}
void SpreaderOrder::_report_state (){
void SpreaderOrder::_report_state (bool report){
_report_dirty=_report_dirty||report;
}
void SpreaderOrder::_comp_and_report_state (){
if (_activated()){
SpreaderBookkeepingData& bookkeeping_data = _bookkeeping_data();
auto& reported_state = bookkeeping_data.reported_state();
@@ -398,10 +431,10 @@ void SpreaderOrder::_report_state (){
// we just publish what was given to us
LOG(OrderMan, Info, "SpreaderOrder::_report_state key:{} status:{} "
"price:{} qty:{} filled:{} avg_price:{}",
key(), status, reported_state.price(),
reported_state.quantity(),
reported_state.filled_quantity(),
reported_state.avg_price());
key(), status, reported_state.price(),
reported_state.quantity(),
reported_state.filled_quantity(),
reported_state.avg_price());
_order_services->report_state(reported_state);
}
}
@@ -459,31 +492,42 @@ Price SpreaderOrder::_compute_effective_price (Price input_price){
else
return input_price;
}
void SpreaderOrder::_keep_hedging_path_hot(){
// keep all the data hot, by just loading
// it in memory
void SpreaderOrder::_touch_hedging_data (){
if (_inited()){
SpreaderBookkeepingData& d=_bookkeeping_data();
for (size_t i=0; i<d.n_legs(); i++){
SpreaderLegData& q_leg = d.get_leg_data(i);
if(q_leg.leg_property_data().is_active_quoting()){
if(q_leg.order_update_message().out_quantity().value() > 0){
for(size_t j=0; j<d.n_legs(); j++){
SpreaderLegData& h_leg = d.get_leg_data(j);
Price hedge_price;
double hedge_qty;
_compute_hedge_price_quantity(i,
Price(1.0),
1.0,
Price(1.0),
j,
hedge_price,
hedge_qty);
UnhedgedDelta& leg_unhedged_delta = h_leg.unhedged_delta();
leg_unhedged_delta.add_unhedged(hedge_price.value(), 0.0);
leg_unhedged_delta.subtract_hedge_if_available(0.0);
}
}
}
// 1. touching the data : no diff
// 2. calling the function : 0.4-0.5 u
// 3. loading the memory : 0.9-1.0 u
//SpreaderLegData& leg_data = d.get_leg_data(i);
//UnhedgedDelta& unh_del = leg_data.unhedged_delta();
// 0. touch the data
// unh_del._qty = unh_del._qty*1.01;
// unh_del._avg_price = unh_del._qty*1.5;
// 1. this calls the functions that are actually
// called to compute hedge price
// unh_del.add_unhedged(0,0);
// unh_del.subtract_hedge_if_available(0);
// 2. this loads the function address in memory
double temp = 0.0;
void (UnhedgedDelta::*add_unhedged_ptr)(double, double) =
&UnhedgedDelta::add_unhedged;
char* func_addr = (char*&)add_unhedged_ptr;
for(size_t i=0; i < 64*2; i++)
temp += *(func_addr++);
// the other functions calls in subtract_hedge_if_available
// are inline, so we dont need to load them
uint32_t (UnhedgedDelta::*subtract_hedge_if_available_ptr)(double) =
&UnhedgedDelta::subtract_hedge_if_available;
func_addr = (char*&)subtract_hedge_if_available_ptr;
for(size_t i=0; i < 64*10; i++)
temp += *(func_addr++);
LOG(OrderMan, Info,"{}", temp);
}
}
}
}}
@@ -18,8 +18,9 @@
#include "lib/orderman/events/OrderStateUpdateEvent.h"
#include "lib/orderman/events/BookUpdateEvent.h"
#include "lib/mercury/events/TimerEvent.h"
//using namespace bts::orderman;
#include "lib/config/config.h"
#include "lib/config/protos/spreader.pb.h"
#include "lib/orderman/orders/spreader_orders/spreader/SpreaderCacheWarmingState.h"
namespace bts {
namespace api {namespace orderman {class OrderState; class CreateSpreaderOrder;}}
@@ -27,13 +28,14 @@ namespace orderman {
class SettlementUpdateEvent;
class SpreaderOrder : public Order, public ISpreaderManager
{
friend class SpreaderStartStateWorker;
friend class SpreaderWaitingForInitialDataStateWorker;
friend class SpreaderWorkingStateWorker;
friend class SpreaderWaitingForCancelAllStateWorker;
friend class SpreaderEndStateWorker;
friend class SpreaderEndStateWorker;
private:
enum class ChildOrderError
{
@@ -48,27 +50,36 @@ private:
UNEXPECTED_CANCEL_ERROR,
// no error condition
NONE
};
};
// when a fill update, this is populated with
// the indices of the other legs
//std::array<uint8_t,16> _non_filled_indices;
// if the order is _cancelled, this is set to true
bool _cancelled;
// timeout before cancel waiting goes to end
const int64_t _cancel_timeout;
// period on which check termination calls are
// sent to the state worker
const int64_t _check_termination_period;
// is set to true when a report needs to be sent
bool _report_dirty;
// is false if created from disk
const bool _active;
// this is the main data
bool _cancel_hedges_on_cancel;
// helps with forced calls, and all the artificiality of these
// calls are trapped here
CacheWarmingState _cw_helper;
// timeout before cancel waiting goes to end
const int64_t _cancel_timeout;
// period on which check termination calls are
// sent to the state worker
const int64_t _check_termination_period;
std::unique_ptr<SpreaderBookkeepingData> _bookkeeping_data_ptr;
// pointer kept to send reports
SpreaderOrderServices* _order_services;
// timestamp of last the on_time call
bts::TimeStamp _curr_timestamp;
// spreader fires a periodic check
bts::TimeStamp _last_check_termination_timestamp;
bts::TimeStamp _last_check_termination_timestamp;
// current state
utils::TimeStamp _perf_ts;
ISpreaderStateWorker* _state_worker;
SpreaderStartStateWorker _start_state_worker;
SpreaderWaitingForInitialDataStateWorker _waiting_for_initial_data_state_worker;
@@ -125,6 +136,11 @@ public:
return _bookkeeping_data().get_leg_data(leg_index).leg_property_data();
}
void setup_new_quoter_order (size_t leg_index) override;
ChildOrder get_new_hedge_order (size_t leg_index) override;
inline bool in_cache_warming_cycle() const override{
return _cw_helper.in_cache_warming_cycle;
}
private:
// events from orders system ////////////////////
/////////////////////////////////////////////////
@@ -132,6 +148,45 @@ private:
void _on_book_update(const BookUpdateEvent&);
void _on_settlement_update(const SettlementUpdateEvent&);
void _on_child_order_update(const OrderStateUpdateEvent&);
// order handling
inline void _on_quoter_order (const OrderStateUpdateEvent& ord_ev,
SpreaderLegData& leg_data,
char& ord_indicator){
//_populate_non_filled_indices(leg_data.leg_property_data().leg_index());
ord_indicator='q';
ChildOrderError order_error = _get_order_error (ord_ev,
leg_data.quoter().expecting_cancel_update());
LOG(OrderMan, _cw_helper.llevel,
"SpreaderOrder::_on_child_order_update quoter order "
"key:{} sec_key:{} state:{} order_key:{} status:{} "
"filled_qty:{} price:{} last_price:{} "
"msg:{} child_order_error:{}",
key(),
ord_ev.definition->security_key(),
static_cast<int>(_state_worker->state()),
ord_ev.key,
ord_ev.status,
ord_ev.filled_quantity,
ord_ev.price,
ord_ev.last_price,
ord_ev.last_msg,
static_cast<int>(order_error));
leg_data.order_update_message().on_order_update(ord_ev);
if (order_error != ChildOrderError::NONE)
_state_worker->on_quoter_order_error (leg_data);
else{
if (!_cw_helper.is_fill(leg_data.order_update_message())){
LOG(OrderMan,Info,"h_p q child update {}",_diff_ns());
_state_worker->on_quoter_order_status_update (leg_data);
}
else{
ord_indicator = 'f';
LOG(OrderMan,_cw_helper.llevel,"h_p {} child update {}",ord_indicator,_diff_ns());
_state_worker->on_quoter_order_fill (leg_data,ord_ev.trace);
}
}
}
void _on_non_quoter_order (const OrderStateUpdateEvent& ord_ev);
// State Transformations ////////////////////
/////////////////////////////////////////////
void _transform_to_START();
@@ -147,11 +202,11 @@ private:
// this means on_init_new or on_init_recovered
// has been called and we have the bookkeeping
// data built
bool _inited () const {
bool _inited() const {
return _bookkeeping_data_ptr != nullptr;
}
// this means on_activate has been called
bool _activated () {
bool _activated() {
return _order_services != nullptr;
}
void _on_init_new(const bts::api::orderman::OrderDefinition&,
@@ -179,7 +234,7 @@ private:
void _send_hedge(size_t hedge_leg_index,
Price hedge_price,
Quantity hedge_qty,
trace::CorID const);
const trace::CorID&);
void _compute_hedge_price_quantity(size_t filled_leg_index,
Price filled_price,
double filled_quantity,
@@ -187,8 +242,12 @@ private:
size_t hedged_leg_index,
Price& hedge_price,
double& hedge_quantity);
// sends the report out
void _report_state ();
// this sets the report dirty bit, which
// is checked in the timer event and if dirty
// _comp_and_report_state is called
void _report_state (bool report=true);
// this acually sends the report out
void _comp_and_report_state();
SpreaderBookkeepingData& _bookkeeping_data(){
bts_assert (_bookkeeping_data_ptr != nullptr);
return *_bookkeeping_data_ptr;
@@ -248,20 +307,42 @@ private:
_perf_ts = utils::TimeStamp::now();
return rv;
}
void _keep_hedging_path_hot();
double _diff_ns(utils::TimeStamp& t_stop, bool reset=true){
double rv = (utils::TimeStamp::now()-t_stop).nanoseconds();
if(reset)
t_stop = utils::TimeStamp::now();
return rv;
}
// hot path related
void _keep_hedging_path_hot();
void _touch_hedging_data ();
// testing only //////////////////
// this provides direct way to inject
// data into the spreader, as well as
// access to the internal state
//////////////////////////////////
public:
SpreaderState test_only_func_spreader_state () {return _state_worker->state();}
SpreaderLegData& test_only_func_spreader_leg_data (size_t index) {return _bookkeeping_data().get_leg_data (index);}
SpreaderBookkeepingData& test_only_func_spreader_full_data () {return _bookkeeping_data();}
SpreaderState test_only_func_spreader_state () const {
return _state_worker->state();
}
SpreaderLegData& test_only_func_spreader_leg_data (size_t index) {
return _bookkeeping_data().get_leg_data (index);
}
SpreaderBookkeepingData& test_only_func_spreader_full_data () {
return _bookkeeping_data();
}
int64_t test_cancel_timeout () {return _cancel_timeout;}
void test_only_on_book_update(const bts::orderman::BookUpdateEvent& b) {_on_book_update(b);}
void test_only_on_book_update(const bts::orderman::Book& b, trace::CorID const&trace) {_on_book_update(bts::orderman::BookUpdateEvent(b, trace));}
void test_only_on_child_order_update (bts::orderman::OrderKey order_key, bts::api::orderman::OrderStatus status, Quantity total_filled_qty, Price avg_price)
void test_only_on_book_update(const BookUpdateEvent& b) {
_on_book_update(b);
}
void test_only_on_book_update(const Book& b,
trace::CorID const&trace) {
_on_book_update(BookUpdateEvent(b, trace));
}
void test_only_on_child_order_update (OrderKey order_key,
bts::api::orderman::OrderStatus status,
Quantity total_filled_qty,
Price avg_price)
{
bts::api::orderman::OrderDefinition order_def;
order_def.set_security_key(0);
@@ -269,15 +350,27 @@ public:
order_state.set_status(status);
order_state.set_filled_quantity(total_filled_qty.value());
order_state.set_last_price(avg_price.value());
OrderStateUpdateEvent ord_ev (order_key, OrderKey(0), order_def, order_state,trace::CorID());
order_state.set_price(avg_price.value());
OrderStateUpdateEvent ord_ev (order_key,
OrderKey(0),
order_def,
order_state,
trace::CorID());
_on_child_order_update (ord_ev);
}
void test_only_on_init_new(const bts::api::orderman::OrderDefinition& order_def, const SpreaderPreActivationOrderServices& pao_services) {_on_init_new(order_def, pao_services);}
void test_only_on_init_new(
const bts::api::orderman::OrderDefinition& order_def,
const SpreaderPreActivationOrderServices& pao_services) {
_on_init_new(order_def, pao_services);
}
void test_only_on_activate(Price price,
Quantity quantity,
SpreaderOrderServices& order_services) {
_on_activate(price, quantity, order_services);
}
void test_only_on_time (TimeStamp){}
void test_only_keep_hedging_path_hot(){
_keep_hedging_path_hot();
}
};
}}
@@ -1,10 +1,12 @@
#pragma once
#include "lib/orderman/orderman_utils/Book.h"
#include "lib/orderman/actors/order_manager/ChildOrder.h"
namespace bts {
namespace orderman {
class LegPropertyData;
class CacheWarmingState;
// The Spreader spawns a bunch of quoters and
// hedgers and hands them this interface to
// i. get data such as book and leg properties
@@ -22,6 +24,15 @@ public:
// this replaces the current quoter order with a new one
// and updates the bookkeeping data
virtual void setup_new_quoter_order (size_t leg_index) = 0;
// this simply returns a new ChildOrder for the hedger,
// it does not alter the state or has side effects (as
// as the spreader goes)
virtual ChildOrder get_new_hedge_order (size_t leg_index) = 0;
// the spreader can be in a cache warming cycle and generate
// fake updates, the quoters and hedgers should ignore them
// (however do as much as they can to keep the instruction
// cache warm)
virtual bool in_cache_warming_cycle() const = 0;
virtual ~ISpreaderManager() {}
};
}}
@@ -0,0 +1,28 @@
#include "LegFillState.h"
#include "SpreaderUtils.h"
namespace bts{
namespace orderman {
LegFillState::LegFillState()
:
_price(0),
_qty(0){
}
void LegFillState::add_fill(Price fill_price,
Quantity fill_qty,
bool){
Price out_price;
Quantity out_qty;
SpreaderUtils::compute_average_price(_price,
_qty,
fill_price,
fill_qty,
out_price,
out_qty);
_price=out_price;
_qty=out_qty;
}
}}
@@ -0,0 +1,24 @@
#pragma once
#include "lib/orderman/orderman_utils/orderman_common.h"
namespace bts{
namespace orderman {
class LegFillState{
private:
Price _price;
Quantity _qty;
public:
LegFillState ();
Price price() const{
return _price;
}
Quantity quantity() const{
return _qty;
}
void add_fill (Price fill_price,
Quantity fill_qty,
bool is_quoting_fill);
};
}}
@@ -8,16 +8,16 @@ namespace orderman {
LegPropertyData::LegPropertyData(size_t leg_index,
const bts::api::ContractLeg& leg_contract,
const bts::spark::LegSpreaderParams& leg_params,
api::Side trading_side,
const bts::api::orderman::OrderDefinition& quoter_order_definition,
const bts::api::orderman::OrderDefinition& hedger_order_definition)
api::Side trading_side)
:
_leg_index(leg_index),
_leg_contract(leg_contract),
_leg_params(leg_params),
_trading_side(trading_side),
_quoter_order_definition (quoter_order_definition),
_hedger_order_definition (hedger_order_definition){
LOG(OrderMan, Info, "LegPropertyData.cons leg_idx: {} sec_key: {} side: {}", leg_index, leg_contract.security().id(), trading_side);
_trading_side(trading_side){
LOG(OrderMan, Info, "LegPropertyData.cons leg_idx: "
"{} sec_key: {} side: {}",
leg_index,
leg_contract.security().id(),
trading_side);
}
}}
@@ -18,15 +18,11 @@ private:
const bts::api::ContractLeg _leg_contract;
const bts::spark::LegSpreaderParams _leg_params;
const api::Side _trading_side;
const bts::api::orderman::OrderDefinition _quoter_order_definition;
const bts::api::orderman::OrderDefinition _hedger_order_definition;
public:
LegPropertyData(size_t leg_index,
const bts::api::ContractLeg& leg_contract,
const bts::spark::LegSpreaderParams& leg_params,
api::Side trading_side,
const bts::api::orderman::OrderDefinition& quoter_order_definition,
const bts::api::orderman::OrderDefinition& hedger_order_definition);
api::Side trading_side);
size_t leg_index() const {return _leg_index;}
const bts::api::ContractLeg& leg_contract() const {return _leg_contract;}
const bts::spark::LegSpreaderParams& leg_params () const {return _leg_params;}
@@ -38,8 +34,6 @@ public:
double size_multiplier() const { return _leg_contract.quantity(); }
double tick_size() const { return _leg_contract.security().contract_spec().low_tick(); }
bool is_active_quoting() const {return _leg_params.is_active_quoting();}
const bts::api::orderman::OrderDefinition& quoter_order_definition() const {return _quoter_order_definition;}
const bts::api::orderman::OrderDefinition& hedger_order_definition() const {return _hedger_order_definition;}
bts::spark::LegSpreaderParams::LegMarketDataCalculation leg_md_calc() const {
return _leg_params.leg_md_calc();
}
@@ -9,24 +9,19 @@
namespace bts {
namespace orderman {
SingleFillHedger::SingleFillHedger(ISpreaderManager& spreader_manager,
const LegPropertyData& leg_property_data,
ChildOrder& child_order,
bts::Price hedge_price,
bts::Quantity hedge_quantity,
trace::CorID const &fill_trace,
bool is_live)
:
_is_permenantly_cancelled(false),
_spreader(spreader_manager),
_leg_property_data (leg_property_data),
_child_order_state (child_order,
leg_property_data.leg_security_key(),
_child_order_state (leg_property_data.leg_security_key(),
is_live,
false),
_price_quantity_state(hedge_price,
hedge_quantity,
leg_property_data.trading_side()),
_fill_trace(fill_trace),
_price_quantity_state(Price(0),
Quantity(0)),
_worker (nullptr),
_start_state_worker (this),
_waiting_for_first_ack_state_worker (this),
@@ -35,25 +30,31 @@ _working_state_worker (this),
_waiting_for_cancel_ack_state_worker (this),
_end_state_worker (this){
LOG(OrderMan, Info, "SingleFillHedger cons, "
"key:{} sec:{} price:{} quantity:{}",
_key(), leg_property_data.security_key(),
hedge_price, hedge_quantity);
"key:{} sec:{}",
_spreader.spreader_order_key(),
leg_property_data.security_key());
transform_to_START ();
}
SingleFillHedger::SingleFillHedger (SingleFillHedger&& other)
:
_is_permenantly_cancelled(other._is_permenantly_cancelled),
_spreader (other._spreader),
_leg_property_data (other._leg_property_data),
_child_order_state (std::move(other._child_order_state)),
_price_quantity_state (other._price_quantity_state),
_fill_trace(other._fill_trace),
_worker(nullptr),
_start_state_worker (this, other._start_state_worker),
_waiting_for_first_ack_state_worker (this, other._waiting_for_first_ack_state_worker),
_waiting_for_first_ack_cancelled_state_worker (this,
other._waiting_for_first_ack_cancelled_state_worker),
_waiting_for_first_ack_state_worker (
this,
other._waiting_for_first_ack_state_worker),
_waiting_for_first_ack_cancelled_state_worker (
this,
other._waiting_for_first_ack_cancelled_state_worker),
_working_state_worker (this, other._working_state_worker),
_waiting_for_cancel_ack_state_worker (this, other._waiting_for_cancel_ack_state_worker),
_waiting_for_cancel_ack_state_worker (
this,
other._waiting_for_cancel_ack_state_worker),
_end_state_worker (this, other._end_state_worker){
HedgerState state = other._worker->state();
switch(state){
@@ -81,35 +82,44 @@ _end_state_worker (this, other._end_state_worker){
/********************************************************************/
/****************************External Calls**************************/
/********************************************************************/
void SingleFillHedger::init(){
LOG(OrderMan, Diag, "SingleFillHedger::init key: {}", _key().value());
_worker->init();
void SingleFillHedger::init_hedger (Price price,
Quantity quantity,
const trace::CorID& trace){
_worker->init(price,quantity,trace);
}
void SingleFillHedger::on_order_update(const SpreaderChildOrderState& u){
//bts_assert (u.order_key() == _order.order_key());
LOG(OrderMan, Info, "SingleFillHedger::update_order_status "
"key:{} state:{} order status:{} fills:{}",
_key(), _worker->state_text(),
_hedge_order_key(), _worker->state_text(),
static_cast<int>(u.status()),
u.last_update_filled_quantity());
_worker->on_order_update (u);
}
void SingleFillHedger::on_book(const bts::orderman::Book& book,trace::CorID &trace){
void SingleFillHedger::on_book(const Book& book,
trace::CorID &trace){
_worker->on_book (book,trace);
}
void SingleFillHedger::cancel_hedge(){
LOG(OrderMan, Info, "SingleFillHedger::cancel_order key:{} state:{}",
_key(), _worker->state_text());
LOG(OrderMan, Info, "SingleFillHedger::cancel_order key:{} state:{}",
_hedge_order_key(), _worker->state_text());
_is_permenantly_cancelled=true;
_worker->cancel ();
}
void SingleFillHedger::terminate (){
LOG(OrderMan, Info, "SingleFillHedger::terminate key:{} state:{}",
_key(), _worker->state_text());
_hedge_order_key(), _worker->state_text());
_is_permenantly_cancelled=true;
_worker->terminate();
}
void SingleFillHedger::on_time (const bts::TimeStamp& ts){
_worker->on_time (ts);
}
bool SingleFillHedger::is_active_order () const {
bool no_active_order = _worker->state() == HedgerState::START ||
_worker->state() == HedgerState::END;
return !no_active_order;
}
bool SingleFillHedger::is_terminal_state () const {
return _worker->state() == HedgerState::END;
}
@@ -117,24 +127,26 @@ bool SingleFillHedger::is_terminal_state () const {
/*************************State Transformations**********************/
/********************************************************************/
void SingleFillHedger::transform_to_START(){
bts_assert (_worker == nullptr);
bts_assert(_worker == nullptr ||
_worker->state() == HedgerState::END);
_switch_state (&_start_state_worker);
}
void SingleFillHedger::transform_to_WAITING_FOR_FIRST_ACK(){
bts_assert (_worker->state() == HedgerState::START);
bts_assert(_worker->state() == HedgerState::START);
_switch_state (&_waiting_for_first_ack_state_worker);
}
void SingleFillHedger::transform_to_WAITING_FOR_FIRST_ACK_CANCELLED () {
bts_assert (_worker->state() == HedgerState::WAITING_FOR_FIRST_ACK);
bts_assert(_worker->state() == HedgerState::WAITING_FOR_FIRST_ACK);
_switch_state (&_waiting_for_first_ack_cancelled_state_worker);
}
void SingleFillHedger::transform_to_WORKING() {
bts_assert (_worker->state() == HedgerState::WAITING_FOR_FIRST_ACK);
bts_assert(_worker->state() == HedgerState::WAITING_FOR_FIRST_ACK);
_switch_state (&_working_state_worker);
}
void SingleFillHedger::transform_to_WAITING_FOR_CANCEL_ACK(){
bts_assert (_worker->state() == HedgerState::WAITING_FOR_FIRST_ACK_CANCELLED ||
_worker->state() == HedgerState::WORKING);
bts_assert(
_worker->state() == HedgerState::WAITING_FOR_FIRST_ACK_CANCELLED ||
_worker->state() == HedgerState::WORKING);
_switch_state (&_waiting_for_cancel_ack_state_worker);
}
void SingleFillHedger::transform_to_END() {
@@ -142,8 +154,7 @@ void SingleFillHedger::transform_to_END() {
_switch_state (&_end_state_worker);
}
void SingleFillHedger::_switch_state (IHedgerStateWorker* new_state){
LOG(OrderMan, Diag, "SingleFillHedger::_switch_state key:{}, {} -> {} ",
_key(),
LOG(OrderMan, Info, "SingleFillHedger::_switch_state {} -> {} ",
(_worker == nullptr ? "NULL" : _worker->state_text()),
new_state->state_text());
_worker = new_state;
@@ -26,15 +26,16 @@ friend HedgerWorkingStateWorker;
friend HedgerWaitingForCancelAckStateWorker;
friend HedgerEndStateWorker;
private:
bool _is_permenantly_cancelled;
ISpreaderManager& _spreader;
const LegPropertyData& _leg_property_data;
SpreaderChildOrderState _child_order_state;
HedgePriceQuantityState _price_quantity_state;
trace::CorID _fill_trace;
IHedgerStateWorker* _worker;
HedgerStartStateWorker _start_state_worker;
HedgerWaitingForFirstAckStateWorker _waiting_for_first_ack_state_worker;
HedgerWaitingForFirstAckCancelledStateWorker _waiting_for_first_ack_cancelled_state_worker;
HedgerWaitingForFirstAckCancelledStateWorker
_waiting_for_first_ack_cancelled_state_worker;
HedgerWorkingStateWorker _working_state_worker;
HedgerWaitingForCancelAckStateWorker _waiting_for_cancel_ack_state_worker;
HedgerEndStateWorker _end_state_worker;
@@ -49,29 +50,37 @@ private:
void _switch_state (IHedgerStateWorker* new_state);
// helpers
void _place_order (Price price,
Quantity quantity) {
Quantity quantity,
const trace::CorID& trace) {
_child_order_state.place_order(price,
quantity,
_spreader.current_time(),
_fill_trace);
trace);
}
void _modify_order (Price new_price){
_price_quantity_state.update_out_price (new_price);
_child_order_state.modify_order(_price_quantity_state.out_price(),
_price_quantity_state.target_quantity());
_child_order_state.modify_order(new_price);
}
void _cancel_order () {
_child_order_state.cancel_order();
}
bts::orderman::OrderKey _key() {return _child_order_state.order_key();}
TimeStamp _current_time () const {return _spreader.current_time();}
// this function is only called for logging
// purposes.
inline bts::orderman::OrderKey _hedge_order_key() const{
if(is_active_order())
return _child_order_state.order_key();
else
// when the hedger does not have an active order
// (ie when in START or END states). since this
// function is only called for logging purposes
// it is safe to return 0
return OrderKey(0);
}
TimeStamp _current_time () const {
return _spreader.current_time();
}
public:
SingleFillHedger(ISpreaderManager& spreader_manager,
const LegPropertyData& leg_property_data,
ChildOrder& child_order,
bts::Price hedge_price,
bts::Quantity hedge_quantity,
trace::CorID const &trace,
bool is_live);
SingleFillHedger() = delete;
SingleFillHedger (const SingleFillHedger&) = delete;
@@ -79,16 +88,20 @@ public:
SingleFillHedger (SingleFillHedger&&);
SingleFillHedger& operator=(const SingleFillHedger&) = delete;
SingleFillHedger& operator=(SingleFillHedger&&) = delete;
// the a-f actions are associated with the state machine
// side effect actions
//a. leave the start state and submit the order
void init();
//the hedging machinery is constructed as follows :
//i.HedgerManger is created
//ii.HedgerManager.init_once constructs the hedgers
//that live in the START state until...
//iii.until init_hedger is called which sets the price
//qty and causes the order to be submitted
void init_hedger (Price price, Quantity quantity,const trace::CorID&);
//b. called by the spreader when there is an order update
// reported by the exchange
void on_order_update(const SpreaderChildOrderState& order_update_message);
//c. called when the market data associated
// with this security is changed
void on_book(const bts::orderman::Book& book,trace::CorID &);
void on_book(const Book& book,trace::CorID &);
//d. called when the hedge order is cancelled, either
// manually or automatically
void cancel_hedge();
@@ -97,18 +110,39 @@ public:
void terminate ();
//f. on the engine infrastructure time
void on_time(const bts::TimeStamp&);
// returns true if the quoter is in the final state, from which
// true if the hedger has an active working order. the order can
// be in an unknown state, but as long as an order has been sent
// and it has not reached a terminal state, its considered "active"
bool is_active_order() const;
// returns true if the hedger is in the final state, from which
// it will never come back
bool is_terminal_state () const;
// true if the hedger can accept a price change
bool expecting_cancel_update() const {
return _worker->state() == HedgerState::WAITING_FOR_CANCEL_ACK;
}
public:
HedgerState test_only_func_hedger_state() const { return _worker->state (); }
const HedgePriceQuantityState& test_only_func_price_quantity_state () const {
return _price_quantity_state;
inline HedgerState state() const{
return _worker->state();
}
SpreaderChildOrderState& order_state() {return _child_order_state;}
inline bool is_free() const{
return state()==HedgerState::START;
}
inline SpreaderChildOrderState& order_state() {
return _child_order_state;
}
inline const SpreaderChildOrderState& order_state() const {
return _child_order_state;
}
inline void keep_hedger_order_warm(){
if (is_free())
_child_order_state.keep_order_warm();
}
public:
HedgerState test_only_func_hedger_state() const {
return _worker->state ();
}
const HedgePriceQuantityState& test_only_func_price_quantity_state() const {
return _price_quantity_state;
}
};
}}
@@ -5,7 +5,7 @@
#include "LegPropertyData.h"
#include "SpreaderChildOrderState.h"
#include "lib/orderman/orderman_utils/orderman_common.h"
//#include "lib/config/protos/speader.pb.h"
#include "lib/orderman/orders/spreader_orders/spreader/SpreaderCacheWarmingState.h"
#include "lib/config/config.h"
namespace bts {
@@ -20,17 +20,16 @@ _two_legged (n_spreader_legs == 2),
_active_quoting(this_leg_property.is_active_quoting()),
_hidden_order(this_leg_property.leg_params().has_quote_params() &&
this_leg_property.leg_params().quote_params().has_quote_hidden()),
//_max_failed_iocs(config::get<config::Spreader>().n_failed_iocs),
_is_permenantly_cancelled (false),
_irrecoverable_error(false),
_n_spreader_legs (n_spreader_legs),
_max_failed_iocs(20),
_n_failed_iocs(0),
_spreader(spreader),
_this_leg_data (this_leg_property),
_n_spreader_legs (n_spreader_legs),
_child_order_state (child_order_state),
_driver_cache(n_spreader_legs,n_spreader_legs>2?10:0,this_leg_property.tick_size()),
_price_quantity_state(Price(0), Quantity(0), child_order_state),
_is_permenantly_cancelled (false),
_irrecoverable_error(false),
_n_failed_iocs(0),
_worker(nullptr),
_start_state_worker(this),
_waiting_for_size_state_worker(this),
@@ -40,13 +39,10 @@ _waiting_for_first_ack_cancelled_state_worker(this),
_waiting_for_cancel_ack_state_worker(this),
_working_state_worker(this),
_end_state_worker(this){
if (_active_quoting){
if (!_two_legged){
for (size_t q_idx = 0; q_idx < _n_spreader_legs; q_idx++){
_drivers_temp_usage.push_back (QuoteLegDriver());
}
}
}
bts_assert(_active_quoting);
if (!_two_legged)
for (size_t q_idx = 0; q_idx < _n_spreader_legs; q_idx++)
_drivers_temp_usage.push_back (QuoteLegDriver());
_transform_to_START ();
}
SingleLegQuoter::SingleLegQuoter(SingleLegQuoter&& other)
@@ -54,19 +50,19 @@ SingleLegQuoter::SingleLegQuoter(SingleLegQuoter&& other)
_two_legged (other._two_legged),
_active_quoting(other._active_quoting),
_hidden_order(other._hidden_order),
_is_permenantly_cancelled (other._is_permenantly_cancelled),
_irrecoverable_error(other._irrecoverable_error),
_n_spreader_legs (other._n_spreader_legs),
_max_failed_iocs(other._max_failed_iocs),
_n_failed_iocs(other._n_failed_iocs),
_spreader(other._spreader),
_this_leg_data (other._this_leg_data),
_n_spreader_legs (other._n_spreader_legs),
_child_order_state (other._child_order_state),
_drivers_temp_usage(other._drivers_temp_usage),
_driver_cache(other._driver_cache),
_price_quantity_state(other._price_quantity_state.target_spread_price(),
other._price_quantity_state.target_leg_quantity(),
other._price_quantity_state.order_state()),
_is_permenantly_cancelled (other._is_permenantly_cancelled),
_irrecoverable_error(other._irrecoverable_error),
_n_failed_iocs(other._n_failed_iocs),
other._price_quantity_state.order_state()),
_worker(nullptr),
_start_state_worker(this),
_waiting_for_size_state_worker(this),
@@ -88,50 +84,48 @@ _end_state_worker(this){
/*********************************************************************************/
void SingleLegQuoter::one_time_activate(Price price,
Quantity quantity){
if (_active_quoting){
Quantity this_leg_quantity = SpreaderUtils::get_leg_quantity_from_spreader_quantity (_this_leg_data, quantity);
LOG(OrderMan, Info, "SingleLegQuoter::one_time_activate "
"spread_key:{} leg_idx:{}"
"price:{} leg_quantity:{}",
_spreader.spreader_order_key(),_this_leg_data.leg_index(),
price, this_leg_quantity);
_worker->one_time_activate (price, this_leg_quantity);
}
bts_assert(_active_quoting);
Quantity this_leg_quantity = SpreaderUtils::get_leg_quantity_from_spreader_quantity (_this_leg_data, quantity);
LOG(OrderMan, Info, "SingleLegQuoter::one_time_activate "
"spread_key:{} leg_idx:{}"
"price:{} leg_quantity:{}",
_spreader.spreader_order_key(),_this_leg_data.leg_index(),
price, this_leg_quantity);
_worker->one_time_activate (price, this_leg_quantity);
}
void SingleLegQuoter::update_order_status(const SpreaderChildOrderState& u){
if (_active_quoting){
LOG(OrderMan, Info, "SingleLegQuoter::update_order_status "
"spread_key:{} leg_idx:{} status:{}",
_spreader.spreader_order_key(),_this_leg_data.leg_index(),
static_cast<int>(u.status()));
_worker->on_order_update (u);
}
bts_assert(_active_quoting);
log::Level llevel=_spreader.in_cache_warming_cycle()?NoLog:Info;
LOG(OrderMan, llevel,
"SingleLegQuoter::update_order_status "
"spread_key:{} leg_idx:{} status:{}",
_spreader.spreader_order_key(),_this_leg_data.leg_index(),
static_cast<int>(u.status()));
_worker->on_order_update (u);
}
void SingleLegQuoter::on_book(size_t leg_index,
const bts::orderman::Book& book,trace::CorID trace){
if (_active_quoting)
_worker->on_book (leg_index, book,trace);
bts_assert(_active_quoting);
_worker->on_book (leg_index, book,trace);
}
void SingleLegQuoter::update_other_leg_fills(size_t leg_index,
Quantity filled_quantity){
if (_active_quoting){
LOG(OrderMan, Info, "SingleLegQuoter::update_other_leg_fills "
"spread_key:{} leg_idx:{} fill_idx:{} filled_quantity:{}",
_spreader.spreader_order_key(), _this_leg_data.leg_index(),
leg_index, filled_quantity);
_worker->on_other_leg_quoter_fill (leg_index, filled_quantity);
}
bts_assert(_active_quoting);
LOG(OrderMan, Info, "SingleLegQuoter::update_other_leg_fills "
"spread_key:{} leg_idx:{} fill_idx:{} filled_quantity:{}",
_spreader.spreader_order_key(), _this_leg_data.leg_index(),
leg_index, filled_quantity);
_worker->on_other_leg_quoter_fill (leg_index, filled_quantity);
}
void SingleLegQuoter::update_order_price_quantity(Price price,
Quantity quantity){
if (_active_quoting){
Quantity this_leg_quantity = SpreaderUtils::get_leg_quantity_from_spreader_quantity (_this_leg_data, quantity);
LOG(OrderMan, Info, "SingleLegQuoter::update_order_price_quantity "
"spread_key:{} leg_idx:{} price:{} this_leg_quantity:{}",
_spreader.spreader_order_key(),_this_leg_data.leg_index(),
price, this_leg_quantity);
_worker->update_spreader_order (price, this_leg_quantity);
}
bts_assert(_active_quoting);
Quantity this_leg_quantity = SpreaderUtils::get_leg_quantity_from_spreader_quantity (_this_leg_data, quantity);
LOG(OrderMan, Info, "SingleLegQuoter::update_order_price_quantity "
"spread_key:{} leg_idx:{} price:{} this_leg_quantity:{}",
_spreader.spreader_order_key(),_this_leg_data.leg_index(),
price, this_leg_quantity);
_worker->update_spreader_order (price, this_leg_quantity);
}
void SingleLegQuoter::cancel_order(){
_is_permenantly_cancelled = true;
@@ -40,15 +40,18 @@ private:
const bool _two_legged;
const bool _active_quoting;
const bool _hidden_order;
bool _is_permenantly_cancelled;
bool _irrecoverable_error;
// this is a config constant. if the quoter
// is in a hidden mode, this is the max
// number of cxl'ed IOCs with no fills,
// before the quoter goes to an irrecoverable
// eror state
const uint32_t _max_failed_iocs;
ISpreaderManager& _spreader;
const LegPropertyData& _this_leg_data;
const size_t _n_spreader_legs;
const uint32_t _max_failed_iocs;
uint32_t _n_failed_iocs;
ISpreaderManager& _spreader;
const LegPropertyData& _this_leg_data;
SpreaderChildOrderState& _child_order_state;
// computing the quoting price/quantity for multiple
// legs requires auxiliary storage for drivers, which
@@ -56,13 +59,10 @@ private:
// to send the orders, the drivers are committed to
std::vector<QuoteLegDriver> _drivers_temp_usage;
QuoteDriverSetHistory _driver_cache;
QuoteLegPriceQuantityState _price_quantity_state;
bool _is_permenantly_cancelled;
// also implies a terminal state (this is not clear)
bool _irrecoverable_error;
QuoteLegPriceQuantityState _price_quantity_state;
// also implies a terminal state (this is not clear)
// in hidden mode, every failed ioc increments
// this counter
uint32_t _n_failed_iocs;
// this counter
// state workers
IQuoterStateWorker* _worker;
QuoterStartStateWorker _start_state_worker;
@@ -119,6 +119,13 @@ public:
// be in an unknown state, but as long as an order has been sent
// and it has not reached a terminal state, its considered "active"
bool is_active_order() const;
// contrary t theabove, the order is surely in a working state, ie
// has been acknowledged and is out there
bool is_working_order() const{
return _worker != nullptr &&
_worker->state() == QuoterState::WORKING &&
_child_order_state.status() == api::orderman::OrderStatus::ORDER_STATUS_WORKING;
}
// returns true if the quoter is in the final state, from which
// it will never come back
bool is_terminal_state () const;
@@ -3,63 +3,62 @@
namespace bts{
namespace orderman{
SpreaderBookkeepingData::SpreaderBookkeepingData (ISpreaderManager& spreader_manager,
bts::orderman::OrderKey spreader_order_key,
const api::orderman::OrderDefinition& order_def,
const SpreaderPreActivationOrderServices& spreader_pao_services,
const std::vector<LegPropertyData>& legs,
bool inited_new)
using namespace bts::api::orderman;
SpreaderBookkeepingData::SpreaderBookkeepingData (
ISpreaderManager& spreader_manager,
bts::orderman::OrderKey spreader_order_key,
const OrderDefinition& order_def,
const SpreaderPreActivationOrderServices& spreader_pao_services,
const std::vector<LegPropertyData>& legs,
const std::vector<OrderDefinition>& quoter_ord_defs,
const std::vector<OrderDefinition>& hedger_ord_defs,
bool inited_new)
:
_is_live(spreader_pao_services.is_live()),
_inited_new (inited_new),
_n_legs(legs.size()),
_n_quoting_legs(_calc_n_quoting_legs(legs)),
_inited_new (inited_new),
_order_def(order_def),
_reported_state(){
LOG(OrderMan, Info, "SpreaderBookkeepingData.cons started key: {} n_legs: {}", spreader_order_key.value(), _n_legs);
_leg_prop_data.reserve(_n_legs),
_books.reserve(_n_legs),
_quoter_order_builders.reserve(_n_legs);
_hedger_order_builders.reserve(_n_legs);
_quoter_child_order_states.reserve(_n_legs);
_quoters.reserve (_n_legs);
_hedgers.reserve (_n_legs);
_unhedged_deltas.reserve(_n_legs);
_leg_data.reserve (_n_legs);
for (const auto& lpd_iter : legs){
_leg_prop_data.push_back(lpd_iter);
const LegPropertyData& leg_property_data = _leg_prop_data.back();
_books.emplace_back(lpd_iter.leg_security_key());
_quoter_order_builders.emplace_back(spreader_order_key, leg_property_data.quoter_order_definition(), spreader_pao_services, 10);
_hedger_order_builders.emplace_back(spreader_order_key, leg_property_data.hedger_order_definition(), spreader_pao_services, 10);
ChildOrder empty_order;
_quoter_child_order_states.emplace_back (empty_order,leg_property_data.leg_security_key(),_is_live,true);
_quoters.emplace_back(spreader_manager, leg_property_data, _n_legs, _quoter_child_order_states.back());
_hedgers.emplace_back ();
_unhedged_deltas.emplace_back();
_leg_data.emplace_back(leg_property_data,
_quoters.back(),
_quoter_order_builders.back(),
_hedger_order_builders.back(),
_hedgers.back (),
_quoter_child_order_states.back(),
_books.back(),
_unhedged_deltas.back());
bts_assert (_sec_key_to_index.find(leg_property_data.security_key()) == _sec_key_to_index.end());
_sec_key_to_index.insert(std::pair<uint32_t, size_t>(leg_property_data.security_key(), leg_property_data.leg_index()));
LOG(OrderMan, Info, "SpreaderBookkeepingData.cons started "
"key: {} n_legs: {}",
spreader_order_key.value(), _n_legs);
_leg_data.reserve(_n_legs);
size_t i=0;
for (const auto& leg_property_data : legs){
_leg_data.emplace_back(spreader_pao_services,
spreader_order_key,
_n_legs,
spreader_manager,
quoter_ord_defs[i],
hedger_ord_defs[i],
leg_property_data);
bts_assert (_sec_key_to_index.find(leg_property_data.security_key())
== _sec_key_to_index.end());
_sec_key_to_index.insert(std::pair<uint32_t, size_t>(
leg_property_data.security_key(),
leg_property_data.leg_index()));
i++;
}
LOG(OrderMan, Info, "SpreaderBookkeepingData.cons completed key: {} n_legs: {}", spreader_order_key.value(), _n_legs);
LOG(OrderMan, Info, "SpreaderBookkeepingData.cons completed "
"key: {} n_legs: {}", spreader_order_key.value(),
_n_legs);
}
SpreaderBookkeepingData::SpreaderBookkeepingData (ISpreaderManager& spreader_manager,
const bts::api::orderman::OrderRecord& order_record,
const SpreaderPreActivationOrderServices& spreader_pao_services,
const std::vector<LegPropertyData>& legs)
SpreaderBookkeepingData::SpreaderBookkeepingData (
ISpreaderManager& spreader_manager,
const OrderRecord& order_record,
const SpreaderPreActivationOrderServices& spreader_pao_services,
const std::vector<LegPropertyData>& legs,
const std::vector<OrderDefinition>& quoter_ord_defs,
const std::vector<OrderDefinition>& hedger_ord_defs)
:
SpreaderBookkeepingData(spreader_manager,
OrderKey(order_record.key()),
order_record.def(),
spreader_pao_services,
legs,
quoter_ord_defs,
hedger_ord_defs,
false){
_reported_state=order_record.state();
}
@@ -89,26 +88,4 @@ void SpreaderBookkeepingData::total_fills (Quantity& out_quantity,
out_quantity = Quantity(fills);
out_price = Price(avg_price);
}
SingleFillHedger& SpreaderBookkeepingData::add_new_hedger(ISpreaderManager& spreader_manager,
size_t leg_index,
bts::Price hedge_price,
bts::Quantity hedge_quantity,
SpreaderOrderServices* ,
const trace::CorID &trace){
LOG(OrderMan, Info, "SpreaderBookkeepingData::add_new_hedger "
"leg_index:{} price:{} quantity:{} cid:{}",
leg_index, hedge_price, hedge_quantity,trace.sn);
bts_assert(leg_index < _n_legs);
SpreaderLegData& leg_data = _leg_data[leg_index];
std::vector<SingleFillHedger>& leg_hedgers = _hedgers[leg_index];
bts::orderman::ChildOrder hedge_order = leg_data.hedger_order_builder().build();
leg_hedgers.emplace_back (spreader_manager,
leg_data.leg_property_data(),
hedge_order,
hedge_price,
hedge_quantity,
trace,
_is_live);
return leg_hedgers.back();
}
}}
}}
@@ -9,6 +9,7 @@
#include "lib/orderman/orders/PreActivationOrderServices.h"
#include "lib/orderman/orders/spreader_orders/spreader/external_interfacing/SpreaderOrderBuilder.h"
#include "lib/orderman/orders/spreader_orders/spreader/hedger/UnhedgedDelta.h"
#include "lib/orderman/orders/spreader_orders/spreader/hedger/HedgerManager.h"
namespace bts{
namespace orderman {
@@ -20,22 +21,13 @@ class SingleFillHedger;
class SpreaderBookkeepingData
{
private:
const bool _is_live;
const bool _inited_new;
const size_t _n_legs;
const size_t _n_quoting_legs;
// when the order is not created from the disk
// (ie created live) this is set to true
const bool _inited_new;
std::vector<LegPropertyData> _leg_prop_data;
std::vector<Book> _books;
const api::orderman::OrderDefinition _order_def;
std::vector<SpreaderOrderBuilder> _quoter_order_builders;
std::vector<SpreaderOrderBuilder> _hedger_order_builders;
std::vector<SpreaderChildOrderState> _quoter_child_order_states;
std::vector<SingleLegQuoter> _quoters;
std::vector<std::vector<SingleFillHedger>> _hedgers;
std::vector<UnhedgedDelta> _unhedged_deltas;
std::vector<SpreaderLegData> _leg_data;
std::vector<SpreaderLegData> _leg_data;
boost::container::flat_map<uint32_t, size_t> _sec_key_to_index;
// state thats sent out in report (its kept here amd modified
// vs creating a new one everytime)
@@ -43,6 +35,7 @@ private:
Price _input_price;
Price _effective_price;
Quantity _quantity;
private:
size_t _calc_n_quoting_legs(const std::vector<LegPropertyData>& legs);
public:
SpreaderBookkeepingData (ISpreaderManager& spreader_manager,
@@ -50,11 +43,15 @@ public:
const api::orderman::OrderDefinition& order_def,
const SpreaderPreActivationOrderServices& spreader_pao_services,
const std::vector<LegPropertyData>& legs,
const std::vector<bts::api::orderman::OrderDefinition>& quoter_ord_defs,
const std::vector<bts::api::orderman::OrderDefinition>& hedger_ord_defs,
bool inited_new=true);
SpreaderBookkeepingData (ISpreaderManager& spreader_manager,
const bts::api::orderman::OrderRecord& order_record,
const SpreaderPreActivationOrderServices& spreader_pao_services,
const std::vector<LegPropertyData>& legs);
const SpreaderPreActivationOrderServices& spreader_pao_services,
const std::vector<LegPropertyData>& legs,
const std::vector<bts::api::orderman::OrderDefinition>& quoter_ord_defs,
const std::vector<bts::api::orderman::OrderDefinition>& hedger_ord_defs);
SpreaderBookkeepingData()=delete;
SpreaderBookkeepingData (const SpreaderBookkeepingData&) = delete;
SpreaderBookkeepingData (SpreaderBookkeepingData&&) = delete;
@@ -106,12 +103,14 @@ public:
// and returns a ref to it
// (dont keep pointers to hedgers since the vector
// can resize)
SingleFillHedger& add_new_hedger(ISpreaderManager& spreader_manager,
size_t leg_index,
bts::Price hedge_price,
bts::Quantity hedge_quantity,
SpreaderOrderServices *services,
trace::CorID const &);
inline SingleFillHedger& add_new_hedger(size_t leg_index,
Price hedge_price,
Quantity hedge_quantity,
const trace::CorID& trace){
bts_assert(leg_index < _leg_data.size());
SpreaderLegData& leg_data=_leg_data[leg_index];
return leg_data.add_new_hedger(hedge_price,hedge_quantity,trace);
}
// finds the spreader fill, price (ie averages it out, using
// the size and price ratio, across all legs)
void total_fills (Quantity& out_quantity, Price& out_price) const;
@@ -0,0 +1,63 @@
#pragma once
#include <memory>
#include "lib/config/config.h"
#include "lib/config/protos/spreader.pb.h"
namespace bts {
namespace orderman {
class CacheWarmingState{
public:
// read from config which enables the machinery to
// push fake fills or not
const bool enable_fills;
const bool enable_child_order_activate;
// period at which fake fills are generated
// (this is a random number generated from a lower
// and upper bound)
const uint32_t fill_fire_period;
bool in_cache_warming_cycle;
log::Level llevel;
Quantity forced_fill_qty;
double fill_mult=1;
bts::TimeStamp last_cache_warm_fill_ts;
CacheWarmingState()
:
enable_fills(config::get<config::Spreader>().cache_warming().enable_fills()),
enable_child_order_activate(config::get<config::Spreader>().cache_warming().enable_child_order_activate()),
fill_fire_period(
SpreaderUtils::random_number(
config::get<config::Spreader>().cache_warming().fill_period_low(),
config::get<config::Spreader>().cache_warming().fill_period_high()
)),
in_cache_warming_cycle(false),
llevel(Info),
forced_fill_qty(0),
fill_mult(1),
last_cache_warm_fill_ts()
{}
void set_forced_flag(){
in_cache_warming_cycle=true;
llevel = NoLog;
forced_fill_qty=Quantity(100);
fill_mult=0.0;
}
void unset_forced_flag(){
in_cache_warming_cycle=false;
llevel = Info;
forced_fill_qty=Quantity(0);
fill_mult=1.0;
}
Quantity comp_fill_qty(const SpreaderChildOrderState& o) const{
//if in_cache_warming_cycle == true, o.last_update_filled_quantity()=0
//so it will return forced_fill_qty;
//else forced_fill_qty == 0, it will return last_update_filled_quantity
return Quantity(o.last_update_filled_quantity().value() +
forced_fill_qty.value());
}
bool is_fill(const SpreaderChildOrderState& o) const{
return comp_fill_qty(o).value()>0;
}
};
}}
@@ -3,6 +3,7 @@
namespace bts{
namespace orderman{
SpreaderChildOrderState::SpreaderChildOrderState (SpreaderChildOrderState&& src)
:
_is_live(src._is_live),
@@ -21,48 +22,42 @@ _filled_quantity(src._filled_quantity),
_last_filled_price(src._last_filled_price),
_last_reported_order_price(src._last_reported_order_price),
_last_filled_quantity(src._last_filled_quantity),
_last_update_trace(src._last_update_trace),
_out_price(src._out_price),
_out_quantity(src._out_quantity),
_order_place_time(src._order_place_time)
{ }
SpreaderChildOrderState::SpreaderChildOrderState (bts::orderman::ChildOrder& child_order,
SecurityKey sec_key,
SpreaderChildOrderState::SpreaderChildOrderState (SecurityKey sec_key,
bool is_live,
bool is_quoting)
:
_is_live (is_live),
_sec_key (sec_key),
_is_quoting (is_quoting),
_test_only_order_key((uint32_t)bts::Timer::now().time_since_epoch().count()){
_init_base_values();
re_init(child_order);
_test_only_order_key((uint32_t)bts::Timer::now().time_since_epoch().count()),
_base_price(0),
_base_quantity(0){
ChildOrder& empty_order=_empty_order();
re_init(empty_order);
}
void SpreaderChildOrderState::_init_base_values(){
_base_quantity = Quantity(0);
_base_price = Price(0);
}
void SpreaderChildOrderState::_init_running_values(){
_n_cancel_orders = _n_modify_orders = 0;
void SpreaderChildOrderState::re_init (ChildOrder& child_order){
SpreaderUtils::compute_average_price(_base_price, _base_quantity,
_filled_price, _filled_quantity,
_base_price, _base_quantity);
_n_place_orders = 0;
_child_order = bts::orderman::ChildOrder();
_n_cancel_orders = _n_modify_orders = 0;
_child_order = ChildOrder();
_child_order = std::move(child_order);
_status = bts::api::orderman::OrderStatus::ORDER_STATUS_INACTIVE;
_filled_price = Price(0);
_filled_quantity = Quantity (0);
_last_filled_price = Price(0);
_last_reported_order_price=Price(0);
_last_filled_quantity = Quantity(0);
_last_update_trace=trace::CorID();
_out_price = Price(0);
_out_quantity = Quantity(0);
_order_place_time=bts::TimeStamp();
}
void SpreaderChildOrderState::re_init(){
SpreaderUtils::compute_average_price(_base_price, _base_quantity,
_filled_price, _filled_quantity,
_base_price, _base_quantity);
_init_running_values();
}
void SpreaderChildOrderState::re_init (bts::orderman::ChildOrder& child_order){
re_init();
_child_order = std::move(child_order);
}
}}
@@ -19,28 +19,44 @@ private:
const bool _is_live;
const SecurityKey _sec_key;
const bool _is_quoting;
const bts::orderman::OrderKey _test_only_order_key;
const OrderKey _test_only_order_key;
// just for statistics
uint16_t _n_place_orders, _n_cancel_orders;
uint32_t _n_modify_orders;
// while child orders can recycle, their
// accumulated price, qty is kept here
Price _base_price;
Quantity _base_quantity;
bts::orderman::ChildOrder _child_order;
ChildOrder _child_order;
api::orderman::OrderStatus _status;
// this is the total filled qty and
// average price for the active order
Price _filled_price;
Quantity _filled_quantity;
// this is the fill price, qty reported
// in the last update for this order
// (if no fill was reported but it was
// merely an order update, the qty
// here will be 0).
// _last_reported_order_price is the
// the working order price as it was
// last reported
Price _last_filled_price;
Price _last_reported_order_price;
Quantity _last_filled_quantity;
// this is the trace of the order event
// from the last on_order_update call
trace::CorID _last_update_trace;
// this is latest price, qty sent out
Price _out_price;
Quantity _out_quantity;
Quantity _out_quantity;
bts::TimeStamp _order_place_time;
public:
// constructors ///////////////////
///////////////////////////////////
SpreaderChildOrderState() = delete;
SpreaderChildOrderState(const SpreaderChildOrderState&) = delete;
SpreaderChildOrderState(bts::orderman::ChildOrder& child_order,
SecurityKey sec_key,
SpreaderChildOrderState(SecurityKey sec_key,
bool is_live,
bool is_quoting);
SpreaderChildOrderState (SpreaderChildOrderState&& src);
@@ -48,8 +64,11 @@ public:
SpreaderChildOrderState& operator=(SpreaderChildOrderState&&) = delete;
// const accessors ////////////////////
///////////////////////////////////////
bts::orderman::OrderKey order_key() const {
return _is_live ? _child_order.key() : _test_only_order_key;
OrderKey order_key() const {
if (_is_live)
return _child_order.key();
else
return _test_only_order_key;
}
api::orderman::OrderStatus status () const {
return _status;
@@ -97,77 +116,92 @@ public:
bts::TimeStamp order_place_time() const{
return _order_place_time;
}
const trace::CorID& last_update_trace() const{
return _last_update_trace;
}
// init ///////////////////
///////////////////////////
// zero's out base values
void _init_base_values();
// zero's out session last price/qty
void _init_running_values ();
void re_init();
// this assignes a new order, and subsumes the earlier order's
// fills and filled price into the base quantity and
// base filled price
void re_init (bts::orderman::ChildOrder& child_order);
void re_init (ChildOrder& child_order);
ChildOrder& _empty_order(){
_child_order = ChildOrder();
return _child_order;
}
/*************************on order updates**************************/
// OrderStateUpdateEvent gives total filled qty and
// last filled price
void on_order_update(const bts::orderman::OrderStateUpdateEvent& e){
void on_order_update(const OrderStateUpdateEvent& e){
bts_assert(!_is_live || e.key == order_key());
bts_assert(e.filled_quantity >= _filled_quantity.value());
_status = e.status;
_last_filled_quantity = Quantity(e.filled_quantity - _filled_quantity.value());
_last_filled_quantity = Quantity(e.filled_quantity
- _filled_quantity.value());
_last_filled_price = Price(e.last_price);
_last_reported_order_price=Price(e.price);
_last_update_trace=e.trace;
SpreaderUtils::compute_average_price(_filled_price,
_filled_quantity,
_last_filled_price,
_last_filled_quantity,
_filled_price,
_filled_quantity);
}
_filled_quantity,
_last_filled_price,
_last_filled_quantity,
_filled_price,
_filled_quantity);
}
// order actions ////////////////////
/////////////////////////////////////
void place_order(Price price,
Quantity quantity,
bts::TimeStamp curr_time,
const trace::CorID& trace){
auto start = utils::TimeStamp::now();
_out_price = price;
_out_quantity = quantity;
_order_place_time=curr_time;
bts_assert(_n_place_orders == 0);
_n_place_orders++;
LOG(OrderMan, Info, "SpreaderChildOrderState::place_order "
"key:{} is_quoting:{} sec_key:{} live:{} "
"price:{} qty:{} cid:{}",
order_key(), _is_quoting, _sec_key,
_is_live, price, quantity, trace.sn);
if (_is_live)
_child_order.activate(price, quantity, trace);
LOG(OrderMan, Info, "SpreaderChildOrderState::place_order "
"is_quoting:{} key:{} sec_key:{} live:{} "
"price:{} qty:{} cid:{} t:{}",
_is_quoting, order_key(), _sec_key,
_is_live, price, quantity, trace.sn,
(utils::TimeStamp::now()-start).nanoseconds());
}
void modify_order(Price price){
trace::CorID dont_care;
modify_order(price,_out_quantity,dont_care);
}
void modify_order(Price price, Quantity quantity){
trace::CorID dont_care;
modify_order(price,quantity,dont_care);
}
void modify_order(Price price, Quantity quantity, trace::CorID &trace){
auto start = utils::TimeStamp::now();
_out_price = price;
_out_quantity = quantity;
_n_modify_orders++;
LOG(OrderMan, Info, "SpreaderChildOrderState::modify_order "
"key:{} is_quoting:{} sec_key:{} live:{} "
"price:{} quantity:{} mod_no:{}",
order_key(), _is_quoting, _sec_key,
_is_live, price, quantity, _n_modify_orders);
if (_is_live) {
_n_modify_orders++;
if (_is_live)
_child_order.update(price, quantity, trace);
}
LOG(OrderMan, Info, "SpreaderChildOrderState::modify_order "
"is_quoting:{} key:{} sec_key:{} live:{} "
"price:{} quantity:{} mod_no:{} t:{}",
_is_quoting, order_key(), _sec_key,
_is_live, price, quantity, _n_modify_orders,
(utils::TimeStamp::now()-start).nanoseconds());
}
void cancel_order(){
bts_assert(_n_cancel_orders == 0);
LOG(OrderMan, Info, "SpreaderChildOrderState::cancel_order order_key: {} is_quoting: {} sec_key: {} live: {} ",
LOG(OrderMan, Info, "SpreaderChildOrderState::cancel_order order_key: {} "
"is_quoting: {} sec_key: {} live: {} ",
order_key().value(), _is_quoting, _sec_key, _is_live);
_n_cancel_orders++;
if (_is_live)
_child_order.cancel();
}
void keep_order_warm(){
_child_order.warm_up_activate();
}
};
}}
@@ -1,39 +0,0 @@
#pragma once
#include "SingleFillHedger.h"
#include "SpreaderChildOrderState.h"
namespace bts{
namespace orderman{
// Pairs up the hedger with its order; this is stored
// in a vector, inside of SpreaderLegData
class SpreaderHedgerData
{
public:
SpreaderChildOrderState _child_order_state;
SingleFillHedger _hedger;
public:
SpreaderHedgerData () = delete;
SpreaderHedgerData (const SpreaderHedgerData&) = delete;
SpreaderHedgerData (SpreaderHedgerData&& src)
:
_child_order_state(std::move(src._child_order_state)),
_hedger(src._hedger, _child_order_state)
{}
SpreaderHedgerData& operator=(const SpreaderHedgerData&) = delete;
SpreaderHedgerData& operator=(SpreaderHedgerData&&) = delete;
SpreaderHedgerData (ISpreaderManager& spreader_manager,
const LegPropertyData& leg_property_data,
bts::orderman::ChildOrder& child_order,
bool is_live,
bts::Price hedge_price,
bts::Quantity hedge_quantity)
: _child_order_state (child_order, leg_property_data, is_live, false),
_hedger (spreader_manager, leg_property_data, _child_order_state, hedge_price, hedge_quantity)
{
}
SingleFillHedger& hedger () {return _hedger;}
SpreaderChildOrderState& order_update_message() {return _child_order_state;}
};
}}
@@ -3,36 +3,38 @@
namespace bts{
namespace orderman{
SpreaderLegData::SpreaderLegData (const LegPropertyData& leg_property_data,
SingleLegQuoter& quoter,
SpreaderOrderBuilder& quoter_order_builder,
SpreaderOrderBuilder& hedger_order_builder,
std::vector<SingleFillHedger>& hedger_data_vector,
SpreaderChildOrderState& quoter_child_order_state,
Book& book,
UnhedgedDelta& unhedged_delta)
:
_leg_property_data (leg_property_data),
_quoter(quoter),
_quoter_order_builder (quoter_order_builder),
_hedger_order_builder (hedger_order_builder),
_quoter_child_order_state (quoter_child_order_state),
_hedger_data_vector (hedger_data_vector),
_book(book),
_unhedged_delta(unhedged_delta),
_settlement_price(md::INT_NAN)
{}
// SpreaderLegData::SpreaderLegData (const LegPropertyData& leg_property_data,
// SingleLegQuoter* quoter,
// SpreaderOrderBuilder* quoter_order_builder,
// SpreaderOrderBuilder& hedger_order_builder,
// HedgerManager& hedger_manager,
// SpreaderChildOrderState* quoter_child_order_state,
// Book& book,
// UnhedgedDelta& unhedged_delta)
// :
// _is_quoting(leg_property_data.is_active_quoting()),
// _leg_property_data (leg_property_data),
// _quoter(quoter),
// _quoter_order_builder (quoter_order_builder),
// _hedger_order_builder (hedger_order_builder),
// _quoter_child_order_state (quoter_child_order_state),
// _hedger_manager(hedger_manager),
// _book(book),
// _unhedged_delta(unhedged_delta),
// _settlement_price(md::INT_NAN)
// {}
SpreaderLegData::SpreaderLegData(SpreaderLegData&& other)
:
_is_quoting(other._is_quoting),
_leg_property_data (other._leg_property_data),
_quoter (other._quoter),
_quoter_order_builder(other._quoter_order_builder),
_hedger_order_builder(other._hedger_order_builder),
_quoter_child_order_state (other._quoter_child_order_state),
_hedger_data_vector (other._hedger_data_vector),
_quoter (other._quoter),
_hedger_manager(std::move(other._hedger_manager)),
_book(other._book),
_unhedged_delta(other._unhedged_delta){
_unhedged_delta(other._unhedged_delta),
_quoter_order_builder(other._quoter_order_builder),
_hedger_order_builder(other._hedger_order_builder){
bts_assert (false);
}
@@ -2,6 +2,7 @@
#include "LegPropertyData.h"
#include "SingleLegQuoter.h"
#include "SingleFillHedger.h"
#include "lib/orderman/orders/spreader_orders/spreader/hedger/HedgerManager.h"
#include "SpreaderChildOrderState.h"
#include "lib/orderman/actors/order_manager/OrderBuilder.h"
#include "lib/orderman/orders/spreader_orders/spreader/external_interfacing/SpreaderOrderBuilder.h"
@@ -21,27 +22,50 @@ namespace orderman{
// SpreaderBookkeeping. all the data except
// for the Book and LegPropertyData
// are references to SpreaderBookkeeping
class SpreaderLegData
{
class SpreaderLegData{
private:
const LegPropertyData& _leg_property_data;
SingleLegQuoter& _quoter;
SpreaderOrderBuilder& _quoter_order_builder;
SpreaderOrderBuilder& _hedger_order_builder;
SpreaderChildOrderState& _quoter_child_order_state;
std::vector<SingleFillHedger>& _hedger_data_vector;
Book& _book;
UnhedgedDelta& _unhedged_delta;
const bool _is_quoting;
const LegPropertyData _leg_property_data;
SpreaderChildOrderState* const _quoter_child_order_state;
SingleLegQuoter* const _quoter;
HedgerManager _hedger_manager;
Book _book;
UnhedgedDelta _unhedged_delta;
md::IntPrice _settlement_price;
SpreaderOrderBuilder* const _quoter_order_builder;
SpreaderOrderBuilder _hedger_order_builder;
public:
SpreaderLegData (const LegPropertyData& leg_property_data,
SingleLegQuoter& quoter,
SpreaderOrderBuilder& quoter_order_builder,
SpreaderOrderBuilder& hedger_order_builder,
std::vector<SingleFillHedger>& hedger_data_vector,
SpreaderChildOrderState& quoter_child_order_state,
Book& book,
UnhedgedDelta& unhedged_delta);
SpreaderLegData (const SpreaderPreActivationOrderServices& services,
OrderKey spreader_order_key,
size_t n_legs,
ISpreaderManager& spreader_manager,
const bts::api::orderman::OrderDefinition& quoter_ord_def,
const bts::api::orderman::OrderDefinition& hedger_ord_def,
const LegPropertyData& leg_property_data)
:
_is_quoting(leg_property_data.is_active_quoting()),
_leg_property_data (leg_property_data),
_quoter_child_order_state (_is_quoting ? new SpreaderChildOrderState(
_leg_property_data.leg_security_key(),
services.is_live(),
true)
: nullptr),
_quoter(_is_quoting ? new SingleLegQuoter(spreader_manager,
_leg_property_data,
n_legs,
*_quoter_child_order_state)
: nullptr),
_hedger_manager(spreader_manager,_leg_property_data,services.is_live()),
_book(),
_unhedged_delta(),
_settlement_price(md::INT_NAN),
_quoter_order_builder (_is_quoting ? new SpreaderOrderBuilder(
spreader_order_key,
quoter_ord_def,
services,10)
: nullptr),
_hedger_order_builder (spreader_order_key,hedger_ord_def,services,10)
{}
SpreaderLegData() = delete;
SpreaderLegData(const SpreaderLegData&) = delete;
// this cons is not deleted since for each leg,
@@ -55,51 +79,81 @@ public:
SpreaderLegData& operator=(SpreaderLegData&&) = delete;
// accessors ////////////////////
/////////////////////////////////
const LegPropertyData& leg_property_data() const {return _leg_property_data;}
SingleLegQuoter& quoter() {return _quoter;}
const SingleLegQuoter& quoter() const {return _quoter;}
const bts::orderman::Book& book () const {return _book;}
Price settlement_price() const {return Price(_settlement_price.price());}
bool settlement_populated() const {return !os::is_nan(_settlement_price.price());}
SpreaderChildOrderState& order_update_message () {return _quoter_child_order_state;}
bts::orderman::OrderKey key() {return _quoter_child_order_state.order_key();}
bts::orderman::OrderKey order_key() {return _quoter_child_order_state.order_key();}
SpreaderOrderBuilder& quoter_order_builder() {return _quoter_order_builder;}
SpreaderOrderBuilder& hedger_order_builder() {return _hedger_order_builder;}
UnhedgedDelta& unhedged_delta() {return _unhedged_delta;}
// returns the number of active hedgers
size_t n_hedgers() const { return _hedger_data_vector.size(); }
// returns the hedger at a given index
SingleFillHedger& get_hedger(size_t index) {
bts_assert(index<_hedger_data_vector.size());
return _hedger_data_vector[index];
const LegPropertyData& leg_property_data() const {
return _leg_property_data;
}
SingleLegQuoter& quoter() {
bts_assert(_is_quoting);
return *_quoter;
}
const SingleLegQuoter& quoter() const {
bts_assert(_is_quoting);
return *_quoter;
}
const bts::orderman::Book& book () const {
return _book;
}
Price settlement_price() const {
return Price(_settlement_price.price());
}
bool settlement_populated() const {
return !os::is_nan(_settlement_price.price());
}
SpreaderChildOrderState& order_update_message () {
//bts_assert(_is_quoting);
if(!_is_quoting)
bts_assert(false);
return *_quoter_child_order_state;
}
const SpreaderChildOrderState& order_update_message () const {
//bts_assert(_is_quoting);
if(!_is_quoting)
bts_assert(false);
return *_quoter_child_order_state;
}
bts::orderman::OrderKey key() const {
return order_update_message().order_key();
}
bts::orderman::OrderKey order_key() const {
return order_update_message().order_key();
}
SpreaderOrderBuilder& quoter_order_builder() {
return *_quoter_order_builder;
}
SpreaderOrderBuilder& hedger_order_builder() {
return _hedger_order_builder;
}
UnhedgedDelta& unhedged_delta() {
return _unhedged_delta;
}
size_t n_active_hedgers() const {
return _hedger_manager.n_active_hedgers();
}
HedgerManager& hedger_manager(){
return _hedger_manager;
}
// returns a hedger from its order_key
SingleFillHedger* find_hedger_from_orderkey(bts::orderman::OrderKey order_key){
for (SingleFillHedger& hedger : _hedger_data_vector)
if (hedger.order_state().order_key() == order_key)
return &hedger;
return nullptr;
SingleFillHedger* find_hedger_from_orderkey(OrderKey order_key){
return _hedger_manager.find_hedger_from_orderkey(order_key);
}
// computes the total fills and the average price of
// the fills - across the quoter and all the hedgers
void total_fills (Quantity& out_quantity,
Price& out_price) const{
_quoter_child_order_state.total_fills(out_quantity, out_price);
double sum = out_quantity.value() * out_price.value();
for (SingleFillHedger& hedger : _hedger_data_vector){
Quantity hedger_quantity; Price hedger_price;
hedger.order_state().total_fills(hedger_quantity, hedger_price);
sum += hedger_quantity.value() * hedger_price.value();
out_quantity = Quantity(out_quantity.value() + hedger_quantity.value());
Price qp=Price(0), hp;
Quantity qq=Quantity(0), hq;
if(_is_quoting){
const auto& quoting_order = order_update_message();
quoting_order.total_fills(qq, qp);
}
out_price = out_quantity.value() == 0 ? Price(0) : Price(sum / out_quantity.value());
_hedger_manager.total_fills(hq,hp);
SpreaderUtils::compute_average_price(qp,qq,hp,hq,out_price,out_quantity);
}
Quantity total_fills() const{
uint32_t rv = _quoter_child_order_state.total_fills().value();
for (SingleFillHedger& hedger : _hedger_data_vector)
rv += hedger.order_state().total_fills().value();
return Quantity(rv);
Price out_p;
Quantity out_q;
total_fills (out_q, out_p);
return out_q;
}
// mutators ////////////////////
////////////////////////////////
@@ -120,5 +174,16 @@ public:
_book);
}
// returns the hedger at a given index
SingleFillHedger& get_hedger_test_only(size_t index) {
return _hedger_manager.test_only_get_hedger_at(index);
}
SingleFillHedger& add_new_hedger(bts::Price hedge_price,
bts::Quantity hedge_quantity,
const trace::CorID& trace){
SingleFillHedger& rv = _hedger_manager.get_hedger();
rv.init_hedger (hedge_price,hedge_quantity,trace);
return rv;
}
};
}}
@@ -17,6 +17,8 @@ api::Side SpreaderUtils::get_leg_side(api::Side spreader_order_side,
bool SpreaderUtils::resolve_synthetic_spread (const SpreaderPreActivationOrderServices& spreader_pao_services,
const api::orderman::OrderDefinition& order_def,
std::vector<LegPropertyData>& out_resolved,
std::vector<bts::api::orderman::OrderDefinition>& out_quoter_defs,
std::vector<bts::api::orderman::OrderDefinition>& out_hedger_defs,
bool resolve_synthetic){
bts_assert(order_def.has_custom_def() && order_def.custom_def().has_spreader());
const bts::api::Security* sec_ptr = resolve_synthetic ? spreader_pao_services.resolve (SecurityKey(order_def.security_key())) :
@@ -51,8 +53,10 @@ bool SpreaderUtils::resolve_synthetic_spread (const SpreaderPreActivationOrderSe
api::orderman::OrderDefinition hedger_order_def = quoter_order_def;
hedger_order_def.set_time_in_force(bts::api::orderman::TimeInForce::TIME_IN_FORCE_DAY);
hedger_order_def.set_sub_type(bts::api::orderman::OrderSubType::ORDER_SUB_TYPE_HEDGING_LEG);
LegPropertyData leg_property_data(leg_idx, leg, leg_params, leg_order_side, quoter_order_def, hedger_order_def);
LegPropertyData leg_property_data(leg_idx, leg, leg_params, leg_order_side);
out_resolved.push_back(leg_property_data);
out_quoter_defs.push_back(quoter_order_def);
out_hedger_defs.push_back(hedger_order_def);
leg_idx++;
}
else{
@@ -9,6 +9,7 @@
#include "lib/orderman/orders/spreader_orders/spreader/quoter/QuoteLegDriver.h"
#include "lib/api/services/OrderService.pb.h"
#include "lib/orderman/actors/order_manager/OrderBuilder.h"
#include <random>
namespace bts {
namespace orderman {
@@ -50,6 +51,20 @@ public:
bts_assert (false);
return Quantity(0);
}
static bool resolve_synthetic_spread (
const SpreaderPreActivationOrderServices& spreader_pao_services,
const api::orderman::OrderDefinition& order_def,
std::vector<LegPropertyData>& out_resolved,
bool resolve_synthetic = true){
std::vector<bts::api::orderman::OrderDefinition> out_quoter_defs;
std::vector<bts::api::orderman::OrderDefinition> out_hedger_defs;
return resolve_synthetic_spread(spreader_pao_services,
order_def,
out_resolved,
out_quoter_defs,
out_hedger_defs,
resolve_synthetic);
}
// Resolves the securities and returns LegPropertyData for eacg
// Returns false if
// i. can not resolve the spreader security
@@ -59,6 +74,8 @@ public:
static bool resolve_synthetic_spread (const SpreaderPreActivationOrderServices& spreader_pao_services,
const api::orderman::OrderDefinition& order_def,
std::vector<LegPropertyData>& out_resolved,
std::vector<bts::api::orderman::OrderDefinition>& out_quoter_defs,
std::vector<bts::api::orderman::OrderDefinition>& out_hedger_defs,
bool resolve_synthetic = true);
////////////////////////////////////////////////////
// orders //////////////////////////////////////////
@@ -763,7 +780,7 @@ public:
// rounds a double to a whole number if its too close
// to it (this is ued in the case of rounding errors
// when 0.9999999 or 1.0000001 is really 1)
static double round_if_near_whole(double qty,
inline static double round_if_near_whole(double qty,
double vicinity=0.000000001){
double round_qty = std::round(qty);
if (std::abs(round_qty - qty) <= vicinity)
@@ -799,6 +816,12 @@ public:
static uint32_t _round_leg_qty_computed_from_another_leg_qty (double qty){
return static_cast<uint32_t>(std::round(qty));
}
static double random_number(double min, double max){
std::random_device rd;
std::mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd()
std::uniform_real_distribution<> dis(min, max);
return dis(gen);
}
/**********************************************************************/
/**************************debugging helpers***************************/
/**********************************************************************/
@@ -81,7 +81,11 @@ public:
const SpreaderPreActivationOrderServices& spreader_pao_services,
size_t reserve_sz);
SpreaderOrderBuilder() = delete;
SpreaderOrderBuilder(const SpreaderOrderBuilder&) = delete;
SpreaderOrderBuilder(const SpreaderOrderBuilder&)
:
_is_live(true){
bts_assert(false);
}
SpreaderOrderBuilder(SpreaderOrderBuilder&& other);
SpreaderOrderBuilder& operator=(const SpreaderOrderBuilder) = delete;
SpreaderOrderBuilder& operator=(SpreaderOrderBuilder&& other) = delete;
@@ -10,54 +10,45 @@ class HedgePriceQuantityState
private:
// these const's are the price, qty and
// side of the hedger order
const Price _target_price;
const Quantity _target_quantity;
const api::Side _side;
Price _target_price;
Quantity _target_quantity;
// filled part of the order, updated as fills come in
Quantity _aggregated_filled_quantity;
// the price of the "active" hedger order
Price _out_price;
Quantity _out_quantity;
public:
HedgePriceQuantityState(Price target_price, Quantity target_quantity, api::Side target_side)
HedgePriceQuantityState(Price target_price,
Quantity target_quantity)
:
_target_price(target_price),
_target_quantity(target_quantity),
_side (target_side),
_aggregated_filled_quantity(0),
_out_price (0),
_out_quantity (0)
_aggregated_filled_quantity(0)
{
}
Price target_price() const { return _target_price; }
Quantity target_quantity() const { return _target_quantity; }
api::Side side() const { return _side; }
Quantity aggregated_filled_quantity() const { return _aggregated_filled_quantity; }
Price out_price () const {return _out_price;}
Quantity out_quantity () const {return _out_quantity;}
}
void re_init(Price price,
Quantity quantity){
_target_price = price;
_target_quantity = quantity;
_aggregated_filled_quantity = Quantity(0);
}
Price target_price() const {
return _target_price;
}
Quantity target_quantity() const {
return _target_quantity;
}
Quantity aggregated_filled_quantity() const {
return _aggregated_filled_quantity;
}
// this is the order quantity net of the fills
Quantity remaining_to_fill() const
{
uint32_t rv = _aggregated_filled_quantity.value() >= _target_quantity.value() ? 0 : _target_quantity.value() - _aggregated_filled_quantity.value();
Quantity remaining_to_fill() const{
uint32_t rv =
_aggregated_filled_quantity.value() >= _target_quantity.value() ? 0 :
_target_quantity.value() - _aggregated_filled_quantity.value();
return Quantity(rv);
}
// called as the hedge order gets filled
void update_fill(Quantity fill_quantity)
{
_aggregated_filled_quantity = Quantity(_aggregated_filled_quantity.value() + fill_quantity.value());
void update_fill(Quantity fill_quantity){
_aggregated_filled_quantity =
Quantity(_aggregated_filled_quantity.value() + fill_quantity.value());
}
void update_out_price_quantity (Price out_price, Quantity out_quantity)
{
_out_price = out_price;
_out_quantity = out_quantity;
}
void update_out_price (Price out_price)
{
_out_price = out_price;
}
void clear_out_price_quantity ()
{
update_out_price_quantity (Price (0), Quantity (0));
}
};
}}
@@ -0,0 +1,37 @@
#include "HedgerManager.h"
namespace bts{
namespace orderman{
HedgerManager::HedgerManager(ISpreaderManager& spreader_manager,
const LegPropertyData& leg_property_data,
bool is_live)
:
_is_live(is_live),
_inited(false),
_init_n_hedgers(2),
_spreader_manager(spreader_manager),
_leg_property_data(leg_property_data){
_hedgers.reserve(_init_n_hedgers);
}
void HedgerManager::init_once(){
bts_assert(!_inited);
_inited=true;
bts_assert(_hedgers.size()==0);
for(uint8_t i=0; i<_init_n_hedgers; i++)
_hedgers.emplace_back(_spreader_manager,
_leg_property_data,
_is_live);
}
HedgerManager::HedgerManager (HedgerManager&& other)
:
_is_live(other._is_live),
_inited(other._inited),
_init_n_hedgers(other._init_n_hedgers),
_spreader_manager(other._spreader_manager),
_leg_property_data(other._leg_property_data),
_hedgers(std::move(other._hedgers)){
bts_assert(false);
}
}}
@@ -0,0 +1,108 @@
#pragma once
#include "lib/orderman/orders/spreader_orders/spreader/SingleFillHedger.h"
//#include <boost/container/small_vector.hpp>
namespace bts{
namespace orderman{
class HedgerManager{
private:
const bool _is_live;
bool _inited;
const uint8_t _init_n_hedgers;
ISpreaderManager& _spreader_manager;
const LegPropertyData& _leg_property_data;
std::vector<SingleFillHedger> _hedgers;
//small_vector unf needs operator= which is deleted
//for the Hedger
//boost::container::small_vector<SingleFillHedger, 3> _hedgers;
public:
HedgerManager(ISpreaderManager& spreader_manager,
const LegPropertyData& leg_property_data,
bool is_live);
void init_once();
HedgerManager() = delete;
HedgerManager (const HedgerManager&) = delete;
// to support HedgerManager being indirectly part of a vector
HedgerManager (HedgerManager&& other);
HedgerManager& operator=(const HedgerManager&) = delete;
HedgerManager& operator=(HedgerManager&&) = delete;
// finds a free hedger
inline SingleFillHedger& get_hedger(){
for (auto& h : _hedgers)
if(h.is_free())
return h;
LOG(OrderMan, Error, "HedgerManager::get_hedger allocating new hedger {} ",
_hedgers.size());
_hedgers.emplace_back(_spreader_manager,
_leg_property_data,
_is_live);
return _hedgers.back();
}
// finds a hedger that is working on
// an order with the specified key
SingleFillHedger* find_hedger_from_orderkey(OrderKey order_key){
for (SingleFillHedger& hedger : _hedgers)
if (hedger.is_active_order() &&
hedger.order_state().order_key() == order_key)
return &hedger;
return nullptr;
}
// udpates all the hedgers with the book update
void update_book (const Book& book, trace::CorID& cid){
for (SingleFillHedger& hedger : _hedgers)
hedger.on_book(book, cid);
}
// udpates all the hedgers with the book update
void update_time(const bts::TimeStamp& ts){
for (SingleFillHedger& hedger : _hedgers)
hedger.on_time(ts);
}
// cancels all the hedgers
void cancel_hedges(){
for (SingleFillHedger& hedger : _hedgers)
hedger.cancel_hedge();
}
// terminates all the hedgers
void terminate_hedges(){
for (SingleFillHedger& hedger : _hedgers)
hedger.terminate();
}
// returns the number of hedgers that
// have an active working order
size_t n_active_hedgers() const {
size_t rv=0;
for (const SingleFillHedger& hedger : _hedgers)
if(hedger.is_active_order())
rv++;
return rv;
}
// computes the total fills and average
// fill price across all the hedgers
inline void total_fills(Quantity& out_qty,
Price& out_price) const{
double sum = 0.0;
uint32_t tot_qty = 0;
for (const SingleFillHedger& hedger : _hedgers){
Quantity hq; Price hp;
hedger.order_state().total_fills(hq, hp);
sum += hq.value() * hp.value();
tot_qty += hq.value();
}
out_qty = Quantity(tot_qty);
out_price = tot_qty == 0 ? Price(0.0) : Price(sum / tot_qty);
}
public:
///////////////////////////
// test only //////////////
SingleFillHedger& test_only_get_hedger_at(size_t index) {
bts_assert(index<_hedgers.size());
return _hedgers[index];
}
size_t test_only_pool_size() const{
return _hedgers.size();
}
};
}}
@@ -3,6 +3,7 @@
#include "lib/utils/bts_chrono.h"
#include "lib/orderman/orderman_utils/orderman_common.h"
#include "lib/orderman/orders/spreader_orders/spreader/hedger/hedger_states/HedgerState.h"
#include "lib/orderman/actors/order_manager/ChildOrder.h"
namespace bts {
namespace trace {struct CorID;}
@@ -20,7 +21,9 @@ public:
// string representation of the state
virtual const std::string state_text () const = 0;
// called once and only once, before any other calls.
virtual void init () = 0;
virtual void init (Price price,
Quantity quantity,
const trace::CorID&) = 0;
// called only when this hedger's order is updated
virtual void on_order_update (const SpreaderChildOrderState& order_update) = 0;
// called only when the security that the hedger is working on
@@ -1,15 +1,27 @@
#include "lib/orderman/orders/spreader_orders/spreader/hedger/hedger_states/HedgerEndStateWorker.h"
#include "lib/orderman/orders/spreader_orders/spreader/SpreaderChildOrderState.h"
#include "lib/orderman/orders/spreader_orders/spreader/SingleFillHedger.h"
namespace bts{
namespace orderman{
void HedgerEndStateWorker::init(){
void HedgerEndStateWorker::on_transition (){
// this is to allow the hedger to be re-used.
// the state is cleared and the hedger returns
// to the start state, waiting for the init call
if(!_hedger->_is_permenantly_cancelled)
_hedger->transform_to_START();
}
void HedgerEndStateWorker::init (Price,
Quantity,
const trace::CorID&){
bts_assert_msg (false, "HedgerEndStateWorker::init illegal operation");
}
void HedgerEndStateWorker::on_order_update(const SpreaderChildOrderState&){
}
void HedgerEndStateWorker::on_book(const bts::orderman::Book&,trace::CorID &){
void HedgerEndStateWorker::on_book(const Book&,
trace::CorID &){
}
void HedgerEndStateWorker::cancel(){
}
@@ -9,15 +9,23 @@ class SingleFillHedger;
// in the former we know that the order has reached its terminal state
class HedgerEndStateWorker : public IHedgerStateWorker
{
private:
SingleFillHedger* const _hedger;
public:
HedgerEndStateWorker (SingleFillHedger*){}
HedgerEndStateWorker (SingleFillHedger*, const HedgerEndStateWorker&){}
void on_transition () override {}
HedgerEndStateWorker (SingleFillHedger* hedger)
:
_hedger(hedger)
{}
HedgerEndStateWorker (SingleFillHedger* hedger,
const HedgerEndStateWorker&)
: HedgerEndStateWorker (hedger){
}
void on_transition () override;
HedgerState state () const override {return HedgerState::END;}
const std::string state_text () const override {return "END";}
void init () override;
void init (Price price,Quantity quantity,const trace::CorID& trace) override;
void on_order_update (const SpreaderChildOrderState& order_update) override;
void on_book (const bts::orderman::Book& book,trace::CorID &) override;
void on_book (const Book& book,trace::CorID &) override;
void cancel() override;
void terminate() override;
void on_time(const TimeStamp&) override;
@@ -6,21 +6,28 @@
namespace bts{
namespace orderman{
void HedgerStartStateWorker::init(){
LOG(OrderMan, Diag, "HedgerStartStateWorker::init order_key: {}, placing order price: {} quantity: {}",
_hedger->_key().value(), _hedger->_price_quantity_state.out_price().value(), _hedger->_price_quantity_state.target_quantity().value());
// this is the only place from where the hedger sends the order
_hedger->_price_quantity_state.update_out_price_quantity (_hedger->_price_quantity_state.target_price(), _hedger->_price_quantity_state.target_quantity());
_hedger->_place_order (_hedger->_price_quantity_state.out_price(),
_hedger->_price_quantity_state.out_quantity());
_hedger->transform_to_WAITING_FOR_FIRST_ACK ();
void HedgerStartStateWorker::on_transition (){
_hedger->_price_quantity_state.re_init(Price(0),Quantity(0));
size_t leg_idx = _hedger->_leg_property_data.leg_index();
ChildOrder new_order = _hedger->_spreader.get_new_hedge_order(leg_idx);
_hedger->_child_order_state.re_init(new_order);
}
void HedgerStartStateWorker::init (Price price,
Quantity quantity,
const trace::CorID& trace){
_hedger->_price_quantity_state.re_init(price,quantity);
if(quantity.value() > 0){
_hedger->_place_order (price, quantity,trace);
_hedger->transform_to_WAITING_FOR_FIRST_ACK ();
}
}
void HedgerStartStateWorker::on_order_update(const SpreaderChildOrderState&){
bts_assert_msg (false, "HedgerStartStateWorker::on_order_update illegal operation");
bts_assert_msg (false, "HedgerStartStateWorker::on_order_update "
"illegal operation");
}
void HedgerStartStateWorker::on_book(const bts::orderman::Book&,trace::CorID &){
// market data will not be sent until out of start.
bts_assert_msg (false, "HedgerStartStateWorker::on_book illegal operation");
void HedgerStartStateWorker::on_book(const Book&,
trace::CorID &){
}
void HedgerStartStateWorker::cancel(){
_hedger->transform_to_END ();
@@ -4,7 +4,8 @@
namespace bts{
namespace orderman{
// this is the starting node. since the hedger
// can be reused, the end state can lead back to this
class HedgerStartStateWorker : public IHedgerStateWorker
{
private:
@@ -13,15 +14,16 @@ public:
HedgerStartStateWorker (SingleFillHedger* hedger)
: _hedger(hedger)
{}
HedgerStartStateWorker (SingleFillHedger* hedger, const HedgerStartStateWorker&)
HedgerStartStateWorker (SingleFillHedger* hedger,
const HedgerStartStateWorker&)
: HedgerStartStateWorker (hedger)
{}
void on_transition () override {}
void on_transition () override;
HedgerState state () const override {return HedgerState::START;}
const std::string state_text () const override {return "START";}
void init () override;
void init (Price price,Quantity quantity,const trace::CorID&) override;
void on_order_update (const SpreaderChildOrderState& order_update) override;
void on_book (const bts::orderman::Book& book,trace::CorID &) override;
void on_book (const Book& book,trace::CorID &) override;
void cancel() override;
void terminate() override;
void on_time(const TimeStamp&) override;
@@ -5,17 +5,17 @@
namespace bts{
namespace orderman{
void HedgerWaitingForCancelAckStateWorker::init(){
bts_assert_msg (false, "HedgerWaitingForCancelAckStateWorker::init illegal operation");
void HedgerWaitingForCancelAckStateWorker::init (Price,
Quantity,
const trace::CorID&){
bts_assert_msg (false, "HedgerWaitingForCancelAckStateWorker::init "
"illegal operation");
}
void HedgerWaitingForCancelAckStateWorker::on_order_update(const SpreaderChildOrderState& order_update){
bts_assert (order_update.acknowledged());
_hedger->_price_quantity_state.update_fill (order_update.last_update_filled_quantity());
if (order_update.terminal_status())
{
_hedger->_price_quantity_state.clear_out_price_quantity ();
_hedger->transform_to_END ();
}
}
void HedgerWaitingForCancelAckStateWorker::on_book(const bts::orderman::Book&,trace::CorID &){
}
@@ -13,13 +13,19 @@ public:
HedgerWaitingForCancelAckStateWorker (SingleFillHedger* hedger)
: _hedger(hedger)
{}
HedgerWaitingForCancelAckStateWorker (SingleFillHedger* hedger, const HedgerWaitingForCancelAckStateWorker&)
HedgerWaitingForCancelAckStateWorker (
SingleFillHedger* hedger,
const HedgerWaitingForCancelAckStateWorker&)
: HedgerWaitingForCancelAckStateWorker (hedger)
{}
void on_transition () override {}
HedgerState state () const override {return HedgerState::WAITING_FOR_CANCEL_ACK;}
const std::string state_text () const override {return "WAITING_FOR_CANCEL_ACK";}
void init () override;
HedgerState state () const override {
return HedgerState::WAITING_FOR_CANCEL_ACK;
}
const std::string state_text () const override {
return "WAITING_FOR_CANCEL_ACK";
}
void init (Price price,Quantity quantity,const trace::CorID&) override;
void on_order_update (const SpreaderChildOrderState& order_update) override;
void on_book (const bts::orderman::Book& book,trace::CorID &) override;
void cancel() override;
@@ -6,16 +6,20 @@
namespace bts{
namespace orderman{
void HedgerWaitingForFirstAckCancelledStateWorker::init(){
bts_assert_msg (false, "HedgerWaitingForFirstAckCancelledStateWorker::init illegal operation");
void HedgerWaitingForFirstAckCancelledStateWorker::init(
Price,
Quantity,
const trace::CorID&){
bts_assert_msg (false, "HedgerWaitingForFirstAckCancelledStateWorker::init "
"illegal operation");
}
void HedgerWaitingForFirstAckCancelledStateWorker::on_order_update(const SpreaderChildOrderState& order_update){
void HedgerWaitingForFirstAckCancelledStateWorker::on_order_update(
const SpreaderChildOrderState& order_update){
bts_assert (order_update.acknowledged());
_hedger->_price_quantity_state.update_fill (order_update.last_update_filled_quantity());
if (order_update.terminal_status()){
_hedger->_price_quantity_state.clear_out_price_quantity();
_hedger->transform_to_END ();
}
_hedger->_price_quantity_state.update_fill
(order_update.last_update_filled_quantity());
if (order_update.terminal_status())
_hedger->transform_to_END ();
else{
// this means the first ack is here and we should
// be able to cancel it now
@@ -23,13 +27,14 @@ void HedgerWaitingForFirstAckCancelledStateWorker::on_order_update(const Spreade
_hedger->transform_to_WAITING_FOR_CANCEL_ACK ();
}
}
void HedgerWaitingForFirstAckCancelledStateWorker::on_book(const bts::orderman::Book&,trace::CorID &){
void HedgerWaitingForFirstAckCancelledStateWorker::on_book(const Book&,
trace::CorID &){
}
void HedgerWaitingForFirstAckCancelledStateWorker::cancel(){
}
void HedgerWaitingForFirstAckCancelledStateWorker::terminate(){
_hedger->transform_to_END ();
}
void HedgerWaitingForFirstAckCancelledStateWorker::on_time(const TimeStamp&){
void HedgerWaitingForFirstAckCancelledStateWorker::on_time(const TimeStamp&){
}
}}
}}
@@ -13,15 +13,21 @@ public:
HedgerWaitingForFirstAckCancelledStateWorker (SingleFillHedger* hedger)
: _hedger(hedger)
{}
HedgerWaitingForFirstAckCancelledStateWorker (SingleFillHedger* hedger, const HedgerWaitingForFirstAckCancelledStateWorker&)
HedgerWaitingForFirstAckCancelledStateWorker (
SingleFillHedger* hedger,
const HedgerWaitingForFirstAckCancelledStateWorker&)
: HedgerWaitingForFirstAckCancelledStateWorker (hedger)
{}
void on_transition () override {}
HedgerState state () const override {return HedgerState::WAITING_FOR_FIRST_ACK_CANCELLED;}
const std::string state_text () const override {return "WAITING_FOR_FIRST_ACK_CANCELLED";}
void init () override;
HedgerState state () const override {
return HedgerState::WAITING_FOR_FIRST_ACK_CANCELLED;
}
const std::string state_text () const override {
return "WAITING_FOR_FIRST_ACK_CANCELLED";
}
void init (Price price,Quantity quantity,const trace::CorID& trace) override;
void on_order_update (const SpreaderChildOrderState& order_update) override;
void on_book (const bts::orderman::Book& book,trace::CorID &) override;
void on_book (const Book& book,trace::CorID &) override;
void cancel() override;
void terminate() override;
void on_time(const TimeStamp&) override;
@@ -4,21 +4,25 @@
namespace bts{
namespace orderman{
void HedgerWaitingForFirstAckStateWorker::init(){
bts_assert_msg (false, "HedgerWaitingForFirstAckStateWorker::init illegal operation");
void HedgerWaitingForFirstAckStateWorker::init (Price,
Quantity,
const trace::CorID&){
bts_assert_msg (false, "HedgerWaitingForFirstAckStateWorker::init "
"illegal operation");
}
void HedgerWaitingForFirstAckStateWorker::on_order_update(const SpreaderChildOrderState& order_update){
void HedgerWaitingForFirstAckStateWorker::on_order_update(
const SpreaderChildOrderState& order_update){
bts_assert (order_update.acknowledged());
_hedger->_price_quantity_state.update_fill (order_update.last_update_filled_quantity());
if (order_update.terminal_status()){
_hedger->_price_quantity_state.clear_out_price_quantity ();
_hedger->_price_quantity_state.update_fill(
order_update.last_update_filled_quantity());
if (order_update.terminal_status())
_hedger->transform_to_END ();
}
else
// this means the first ack
_hedger->transform_to_WORKING ();
}
void HedgerWaitingForFirstAckStateWorker::on_book(const bts::orderman::Book&,trace::CorID &){
void HedgerWaitingForFirstAckStateWorker::on_book(const Book&,
trace::CorID &){
}
void HedgerWaitingForFirstAckStateWorker::cancel(){
_hedger->transform_to_WAITING_FOR_FIRST_ACK_CANCELLED ();
@@ -26,6 +30,6 @@ void HedgerWaitingForFirstAckStateWorker::cancel(){
void HedgerWaitingForFirstAckStateWorker::terminate(){
_hedger->transform_to_END ();
}
void HedgerWaitingForFirstAckStateWorker::on_time(const TimeStamp&){
void HedgerWaitingForFirstAckStateWorker::on_time(const TimeStamp&){
}
}}
@@ -13,15 +13,22 @@ public:
HedgerWaitingForFirstAckStateWorker (SingleFillHedger* hedger)
: _hedger(hedger)
{}
HedgerWaitingForFirstAckStateWorker (SingleFillHedger* hedger, const HedgerWaitingForFirstAckStateWorker&)
HedgerWaitingForFirstAckStateWorker (
SingleFillHedger* hedger,
const HedgerWaitingForFirstAckStateWorker&)
: HedgerWaitingForFirstAckStateWorker (hedger)
{}
void on_transition () override {}
HedgerState state () const override {return HedgerState::WAITING_FOR_FIRST_ACK;}
const std::string state_text () const override {return "WAITING_FOR_FIRST_ACK";}
void init () override;
void on_transition () override {
}
HedgerState state () const override {
return HedgerState::WAITING_FOR_FIRST_ACK;
}
const std::string state_text () const override {
return "WAITING_FOR_FIRST_ACK";
}
void init (Price price,Quantity quantity,const trace::CorID& trace) override;
void on_order_update (const SpreaderChildOrderState& order_update) override;
void on_book (const bts::orderman::Book& book,trace::CorID &) override;
void on_book (const Book& book,trace::CorID &) override;
void cancel() override;
void terminate() override;
void on_time(const TimeStamp&) override;
@@ -9,24 +9,30 @@ namespace orderman{
void HedgerWorkingStateWorker::on_transition(){
_order_touch_time = _hedger->_current_time();
}
void HedgerWorkingStateWorker::init(){
void HedgerWorkingStateWorker::init(Price,
Quantity,
const trace::CorID&){
bts_assert_msg (false, "HedgerWorkingStateWorker::init illegal operation");
}
void HedgerWorkingStateWorker::on_order_update(const SpreaderChildOrderState& order_update){
void HedgerWorkingStateWorker::on_order_update(
const SpreaderChildOrderState& order_update){
bts_assert (order_update.acknowledged());
_hedger->_price_quantity_state.update_fill (order_update.last_update_filled_quantity());
if (order_update.terminal_status()){
_hedger->_price_quantity_state.clear_out_price_quantity ();
_hedger->transform_to_END ();
}
_hedger->_price_quantity_state.update_fill(
order_update.last_update_filled_quantity());
if (order_update.terminal_status())
_hedger->transform_to_END ();
}
void HedgerWorkingStateWorker::on_book(const bts::orderman::Book&,trace::CorID &){
void HedgerWorkingStateWorker::on_book(const Book&,
trace::CorID &){
double new_price;
if (SpreaderUtils::post_hedge_market_based_price_action (_hedger->_price_quantity_state.out_price().value(),
_hedger->_price_quantity_state.out_quantity().value(),
_hedger->_price_quantity_state.remaining_to_fill().value(),
_hedger->_leg_property_data,
_hedger->_spreader.get_book (_hedger->_leg_property_data.leg_index()), new_price))
if (SpreaderUtils::post_hedge_market_based_price_action(
_hedger->_child_order_state.out_price().value(),
_hedger->_child_order_state.out_quantity().value(),
_hedger->_price_quantity_state.remaining_to_fill().value(),
_hedger->_leg_property_data,
_hedger->_spreader.get_book(
_hedger->_leg_property_data.leg_index()),
new_price))
_hedger->_modify_order (Price(new_price));
}
void HedgerWorkingStateWorker::cancel(){
@@ -38,8 +44,12 @@ void HedgerWorkingStateWorker::terminate(){
}
void HedgerWorkingStateWorker::on_time(const TimeStamp& ts){
double new_price;
if (SpreaderUtils::post_hedge_time_based_price_action (_order_touch_time, ts, _hedger->_leg_property_data,
_hedger->_spreader.get_book (_hedger->_leg_property_data.leg_index()), new_price)){
if (SpreaderUtils::post_hedge_time_based_price_action (
_order_touch_time,
ts,
_hedger->_leg_property_data,
_hedger->_spreader.get_book (_hedger->_leg_property_data.leg_index()),
new_price)){
_hedger->_modify_order (Price(new_price));
_order_touch_time = _hedger->_current_time();
}
@@ -14,7 +14,8 @@ public:
HedgerWorkingStateWorker (SingleFillHedger* hedger)
: _hedger(hedger)
{}
HedgerWorkingStateWorker (SingleFillHedger* hedger, const HedgerWorkingStateWorker& other)
HedgerWorkingStateWorker (SingleFillHedger* hedger,
const HedgerWorkingStateWorker& other)
: HedgerWorkingStateWorker (hedger)
{
_order_touch_time = other._order_touch_time;
@@ -22,9 +23,9 @@ public:
void on_transition () override;
HedgerState state () const override {return HedgerState::WORKING;}
const std::string state_text () const override {return "WORKING";}
void init () override;
void init (Price price,Quantity quantity,const trace::CorID& trace) override;
void on_order_update (const SpreaderChildOrderState& order_update) override;
void on_book (const bts::orderman::Book& book,trace::CorID &) override;
void on_book (const Book& book,trace::CorID &) override;
void cancel() override;
void terminate() override;
void on_time(const TimeStamp&) override;
@@ -4,6 +4,7 @@
#include "lib/orderman/orders/spreader_orders/spreader/SingleLegQuoter.h"
#include "lib/orderman/orders/spreader_orders/spreader/ISpreaderManager.h"
#include "lib/orderman/orderman_utils/orderman_common.h"
#include "lib/orderman/orders/spreader_orders/spreader/SpreaderCacheWarmingState.h"
namespace bts{
namespace orderman{
@@ -16,7 +17,9 @@ void QuoterWorkingStateWorker::on_transition (){
// waiting for the ack, things could have changed (namely
// fills in other legs, books, order qty change), hence
// we analyze and potentially modify/cxl immediatly
_longpath_analyze_and_act ();
trace::CorID ack_message_trace_id=
_quoter->_child_order_state.last_update_trace();
_longpath_analyze_and_act (ack_message_trace_id);
}
void QuoterWorkingStateWorker::one_time_activate (Price,
Quantity){
@@ -24,10 +27,11 @@ void QuoterWorkingStateWorker::one_time_activate (Price,
}
void QuoterWorkingStateWorker::on_order_update (const SpreaderChildOrderState& u){
bts_assert (u.acknowledged());
trace::CorID ack_message_trace_id=u.last_update_trace();
if (u.terminal_status())
_quoter->_transform_to_END ();
else
_longpath_analyze_and_act ();
_longpath_analyze_and_act (ack_message_trace_id);
}
void QuoterWorkingStateWorker::on_book (size_t,
const bts::orderman::Book&,
@@ -88,6 +92,7 @@ void QuoterWorkingStateWorker::_longpath_analyze_and_act (trace::CorID &trace)
if (_quoter->_this_leg_data.leg_params().quote_params().has_quote_mute_zone())
reprice = SpreaderUtils::reprice_quote_mute_zone(_quoter->_price_quantity_state.out_price().value(), computed_price.value(), _quoter->_this_leg_data.trading_side(),
_quoter->_this_leg_data.tick_size(), _quoter->_this_leg_data.leg_params().quote_params().quote_mute_zone());
reprice=reprice && !(_quoter->_spreader.in_cache_warming_cycle());
if (reprice)
_modify_order (computed_price, order_quantity,trace);
}
@@ -14,23 +14,29 @@ _spreader(spreader){
SpreaderState SpreaderStartStateWorker::state() const {
return SpreaderState::START;
}
void SpreaderStartStateWorker::on_transition (){
void SpreaderStartStateWorker::on_transition (){
}
void SpreaderStartStateWorker::on_init_new(const api::orderman::OrderDefinition& order_def,
const SpreaderPreActivationOrderServices& spreader_pao_services)
{
bts_assert(!_spreader->_inited());
std::vector<LegPropertyData> spreader_legs;
std::vector<bts::api::orderman::OrderDefinition> quoter_ord_defs,
hedger_ord_defs;
if (SpreaderUtils::resolve_synthetic_spread (spreader_pao_services,
order_def,
spreader_legs,
quoter_ord_defs,
hedger_ord_defs,
false)){
_spreader->_bookkeeping_data_ptr =
std::make_unique<SpreaderBookkeepingData>(*_spreader,
_spreader->key(),
order_def,
spreader_pao_services,
spreader_legs);
spreader_legs,
quoter_ord_defs,
hedger_ord_defs);
_spreader->_cancel_hedges_on_cancel=
order_def.custom_def().spreader().execution_strategy().cancel_hedges_on_cancel();
}
@@ -40,7 +46,9 @@ void SpreaderStartStateWorker::on_init_new(const api::orderman::OrderDefinition&
_spreader->key(),
order_def,
spreader_pao_services,
std::vector<LegPropertyData>());
std::vector<LegPropertyData>(),
std::vector<bts::api::orderman::OrderDefinition>(),
std::vector<bts::api::orderman::OrderDefinition>());
// if securities can not be resolved, the order is terminated
LOG(OrderMan, Error, "SpreaderStartStateWorker.on_init_new key: {} , could not resolve securities", _spreader->key().value());
std::string msg = "Could not resolve securities";
@@ -61,7 +69,9 @@ void SpreaderStartStateWorker::on_init_recovered(const bts::api::orderman::Order
_spreader->_bookkeeping_data_ptr = std::make_unique<SpreaderBookkeepingData>(*_spreader,
order_record_copy,
spreader_pao_services,
std::vector<LegPropertyData>());
std::vector<LegPropertyData>(),
std::vector<bts::api::orderman::OrderDefinition>(),
std::vector<bts::api::orderman::OrderDefinition>());
LOG(OrderMan, Warn, "SpreaderStartStateWorker.on_init_recovered key: {} , going to END", _spreader->key().value());
std::string msg = "Recovered spreader orders (from last session) are cancelled";
_spreader->_transform_to_END (&msg);
@@ -74,16 +84,19 @@ void SpreaderStartStateWorker::on_activate(Price price,
// 2. activate order builders
for (size_t l_idx = 0; l_idx < _spreader->_bookkeeping_data().n_legs(); l_idx++)
{
SpreaderLegData& leg_data = _spreader->_bookkeeping_data().get_leg_data(l_idx);
leg_data.quoter_order_builder().activate(order_services);
SpreaderLegData& leg_data = _spreader->_bookkeeping_data().get_leg_data(l_idx);
leg_data.hedger_order_builder().activate(order_services);
if(leg_data.leg_property_data().is_active_quoting())
leg_data.quoter_order_builder().activate(order_services);
}
// 3. create orders
for (size_t l_idx = 0; l_idx < _spreader->_bookkeeping_data_ptr->n_legs(); l_idx++)
{
SpreaderLegData& leg_data = _spreader->_bookkeeping_data().get_leg_data(l_idx);
bts::orderman::ChildOrder child_order = leg_data.quoter_order_builder().build();
leg_data.order_update_message().re_init(child_order);
if(leg_data.leg_property_data().is_active_quoting()){
bts::orderman::ChildOrder child_order = leg_data.quoter_order_builder().build();
leg_data.order_update_message().re_init(child_order);
}
}
if(order_services.is_live())
{
@@ -42,16 +42,20 @@ void SpreaderWaitingForCancelAllStateWorker::on_time(const TimeStamp& ts){
_spreader->_transform_to_END ();
}
}
void SpreaderWaitingForCancelAllStateWorker::on_settlement_price(const SettlementUpdateEvent&){
void SpreaderWaitingForCancelAllStateWorker::on_settlement_price(
const SettlementUpdateEvent&){
}
void SpreaderWaitingForCancelAllStateWorker::on_quoter_order_error(SpreaderLegData& leg_data){
void SpreaderWaitingForCancelAllStateWorker::on_quoter_order_error(
SpreaderLegData& leg_data){
leg_data.quoter().update_order_status (leg_data.order_update_message());
}
void SpreaderWaitingForCancelAllStateWorker::on_quoter_order_fill(SpreaderLegData& leg_data,
trace::CorID ){
void SpreaderWaitingForCancelAllStateWorker::on_quoter_order_fill(
SpreaderLegData& leg_data,
trace::CorID ){
leg_data.quoter().update_order_status (leg_data.order_update_message());
}
void SpreaderWaitingForCancelAllStateWorker::on_quoter_order_status_update(SpreaderLegData& leg_data){
void SpreaderWaitingForCancelAllStateWorker::on_quoter_order_status_update(
SpreaderLegData& leg_data){
leg_data.quoter().update_order_status (leg_data.order_update_message());
}
void SpreaderWaitingForCancelAllStateWorker::on_hedger_order_error(SingleFillHedger& hedger){
@@ -68,4 +72,4 @@ void SpreaderWaitingForCancelAllStateWorker::on_check_termination(){
void SpreaderWaitingForCancelAllStateWorker::on_quoter_irrecoverable_error (){
// everything has already been cancelled
}
}}
}}
@@ -98,6 +98,7 @@ void SpreaderWaitingForInitialDataStateWorker::_init_quoters(){
Quantity quantity = _spreader->_bookkeeping_data().quantity();
for (size_t l_idx = 0; l_idx < _spreader->_bookkeeping_data().n_legs(); l_idx++){
SpreaderLegData& leg_data = _spreader->_bookkeeping_data().get_leg_data(l_idx);
leg_data.hedger_manager().init_once();
if (leg_data.leg_property_data().is_active_quoting())
leg_data.quoter().one_time_activate(effective_price, quantity);
}
@@ -56,8 +56,7 @@ void SpreaderWorkingStateWorker::on_book(const Book& book,trace::CorID trace){
d.quoter().on_book(md_leg.leg_property_data().leg_index(), book,trace);
}
//send md to hedgers hedging this security only
for (size_t h_idx = 0; h_idx < md_leg.n_hedgers(); h_idx++)
md_leg.get_hedger(h_idx).on_book(book,trace);
md_leg.hedger_manager().update_book(book,trace);
}
void SpreaderWorkingStateWorker::on_settlement_price(const SettlementUpdateEvent&){
}
@@ -67,8 +66,7 @@ void SpreaderWorkingStateWorker::on_time(const TimeStamp& ts){
SpreaderLegData& leg_data = bookkeeping_data.get_leg_data(q_idx);
if (leg_data.leg_property_data().is_active_quoting())
leg_data.quoter().on_time(ts);
for (size_t h_idx = 0; h_idx < leg_data.n_hedgers(); h_idx++)
leg_data.get_hedger(h_idx).on_time (ts);
leg_data.hedger_manager().update_time(ts);
}
}
void SpreaderWorkingStateWorker::on_quoter_order_error(SpreaderLegData& leg_data){
@@ -79,9 +77,11 @@ void SpreaderWorkingStateWorker::on_quoter_order_error(SpreaderLegData& leg_data
}
void SpreaderWorkingStateWorker::on_quoter_order_fill(SpreaderLegData& leg_data,
trace::CorID trace){
LOG(OrderMan, Info,"h_p w start {0}",_spreader->_diff_ns());
_spreader->_order_services->log_scope(trace, "SpreaderWorkingStateWorker::on_quoter_order_fill");
const CacheWarmingState& cw=_spreader->_cw_helper;
SpreaderChildOrderState& updated_order = leg_data.order_update_message();
double mult = cw.fill_mult;
LOG(OrderMan, cw.llevel,"h_p w start {}", _spreader->_diff_ns());
//_spreader->_order_services->log_scope(trace, "SpreaderWorkingStateWorker::on_quoter_order_fill");
// this is the most complex part of the spreader. the following needs to be done
// 1. tell other quoters about it (so they reduce their sizes)
// 2. launch a new hedger
@@ -97,56 +97,61 @@ void SpreaderWorkingStateWorker::on_quoter_order_fill(SpreaderLegData& leg_data,
// - or more generally counting the total working hedges and reducing their qty)
// ii. not send a hedge on leg_2 since its already filled
// however doing the above would mess up the leg ratio's so we are preffering
// getting overfilled
SpreaderChildOrderState& order_update_message = leg_data.order_update_message();
bts_assert (order_update_message.last_update_filled_quantity().value() > 0);
for (size_t q_idx = 0; q_idx < _spreader->_bookkeeping_data().n_legs(); q_idx++){
if (q_idx != leg_data.leg_property_data().leg_index()){
SpreaderLegData& other_leg_data = _spreader->_bookkeeping_data().get_leg_data (q_idx);
// getting overfilled
Quantity filled_qty=cw.comp_fill_qty(updated_order);
bts_assert (filled_qty.value() > 0);
for (size_t q_idx = 0; q_idx < _spreader->_bookkeeping_data().n_legs(); q_idx++){
if (q_idx!=leg_data.leg_property_data().leg_index()){
SpreaderLegData& other_leg_data =
_spreader->_bookkeeping_data().get_leg_data (q_idx);
Price hedge_price;
double hedge_qty;
_spreader->_compute_hedge_price_quantity(leg_data.leg_property_data().leg_index(),
order_update_message.last_update_filled_price(),
order_update_message.last_update_filled_quantity().value(),
order_update_message.last_reported_order_price(),
q_idx,
hedge_price,
hedge_qty);
_spreader->_compute_hedge_price_quantity(
leg_data.leg_property_data().leg_index(),
updated_order.last_update_filled_price(),
filled_qty.value(),
updated_order.last_reported_order_price(),
q_idx,
hedge_price,
hedge_qty);
hedge_qty = hedge_qty * mult;
LOG(OrderMan,cw.llevel,"h_p comp {} {}", q_idx, _spreader->_diff_ns());
UnhedgedDelta& leg_unhedged_delta = other_leg_data.unhedged_delta();
leg_unhedged_delta.add_unhedged(hedge_price.value(), hedge_qty);
uint32_t total_unhedged_qty=leg_unhedged_delta.subtract_hedge_if_available();
LOG(OrderMan, Info,"h_p comp {0} {1}", q_idx, _spreader->_diff_ns());
if (total_unhedged_qty != 0){
// if it is an actively quoting leg, inform the quoter about it
// so it can decrease the proportional size
if (other_leg_data.leg_property_data().is_active_quoting()){
other_leg_data.quoter().update_other_leg_fills(leg_data.leg_property_data().leg_index(),
Quantity(total_unhedged_qty));
LOG(OrderMan, Info,"h_p q minus {0} {1}",
q_idx, _spreader->_diff_ns());
}
// a hedge is sent regardless
_spreader->_send_hedge(other_leg_data.leg_property_data().leg_index(),
hedge_price,
Quantity(total_unhedged_qty),
trace);
LOG(OrderMan, Info,"h_p send hedge {0} {1}",
q_idx, _spreader->_diff_ns());
uint32_t total_unhedged_qty=
leg_unhedged_delta.subtract_hedge_if_available(mult);
total_unhedged_qty = total_unhedged_qty * mult;
LOG(OrderMan,cw.llevel,"h_p delta {} {}", q_idx, _spreader->_diff_ns());
// if it is an actively quoting leg, inform the quoter about it
// so it can decrease the proportional size
if (!cw.in_cache_warming_cycle &&
other_leg_data.leg_property_data().is_active_quoting()){
other_leg_data.quoter().update_other_leg_fills(
leg_data.leg_property_data().leg_index(),
Quantity(total_unhedged_qty));
LOG(OrderMan,cw.llevel,"h_p q minus {} {}", q_idx, _spreader->_diff_ns());
}
// a hedge is sent regardless
_spreader->_send_hedge(other_leg_data.leg_property_data().leg_index(),
hedge_price,
Quantity(total_unhedged_qty),
trace);
LOG(OrderMan,cw.llevel,"h_p send hedge {} {}", q_idx, _spreader->_diff_ns());
}
}
// this is done at the end (i dont like ordering though) because
// if the order is fully filled but there is more to quote, the quoter
// will generate a new order, which will modify order_update_message
// will generate a new order, which will modify updated_order
// (we could have cached the last fill price and qty and called it
// earlier too)
// todo:this needs to be addressed, its confusing
leg_data.quoter().update_order_status (order_update_message);
LOG(OrderMan, Info,"h_p update q {0}", _spreader->_diff_ns());
leg_data.quoter().update_order_status (updated_order);
LOG(OrderMan,cw.llevel,"h_p update q {}", _spreader->_diff_ns());
}
void SpreaderWorkingStateWorker::on_quoter_order_status_update(SpreaderLegData& leg_data){
bts_assert (leg_data.order_update_message().last_update_filled_quantity().value() == 0);
void SpreaderWorkingStateWorker::on_quoter_order_status_update(
SpreaderLegData& leg_data){
bts_assert (
leg_data.order_update_message().last_update_filled_quantity().value() == 0);
leg_data.quoter().update_order_status (leg_data.order_update_message());
}
void SpreaderWorkingStateWorker::on_hedger_order_error(SingleFillHedger& hedger){
File diff suppressed because it is too large. Load diff
@@ -15,6 +15,7 @@
#include "lib/orderman/orders/spreader_orders/spreader/external_interfacing/SpreaderOrderBuilder.h"
#include <google/protobuf/util/message_differencer.h>
#include "lib/os/os.h"
#include <algorithm>
namespace bts {
namespace orderman {
@@ -29,15 +30,19 @@ class SpreaderManagerForTest : public ISpreaderManager
{
private:
bts::orderman::OrderKey _spreader_order_key;
SpreaderBookkeepingData _bookkeeping_data;
SpreaderBookkeepingData _bookkeeping_data;
CacheWarmingState _cw_state;
public:
SpreaderManagerForTest (bts::orderman::OrderKey spreader_order_key,
const api::orderman::OrderDefinition& order_def,
const SpreaderPreActivationOrderServices& spreader_pao_services,
const std::vector<LegPropertyData>& legs)
const std::vector<LegPropertyData>& legs,
const std::vector<bts::api::orderman::OrderDefinition>& quoter_ord_defs,
const std::vector<bts::api::orderman::OrderDefinition>& hedger_ord_defs)
:
_spreader_order_key (spreader_order_key),
_bookkeeping_data (*this, spreader_order_key, order_def, spreader_pao_services, legs)
_bookkeeping_data (*this, spreader_order_key, order_def, spreader_pao_services, legs,
quoter_ord_defs,hedger_ord_defs)
{}
SpreaderBookkeepingData& bookkeeping_data() {return _bookkeeping_data;}
@@ -81,6 +86,13 @@ public:
bts::orderman::ChildOrder child_order;
_bookkeeping_data.get_leg_data(leg_idx).order_update_message().re_init(child_order);
}
ChildOrder get_new_hedge_order (size_t) override{
ChildOrder rv;
return rv;
}
bool in_cache_warming_cycle() const override{
return false;
}
};
/**********************************************************************/
/***************************orders ************************************/
@@ -233,7 +245,7 @@ LegPropertyData create_leg_property_data (const ContractLeg& leg,
PostHedgeParams post_hp;
fill_leg_spreader_params (lsp, price_multiplier, is_active_quoting, qp, pre_hp, post_hp);
bts::api::orderman::OrderDefinition order_def;
return LegPropertyData (leg_index, leg, lsp, leg.side(),order_def,order_def);
return LegPropertyData (leg_index, leg, lsp, leg.side());
}
SpreaderPreActivationOrderServices::TestOnlySpreaderPreActivationOrderServices _create_spreader_pao_services(const Security& synthetic_security)
{
@@ -363,8 +375,12 @@ SpreaderManagerForTest* create_spreader_manager_2_legs (bts::orderman::OrderKey
SpreaderPreActivationOrderServices::TestOnlySpreaderPreActivationOrderServices testonly_spreader_pao_services = _create_spreader_pao_services(synthetic_security);
SpreaderPreActivationOrderServices pao_services (&testonly_spreader_pao_services);
std::vector<LegPropertyData> leg_props;
SpreaderUtils::resolve_synthetic_spread(pao_services, order_def, leg_props);
return new SpreaderManagerForTest (order_key, order_def, pao_services, leg_props);
std::vector<bts::api::orderman::OrderDefinition> quoter_ord_defs,
hedger_ord_defs;
SpreaderUtils::resolve_synthetic_spread(pao_services, order_def, leg_props,
quoter_ord_defs,hedger_ord_defs);
return new SpreaderManagerForTest (order_key, order_def, pao_services, leg_props,
quoter_ord_defs,hedger_ord_defs);
}
SpreaderManagerForTest* create_spreader_manager_n_legs (bts::orderman::OrderKey order_key,
@@ -380,8 +396,12 @@ SpreaderManagerForTest* create_spreader_manager_n_legs (bts::orderman::OrderKey
SpreaderPreActivationOrderServices::TestOnlySpreaderPreActivationOrderServices testonly_spreader_pao_services = _create_spreader_pao_services(synthetic_security);
SpreaderPreActivationOrderServices pao_services (&testonly_spreader_pao_services);
std::vector<LegPropertyData> leg_props;
SpreaderUtils::resolve_synthetic_spread(pao_services, order_def, leg_props);
return new SpreaderManagerForTest (order_key, order_def, pao_services, leg_props);
std::vector<bts::api::orderman::OrderDefinition> quoter_ord_defs,
hedger_ord_defs;
SpreaderUtils::resolve_synthetic_spread(pao_services, order_def, leg_props,
quoter_ord_defs,hedger_ord_defs);
return new SpreaderManagerForTest (order_key, order_def, pao_services, leg_props,
quoter_ord_defs,hedger_ord_defs);
}
void fire_book_update (double* prices, uint16_t* sizes, api::Side side, size_t leg_index,
@@ -444,17 +464,41 @@ void fire_book_update_hedger (double* prices, uint16_t* sizes, api::Side side, S
trace::CorID trace;
hedger.on_book (book,trace);
}
void verify_complete_hedger_state (const SingleFillHedger& hedger, HedgerState state, Price target_price, Quantity target_quantity, Quantity aggregated_filled_quantity,
Price out_price, Quantity, Quantity remaining_to_fill)
void verify_complete_hedger_state (const SingleFillHedger& hedger,
HedgerState state,
Price target_price,
Quantity target_quantity,
Quantity aggregated_filled_quantity,
Price out_price,
Quantity,
Quantity remaining_to_fill)
{
REQUIRE (hedger.test_only_func_hedger_state() == state);
const HedgePriceQuantityState& hedger_pq_state = hedger.test_only_func_price_quantity_state ();
REQUIRE (hedger_pq_state.target_price() == target_price);
REQUIRE (hedger_pq_state.target_price() == target_price);
REQUIRE (hedger_pq_state.target_quantity() == target_quantity);
REQUIRE (hedger_pq_state.aggregated_filled_quantity() == aggregated_filled_quantity);
REQUIRE (hedger_pq_state.out_price() == out_price);
if (hedger.is_active_order())
REQUIRE (hedger.order_state().out_price() == out_price);
REQUIRE (hedger_pq_state.remaining_to_fill() == remaining_to_fill);
}
void fill_all_working_hedgers(SpreaderOrder& s){
SpreaderBookkeepingData& d=s.test_only_func_spreader_full_data();
for (size_t i=0;i<d.n_legs();i++){
SpreaderLegData& l = d.get_leg_data(i);
HedgerManager& hm = l.hedger_manager();
for(size_t hi=0;hi<hm.test_only_pool_size();hi++){
SingleFillHedger& h=hm.test_only_get_hedger_at(hi);
if(h.is_active_order()){
SpreaderChildOrderState& c=h.order_state();
s.test_only_on_child_order_update(c.order_key(),
api::orderman::OrderStatus::ORDER_STATUS_COMPLETED,
c.out_quantity(),
c.out_price());
}
}
}
}
/**********************************************************************/
/******************************spreader********************************/
/**********************************************************************/
@@ -530,8 +574,10 @@ void set_spreader_orders (SpreaderOrder& spreader, double* leg_1_bid_prices, uin
leg_2_bid_prices, leg_2_bid_sizes,
leg_2_ask_prices, leg_2_ask_sizes, true);
spreader.test_only_on_book_update (bts::orderman::BookUpdateEvent(book_leg_2,trace::CorID()));
spreader.test_only_on_child_order_update (leg_data_1.order_key(), api::orderman::OrderStatus::ORDER_STATUS_WORKING, Quantity (leg_1_fills), Price (leg_1_filled_price));
spreader.test_only_on_child_order_update (leg_data_2.order_key(), api::orderman::OrderStatus::ORDER_STATUS_WORKING, Quantity (leg_2_fills), Price (leg_2_filled_price));
if(leg_data_1.leg_property_data().is_active_quoting())
spreader.test_only_on_child_order_update (leg_data_1.order_key(), api::orderman::OrderStatus::ORDER_STATUS_WORKING, Quantity (leg_1_fills), Price (leg_1_filled_price));
if(leg_data_2.leg_property_data().is_active_quoting())
spreader.test_only_on_child_order_update (leg_data_2.order_key(), api::orderman::OrderStatus::ORDER_STATUS_WORKING, Quantity (leg_2_fills), Price (leg_2_filled_price));
}
/**********************************************************************/
/******************************misc************************************/
@@ -602,7 +648,7 @@ void generate_random_book(size_t n_levels,
ask_prices,direct_ask_sizes,implied_ask_sizes,
bts::api::Side::SIDE_SELL,false);
}
void stats (std::vector<double>& p,
void stats (std::vector<double> p,
double& min,
double& max,
double& mean,
@@ -626,6 +672,14 @@ void spin_ms (int64_t ms)
;
}
}
void spin_us (int64_t us)
{
if (us > 0) {
auto now = Timer::now();
while (ns_diff(Timer::now(),now) < us * 1000)
;
}
}
void test_sleep (int64_t ms, bool is_spin){
if (is_spin)
spin_ms(ms);
+29 -8
View File
@@ -19,7 +19,7 @@ fi
SQLLITE=../../../../externals/linux/bin/sqlite3
SQLITE_EXT=../../../../externals/linux/bin/libsqlitefunctions
DB=file:$HOME/sparkdata/data/databases/perf_log/perf_log.sqlite?mode=ro
DB=file:$HOME/sparkdata/data/databases/perf_log/perf_log.sqlite
if [ $# -eq 1 ]
then
@@ -28,6 +28,19 @@ else
SESSION=\'$2\'
fi
echo Generating stats...
$SQLLITE $DB<<EOF
begin transaction;
insert into hedge_trace_archive select * from hedge_trace_view;
insert into requote_trace_archive select * from requote_trace_view;
insert into event_archive select * from event_view;
insert into log_archive select * from log_view;
insert into event_log_archive select * from event_log_aux;
delete from trace;
commit;
EOF
echo Done
if [ $1 == "log" ]; then
$SQLLITE $DB<<EOF
.mode column
@@ -52,15 +65,23 @@ $SQLLITE $DB<<EOF
.mode column
.headers on
.load $SQLITE_EXT
select
session_start,event_type,count(*) count, avg(duration) avg,
max(duration) max, min(duration) min, median(duration) median,
stdev(duration) stdev
from events where session_start=$SESSION group by session_start,event_type;
select
session_start,
event_type,
count(*) count,
avg(duration) avg,
max(duration) max,
min(duration) min,
median(duration) median,
stdev(duration) stdev
from
event_archive
where
session_start=$SESSION
group by
event_type
EOF
exit 0
fi
help()
+1 -3
View File
@@ -544,7 +544,6 @@ uint64_t tick_count() {
#endif
}
#ifdef _WIN32
std::vector<std::string> g_cmd_args;
bool g_cmd_args_set;
@@ -559,14 +558,13 @@ extern "C" BTS_EXPORT void provide_command_line(const char **argv, int argc) {
g_cmd_args_set = true;
}
}
#endif
std::vector<std::string> get_command_line() {
#ifdef _WIN32
if (g_cmd_args_set) {
// return cached copy
return g_cmd_args;
}
#ifdef _WIN32
for (int index = 0; index < __argc; index++) {
if (__argv != nullptr) {
g_cmd_args.push_back(__argv[index]);
+55 -33
View File
@@ -3,9 +3,10 @@
#include "lib/config/protos/perflog_cfg.pb.h"
#include "lib/config/protos/system.pb.h"
#include "lib/persistence/persistence_utils.h"
#include "lib/utils/bts_array_size.h"
#include "lib/utils/datetime.h"
#include "lib/utils/stats.h"
#include "lib/utils/bts_array_size.h"
#include "lib/utils/thread_manager.h"
#include <SQLiteCpp/SQLiteCpp.h>
#include <boost/algorithm/string/replace.hpp>
@@ -81,6 +82,8 @@ struct PerfLog::PImpl {
std::string session_start;
boost::circular_buffer<PerfLogEntry> entry_batch;
std::mutex lock;
std::thread init_thread;
std::atomic<bool> init_completed;
};
PerfLog::PerfLog() : _pending(), _disabled(false), me() {
@@ -98,38 +101,56 @@ PerfLog::PerfLog() : _pending(), _disabled(false), me() {
try {
me = std::make_unique<PImpl>(loc);
init_db();
me->insert_trace_statement =
std::make_unique<SQLite::Statement>(me->db, insert_trace);
me->insert_session_statement =
std::make_unique<SQLite::Statement>(me->db, insert_session);
me->requote_query_statement =
std::make_unique<SQLite::Statement>(me->db, requote_query);
me->hedge_query_statement =
std::make_unique<SQLite::Statement>(me->db, hedge_query);
me->session_start = datetime::FIX_timestamp(datetime::local_now());
me->insert_session_statement->bind(
":version", config::get<config::System>().version());
me->insert_session_statement->bind(
":host", config::get<config::System>().host_name());
#ifdef DEBUG
me->insert_session_statement->bind(":build_type", "debug");
#else
me->insert_session_statement->bind(":build_type", "release");
#endif
me->insert_session_statement->bind(":session_start", me->session_start);
me->insert_session_statement->exec();
me->insert_session_statement->reset();
} catch (SQLite::Exception const &e) {
LOG(persist, Error, "Error initializing perf_trace DB {}. Error: {}", loc,
std::string(e.getErrorStr()));
_disabled = true;
return;
}
me->init_completed = false;
me->init_thread = threading::make_thread("perf_log_ini", [loc,this]() {
try {
LOG(persist,Info, "Started initialization of perf_log db");
init_db();
me->insert_trace_statement =
std::make_unique<SQLite::Statement>(me->db, insert_trace);
me->insert_session_statement =
std::make_unique<SQLite::Statement>(me->db, insert_session);
me->requote_query_statement =
std::make_unique<SQLite::Statement>(me->db, requote_query);
me->hedge_query_statement =
std::make_unique<SQLite::Statement>(me->db, hedge_query);
me->session_start = datetime::FIX_timestamp(datetime::local_now());
me->insert_session_statement->bind(
":version", config::get<config::System>().version());
me->insert_session_statement->bind(
":host", config::get<config::System>().host_name());
#ifdef DEBUG
me->insert_session_statement->bind(":build_type", "debug");
#else
me->insert_session_statement->bind(":build_type", "release");
#endif
me->insert_session_statement->bind(":session_start",
me->session_start);
me->insert_session_statement->exec();
me->insert_session_statement->reset();
} catch (SQLite::Exception const &e) {
LOG(persist, Error,
"Error initializing perf_trace DB (2) {}. Error: {}", loc,
std::string(e.getErrorStr()));
_disabled = true;
}
LOG(persist,Info, "Finished initialization of perf_log db");
me->init_completed = true;
});
}
PerfLog::~PerfLog() {}
PerfLog::~PerfLog() {
me->init_thread.join();
}
std::string PerfLog::init_storage_location() {
auto loc = persistence::storage_location("perf_log");
@@ -145,7 +166,7 @@ std::string PerfLog::init_storage_location() {
}
size_t PerfLog::store_log_entries() {
if (_disabled) {
if (!me->init_completed || _disabled) {
_pending.clear();
return 0;
}
@@ -167,9 +188,6 @@ void PerfLog::init_db() {
std::string str((std::istreambuf_iterator<char>(f)),
(std::istreambuf_iterator<char>()));
config::PerfLogCfg defaults;
boost::replace_all(
str, std::to_string(defaults.recent_traces_size()),
std::to_string(config::get<config::PerfLogCfg>().recent_traces_size()));
boost::replace_all(
str, std::to_string(defaults.max_rows()),
std::to_string(config::get<config::PerfLogCfg>().max_rows()));
@@ -187,7 +205,9 @@ std::string encode_number(SQLite::Column const &c) {
std::vector<std::map<std::string, std::string>>
PerfLog::get_latest_requote_traces(size_t n) {
std::vector<std::map<std::string, std::string>> result;
if (!me->init_completed)
return result;
std::lock_guard<std::mutex> l(me->lock);
try {
me->requote_query_statement->bind(1, (long long)n);
@@ -221,7 +241,9 @@ PerfLog::get_latest_requote_traces(size_t n) {
std::vector<std::map<std::string, std::string>>
PerfLog::get_latest_hedge_traces(size_t n) {
std::vector<std::map<std::string, std::string>> result;
if (!me->init_completed)
return result;
std::lock_guard<std::mutex> l(me->lock);
try {
me->hedge_query_statement->bind(1, (long long)n);
@@ -276,7 +298,7 @@ size_t PerfLog::flush_current_batch_to_disk() {
me->insert_trace_statement->bind(":cor_event",
(long long)ev.cor_event.to_uint64());
char tn[17];
pthread_getname_np(ev.thread_id, tn, BTS_ARRAYSIZE(tn)-1);
pthread_getname_np(ev.thread_id, tn, BTS_ARRAYSIZE(tn) - 1);
me->insert_trace_statement->bind(":thread", tn);
me->insert_trace_statement->exec();
count++;
+5
View File
@@ -44,6 +44,11 @@ long unix_now() {
return (boost::posix_time::microsec_clock::universal_time() - PTime(anchor)).total_microseconds() / (long)1E6;
}
std::chrono::system_clock::time_point utc_ll_to_timepoint(unsigned long long ll) {
auto us_since_epoch = ((ll & ticks_mask) - anchor_ticks) / ticks_per_micro;
return std::chrono::system_clock::time_point (std::chrono::microseconds(us_since_epoch));
}
long long ll_diff_in_us (unsigned long long ll1, unsigned long long ll2)
{
#ifndef NO_CPP0X
+3
View File
@@ -2,6 +2,8 @@
#include "bts_export.h"
#include <string>
#include <chrono>
#include "windows_wrapper.h" // tick_count.h includes windows.h
namespace boost {namespace posix_time {class ptime;}}
@@ -40,6 +42,7 @@ extern "C" BTS_EXPORT double OA_date_from_ll (LLDateTime ll);
extern "C" BTS_EXPORT LLDateTime ll_from_OA_date (double oa_date);
extern "C" BTS_EXPORT long unix_now();
BTS_EXPORT std::chrono::system_clock::time_point utc_ll_to_timepoint(unsigned long long ll) ;
BTS_EXPORT PTime now ();
BTS_EXPORT PTime local_now ();
BTS_EXPORT PTime make_ptime (int year, int month, int day);
+2 -1
View File
@@ -32,7 +32,8 @@ FOR %%i IN ("%src%") DO (
set filename=%%~ni
set fileextension=%%~xi
if "%%~xi" == ".nupkg" (
unzip -o %%i -d %STAGE%\\%SDK%\\
rem unzip -o %%i -d %STAGE%\\%SDK%\\
7z x -o%STAGE%\\%SDK%\\ -y -spe %%i
rmdir /Q /S %STAGE%\\%SDK%\\_rels
rmdir /Q /S %STAGE%\\%SDK%\\package
del /Q %STAGE%\\%SDK%\\spark_sdk.nuspec
+11 -3
View File
@@ -16,6 +16,7 @@
#include "lib/utils/mpmc_bounded_queue.h"
#include "server/sparkd/cmds/CmdRegistry.h"
#include "lib/tracing/PerfLog.h"
#include <benchmark/benchmark.h>
#include <boost/container/flat_map.hpp>
#include <boost/container/small_vector.hpp>
@@ -32,15 +33,22 @@
#include <moodycamel/readerwriterqueue.h>
#include <mutex>
#include <sys/time.h>
#include "lib/tracing/PerfLog.h"
#include <date/date.h>
#include <tbb/concurrent_queue.h>
namespace bts {
namespace cmd {
using namespace orderman;
static void test_date_conversion(benchmark::State &state) {
auto lldt = datetime::ll_now();
for (auto _ : state) {
benchmark::DoNotOptimize(datetime::utc_ll_to_timepoint(lldt));
}
}
BENCHMARK(test_date_conversion);
static void gen_id(benchmark::State &state) {
std::string scope = "benchmark";
@@ -626,7 +634,7 @@ BENCHMARK(fix_time_stamp);
static void perf_log(benchmark::State &state) {
trace::PerfLog l;
for (auto _ : state) {
for (auto _ : state) {
l.log(trace::CorID{"sim", 0}, "this", 1, "txt", 3);
}
}
@@ -19,6 +19,7 @@ MDP3Listener::MDP3Listener(
const std::vector<MDProducerChannel> channels) :
_index(index),
_producer(producer),
_verbose_ticker(config::get<CMEOptions>().verbose_ticker()),
_packet_arrived(),
_latest_sequence(0),
// _settlement_recorder(settles),
@@ -182,6 +182,7 @@ private:
CMESecurityIndex& _index;
MDProducer& _producer;
bool _verbose_ticker;
utils::TimeStamp _packet_arrived;
uint32_t _latest_sequence;
// co2::SettlementRecorder& _settlement_recorder;
@@ -416,7 +417,17 @@ void MDP3Listener::onTradeSummary(const onixs::Security& sec, const TradeSummary
onixs::Decimal price = arg.entry().entryPx();
onixs::AggressorSide::Enum agside = onixs::AggressorSide::NoAggressor;
arg.entry().aggressorSide(agside);
on_trade(data, price, size, agside, arg.message().transactTime());
if (_verbose_ticker) {
const auto& details = arg.orderIds();
auto sz = details.size();
for (size_t x = 0; x < sz; ++x) {
const auto& d = details[x];
on_trade(data, price, d.lastQty(), agside, arg.message().transactTime());
}
}
else {
on_trade(data, price, size, agside, arg.message().transactTime());
}
}
}
@@ -442,7 +453,6 @@ void MDP3Listener::onSettlementPrice(const onixs::Security& sec, const Statistic
onixs::Timestamp time;
args.entry().entryPx(price);
args.entry().tradingReferenceDate(time);
// args.entry().
auto display_factor = data->handle.security().display_factor;
data->handle.update_settlement(
utils::TimeStamp::now(),
@@ -454,19 +464,16 @@ void MDP3Listener::onSettlementPrice(const onixs::Security& sec, const Statistic
template <typename SessionArgs>
void MDP3Listener::onSessionOpeningPrice(const onixs::Security& sec, const SessionArgs& args) {
tracepoint (bts_trace,trace_order,(char*) "onOpeningPrice.session", _latest_sequence, (uint16_t) _latest_sequence, 0);
// auto type = args.entry().settlPriceType();
// if (type.final() && type.actual()) {
auto* data = _index.get_security(sec.id());
if (data != nullptr) {
onixs::Decimal price = args.entry().entryPx();
onixs::Timestamp time = args.message().transactTime();
auto display_factor = data->handle.security().display_factor;
data->handle.update_opening(
utils::TimeStamp::now(),
double_of_price(price, display_factor),
date_of_timestamp(time));
}
//
auto* data = _index.get_security(sec.id());
if (data != nullptr) {
onixs::Decimal price = args.entry().entryPx();
onixs::Timestamp time = args.message().transactTime();
auto display_factor = data->handle.security().display_factor;
data->handle.update_opening(
utils::TimeStamp::now(),
double_of_price(price, display_factor),
date_of_timestamp(time));
}
}
template <typename RecoveryArgs>
void MDP3Listener::onRecoveryOpeningPrice(const onixs::Security& sec, const RecoveryArgs& args) {
@@ -269,7 +269,10 @@ unique_ptr<onixs::Handler> MDP3Main::prepare_handler(const string& channel_id) {
// Misc settings
settings.bookManagement().updateNotification(onixs::BookUpdateNotification::OnEndOfEvent);
settings.tradeProcessing(onixs::TradeProcessing::SummaryLevel);
settings.tradeProcessing(
config::get<CMEOptions>().verbose_ticker() ?
onixs::TradeProcessing::OrderDetails :
onixs::TradeProcessing::SummaryLevel);
//
// Logging
+5 -2
View File
@@ -252,14 +252,17 @@ void dump_db(const vector<string>& args) {
void run_tail(const vector<string>& args) {
std::set<UserSymbol> products;
auto conf = parse_md_args(args);
bool monitor_all = false;
for (const auto& b : args) { if (b=="all") monitor_all = true; }
PlatformConfig config;
config.set_type(api::SecurityType_Name(api::SECURITYTYPE_FUTURE));
if (!monitor_all) config.set_type(api::SecurityType_Name(api::SECURITYTYPE_FUTURE));
config.set_platform(co2::Platform_Name(conf.platform));
get_system_md_symbols(config, products);
auto mod = conf.number > 0 ? conf.number : 1000;
PrintFormat fmt = PrintFormat::CSV;
for (const auto& a : args) { if (a == "longform") fmt = PrintFormat::Human; }
md::run_periodic_dumper(products, mod, fmt);
md::run_periodic_dumper(products, mod, fmt, monitor_all);
}
void run_channels() {
+1 -1
View File
@@ -1 +1 @@
set(sources ../../externals/linux/include/google/protobuf/descriptor.proto;../../externals/linux/include/google/protobuf/timestamp.proto;../lib/api/apicore.proto;../lib/api/apigateway.proto;../lib/api/apioptions.proto;../lib/api/services/AdminService.proto;../lib/api/services/ApiUnitTestService.proto;../lib/api/services/AuthService.proto;../lib/api/services/ErrorReportService.proto;../lib/api/services/HelloService.proto;../lib/api/services/MarketDataAuxiliaryService.proto;../lib/api/services/MarketDataRelay.proto;../lib/api/services/OrderService.proto;../lib/api/services/OutOfProcessURLListener.proto;../lib/api/services/PersistenceService.proto;../lib/api/services/PositionService.proto;../lib/api/services/ShellDiagnosticService.proto;../lib/api/services/ShellLatencyService.proto;../lib/api/services/SyntheticSecurityService.proto;../lib/domain_model/Security.proto;../lib/md/protos/co2.proto;../lib/sparkproto/SparkProtos.proto;api/build.sh;api/node_modules/cssstyle/scripts/run_tests.sh;api/node_modules/exit/test/fixtures/create-files.sh;api/node_modules/protobufjs/cli/node_modules/acorn-jsx/node_modules/acorn/bin/update_authors.sh;api/node_modules/watch/release.sh;api/quiet_build.sh;api/src/DynamicService.ts;api/src/ServiceContext.ts;api/src/bts_assert.ts;ui/src/Access.fs;ui/src/Accounts/ClipLimits.fs;ui/src/Accounts/ProductLimits.fs;ui/src/Accounts/SecurityLimits.fs;ui/src/Accounts/State.fs;ui/src/Accounts/Types.fs;ui/src/Accounts/View.fs;ui/src/App.fs;ui/src/Cmd.fs;ui/src/Controls/Button.fs;ui/src/Controls/DataGrid.fs;ui/src/Controls/Fields.fs;ui/src/Controls/Modal.fs;ui/src/Controls/ReactGrid.fs;ui/src/Controls/Table.fs;ui/src/Controls/nbsp.fs;ui/src/Global.fs;ui/src/Imports.fs;ui/src/Lens.fs;ui/src/Login/Login.fs;ui/src/Nav.fs;ui/src/State.fs;ui/src/Types.fs;ui/src/Users/State.fs;ui/src/Users/Types.fs;ui/src/Users/View.fs;ui/src/Util/EnumUtils.fs;ui/src/Util/JSMap.fs;ui/src/obj/Debug/netstandard2.0/ui.AssemblyInfo.fs)
set(sources ../../externals/linux/include/google/protobuf/descriptor.proto;../../externals/linux/include/google/protobuf/timestamp.proto;../lib/api/apicore.proto;../lib/api/apigateway.proto;../lib/api/apioptions.proto;../lib/api/services/AdminService.proto;../lib/api/services/ApiUnitTestService.proto;../lib/api/services/AuthService.proto;../lib/api/services/ErrorReportService.proto;../lib/api/services/HelloService.proto;../lib/api/services/MarketDataAuxiliaryService.proto;../lib/api/services/MarketDataRelay.proto;../lib/api/services/OrderService.proto;../lib/api/services/OutOfProcessURLListener.proto;../lib/api/services/PersistenceService.proto;../lib/api/services/PositionService.proto;../lib/api/services/ShellDiagnosticService.proto;../lib/api/services/ShellLatencyService.proto;../lib/api/services/SyntheticSecurityService.proto;../lib/domain_model/Security.proto;../lib/md/protos/co2.proto;../lib/sparkproto/SparkProtos.proto;api/build.sh;api/node_modules/exit/test/fixtures/create-files.sh;api/node_modules/protobufjs/cli/node_modules/acorn-jsx/node_modules/acorn/bin/update_authors.sh;api/node_modules/watch/release.sh;api/quiet_build.sh;api/src/DynamicService.ts;api/src/ServiceContext.ts;api/src/bts_assert.ts;ui/src/Access.fs;ui/src/Accounts/ClipLimits.fs;ui/src/Accounts/ProductLimits.fs;ui/src/Accounts/SecurityLimits.fs;ui/src/Accounts/State.fs;ui/src/Accounts/Types.fs;ui/src/Accounts/View.fs;ui/src/App.fs;ui/src/Cmd.fs;ui/src/Controls/Button.fs;ui/src/Controls/DataGrid.fs;ui/src/Controls/Fields.fs;ui/src/Controls/Modal.fs;ui/src/Controls/ReactGrid.fs;ui/src/Controls/Table.fs;ui/src/Controls/nbsp.fs;ui/src/Global.fs;ui/src/Imports.fs;ui/src/Lens.fs;ui/src/Login/Login.fs;ui/src/Nav.fs;ui/src/State.fs;ui/src/Types.fs;ui/src/Users/State.fs;ui/src/Users/Types.fs;ui/src/Users/View.fs;ui/src/Util/EnumUtils.fs;ui/src/Util/JSMap.fs;ui/src/obj/Debug/netstandard2.0/ui.AssemblyInfo.fs)
@@ -23,7 +23,7 @@ we will add it to the "unhedged delta" till the unhedged delta > 1, and then we
or more orders to get the unhedged delta < 1.
Force quoting fills based on the left most column (by lifting the quoting offer)
and verify that hedge orders of size "hedge_qty" (3rd column) are sent as a result.
and verify that hedge orders of size "hedge_order_sent" (3rd column) are sent as a result.
The "unhedged_delta" column shows the unhedged delta immediatly after the quoting fill
and the "remaining_unhedged_delta" column is the unhedged delta after the hedge order
has been sent.
@@ -0,0 +1,34 @@
This test verifies the pecularities of the spreader with > 1 quoting leg.
0. This needs sim with replay turned off (see step 0 in common_setup_1), so we can create our own markets.
1. Construct a 2 legged spreader, buy GCM2 sell GCZ2. Set the Price Multipliers to 1 and -1 respectively as
well. Disable all Quote, Pre-Hedge and Post-Hedge parameters.
2. Enter a 20 lot sell order for GCM2 at 20.50 and a 20 lot buy order for GCZ2 at 19.50. A synthetic sell side
market of 20 at 1.00 should show up.
3. Now enter a 5 lot synthetic buy order at 0.70. Since both legs are quoting GCM2 uses GCZ2 (19.50 bid)
to quote its bid at 19.50+0.7=20.2, and GCZ2 uses GCM2 (20.05) to quote its offer at 20.5-0.70=19.8. Verify
these quoting prices
(remember, for quoting prices:
leg_1=leg_2_bid_market+spreader_price
leg_2=leg_2_bid_market-spreader_price
)
4. Move the markets a few times (without crossing and causing any fills) and make sure the quoting prices
are correct, and bring them back to where they were in 2.
5. Now force a 1 lot fill on the GCZ2 quoting order, by sending a 1 lot buy against it. This should send
out a buy hedge on GCM2 at 20.50 and the quote order for GCM2 should be reduced by 1.
6. Now lets fill the other quote order with a 2 lot, ie send a sell order of 2 against the GCM2 bid.
Verify that this send a GCZ2 sell hedge at 19.50 and the GCZ2 quote order is now a 1/3 order.
7. Now we have the spreader 3/5 at 0.70. Verify that.
8. Now lets remove the GCM2 market (ie the 1/20 at 20.50). This will cause the GCZ2 quoting order to be
pulled. Put the GCM2 market back and the 2 lot GCZ2 quoting order should come back.
9. Drag the spreader order to 1.0. This should cause the whole spreader order to fill. There will be
2 overfills and an Avg filled price of 0.90.
+5
View File
@@ -13,6 +13,11 @@ IF %ERRORLEVEL% NEQ 0 GOTO :ERROR_AND_END
msbuild /m /p:debugtype=portable /p:platform=x64 /clp:verbosity=minimal /clp:summary spark_client.sln /p:Configuration=release
IF %ERRORLEVEL% NEQ 0 GOTO :ERROR_AND_END
:: build netcore
cd src\client\sparkcmd
dotnet publish -f netcoreapp2.1 -c release
IF %ERRORLEVEL% NEQ 0 GOTO :ERROR_AND_END
GOTO :END
:ERROR_AND_END