generator looking good

This commit is contained in:
Kevin Darby committed 2019-07-15 10:55:19 -04:00
1 parent d5427f6c24
commit 88494eb0fc
5 files changed
+144 -54

No files matched your search

+12 -38
View File
@@ -15,9 +15,11 @@ using namespace bts::orderman;
#define __AlgoName__ "_";
#define __FunctionDefs__
#define __FunctionImpls__
#define __FunctionCalls__
#define __ParameterDefs__
#define __ParameterImpls__ do {} while(0);
#define __ParameterLaunches__
#define __ParameterSetups__ do {} while(0);
#define __ParameterUpdates__ do {} while(0);
#define __NeedsBook__ true
#define __NeedsTrades__ true
@@ -83,12 +85,7 @@ OrderOpResult __AlgoName__::do_before_launch(IBeforeLaunchAlgoServices &serv)
// problem. If an error is returned here the order will be
// abandoned.
auto &params = def.custom_def().dynamic().order_params();
//TODO params
auto mqi = params.find("Min Quantity");
if ( mqi == params.end() )
return OrderOpResult{ ORDER_ERROR_INVALID_ORDER_DEFINITON, "Min Quantity parameter must be defined and non zero"};
me->min_quantity_to_join = Quantity(mqi->second.as_uint32());
__ParameterLaunches__;
if (!serv.is_attaching())
{
@@ -196,16 +193,7 @@ void __AlgoName__::do_update(api::orderman::Update const &update)
if (update.has_custom())
{
auto &params = update.custom().dynamic().order_params();
__ParameterUpdates__;
auto mqi = params.find("Min Quantity");
if (mqi == params.end() || mqi->second.as_uint32() == 0)
{
algo_services().terminate_error("Minimum quantity to join must be greater than 0");
return;
}
has_new_param = true;
me->min_quantity_to_join = Quantity(mqi->second.as_uint32());
__ParameterUpdates__;
}
// Update is good, forward it to our child
@@ -234,8 +222,6 @@ void __AlgoName__::on_book(SecurityKey, Book const &b)
do_functions();
}
// Given the current book, our target price and our min quantity
// parameter, join if appropriate
void __AlgoName__::do_functions()
{
auto my_side = algo_services().definition().side();
@@ -251,7 +237,7 @@ void __AlgoName__::do_functions()
my_side_psz.size(), my_side_psz.price(),
other_side_psz.size(), other_side_psz.price()
);
__FunctionCalls__;
}
// Our child reported a change:
@@ -278,34 +264,22 @@ void __AlgoName__::on_child_change(OrderKey, ChildState const &st)
}
}
__FunctionImpls__
// This is called by the spark daemon at startup. In this call we
// describe our algo's parameters and register a factory function to
// create an instance of the algo.
extern "C" void init_spark_plugin(bts::SparkServices &serv)
{
using namespace bts;
DynamicOrderDescriptor algo_desc;
// Start by naming our order type
algo_desc.set_dynamic_order_type("__AlgoName__");
// Now add a single unsigned int parameter, Min Quantity, that will
// determine the minimum quantity that must exist in the book in
// order for us to join the market.
//TODO params
__ParameterImpls__;
auto param = algo_desc.add_param_desc();
param->set_param_name("Min Quantity");
param->mutable_param_type()->set_primitive(DYNAMIC_ORDER_PRIMITIVE_UINT);
__ParameterSetups__;
// Associate the descriptor with a builder function and register
// that with the Spark server.
serv.register_dynamic_algo_order(algo_desc, []() { return std::make_unique<__AlgoName__>(); });
}
__FunctionImpls__
} // namespace order_example
} // namespace bts
+127 -5
View File
@@ -33,6 +33,13 @@ namespace bts.excel
public class Generate
{
static ConcurrentDictionary<string, string> _algo_params = new ConcurrentDictionary<string, string>();
public static IReadOnlyDictionary<string, string> algo_params => _algo_params;
public static void register_algo_parameter(string type, string name)
{
_algo_params[name] = string.IsNullOrEmpty(type) ? "double" : type.ToLower();
}
static public string bts_generate_algo_class(string algoname, Dictionary<string, string> functions)
{
var ass = Assembly.GetExecutingAssembly();
@@ -46,28 +53,143 @@ namespace bts.excel
string cpp = reader.ReadToEnd();
cpp = cpp.Replace("#define __AlgoName__", "//#define __#AlgoName__");
cpp = cpp.Replace("#define __FunctionDefs__", "//#define __#FunctionDefs__");
cpp = cpp.Replace("#define __FunctionImpls__", "//#define __#FunctionImpls__");
cpp = cpp.Replace("#define __FunctionCalls__", "//#define __#FunctionCalls__");
cpp = cpp.Replace("#define __FunctionImpls__", "//#define __#FunctionImpls__");
cpp = cpp.Replace("#define __ParameterDefs__", "//#define __#ParameterDefs__");
cpp = cpp.Replace("#define __ParameterImpls__", "//#define __#ParameterImpls__");
cpp = cpp.Replace("#define __FunctionImpls__", "//#define __#FunctionImpls__");
cpp = cpp.Replace("#define __ParameterLaunches__", "//#define __#ParameterLaunches__");
cpp = cpp.Replace("#define __ParameterUpdates__", "//#define __#ParameterUpdates__");
cpp = cpp.Replace("#define __ParameterSetups__", "//#define __#ParameterSetups__");
cpp = cpp.Replace("__AlgoName__", algoname);
string funcdefs = string.Join(Environment.NewLine, functions.Keys.Select(x => $"void {x}();"));
cpp = cpp.Replace("__FunctionDefs__", funcdefs);
string funccalls = string.Join(Environment.NewLine, functions.Keys.Select(x => x + "();" ));
cpp = cpp.Replace("__FunctionCalls__", funccalls);
string funcimpls = string.Join(Environment.NewLine, functions.Values.Select(x => x.Replace("__AlgoName__", algoname) ));
cpp = cpp.Replace("__FunctionImpls__", funcimpls);
//__ParameterDefs__
string paramdefs = string.Join(Environment.NewLine, _algo_params.Select( kv => kv.Value + " " + kv.Key + ";"));
cpp = cpp.Replace("__ParameterDefs__", paramdefs);
//__ParameterLaunches__
{
List<string> launches = new List<string>();
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} = mqi->second.{get_as(kv.Key, kv.Value)}();
";
launches.Add(launch);
}
string paramlaunch = string.Join(Environment.NewLine, launches);
cpp = cpp.Replace("__ParameterLaunches__", paramlaunch);
}
//__ParameterUpdates__
{
List<string> updates = new List<string>();
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;
}}
has_new_param = true;
me->{kv.Key} = mqi->second.{get_as(kv.Key, kv.Value)}();
";
updates.Add(update);
}
string paramupdates = string.Join(Environment.NewLine, updates);
cpp = cpp.Replace("__ParameterUpdates__", paramupdates);
}
//__ParameterSetups__
{
List<string> setups = new List<string>();
setups.Add($@"
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)});
";
setups.Add(setup);
}
setups.Add($@"
serv.register_dynamic_algo_order(algo_desc, []() {{ return std::make_unique<{algoname}>(); }});
");
string paramsetups = string.Join(Environment.NewLine, setups);
cpp = cpp.Replace("__ParameterSetups__", paramsetups);
}
string cppname = try_write_file(algoname + ".cpp", cpp);
return cppname;
}
}
}
static private string get_as(string paramname, string paramtype)
{
switch (paramtype.ToLower())
{
case "int":
return "as_int32";
case "uint":
return "as_uint32";
case "double":
return "as_double";
case "string":
return "as_string";
case "bool":
return "as_bool";
case "order":
case "orderdef":
return "as_order_def";
default:
throw new ArgumentException("unknown type " + paramname + " " + paramtype);
}
}
static private string get_param_type_enum(string paramname, string paramtype)
{
switch(paramtype.ToLower())
{
case "int":
return "DYNAMIC_ORDER_PRIMITIVE_INT";
case "uint":
return "DYNAMIC_ORDER_PRIMITIVE_UINT";
case "double":
return "DYNAMIC_ORDER_PRIMITIVE_DOUBLE";
case "string":
return "DYNAMIC_ORDER_PRIMITIVE_STRING";
case "bool":
return "DYNAMIC_ORDER_PRIMITIVE_BOOL";
case "order":
case "orderdef":
return "DYNAMIC_ORDER_PRIMITIVE_ORDER_DEF";
default:
throw new ArgumentException("unknown type " + paramname + " " + paramtype);
}
}
//[ExcelFunction(IsMacroType = true, IsThreadSafe = false)]
//we can't call this as a UDF or the array / selectspecial thing doesn't always work
static public string bts_generate_code(ExcelReference rref, ref string function_name)
static public string bts_generate_function_code(ExcelReference rref, ref string function_name)
{
var formula = XlCall.Excel(XlCall.xlfGetCell, 41, rref);
@@ -75,17 +75,10 @@ namespace bts.excel
var cref = new CellRef(rref);
_document_name = (string)XlCall.Excel(XlCall.xlfGetDocument, 88, cref.SheetName);
}
public string classname => CellRef.stringize_workbook( _document_name );
#region Context visitors overrides
static ConcurrentDictionary<string, string> _algo_params = new ConcurrentDictionary<string, string>();
public static void register_algo_parameter(string type, string name)
{
_algo_params[name] = type;
}
public override object VisitName(ExcelFormulaParser.NameContext context)
{
var name = context.GetText();
@@ -403,7 +396,7 @@ auto vector_push_back = [](auto& vector, auto& val) -> auto&
{
def = $"auto&& {cellref.ToString()} = {new GeneratorVisitor(rref, _defs).VisitMain(parsetree)} ;";
_defs.add_def(def);
if (named && _algo_params.ContainsKey(cellref.named_range))
if (named && Generate.algo_params.ContainsKey(cellref.named_range))
{
_defs.map_def(def, $"auto&& {cellref.named_range} = me->{cellref.named_range};");
}
@@ -47,6 +47,7 @@ namespace bts.excel.ribbon
string document_name = string.Empty;
Dictionary<string, string> functions = new Dictionary<string, string>();
// ExcelAsyncUtil.QueueAsMacro( () =>
MessageBox.Show(wind, "About to generate code, pretend you see a progress bar and click ok");
foreach(var cell in app.Selection as Excel.Range)
{
ExcelReference rref = RangeHelper.RangeToExcelReference(cell as Excel.Range);
@@ -57,7 +58,7 @@ namespace bts.excel.ribbon
document_name = (string)XlCall.Excel(XlCall.xlfGetDocument, 88, cellref.SheetName);
string fname = null;
string f = bts.excel.Generate.bts_generate_code(rref, ref fname);
string f = bts.excel.Generate.bts_generate_function_code(rref, ref fname);
functions[fname] = f;
}
if(functions.Count > 0)
+1 -1
View File
@@ -25,7 +25,7 @@ namespace bts.excel
var cellname = XlCall.Excel(XlCall.xlfGetDef, r1c1, sheetname);
if (!(cellname is ExcelError))
{
GeneratorVisitor.register_algo_parameter(type ?? "double", cellname.ToString());
Generate.register_algo_parameter(type ?? "double", cellname.ToString());
return true;
}
else