185 lines
8.9 KiB
C#
185 lines
8.9 KiB
C#
using System;
|
|
using System.Collections.Concurrent;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using ExcelDna.Integration;
|
|
using ExcelDna.Integration.Rtd;
|
|
using ExcelDna.Registration.Utils;
|
|
|
|
using bts.api.admin;
|
|
using bts.excel;
|
|
using bts.excel.extensions;
|
|
using bts.excel.rtd;
|
|
using Newtonsoft.Json;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace bts.api.orderman
|
|
{
|
|
public partial class OrderService_Excel
|
|
{
|
|
readonly static Lazy<user.AuthService> _users = new Lazy<user.AuthService>(() => Locator.locate<user.AuthService>(), true);
|
|
readonly static Lazy<admin.AdminService> _admin = new Lazy<admin.AdminService>(() => Locator.locate<admin.AdminService>(), true);
|
|
|
|
[ExcelFunction(Category = "BTS", Description = "Get All Accessible Accounts")]
|
|
async public static Task<object> spark_get_accounts(string login_token)
|
|
{
|
|
try
|
|
{
|
|
List<string> accts = new List<string>();
|
|
var admin = await _admin.Value.get_filtered_admin_data_async(new FilteredAdminDataRequest());
|
|
if (admin.success)
|
|
{
|
|
foreach (var acc in admin.value.Accounts)
|
|
if(acc.IsPresent)
|
|
accts.Add(acc.Name);
|
|
}
|
|
var res = Utilities.make_column_vector(accts.ToArray());
|
|
return res;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ex.ToString();
|
|
}
|
|
}
|
|
|
|
[ExcelFunction(Category = "BTS", Description = "Cancel an Order by Order ID", IsMacroType = true)]
|
|
public static object spark_orderservice_cancel_order(long order_id)
|
|
{
|
|
var resp = _service.Value.cancel_order(new Cancel() { OrderKey = Convert.ToUInt32(order_id) });
|
|
return parse_response(resp);
|
|
}
|
|
|
|
[ExcelFunction(Category = "BTS", Description = "Update an Order by Order ID", IsMacroType = true)]
|
|
public static object spark_orderservice_update_order(long order_id, double price, long quantity)
|
|
{
|
|
var resp = _service.Value.update_order(new Update() { OrderKey = Convert.ToUInt32(order_id), Price = price, Quantity = Convert.ToUInt32(quantity) });
|
|
return parse_response(resp);
|
|
}
|
|
|
|
[ExcelFunction(Category = "BTS", Description = "Send an Order to Spark via security_key", IsMacroType = true)]
|
|
public static object spark_orderservice_send_order_key(
|
|
[ExcelArgument("Required. Only send the order if > last")] long local_order_id,
|
|
[ExcelArgument("Required. Security key")] long security_key,
|
|
[ExcelArgument("Required. Side BUY or SELL")] string side,
|
|
[ExcelArgument("Required. User")] string user,
|
|
[ExcelArgument("Required. Account")] string account,
|
|
[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 = null,
|
|
|
|
[ExcelArgument("OPTIONAL. (DAY,GTC,IOC) - Default DAY")] string time_in_force = null,
|
|
[ExcelArgument("OPTIONAL. (YES,NO) - Default YES")] string manual_order_indicator = null,
|
|
[ExcelArgument("OPTIONAL. (1,2,3,4) - Default 1")] string cti_code = null,
|
|
[ExcelArgument("OPTIONAL. (CUSTOMER,FIRM) - Default FIRM")] string customer_or_firm = null)
|
|
{
|
|
if (local_order_id <= _local_order_id)
|
|
return "local_order_id must be greater than " + _local_order_id;
|
|
Create cr = new Create();
|
|
cr.NewOrder = new OrderDefinition()
|
|
{
|
|
SecurityKey = Convert.ToUInt32(security_key),
|
|
Side = EnumParser.ToEnum<api.Side>(side),
|
|
TimeInForce = time_in_force.ToEnum<TimeInForce>(TimeInForce.Day),
|
|
ManualOrderIndicator = manual_order_indicator.ToEnum<ManualOrderIndicator>(ManualOrderIndicator.Yes),
|
|
CtiCode = cti_code.ToEnum<CTICode>(CTICode.CtiCode1),
|
|
CustomerOrFirm = customer_or_firm.ToEnum<CustomerOrFirm>(CustomerOrFirm.Firm),
|
|
};
|
|
cr.Price = price;
|
|
cr.Quantity = Convert.ToUInt32(quantity);
|
|
|
|
var btsuser = resolve_user(user);
|
|
if(btsuser == null)
|
|
return "Could not resolve user: " + user;
|
|
cr.NewOrder.User = btsuser.Name;
|
|
cr.NewOrder.UserId = btsuser.Id;
|
|
var btaccount = resolve_account(account);
|
|
if(btaccount == null)
|
|
return "Could not resolve account: " + account;
|
|
cr.NewOrder.Account = btaccount.Name;
|
|
cr.NewOrder.AccountId = btaccount.Id;
|
|
cr.NewOrder.CustomDef = custom_def(order_type, order_params);
|
|
if(cr.NewOrder.CustomDef == null || cr.NewOrder.CustomDef.CustomCase == CustomOrderDefinition.CustomOneofCase.None)
|
|
return "Could not parse order_type and/or order_params info";
|
|
|
|
var resp = _service.Value.create_order(cr);
|
|
_local_order_id = local_order_id;
|
|
return parse_response(resp, true);
|
|
}
|
|
|
|
private static object parse_response(Response resp, bool show_vertically = false)
|
|
{
|
|
object[,] response_cells = new JsonFormatter(resp).format(false);
|
|
if(show_vertically)
|
|
response_cells = bts.excel.Utilities.transpose_keep_dimension(response_cells);
|
|
return response_cells;
|
|
}
|
|
|
|
static Lazy<AdminData> _admin_data = new Lazy<AdminData>(() => _admin.Value.get_admin_data(new VoidMessage()), true);
|
|
static BTSUser resolve_user(string username)
|
|
{
|
|
var user = _admin_data.Value.Users.FirstOrDefault(x => username != null ? x.Name.ToLowerInvariant() == username.ToLowerInvariant() : true);
|
|
return user;
|
|
}
|
|
|
|
static BTSAccount resolve_account(string acctname)
|
|
{
|
|
var acc = _admin_data.Value.Accounts.FirstOrDefault(x => x.Name.ToLowerInvariant() == acctname.ToLowerInvariant());
|
|
return acc;
|
|
}
|
|
|
|
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();
|
|
if (member != null)
|
|
{
|
|
var mi = member.member_type();
|
|
var instance = APIDispatch.generate_imessage(mi, order_params, false);
|
|
var cust = new CustomOrderDefinition();
|
|
ReflectionExtensions.set_value(member, cust, instance);
|
|
return cust;
|
|
}
|
|
else
|
|
return null;
|
|
}
|
|
|
|
static long _local_order_id = 0;
|
|
|
|
[ExcelFunction(Category = "BTS", Description = "Send an Order to Spark via security_key", IsMacroType = true)]
|
|
public static object spark_orderservice_send_order(
|
|
[ExcelArgument("Required. Only send the order if > last")] long local_order_id,
|
|
[ExcelArgument("Required. Exchange (GLBX,SIM)")] string exchange,
|
|
[ExcelArgument("Required. Security Desc (ZNZ9 or ZNZ9-ZNH0)")] string security_desc,
|
|
[ExcelArgument("Required. Side BUY or SELL")] string side,
|
|
[ExcelArgument("Required. User")] string user,
|
|
[ExcelArgument("Required. Account")] string account,
|
|
[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 = null,
|
|
|
|
[ExcelArgument("OPTIONAL. (DAY,GTC,IOC) - Default DAY")] string time_in_force = null,
|
|
[ExcelArgument("OPTIONAL. (YES,NO) - Default YES")] string manual_order_indicator = null,
|
|
[ExcelArgument("OPTIONAL. (1,2,3,4) - Default 1")] string cti_code = null,
|
|
[ExcelArgument("OPTIONAL. (CUSTOMER,FIRM) - Default FIRM")] string customer_or_firm = null)
|
|
{
|
|
if (local_order_id <= _local_order_id)
|
|
return "local_order_id must be greater than " + _local_order_id;
|
|
Venue ven;
|
|
if (!Utilities.parse_venue(exchange, out ven))
|
|
return "Can't Parse Exchange: " + (exchange ?? string.Empty);
|
|
|
|
uint key;
|
|
if (MDSubscribe.market_data(ven).lookup_security_key_from_desc(security_desc, out key))
|
|
{
|
|
return spark_orderservice_send_order_key(local_order_id, (long)key, side, user, account, price, quantity, order_type, order_params,
|
|
time_in_force, manual_order_indicator, cti_code, customer_or_firm);
|
|
}
|
|
else
|
|
{
|
|
return "Can't find security desc " + security_desc;
|
|
}
|
|
}
|
|
}
|
|
} |