From 4490ac303e594e25bb03c2c419f9d8c0bc00a337 Mon Sep 17 00:00:00 2001 From: Michael Harding Date: Wed, 12 Jun 2019 15:41:16 -0400 Subject: [PATCH] SPARK-2438: Lean Qty test --- src/client/spark_test/System/SpreaderTests.cs | 158 +++++++++++++++++- src/client/spark_test/Utils/OrderUtilities.cs | 6 +- .../spark_test/Utils/SpreaderUtilities.cs | 28 +++- 3 files changed, 179 insertions(+), 13 deletions(-) diff --git a/src/client/spark_test/System/SpreaderTests.cs b/src/client/spark_test/System/SpreaderTests.cs index af23a7fed..4116f9b4a 100644 --- a/src/client/spark_test/System/SpreaderTests.cs +++ b/src/client/spark_test/System/SpreaderTests.cs @@ -76,7 +76,7 @@ namespace bts.tests ); 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"); + Assert.IsTrue(spreader_order1_response.success, "Failed to send 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"); @@ -120,11 +120,11 @@ namespace bts.tests } - [Test(Timeout = 30)] + [Test(Timeout = 300000)] public async Task spreader_lean_qty_factor() { - Assert.IsTrue(await TestUtils.test_for_system_ready(_context, TimeSpan.FromMinutes(5)), "System is not ready"); + await setup_spreader_tests(); - string test_symbol = "SI";// don't use ES, the prices will be rounded to the same number and the result will be off + string test_symbol = "SI"; var test_user = (name: "bts", id: 1u); var test_account = (name: "ACC A", id: 1u); @@ -132,6 +132,141 @@ namespace bts.tests 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 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 {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"); + + // change the synthetic + var hedge_leg = setup_product.synthetic.strategy.LegParamsMap.Where(m => !m.Value.IsActiveQuoting).TryFirst(); + Assert.IsTrue(hedge_leg.HasValue, "you need more legs"); + + foreach(var key in setup_product.synthetic.strategy.LegParamsMap.Keys) { + if(!setup_product.synthetic.strategy.LegParamsMap[key].IsActiveQuoting) { + setup_product.synthetic.strategy.LegParamsMap[key].QuoteParams.LeanQuantityRatio = 2d; + break; + } + } + + Assert.IsTrue(await SpreaderUtilities.save_strategy(_synthetic_service, setup_product.synthetic.security.Id, (ExecutionStrategyImmutable) setup_product.synthetic.strategy), "Failed to save strategy"); + + var order2_def = OrderDefinitionUtilities.create_spreader_order_def( + setup_product.synthetic.security, + Side.Buy, + setup_product.synthetic.strategy, + test_account, + test_user + ); + + var spreader_order2_response = await OrderUtilities.send_order(_order_service, order2_def, -0.30, 1); + Assert.IsTrue(spreader_order2_response.success, "Failed to send spreader order"); + + 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 is not Working"); + + var (quoting_order2_is_working, working_quoting_order2) = await verify_child_order_status(spreader_order2_response.value.OrderKey, OrderStatus.Working); + Assert.IsTrue(quoting_order2_is_working, "Order is not Working"); + Assert.IsTrue(working_quoting_order2.Value.State.Price == 18.61, "Quoting order has the wrong price"); + + Assert.IsTrue(await OrderUtilities.cancel_orders(_order_service, new List {working_spreader_order2.Value}), "Failed to cancel order"); + + var (quoting_order2_is_canceled, canceled_quoting_order2) = await verify_child_order_status(spreader_order2_response.value.OrderKey, OrderStatus.Canceled); + Assert.IsTrue(quoting_order2_is_canceled, "Quoting order may not have been canceled"); + + + // Order 3 + foreach(var key in setup_product.synthetic.strategy.LegParamsMap.Keys) { + if(!setup_product.synthetic.strategy.LegParamsMap[key].IsActiveQuoting) { + setup_product.synthetic.strategy.LegParamsMap[key].QuoteParams.LeanQuantityRatio = 4d; + break; + } + } + + Assert.IsTrue(await SpreaderUtilities.save_strategy(_synthetic_service, setup_product.synthetic.security.Id, (ExecutionStrategyImmutable) setup_product.synthetic.strategy), "Failed to save strategy"); + + var order3_def = OrderDefinitionUtilities.create_spreader_order_def( + setup_product.synthetic.security, + Side.Buy, + setup_product.synthetic.strategy, + test_account, + test_user + ); + + var spreader_order3_response = await OrderUtilities.send_order(_order_service, order3_def, -0.30, 1); + Assert.IsTrue(spreader_order3_response.success, "Failed to send spreader order"); + + var (spreader_order3_is_working, working_spreader_order3) = await verify_order_status(spreader_order3_response.value.OrderKey, OrderStatus.Working); + Assert.IsTrue(spreader_order3_is_working, "Spreader order is not Working"); + + var (quoting_order3_is_working, working_quoting_order3) = await verify_child_order_status(spreader_order3_response.value.OrderKey, OrderStatus.Working); + Assert.IsTrue(quoting_order3_is_working, "Order is not Working"); + Assert.IsTrue(working_quoting_order3.Value.State.Price == 18.63, "Quoting order has the wrong price"); + + Assert.IsTrue(await OrderUtilities.cancel_orders(_order_service, new List {working_spreader_order3.Value}), "Failed to cancel order"); + + var (quoting_order3_is_canceled, canceled_quoting_order3) = await verify_child_order_status(spreader_order3_response.value.OrderKey, OrderStatus.Canceled); + Assert.IsTrue(quoting_order3_is_canceled, "Quoting order may not have been canceled"); + + // Order 4 + foreach(var key in setup_product.synthetic.strategy.LegParamsMap.Keys) { + if(!setup_product.synthetic.strategy.LegParamsMap[key].IsActiveQuoting) { + setup_product.synthetic.strategy.LegParamsMap[key].QuoteParams.LeanQuantityRatio = 5d; + break; + } + } + + Assert.IsTrue(await SpreaderUtilities.save_strategy(_synthetic_service, setup_product.synthetic.security.Id, (ExecutionStrategyImmutable) setup_product.synthetic.strategy), "Failed to save strategy"); + + var order4_def = OrderDefinitionUtilities.create_spreader_order_def( + setup_product.synthetic.security, + Side.Buy, + setup_product.synthetic.strategy, + test_account, + test_user + ); + + var spreader_order4_response = await OrderUtilities.send_order(_order_service, order4_def, -0.30, 1); + Assert.IsTrue(spreader_order4_response.success, "Failed to send spreader order"); + + var (spreader_order4_is_working, working_spreader_order4) = await verify_order_status(spreader_order4_response.value.OrderKey, OrderStatus.Working); + Assert.IsTrue(spreader_order4_is_working, "Spreader order is not Working"); + + var no_quoting_order4 = await get_order(ord => ord.ParentKey == spreader_order4_response.value.OrderKey, TimeSpan.FromSeconds(10)); + Assert.IsFalse(no_quoting_order4.HasValue, "There should not have been a quoting order here..."); + + OrderRecordImmutable order_from_book = setup_product.book.Where(ordi => ordi.State.Price == 18.30).First(); + var mod_resp = await OrderUtilities.modify_order(_order_service, order_from_book, 2); + Assert.IsTrue(mod_resp.success, "Order size was not changed"); + + var (quoting_order4_is_working, working_quoting_order4) = await verify_child_order_status(spreader_order4_response.value.OrderKey, OrderStatus.Working); + Assert.IsTrue(quoting_order4_is_working, "Order is not Working"); + Assert.IsTrue(working_quoting_order4.Value.State.Price == 18.63, "Quoting order has the wrong price"); + + var mod_resp2 = await OrderUtilities.modify_order(_order_service, order_from_book, 1); + Assert.IsTrue(mod_resp.success, "Order size was not changed"); + + var (quoting_order5_is_canceled, canceled_quoting_order5) = await verify_child_order_status(spreader_order4_response.value.OrderKey, OrderStatus.Canceled); + Assert.IsTrue(quoting_order5_is_canceled, "Order was not canceld"); + + Assert.IsTrue(await OrderUtilities.cancel_orders(_order_service, new List {working_spreader_order4.Value}), "Failed to cancel order"); } @@ -155,13 +290,22 @@ namespace bts.tests return order.HasValue && (Math.Truncate(order.Value.State.Price * precision) == precision * expected_price); } - private async Task get_order(Func predicate) + private async Task get_order(Func predicate, TimeSpan? timeout = null) { + if(timeout == null){ + timeout = TimeSpan.FromSeconds(60); + } 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(); + timeout); + if(orders.result != null) { + return orders.result.TryFirst(); + } + else { + return null; + } + } diff --git a/src/client/spark_test/Utils/OrderUtilities.cs b/src/client/spark_test/Utils/OrderUtilities.cs index 031484a5c..ab89a0f83 100644 --- a/src/client/spark_test/Utils/OrderUtilities.cs +++ b/src/client/spark_test/Utils/OrderUtilities.cs @@ -75,6 +75,10 @@ namespace bts.tests }); var tr = await tcs.Task.timeout(timeout.Value); sub.Dispose(); + if(tr.result == null) { + Console.WriteLine("Timed out while retreving orders."); + } + return tr; } @@ -124,7 +128,7 @@ namespace bts.tests } /// - /// We should replace with something like the OrderDataHeler from the Spark project. + /// We should replace with something like the OrderDataHelper from the Spark project. /// public static class OrderDefinitionUtilities { diff --git a/src/client/spark_test/Utils/SpreaderUtilities.cs b/src/client/spark_test/Utils/SpreaderUtilities.cs index db2f830f6..5de24e9a6 100644 --- a/src/client/spark_test/Utils/SpreaderUtilities.cs +++ b/src/client/spark_test/Utils/SpreaderUtilities.cs @@ -27,8 +27,24 @@ namespace bts.tests Assert.IsNotNull(exps); Assert.IsTrue(exps.Count() > 2, "Need at least two expirations"); - var exp1 = exps[0]; - var exp2 = exps[1]; + (uint, uint) preferred_expirations = (07,12); + + FutureContract exp1; + FutureContract exp2; + + if(exps.Exists(expir => expir.ExpirationMonth == preferred_expirations.Item1)) { + exp1 = exps.Where(ex => ex.ExpirationMonth == preferred_expirations.Item1).First(); + } + else { + exp1 = exps[0]; + } + + if(exps.Exists(expir => expir.ExpirationMonth == preferred_expirations.Item2 && expir.ExpirationYear == exp1.ExpirationYear)) { + exp2 = exps.Where(ex => ex.ExpirationMonth == preferred_expirations.Item2 && ex.ExpirationYear == exp1.ExpirationYear).First(); + } + else { + exp2 = exps[1]; + } Func sec1_match = ch => ch.Security.Future.ExpirationYear == exp1.ExpirationYear && @@ -103,13 +119,15 @@ namespace bts.tests 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.LowTick = 0.005; + new_synth.ContractSpec.HighTick = 0.005; new_synth.ContractSpec.PriceBreak = double.MaxValue; new_synth.ContractSpec.MainFraction = 1u; new_synth.ContractSpec.PriceDisplayFormat = 3u; + new_synth.ContractSpec.CashPerPoint = 1u; + new_synth.ContractSpec.PriceMultiplier = 1u; new_synth.Exchange = Venue.Bts; - new_synth.Synthetic.Name = "TEST_SYNTH" + DateTime.Now.ToString(); + new_synth.Synthetic.Name = "TEST_SYNTH" + DateTime.Now.TimeOfDay.ToString(); var result = await service.create_synthetic_async(new_synth); Assert.IsTrue(result.success, "Unable to create synthetic");