Compare commits

..
Author SHA1 Message Date
Taha Afzal b3dc0e9533 maker taker hedging logic added, not hooked 2019-05-14 19:17:30 -04:00
45 changed files with 427 additions and 2256 deletions

No files matched your search

-37
View File
@@ -281,42 +281,5 @@
"RedirectOutput"
]
},
{
"name": ".NET Core Remote Attach",
"type": "coreclr",
"request": "attach",
"processId": "${command:pickRemoteProcess}",
"pipeTransport": {
"pipeProgram": "c:\\program files\\git\\usr\\bin\\ssh.exe",
"pipeArgs": ["bts@sparkvm"],
"debuggerPath": "/home/bts/vsdbg/vsdbg",
"pipeCwd": "${workspaceRoot}",
"quoteArgs": true
},
"sourceFileMap": {
"/home/bts/spark/": "${workspaceRoot}"
},
"justMyCode": true
},
{
"name": ".NET Core Remote Launch",
"type": "coreclr",
"request": "launch",
"program": "/usr/bin/dotnet",
"args": ["sparkcmd.dll", "test", "test_join_create_and_takeout_market"],
"stopAtEntry": false,
"cwd": "/home/bts/spark/build/ninja_debug/server",
"pipeTransport": {
"pipeProgram": "c:\\program files\\git\\usr\\bin\\ssh.exe",
"pipeArgs": ["bts@sparkvm"],
"debuggerPath": "/home/bts/vsdbg/vsdbg",
"pipeCwd": "${workspaceRoot}",
"quoteArgs": true
},
"sourceFileMap": {
"/home/bts/spark/": "${workspaceRoot}"
},
"justMyCode": false
},
]
}
+2 -9
View File
@@ -13,19 +13,12 @@ insert_anchor_links = "right"
- Simpler representation of complex algo chains in order book and mini order book
- Visual and usability improvements to mini order book
- Streamlining of algo parameter dialog in ladder
- New spreader queue holder behavior
- New spreader queue holder behaviour
- Visual improvements to the order book
- New server side API
- Improved admin UI performance
- Optimized PIQ rendering
### Internal changes
- Exchange simulator no longer requires daily sequence number reset
- Improved MD resilience to corrupted shared memory
- Native orders can now buffer updates/cancels when disconnected or pending first acknowledgment
- Improved settings manager tool output
- Added diagnostics in the event MD memory corruption is detected
- Added systems tests for join,lean, iceberg and spreader orders
- Made exchange order recovery more resilient in the presence of database loss
- Native orders can now buffer updates/cancels when disconected or pending first acknowledgment
## 1.6.2
### User Visible Changes
@@ -44,9 +44,6 @@ namespace mbts.spark {
double _real_price = double.NaN, _phantom_price = double.NaN;
uint filled, quantity, num_root_orders;
int working_quantity;
uint? _min_piq = UInt32.MaxValue;
public event Action piq_changed;
LadderModel model => _owner.model;
Dictionary<uint, (OrderRecordImmutable order, uint? p, AlgoData? algo, AlgoDefinitionMetadata? meta)> _orders = new Dictionary<uint, (OrderRecordImmutable order, uint? p, AlgoData? algo, AlgoDefinitionMetadata? meta)>();
@@ -171,25 +168,10 @@ namespace mbts.spark {
public void add_order((OrderRecordImmutable order, uint? p, AlgoData? algo, AlgoDefinitionMetadata? meta) new_op) {
_orders[new_op.order.Key] = new_op;
// reevaluate min piq if this order's piq is less
if (new_op.p.HasValue) {
// if this lovm only represents 1 order and the piq is now different, regardless of what it is that's our piq
if (_orders.Count == 1 && new_op.p.Value != min_piq) {
min_piq = new_op.p.Value;
} else if (new_op.p.Value < min_piq) {
min_piq = new_op.p.Value;
}
}
}
public void remove_order(uint order_key) {
_orders.Remove(order_key);
// on a remove, potentially the order w/ the lowest piq has been removed, so
// if the min() is now > than the current min_piq, update the min_piq
var prospective_piq = _orders.Select(op => op.Value.p).Where(piq => piq.HasValue).Min();
if (prospective_piq > min_piq) {
min_piq = prospective_piq;
}
}
//public void update(OrderRecordImmutable neworder) {
@@ -201,16 +183,7 @@ namespace mbts.spark {
public uint security_key => _owner.security.Id;
public double price { get => double.IsNaN(_phantom_price) ? _real_price : _phantom_price; }
public uint? min_piq {
get {
return _min_piq;
}
set {
if (Set(ref _min_piq, value)) {
piq_changed?.Invoke();
}
}
}
public uint? min_piq => _orders.Select(op => op.Value.p).Where(piq => piq.HasValue).Min();
public double real_price => _real_price;
public Side side {
get {
@@ -56,7 +56,7 @@ namespace mbts.spark {
LadderTactileAdorner _tactile_adorner;
ulong _prices_updated;
NativePriceSize[] _bids = new NativePriceSize[NativePriceHelpers.COCONUT_MAX_DEPTH], _asks = new NativePriceSize[NativePriceHelpers.COCONUT_MAX_DEPTH];
bool _price_dirty = false, _piq_dirty = false;
bool _price_dirty = false;
double[] _price_list = null;
IComparer<double> _price_comparer;
double _last_origin_price;
@@ -257,15 +257,11 @@ namespace mbts.spark {
case NotifyCollectionChangedAction.Add:
foreach (LadderOrderVM item in e.NewItems) {
_ordershost.add_order(item);
item.piq_changed += _set_piq_dirty;
_set_piq_dirty();
}
break;
case NotifyCollectionChangedAction.Remove:
foreach (LadderOrderVM item in e.OldItems) {
_ordershost.remove_order(item);
item.piq_changed -= _set_piq_dirty;
_set_piq_dirty();
}
break;
case NotifyCollectionChangedAction.Reset:
@@ -276,9 +272,6 @@ namespace mbts.spark {
throw new NotImplementedException();
}
}
void _set_piq_dirty() { _piq_dirty = true; }
#endregion
Column[] columns { get; set; } = new Column[0];
@@ -492,7 +485,6 @@ namespace mbts.spark {
_prices_updated = 0;
_price_comparer = Comparer<double>.Create((x, y) => security.ContractSpec.tick_equals(x, y) ? 0 : y.CompareTo(x));
_price_dirty = true;
_piq_dirty = true;
_vm.on_tick += _tick;
_settings_sub = _vm.settings.OnDispatcher().Subscribe(_settings_updated);
_volume_sub = _vm.model.order_gateway.md_auxiliary_model.security_volume_at_price(security.Id).Subscribe(t => _volume_container.write_value(t.state.Values));
@@ -588,10 +580,8 @@ namespace mbts.spark {
if (depth_changed)
_refresh_sizes();
}
if (_piq_dirty) {
_refresh_piq();
_piq_dirty = false;
}
// TODO - detect changes in PIQ to selectively redraw
_refresh_piq();
_refresh_volume_at_price();
_last_origin_price = _mkt_break_lines.origin;
_price_dirty = false;
@@ -632,7 +622,8 @@ namespace mbts.spark {
if (!_settings.piq_column.show)
return;
int origin_index = LadderRender.index_of(_price_list, _mkt_break_lines.origin, _price_comparer);
LadderRender.render_piqs(_piq_vis_tup.visual, _last_vis_tup.glyph_context, origin_index, _price_list, _vm.price_vs_piq, _settings.row_height, security.ContractSpec, _settings.piq_column);
var piq_list = _vm.price_vs_piq;
LadderRender.render_piqs(_piq_vis_tup.visual, _last_vis_tup.glyph_context, origin_index, _price_list, piq_list.ToArray(), _settings.row_height, security.ContractSpec, _settings.piq_column);
}
void _refresh_sizes() {
@@ -288,22 +288,22 @@ namespace mbts.spark {
}
// this is basically a copy of redner_sizes up above
internal static void render_piqs(DrawingVisual dv, GlyphContext glyph_context, int origin_index, double[] prices, IEnumerable<(double price, uint piq)> pvp, double row_height, ContractSpecificationImmutable spec, LadderColumnSettings settings) {
internal static void render_piqs(DrawingVisual dv, GlyphContext glyph_context, int origin_index, double[] prices, (double price, uint piq)[] piqs, double row_height, ContractSpecificationImmutable spec, LadderColumnSettings settings) {
using (var dc = dv.RenderOpen()) {
using (_draw_with_clip(dc, settings, row_height, prices.Length)) {
if (!prices.within_range(origin_index) || pvp.is_null_or_empty())
if (!prices.within_range(origin_index) || piqs.is_null_or_empty())
return;
var foreground = settings.foreground.rgb_brush();
int curr_index = origin_index;
double curr_price = prices[curr_index];
foreach (var entry in pvp) {
double price = entry.price;
for (int i = 0; i < piqs.Length; i++) {
double price = piqs[i].price;
// prices are in descending order, so get diff from curr_price - price
int tick_diff = spec.tickdiff_unchecked(curr_price, price);
curr_index += tick_diff;
curr_price = price;
if (curr_index >= 0 && curr_index < prices.Length) {
var gr = format_text(entry.piq.ToString(), curr_index, row_height, settings, glyph_context);
var gr = format_text(piqs[i].piq.ToString(), curr_index, row_height, settings, glyph_context);
dc.DrawGlyphRun(foreground, gr);
} else break;
}
+3 -4
View File
@@ -18,9 +18,9 @@
<RootNamespace>bts</RootNamespace>
<LangVersion>7.2</LangVersion>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<WarningsAsErrors />
<Version>1.0.0</Version>
</PropertyGroup>
<WarningsAsErrors />
<Version>1.0.0</Version>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="../spark_api/spark_api.csproj" />
<ProjectReference Include="../spark_core/spark_core.csproj" />
@@ -121,7 +121,6 @@
<Generator>MSBuild:Compile</Generator>
</None>
</ItemGroup>
<Target Name="diagnostic" AfterTargets="PreBuildEvent">
</Target>
</Project>
@@ -19,21 +19,6 @@ namespace bts.settings {
var from_persist = new UserUISettings(user_list.Users.First(u => u.Login == from).Id, persist, null);
var workspaces = from_persist.query<WorkspaceDefinition>(WorkspacesModel.WORKSPACE_NAMESPACE, ".*").Result;
ConsoleEx.WriteLine("Workspace Descriptions", ConsoleColor.Cyan);
foreach(var w in workspaces) {
var r = from ws in w.value.windows
group ws by ws.owner into grouping
select new { key = grouping.Key.Replace("bts.shell.", ""), window_count = grouping.Count() };
string desc = string.Empty;
foreach(var i in r) {
desc += $"{Environment.NewLine}\t{i.window_count} - {i.key}";
}
ConsoleEx.Write($"{w.key}", ConsoleColor.Yellow);
ConsoleEx.WriteLine($" - {w.value.windows.Count} windows - {w.value.named_windows.Count} named windows - {desc}");
}
ConsoleEx.WriteLine("");
var selected_ws = ConsoleEx.prompt("Choose workspace:", workspaces.Select(w => w.key));
var to = ConsoleEx.prompt("To User", user_names);
@@ -67,10 +67,6 @@ namespace mbts.co2
[DllImport("libspark")]
internal unsafe static extern void md_consumer_get_system_md_symbols(raw_usersymbol_callback cb);
[DllImport("libspark")]
internal unsafe static extern void dump_co2_memory_to_disk(IntPtr consumer, string path);
//
// Relay control
+3 -9
View File
@@ -37,10 +37,7 @@ namespace mbts.co2
unsafe void init() {
_raw_sec_cb = (key, buf, len, handle) => {
var security = Co2NativeMethods.deserialize<Security>(buf, len);
var next = new SecurityAndHandle() {
security = security,
handle = new MDConsumerHandle(_consumer, security, handle)
};
var next = new SecurityAndHandle() { security = security, handle = new MDConsumerHandle(handle) };
_securities.Add(next);
_on_security?.Invoke(new List<SecurityAndHandle>(1) { next });
};
@@ -70,7 +67,7 @@ namespace mbts.co2
public MDConsumerHandle try_get_handle(security_key key) {
Co2NativeMethods.BlitPointerTuple raw;
if (Co2NativeMethods.md_consumer_get_handle(_consumer, key, out raw)) {
return new MDConsumerHandle(this._consumer, null, raw);
return new MDConsumerHandle(raw);
}
return null;
}
@@ -80,10 +77,7 @@ namespace mbts.co2
List<SecurityAndHandle> result = new List<SecurityAndHandle>();
Co2NativeMethods.raw_security_callback cb = (key, buf, len, handle) => {
var security = Co2NativeMethods.deserialize<Security>(buf, len);
result.Add(
new SecurityAndHandle() {
security = new Security(security),
handle = new MDConsumerHandle(_consumer, security, handle) });
result.Add(new SecurityAndHandle() { security = new Security(security), handle = new MDConsumerHandle(handle) });
};
Co2NativeMethods.md_consumer_walk_security_list(_consumer, cb);
return result;
+2 -11
View File
@@ -156,22 +156,13 @@ namespace mbts.co2 {
//
// Internal
internal MDConsumerHandle(
IntPtr consumer,
Security security,
Co2NativeMethods.BlitPointerTuple tuple) {
internal MDConsumerHandle(Co2NativeMethods.BlitPointerTuple tuple) {
_header = (ColdHeader*)tuple.cold_header.ToPointer();
_cold = (ColdEntry*)tuple.cold_data.ToPointer();
if (has_depth) {
_hot_depth = (HotDepthEntry*)tuple.hot.ToPointer();
if (_hot_depth->key != _cold->security_key) {
var temp_path = Path.Combine(Path.GetTempPath(), "btsshm");
Co2NativeMethods.dump_co2_memory_to_disk(consumer, temp_path);
Log.assert(
_hot_depth->key == _cold->security_key,
$"Please attach the contents of '${temp_path}' to the bug report you are about to file. security:{security?.Id} cold:{_cold->security_key} hot:{_hot->key}");
}
Log.assert(_hot_depth->key == _cold->security_key);
}
else {
_hot = (HotEntry*)tuple.hot.ToPointer();
@@ -67,8 +67,8 @@ namespace bts.api.core {
while (true) {
var response = await check_stream_access(method_id, qualifier);
if (response.success) {
if (response.value.Authorized) {
// success. Break loop and subscribe
if (response.value.Authorized) {
// success. Break loop and subscribe
break;
}
else {
@@ -94,8 +94,7 @@ namespace bts.api.core {
// Authorized. Stream events
unsubscribe_temp_sub();
return SubscribeNative(method, qualifier, parser, converter, observer);
});
Log.QBus.diag($"Subscribing for sub: '{method}/{qualifier}'");
});
return result;
}
private Task<Result<StreamAccessResponse>> check_stream_access(int method_id, string qualifier) {
@@ -146,7 +145,6 @@ namespace bts.api.core {
};
var subscription = ApiNativeMethods.api_client_subscribe(_client, method, qualifier, cb, IntPtr.Zero);
return FinalizeableAnonymousDisposable.Create((disposing) => {
Log.QBus.diag($"Unsubscribing for sub: '{method}/{qualifier}'");
ApiNativeMethods.api_client_unsubscribe(_client, subscription);
subscription = IntPtr.Zero;
GC.KeepAlive(cb);
@@ -1,132 +0,0 @@
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;
using bts.api.orderman.algo;
namespace bts.tests {
[TestGroup(TestType = TestType.System)]
public class AlgoDefinitionTests : IDisposable {
List<IDisposable> _resources;
ServiceContext _context;
//OrderService _order_service;
AdminService _admin_service;
AlgoDefinitionService _algo_definition_service;
IEnumerable<AlgoDefinitionRecordImmutable> _non_deleted_defs;
Random _rng = new Random();
public AlgoDefinitionTests() {
_context = new ServiceContext();
//_order_service = _context.locate<OrderService>();
_admin_service = _context.locate<AdminService>();
_algo_definition_service = _context.locate<AlgoDefinitionService>();
_resources = new List<IDisposable> {
_context,
_admin_service,
_algo_definition_service,
_algo_definition_service.subscribei_algo_definitions().Subscribe(defs => _non_deleted_defs = defs.all.Where( x => x.Metadata.IsActive))
};
var auth = _context.login("bts", "");
auth.Wait(3000);
}
[Test(Timeout = 25)]
public async Task test_crud_algo_definitions() {
var created_algo_records = new List<AlgoDefinitionRecord>();
var watch = new System.Diagnostics.Stopwatch();
watch.Start();
var user_data = await AdminUtilities.get_user_data(_admin_service, "bts");
int num_created = 0;
if (user_data.HasValue) {
// create 100 algo defs
while (num_created < 100) {
var result = await _algo_definition_service.save_algo_definition_async(
new AlgoDefinitionUpdateRequest {
UserId = user_data.Value.Id,
Key = 0,
Algo = generate_random_algo_definition()
}
);
Assert.IsTrue(result.value.Success, "There was a problem creating an algo defintion");
created_algo_records.Add(result.value.Record);
num_created++;
}
// update the 100 we just created algo defs
foreach(var r in created_algo_records) {
var def = r.Algo;
def.Name += " modified";
await _algo_definition_service.save_algo_definition_async(
new AlgoDefinitionUpdateRequest {
UserId = user_data.Value.Id,
Key = r.Key,
Algo = def
}
);
}
// delete all non-deleted algo defs
Assert.IsTrue(_non_deleted_defs.Count() > 0, "There are no algo definitions to delete.");
foreach (var i in _non_deleted_defs.Select(x => x.Key).ToList()) {
var result = await _algo_definition_service.remove_algo_definition_async(new AlgoDefinitionRemoveRequest { Key = i });
Assert.IsTrue(result.value.Success, "There was a problem removing an algo definition");
}
Assert.IsTrue(_non_deleted_defs.Count() == 0, "There was a problem removing all algo definitions");
watch.Stop();
Console.WriteLine($"Execution Time: {watch.ElapsedMilliseconds} ms");
}
}
// not quite random yet
private AlgoDefinition generate_random_algo_definition() {
return new AlgoDefinition {
Definition = new CustomOrderDefinition {
Iceberg = new IcebergOrderDefinition {
ShowQuantity = _rng.NextDouble() * 100,
Child = new CustomOrderDefinition {
Limit = new LimitOrderDefinition { }
}
},
},
Name = $"Test Algo Def: {generate_name(_rng, _rng.Next(4, 15))}"
};
}
// https://stackoverflow.com/questions/14687658/random-name-generator-in-c-sharp
public static string generate_name(Random r, int len) {
string[] consonants = { "b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "l", "n", "p", "q", "r", "s", "sh", "zh", "t", "v", "w", "x" };
string[] vowels = { "a", "e", "i", "o", "u", "ae", "y" };
string name = "";
name += consonants[r.Next(consonants.Length)].ToUpper();
name += vowels[r.Next(vowels.Length)];
int b = 2; //b tells how many times a new letter has been added. It's 2 right now because the first two letters are already in the name.
while (b < len) {
name += consonants[r.Next(consonants.Length)];
b++;
name += vowels[r.Next(vowels.Length)];
b++;
}
return name;
}
public void Dispose() {
Disposable.dispose(_resources);
}
}
}
@@ -1,124 +0,0 @@
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.test;
using bts.api.orderman;
using mbts;
using bts.co2;
using bts.tests.utils;
namespace bts.tests {
[TestGroup(TestType = TestType.System)]
class IcebergAlgoTests : TestBase {
public IcebergAlgoTests() { }
[Test(Timeout = 25)]
public async Task test_ib_create_and_takeout_market() {
// find a security with no market
var s = await MarketDataUtilities.get_security(_md_relay, "CL", SecurityType.Future, update => update.Direct.Asks.Count == 0 && update.Direct.Bids.Count == 0);
Assert.IsTrue(s.HasValue, "Could not find a security with no maket");
// create a market in the instrument
List<Result<Response>> orders_response_list = new List<Result<Response>>();
var bid_order_def = OrderDefinitionUtilities.create_limit_order_def(s.Value.Id, Side.Buy, _test_account, _test_user);
var ask_order_def = OrderDefinitionUtilities.create_limit_order_def(s.Value.Id, Side.Sell, _test_account, _test_user);
orders_response_list.Add(await OrderUtilities.send_order(_order_service, bid_order_def, 55.00, 50));
orders_response_list.Add(await OrderUtilities.send_order(_order_service, ask_order_def, 55.05, 50));
List<OrderRecordImmutable> test_orders = new List<OrderRecordImmutable>();
foreach (var i in orders_response_list) {
var iret = (await OrderUtilities.wait_for_filtered_orders(
_order_service,
rows => rows.Where(o => o.Key == i.value.OrderKey && o.HasState && o.State.Status == OrderStatus.Working).Count() == 1,
rows => rows.Where(o => o.Key == i.value.OrderKey))).result ?? Enumerable.Empty<OrderRecordImmutable>();
Assert.IsTrue(iret.Count() == 1, "This order should have been 'working'");
test_orders.AddRange(iret);
}
// create an IB that will cross this market
var ib_order_def = OrderDefinitionUtilities.create_iceberg_order_def(s.Value.Id, Side.Buy, _test_account, _test_user, 3);
var ib_order_result = await OrderUtilities.send_order(_order_service, ib_order_def, 55.05, 30);
var ib_filtered_order = (await OrderUtilities.wait_for_filtered_orders(
_order_service,
rows => rows.Where(x => x.Key == ib_order_result.value.OrderKey && x.HasState && x.State.Status == OrderStatus.Completed).Count() == 1,
rows => rows.Where(x => x.Key == ib_order_result.value.OrderKey))).result ?? Enumerable.Empty<OrderRecordImmutable>();
var ib_child_orders = (await OrderUtilities.wait_for_filtered_orders(
_order_service,
rows => rows.Where(x => x.ParentKey == ib_order_result.value.OrderKey && x.HasState && x.State.Status == OrderStatus.Completed).Count() == 10,
rows => rows.Where(x => x.ParentKey == ib_order_result.value.OrderKey))).result ?? Enumerable.Empty<OrderRecordImmutable>();
Assert.IsTrue(ib_filtered_order.Count() == 1, "This IB order should have been 'Completed'");
Assert.IsTrue(ib_child_orders.Count() == 10, "There should have been 10 completed child orders");
var filled_childen = (await OrderUtilities.wait_for_filtered_fills(
_order_service,
rows => rows.Where(x => ib_child_orders.Select(o => o.Key).Contains(x.OrderKey)).Count() == 10,
rows => rows.Where(x => ib_child_orders.Select(o => o.Key).Contains(x.OrderKey)))).result ?? Enumerable.Empty<FillImmutable>();
Assert.IsTrue(filled_childen.Count() == 10, "There should have been 10 fills");
Assert.IsTrue(filled_childen.All(x =>
TestUtils.test_price_equality(x.LastPrice, 55.05, s.Value.ContractSpec.ticksize(x.LastPrice)) && x.LastShares == 3), "All child fills should have been filled at price 55.50 with quantity 3");
// cleanup
Assert.IsTrue(await OrderUtilities.cancel_orders(_order_service, test_orders), "Unable to cancel test orders");
}
[Test(Timeout = 25)]
public async Task test_ib_avg_fill() {
// find a security with no market
var s = await MarketDataUtilities.get_security(_md_relay, "CL", SecurityType.Future, update => update.Direct.Asks.Count == 0 && update.Direct.Bids.Count == 0);
Assert.IsTrue(s.HasValue, "Could not find a security with no maket");
// create a market in the instrument
List<Result<Response>> orders_response_list = new List<Result<Response>>();
var bid_order_def = OrderDefinitionUtilities.create_limit_order_def(s.Value.Id, Side.Buy, _test_account, _test_user);
var ask_order_def = OrderDefinitionUtilities.create_limit_order_def(s.Value.Id, Side.Sell, _test_account, _test_user);
orders_response_list.Add(await OrderUtilities.send_order(_order_service, ask_order_def, 56.02, 1));
orders_response_list.Add(await OrderUtilities.send_order(_order_service, ask_order_def, 56.01, 1));
orders_response_list.Add(await OrderUtilities.send_order(_order_service, ask_order_def, 56.00, 1));
orders_response_list.Add(await OrderUtilities.send_order(_order_service, ask_order_def, 55.50, 5));
orders_response_list.Add(await OrderUtilities.send_order(_order_service, ask_order_def, 55.01, 20));
orders_response_list.Add(await OrderUtilities.send_order(_order_service, bid_order_def, 55.00, 1));
orders_response_list.Add(await OrderUtilities.send_order(_order_service, bid_order_def, 54.99, 1));
orders_response_list.Add(await OrderUtilities.send_order(_order_service, bid_order_def, 54.98, 1));
orders_response_list.Add(await OrderUtilities.send_order(_order_service, bid_order_def, 54.97, 1));
orders_response_list.Add(await OrderUtilities.send_order(_order_service, bid_order_def, 54.96, 1));
orders_response_list.Add(await OrderUtilities.send_order(_order_service, bid_order_def, 54.95, 1));
List<OrderRecordImmutable> test_orders = new List<OrderRecordImmutable>();
foreach (var i in orders_response_list) {
var iret = (await OrderUtilities.wait_for_filtered_orders(
_order_service,
rows => rows.Where(o => o.Key == i.value.OrderKey && o.HasState && o.State.Status == OrderStatus.Working).Count() == 1,
rows => rows.Where(o => o.Key == i.value.OrderKey))).result ?? Enumerable.Empty<OrderRecordImmutable>();
Assert.IsTrue(iret.Count() == 1, "This order should have been 'working'");
test_orders.AddRange(iret);
}
// create an IB that will cross this market
var ib_order_def = OrderDefinitionUtilities.create_iceberg_order_def(s.Value.Id, Side.Buy, _test_account, _test_user, 1);
var ib_order_result = await OrderUtilities.send_order(_order_service, ib_order_def, 56.02, 26);
var ib_filtered_order = (await OrderUtilities.wait_for_filtered_orders(
_order_service,
rows => rows.Where(x => x.Key == ib_order_result.value.OrderKey && x.HasState && x.State.Status == OrderStatus.Completed).Count() == 1,
rows => rows.Where(x => x.Key == ib_order_result.value.OrderKey))).result ?? Enumerable.Empty<OrderRecordImmutable>();
var ib_child_orders = (await OrderUtilities.wait_for_filtered_orders(
_order_service,
rows => rows.Where(x => x.ParentKey == ib_order_result.value.OrderKey && x.HasState && x.State.Status == OrderStatus.Completed).Count() == 10,
rows => rows.Where(x => x.ParentKey == ib_order_result.value.OrderKey))).result ?? Enumerable.Empty<OrderRecordImmutable>();
Assert.IsTrue(ib_filtered_order.Count() == 1, "This IB order should have been 'Completed'");
Assert.IsTrue(ib_filtered_order.All(x =>
TestUtils.test_price_equality(x.State.AvgPrice, 55.1423, s.Value.ContractSpec.ticksize(x.State.AvgPrice))), "IB avg price should be 55.1423");
// cleanup
Assert.IsTrue(await OrderUtilities.cancel_orders(_order_service, test_orders), "Unable to cancel test orders");
}
}
}
@@ -1,194 +0,0 @@
using bts.api;
using bts.api.core;
using bts.api.orderman;
using bts.test;
using bts.tests.utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;
using System.Text;
using System.Threading.Tasks;
namespace bts.tests {
/// <summary>
/// For buy orders:
/// triggers when: curr_bid.price > initial_bid && current_bid.qty >= join_threshold.
/// i.e. When the join is first entered, it looks at the market to capture the initial bid and
/// then it waits until the current bid is above that level AND that the qty at that level is greater
/// that some threshold.
/// </summary>
[TestGroup(TestType = TestType.System)]
public class JoinAlgoTests : TestBase {
public JoinAlgoTests() { }
[Test(Timeout = 25)]
public async Task test_join_price() {
double initial_price = 54.99;
double ask_price = 55.01;
// find a security with no market
var s = await MarketDataUtilities.get_security(_md_relay, "CL", SecurityType.Future, update => update.Direct.Asks.Count == 0 && update.Direct.Bids.Count == 0);
Assert.IsTrue(s.HasValue, "Could not find a security with no maket");
// create a market in the instrument
List<Result<Response>> orders_response_list = new List<Result<Response>>();
var bid_order_def = OrderDefinitionUtilities.create_limit_order_def(s.Value.Id, Side.Buy, _test_account, _test_user);
var ask_order_def = OrderDefinitionUtilities.create_limit_order_def(s.Value.Id, Side.Sell, _test_account, _test_user);
orders_response_list.Add(await OrderUtilities.send_order(_order_service, bid_order_def, 55.00, 10));
orders_response_list.Add(await OrderUtilities.send_order(_order_service, ask_order_def, ask_price, 1));
List<OrderRecordImmutable> test_orders = new List<OrderRecordImmutable>();
foreach (var i in orders_response_list) {
var iret = (await OrderUtilities.wait_for_filtered_orders(
_order_service,
rows => rows.Where(o => o.Key == i.value.OrderKey && o.HasState && o.State.Status == OrderStatus.Working).Count() == 1,
rows => rows.Where(o => o.Key == i.value.OrderKey))).result ?? Enumerable.Empty<OrderRecordImmutable>();
Assert.IsTrue(iret.Count() == 1, "This order should have been 'working'");
test_orders.AddRange(iret);
}
// create a join order
var join_order_def = OrderDefinitionUtilities.create_join_order_definition(s.Value.Id, Side.Buy, _test_account, _test_user, new spark.QuantityMeasure() { Absolute = 1 });
var join_order_response = await OrderUtilities.send_order(_order_service, join_order_def, initial_price, 1);
var join_order = (await OrderUtilities.wait_for_filtered_orders(
_order_service,
rows => rows.Where(o => o.Key == join_order_response.value.OrderKey && o.HasState && o.State.Status == OrderStatus.Working).Count() == 1,
rows => rows.Where(o => o.Key == join_order_response.value.OrderKey))).result ?? Enumerable.Empty<OrderRecordImmutable>();
Assert.IsTrue(join_order.Count() == 1, "The join order should have 'working'");
test_orders.Add(join_order.First());
// verify the join order's child is working at the specefied price
// note: the price doesn't actually matter in the join order
var join_child_order = (await OrderUtilities.wait_for_filtered_orders(
_order_service,
rows => rows.Where(o => o.ParentKey == join_order.First().Key &&
o.HasState && (o.State.Status == OrderStatus.Working &&
TestUtils.test_price_equality(o.State.Price, initial_price, s.Value.ContractSpec.ticksize(o.State.Price)))).Count() == 1,
rows => rows.Where(o => o.ParentKey == join_order.First().Key))).result ?? Enumerable.Empty<OrderRecordImmutable>();
Assert.IsTrue(join_child_order.Count() == 1, $"The join child order should have been 'working' at {initial_price}");
// now we move the best bid to become the best offer
var original_bid_order = test_orders.Where(o => o.Def.Side == Side.Buy).First();
var bid_order_response = await OrderUtilities.modify_order(_order_service, original_bid_order, ask_price);
var modified_bid_order = (await OrderUtilities.wait_for_filtered_orders(
_order_service,
rows => rows.Where(o => o.Key == bid_order_response.value.OrderKey && o.HasState &&
TestUtils.test_price_equality(o.State.Price, ask_price, s.Value.ContractSpec.ticksize(o.State.Price))).Count() == 1,
rows => rows.Where(o => o.Key == bid_order_response.value.OrderKey))).result ?? Enumerable.Empty<OrderRecordImmutable>();
Assert.IsTrue(modified_bid_order.Count() == 1, "The working bid should have become the best offer");
// and we finally verify that the child order has joined the market at the appropriate price
join_child_order = (await OrderUtilities.wait_for_filtered_orders(
_order_service,
rows => rows.Where(o => o.ParentKey == join_order.First().Key &&
o.HasState && (o.State.Status == OrderStatus.Working &&
TestUtils.test_price_equality(o.State.Price, ask_price, s.Value.ContractSpec.ticksize(o.State.Price)))).Count() == 1,
rows => rows.Where(o => o.ParentKey == join_order.First().Key))).result ?? Enumerable.Empty<OrderRecordImmutable>();
Assert.IsTrue(join_child_order.Count() == 1, $"The join child order should now have been 'working' at {ask_price}");
test_orders.Add(join_child_order.First());
// cleanup
Assert.IsTrue(await OrderUtilities.cancel_orders(_order_service, test_orders), "Unable to cancel test orders");
}
[Test(Timeout = 25)]
public async Task test_join_qty_threshold() {
double initial_price = 54.99;
double ask_price = 55.01;
uint ask_size = 1;
uint qty_threshold = 15;
// find a security with no market
var s = await MarketDataUtilities.get_security(_md_relay, "CL", SecurityType.Future, update => update.Direct.Asks.Count == 0 && update.Direct.Bids.Count == 0);
Assert.IsTrue(s.HasValue, "Could not find a security with no maket");
// create a market in the instrument
List<Result<Response>> orders_response_list = new List<Result<Response>>();
var bid_order_def = OrderDefinitionUtilities.create_limit_order_def(s.Value.Id, Side.Buy, _test_account, _test_user);
var ask_order_def = OrderDefinitionUtilities.create_limit_order_def(s.Value.Id, Side.Sell, _test_account, _test_user);
orders_response_list.Add(await OrderUtilities.send_order(_order_service, bid_order_def, 55.00, 10));
orders_response_list.Add(await OrderUtilities.send_order(_order_service, ask_order_def, ask_price, ask_size));
List<OrderRecordImmutable> test_orders = new List<OrderRecordImmutable>();
foreach (var i in orders_response_list) {
var iret = (await OrderUtilities.wait_for_filtered_orders(
_order_service,
rows => rows.Where(o => o.Key == i.value.OrderKey && o.HasState && o.State.Status == OrderStatus.Working).Count() == 1,
rows => rows.Where(o => o.Key == i.value.OrderKey))).result ?? Enumerable.Empty<OrderRecordImmutable>();
Assert.IsTrue(iret.Count() == 1, "This order should have been 'working'");
test_orders.AddRange(iret);
}
// create a join order
var join_order_def = OrderDefinitionUtilities.create_join_order_definition(s.Value.Id, Side.Buy, _test_account, _test_user, new spark.QuantityMeasure() { Absolute = qty_threshold });
var join_order_response = await OrderUtilities.send_order(_order_service, join_order_def, initial_price, 1);
var join_order = (await OrderUtilities.wait_for_filtered_orders(
_order_service,
rows => rows.Where(o => o.Key == join_order_response.value.OrderKey && o.HasState && o.State.Status == OrderStatus.Working).Count() == 1,
rows => rows.Where(o => o.Key == join_order_response.value.OrderKey))).result ?? Enumerable.Empty<OrderRecordImmutable>();
Assert.IsTrue(join_order.Count() == 1, "The join order should have 'working'");
test_orders.Add(join_order.First());
// verify the join order's child is working at the specefied price
// note: the price doesn't actually matter in the join order
var join_child_order = (await OrderUtilities.wait_for_filtered_orders(
_order_service,
rows => rows.Where(o => o.ParentKey == join_order.First().Key &&
o.HasState && (o.State.Status == OrderStatus.Working &&
TestUtils.test_price_equality(o.State.Price, initial_price, s.Value.ContractSpec.ticksize(o.State.Price)))).Count() == 1,
rows => rows.Where(o => o.ParentKey == join_order.First().Key))).result ?? Enumerable.Empty<OrderRecordImmutable>();
Assert.IsTrue(join_child_order.Count() == 1, $"The join child order should have been 'working' at {initial_price}");
// now we move the best bid to become the best offer
var top_bid_order = test_orders.Where(o => o.Def.Side == Side.Buy).First();
var bid_order_response = await OrderUtilities.modify_order(_order_service, top_bid_order, ask_price);
var modified_bid_order = (await OrderUtilities.wait_for_filtered_orders(
_order_service,
rows => rows.Where(o => o.Key == bid_order_response.value.OrderKey && o.HasState &&
TestUtils.test_price_equality(o.State.Price, ask_price, s.Value.ContractSpec.ticksize(o.State.Price))).Count() == 1,
rows => rows.Where(o => o.Key == bid_order_response.value.OrderKey))).result ?? Enumerable.Empty<OrderRecordImmutable>();
Assert.IsTrue(modified_bid_order.Count() == 1, "The working bid should have become the best offer");
// verify the join order's child is *still* working at the initial price
// b/c the quantity threshold has not yet been met
join_child_order = (await OrderUtilities.wait_for_filtered_orders(
_order_service,
rows => rows.Where(o => o.ParentKey == join_order.First().Key &&
o.HasState && (o.State.Status == OrderStatus.Working &&
TestUtils.test_price_equality(o.State.Price, initial_price, s.Value.ContractSpec.ticksize(o.State.Price)))).Count() == 1,
rows => rows.Where(o => o.ParentKey == join_order.First().Key))).result ?? Enumerable.Empty<OrderRecordImmutable>();
Assert.IsTrue(join_child_order.Count() == 1, $"The join child order should still have been 'working' at {initial_price}");
// now we modify our top bid's volume to meet the join's threshold
// note that we must add the intial ask_size to the qty_threshold b/c we have partially filled our bid order
// when we moved the bid up.
var target_qty = qty_threshold + ask_size;
bid_order_response = await OrderUtilities.modify_order(_order_service, modified_bid_order.First(), target_qty);
modified_bid_order = (await OrderUtilities.wait_for_filtered_orders(
_order_service,
rows => rows.Where(o => o.Key == bid_order_response.value.OrderKey && o.HasState && o.State.Quantity == target_qty).Count() == 1,
rows => rows.Where(o => o.Key == bid_order_response.value.OrderKey))).result ?? Enumerable.Empty<OrderRecordImmutable>();
Assert.IsTrue(modified_bid_order.Count() == 1, $"The working bid should have its quantity modified to {target_qty}");
// now that the qty threshold has been met, verify that the child order has joined the market at the appropriate price
join_child_order = (await OrderUtilities.wait_for_filtered_orders(
_order_service,
rows => rows.Where(o => o.ParentKey == join_order.First().Key &&
o.HasState && (o.State.Status == OrderStatus.Working &&
TestUtils.test_price_equality(o.State.Price, ask_price, s.Value.ContractSpec.ticksize(o.State.Price)))).Count() == 1,
rows => rows.Where(o => o.ParentKey == join_order.First().Key))).result ?? Enumerable.Empty<OrderRecordImmutable>();
Assert.IsTrue(join_child_order.Count() == 1, $"The join child order should now have been 'working' at {ask_price}");
test_orders.Add(join_child_order.First());
// cleanup
Assert.IsTrue(await OrderUtilities.cancel_orders(_order_service, test_orders), "Unable to cancel test orders");
}
}
}
@@ -1,144 +0,0 @@
using bts.api;
using bts.api.core;
using bts.api.orderman;
using bts.test;
using bts.tests.utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;
using System.Text;
using System.Threading.Tasks;
namespace bts.tests {
[TestGroup(TestType = TestType.System)]
public class LeanAlgoTests : TestBase {
public LeanAlgoTests() { }
[Test(Timeout = 25)]
public async Task test_lean_price() {
double initial_price = 54.98;
double ask_price_1 = 55.00;
double ask_price_2 = 55.01;
uint ask_size = 10;
uint lean_order_size = 30;
uint cross_threshold = ask_size - 1;
// find a security with no market
var s = await MarketDataUtilities.get_security(_md_relay, "CL", SecurityType.Future, update => update.Direct.Asks.Count == 0 && update.Direct.Bids.Count == 0);
Assert.IsTrue(s.HasValue, "Could not find a security with no maket");
// create a market in the instrument
List<Result<Response>> orders_response_list = new List<Result<Response>>();
var bid_order_def = OrderDefinitionUtilities.create_limit_order_def(s.Value.Id, Side.Buy, _test_account, _test_user);
var ask_order_def = OrderDefinitionUtilities.create_limit_order_def(s.Value.Id, Side.Sell, _test_account, _test_user);
orders_response_list.Add(await OrderUtilities.send_order(_order_service, bid_order_def, 54.99, 1));
orders_response_list.Add(await OrderUtilities.send_order(_order_service, ask_order_def, ask_price_1, ask_size));
orders_response_list.Add(await OrderUtilities.send_order(_order_service, ask_order_def, ask_price_2, ask_size));
orders_response_list.Add(await OrderUtilities.send_order(_order_service, ask_order_def, ask_price_2 + 0.01, ask_size));
orders_response_list.Add(await OrderUtilities.send_order(_order_service, ask_order_def, ask_price_2 + 0.02, ask_size));
List<OrderRecordImmutable> test_orders = new List<OrderRecordImmutable>();
foreach (var i in orders_response_list) {
var iret = (await OrderUtilities.wait_for_filtered_orders(
_order_service,
rows => rows.Where(o => o.Key == i.value.OrderKey && o.HasState && o.State.Status == OrderStatus.Working).Count() == 1,
rows => rows.Where(o => o.Key == i.value.OrderKey))).result ?? Enumerable.Empty<OrderRecordImmutable>();
Assert.IsTrue(iret.Count() == 1, "This order should have been 'working'");
test_orders.AddRange(iret);
}
// create a lean order, 2 tick chase (default)
var lean_order_def = OrderDefinitionUtilities.create_lean_order_definition(s.Value.Id, Side.Buy, _test_account, _test_user, new spark.QuantityMeasure() { Absolute = cross_threshold });
var lean_order_response = await OrderUtilities.send_order(_order_service, lean_order_def, initial_price, lean_order_size);
var lean_order = (await OrderUtilities.wait_for_filtered_orders(
_order_service,
rows => rows.Where(o => o.Key == lean_order_response.value.OrderKey && o.HasState && o.State.Status == OrderStatus.Working).Count() == 1,
rows => rows.Where(o => o.Key == lean_order_response.value.OrderKey))).result ?? Enumerable.Empty<OrderRecordImmutable>();
Assert.IsTrue(lean_order.Count() == 1, "The lean order should have 'working'");
test_orders.Add(lean_order.First());
// verify the lean order's child is working at the specified price
// note: the price doesn't actually matter in the lean order
var lean_child_order = (await OrderUtilities.wait_for_filtered_orders(
_order_service,
rows => rows.Where(o => o.ParentKey == lean_order.First().Key &&
o.HasState && (o.State.Status == OrderStatus.Working &&
TestUtils.test_price_equality(o.State.Price, initial_price, s.Value.ContractSpec.ticksize(o.State.Price)))).Count() == 1,
rows => rows.Where(o => o.ParentKey == lean_order.First().Key))).result ?? Enumerable.Empty<OrderRecordImmutable>();
Assert.IsTrue(lean_child_order.Count() == 1, $"The lean child order should have been 'working' at {initial_price}");
// now we modify the opposite inside market qty to breach the lean threshold
var target_ask_order = test_orders.Where(o => o.Def.Side == Side.Sell &&
TestUtils.test_price_equality(o.State.Price, ask_price_1, s.Value.ContractSpec.ticksize(o.State.Price))).First();
var ask_order_response = await OrderUtilities.modify_order(_order_service, target_ask_order, cross_threshold);
var modified_ask_order = (await OrderUtilities.wait_for_filtered_orders(
_order_service,
rows => rows.Where(o => o.Key == ask_order_response.value.OrderKey && o.HasState &&
o.State.Quantity == cross_threshold).Count() == 1,
rows => rows.Where(o => o.Key == ask_order_response.value.OrderKey))).result ?? Enumerable.Empty<OrderRecordImmutable>();
Assert.IsTrue(modified_ask_order.Count() == 1, "The opposite inside market should have been adjusted to the Lean's cross threshold");
// verify that the child order has leaned into the market at the appropriate price and been parially filled appropriately
// this is the initial trigger payup
lean_child_order = (await OrderUtilities.wait_for_filtered_orders(
_order_service,
rows => rows.Where(o => o.ParentKey == lean_order.First().Key &&
o.HasState && (
o.State.Status == OrderStatus.Working &&
TestUtils.test_price_equality(o.State.Price, ask_price_1, s.Value.ContractSpec.ticksize(o.State.Price)) &&
o.State.FilledQuantity == 9)
).Count() == 1,
rows => rows.Where(o => o.ParentKey == lean_order.First().Key))).result ?? Enumerable.Empty<OrderRecordImmutable>();
Assert.IsTrue(lean_child_order.Count() == 1, $"Initial trigger: The lean child order should now have been 'working' at {ask_price_1}");
test_orders.Add(lean_child_order.First());
// now we modify the opposite inside market qty again to start checking that the case is working
target_ask_order = test_orders.Where(o => o.Def.Side == Side.Sell &&
TestUtils.test_price_equality(o.State.Price, ask_price_2, s.Value.ContractSpec.ticksize(o.State.Price))).First();
ask_order_response = await OrderUtilities.modify_order(_order_service, target_ask_order, cross_threshold);
modified_ask_order = (await OrderUtilities.wait_for_filtered_orders(
_order_service,
rows => rows.Where(o => o.Key == ask_order_response.value.OrderKey && o.HasState &&
o.State.Quantity == cross_threshold).Count() == 1,
rows => rows.Where(o => o.Key == ask_order_response.value.OrderKey))).result ?? Enumerable.Empty<OrderRecordImmutable>();
Assert.IsTrue(modified_ask_order.Count() == 1, "The opposite inside market should have been adjusted to the Lean's cross threshold");
// verify that the child order has chased and that it's filled quantity has increased
lean_child_order = (await OrderUtilities.wait_for_filtered_orders(
_order_service,
rows => rows.Where(o => o.ParentKey == lean_order.First().Key &&
o.HasState && (
o.State.Status == OrderStatus.Working &&
TestUtils.test_price_equality(o.State.Price, ask_price_2, s.Value.ContractSpec.ticksize(o.State.Price)) &&
o.State.FilledQuantity == 9)
).Count() == 1,
rows => rows.Where(o => o.ParentKey == lean_order.First().Key))).result ?? Enumerable.Empty<OrderRecordImmutable>();
Assert.IsTrue(lean_child_order.Count() == 1, $"First Chase: The lean child order should now have been 'working' at {ask_price_1}");
test_orders.Add(lean_child_order.First());
// TODO:
// verify the parent order also reflects the increase filled quantity from the child order
// TODO:
// instigate another chase
// TODO:
// verify parent order again reflects
// TODO:
// instigate another chanse
// TODO:
// verify we are *not* chasing any longer, verify appropriate order statuses
// cleanup
Assert.IsTrue(await OrderUtilities.cancel_orders(_order_service, test_orders), "Unable to cancel test orders");
}
}
}
+262 -27
View File
@@ -13,7 +13,6 @@ using bts.api.orderman;
using mbts;
using mbts.tests;
using bts.co2;
using bts.tests.utils;
namespace bts.tests
{
@@ -44,7 +43,6 @@ namespace bts.tests
Disposable.dispose(ref _order_service);
Disposable.dispose(ref _admin_service);
Disposable.dispose(ref _context);
Disposable.dispose(ref _order_subscription);
}
async Task<IEnumerable<SecurityImmutable>> all_futures(string symbol) {
@@ -80,31 +78,28 @@ namespace bts.tests
return securities;
}
[Test(Timeout = 300)]
[Test(Timeout = 30)]
public async Task test_position_limits() {
bool system_ready = await TestUtils.test_for_system_ready(_context, TimeSpan.FromMinutes(5));
Assert.IsTrue(system_ready, "System is not ready");
string test_symbol = "CL";
var test_user = ( name: "bts", id: 0u );
var test_account = ( name: "ACC A", id: 0u );
var account_task = AdminUtilities.get_account_data(_admin_service, test_account.name);
BTSAccountImmutable? account_data = await TestUtils.await_with_timeout<BTSAccountImmutable?>(account_task, TimeSpan.FromMinutes(0.5));
BTSAccountImmutable? account_data = await AdminUtilities.get_account_data(_admin_service, test_account.name);
Assert.IsTrue(account_data.HasValue);
test_account.id = account_data.Value.Id;
var user_data = await AdminUtilities.get_user_data(_admin_service, test_user.name);
Assert.IsTrue(user_data.HasValue);
test_user.id = user_data.Value.Id;
var clip_set_success = await AdminUtilities.set_clip_limit(_admin_service, 10000, account_data, test_symbol);
Assert.IsTrue(clip_set_success, "Failed to set clip limit ");
var all_securities = await all_futures(test_symbol);
List<SecurityImmutable> security_list = all_securities.ToList();
Assert.IsTrue(all_securities.Count() >= 2, "At least two securities are needed");
Assert.IsTrue(all_securities.Count() >= 2);
SecurityImmutable first_sec = security_list[0];
SecurityImmutable second_sec = security_list[1];
@@ -114,11 +109,10 @@ namespace bts.tests
var second_order_def = create_order_def(second_sec.Id, Side.Buy, test_account, test_user);
Result<Response> no_limit_order_result = await OrderUtilities.send_order(_order_service, first_order_def, 5, 1);
Result<Response> no_limit_order_result = await send_order(first_order_def, 5, 1);
IEnumerable<OrderRecordImmutable> no_limit_filtered_orders = await get_filtered_orders(x => x.Key == no_limit_order_result.value.OrderKey);
Assert.IsNotNull(no_limit_filtered_orders, "No orders retrieved");
Assert.IsTrue(no_limit_filtered_orders.First().State.Status == OrderStatus.RejectedBySpark, "'Absent Limit' Order is not 'Rejected by Spark'");
bool position_set_success = await AdminUtilities.set_position_limit(_admin_service, test_account.name, test_symbol, 60, 120);
@@ -127,11 +121,11 @@ namespace bts.tests
List<Result<Response>> orders_response_list = new List<Result<Response>>();
for(int i = 0; i <3; i++){
orders_response_list.Add(await OrderUtilities.send_order(_order_service, first_order_def, 5, 10));
orders_response_list.Add(await send_order(first_order_def, 5, 10));
}
for(int i = 0; i <3; i++){
orders_response_list.Add(await OrderUtilities.send_order(_order_service, second_order_def, 5, 10));
orders_response_list.Add(await send_order(second_order_def, 5, 10));
}
foreach(var i in orders_response_list){
@@ -139,24 +133,24 @@ namespace bts.tests
Assert.IsTrue(iret.First().State.Status == OrderStatus.Working, "This order should have been 'working'");
}
var fail_order_result = await OrderUtilities.send_order(_order_service, first_order_def, 5, 1);
var fail_order_result = await send_order(first_order_def, 5, 1);
var fail_order_filtered_orders = await get_filtered_orders(x => x.Key == fail_order_result.value.OrderKey);
Assert.IsTrue(fail_order_filtered_orders.First().State.Status == OrderStatus.RejectedBySpark, "This order should have been 'Rejected by Spark'");
bool position_increase_success = await AdminUtilities.set_position_limit(_admin_service, test_account.name, test_symbol, 70, 120);
Assert.IsTrue(position_increase_success);
var increased_order_result = await OrderUtilities.send_order(_order_service, first_order_def, 5, 10);
var increased_order_result = await send_order(first_order_def, 5, 10);
var increased_order_filtered_orders = await get_filtered_orders(x => x.Key == increased_order_result.value.OrderKey);
Assert.IsTrue(increased_order_filtered_orders.First().State.Status == OrderStatus.Working, "This order should have been 'Working'");
var second_fail_order_result = await OrderUtilities.send_order(_order_service, first_order_def, 5, 10);
var second_fail_order_result = await send_order(first_order_def, 5, 10);
var second_fail_filtered_orders = await get_filtered_orders(x => x.Key == second_fail_order_result.value.OrderKey);
Assert.IsTrue(second_fail_filtered_orders.First().State.Status == OrderStatus.RejectedBySpark, "This order should have been 'Rejected by Spark'");
/* Test Short Position Limit */
Assert.IsTrue(await OrderUtilities.cancel_orders(_order_service, _orders.all), "Unable to cancel orders");
Assert.IsTrue(await cancel_orders(), "Unable to cancel orders");
await AdminUtilities.remove_position_limit(_admin_service, test_account.name, test_symbol);
@@ -168,7 +162,7 @@ namespace bts.tests
uint short_no_limit_order_key;
Result<Response> short_no_limit_order_result = await OrderUtilities.send_order(_order_service, first_short_order_def, 50000, 7);
Result<Response> short_no_limit_order_result = await send_order(first_short_order_def, 50000, 7);
short_no_limit_order_key = short_no_limit_order_result.value.OrderKey;
IEnumerable<OrderRecordImmutable> short_no_limit_filtered_orders = await get_filtered_orders(x => x.Key == short_no_limit_order_key);
@@ -181,11 +175,11 @@ namespace bts.tests
List<Result<Response>> short_orders_response_list = new List<Result<Response>>();
for(int i = 0; i <3; i++){
short_orders_response_list.Add(await OrderUtilities.send_order(_order_service, first_short_order_def, 50000, 10));
short_orders_response_list.Add(await send_order(first_short_order_def, 50000, 10));
}
for(int i = 0; i <3; i++){
short_orders_response_list.Add(await OrderUtilities.send_order(_order_service, second_short_order_def, 50000, 10));
short_orders_response_list.Add(await send_order(second_short_order_def, 50000, 10));
}
foreach(var i in short_orders_response_list){
@@ -193,22 +187,22 @@ namespace bts.tests
Assert.IsTrue(iret.First().State.Status == OrderStatus.Working, "This order should have been 'working'");
}
var short_fail_order_result = await OrderUtilities.send_order(_order_service, first_short_order_def, 50000, 1);
var short_fail_order_result = await send_order(first_short_order_def, 50000, 1);
var short_fail_order_filtered_orders = await get_filtered_orders(x => x.Key == short_fail_order_result.value.OrderKey);
Assert.IsTrue(short_fail_order_filtered_orders.First().State.Status == OrderStatus.RejectedBySpark, "This order should have been 'Rejected by Spark'");
bool short_position_increase_success = await AdminUtilities.set_position_limit(_admin_service, test_account.name, test_symbol, 120, 70);
Assert.IsTrue(short_position_increase_success);
var short_increased_order_result = await OrderUtilities.send_order(_order_service, first_short_order_def, 50000, 10);
var short_increased_order_result = await send_order(first_short_order_def, 50000, 10);
var short_increased_order_filtered_orders = await get_filtered_orders(x => x.Key == short_increased_order_result.value.OrderKey);
Assert.IsTrue(short_increased_order_filtered_orders.First().State.Status == OrderStatus.Working, "This order should have been 'Working'");
var short_second_fail_order_result = await OrderUtilities.send_order(_order_service, first_short_order_def, 50000, 10);
var short_second_fail_order_result = await send_order(first_short_order_def, 50000, 10);
var short_second_fail_filtered_orders = await get_filtered_orders(x => x.Key == short_second_fail_order_result.value.OrderKey);
Assert.IsTrue(short_second_fail_filtered_orders.First().State.Status == OrderStatus.RejectedBySpark, "This order should have been 'Rejected by Spark'");
Assert.IsTrue(await OrderUtilities.cancel_orders(_order_service, _orders.all), "Unable to cancel orders");
Assert.IsTrue(await cancel_orders(), "Unable to cancel orders");
}
@@ -292,7 +286,6 @@ namespace bts.tests
}
Assert.IsTrue(success, "Failed to restore Limit");
Assert.IsTrue(await OrderUtilities.cancel_orders(_order_service, _orders.all), "Unable to cancel orders");
}
@@ -312,12 +305,40 @@ namespace bts.tests
}
await Task.Delay(100);
}
while(it < 100 && (_orders == null || current_count < count));
while(it < 100 &&(_orders == null || current_count < count));
return null;
}
public async Task<Result<Response>> send_order(OrderDefinition order_def, double price, uint size){
return await _order_service.create_order_async(new Create() {
NewOrder = order_def,
Price = price,
Quantity = size
});
}
public async Task<bool> cancel_orders(uint key = 0){
if (key > 0) {
Result<Response> resp = await _order_service.cancel_order_async(new Cancel() { OrderKey = key });
return resp.success;
}
else {
bool suc = true;
var working_orders = await get_filtered_orders(x => x.State.Status == OrderStatus.Working);
if(working_orders != null){
foreach (var o in working_orders){
Result<Response> resp = await _order_service.cancel_order_async(new Cancel() { OrderKey = o.Key });
suc &= resp.success;
}
return suc;
}
return true;// there are no working orders... so true enough
}
}
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,
@@ -337,4 +358,218 @@ namespace bts.tests
}
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 async Task<BTSUserImmutable?> get_user_data (AdminService admin_service, string user_name)
{
Update<AdminDataImmutable, int> update = await admin_service.subscribei_admin_data().FirstAsync();
foreach (var usr in update.all.First().Users) {
if(usr.Name == user_name) {
return usr;
}
}
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> clear_all_limits(AdminService admin_service, string account_name) {
var account_data = await get_account_data(admin_service, account_name);
return await clear_all_limits(admin_service, account_data);
}
public static async Task<bool> clear_all_limits(AdminService admin_service, BTSAccountImmutable? account_data) {
var new_account_data = account_data.Value.with(security_clip_limits: new AccountSecurityClipLimits(), product_position_limits: new List<AccountProductPositionLimitUnit>());
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;
}
}
public static async Task<AccountProductPositionLimitUnitImmutable?> get_position_limit_row(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_position_limit_row(account_data, symbol, security_type, venue);
}
public static AccountProductPositionLimitUnitImmutable? get_position_limit_row(BTSAccountImmutable? account_data, string symbol, SecurityType security_type = SecurityType.Future, Venue venue = Venue.Sim) {
IEnumerable<AccountProductPositionLimitUnitImmutable> limit_rows = account_data.Value.ProductPositionLimits.Where(
r => r.Product.SecuritySymbol == symbol
&& r.Product.Exchange == venue
&& r.Product.SecurityType == security_type);
return limit_rows.Any() ? limit_rows.First() : (AccountProductPositionLimitUnitImmutable?) null;
}
public static async Task<bool> remove_position_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_position_limit(admin_service, account_data, symbol, security_type, venue);
}
public static async Task<bool> remove_position_limit(AdminService admin_service, BTSAccountImmutable? account_data, string symbol, SecurityType security_type = SecurityType.Future, Venue venue = Venue.Sim) {
AccountProductPositionLimitUnitImmutable? origin_limit_row = get_position_limit_row(account_data,symbol,security_type,venue);
List<AccountProductPositionLimitUnitImmutable> limits_list = account_data.Value.ProductPositionLimits.ToList();
if(origin_limit_row.HasValue) {
limits_list.Remove(origin_limit_row.Value);
List<AccountProductPositionLimitUnit> temp_plu = new List<AccountProductPositionLimitUnit>();
temp_plu.AddRange(limits_list.Select(x => x.CloneToMutable()));
var new_account_data = account_data.Value.with(product_position_limits: temp_plu);
var update_ret = await admin_service.update_account_async(new AccountUpdateAPIArgument {
CompleteData = new_account_data.CloneToMutable()
});
return update_ret.success;
}
else
{
return false;
}
}
public static async Task<bool> set_position_limit(AdminService admin_service, string account_name, string symbol, uint long_limit, uint short_limit, SecurityType security_type = SecurityType.Future, Venue venue = Venue.Sim) {
var account_data = await get_account_data(admin_service, account_name);
return await set_position_limit(admin_service, account_data, symbol, long_limit, short_limit, security_type, venue);
}
public static async Task<bool> set_position_limit(AdminService admin_service, BTSAccountImmutable? account_data, string symbol, uint long_limit, uint short_limit, SecurityType security_type = SecurityType.Future, Venue venue = Venue.Sim) {
AccountProductPositionLimitUnitImmutable? limit_row = get_position_limit_row(account_data,symbol,security_type,venue);
List<AccountProductPositionLimitUnitImmutable> limits_list = account_data.Value.ProductPositionLimits.ToList();
if(limit_row.HasValue) {
// update the row
limits_list.Remove(limit_row.Value);
limit_row = limit_row.Value.with(long_position_limit: long_limit, short_position_limit: short_limit);
}
else {
// create a new row
AccountProductPositionLimitUnit acplu = new AccountProductPositionLimitUnit();
acplu.LongPositionLimit = long_limit;
acplu.ShortPositionLimit = short_limit;
acplu.Product = new ProductScope {
SecuritySymbol = symbol,
SecurityType = security_type,
Exchange = venue
};
limit_row = acplu;
}
limits_list.Add(limit_row.Value);
List<AccountProductPositionLimitUnit> temp_plu = new List<AccountProductPositionLimitUnit>();
temp_plu.AddRange(limits_list.Select(x => x.CloneToMutable()));
var new_account_data = account_data.Value.with(product_position_limits: temp_plu);
var update_ret = await admin_service.update_account_async(new AccountUpdateAPIArgument {
CompleteData = new_account_data.CloneToMutable()
});
return update_ret.success;
}
}
}
@@ -1,152 +0,0 @@
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;
using bts.tests.utils;
using bts.api.synthetic;
using bts.spark;
namespace bts.tests
{
[TestGroup(TestType = TestType.System)]
public class SpreaderTests : IDisposable {
ServiceContext _context;
OrderService _order_service;
AdminService _admin_service;
MarketDataRelay _md_relay;
SyntheticSecurityService _synthetic_service;
IDisposable _order_subscription;
Update<OrderRecordImmutable, uint> _orders;
public SpreaderTests (){
_context = new ServiceContext();
_order_service = _context.locate<OrderService>();
_admin_service = _context.locate<AdminService>();
_md_relay = _context.locate<MarketDataRelay>();
_synthetic_service = _context.locate<SyntheticSecurityService>();
var auth = _context.login("bts","");
auth.Wait(3000);
_order_subscription = _order_service.subscribei_orders().Subscribe(ords =>
{
_orders = ords;
});
}
public void Dispose() {
Disposable.dispose(ref _order_service);
Disposable.dispose(ref _admin_service);
Disposable.dispose(ref _context);
Disposable.dispose(ref _order_subscription);
}
[Test(Timeout = 30)]
public async Task spreader_simple_sanity() {
bool system_ready = await TestUtils.test_for_system_ready(_context, TimeSpan.FromMinutes(5));
Assert.IsTrue(system_ready, "System is not ready");
string test_symbol = "SI";// don't use ES, the prices will be rounded to the same number and the result will be off
var test_user = (name: "bts", id: 1u);
var test_account = (name: "ACC A", id: 1u);
(SecurityImmutable s1, SecurityImmutable s2) test_securities = await SpreaderUtilities.get_securities_for_common_setup_one(_md_relay, test_symbol);
var setup_product = await SpreaderUtilities.common_setup_one(_order_service, _synthetic_service, test_securities, test_account, test_user);
var order1_def = OrderDefinitionUtilities.create_spreader_order_def(
setup_product.synthetic.security,
Side.Buy,
setup_product.synthetic.strategy,
test_account,
test_user
);
var spreader_order1_response = await OrderUtilities.send_order(_order_service, order1_def, -0.30, 1);
Assert.IsTrue(spreader_order1_response.success, "Failed to send first spreader order");
var (spreader_order_is_working, working_spreader_order1) = await verify_order_status(spreader_order1_response.value.OrderKey, OrderStatus.Working);
Assert.IsTrue(spreader_order_is_working, "Spreader order is not Working");
var (quoting_order_is_working, working_quoting_order) = await verify_child_order_status(spreader_order1_response.value.OrderKey, OrderStatus.Working);
Assert.IsTrue(quoting_order_is_working, "Order is not Working");
Assert.IsTrue(working_quoting_order.Value.State.Price == 18.6, "Quoting order has the wrong price");
Assert.IsTrue(await OrderUtilities.cancel_orders(_order_service, new List<OrderRecordImmutable> {working_spreader_order1.Value}), "Failed to cancel order");
var (quoting_order_is_canceled, canceled_quoting_order) = await verify_child_order_status(spreader_order1_response.value.OrderKey, OrderStatus.Canceled);
Assert.IsTrue(quoting_order_is_canceled, "Quoting order may not have been canceled");
// Spreader Order 2
var spreader_order2_response = await OrderUtilities.send_order(_order_service, order1_def, -0.30, 1);
Assert.IsTrue(spreader_order2_response.success, "Failed to send spreader order 2");
var (spreader_order2_is_working, working_spreader_order2) = await verify_order_status(spreader_order2_response.value.OrderKey, OrderStatus.Working);
Assert.IsTrue(spreader_order2_is_working, "Spreader order 2 is not 'Working'");
var spreader_order2_change_response = await OrderUtilities.modify_order(_order_service, working_spreader_order2.Value, -0.6);
Assert.IsTrue(spreader_order2_change_response.success, "failed to modify spreader order");
Assert.IsTrue(await verify_order_price(spreader_order2_response.value.OrderKey, -0.6), "Failed to change price of spreader order");
// verify quoting order price
Assert.IsTrue(await verify_child_order_price(spreader_order2_response.value.OrderKey, 18.9), "Quoting order incorrect or not found");
//fill spreader order
var spreader_order2_fill_change_response = await OrderUtilities.modify_order(_order_service, working_spreader_order2.Value, -0.2);
Assert.IsTrue(spreader_order2_fill_change_response.success, "failed to modify spreader order");
Assert.IsTrue(await verify_order_price(spreader_order2_response.value.OrderKey, -0.2), "Failed to change price of spreader order");
// verify quoting order price
Assert.IsTrue(await verify_child_order_price(spreader_order2_response.value.OrderKey, 18.5), "Quoting order incorrect or not found");
// verify Hedging order price
Assert.IsTrue(await verify_child_order_price(spreader_order2_response.value.OrderKey, 18.3), "Hedging order incorrect or not found");
}
private async Task<(Boolean, OrderRecordImmutable?)> verify_child_order_status(uint parent_order_key, OrderStatus expected_status) {
var order = await get_order(ordi => ordi.ParentKey == parent_order_key && ordi.State.Status == expected_status);
return (order.HasValue && order.Value.State.Status == expected_status, order);
}
private async Task<(Boolean, OrderRecordImmutable?)> verify_order_status(uint order_key, OrderStatus expected_status) {
var order = await get_order(ordi => ordi.Key == order_key);
return (order.HasValue && order.Value.State.Status == expected_status, order);
}
private async Task<Boolean> verify_order_price(uint order_key, double expected_price, double precision = 10) {
var order = await get_order(ordi => ordi.Key == order_key && Math.Truncate(ordi.State.Price * precision) == precision * expected_price);
return order.HasValue && (Math.Truncate(order.Value.State.Price * precision) == precision * expected_price);
}
private async Task<Boolean> verify_child_order_price(uint parent_order_key, double expected_price, double precision = 10.0)
{
var order = await get_order(ordi => ordi.ParentKey == parent_order_key && Math.Truncate(ordi.State.Price * precision) == precision * expected_price);
return order.HasValue && (Math.Truncate(order.Value.State.Price * precision) == precision * expected_price);
}
private async Task<OrderRecordImmutable?> get_order(Func<OrderRecordImmutable, bool> predicate)
{
var orders = await OrderUtilities.wait_for_filtered_orders(_order_service,
o => o.Where(predicate).Count() > 0,
ord => ord.Where(predicate),
TimeSpan.FromSeconds(30));
return orders.result.TryFirst();
}
}
}
-59
View File
@@ -1,59 +0,0 @@
using bts.api.admin;
using bts.api.core;
using bts.api.orderman;
using bts.co2;
using bts.test;
using mbts;
using System;
using System.Collections.Generic;
using System.Text;
namespace bts.tests {
public abstract class TestBase : IDisposable {
protected (string name, uint id) _test_account = ("ACC A", 0u);
protected (string name, uint id) _test_user = ("bts", 0u);
protected ServiceContext _context;
protected AdminService _admin_service;
protected OrderService _order_service;
protected MarketDataRelay _md_relay;
protected List<IDisposable> _resources;
public TestBase() {
_context = new ServiceContext();
_admin_service = _context.locate<AdminService>();
_order_service = _context.locate<OrderService>();
_md_relay = _context.locate<MarketDataRelay>();
_resources = new List<IDisposable> {
_context,
_admin_service,
_order_service,
_md_relay
};
var auth = _context.login("bts", "");
auth.Wait(3000);
// get the ACC A account
var account_data = AdminUtilities.get_account_data(_admin_service, _test_account.name);
account_data.Wait(3000);
Assert.IsTrue(account_data.Result.HasValue);
_test_account.id = account_data.Result.Value.Id;
// get the bts user
var user_data = AdminUtilities.get_user_data(_admin_service, _test_user.name);
user_data.Wait(3000);
Assert.IsTrue(user_data.Result.HasValue);
_test_user.id = user_data.Result.Value.Id;
}
public void Dispose() {
// seg faults on linux during this dispose, not sure why yet
if (System.Environment.OSVersion.Platform == PlatformID.Win32NT) {
Disposable.dispose(_resources);
}
}
}
}
@@ -1,230 +0,0 @@
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
{
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 async Task<BTSUserImmutable?> get_user_data (AdminService admin_service, string user_name)
{
Update<AdminDataImmutable, int> update = await admin_service.subscribei_admin_data().FirstAsync();
foreach (var usr in update.all.First().Users) {
if(usr.Name == user_name) {
return usr;
}
}
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> clear_all_limits(AdminService admin_service, string account_name) {
var account_data = await get_account_data(admin_service, account_name);
return await clear_all_limits(admin_service, account_data);
}
public static async Task<bool> clear_all_limits(AdminService admin_service, BTSAccountImmutable? account_data) {
var new_account_data = account_data.Value.with(security_clip_limits: new AccountSecurityClipLimits(), product_position_limits: new List<AccountProductPositionLimitUnit>());
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;
}
}
public static async Task<AccountProductPositionLimitUnitImmutable?> get_position_limit_row(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_position_limit_row(account_data, symbol, security_type, venue);
}
public static AccountProductPositionLimitUnitImmutable? get_position_limit_row(BTSAccountImmutable? account_data, string symbol, SecurityType security_type = SecurityType.Future, Venue venue = Venue.Sim) {
IEnumerable<AccountProductPositionLimitUnitImmutable> limit_rows = account_data.Value.ProductPositionLimits.Where(
r => r.Product.SecuritySymbol == symbol
&& r.Product.Exchange == venue
&& r.Product.SecurityType == security_type);
return limit_rows.Any() ? limit_rows.First() : (AccountProductPositionLimitUnitImmutable?) null;
}
public static async Task<bool> remove_position_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_position_limit(admin_service, account_data, symbol, security_type, venue);
}
public static async Task<bool> remove_position_limit(AdminService admin_service, BTSAccountImmutable? account_data, string symbol, SecurityType security_type = SecurityType.Future, Venue venue = Venue.Sim) {
AccountProductPositionLimitUnitImmutable? origin_limit_row = get_position_limit_row(account_data,symbol,security_type,venue);
List<AccountProductPositionLimitUnitImmutable> limits_list = account_data.Value.ProductPositionLimits.ToList();
if(origin_limit_row.HasValue) {
limits_list.Remove(origin_limit_row.Value);
List<AccountProductPositionLimitUnit> temp_plu = new List<AccountProductPositionLimitUnit>();
temp_plu.AddRange(limits_list.Select(x => x.CloneToMutable()));
var new_account_data = account_data.Value.with(product_position_limits: temp_plu);
var update_ret = await admin_service.update_account_async(new AccountUpdateAPIArgument {
CompleteData = new_account_data.CloneToMutable()
});
return update_ret.success;
}
else
{
return false;
}
}
public static async Task<bool> set_position_limit(AdminService admin_service, string account_name, string symbol, uint long_limit, uint short_limit, SecurityType security_type = SecurityType.Future, Venue venue = Venue.Sim) {
var account_data = await get_account_data(admin_service, account_name);
return await set_position_limit(admin_service, account_data, symbol, long_limit, short_limit, security_type, venue);
}
public static async Task<bool> set_position_limit(AdminService admin_service, BTSAccountImmutable? account_data, string symbol, uint long_limit, uint short_limit, SecurityType security_type = SecurityType.Future, Venue venue = Venue.Sim) {
AccountProductPositionLimitUnitImmutable? limit_row = get_position_limit_row(account_data,symbol,security_type,venue);
List<AccountProductPositionLimitUnitImmutable> limits_list = account_data.Value.ProductPositionLimits.ToList();
if(limit_row.HasValue) {
// update the row
limits_list.Remove(limit_row.Value);
limit_row = limit_row.Value.with(long_position_limit: long_limit, short_position_limit: short_limit);
}
else {
// create a new row
AccountProductPositionLimitUnit acplu = new AccountProductPositionLimitUnit();
acplu.LongPositionLimit = long_limit;
acplu.ShortPositionLimit = short_limit;
acplu.Product = new ProductScope {
SecuritySymbol = symbol,
SecurityType = security_type,
Exchange = venue
};
limit_row = acplu;
}
limits_list.Add(limit_row.Value);
List<AccountProductPositionLimitUnit> temp_plu = new List<AccountProductPositionLimitUnit>();
temp_plu.AddRange(limits_list.Select(x => x.CloneToMutable()));
var new_account_data = account_data.Value.with(product_position_limits: temp_plu);
var update_ret = await admin_service.update_account_async(new AccountUpdateAPIArgument {
CompleteData = new_account_data.CloneToMutable()
});
return update_ret.success;
}
}
}
@@ -1,74 +0,0 @@
using bts.api;
using bts.co2;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace bts.tests {
class MarketDataUtilities {
const string _MD_HOST = "system_test";
public static async Task<SecurityImmutable?> get_security(MarketDataRelay relay, string symbol, SecurityType security_type, Func<ChannelUpdate, bool> match, MDRequest.Types.Flags flags = MDRequest.Types.Flags.Bbo, Venue v = Venue.Sim) {
var request = new MDRequest() {
Host = _MD_HOST,
Channels = {
new SecurityChannel() {
Time = 0,
Flags = (uint)flags,
Type = security_type,
Exchange = v,
Symbol = symbol,
TradeCursor = 0,
RfqCursor = 0
}
}
};
var result = await relay.requestmd_async(request);
if (result.success) {
foreach (var ch in result.value.Channels) {
foreach (var update in ch.Updates) {
if (match(update)) {
return new SecurityImmutable(update.Security);
}
}
}
}
return null;
}
public static async Task<IEnumerable<FutureContract>> get_expirations(MarketDataRelay relay, string symbol, SecurityType security_type, MDRequest.Types.Flags flags = MDRequest.Types.Flags.Bbo, Venue v = Venue.Sim) {
var request = new MDRequest() {
Host = _MD_HOST,
Channels = {
new SecurityChannel() {
Time = 0,
Flags = (uint)flags,
Type = security_type,
Exchange = v,
Symbol = symbol,
TradeCursor = 0,
RfqCursor = 0
}
}
};
var result = await relay.requestmd_async(request);
var futures = new List<FutureContract>();
if (result.success) {
foreach (var ch in result.value.Channels) {
foreach (var update in ch.Updates) {
futures.Add(update.Security.Future);
}
}
}
return (IEnumerable<FutureContract>) futures;
}
}
}
@@ -1,200 +0,0 @@
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;
using bts.tests.utils;
using bts.spark;
namespace bts.tests
{
public class OrderUtilities {
public static async Task<Result<Response>> send_order(OrderService order_service, OrderDefinition order_def, double price, uint size){
return await order_service.create_order_async(new Create() {
NewOrder = order_def,
Price = price,
Quantity = size
});
}
public static async Task<Result<Response>> modify_order(OrderService order_service, OrderRecordImmutable o, uint size) {
return await modify_order(order_service, o.Key, o.State.Price, size);
}
public static async Task<Result<Response>> modify_order(OrderService order_service, OrderRecordImmutable o, double price) {
return await modify_order(order_service, o.Key, price, o.State.Quantity);
}
public static async Task<Result<Response>> modify_order(OrderService order_service, OrderRecordImmutable o, double price, uint size) {
return await modify_order(order_service, o.Key, price, size);
}
private static async Task<Result<Response>> modify_order(OrderService order_service, uint order_key, double price, uint size) {
return await order_service.update_order_async(new Update() {
OrderKey = order_key,
Price = price,
Quantity = size
});
}
public static async Task<bool> cancel_orders(OrderService order_service, IEnumerable<OrderRecordImmutable> orders, uint key = 0){
if (key > 0) {
Result<Response> resp = await order_service.cancel_order_async(new Cancel() { OrderKey = key });
return resp.success;
}
else {
bool suc = true;
var working_orders = orders.Where(x => x.State.Status == OrderStatus.Working);
if(working_orders != null){
foreach (var o in working_orders){
Result<Response> resp = await order_service.cancel_order_async(new Cancel() { OrderKey = o.Key });
suc &= resp.success;
}
return suc;
}
return true;// there are no working orders... so true enough
}
}
public async static Task<utils.TestUtils.TimeoutResult<IEnumerable<OrderRecordImmutable>>>
wait_for_filtered_orders(OrderService order_service, Func<IEnumerable<OrderRecordImmutable>, bool> match, Func<IEnumerable<OrderRecordImmutable>, IEnumerable<OrderRecordImmutable>> selector, TimeSpan? timeout = null) {
if (timeout == null) {
timeout = TimeSpan.FromSeconds(3);
}
var tcs = new TaskCompletionSource<IEnumerable<OrderRecordImmutable>>();
var sub = order_service.subscribei_orders().Subscribe(update => {
if (match(update.all)) {
tcs.TrySetResult(selector(update.all));
}
});
var tr = await tcs.Task.timeout(timeout.Value);
sub.Dispose();
return tr;
}
public async static Task<utils.TestUtils.TimeoutResult<IEnumerable<FillImmutable>>>
wait_for_filtered_fills(OrderService order_service, Func<IEnumerable<FillImmutable>, bool> match, Func<IEnumerable<FillImmutable>, IEnumerable<FillImmutable>> selector, TimeSpan? timeout = null) {
if (timeout == null) {
timeout = TimeSpan.FromSeconds(3);
}
var tcs = new TaskCompletionSource<IEnumerable<FillImmutable>>();
var sub = order_service.subscribei_fills().Subscribe(update => {
if (match(update.all)) {
tcs.TrySetResult(selector(update.all));
}
});
var tr = await tcs.Task.timeout(timeout.Value);
sub.Dispose();
return tr;
}
public async static Task<IEnumerable<OrderRecordImmutable>> create_book(OrderService order_service, (OrderDefinition order_def, double price, uint size)[] order_defs) {
List<Result<Response>> results = new List<Result<Response>>();
foreach(var ord in order_defs) {
Assert.IsTrue(ord.order_def != null, "Order definition can not be null");
results.Add(await send_order(order_service, ord.order_def, ord.price, ord.size));
}
Func<IEnumerable<OrderRecordImmutable>, IEnumerable<OrderRecordImmutable>> selector = existing_orders => {
return existing_orders.Where(z => results.Exists(y => z.Key == y.value.OrderKey));
};
Func<IEnumerable<OrderRecordImmutable>, bool> match = existing_orders => {
return selector(existing_orders).Count() > 0;
};
await Task.Delay(5000);// I'm not sure how to get around this
utils.TestUtils.TimeoutResult<IEnumerable<OrderRecordImmutable>> filtered_orders = await wait_for_filtered_orders(order_service, match, selector, TimeSpan.FromSeconds(30));
Assert.IsNotNull(filtered_orders.result, "Book is incomplete");
Assert.IsTrue(filtered_orders.result.Count() == order_defs.Length, "Book is incomplete");
return filtered_orders.result;
}
}
/// <summary>
/// We should replace with something like the OrderDataHeler from the Spark project.
/// </summary>
public static class OrderDefinitionUtilities {
private static OrderDefinition create_base_order(uint security_id, Side side, (String name, uint id) account, (String name, uint id) user) {
return new OrderDefinition {
SecurityKey = security_id,
Side = side,
TimeInForce = TimeInForce.Day,
CtiCode = CTICode.CtiCode1,
CustomerOrFirm = CustomerOrFirm.Firm,
ManualOrderIndicator = ManualOrderIndicator.Yes,
Account = account.name,
AccountId = account.id,
User = user.name,
UserId = user.id
};
}
public static OrderDefinition create_limit_order_def(uint security_id, Side side, (String name, uint id) account, (String name, uint id) user) {
var def = create_base_order(security_id, side, account, user);
def.CustomDef = new CustomOrderDefinition { Limit = new LimitOrderDefinition { } };
return def;
}
public static OrderDefinition create_spreader_order_def(Security security, Side side, ExecutionStrategy strategy, (String name, uint id) account, (String name, uint id) user) {
var def = create_base_order(security.Id, side, account, user);
def.CustomDef = new CustomOrderDefinition { Spreader = new SpreaderOrderDefinition {Security = security, ExecutionStrategy = strategy} };
return def;
}
public static OrderDefinition create_iceberg_order_def(uint security_id, Side side, (String name, uint id) account, (String name, uint id) user, double show_quantity) {
var def = create_base_order(security_id, side, account, user);
def.CustomDef = new CustomOrderDefinition {
Iceberg = new IcebergOrderDefinition {
ShowQuantity = show_quantity,
Child = new CustomOrderDefinition {
Limit = new LimitOrderDefinition { }
}
}
};
return def;
}
public static OrderDefinition create_join_order_definition(uint security_id, Side side, (String name, uint id) account, (String name, uint id) user, spark.QuantityMeasure join_threshold) {
var def = create_base_order(security_id, side, account, user);
def.CustomDef = new CustomOrderDefinition {
Join = new JoinOrderDefinition {
JoinThreshold = join_threshold,
Child = new CustomOrderDefinition {
Limit = new LimitOrderDefinition { }
}
}
};
return def;
}
public static OrderDefinition create_lean_order_definition(uint security_id, Side side, (String name, uint id) account, (String name, uint id) user, spark.QuantityMeasure lean_threshold, uint max_chase_ticks = 2, LeanOrderDefinition.Types.OnToward ontoward_behavor = LeanOrderDefinition.Types.OnToward.NoChange) {
var def = create_base_order(security_id, side, account, user);
def.CustomDef = new CustomOrderDefinition {
Lean = new LeanOrderDefinition {
CrossThreshold = lean_threshold,
MaxChaseTicks = max_chase_ticks,
TradeTowardBehavior = ontoward_behavor,
Child = new CustomOrderDefinition {
Limit = new LimitOrderDefinition { }
}
}
};
return def;
}
}
}
@@ -1,180 +0,0 @@
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;
using bts.tests.utils;
using bts.api.synthetic;
using bts.spark;
namespace bts.tests
{
public class SpreaderUtilities {
public static async Task<(SecurityImmutable s1, SecurityImmutable s2)> get_securities_for_common_setup_one(MarketDataRelay relay, string symbol) {
var exps = (await MarketDataUtilities.get_expirations(relay, symbol, SecurityType.Future)).ToList();
Assert.IsNotNull(exps);
Assert.IsTrue(exps.Count() > 2, "Need at least two expirations");
var exp1 = exps[0];
var exp2 = exps[1];
Func<ChannelUpdate, bool> sec1_match = ch =>
ch.Security.Future.ExpirationYear == exp1.ExpirationYear &&
ch.Security.Future.ExpirationMonth == exp1.ExpirationMonth &&
ch.Security.Future.ExpirationDay == exp1.ExpirationDay;
Func<ChannelUpdate, bool> sec2_match = ch =>
ch.Security.Future.ExpirationYear == exp2.ExpirationYear &&
ch.Security.Future.ExpirationMonth == exp2.ExpirationMonth &&
ch.Security.Future.ExpirationDay == exp2.ExpirationDay;
var sec_task1 = MarketDataUtilities.get_security(relay, symbol, SecurityType.Future, sec1_match);
var sec_task2 = MarketDataUtilities.get_security(relay, symbol, SecurityType.Future, sec2_match);
var sec1 = await sec_task1;
var sec2 = await sec_task2;
Assert.IsFalse(sec1 == null, "security is null");
Assert.IsFalse(sec2 == null, "security is null");
return (s1: sec1.Value, s2: sec2.Value);
}
public static async Task<(IEnumerable<OrderRecordImmutable> book, (Security security, ExecutionStrategy strategy) synthetic)> common_setup_one(OrderService order_service, SyntheticSecurityService synthetic_service, (SecurityImmutable? s1, SecurityImmutable? s2) securities, (String name, uint id) account, (String name, uint id) user) {
var order_defs = new (OrderDefinition order_def, double price, uint size)[10];
for (int i = 0; i < 4; i ++) {
double p = (0.01 * i) + 18.230;
order_defs[i] = (
order_def: OrderDefinitionUtilities.create_limit_order_def(securities.s1.Value.Id, Side.Buy, account, user),
price: p,
size: 1
);
}
for (int i = 4; i < 8; i ++) {
double p = (0.01 * (i - 4)) + 18.300;
order_defs[i] = (
order_def: OrderDefinitionUtilities.create_limit_order_def(securities.s1.Value.Id, Side.Sell, account, user),
price: p,
size: 1
);
}
order_defs[8] = (
order_def: OrderDefinitionUtilities.create_limit_order_def(securities.s2.Value.Id, Side.Buy, account, user),
price: 18.500,
size: 5
);
order_defs[9] = (
order_def: OrderDefinitionUtilities.create_limit_order_def(securities.s2.Value.Id, Side.Sell, account, user),
price: 18.53,
size: 5
);
var book = await OrderUtilities.create_book(order_service, order_defs);
List<(ContractLeg contract, LegSpreaderParams parameters)> legs = new List<(ContractLeg, LegSpreaderParams)>();
legs.Add(create_leg(securities.s1.Value.CloneToMutable(),1,1,false,1));
legs.Add(create_leg(securities.s2.Value.CloneToMutable(),-1,-1,true,1));
(Security security, ExecutionStrategy strategy) synthetic = await create_synthetic(synthetic_service, legs);
return(book, synthetic);
}
public static async Task<(Security, ExecutionStrategy)> create_synthetic(SyntheticSecurityService service, IEnumerable<(ContractLeg contract, LegSpreaderParams parameters)> legs) {
var new_synth = new Security(){};
new_synth.Synthetic = new SyntheticContract();
new_synth.Synthetic.Legs.Add(legs.Select(l => l.contract));
new_synth.Symbol = deduce_symbol(new_synth);
new_synth.ContractSpec = new ContractSpecification();
new_synth.ContractSpec.LowTick = 0.01;
new_synth.ContractSpec.HighTick = 0.01;
new_synth.ContractSpec.PriceBreak = double.MaxValue;
new_synth.ContractSpec.MainFraction = 1u;
new_synth.ContractSpec.PriceDisplayFormat = 3u;
new_synth.Exchange = Venue.Bts;
new_synth.Synthetic.Name = "TEST_SYNTH" + DateTime.Now.ToString();
var result = await service.create_synthetic_async(new_synth);
Assert.IsTrue(result.success, "Unable to create synthetic");
Assert.IsTrue(result.value.Error == CreateResult.Types.Error.Ok, "Create synthetic was not OK");
uint? id = result.value.Key;
Assert.IsTrue(id.HasValue && id.Value != 0, "Failed to get synthetic security key");
new_synth.Id = result.value.Key;
ExecutionStrategy strategy = new ExecutionStrategy();
foreach(var leg in legs){
strategy.LegParamsMap.Add(leg.contract.Security.Id, leg.parameters);
}
bool success = await save_strategy(service, new_synth.Id, new ExecutionStrategyImmutable(strategy));
Assert.IsTrue(success, "Failed to save synthetic strategy");
return (new_synth, strategy);
}
public static (ContractLeg contract, LegSpreaderParams parameters) create_leg(
Security security,
double quantity,
double price_multiplier,
bool is_quoting,
double lean_qty_factor,
LegSpreaderParams.Types.MarketDataType market_data_type = 0
)
{
var contract = new ContractLeg();
contract.Security = security;
contract.PriceMultiplier = price_multiplier;
contract.Side = quantity > 0 ? Side.Buy : Side.Sell;
contract.Quantity = Math.Abs(quantity);
var parameters = new LegSpreaderParams();
parameters.IsActiveQuoting = is_quoting;
parameters.MarketDataType = market_data_type; // 0 == Merged, 1 == Direct
parameters.EnableHedging = !is_quoting;
parameters.PostHedgeParams = new PostHedgeParams();
parameters.PreHedgeParams = new PreHedgeParams();
parameters.QuoteParams = new QuoteParams();
parameters.QuoteParams.LeanQuantityRatio = lean_qty_factor;
parameters.QuoteParams.QuoteMuteZone = null; // == Throttle unchecked
return (contract: contract, parameters: parameters);
}
public static string deduce_symbol(SecurityImmutable? security) {
if (!string.IsNullOrWhiteSpace(security?.Symbol)) return security?.Symbol;
if (security?.Synthetic.Legs.Count() > 0) return security?.Synthetic.Legs.First().Security.Symbol;
return string.Empty;
}
public static async Task<uint?> create_synthetic(SyntheticSecurityService service, SecurityImmutable? synthetic) {
var result = await service.create_synthetic_async(synthetic.Value);
Assert.IsTrue(result.success, "failed to create synthetic");
return result.value.Key;
}
public static async Task<bool> save_strategy(SyntheticSecurityService service, uint synthetic_id, ExecutionStrategyImmutable strategy) {
var result = await service.update_security_metadata_async(new Metadata { SecurityKey = synthetic_id, Strategy = strategy.CloneToMutable() });
return result.success;
}
}
}
+2 -102
View File
@@ -1,16 +1,10 @@
using bts.api;
using bts.api.admin;
using bts.api.core;
using bts.api.orderman;
using bts.collections;
using bts.collections;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;
using System.Threading.Tasks;
namespace bts.tests.utils {
public static class TestUtils {
static class TestUtils {
public static List<T> randomize_values<T>(IReadOnlyList<T> input) {
var remaining_indices = Enumerable.Range(0, input.Count).ToList();
var rand = new Random();
@@ -44,99 +38,5 @@ namespace bts.tests.utils {
}
return reshuffled;
}
public static bool test_price_equality(double price_1, double price_2, double tick_size) {
return Math.Abs(price_2 - price_1) < 0.01 * tick_size;
}
// this is a copy from TickUtils -- ask what's legal for the spark_test project...
public static double ticksize(this ContractSpecificationImmutable spec, double price) =>
Math.Abs(price) > spec.PriceBreak ? spec.HighTick : spec.LowTick;
// this was copied from GlobalExtensions
public struct TimeoutResult<T> {
public TimeoutResult(T result, TimeSpan ts) {
wait_duration = ts;
this.result = result;
}
public readonly TimeSpan wait_duration;
public readonly T result;
}
// this was copied from GlobalExtensions
public static async Task<TimeoutResult<T>> timeout<T>(this Task<T> task, TimeSpan timeout) {
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
var timeout_task = Task.Delay(timeout);
var completed = await Task.WhenAny(task, timeout_task);
return (completed == timeout_task) ?
new TimeoutResult<T>(default(T), timeout) :
new TimeoutResult<T>(task.Result, sw.Elapsed);
}
public static async Task<T> await_with_timeout<T>(Task<T> task, TimeSpan timeout){
var to = timeout<T>(task, timeout);
var tr = await to;
return tr.result;
}
public static async Task<bool> test_for_system_ready(ServiceContext context, TimeSpan? timeout = null) {
if(timeout == null) {
timeout = TimeSpan.FromSeconds(30);
}
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
bool admin_ready = await test_for_admin_service_ready(context, timeout.Value.Subtract(sw.Elapsed));
Console.WriteLine("Admin Service Ready: " + admin_ready);
bool md_ready = await test_for_md_service_ready(context, timeout.Value.Subtract(sw.Elapsed));
Console.WriteLine("MD Service Ready: " + md_ready);
return admin_ready && md_ready;
}
public static async Task<bool> test_for_admin_service_ready(ServiceContext context, TimeSpan timeout) {
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
var admin_service = context.locate<AdminService>();
Update<AdminDataImmutable, int> admin_value = null;
var account_sub = admin_service.subscribei_admin_data().Subscribe(update => admin_value = update);
while(admin_value == null && sw.Elapsed < timeout) {
Console.WriteLine("Waiting for admin service...");
await Task.Delay(100);
}
return admin_value != null;
}
public static async Task<bool> test_for_md_service_ready(ServiceContext context, TimeSpan timeout) {
var md_request = new co2.MDRequest() {
Host = "system_test",
Channels = {
new co2.SecurityChannel() {
Time = 0,
Flags = 0,
Type = SecurityType.Future,
Exchange = Venue.Sim,
Symbol = "*",
TradeCursor = 0,
RfqCursor = 0
}
}
};
var md_relay = context.locate<co2.MarketDataRelay>();
Result<co2.MDReply>? md_result = await await_with_timeout(
md_relay.requestmd_async(md_request),
timeout);
return md_result != null;
}
}
}
-4
View File
@@ -11,8 +11,4 @@
<ProjectReference Include="..\spark_core\spark_core.csproj" />
</ItemGroup>
<ItemGroup>
<Content Include="../spark_test/system_test_scripts/*.sh" CopyToOutputDirectory="PreserveNewest"></Content>
</ItemGroup>
</Project>
@@ -1,54 +0,0 @@
#!/bin/bash
if [ -d ~/sparkdata/ ]
then
sparkdata_dir=~/sparkdata
else
sparkdata_dir=/sparkdata
fi
config=$sparkdata_dir/conf/spark.conf
bak=$sparkdata_dir/conf/spark.conf.bak
rm -r $sparkdata_dir/data/databases/ 2>/dev/null
[ ! -d "$sparkdata_dir/conf" ] && mkdir $sparkdata_dir/conf/
if [ -f "$conf" ];then
mv $conf $bak 2>/dev/null
fi
cat << EOF > $sparkdata_dir/conf/spark.conf
ExchangeSim {
value {
merge_with_md:false
}
}
Logging {
value {
console { default_level: "Error" }
syslog { default_level: "Error" }
file { default_level: "Error" }
}
}
EOF
nohup ./sparkd run &
pid=$!
echo "sparkd pid: " $pid
echo "sleeping for 120 seconds..."
sleep 120
dotnet sparkcmd.dll test spreader_simple_sanity
exit_status=$?
pkill -P $$
if [ -f "$bak" ];then
mv $bak $conf
fi
[ $exit_status -eq 0 ] && echo "Test Finished" || echo "Test failed"
exit $exit_status
@@ -1,54 +0,0 @@
#!/bin/bash
if [ -d ~/sparkdata/ ]
then
sparkdata_dir=~/sparkdata
else
sparkdata_dir=/sparkdata
fi
config=$sparkdata_dir/conf/spark.conf
bak=$sparkdata_dir/conf/spark.conf.bak
rm -r $sparkdata_dir/data/databases/ 2>/dev/null
[ ! -d "$sparkdata_dir/conf" ] && mkdir $sparkdata_dir/conf/
if [ -f "$conf" ];then
mv $conf $bak 2>/dev/null
fi
cat << EOF > $sparkdata_dir/conf/spark.conf
ExchangeSim {
value {
merge_with_md:true
}
}
Logging {
value {
console { default_level: "Error" }
syslog { default_level: "Error" }
file { default_level: "Error" }
}
}
EOF
nohup ./sparkd run &
pid=$!
echo "sparkd pid: " $pid
echo "sleeping for 120 seconds..."
sleep 120
dotnet sparkcmd.dll test test_clip_limits
exit_status=$?
pkill -P $$
if [ -f "$bak" ];then
mv $bak $conf
fi
[ $exit_status -eq 0 ] && echo "Test Finished" || echo "Test failed"
exit $exit_status
@@ -1,160 +0,0 @@
#!/usr/bin/env bash
# Stop script on NZEC
#set -e # the will allow sparkd to keep running if a test fails, preventing Jenkins from continuing
# Stop script if unbound variable found (use ${var:-} if intentional)
set -u
if [ -d ~/sparkdata/ ]
then
sparkdata_dir=~/sparkdata
else
sparkdata_dir=/sparkdata
fi
# Use in the the functions: eval $invocation
invocation='say_verbose "Calling: ${yellow:-}${FUNCNAME[0]} ${green:-}$*${normal:-}"'
# standard output may be used as a return value in the functions
# we need a way to write text on the screen in the functions so that
# it won't interfere with the return value.
# Exposing stream 3 as a pipe to standard output of the script itself
exec 3>&1
# Setup some colors to use. These need to work in fairly limited shells, like the Ubuntu Docker container where there are only 8 colors.
# See if stdout is a terminal
if [ -t 1 ] && command -v tput > /dev/null; then
# see if it supports colors
ncolors=$(tput colors)
if [ -n "$ncolors" ] && [ $ncolors -ge 8 ]; then
bold="$(tput bold || echo)"
normal="$(tput sgr0 || echo)"
black="$(tput setaf 0 || echo)"
red="$(tput setaf 1 || echo)"
green="$(tput setaf 2 || echo)"
yellow="$(tput setaf 3 || echo)"
blue="$(tput setaf 4 || echo)"
magenta="$(tput setaf 5 || echo)"
cyan="$(tput setaf 6 || echo)"
white="$(tput setaf 7 || echo)"
fi
fi
say_warning() {
printf "%b\n" "${yellow:-}$test_script_name: Warning: $1${normal:-}"
}
say_err() {
printf "%b\n" "${red:-}$test_script_name: Error: $1${normal:-}" >&2
}
say() {
# using stream 3 (defined in the beginning) to not interfere with stdout of functions
# which may be used as return value
printf "%b\n" "${cyan:-}$test_script_name:${normal:-} $1" >&3
}
say_verbose() {
if [ "$verbose" = true ]; then
say "$1"
fi
}
is_merge_with_md_on() {
eval $invocation
if [ $(./sparkd configcheck ExchangeSim | grep merge_with_md: | cut -d " " -f 2) == "false" ]; then
return 0
else
return 1
fi
}
set_merge_with_md_off() {
eval $invocation
if [ -f $config_file_path ]; then
say_verbose "config file exists ... removing"
rm $config_file_path
fi
echo "$merge_with_md_key" >> $config_file_path && test_config_file_created=true
return 0
}
# args
# return_var - $1
# class_name - $2
get_tests_in_class() {
eval $invocation
local return_var=$1
local class_name=$2
local arr=($(dotnet sparkcmd.dll test --type System --list | perl -ne '/^( *)/; $i = length $1; $j && $i >= $j and print, next; $j = 0; /'"$class_name"'/ and $j = $i + 1, print' | tail -n +2))
eval $return_var='("${arr[@]}")'
return 0
}
# Start sparkd
nohup ./sparkd run &
pid=$!
echo "sparkd pid: " $pid
echo "sleeping for 120 seconds..."
sleep 120
# args
# class_name - $1
run_bts_test() {
eval $invocation
class_name=$1
local test_names_tuple=()
get_tests_in_class test_names_tuple $class_name
for test_name in ${test_names_tuple[@]}; do
say_verbose "running test with name: $test_name ...";
dotnet sparkcmd.dll test $test_name
exit_status=$?
done
return 0
}
test_script_name=$(basename "$0")
verbose=true
config_file_path="$sparkdata_dir/conf/test_join_algo_tests.conf"
test_config_file_created=false
merge_with_md_key=$(/bin/cat <<'END_HEREDOC'
ExchangeSim {
value {
merge_with_md: false
}
}
END_HEREDOC
)
if is_merge_with_md_on; then
set_merge_with_md_off
if is_merge_with_md_on; then
say_err "Could not turn off merge_with_md"
say_err "Please investigate manually"
exit 1
fi
else
say_verbose "merge with md is already off"
fi
run_bts_test "JoinAlgoTests"
if [ "$test_config_file_created" = true ]; then
rm $config_file_path
fi
# Kill sparkd
pkill -P $$
[ $exit_status -eq 0 ] && echo "Test Finished" || echo "Test failed"
exit $exit_status
@@ -1,53 +0,0 @@
#!/bin/bash
if [ -d ~/sparkdata/ ]
then
sparkdata_dir=~/sparkdata
else
sparkdata_dir=/sparkdata
fi
config=$sparkdata_dir/conf/spark.conf
bak=$sparkdata_dir/conf/spark.conf.bak
rm -r $sparkdata_dir/data/databases/ 2>/dev/null
[ ! -d "$sparkdata_dir/conf" ] && mkdir $sparkdata_dir/conf/
if [ -f "$conf" ];then
mv $conf $bak 2>/dev/null
fi
cat << EOF > $sparkdata_dir/conf/spark.conf
ExchangeSim {
value {
merge_with_md:true
}
}
Logging {
value {
console { default_level: "Error" }
syslog { default_level: "Error" }
file { default_level: "Error" }
}
}
EOF
nohup ./sparkd run &
pid=$!
echo "sparkd pid: " $pid
echo "sleeping for 120 seconds..."
sleep 120
dotnet sparkcmd.dll test test_position_limits
exit_status=$?
pkill -P $$
if [ -f "$bak" ];then
mv $bak $conf
fi
[ $exit_status -eq 0 ] && echo "Test Finished" || echo "Test failed"
exit $exit_status
-21
View File
@@ -269,16 +269,6 @@ public:
if (!_cold.is_initialized()) return true;
return is_heart_beat_ok(cold_header());
}
void dump_shm_to(const char* directory) const {
_cold.dump_to(directory);
if (_hot) {
_hot->dump_to(directory);
}
if (_hot_depth) {
_hot_depth->dump_to(directory);
}
}
private:
@@ -920,15 +910,6 @@ public:
return result;
}
void dump_shm_to(const char* p) const {
// To be safe we should lock _work_mutex -but-
// we need this function to diagnose problems happening in the on_security callback
// which holds the _work_mutex. Since anyone calling this function is going down
// anyway we as well live dangerously.
for (auto& inst : _instances) {
inst->dump_shm_to(p);
}
}
};
@@ -976,8 +957,6 @@ MDConsumerChannel MDConsumer::get_channel(const std::string& symbol, SecurityTyp
MDConsumerChannel MDConsumer::get_channel(const UserSymbol& u) const {
return _pimpl->get_channel(u.symbol(), u.type(), u.exchange());
}
void MDConsumer::dump_shm_to(const char* p) const { _pimpl->dump_shm_to(p); }
MDConsumerChannel::MDConsumerChannel(const shared_ptr<MDConsumerInst>& inst) : _pimpl(inst) {}
int64_t MDConsumerChannel::read_trades(google::protobuf::RepeatedPtrField<co2::TradeMessage>& trades, int64_t cursor) { return _pimpl->read_trades(trades, cursor); }
-2
View File
@@ -67,8 +67,6 @@ public:
}
uintptr_t id() const { return reinterpret_cast<uintptr_t>(_pimpl.get()); }
void dump_shm_to(const char* directory) const;
private:
//BTS_NO_COPY(MDConsumer); COPY OK
std::shared_ptr<MDConsumerImpl> _pimpl;
-6
View File
@@ -177,11 +177,5 @@ CAPI void post_on_scan_security_thread(just_action a) {
md::scan_securities_thread().post(a);
}
CAPI void dump_co2_memory_to_disk(MDConsumer* consumer, const char* path) {
if (consumer) {
consumer->dump_shm_to(path);
}
}
}} //namespace
#endif
+1 -3
View File
@@ -143,9 +143,7 @@ public:
LOG(md,Diag, "Added {} trades", ch.trade_log_size());
}
for (const auto& trade : ch.trade_log()) {
if (trade.key()) {
channel.add_trade(trade);
}
channel.add_trade(trade);
}
for (const auto& rfq : ch.rfq_log()) {
channel.add_rfq(rfq);
@@ -265,11 +265,12 @@ void LeanAlgo::_on_toward_me() {
// it returns true if it successfully crossed.
bool LeanAlgo::_try_cross(Price price) {
if(_within_chase_band(price)){
Quantity cumm_qty;
SpreaderUtils::find_qty_upto_price_level(me->curr_book,
Quantity level_qty, cumm_qty;
SpreaderUtils::find_qty_at_price_level(me->curr_book,
_opp_side(),
price,
me->tick_size,
level_qty,
cumm_qty);
Quantity trigger_quantity = _cross_threshold();
if(cumm_qty.value()<trigger_quantity.value()){
@@ -366,6 +366,22 @@ void SpreaderOrder::_send_hedge(size_t hedge_leg_index,
hedge_price,
hedge_qty);
}
void SpreaderOrder::_compute_nlegs_crossed_hedges(size_t filled_leg_index,
Price filled_price,
double filled_quantity){
SpreaderBookkeepingData& bk_data = _bookkeeping_data();
const LegPropertyData& q_leg_data =
bk_data.get_leg_data(filled_leg_index).leg_property_data();
const ISpreaderManager& spreader = *this;
SpreaderUtils::compute_nlegs_crossed_hedges(bk_data.side(),
bk_data.effective_price(),
bk_data.n_legs(),
q_leg_data,
filled_price,
filled_quantity,
spreader,
bk_data.nlegs_crossed_hedges());
}
void SpreaderOrder::_compute_hedge_price_quantity(size_t filled_leg_index,
Price fill_price,
double fill_quantity,
@@ -386,15 +402,13 @@ 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,
hedged_leg_data.leg_property_data(),
driver_data,
computed_hedge_price,
hedge_quantity);
hedge_quantity);
}
hedge_price = SpreaderUtils::apply_pre_hedge_parameters (hedged_leg_data.leg_property_data().leg_params().pre_hedge_params(),
hedged_leg_data.book(),
@@ -233,6 +233,13 @@ private:
void _send_hedge(size_t hedge_leg_index,
Price hedge_price,
Quantity hedge_qty);
// computes the hedges in the case of nlegs
// from crossing the hedge markets. If the
// crossing doesnt result in better hedge prices
// 0's are stored in the qty's in
void _compute_nlegs_crossed_hedges(size_t filled_leg_index,
Price filled_price,
double filled_quantity);
void _compute_hedge_price_quantity(size_t filled_leg_index,
Price filled_price,
double filled_quantity,
@@ -1,4 +1,5 @@
#include "SpreaderBookkeepingData.h"
#include "lib/os/os.h"
namespace bts{
namespace orderman{
@@ -18,12 +19,14 @@ SpreaderBookkeepingData::SpreaderBookkeepingData (
_inited_new (inited_new),
_n_legs(legs.size()),
_n_quoting_legs(_calc_n_quoting_legs(legs)),
_side(order_def.side()),
_order_def(order_def),
_reported_state(){
LOG(OrderMan, Info, "SpreaderBookkeepingData.cons started "
"key: {} n_legs: {}",
spreader_order_key.value(), _n_legs);
_leg_data.reserve(_n_legs);
_nlegs_crossed_hedges.reserve(_n_legs);
size_t i=0;
for (const auto& leg_property_data : legs){
_leg_data.emplace_back(spreader_pao_services,
@@ -33,6 +36,7 @@ _reported_state(){
quoter_ord_defs[i],
hedger_ord_defs[i],
leg_property_data);
_nlegs_crossed_hedges.emplace_back(std::pair<Price, double>(os::nan(),0.0));
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>(
@@ -24,6 +24,7 @@ private:
const bool _inited_new;
const size_t _n_legs;
const size_t _n_quoting_legs;
const api::Side _side;
// when the order is not created from the disk
// (ie created live) this is set to true
const api::orderman::OrderDefinition _order_def;
@@ -35,6 +36,10 @@ private:
Price _input_price;
Price _effective_price;
Quantity _quantity;
// this is used to pass in the calcuated hedge price/qty
// (preallocating so we dont have to allocate
// memory when calculating and calculating values)
std::vector<std::pair<Price,double>> _nlegs_crossed_hedges;
private:
size_t _calc_n_quoting_legs(const std::vector<LegPropertyData>& legs);
public:
@@ -61,10 +66,14 @@ public:
/////////////////////////////////
size_t n_legs() const { return _n_legs; }
bool all_legs_resolved() const {return n_legs() > 0;}
api::Side side() const {return _side;}
const api::orderman::OrderDefinition& order_def() const {return _order_def;}
Price input_price () const {return _input_price;}
Price effective_price () const {return _effective_price;}
Quantity quantity () const {return _quantity;}
std::vector<std::pair<Price,double>>& nlegs_crossed_hedges(){
return _nlegs_crossed_hedges;
}
// gets leg data from leg's sec key
SpreaderLegData& get_leg_data (SecurityKey security_key){
boost::container::flat_map<uint32_t, size_t>::const_iterator l_it = _sec_key_to_index.find(security_key.value());
@@ -121,7 +121,8 @@ public:
void static traverse_depth_for_quantity(const orderman::Book& leg_book,
const api::Side book_side,
Quantity target_quantity,
Price& out_price, Quantity& out_quantity){
Price& out_price,
Quantity& out_quantity){
uint32_t cum_quantity_u = 0;
const size_t n_levels = leg_book.n_levels();
// we could have kept a running price, updated at each level,
@@ -239,18 +240,19 @@ public:
// cum qty upto that price.
// returns false if the target_price is not found.
// this is used to find the qty and cumm_qty
static bool find_qty_upto_price_level (const Book& book,
static bool find_qty_at_price_level (const Book& book,
api::Side book_side,
Price target_price,
double,
Quantity& price_qty,
Quantity& cumm_qty){
cumm_qty = Quantity(0);
price_qty = Quantity(0); cumm_qty = Quantity(0);
const size_t n_levels = book.n_levels();
const bts::orderman::BookSide& book_depth = book.side(book_side);
for (size_t level = 0; level < n_levels; level++){
const md::PriceSize& level_price_qty = book_depth[level];
double level_price = level_price_qty.price();
auto price_qty = Quantity(level_price_qty.size());
price_qty = Quantity(level_price_qty.size());
if(securities::is_better(book_side,target_price,Price(level_price)) ||
price_qty.value()==0)
break;
@@ -406,8 +408,8 @@ public:
Price& out_price){
out_price = current_price;
api::Side book_side = invert_side(hedge_side);
Quantity cumm_qty;
bool find_qty_at_price = find_qty_upto_price_level (book, book_side, current_price, tick_size, cumm_qty);
Quantity price_level_qty, cumm_qty;
bool find_qty_at_price = find_qty_at_price_level (book, book_side, current_price, tick_size, price_level_qty, cumm_qty);
if (find_qty_at_price){
Quantity qty_threshold = _quantity_threshold(params.quantity_threshold(), current_qty);
if (cumm_qty.value() > qty_threshold.value()){
@@ -431,8 +433,8 @@ public:
Price& out_price){
out_price = current_price;
api::Side book_side = invert_side(hedge_side);
Quantity cumm_qty;
bool find_qty_at_price = find_qty_upto_price_level (book, book_side, current_price, tick_size, cumm_qty);
Quantity price_level_qty, cumm_qty;
bool find_qty_at_price = find_qty_at_price_level (book, book_side, current_price, tick_size, price_level_qty, cumm_qty);
if (find_qty_at_price){
Quantity qty_threshold = _quantity_threshold(params.quantity_threshold(), current_qty);
if (cumm_qty.value() < qty_threshold.value()){
@@ -635,16 +637,18 @@ public:
const Book& book,
Price& price,
Quantity& quantity){
Quantity out_cum_qty(0);
Quantity out_qty(0), out_cum_qty(0);
if(leg.trading_side()==api::Side::SIDE_BUY)
find_qty_upto_price_level(book,api::Side::SIDE_SELL,
find_qty_at_price_level(book,api::Side::SIDE_SELL,
price,
leg.tick_size(),
out_qty,
out_cum_qty);
else if(leg.trading_side()==api::Side::SIDE_SELL)
find_qty_upto_price_level(book,api::Side::SIDE_BUY,
find_qty_at_price_level(book,api::Side::SIDE_BUY,
price,
leg.tick_size(),
out_qty,
out_cum_qty);
quantity = Quantity(std::min(quantity.value(),out_cum_qty.value()));
}
@@ -790,6 +794,47 @@ public:
quote_fill_quantity,
hedge_leg);
}
static bool compute_nlegs_crossed_hedges(
api::Side spr_side,
Price spr_price,
size_t n_legs,
const LegPropertyData& q_leg,
Price q_fill_price,
double q_fill_qty,
const ISpreaderManager& spreader,
std::vector<std::pair<Price,double>>& out_drivers){
// spr_cross_price is the spreader price computed by
// crossing all the hedges ie
// spr_cross_price=sum(ratio*price) where for the filled leg
// it is the fill price and for the hedging legs
// it is the cross that satisifies the size
double spr_cross_price=q_fill_price.value()*q_leg.price_multiplier();
for (size_t i=0;i<n_legs;i++)
if(i!=q_leg.leg_index()){
Price hedge_price;
Quantity hedge_qty;
const auto& hedge_leg=spreader.get_leg_data(i);
double hedge_qty_needed_raw =
get_leg_quantity_from_another_leg_quantity(q_leg,
q_fill_qty,
hedge_leg);
Quantity hedge_qty_needed_rounded_up =
Quantity(_round_hedge_target(hedge_qty_needed_raw));
traverse_depth_for_quantity(
spreader.get_book(i),
invert_side(hedge_leg.trading_side()),
hedge_qty_needed_rounded_up,
hedge_price,
hedge_qty);
if(hedge_qty.value()<hedge_qty_needed_rounded_up.value())
return false;
spr_cross_price += hedge_price.value()*hedge_leg.price_multiplier();
auto& driver_i=out_drivers[i];
driver_i.first=hedge_price;
driver_i.second=hedge_qty_needed_raw;
}
return securities::is_better(spr_side,Price(spr_cross_price),spr_price);
}
////////////////////////////////////////////////////
// Rounding functions //////////////////////////////
////////////////////////////////////////////////////
@@ -2340,43 +2340,34 @@ TEST_CASE ("spreader_WAITING_FOR_CANCEL_ACK", "[spreader]")
}
TEST_CASE ("find_target_price_in_book", "[spreader]"){
SECTION("0.05"){
Quantity out_cum_qty;
Quantity out_level_qty, out_cum_qty;
bts::orderman::Book book;
uint16_t sizes[] = { 11, 13, 17, 19, 23 };
double prices[] = { 22, 21.15, 20.10, 20.05, 20.00 };
fill_book (book, prices, sizes, bts::api::Side::SIDE_BUY);
// middle
bool res = SpreaderUtils::find_qty_upto_price_level(book, bts::api::Side::SIDE_BUY, Price(20.10), 0.05, out_cum_qty);
bool res = SpreaderUtils::find_qty_at_price_level(book, bts::api::Side::SIDE_BUY, Price(20.10), 0.05, out_level_qty, out_cum_qty);
REQUIRE(res);
REQUIRE(out_level_qty.value()==17);
REQUIRE(out_cum_qty.value()==41);
// first
res = SpreaderUtils::find_qty_upto_price_level(book, bts::api::Side::SIDE_BUY, Price(22.00), 0.05, out_cum_qty);
res = SpreaderUtils::find_qty_at_price_level(book, bts::api::Side::SIDE_BUY, Price(22.00), 0.05, out_level_qty, out_cum_qty);
REQUIRE(res);
REQUIRE(out_level_qty.value()==11);
REQUIRE(out_cum_qty.value()==11);
// last
res = SpreaderUtils::find_qty_upto_price_level(book, bts::api::Side::SIDE_BUY, Price(20.00), 0.05, out_cum_qty);
res = SpreaderUtils::find_qty_at_price_level(book, bts::api::Side::SIDE_BUY, Price(20.00), 0.05, out_level_qty, out_cum_qty);
REQUIRE(res);
REQUIRE(out_level_qty.value()==23);
REQUIRE(out_cum_qty.value()==83);
// missing
res = SpreaderUtils::find_qty_upto_price_level(book, bts::api::Side::SIDE_BUY, Price(21.00), 0.05, out_cum_qty);
REQUIRE(res);
REQUIRE(out_cum_qty.value()==24);
// missing
res = SpreaderUtils::find_qty_upto_price_level(book, bts::api::Side::SIDE_BUY, Price(20.075), 0.05, out_cum_qty);
REQUIRE(res);
REQUIRE(out_cum_qty.value()==41);
// rounding error
res = SpreaderUtils::find_qty_upto_price_level(book, bts::api::Side::SIDE_BUY, Price(20.04999999), 0.05, out_cum_qty);
REQUIRE(res);
REQUIRE(out_cum_qty.value()==60);
// before
res = SpreaderUtils::find_qty_upto_price_level(book, bts::api::Side::SIDE_BUY, Price(22.05), 0.05, out_cum_qty);
// not found
res = SpreaderUtils::find_qty_at_price_level(book, bts::api::Side::SIDE_BUY, Price(21.00), 0.05, out_level_qty, out_cum_qty);
REQUIRE(!res);
REQUIRE(out_cum_qty.value()==0);
// after
res = SpreaderUtils::find_qty_upto_price_level(book, bts::api::Side::SIDE_BUY, Price(19), 0.05, out_cum_qty);
// rounding error
res = SpreaderUtils::find_qty_at_price_level(book, bts::api::Side::SIDE_BUY, Price(20.04999999), 0.05, out_level_qty, out_cum_qty);
REQUIRE(res);
REQUIRE(out_cum_qty.value()==83);
REQUIRE(out_level_qty.value()==19);
REQUIRE(out_cum_qty.value()==60);
}
SECTION("1/32"){
Quantity out_level_qty, out_cum_qty;
@@ -2385,33 +2376,34 @@ TEST_CASE ("find_target_price_in_book", "[spreader]"){
double prices[] = { 100.21875, 100.28125, 101, 101.53125, 101.71875 };
fill_book (book, prices, sizes, bts::api::Side::SIDE_SELL);
// first
bool res = SpreaderUtils::find_qty_upto_price_level(book, bts::api::Side::SIDE_SELL, Price(100.21875), 0.03125, out_cum_qty);
bool res = SpreaderUtils::find_qty_at_price_level(book, bts::api::Side::SIDE_SELL, Price(100.21875), 0.03125, out_level_qty, out_cum_qty);
REQUIRE(res);
REQUIRE(out_level_qty.value()==12);
REQUIRE(out_cum_qty.value()==12);
// middle
res = SpreaderUtils::find_qty_upto_price_level(book, bts::api::Side::SIDE_SELL, Price(101.53125), 0.03125, out_cum_qty);
res = SpreaderUtils::find_qty_at_price_level(book, bts::api::Side::SIDE_SELL, Price(101.53125), 0.03125, out_level_qty, out_cum_qty);
REQUIRE(res);
REQUIRE(out_level_qty.value()==24);
REQUIRE(out_cum_qty.value()==132);
// end
res = SpreaderUtils::find_qty_upto_price_level(book, bts::api::Side::SIDE_SELL, Price(101.71875), 0.03125, out_cum_qty);
res = SpreaderUtils::find_qty_at_price_level(book, bts::api::Side::SIDE_SELL, Price(101.71875), 0.03125, out_level_qty, out_cum_qty);
REQUIRE(res);
REQUIRE(out_level_qty.value()==33);
REQUIRE(out_cum_qty.value()==165);
// absent
res = SpreaderUtils::find_qty_upto_price_level(book, bts::api::Side::SIDE_SELL, Price(101.5625), 0.03125, out_cum_qty);
REQUIRE(res);
REQUIRE(out_cum_qty.value()==132);
// rounding
res = SpreaderUtils::find_qty_upto_price_level(book, bts::api::Side::SIDE_SELL, Price(101.531249999999), 0.03125, out_cum_qty);
REQUIRE(res);
REQUIRE(out_cum_qty.value()==132);
// before
res = SpreaderUtils::find_qty_upto_price_level(book, bts::api::Side::SIDE_SELL, Price(100.21875-0.03125), 0.03125, out_cum_qty);
res = SpreaderUtils::find_qty_at_price_level(book, bts::api::Side::SIDE_SELL, Price(101.5625), 0.03125, out_level_qty, out_cum_qty);
REQUIRE(!res);
REQUIRE(out_level_qty.value()==0);
REQUIRE(out_cum_qty.value()==0);
// after
res = SpreaderUtils::find_qty_upto_price_level(book, bts::api::Side::SIDE_SELL, Price(101.71875+0.03125), 0.03125, out_cum_qty);
res = SpreaderUtils::find_qty_at_price_level(book, bts::api::Side::SIDE_BUY, Price(101.71875), 0.03125, out_level_qty, out_cum_qty);
REQUIRE(!res);
REQUIRE(out_level_qty.value()==0);
REQUIRE(out_cum_qty.value()==0);
// rounding
res = SpreaderUtils::find_qty_at_price_level(book, bts::api::Side::SIDE_SELL, Price(101.531249999999), 0.03125, out_level_qty, out_cum_qty);
REQUIRE(res);
REQUIRE(out_cum_qty.value()==165);
REQUIRE(out_level_qty.value()==24);
REQUIRE(out_cum_qty.value()==132);
}
}
TEST_CASE ("prehedge_payless", "[spreader]"){
@@ -361,7 +361,7 @@ void OrderServiceServer::on_order_op_result(
bts_assert(it!=me->in_flight_order_ops.end());
auto &responder = it->second;
api::orderman::Response r;
r.set_order_key(op_result.ok.value());
r.set_order_key(0);
r.set_error_code(op_result.result.error);
r.set_msg(op_result.result.error_msg);
responder(r);
+1 -35
View File
@@ -15,10 +15,8 @@
#include <utility>
#include <thread>
#include <atomic>
#include <iostream>
#include <tbb/queuing_mutex.h>
#include <boost/filesystem.hpp>
namespace bts {
namespace utils {
@@ -240,38 +238,6 @@ public:
offset = sizeof(SegmentHeader);
}
}
void dump_to(const char* directory) const {
using namespace std;
boost::system::error_code _;
if (!boost::filesystem::exists(directory, _)) {
boost::filesystem::create_directories(directory, _);
}
if (!boost::filesystem::is_directory(directory)) {
LOG(md,Error, "Error dumping memory to {}", std::string(directory));
return;
}
boost::filesystem::path root(directory);
// OK to hold the lock for a while as we're typically crashing when this is called
tbb::queuing_mutex::scoped_lock lock(_mutex);
for (size_t x = 0; x < _segments.size(); ++x) {
char segment_name[512];
SharedList<THeader, TEntry>::get_segment_name(segment_name, BTS_ARRAYSIZE(segment_name), _name, x);
// create/overwrite output file
std::ofstream writer( (root/segment_name).string(), ios::out | ios::app | ios::binary);
if (writer.is_open()) {
// writer just loves signed types
writer.write(
(const char*)_segments[x].address,
(std::streamsize)_segments[x].size);
}
}
}
private:
BTS_NO_COPY(SharedListReader);
@@ -315,7 +281,7 @@ private:
const std::string _name;
THeader* _header; // null value indicates no memory segment
mutable tbb::queuing_mutex _mutex;
tbb::queuing_mutex _mutex;
std::unique_ptr<LowLevelSharedMemory> _shm;
std::vector<Segment> _segments;
};
@@ -36,7 +36,7 @@ void ShellLatencyServer::report(
reply(bts::api::VoidMessage());
// look up or create the metric
auto key = request.host_name() + "-" + request.version() + "-" + request.user();
auto key = request.host_name() + "-" + request.version();
auto iter = _metrics.find(key);
std::shared_ptr<AutoResetMetric> metric;
// report at least 1ms because the metric will not report anything if it's 0
@@ -44,8 +44,8 @@ void ShellLatencyServer::report(
if (iter == _metrics.end()) {
// create metric
auto metric =
std::make_shared<AutoResetMetric>("spark_shell_latency",
std::vector<Label>({{"host",request.host_name()}, {"version",request.version()}, {"user", request.user()}}));
std::make_shared<AutoResetMetric>("spark_shell_latency",
std::vector<Label>({{"host",request.host_name()}, {"version",request.version()}, {"user", request.user()}}));
_metrics.insert(std::make_pair(key, metric));
metric->max(delta);
_mc.publish(metric);
+4 -19
View File
@@ -10,7 +10,6 @@ open EnumUtils
open Fable
open System
open bts.co2
open Elmish.React.Common
let accountIsSelected (initialState:BTSAccount) (scratch:BTSAccount) = scratch <> initialState
@@ -180,6 +179,7 @@ let getSecurityMap (model:Model) (positionLimit:AccountSecurityPositionLimitUnit
let clipLimitsView model dispatch =
let symbols = GetSymbolArray model
let headerRow:TableControl.row =
{
cells =
@@ -322,8 +322,7 @@ let clipLimitsView model dispatch =
| true -> "is-selected"
header = None
}
yield footerRow
]
] @ [footerRow]
let tempSec = { SecurityScope.empty with securitySymbol =
match GetFilteredSymbolArray model usedSymbolNames |> Array.tryHead with
@@ -348,13 +347,6 @@ let clipLimitsView model dispatch =
TableControl.createDataTable (Some headerRow) rws (Button.button "Add" "" "plus-circle" Button.Enabled addRow)
]
let lazyClipLimitsView model dispatch =
lazyView2With (fun a b ->
LanguagePrimitives.PhysicalEquality a.SelectedClipLimits b.SelectedClipLimits &&
LanguagePrimitives.PhysicalEquality a.scratch.securityClipLimits b.scratch.securityClipLimits)
clipLimitsView
model dispatch
let productLimits model dispatch =
let headerRow:TableControl.row =
{
@@ -542,12 +534,6 @@ let productLimits model dispatch =
br []
]
let lazyProductLimitsView model dispatch =
lazyView2With (fun a b ->
LanguagePrimitives.PhysicalEquality a.SelectedPositionLimits b.SelectedPositionLimits &&
LanguagePrimitives.PhysicalEquality a.scratch.productPositionLimits b.scratch.productPositionLimits)
productLimits
model dispatch
let securityLimitsView model dispatch =
let headerRow:TableControl.row =
@@ -667,9 +653,8 @@ let securityLimitsView model dispatch =
let LimitsView model dispatch =
div [ Class "limits-container"] [
lazyClipLimitsView model dispatch
lazyProductLimitsView model dispatch
clipLimitsView model dispatch
productLimits model dispatch
//securityLimitsView model dispatch
accountControls model dispatch
@@ -3,10 +3,11 @@ This test makes sure the basic spreader functionality works for 2 legged synthet
0. Follow the instructions from docs/spreader_common_setup_1.txt
1. Enter a synthetic buy order of 1 lot at -0.30. Verify that a quoting order of 18.60 shows up in the second leg.
Cancel the synthetic order and the quoting order should be cancelled too.
2. Enter a synthetic buy order of 1 lot at -0.30, again. Now drag the the synthetic order down the ladder (ie lower bid prices) to a few random prices of your choice. Verify that the quoter leg price is adjusted accordingly and is correct.
2. Enter a synthetic buy order of 1 lot at -0.30, again. Now drag the the synthetic order down the ladder
(ie lower bid prices) to a few random prices of your choice. Verify that the quoter leg price is adjusted
accordingly and is correct.
(If you have followed the instructions in common_setup_1.txt: Leg_1 will buy at 18.300 , and leg_2 will be your quoting leg)
@@ -15,5 +16,4 @@ spread_price == price_mult_1 * leg_1 + price_mult_2 * leg_2
leg_2 == price_mult_1 * leg_1 - spread_price / -1.0 * price_mult_2
3. Now cross the synthetic order by dragging it all the way to -0.20 (there should be a 1 lot at that price).
Verify that the synthetic order gets filled at -0.20, the quoter order at 18.50, and the hedger order at 18.30.
+1 -26
View File
@@ -1,18 +1,11 @@
@echo off
@setlocal
SETLOCAL
if [%1]==[] goto PRINT_USAGE
if [%3] == [r] (
set build_type=release
) else (
set build_type=debug
)
echo(%0)
echo(%1)
echo(%2)
echo(%3)
echo %build_type%
if "%SPARKVM_PORT%"=="" set SPARKVM_PORT=22
echo.
@@ -23,25 +16,7 @@ dotnet publish -f netcoreapp2.1
cd ..\..\..\build\Debug\bin\netcoreapp2.1\publish
bash -c "scp * bts@sparkvm:~/spark/build/ninja_%build_type%/server/"
copy /Y %~dp0\..\src\client\spark_test\system_test_scripts\*.sh ..\..\net471\
cd ../../net471
bash -c "scp *.sh bts@sparkvm:~/spark/build/ninja_%build_type%/server/"
ssh bts@sparkvm -p %SPARKVM_PORT% -o StrictHostKeyChecking=no -t "cd /home/bts/spark/build/ninja_%build_type%/server && ./%1 %2"
ssh bts@sparkvm -p %SPARKVM_PORT% -o StrictHostKeyChecking=no -t "cd /home/bts/spark/build/ninja_%build_type%/server && dotnet sparkcmd.dll test %1 %2"
ENDLOCAL
popd
goto END
:PRINT_USAGE
echo Runs a spark test on a linux machine.
echo(
echo usage:
echo run_sparkcmd_test_linux.cmd test_name [r]
echo(
echo r Specifies the build should be in release mode
echo(
goto END
:END