Files
spark/src/client/spark_test/System/LeanAlgoTests.cs
T
2019-03-21 08:05:33 -05:00

145 lines
8.6 KiB
C#

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");
}
}
}