204 lines
9.1 KiB
C#
204 lines
9.1 KiB
C#
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();
|
|
if(tr.result == null) {
|
|
Console.WriteLine("Timed out while retreving orders.");
|
|
}
|
|
|
|
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 OrderDataHelper 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;
|
|
}
|
|
|
|
}
|
|
|
|
} |