diff --git a/src/client/spark_analytics/VegaFactor.cs b/src/client/spark_analytics/VegaFactor.cs index c537ca263..d599fcc5a 100644 --- a/src/client/spark_analytics/VegaFactor.cs +++ b/src/client/spark_analytics/VegaFactor.cs @@ -19,6 +19,23 @@ namespace bts.analytics return vol; } + public static double interpolate_vol_array(double[] x, double[] y, double x_val) + { + if (y == null || x == null || x.Length == 0 || y.Length == 0 || x.Length != y.Length) return double.NaN; + int npts = x.Length; + if (npts < 3) return Interpolation.interp_linear(x_val, x, y); + int klo = 0, khi = npts - 1, k = 0; + while (khi - klo > 1) + { + k = (khi + klo) >> 1; + if (x[k] > x_val) + khi = k; + else + klo = k; + } + return interpolate_vol(x[klo], y[klo], x[khi], y[khi], x_val); + } + public static double interpolate_vol(double t1, double vol1, double t2, double vol2, double t) { if (t1 == t2 || t1 > t) diff --git a/src/client/spark_excel/ExcelAddin.cs b/src/client/spark_excel/ExcelAddin.cs index 421639146..b78bed26d 100644 --- a/src/client/spark_excel/ExcelAddin.cs +++ b/src/client/spark_excel/ExcelAddin.cs @@ -12,6 +12,7 @@ using System.Diagnostics; using ExcelDna.Integration; using ExcelDna.IntelliSense; using ExcelDna.Logging; +using ExcelDna.Registration; namespace bts.excel { @@ -54,6 +55,11 @@ namespace bts.excel Trace.Listeners.Add(new LogDisplayTraceListener("bts")); IntelliSenseServer.Install(); ExcelIntegration.RegisterUnhandledExceptionHandler(ex => ex.ToString()); + + //https://github.com/Excel-DNA/ExcelDna/issues/255 + ExcelRegistration.GetExcelFunctions() + .ProcessAsyncRegistrations(nativeAsyncIfAvailable: false) + .RegisterFunctions(); load_assemblies(); ExcelComAddInHelper.LoadComAddIn(new ContextTeardown(APIDispatch.service_context.Value)); diff --git a/src/client/spark_excel/analytics/AnalyticsWrappers.cs b/src/client/spark_excel/analytics/AnalyticsWrappers.cs index 8b5493cb2..41d53d8b8 100644 --- a/src/client/spark_excel/analytics/AnalyticsWrappers.cs +++ b/src/client/spark_excel/analytics/AnalyticsWrappers.cs @@ -14,6 +14,11 @@ namespace bts.excel { public class AnalyticsWrappers { + [ExcelFunction] + public static double bts_interpolate_vol(double[] t, double[] vol, double x) + { + return VegaFactor.interpolate_vol_array(t, vol, x); + } [ExcelFunction] static public double bts_expected_move(double spot, double growth, double vol, double bizt, double calt) diff --git a/src/client/spark_excel/bts_arrival_price.xlsx b/src/client/spark_excel/bts_arrival_price.xlsx index 1be23b62b..eb80e495d 100644 Binary files a/src/client/spark_excel/bts_arrival_price.xlsx and b/src/client/spark_excel/bts_arrival_price.xlsx differ diff --git a/src/client/spark_excel/extensions/OrderService_Excel.cs b/src/client/spark_excel/extensions/OrderService_Excel.cs index 09ba73298..a08c5e0d5 100644 --- a/src/client/spark_excel/extensions/OrderService_Excel.cs +++ b/src/client/spark_excel/extensions/OrderService_Excel.cs @@ -11,7 +11,7 @@ using bts.excel; using bts.excel.extensions; using bts.excel.rtd; using Newtonsoft.Json; - +using System.Threading.Tasks; namespace bts.api.orderman { @@ -21,7 +21,7 @@ namespace bts.api.orderman readonly static Lazy _admin = new Lazy(() => Locator.locate(), true); [ExcelFunction(Category = "BTS", Description = "Get All Accessible Accounts")] - async public static void spark_get_accounts(string login_token, ExcelAsyncHandle handle) + async public static Task spark_get_accounts(string login_token) { try { @@ -34,11 +34,11 @@ namespace bts.api.orderman accts.Add(acc.Name); } var res = Utilities.make_column_vector(accts.ToArray()); - handle.SetResult(res); + return res; } catch (Exception ex) { - handle.SetException(ex); + return ex.ToString(); } } diff --git a/src/client/spark_excel/rtd/subscriptions/MDSubscribe.cs b/src/client/spark_excel/rtd/subscriptions/MDSubscribe.cs index ea3d81c79..93a449ee9 100644 --- a/src/client/spark_excel/rtd/subscriptions/MDSubscribe.cs +++ b/src/client/spark_excel/rtd/subscriptions/MDSubscribe.cs @@ -8,14 +8,14 @@ using ExcelDna.Registration.Utils; using System.Reactive.Linq; using bts.api; using bts.md; -using bts.api.orderman; +using System.Threading.Tasks; namespace bts.excel.rtd { public class MDSubscribe { [ExcelFunction(Category = "BTS", Description = "Log in to BTS Spark")] - async public static void spark_login(string login, string password, ExcelAsyncHandle handle) + async public static Task spark_login(string login, string password) { try { @@ -23,14 +23,14 @@ namespace bts.excel.rtd if (res.success && Locator.token.HasValue) { _md.set_auth_token(Locator.token); - handle.SetResult("Login Successful"); + return ("Login Successful"); } else - handle.SetResult("Login Error: Could not log in check user/pass " + res.reason); + return ("Login Error: Could not log in check user/pass " + res.reason); } catch(Exception ex) { - handle.SetException(ex); + return ("Login Exception: " + ex.ToString()); } } diff --git a/src/client/spark_excel/spark_excel-addin.dna b/src/client/spark_excel/spark_excel-addin.dna index 810b9922e..6437fe88f 100644 --- a/src/client/spark_excel/spark_excel-addin.dna +++ b/src/client/spark_excel/spark_excel-addin.dna @@ -1,5 +1,5 @@ - + diff --git a/src/client/spark_excel/spark_excel.csproj b/src/client/spark_excel/spark_excel.csproj index 57c13abbf..54311079d 100644 --- a/src/client/spark_excel/spark_excel.csproj +++ b/src/client/spark_excel/spark_excel.csproj @@ -29,10 +29,10 @@ - - - - + + + + @@ -75,10 +75,10 @@ --> - - + + diff --git a/src/lib/analytics/AlmgrenChriss.cpp b/src/lib/analytics/AlmgrenChriss.cpp index 783463126..4bbd28c1b 100644 --- a/src/lib/analytics/AlmgrenChriss.cpp +++ b/src/lib/analytics/AlmgrenChriss.cpp @@ -47,32 +47,28 @@ CAPI double liquidity_cost(double qty, double tick_size, double* bookprices, int CAPI double ac_xt(double lambda, double vol, double dt, /*days*/ double T, double s0, double st, double x0, double cost, double adaptivity, int generation, int ngenerations, bool is_buy) { - if(generation < ngenerations) - { - //days - if(T < dt) - T = dt; - double time_factor = 365.0; - double sigma = st * vol/ sqrt(time_factor / dt); - //double nn = algo().quantity().value() - total_filled().value(); - double nn = x0; - //eta is in ($/share) / (share/period) - double eta = dt * cost / nn; - double k = sqrt( fabs(lambda) * sigma * sigma / eta ); - //=IF(B42<=N, U41*SINH(K42*(T-dt*B42))/SINH(K42*(T-dt*B41)),0) - double x1 = x0 * sinh(k * (T-dt*generation) ) / sinh(k * T-dt*(generation-1)); - double alpha = ::log(st/s0) * s0 / time_factor; - if(is_buy) - alpha = ::log(s0/st) * s0 / time_factor; - double x_bar = adaptivity * alpha / (2.0 * lambda * sigma * sigma); - double alpha_correct = x_bar * ( 1.0 - ( sinh(k*(T-dt*generation) ) + sinh(k*dt) ) / sinh( k * (T) ) ); - //=-IF(B42<=N, AA42*(1-(SINH(K42*(T-dt*B42))+SINH(K42*dt))/SINH(K42*(T))),0) - double x1_adapt = std::min( std::max( alpha_correct + x1, 0.0), x0); - return x1_adapt; - } - else - return x0; + if(generation >= ngenerations) + return x0; + //days + if(T < dt) + T = dt; + double time_factor = 365.0; + double sigma = st * vol/ sqrt(time_factor / dt); + //double nn = algo().quantity().value() - total_filled().value(); + double nn = x0; + //eta is in ($/share) / (share/period) + double eta = dt * cost / nn; + double k = sqrt( fabs(lambda) * sigma * sigma / eta ); + //=IF(B42<=N, U41*SINH(K42*(T-dt*B42))/SINH(K42*(T-dt*B41)),0) + double x1 = x0 * sinh(k * (T-dt*generation) ) / sinh(k * T-dt*(generation-1)); + double alpha = ::log(st/s0) * s0 / time_factor; + if(is_buy) + alpha = ::log(s0/st) * s0 / time_factor; + double x_bar = adaptivity * alpha / (2.0 * lambda * sigma * sigma); + double alpha_correct = x_bar * ( 1.0 - ( sinh(k*(T-dt*generation) ) + sinh(k*dt) ) / sinh( k * (T) ) ); + //=-IF(B42<=N, AA42*(1-(SINH(K42*(T-dt*B42))+SINH(K42*dt))/SINH(K42*(T))),0) + double x1_adapt = std::min( std::max( alpha_correct + x1, 0.0), x0); + return x1_adapt; } - }} diff --git a/src/lib/api/service_compiler/templates/excel/excel.pb.cs b/src/lib/api/service_compiler/templates/excel/excel.pb.cs index f10b33582..9ae171f1b 100644 --- a/src/lib/api/service_compiler/templates/excel/excel.pb.cs +++ b/src/lib/api/service_compiler/templates/excel/excel.pb.cs @@ -12,7 +12,7 @@ using bts.api.orderman; using bts.co2; using bts.excel; using bts.excel.rtd; - +using System.Threading.Tasks; using ExcelDna.Integration; using ExcelDna.Integration.Rtd; @@ -85,7 +85,7 @@ public partial class {{name}}_Excel : IDisposable {{#unless skip_excel_generation}} [ExcelFunction(Category = "BTS", Description = "Call BTS Spark {{../lname}} API Method {{lname}} with parameter {{input_type_full}}")] - async public static void spark_{{../lname}}_{{lname}}_async (object[,] parameters, ExcelAsyncHandle handle, bool show_headers = false, bool display_vertically = false) + async public static Task spark_{{../lname}}_{{lname}}_async (object[,] parameters, bool show_headers = false, bool display_vertically = false) { try { @@ -112,7 +112,7 @@ public partial class {{name}}_Excel : IDisposable var members = bts.excel.extensions.ReflectionExtensions .get_fields_or_properties(typeof({{input_type_full}})) .Where(x => !DataParser.excluded_member(x)); - handle.SetResult( + return ( "Error in calling method {{lname}}( {{input_type_full}} ), input members are: " + string.Join(",", members.Select(x => x.Name).ToArray()) ); @@ -123,12 +123,13 @@ public partial class {{name}}_Excel : IDisposable object[,] scattered = new JsonFormatter(res_res).format(show_headers); if(display_vertically) scattered = bts.excel.Utilities.transpose_keep_dimension(scattered); - handle.SetResult(scattered); + return (scattered); } catch(Exception ex) { Trace.TraceError(ex.ToString()); - handle.SetException(ex); + //handle.SetException(ex); + return ex.ToString(); } }