refactoring/reorg
This commit is contained in:
1 parent
5f4eb58db9
commit
0e92a12e65
3 files changed
+64
-51
No files matched your search
@@ -7,34 +7,20 @@ 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;
|
||||
|
||||
namespace bts.tests {
|
||||
|
||||
static class TestUtils {
|
||||
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;
|
||||
|
||||
}
|
||||
|
||||
|
||||
[TestGroup(TestType = TestType.System)]
|
||||
class IcebergAlgoTests : IDisposable {
|
||||
List<IDisposable> _resources;
|
||||
ServiceContext _context;
|
||||
AdminService _admin_service;
|
||||
OrderService _order_service;
|
||||
Update<FillImmutable, string> _fills;
|
||||
|
||||
public IcebergAlgoTests() {
|
||||
_context = new ServiceContext();
|
||||
@@ -45,11 +31,6 @@ namespace bts.tests {
|
||||
_context,
|
||||
_admin_service,
|
||||
_order_service,
|
||||
_order_service.subscribei_fills().Subscribe(phills =>
|
||||
{
|
||||
// lol >8)
|
||||
_fills = phills;
|
||||
})
|
||||
};
|
||||
|
||||
var auth = _context.login("bts", "");
|
||||
@@ -94,28 +75,6 @@ namespace bts.tests {
|
||||
return security_with_no_market;
|
||||
}
|
||||
|
||||
public OrderDefinition create_iceberg_order_def(uint security_id, Side side, (String name, uint id) account, (String name, uint id) user) {
|
||||
return new OrderDefinition {
|
||||
SecurityKey = security_id,
|
||||
Side = side,
|
||||
TimeInForce = TimeInForce.Day,
|
||||
CustomDef = new CustomOrderDefinition {
|
||||
Iceberg = new IcebergOrderDefinition {
|
||||
ShowQuantity = 3,
|
||||
Child = new CustomOrderDefinition {
|
||||
Limit = new LimitOrderDefinition { }
|
||||
}
|
||||
}
|
||||
},
|
||||
CtiCode = CTICode.CtiCode1,
|
||||
CustomerOrFirm = CustomerOrFirm.Firm,
|
||||
ManualOrderIndicator = ManualOrderIndicator.Yes,
|
||||
Account = account.name,
|
||||
AccountId = account.id,
|
||||
User = user.name,
|
||||
UserId = user.id
|
||||
};
|
||||
}
|
||||
|
||||
[Test(Timeout = 25)]
|
||||
public async Task test_ib_create_and_takeout_market() {
|
||||
@@ -153,7 +112,7 @@ namespace bts.tests {
|
||||
}
|
||||
|
||||
// create an IB that will cross this market
|
||||
var ib_order_def = create_iceberg_order_def(s.Value.Id, Side.Buy, test_account, test_user);
|
||||
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,
|
||||
@@ -165,10 +124,13 @@ namespace bts.tests {
|
||||
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 child orders");
|
||||
Assert.IsTrue(ib_child_orders.All(x => x.HasState && x.State.Status == OrderStatus.Completed), "All child orders should be completed");
|
||||
Assert.IsTrue(ib_child_orders.Count() == 10, "There should have been 10 completed child orders");
|
||||
|
||||
var filled_childen = _fills.all.Where(x => ib_child_orders.Select(o => o.Key).Contains(x.OrderKey));
|
||||
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");
|
||||
|
||||
@@ -45,18 +45,36 @@ namespace bts.tests
|
||||
}
|
||||
}
|
||||
|
||||
public static Task<utils.TestUtils.TimeoutResult<IEnumerable<OrderRecordImmutable>>>
|
||||
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 s1 = order_service.subscribei_orders().Subscribe(update => {
|
||||
var sub = order_service.subscribei_orders().Subscribe(update => {
|
||||
if (match(update.all)) {
|
||||
tcs.TrySetResult(selector(update.all));
|
||||
}
|
||||
}) ;
|
||||
return tcs.Task.timeout(timeout.Value);
|
||||
});
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -80,5 +98,29 @@ namespace bts.tests
|
||||
UserId = user.id
|
||||
};
|
||||
}
|
||||
|
||||
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) {
|
||||
return new OrderDefinition {
|
||||
SecurityKey = security_id,
|
||||
Side = side,
|
||||
TimeInForce = TimeInForce.Day,
|
||||
CustomDef = new CustomOrderDefinition {
|
||||
Iceberg = new IcebergOrderDefinition {
|
||||
ShowQuantity = show_quantity,
|
||||
Child = new CustomOrderDefinition {
|
||||
Limit = new LimitOrderDefinition { }
|
||||
}
|
||||
}
|
||||
},
|
||||
CtiCode = CTICode.CtiCode1,
|
||||
CustomerOrFirm = CustomerOrFirm.Firm,
|
||||
ManualOrderIndicator = ManualOrderIndicator.Yes,
|
||||
Account = account.name,
|
||||
AccountId = account.id,
|
||||
User = user.name,
|
||||
UserId = user.id
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using bts.collections;
|
||||
using bts.api;
|
||||
using bts.collections;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@@ -40,6 +41,14 @@ 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) {
|
||||
|
||||
Reference in new issue
Block a user