diff --git a/src/client/spark_excel/algo_example.xlsm b/src/client/spark_excel/algo_example.xlsm index 053bb43c6..824f245e0 100644 Binary files a/src/client/spark_excel/algo_example.xlsm and b/src/client/spark_excel/algo_example.xlsm differ diff --git a/src/client/spark_excel/algogen/Generate.cs b/src/client/spark_excel/algogen/Generate.cs index 4815ad1ab..9f3c3a46a 100644 --- a/src/client/spark_excel/algogen/Generate.cs +++ b/src/client/spark_excel/algogen/Generate.cs @@ -35,6 +35,10 @@ namespace bts.excel { static ConcurrentDictionary _algo_params = new ConcurrentDictionary(); public static IReadOnlyDictionary algo_params => _algo_params; + + static ConcurrentDictionary _special_params = new ConcurrentDictionary(); + public static IReadOnlyDictionary special_params => _special_params; + public static void register_algo_parameter(string type, string name) { _algo_params[name] = string.IsNullOrEmpty(type) ? "double" : type.ToLower(); @@ -43,6 +47,16 @@ namespace bts.excel public static void unregister_algo_parameter(string name) { _algo_params.TryRemove(name, out _); + } + + public static void register_special_parameter(string alias, string name) + { + _special_params[alias] = name; + } + + public static void unregister_special_parameter(string name) + { + _special_params.TryRemove(name, out _); } static public string bts_generate_algo_class(string algoname, IEnumerable functions, string[] filepaths = null) @@ -97,11 +111,11 @@ namespace bts.excel foreach (var kv in _algo_params) { string launch = $@" - auto {kv.Key}_param = params.find(""{kv.Key}""); - if ( {kv.Key}_param == params.end() ) - return OrderOpResult{{ ORDER_ERROR_INVALID_ORDER_DEFINITON,""{kv.Key} parameter must be defined""}}; - me->{kv.Key} = {kv.Key}_param->second.{get_as(kv.Key, kv.Value)}(); - "; +auto {kv.Key}_param = params.find(""{kv.Key}""); +if ( {kv.Key}_param == params.end() ) + return OrderOpResult{{ ORDER_ERROR_INVALID_ORDER_DEFINITON,""{kv.Key} parameter must be defined""}}; +me->{kv.Key} = {kv.Key}_param->second.{get_as(kv.Key, kv.Value)}(); +"; launches.Add(launch); } string paramlaunch = string.Join(Environment.NewLine, launches); @@ -114,14 +128,14 @@ namespace bts.excel foreach(var kv in _algo_params) { string update = $@" - auto {kv.Key}_param = params.find(""{kv.Key}""); - if ( {kv.Key}_param == params.end() ) - {{ - algo_services().terminate_error(""Can't find parameter {kv.Key} ""); - return; - }} - me->{kv.Key} = {kv.Key}_param->second.{get_as(kv.Key, kv.Value)}(); - "; +auto {kv.Key}_param = params.find(""{kv.Key}""); +if ( {kv.Key}_param == params.end() ) +{{ + algo_services().terminate_error(""Can't find parameter {kv.Key} ""); + return; +}} +me->{kv.Key} = {kv.Key}_param->second.{get_as(kv.Key, kv.Value)}(); +"; updates.Add(update); } string paramupdates = string.Join(Environment.NewLine, updates); @@ -132,20 +146,20 @@ namespace bts.excel { List setups = new List(); setups.Add($@" - DynamicOrderDescriptor algo_desc; - algo_desc.set_dynamic_order_type(""{algoname}""); +DynamicOrderDescriptor algo_desc; +algo_desc.set_dynamic_order_type(""{algoname}""); "); foreach (var kv in _algo_params) { string setup = $@" - auto {kv.Key}_param = algo_desc.add_param_desc(); - {kv.Key}_param->set_param_name(""{kv.Key}""); - {kv.Key}_param->mutable_param_type()->set_primitive({get_param_type_enum(kv.Key, kv.Value)}); - "; +auto {kv.Key}_param = algo_desc.add_param_desc(); +{kv.Key}_param->set_param_name(""{kv.Key}""); +{kv.Key}_param->mutable_param_type()->set_primitive({get_param_type_enum(kv.Key, kv.Value)}); +"; setups.Add(setup); } setups.Add($@" - serv.register_dynamic_algo_order(algo_desc, []() {{ return std::make_unique<{algoname}>(); }}); +serv.register_dynamic_algo_order(algo_desc, []() {{ return std::make_unique<{algoname}>(); }}); "); string paramsetups = string.Join(Environment.NewLine, setups); cpp = cpp.Replace("__ParameterSetups__", paramsetups); diff --git a/src/client/spark_excel/algogen/GeneratorVisitor.cs b/src/client/spark_excel/algogen/GeneratorVisitor.cs index 4b2ba90c7..3bc14dd1b 100644 --- a/src/client/spark_excel/algogen/GeneratorVisitor.cs +++ b/src/client/spark_excel/algogen/GeneratorVisitor.cs @@ -229,7 +229,7 @@ auto range_init = [](auto& initval, int nrows, int ncols) "; _defs.add_def(initdef); var loaddef = $@" -auto range_set = [](auto& range, auto& val, auto r, auto c) +auto range_set = [](auto& range, auto& val, int r, int c) {{ range(r,c) = val; }}; @@ -312,11 +312,6 @@ auto range_set = [](auto& range, auto& val, auto r, auto c) var formula = XlCall.Excel(XlCall.xlfGetCell, 41, rref); var cellref = new CellRef(rref); string r1c1 = cellref.r1_c1(); - bool is_array_formula = (bool)XlCall.Excel(XlCall.xlfGetCell, 49, rref); - if (is_array_formula) - { - var arrayformula = XlCall.Excel(XlCall.xlfGetFormula, rref); - } var formstring = formula.ToString(); if (string.IsNullOrEmpty(formstring)) @@ -352,6 +347,12 @@ auto range_set = [](auto& range, auto& val, auto r, auto c) { _defs.map_def(def, $"auto&& {cellref.named_range} = me->{cellref.named_range};"); } + if (named && Generate.special_params.ContainsKey(cellref.named_range)) + { + string realname = cellref.named_range; + Generate.special_params.TryGetValue(cellref.named_range, out realname); + _defs.map_def(def, $"auto&& {cellref.named_range} = get_{realname}();"); + } } return cellref; } diff --git a/src/client/spark_excel/algogen/functions/FunctionLibrary.cs b/src/client/spark_excel/algogen/functions/FunctionLibrary.cs index 4eaec4d5c..3dbd0c15b 100644 --- a/src/client/spark_excel/algogen/functions/FunctionLibrary.cs +++ b/src/client/spark_excel/algogen/functions/FunctionLibrary.cs @@ -64,13 +64,6 @@ namespace bts.excel.functions.lib object bts_function_or_throw(string name, IExpression[] args, IDefinitionVisitor dv, ExcelReference containing_cell) { - //let's just reduce the surf - // object[] dparam = args.Select(x => x.evaluate()).ToArray(); - // string dcode = $"auto {name} ( "; int dz = 0; - // foreach (var p in dparam) - // dcode += "const auto& " + p.ToString() + (dz++ < dparam.Length - 1 ? ", " : ""); - // dcode += ");" + Environment.NewLine; - // dv.add_def(dcode); SparkAlgoCodeAttribute att = null; if (_dnafunctions.TryGetValue(name, out att)) { @@ -89,19 +82,22 @@ namespace bts.excel.functions.lib foreach (var p in param) function_call += p.ToString() + (z++ < param.Length - 1 ? ", " : ""); function_call += ")"; - - bool is_array_formula = (bool)XlCall.Excel(XlCall.xlfGetCell, 49, containing_cell); - if (is_array_formula) - return process_array_formula2(function_call, containing_cell); - return function_call; + return modify_if_array_formula(function_call, containing_cell); } } throw new NotSupportedException("Function " + name + " not currently supported by algogen"); } - object process_array_formula2(object code, ExcelReference containing_cell) + internal bool is_array_formula(ExcelReference containing_cell) { - ExcelReference array = null; + bool is_array_formula = (bool)XlCall.Excel(XlCall.xlfGetCell, 49, containing_cell); + return is_array_formula; + } + + internal object modify_if_array_formula(object code, ExcelReference containing_cell) + { + if(!is_array_formula(containing_cell)) + return code; using (var ech = new ExcelEchoOffHelper()) { using (var calc = new ExcelCalculationManualHelper()) @@ -113,7 +109,7 @@ namespace bts.excel.functions.lib var formula = XlCall.Excel(XlCall.xlfGetCell, 41, containing_cell); var c = new CellRef(containing_cell); var a = XlCall.Excel(XlCall.xlcSelectSpecial, 6); - array = (ExcelReference)XlCall.Excel(XlCall.xlfSelection); + ExcelReference array = (ExcelReference)XlCall.Excel(XlCall.xlfSelection); code += $"({containing_cell.RowFirst - array.RowFirst},{containing_cell.ColumnFirst - array.ColumnFirst})"; } } diff --git a/src/client/spark_excel/algogen/functions/bts/SparkAlgoCodeAttribute.cs b/src/client/spark_excel/algogen/functions/bts/SparkAlgoCodeAttribute.cs index 06cd55a57..292d9b84d 100644 --- a/src/client/spark_excel/algogen/functions/bts/SparkAlgoCodeAttribute.cs +++ b/src/client/spark_excel/algogen/functions/bts/SparkAlgoCodeAttribute.cs @@ -14,5 +14,22 @@ namespace bts.excel //nulls ok } } + + [AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = false)] + public class SparkAlgoOrderSinkAttribute : Attribute + { + public SparkAlgoOrderSinkAttribute() + { + } + } + + [AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = false)] + public class SparkAlgoSetupSinkAttribute : Attribute + { + public SparkAlgoSetupSinkAttribute() + { + } + } + } diff --git a/src/client/spark_excel/algogen/functions/bts/SparkAlgoFunctions.cs b/src/client/spark_excel/algogen/functions/bts/SparkAlgoFunctions.cs index 5edaaa25a..1bb90a81d 100644 --- a/src/client/spark_excel/algogen/functions/bts/SparkAlgoFunctions.cs +++ b/src/client/spark_excel/algogen/functions/bts/SparkAlgoFunctions.cs @@ -47,26 +47,89 @@ namespace bts.excel return $"{ErrorValue.Name}, no parameter named " + parameter; } + //See ExcelAlgoTemplate.cpp __AlgoName__ class def + [ExcelFunction(IsMacroType = true)] + static public object sparkalgo_register_user_parameter([ExcelArgument(AllowReference = true)] object parameter) + { + return sparkalgo_register_special_parameter(parameter, "user"); + } + [ExcelFunction(IsMacroType = true)] + static public object sparkalgo_register_account_parameter([ExcelArgument(AllowReference = true)] object parameter) + { + return sparkalgo_register_special_parameter(parameter, "account"); + } + [ExcelFunction(IsMacroType = true)] + static public object sparkalgo_register_side_parameter([ExcelArgument(AllowReference = true)] object parameter) + { + return sparkalgo_register_special_parameter(parameter, "side"); + } + [ExcelFunction(IsMacroType = true)] + static public object sparkalgo_register_price_parameter([ExcelArgument(AllowReference = true)] object parameter) + { + return sparkalgo_register_special_parameter(parameter, "price"); + } + [ExcelFunction(IsMacroType = true)] + static public object sparkalgo_register_quantity_parameter([ExcelArgument(AllowReference = true)] object parameter) + { + return sparkalgo_register_special_parameter(parameter, "quantity"); + } + [ExcelFunction(IsMacroType = true)] + static public object sparkalgo_register_tif_parameter([ExcelArgument(AllowReference = true)] object parameter) + { + return sparkalgo_register_special_parameter(parameter, "time_in_force"); + } + + [ExcelFunction(IsMacroType = true)] + static public object sparkalgo_register_special_parameter([ExcelArgument(AllowReference = true)] object parameter, string name) + { + ExcelReference par = (ExcelReference)parameter; + string sheetname = (string)XlCall.Excel(XlCall.xlSheetNm, par); + var cellref = new CellRef(sheetname, (par.ColumnFirst + 1, par.RowFirst + 1)); + string r1c1 = cellref.r1_c1(); + var cellname = XlCall.Excel(XlCall.xlfGetDef, r1c1, sheetname); + if (!(cellname is ExcelError)) + { + if(string.IsNullOrEmpty(name)) + name = cellname.ToString(); + Generate.register_special_parameter(cellname.ToString(), name); + return true; + } + else + return $"{ErrorValue.Name}, please make special parameters named ranges"; + } + + [ExcelFunction(IsMacroType = true)] + static public object sparkalgo_unregister_special_parameter(string parameter) + { + if (!string.IsNullOrEmpty(parameter)) + { + Generate.unregister_special_parameter(parameter); + return true; + } + else + return $"{ErrorValue.Name}, no special parameter named " + parameter; + } + [ExcelFunction] [SparkAlgoCode( dual_definition = @" - template < typename T > - T& sparkalgo_take_snapshot(T&, const std::string& name); +template < typename T > +T& sparkalgo_take_snapshot(T&, const std::string& name); ", dual_impl = @" - template - T& __AlgoName__::sparkalgo_take_snapshot(T& rng, const std::string& name) - { - static std::map< std::string, T > cache; - auto it = cache.find(name); - if(it == cache.end()) - { - cache[name] = rng; - return rng; - } - else - return it->second; - } +template +T& __AlgoName__::sparkalgo_take_snapshot(T& rng, const std::string& name) +{ + static std::map< std::string, T > cache; + auto it = cache.find(name); + if(it == cache.end()) + { + cache[name] = rng; + return rng; + } + else + return it->second; +} " )] static public object sparkalgo_take_snapshot(object data, string tag) @@ -83,10 +146,10 @@ namespace bts.excel [SparkAlgoCode( dual_definition = "int sparkalgo_snapshot_switch(const std::string& name, int on_off);", dual_impl = @" - int __AlgoName__::sparkalgo_snapshot_switch(const std::string& name, int on_off) - { - return sparkalgo_take_snapshot(on_off,name+""_switch""); - } +int __AlgoName__::sparkalgo_snapshot_switch(const std::string& name, int on_off) +{ + return sparkalgo_take_snapshot(on_off,name+""_switch""); +} " )] static public object sparkalgo_snapshot_switch(string tag, int on_off) @@ -121,7 +184,7 @@ namespace bts.excel [SparkAlgoCode( replacement_call = "sparkalgo_resolve_security_id()" )] - public async static void sparkalgo_resolve_security_id( + public async static void sparkalgo_resolve_primary_security_id( [ExcelArgument("Required. The specific contract, GCZ8 or ZNU8-ZNZ8, etc")] string contract, [ExcelArgument("Required. The symbol, GC or ZN, etc")] string symbol, [ExcelArgument("Optional. The type, Future or Spread, default = Future")] string security_type, @@ -145,15 +208,41 @@ namespace bts.excel } } + [ExcelFunction(Category = "BTS", Description = "Resolve a security id from contract details")] + [SparkAlgoCode()] + public async static void sparkalgo_resolve_security_id( + [ExcelArgument("Required. The specific contract, GCZ8 or ZNU8-ZNZ8, etc")] string contract, + [ExcelArgument("Required. The symbol, GC or ZN, etc")] string symbol, + [ExcelArgument("Optional. The type, Future or Spread, default = Future")] string security_type, + [ExcelArgument("Optional. The exchange, default = GLBX")] string exchange, + ExcelAsyncHandle handle) + { + exchange = string.IsNullOrEmpty(exchange) ? Security.ExchangeDataOneofCase.Glbx.ToString() : exchange; + Func where = MD.md_where(contract, symbol, security_type, exchange, out Venue ven); + var md = bts.excel.rtd.MDSubscribe.market_data(ven); + try + { + SecurityImmutable? sec = await md.find_security_async(where, TimeSpan.FromSeconds(15)); + if (sec != null && sec.HasValue) + handle.SetResult(sec.Value.Id); + else + handle.SetResult(0); + } + catch (Exception ex) + { + handle.SetException(ex); + } + } + [ExcelFunction(Category = "BTS", Description = "Subscribe to BTS Spark full depth market data by security_id")] [SparkAlgoCode( dual_definition = "ExcelBookAccessor sparkalgo_md_book_id(SecurityKey sk);", dual_impl = @" - ExcelBookAccessor __AlgoName__::sparkalgo_md_book_id(SecurityKey sk) - { - ExcelBookAccessor acc(me->last_book); - return acc; - }" +ExcelBookAccessor __AlgoName__::sparkalgo_md_book_id(SecurityKey sk) +{ + ExcelBookAccessor acc(me->last_book); + return acc; +}" )] public static object sparkalgo_md_book_id([ExcelArgument("Required. The security_id")] long security_id) { @@ -168,6 +257,23 @@ namespace bts.excel return x; })); } + + [ExcelFunction(Category = "BTS", IsMacroType = true)] + [SparkAlgoCode( + dual_definition = "uint sparkalgo_generate_local_orderid();", + dual_impl = @" +uint __AlgoName__::sparkalgo_generate_local_orderid() +{ + static uint id; + return ++id; +}" + )] + public static object sparkalgo_generate_local_orderid() + { + ExcelReference caller = XlCall.Excel(XlCall.xlfCaller) as ExcelReference; + CellRef rref = new CellRef(caller); + return rref.Row*1000 + rref.Column; + } } diff --git a/src/client/spark_excel/extensions/OrderService_Excel.cs b/src/client/spark_excel/extensions/OrderService_Excel.cs index 0acf70497..3538bb2e0 100644 --- a/src/client/spark_excel/extensions/OrderService_Excel.cs +++ b/src/client/spark_excel/extensions/OrderService_Excel.cs @@ -21,10 +21,11 @@ namespace bts.api.orderman readonly static Lazy _users = new Lazy(() => Locator.locate(), true); readonly static Lazy _admin = new Lazy(() => Locator.locate(), true); - struct OrderDef + class OrderDef { public Create create; - public core.Result response; + public string order_type; + public core.Result? response; } static ConcurrentDictionary _orders = new ConcurrentDictionary(); @@ -96,14 +97,12 @@ namespace bts.api.orderman handle.SetException(ex); } } - - static Tuple>> send_order( - long local_order_id, long security_key, string side, string user, string account, double price, - long quantity,string order_type, string order_params, string time_in_force) - { - if (_orders.ContainsKey(local_order_id)) - throw new ArgumentException("use a new local order id per order"); + + private static Create create_order(long security_key, string user, string account, string side, + double price, long quantity, string time_in_force, + string order_type, string order_params) + { Create cr = new Create(); cr.NewOrder = new OrderDefinition() { @@ -129,32 +128,34 @@ namespace bts.api.orderman cr.NewOrder.CustomDef = custom_def(order_type, order_params); if (cr.NewOrder.CustomDef == null || cr.NewOrder.CustomDef.CustomCase == CustomOrderDefinition.CustomOneofCase.None) throw new ArgumentException("Could not parse order_type and/or order_params info"); - + return cr; + } + + static Tuple>> send_order(Create cr) + { var resp = Tuple.Create(cr, _service.Value.create_order_async(cr)); return resp; } [ExcelFunction(Category = "BTS", Description = "Send an Order to Spark via security_key")] - public static async void spark_orderservice_send_order_key( - [ExcelArgument("Required. Local Order ID used to differentiate between create / update")] long local_order_id, - [ExcelArgument("Required. Security key")] long security_key, - [ExcelArgument("Required. Side BUY or SELL")] string side, + public static async void spark_orderservice_send_order_by_security_key( + [ExcelArgument("Required. Security key")] long security_key, [ExcelArgument("Required. User")] string user, [ExcelArgument("Required. Account")] string account, + [ExcelArgument("Required. Side BUY or SELL")] string side, [ExcelArgument("Required. Price")] double price, [ExcelArgument("Required. Quantity")] long quantity, - [ExcelArgument("Required. OrderType (Limit, Stop, Stop_Limit, Iceberg) - Default Limit")] string order_type, - [ExcelArgument("OPTIONAL. Order Params JSON (\"stop_price\" : 51.88)")] string order_params, - [ExcelArgument("OPTIONAL. (DAY,GTC,IOC) - Default DAY")] string time_in_force, + [ExcelArgument("OPTIONAL. OrderType (Limit, Stop, Stop_Limit, Iceberg) - Default Limit")] string order_type, + [ExcelArgument("OPTIONAL. Order Params JSON (\"stop_price\" : 51.88)")] string order_params, ExcelAsyncHandle handle) { try { - var order = send_order(local_order_id, security_key, side, user, account, price, quantity, order_type, order_params, time_in_force); + Create cr = create_order(security_key, user, account, side, price, quantity, order_type, order_params, time_in_force); + var order = send_order(cr); var resp = await order.Item2; - _orders[local_order_id] = new OrderDef { create = order.Item1, response = resp }; handle.SetResult(parse_response(resp, true)); } catch(Exception ex) @@ -187,7 +188,7 @@ namespace bts.api.orderman static CustomOrderDefinition custom_def(string order_type, string order_params) { var member = ReflectionExtensions.get_fields_or_properties(typeof(CustomOrderDefinition)) - .Where(x => x.Name.ToLower() == order_type.ToLower()).FirstOrDefault(); + .Where(x => x.Name.ToLower() == order_type?.ToLower()).FirstOrDefault(); if (member != null) { var mi = member.member_type(); @@ -203,62 +204,86 @@ namespace bts.api.orderman [ExcelFunction(Category = "BTS", Description = "Send or Modify or Cancel a Spark Order ")] [SparkAlgoCode] - public async static void sparkalgo_send_or_update_order( - [ExcelArgument("Required. Only send the order if > 1")] long on_off, - [ExcelArgument("Required. Local Order ID used to differentiate between create / update")] long local_order_id, - [ExcelArgument("Required. Security Desc (ZNZ9 or ZNZ9-ZNH0)")] long security_id, - [ExcelArgument("Required. Side BUY or SELL")] string side, + public static object sparkalgo_setup_order( + [ExcelArgument("Required. Security Key")] long local_order_id, + [ExcelArgument("Required. Security Key")] long security_key, [ExcelArgument("Required. User")] string user, [ExcelArgument("Required. Account")] string account, + [ExcelArgument("Required. Side BUY or SELL")] string side, [ExcelArgument("Required. Price")] double price, [ExcelArgument("Required. Quantity")] long quantity, - [ExcelArgument("Required. OrderType (Limit, Stop, Stop_Limit, Iceberg) - Default Limit")] string order_type, - [ExcelArgument("OPTIONAL. Order Params JSON (\"stop_price\" : 51.88)")] string order_params, - [ExcelArgument("OPTIONAL. (DAY,GTC,IOC) - Default DAY")] string time_in_force, + [ExcelArgument("OPTIONAL. OrderType (Limit, Stop, Stop_Limit, Iceberg) - Default Limit")] string order_type, + [ExcelArgument("OPTIONAL. Order Params JSON (\"stop_price\" : 51.88)")] string order_params) + { + if (_orders.ContainsKey(local_order_id)) + return local_order_id; + // throw new ArgumentException("use a new local order id per order"); + + Create cr = create_order(security_key, user, account, side, price, quantity, order_type, order_params, time_in_force); + _orders[local_order_id] = new OrderDef { create = cr, order_type = order_type, response = null}; + return local_order_id; + } + + [ExcelFunction(Category = "BTS", Description = "Send or Modify or Cancel a Spark Order ")] + [SparkAlgoCode] + public async static void sparkalgo_send_or_update_order( + [ExcelArgument("Required. Local Order returned from sparkalgo_setup_order")] long local_order_id, + [ExcelArgument("Required. Only process inputs for the order if > 1")] long should_process_inputs, + [ExcelArgument("Required. Price")] double price, + [ExcelArgument("Required. Quantity")] long quantity, + [ExcelArgument("OPTIONAL. Order Params JSON (\"stop_price\" : 51.88)")] string order_param_update, ExcelAsyncHandle handle) { try { - if (on_off <= 0) - handle.SetResult("this order is turned off"); - - if (_orders.TryGetValue(local_order_id, out OrderDef def) && def.response.value.OrderKey > 0) + if(should_process_inputs <= 0) + handle.SetResult("order off"); + if (!_orders.TryGetValue(local_order_id, out OrderDef def)) { - if (quantity > 0) - { - if (string.IsNullOrEmpty(order_type)) - order_type = "Limit"; - var cust = custom_def(order_type, order_params); - if (cust == null || cust.CustomCase == CustomOrderDefinition.CustomOneofCase.None) - handle.SetResult("Could not parse order_type and/or order_params info"); - if (def.create.Price != price || def.create.Quantity != Convert.ToUInt32(quantity) || - (cust != null && !cust.Equals(def.create.NewOrder.CustomDef))) - { - var resp = await update_order(def.response.value.OrderKey, price, quantity, cust); - if (resp.success && resp.value.ErrorCode == OrderError.Ok) - { - def.create.Price = price; - def.create.Quantity = Convert.ToUInt32(quantity); - } - handle.SetResult(parse_response(resp, true)); - } - else - handle.SetResult(parse_response(def.response, true)); - } - else - { - var resp = await cancel_order(def.response.value.OrderKey); - handle.SetResult(parse_response(resp, true)); - } + handle.SetResult("No order setup for id " + local_order_id); } else { - var order = send_order(local_order_id, security_id, side, user, account, price, quantity, order_type, order_params, time_in_force); - var resp = await order.Item2; - - _orders[local_order_id] = new OrderDef { create = order.Item1, response = resp }; - handle.SetResult(parse_response(resp, true)); + if(!def.response.HasValue) //is new + { + def.create.Price = price; + def.create.Quantity = Convert.ToUInt32(quantity); + var resp = await send_order(def.create).Item2; + if(resp.success) + def.response = resp; + handle.SetResult(parse_response(resp, true)); + } + else //is working + { + if (quantity > 0) + { + CustomOrderDefinition cust = null; + if(!string.IsNullOrEmpty(order_param_update)) + cust = custom_def(def.order_type, order_param_update); + + if (def.create.Price != price || def.create.Quantity != Convert.ToUInt32(quantity) || + (cust != null && !cust.Equals(def.create.NewOrder.CustomDef))) + { + var resp = await update_order(def.response.Value.value.OrderKey, price, quantity, cust); + if (resp.success && resp.value.ErrorCode == OrderError.Ok) + { + def.create.Price = price; + def.create.Quantity = Convert.ToUInt32(quantity); + } + handle.SetResult(parse_response(resp, true)); + } + else + handle.SetResult(parse_response(def.response.Value, true)); + } + else + { + var resp = await cancel_order(def.response.Value.value.OrderKey); + if(resp.success) + _orders.TryRemove(local_order_id, out _); //take out of working so we don't try to update it + handle.SetResult(parse_response(resp, true)); + } + } } } catch (Exception ex) diff --git a/src/sdk/examples/ExcelAlgoTemplate.cpp b/src/sdk/examples/ExcelAlgoTemplate.cpp index 18cef8b37..d6ac142cb 100644 --- a/src/sdk/examples/ExcelAlgoTemplate.cpp +++ b/src/sdk/examples/ExcelAlgoTemplate.cpp @@ -112,12 +112,12 @@ public: bool operator!=(const range_iterator& rhs) const {return row != rhs.row || col != rhs.col;} T& operator*() { - auto&& v = p(row, col); + auto& v = p(row, col); return v; } const T& operator*() const { - auto&& v = p(row, col); + const auto& v = p(row, col); return v; } }; @@ -143,12 +143,12 @@ struct Range const T& operator()(int r, int c) const { - auto&& v = rows[r][c]; + const auto& v = rows[r][c]; return v; } T& operator()(int r, int c) { - auto&& v = rows[r][c]; + auto& v = rows[r][c]; return v; } size_t size() const @@ -213,23 +213,73 @@ class __AlgoName__ : public IAlgoOrder IAlgoServices& algo_services() const; + void do_functions(); __FunctionDefs__; - void do_functions(); - SecurityKey sparkalgo_resolve_security_id(const string& contract = "", const string& symbol = "", const string& security_type = "", const string& exchange = ""); + const string& get_user() const; + const string& get_account() const; + bts::api::Side get_side() const; + Price get_price() const; + Quantity get_quantity() const; + bts::api::orderman::TimeInForce get_time_in_force() const; __DualDefs__; }; SecurityKey __AlgoName__::sparkalgo_resolve_security_id(const string& contract, const string& symbol, const string& security_type, const string& exchange) { + bts_assert_msg(contract == "", "ExcelAlgoTemplate doesn't support multiple contracts yet") auto sk = SecurityKey(algo_services().security().id()); return sk; } +const string& __AlgoName__::get_user() const +{ + return algo_services().definition().user(); +} +const string& __AlgoName__::get_account() const +{ + return algo_services().definition().account(); +} +bts::api::Side __AlgoName__::get_side() const +{ + return algo_services().definition().side(); +} +Price __AlgoName__::get_price() const +{ + return algo_services().price(); +} +Quantity __AlgoName__::get_quantity() const +{ + return algo_services().quantity(); +} +bts::api::orderman::TimeInForce __AlgoName__::get_time_in_force() const +{ + return algo_services().definition().time_in_force(); +} + +struct OrderDef +{ + OrderKey child_key; + bts::api::Side; + Price price; + Quantity quantity; +}; + +long sparkalgo_setup_order(long local_id, + SecurityKey security_id, + string side, + string user, + string account, + const string& order_type = "", + const string& order_params = "", + const string& time_in_force = "") +{ +} + void sparkalgo_send_or_update_order(long on_off, long local_order_id, SecurityKey security_id, @@ -242,6 +292,7 @@ void sparkalgo_send_or_update_order(long on_off, const string& order_params = "", const string& time_in_force = "") { + //me->child_key = services.launch_child(*me->child_template, algo_services().price(), algo_services().quantity()); } @@ -249,7 +300,7 @@ void sparkalgo_send_or_update_order(long on_off, struct __AlgoName__::PImpl { IAlgoServices *algo_services; - OrderKey child_key = null_order_key(); + std::map _local_order_id_to_child; ChildTemplate *child_template = nullptr; Book last_book; @@ -276,6 +327,7 @@ OrderOpResult __AlgoName__::do_before_launch(IBeforeLaunchAlgoServices& serv) // problem. If an error is returned here the order will be // abandoned. auto ¶ms = def.custom_def().dynamic().order_params(); + __ParameterLaunches__; if (!serv.is_attaching()) @@ -283,16 +335,29 @@ OrderOpResult __AlgoName__::do_before_launch(IBeforeLaunchAlgoServices& serv) // If we are not attaching, create a child template so we can // quickly launch our child when the time comes. - // We will be sending a limit order with the same side, + // By default, we will be sending a child order with the same side, // security, trader, account, etc, as this algo order, so just // copy our definition to the child definition to start with. auto child_def = def; + //TODO this could maybe change according to the algo? + + int defcount = 0; + for(auto& p : params) + { + if(p->has_as_order_def()) + { + bts_assert(++defcount == 1, "ExcelAlgoTemplate can't support multiple orderdef parameters right now"); + *child_def.mutable_custom_def()->CopyFrom(p->as_order_def()); + } + } // Now add a limit order component to mark our child as a limit - // order. Limit orders do not require any extra parameters + // order if we haven't received child order instructions in the parameters. + // Limit orders do not require any extra parameters // beyond those already inherited from this algo order // (i.e. beyond what we obtained through serv.definition()) so - // we can just use a default constructed LimitOrderDefinition. - *child_def.mutable_custom_def()->mutable_limit() = LimitOrderDefinition{}; + // we can just use a default constructed LimitOrderDefinition. + if(defcount == 0) + *child_def.mutable_custom_def()->mutable_limit() = LimitOrderDefinition{}; // Now just build the child template and cache it for future use me->child_template = &(serv.make_child_template(child_def)); } @@ -318,12 +383,10 @@ void __AlgoName__::do_launch(IAlgoServices& services) LOG(OrderMan, Diag, "Launching __AlgoName__ k: {} ", algo_services().order_key().value()); - // Launch our child order - me->child_key = services.launch_child(*me->child_template, algo_services().price(), algo_services().quantity()); - // Subscribe to market data if(__NeedsBook__) services.subscribe_book( sparkalgo_resolve_security_id() ); + do_functions(); } // Called in the latency sensitive path, once, when the algo is first @@ -342,18 +405,20 @@ void __AlgoName__::do_launch_attached(IAlgoServices& services, OrderKey attached algo_services().order_key().value(), attached_order.value()); // Cache the key of the attached order we will be managing - me->child_key = attached_order; + bts_assert_msg(_local_order_id_to_child.size() == 0, "ExcelAlgoTemplate Limit one child in attach case"); + _local_order_id_to_child[0] = OrderDef{ attached_order, services.definition().side(), services.price(), services.quantity() }; // Subscribe to market data if(__NeedsBook__) services.subscribe_book( sparkalgo_resolve_security_id() ); - + // Simulate a child change so we can update our own state to // reflect that of the child order we attached to. From this point // forward there is no distinction between an algo instance that // spawned a new child order and one that attached to an existing // order. on_child_change(attached_order, st); + do_functions(); } // Called when a human (or a supervising algo) updates a parameter for @@ -390,7 +455,8 @@ void __AlgoName__::do_update(const api::orderman::Update& update) Update child_update; child_update.set_price(update.price()); child_update.set_quantity(update.quantity()); - algo_services().update_child(me->child_key, child_update); + for(auto & child : _local_order_id_to_child) + algo_services().update_child(i->second.child_key, child_update); do_functions(); } @@ -465,7 +531,5 @@ extern "C" void __Remove__init_spark_plugin(bts::SparkServices& serv) __FunctionImpls__ - - } // namespace order_example } // namespace bts diff --git a/src/sdk/examples/excel_plugin_example.cpp b/src/sdk/examples/excel_plugin_example.cpp index 91448418a..e2809c501 100644 --- a/src/sdk/examples/excel_plugin_example.cpp +++ b/src/sdk/examples/excel_plugin_example.cpp @@ -112,12 +112,12 @@ public: bool operator!=(const range_iterator& rhs) const {return row != rhs.row || col != rhs.col;} T& operator*() { - auto&& v = p(row, col); + auto& v = p(row, col); return v; } const T& operator*() const { - auto&& v = p(row, col); + const auto& v = p(row, col); return v; } }; @@ -143,12 +143,12 @@ struct Range const T& operator()(int r, int c) const { - auto&& v = rows[r][c]; + const auto& v = rows[r][c]; return v; } T& operator()(int r, int c) { - auto&& v = rows[r][c]; + auto& v = rows[r][c]; return v; } size_t size() const @@ -213,28 +213,79 @@ class algo_example_xlsm : public IAlgoOrder IAlgoServices& algo_services() const; - void join_n31();; + void do_functions(); + void join_h31();; - void do_functions(); - SecurityKey sparkalgo_resolve_security_id(const string& contract = "", const string& symbol = "", const string& security_type = "", const string& exchange = ""); - ExcelBookAccessor sparkalgo_md_book_id(SecurityKey sk); + const string& get_user() const; + const string& get_account() const; + bts::api::Side get_side() const; + Price get_price() const; + Quantity get_quantity() const; + bts::api::orderman::TimeInForce get_time_in_force() const; + uint sparkalgo_generate_local_orderid(); +ExcelBookAccessor sparkalgo_md_book_id(SecurityKey sk); int sparkalgo_snapshot_switch(const std::string& name, int on_off); - template < typename T > - T& sparkalgo_take_snapshot(T&, const std::string& name); +template < typename T > +T& sparkalgo_take_snapshot(T&, const std::string& name); ; }; SecurityKey algo_example_xlsm::sparkalgo_resolve_security_id(const string& contract, const string& symbol, const string& security_type, const string& exchange) { + bts_assert_msg(contract == "", "ExcelAlgoTemplate doesn't support multiple contracts yet") auto sk = SecurityKey(algo_services().security().id()); return sk; } +const string& algo_example_xlsm::get_user() const +{ + return algo_services().definition().user(); +} +const string& algo_example_xlsm::get_account() const +{ + return algo_services().definition().account(); +} +bts::api::Side algo_example_xlsm::get_side() const +{ + return algo_services().definition().side(); +} +Price algo_example_xlsm::get_price() const +{ + return algo_services().price(); +} +Quantity algo_example_xlsm::get_quantity() const +{ + return algo_services().quantity(); +} +bts::api::orderman::TimeInForce algo_example_xlsm::get_time_in_force() const +{ + return algo_services().definition().time_in_force(); +} + +struct OrderDef +{ + OrderKey child_key; + bts::api::Side; + Price price; + Quantity quantity; +}; + +long sparkalgo_setup_order(long local_id, + SecurityKey security_id, + string side, + string user, + string account, + const string& order_type = "", + const string& order_params = "", + const string& time_in_force = "") +{ +} + void sparkalgo_send_or_update_order(long on_off, long local_order_id, SecurityKey security_id, @@ -247,6 +298,7 @@ void sparkalgo_send_or_update_order(long on_off, const string& order_params = "", const string& time_in_force = "") { + //me->child_key = services.launch_child(*me->child_template, algo_services().price(), algo_services().quantity()); } @@ -254,44 +306,46 @@ void sparkalgo_send_or_update_order(long on_off, struct algo_example_xlsm::PImpl { IAlgoServices *algo_services; - OrderKey child_key = null_order_key(); + std::map _local_order_id_to_child; ChildTemplate *child_template = nullptr; Book last_book; - string account; -double initial_price; -double quantity; -string side; -double join_thresh;; + double join_thresh;; }; - ExcelBookAccessor algo_example_xlsm::sparkalgo_md_book_id(SecurityKey sk) - { - ExcelBookAccessor acc(me->last_book); - return acc; - } +uint algo_example_xlsm::sparkalgo_generate_local_orderid() +{ + static uint id; + return ++id; +} - int algo_example_xlsm::sparkalgo_snapshot_switch(const std::string& name, int on_off) - { - return sparkalgo_take_snapshot(on_off,name+"_switch"); - } +ExcelBookAccessor algo_example_xlsm::sparkalgo_md_book_id(SecurityKey sk) +{ + ExcelBookAccessor acc(me->last_book); + return acc; +} + +int algo_example_xlsm::sparkalgo_snapshot_switch(const std::string& name, int on_off) +{ + return sparkalgo_take_snapshot(on_off,name+"_switch"); +} - template - T& algo_example_xlsm::sparkalgo_take_snapshot(T& rng, const std::string& name) - { - static std::map< std::string, T > cache; - auto it = cache.find(name); - if(it == cache.end()) - { - cache[name] = rng; - return rng; - } - else - return it->second; - } +template +T& algo_example_xlsm::sparkalgo_take_snapshot(T& rng, const std::string& name) +{ + static std::map< std::string, T > cache; + auto it = cache.find(name); + if(it == cache.end()) + { + cache[name] = rng; + return rng; + } + else + return it->second; +} algo_example_xlsm::algo_example_xlsm() : me(new PImpl()) {} @@ -312,51 +366,41 @@ OrderOpResult algo_example_xlsm::do_before_launch(IBeforeLaunchAlgoServices& ser // abandoned. auto ¶ms = def.custom_def().dynamic().order_params(); - auto account_param = params.find("account"); - if ( account_param == params.end() ) - return OrderOpResult{ ORDER_ERROR_INVALID_ORDER_DEFINITON,"account parameter must be defined"}; - me->account = account_param->second.as_string(); - - - auto initial_price_param = params.find("initial_price"); - if ( initial_price_param == params.end() ) - return OrderOpResult{ ORDER_ERROR_INVALID_ORDER_DEFINITON,"initial_price parameter must be defined"}; - me->initial_price = initial_price_param->second.as_double(); - - - auto quantity_param = params.find("quantity"); - if ( quantity_param == params.end() ) - return OrderOpResult{ ORDER_ERROR_INVALID_ORDER_DEFINITON,"quantity parameter must be defined"}; - me->quantity = quantity_param->second.as_double(); - - - auto side_param = params.find("side"); - if ( side_param == params.end() ) - return OrderOpResult{ ORDER_ERROR_INVALID_ORDER_DEFINITON,"side parameter must be defined"}; - me->side = side_param->second.as_string(); - - - auto join_thresh_param = params.find("join_thresh"); - if ( join_thresh_param == params.end() ) - return OrderOpResult{ ORDER_ERROR_INVALID_ORDER_DEFINITON,"join_thresh parameter must be defined"}; - me->join_thresh = join_thresh_param->second.as_double(); - ; + +auto join_thresh_param = params.find("join_thresh"); +if ( join_thresh_param == params.end() ) + return OrderOpResult{ ORDER_ERROR_INVALID_ORDER_DEFINITON,"join_thresh parameter must be defined"}; +me->join_thresh = join_thresh_param->second.as_double(); +; if (!serv.is_attaching()) { // If we are not attaching, create a child template so we can // quickly launch our child when the time comes. - // We will be sending a limit order with the same side, + // By default, we will be sending a child order with the same side, // security, trader, account, etc, as this algo order, so just // copy our definition to the child definition to start with. auto child_def = def; + //TODO this could maybe change according to the algo? + + int defcount = 0; + for(auto& p : params) + { + if(p->has_as_order_def()) + { + bts_assert(++defcount == 1, "ExcelAlgoTemplate can't support multiple orderdef parameters right now"); + *child_def.mutable_custom_def()->CopyFrom(p->as_order_def()); + } + } // Now add a limit order component to mark our child as a limit - // order. Limit orders do not require any extra parameters + // order if we haven't received child order instructions in the parameters. + // Limit orders do not require any extra parameters // beyond those already inherited from this algo order // (i.e. beyond what we obtained through serv.definition()) so - // we can just use a default constructed LimitOrderDefinition. - *child_def.mutable_custom_def()->mutable_limit() = LimitOrderDefinition{}; + // we can just use a default constructed LimitOrderDefinition. + if(defcount == 0) + *child_def.mutable_custom_def()->mutable_limit() = LimitOrderDefinition{}; // Now just build the child template and cache it for future use me->child_template = &(serv.make_child_template(child_def)); } @@ -382,12 +426,10 @@ void algo_example_xlsm::do_launch(IAlgoServices& services) LOG(OrderMan, Diag, "Launching algo_example_xlsm k: {} ", algo_services().order_key().value()); - // Launch our child order - me->child_key = services.launch_child(*me->child_template, algo_services().price(), algo_services().quantity()); - // Subscribe to market data if(__NeedsBook__) services.subscribe_book( sparkalgo_resolve_security_id() ); + do_functions(); } // Called in the latency sensitive path, once, when the algo is first @@ -406,18 +448,20 @@ void algo_example_xlsm::do_launch_attached(IAlgoServices& services, OrderKey att algo_services().order_key().value(), attached_order.value()); // Cache the key of the attached order we will be managing - me->child_key = attached_order; + bts_assert_msg(_local_order_id_to_child.size() == 0, "ExcelAlgoTemplate Limit one child in attach case"); + _local_order_id_to_child[0] = OrderDef{ attached_order, services.definition().side(), services.price(), services.quantity() }; // Subscribe to market data if(__NeedsBook__) services.subscribe_book( sparkalgo_resolve_security_id() ); - + // Simulate a child change so we can update our own state to // reflect that of the child order we attached to. From this point // forward there is no distinction between an algo instance that // spawned a new child order and one that attached to an existing // order. on_child_change(attached_order, st); + do_functions(); } // Called when a human (or a supervising algo) updates a parameter for @@ -448,57 +492,22 @@ void algo_example_xlsm::do_update(const api::orderman::Update& update) { auto ¶ms = update.custom().dynamic().order_params(); - auto account_param = params.find("account"); - if ( account_param == params.end() ) - { - algo_services().terminate_error("Can't find parameter account "); - return; - } - me->account = account_param->second.as_string(); - - - auto initial_price_param = params.find("initial_price"); - if ( initial_price_param == params.end() ) - { - algo_services().terminate_error("Can't find parameter initial_price "); - return; - } - me->initial_price = initial_price_param->second.as_double(); - - - auto quantity_param = params.find("quantity"); - if ( quantity_param == params.end() ) - { - algo_services().terminate_error("Can't find parameter quantity "); - return; - } - me->quantity = quantity_param->second.as_double(); - - - auto side_param = params.find("side"); - if ( side_param == params.end() ) - { - algo_services().terminate_error("Can't find parameter side "); - return; - } - me->side = side_param->second.as_string(); - - - auto join_thresh_param = params.find("join_thresh"); - if ( join_thresh_param == params.end() ) - { - algo_services().terminate_error("Can't find parameter join_thresh "); - return; - } - me->join_thresh = join_thresh_param->second.as_double(); - ; +auto join_thresh_param = params.find("join_thresh"); +if ( join_thresh_param == params.end() ) +{ + algo_services().terminate_error("Can't find parameter join_thresh "); + return; +} +me->join_thresh = join_thresh_param->second.as_double(); +; } // Update is good, forward it to our child Update child_update; child_update.set_price(update.price()); child_update.set_quantity(update.quantity()); - algo_services().update_child(me->child_key, child_update); + for(auto & child : _local_order_id_to_child) + algo_services().update_child(i->second.child_key, child_update); do_functions(); } @@ -531,7 +540,7 @@ void algo_example_xlsm::do_functions() my_side_psz.size(), my_side_psz.price(), other_side_psz.size(), other_side_psz.price() ); - join_n31();; + join_h31();; } // Our child reported a change: @@ -568,45 +577,31 @@ extern "C" void init_spark_plugin(bts::SparkServices& serv) // Associate the descriptor with a builder function and register // that with the Spark server. - DynamicOrderDescriptor algo_desc; - algo_desc.set_dynamic_order_type("algo_example_xlsm"); +DynamicOrderDescriptor algo_desc; +algo_desc.set_dynamic_order_type("algo_example_xlsm"); - auto account_param = algo_desc.add_param_desc(); - account_param->set_param_name("account"); - account_param->mutable_param_type()->set_primitive(DYNAMIC_ORDER_PRIMITIVE_STRING); - +auto join_thresh_param = algo_desc.add_param_desc(); +join_thresh_param->set_param_name("join_thresh"); +join_thresh_param->mutable_param_type()->set_primitive(DYNAMIC_ORDER_PRIMITIVE_DOUBLE); - auto initial_price_param = algo_desc.add_param_desc(); - initial_price_param->set_param_name("initial_price"); - initial_price_param->mutable_param_type()->set_primitive(DYNAMIC_ORDER_PRIMITIVE_DOUBLE); - - auto quantity_param = algo_desc.add_param_desc(); - quantity_param->set_param_name("quantity"); - quantity_param->mutable_param_type()->set_primitive(DYNAMIC_ORDER_PRIMITIVE_DOUBLE); - - - auto side_param = algo_desc.add_param_desc(); - side_param->set_param_name("side"); - side_param->mutable_param_type()->set_primitive(DYNAMIC_ORDER_PRIMITIVE_STRING); - - - auto join_thresh_param = algo_desc.add_param_desc(); - join_thresh_param->set_param_name("join_thresh"); - join_thresh_param->mutable_param_type()->set_primitive(DYNAMIC_ORDER_PRIMITIVE_DOUBLE); - - - serv.register_dynamic_algo_order(algo_desc, []() { return std::make_unique(); }); +serv.register_dynamic_algo_order(algo_desc, []() { return std::make_unique(); }); ; } -void algo_example_xlsm::join_n31() +void algo_example_xlsm::join_h31() { - auto&& join_thresh = me->join_thresh; + auto&& join_j8 = sparkalgo_generate_local_orderid ( ) ; auto&& security_id = sparkalgo_resolve_security_id() ; - auto&& join_c26 = ( join_thresh > 0.0 ? (sparkalgo_md_book_id ( security_id)(18,1)) : (0.0) ) ; + auto&& user = get_user(); + auto&& account = get_account(); + auto&& side = get_side(); + auto&& init_price = get_price(); + auto&& quantity = get_quantity(); + auto&& join_k8 = sparkalgo_setup_order ( join_j8, security_id, user, account, side, init_price, quantity) ; + auto&& join_c26 = sparkalgo_md_book_id ( security_id)(18,1) ; auto range_init = [](auto& initval, int nrows, int ncols) { @@ -616,159 +611,43 @@ auto range_init = [](auto& initval, int nrows, int ncols) }; -auto range_set = [](auto& range, auto& val, auto r, auto c) +auto range_set = [](auto& range, auto& val, int r, int c) { range(r,c) = val; }; auto&& Join_C26_C27 = range_init(join_c26, 2, 1); - auto&& join_c27 = ( join_thresh > 0.0 ? (sparkalgo_md_book_id ( security_id)(19,1)) : (0.0) ) ; + auto&& join_c27 = sparkalgo_md_book_id ( security_id)(19,1) ; range_set(Join_C26_C27, join_c27, 1, 0); - - auto Join_C26_C27_avg = 0.0; - for(auto& Join_C26_C27_i : Join_C26_C27 ) - Join_C26_C27_avg += Join_C26_C27_i; - Join_C26_C27_avg /= Join_C26_C27.size(); + + auto Join_C26_C27_avg = 0.0; + for(auto& Join_C26_C27_i : Join_C26_C27 ) + Join_C26_C27_avg += Join_C26_C27_i; + Join_C26_C27_avg /= Join_C26_C27.size(); auto&& join_c33 = Join_C26_C27_avg ; auto&& on_off = 1.0 + join_c33 ; - auto&& join_d31 = 1.0 ; - auto&& side = me->side; - auto&& join_g31 = side ; - auto&& user = std::string("bts") ; - auto&& join_h31 = user ; - auto&& account = me->account; - auto&& join_i31 = account ; - auto&& join_c18 = ( join_thresh > 0.0 ? (sparkalgo_md_book_id ( security_id)(10,1)) : (0.0) ) ; - auto&& join_c17 = ( join_thresh > 0.0 ? (sparkalgo_md_book_id ( security_id)(9,1)) : (0.0) ) ; - auto&& join_b8 = ( join_thresh > 0.0 ? (sparkalgo_md_book_id ( security_id)(0,0)) : (0.0) ) ; - auto&& Join_B8_D27 = range_init(join_b8, 20, 3); - auto&& join_c8 = ( join_thresh > 0.0 ? (sparkalgo_md_book_id ( security_id)(0,1)) : (0.0) ) ; - range_set(Join_B8_D27, join_c8, 0, 1); - auto&& join_d8 = ( join_thresh > 0.0 ? (sparkalgo_md_book_id ( security_id)(0,2)) : (0.0) ) ; - range_set(Join_B8_D27, join_d8, 0, 2); - auto&& join_b9 = ( join_thresh > 0.0 ? (sparkalgo_md_book_id ( security_id)(1,0)) : (0.0) ) ; - range_set(Join_B8_D27, join_b9, 1, 0); - auto&& join_c9 = ( join_thresh > 0.0 ? (sparkalgo_md_book_id ( security_id)(1,1)) : (0.0) ) ; - range_set(Join_B8_D27, join_c9, 1, 1); - auto&& join_d9 = ( join_thresh > 0.0 ? (sparkalgo_md_book_id ( security_id)(1,2)) : (0.0) ) ; - range_set(Join_B8_D27, join_d9, 1, 2); - auto&& join_b10 = ( join_thresh > 0.0 ? (sparkalgo_md_book_id ( security_id)(2,0)) : (0.0) ) ; - range_set(Join_B8_D27, join_b10, 2, 0); - auto&& join_c10 = ( join_thresh > 0.0 ? (sparkalgo_md_book_id ( security_id)(2,1)) : (0.0) ) ; - range_set(Join_B8_D27, join_c10, 2, 1); - auto&& join_d10 = ( join_thresh > 0.0 ? (sparkalgo_md_book_id ( security_id)(2,2)) : (0.0) ) ; - range_set(Join_B8_D27, join_d10, 2, 2); - auto&& join_b11 = ( join_thresh > 0.0 ? (sparkalgo_md_book_id ( security_id)(3,0)) : (0.0) ) ; - range_set(Join_B8_D27, join_b11, 3, 0); - auto&& join_c11 = ( join_thresh > 0.0 ? (sparkalgo_md_book_id ( security_id)(3,1)) : (0.0) ) ; - range_set(Join_B8_D27, join_c11, 3, 1); - auto&& join_d11 = ( join_thresh > 0.0 ? (sparkalgo_md_book_id ( security_id)(3,2)) : (0.0) ) ; - range_set(Join_B8_D27, join_d11, 3, 2); - auto&& join_b12 = ( join_thresh > 0.0 ? (sparkalgo_md_book_id ( security_id)(4,0)) : (0.0) ) ; - range_set(Join_B8_D27, join_b12, 4, 0); - auto&& join_c12 = ( join_thresh > 0.0 ? (sparkalgo_md_book_id ( security_id)(4,1)) : (0.0) ) ; - range_set(Join_B8_D27, join_c12, 4, 1); - auto&& join_d12 = ( join_thresh > 0.0 ? (sparkalgo_md_book_id ( security_id)(4,2)) : (0.0) ) ; - range_set(Join_B8_D27, join_d12, 4, 2); - auto&& join_b13 = ( join_thresh > 0.0 ? (sparkalgo_md_book_id ( security_id)(5,0)) : (0.0) ) ; - range_set(Join_B8_D27, join_b13, 5, 0); - auto&& join_c13 = ( join_thresh > 0.0 ? (sparkalgo_md_book_id ( security_id)(5,1)) : (0.0) ) ; - range_set(Join_B8_D27, join_c13, 5, 1); - auto&& join_d13 = ( join_thresh > 0.0 ? (sparkalgo_md_book_id ( security_id)(5,2)) : (0.0) ) ; - range_set(Join_B8_D27, join_d13, 5, 2); - auto&& join_b14 = ( join_thresh > 0.0 ? (sparkalgo_md_book_id ( security_id)(6,0)) : (0.0) ) ; - range_set(Join_B8_D27, join_b14, 6, 0); - auto&& join_c14 = ( join_thresh > 0.0 ? (sparkalgo_md_book_id ( security_id)(6,1)) : (0.0) ) ; - range_set(Join_B8_D27, join_c14, 6, 1); - auto&& join_d14 = ( join_thresh > 0.0 ? (sparkalgo_md_book_id ( security_id)(6,2)) : (0.0) ) ; - range_set(Join_B8_D27, join_d14, 6, 2); - auto&& join_b15 = ( join_thresh > 0.0 ? (sparkalgo_md_book_id ( security_id)(7,0)) : (0.0) ) ; - range_set(Join_B8_D27, join_b15, 7, 0); - auto&& join_c15 = ( join_thresh > 0.0 ? (sparkalgo_md_book_id ( security_id)(7,1)) : (0.0) ) ; - range_set(Join_B8_D27, join_c15, 7, 1); - auto&& join_d15 = ( join_thresh > 0.0 ? (sparkalgo_md_book_id ( security_id)(7,2)) : (0.0) ) ; - range_set(Join_B8_D27, join_d15, 7, 2); - auto&& join_b16 = ( join_thresh > 0.0 ? (sparkalgo_md_book_id ( security_id)(8,0)) : (0.0) ) ; - range_set(Join_B8_D27, join_b16, 8, 0); - auto&& join_c16 = ( join_thresh > 0.0 ? (sparkalgo_md_book_id ( security_id)(8,1)) : (0.0) ) ; - range_set(Join_B8_D27, join_c16, 8, 1); - auto&& join_d16 = ( join_thresh > 0.0 ? (sparkalgo_md_book_id ( security_id)(8,2)) : (0.0) ) ; - range_set(Join_B8_D27, join_d16, 8, 2); - auto&& join_b17 = ( join_thresh > 0.0 ? (sparkalgo_md_book_id ( security_id)(9,0)) : (0.0) ) ; - range_set(Join_B8_D27, join_b17, 9, 0); - range_set(Join_B8_D27, join_c17, 9, 1); - auto&& join_d17 = ( join_thresh > 0.0 ? (sparkalgo_md_book_id ( security_id)(9,2)) : (0.0) ) ; - range_set(Join_B8_D27, join_d17, 9, 2); - auto&& join_b18 = ( join_thresh > 0.0 ? (sparkalgo_md_book_id ( security_id)(10,0)) : (0.0) ) ; - range_set(Join_B8_D27, join_b18, 10, 0); - range_set(Join_B8_D27, join_c18, 10, 1); - auto&& join_d18 = ( join_thresh > 0.0 ? (sparkalgo_md_book_id ( security_id)(10,2)) : (0.0) ) ; - range_set(Join_B8_D27, join_d18, 10, 2); - auto&& join_b19 = ( join_thresh > 0.0 ? (sparkalgo_md_book_id ( security_id)(11,0)) : (0.0) ) ; - range_set(Join_B8_D27, join_b19, 11, 0); - auto&& join_c19 = ( join_thresh > 0.0 ? (sparkalgo_md_book_id ( security_id)(11,1)) : (0.0) ) ; - range_set(Join_B8_D27, join_c19, 11, 1); - auto&& join_d19 = ( join_thresh > 0.0 ? (sparkalgo_md_book_id ( security_id)(11,2)) : (0.0) ) ; - range_set(Join_B8_D27, join_d19, 11, 2); - auto&& join_b20 = ( join_thresh > 0.0 ? (sparkalgo_md_book_id ( security_id)(12,0)) : (0.0) ) ; - range_set(Join_B8_D27, join_b20, 12, 0); - auto&& join_c20 = ( join_thresh > 0.0 ? (sparkalgo_md_book_id ( security_id)(12,1)) : (0.0) ) ; - range_set(Join_B8_D27, join_c20, 12, 1); - auto&& join_d20 = ( join_thresh > 0.0 ? (sparkalgo_md_book_id ( security_id)(12,2)) : (0.0) ) ; - range_set(Join_B8_D27, join_d20, 12, 2); - auto&& join_b21 = ( join_thresh > 0.0 ? (sparkalgo_md_book_id ( security_id)(13,0)) : (0.0) ) ; - range_set(Join_B8_D27, join_b21, 13, 0); - auto&& join_c21 = ( join_thresh > 0.0 ? (sparkalgo_md_book_id ( security_id)(13,1)) : (0.0) ) ; - range_set(Join_B8_D27, join_c21, 13, 1); - auto&& join_d21 = ( join_thresh > 0.0 ? (sparkalgo_md_book_id ( security_id)(13,2)) : (0.0) ) ; - range_set(Join_B8_D27, join_d21, 13, 2); - auto&& join_b22 = ( join_thresh > 0.0 ? (sparkalgo_md_book_id ( security_id)(14,0)) : (0.0) ) ; - range_set(Join_B8_D27, join_b22, 14, 0); - auto&& join_c22 = ( join_thresh > 0.0 ? (sparkalgo_md_book_id ( security_id)(14,1)) : (0.0) ) ; - range_set(Join_B8_D27, join_c22, 14, 1); - auto&& join_d22 = ( join_thresh > 0.0 ? (sparkalgo_md_book_id ( security_id)(14,2)) : (0.0) ) ; - range_set(Join_B8_D27, join_d22, 14, 2); - auto&& join_b23 = ( join_thresh > 0.0 ? (sparkalgo_md_book_id ( security_id)(15,0)) : (0.0) ) ; - range_set(Join_B8_D27, join_b23, 15, 0); - auto&& join_c23 = ( join_thresh > 0.0 ? (sparkalgo_md_book_id ( security_id)(15,1)) : (0.0) ) ; - range_set(Join_B8_D27, join_c23, 15, 1); - auto&& join_d23 = ( join_thresh > 0.0 ? (sparkalgo_md_book_id ( security_id)(15,2)) : (0.0) ) ; - range_set(Join_B8_D27, join_d23, 15, 2); - auto&& join_b24 = ( join_thresh > 0.0 ? (sparkalgo_md_book_id ( security_id)(16,0)) : (0.0) ) ; - range_set(Join_B8_D27, join_b24, 16, 0); - auto&& join_c24 = ( join_thresh > 0.0 ? (sparkalgo_md_book_id ( security_id)(16,1)) : (0.0) ) ; - range_set(Join_B8_D27, join_c24, 16, 1); - auto&& join_d24 = ( join_thresh > 0.0 ? (sparkalgo_md_book_id ( security_id)(16,2)) : (0.0) ) ; - range_set(Join_B8_D27, join_d24, 16, 2); - auto&& join_b25 = ( join_thresh > 0.0 ? (sparkalgo_md_book_id ( security_id)(17,0)) : (0.0) ) ; - range_set(Join_B8_D27, join_b25, 17, 0); - auto&& join_c25 = ( join_thresh > 0.0 ? (sparkalgo_md_book_id ( security_id)(17,1)) : (0.0) ) ; - range_set(Join_B8_D27, join_c25, 17, 1); - auto&& join_d25 = ( join_thresh > 0.0 ? (sparkalgo_md_book_id ( security_id)(17,2)) : (0.0) ) ; - range_set(Join_B8_D27, join_d25, 17, 2); - auto&& join_b26 = ( join_thresh > 0.0 ? (sparkalgo_md_book_id ( security_id)(18,0)) : (0.0) ) ; - range_set(Join_B8_D27, join_b26, 18, 0); - range_set(Join_B8_D27, join_c26, 18, 1); - auto&& join_d26 = ( join_thresh > 0.0 ? (sparkalgo_md_book_id ( security_id)(18,2)) : (0.0) ) ; - range_set(Join_B8_D27, join_d26, 18, 2); - auto&& join_b27 = ( join_thresh > 0.0 ? (sparkalgo_md_book_id ( security_id)(19,0)) : (0.0) ) ; - range_set(Join_B8_D27, join_b27, 19, 0); - range_set(Join_B8_D27, join_c27, 19, 1); - auto&& join_d27 = ( join_thresh > 0.0 ? (sparkalgo_md_book_id ( security_id)(19,2)) : (0.0) ) ; - range_set(Join_B8_D27, join_d27, 19, 2); - auto&& join_g18 = ( join_c17 > 0.0 ? (sparkalgo_take_snapshot ( Join_B8_D27, std::string("book"))(10,1)) : (0.0) ) ; - auto&& join_l31 = sparkalgo_snapshot_switch ( std::string("should_join"), ( (join_c18 > join_g18 && join_b18 > join_thresh && true) ? (1.0) : (0.0) ) ) ; - auto&& initial_price = me->initial_price; - auto&& join_j31 = ( join_l31 ? (sparkalgo_take_snapshot ( join_c18, std::string("join_price"))) : (initial_price) ) ; - auto&& quantity = me->quantity; - auto&& join_k31 = quantity ; - sparkalgo_send_or_update_order ( on_off, join_d31, security_id, join_g31, join_h31, join_i31, join_j31, join_k31) ; + auto&& join_c18 = sparkalgo_md_book_id ( security_id)(10,1) ; + auto&& join_c17 = sparkalgo_md_book_id ( security_id)(9,1) ; + auto&& join_b17 = sparkalgo_md_book_id ( security_id)(9,0) ; + auto&& Join_B17_D18 = range_init(join_b17, 2, 3); + range_set(Join_B17_D18, join_c17, 0, 1); + auto&& join_d17 = sparkalgo_md_book_id ( security_id)(9,2) ; + range_set(Join_B17_D18, join_d17, 0, 2); + auto&& join_b18 = sparkalgo_md_book_id ( security_id)(10,0) ; + range_set(Join_B17_D18, join_b18, 1, 0); + range_set(Join_B17_D18, join_c18, 1, 1); + auto&& join_d18 = sparkalgo_md_book_id ( security_id)(10,2) ; + range_set(Join_B17_D18, join_d18, 1, 2); + auto&& join_g18 = ( join_c17 > 0.0 ? (sparkalgo_take_snapshot ( Join_B17_D18, std::string("book"))(1,1)) : (0.0) ) ; + auto&& join_thresh = me->join_thresh; + auto&& join_f31 = sparkalgo_snapshot_switch ( std::string("should_join"), ( (join_c18 > join_g18 && join_b18 > join_thresh && true) ? (1.0) : (0.0) ) ) ; + auto&& join_d31 = ( join_f31 ? (sparkalgo_take_snapshot ( join_c18, std::string("join_price"))) : (init_price) ) ; + auto&& join_e31 = quantity ; + sparkalgo_send_or_update_order ( join_k8, on_off, join_d31, join_e31) ; } - - } // namespace order_example } // namespace bts