177 lines
6.9 KiB
C#
177 lines
6.9 KiB
C#
|
|
namespace bts.sdk {
|
|
using bts.api.admin;
|
|
using bts.api.core;
|
|
using bts.api.orderman;
|
|
using bts.api.position;
|
|
using bts.co2;
|
|
using bts.md;
|
|
|
|
using Microsoft.Extensions.Hosting;
|
|
using Microsoft.Extensions.Options;
|
|
|
|
using Spark.Api.Admin;
|
|
using Spark.Api.Orders;
|
|
using Spark.Api.Reactive;
|
|
using Spark.Api.Services;
|
|
using Spark.Core.Services;
|
|
using Spark.Sdk;
|
|
|
|
using System;
|
|
using System.Linq;
|
|
using System.Reactive.Disposables;
|
|
using System.Reactive.Linq;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
public class SdkSampleService : IHostedService {
|
|
readonly ISparkConfigLoader sparkConfigLoader;
|
|
readonly SdkSampleConfig sdkConfig;
|
|
readonly Spark.Api.IServiceProvider sparkServiceProvider;
|
|
readonly IUserDataPermissions userDataPermissions;
|
|
readonly ISparkdLoginService loginService;
|
|
|
|
OrderService orderService;
|
|
OrdersClient ordersClient;
|
|
PositionService positionService;
|
|
AdminService adminService;
|
|
AdminClient adminClient;
|
|
MarketDataRelay mdRelay;
|
|
|
|
public SdkSampleService(
|
|
ISparkConfigLoader sparkConfigLoader,
|
|
ISparkdLoginService loginService,
|
|
IUserDataPermissions userDataPermissions,
|
|
Spark.Api.IServiceProvider sparkServiceProvider,
|
|
OrderService orderService,
|
|
OrdersClient ordersClient,
|
|
PositionService positionService,
|
|
AdminService adminService,
|
|
AdminClient adminClient,
|
|
MarketDataRelay mdRelay,
|
|
IOptions<SdkSampleConfig> sdkConfig)
|
|
: this(sparkConfigLoader, loginService, userDataPermissions, sparkServiceProvider, orderService, ordersClient, positionService, adminService, adminClient, mdRelay, sdkConfig.Value) { }
|
|
|
|
public SdkSampleService(
|
|
ISparkConfigLoader sparkConfigLoader,
|
|
ISparkdLoginService loginService,
|
|
IUserDataPermissions userDataPermissions,
|
|
Spark.Api.IServiceProvider sparkServiceProvider,
|
|
OrderService orderService,
|
|
OrdersClient ordersClient,
|
|
PositionService positionService,
|
|
AdminService adminService,
|
|
AdminClient adminClient,
|
|
MarketDataRelay mdRelay,
|
|
SdkSampleConfig sdkConfig) {
|
|
this.sparkConfigLoader = sparkConfigLoader;
|
|
this.loginService = loginService;
|
|
this.sparkServiceProvider = sparkServiceProvider;
|
|
this.userDataPermissions = userDataPermissions;
|
|
this.orderService = orderService;
|
|
this.ordersClient = ordersClient;
|
|
this.positionService = positionService;
|
|
this.adminService = adminService;
|
|
this.adminClient = adminClient;
|
|
this.mdRelay = mdRelay;
|
|
this.sdkConfig = sdkConfig;
|
|
}
|
|
|
|
UserTuple user {
|
|
get {
|
|
var loginTask = loginService.LoginAsync();
|
|
if (!loginTask.IsCompletedSuccessfully) {
|
|
return loginTask.AsTask().GetAwaiter().GetResult().user.Value;
|
|
}
|
|
return loginTask.Result.user.Value;
|
|
}
|
|
}
|
|
|
|
private static void user_test(ServiceContext ctx) {
|
|
var s = ctx.locate<api.user.AuthService>();
|
|
var r = new api.user.GetUsersRequest();
|
|
var resp = s.get_users(r);
|
|
Console.WriteLine("{0}", resp);
|
|
}
|
|
|
|
public async Task StartAsync(CancellationToken cancellationToken) {
|
|
BTS.init();
|
|
Log.Sdk.info("Welcome to the BTS Client Side SDK example");
|
|
|
|
await loginService.LoginAsync();
|
|
var md_source = new MDSource(userDataPermissions);
|
|
var md_client = new MarketDataClient(md_source);
|
|
using (var disp = new CompositeDisposable(md_source)) {
|
|
if (sdkConfig.MarketData is not null) {
|
|
var s = new MarketDataSample(md_client);
|
|
var mdCfg = sdkConfig.MarketData;
|
|
s.print_security(mdCfg.Exchange, mdCfg.SecurityDesc, mdCfg.Symbol, mdCfg.SecurityDesc);
|
|
} else if (sdkConfig.Order is not null) {
|
|
var os = new OrderSample(
|
|
ordersClient,
|
|
orderService,
|
|
adminService,
|
|
md_client,
|
|
md_source.subscribe_securities().Select(sec_map =>
|
|
sec_map.ToDictionary(kv => kv.Key, kv => kv.Value.security)));
|
|
disp.Add(os);
|
|
order_sample(os, user.name, sdkConfig.Order);
|
|
} else
|
|
help();
|
|
|
|
wait_for_stop();
|
|
}
|
|
}
|
|
|
|
public Task StopAsync(CancellationToken cancellationToken) =>
|
|
Task.CompletedTask;
|
|
|
|
static void order_sample(OrderSample os, string user, SdkOrderSampleConfig cfg) {
|
|
os.send_order(user, cfg.Account, cfg.Exchange, cfg.SecurityType, cfg.Symbol, cfg.SecurityDesc, cfg.Quantity, cfg.Price);
|
|
}
|
|
|
|
static void wait_for_stop() {
|
|
Console.WriteLine("Hit q to stop");
|
|
ConsoleKeyInfo c;
|
|
do {
|
|
c = Console.ReadKey();
|
|
Console.WriteLine();
|
|
} while (c.KeyChar != 'q');
|
|
}
|
|
|
|
static void help() {
|
|
Console.WriteLine("BTS Spark SDK");
|
|
Console.WriteLine("Usages: spark_sdk [command] [arguments]");
|
|
Console.WriteLine();
|
|
Console.WriteLine("Commands:");
|
|
Console.WriteLine(" md <exchange> <security_type> <symbol> [<security_desc>]");
|
|
Console.WriteLine(" order <account> <exchange> <security_type> <symbol> <security_desc> <signed_quantity> [<price>]");
|
|
Console.WriteLine("Arguments:");
|
|
Console.WriteLine(" --user=<bts user name>");
|
|
Console.WriteLine(" --pass=<password>");
|
|
Console.WriteLine(" --pump=<pump_interval_in_ms=100 default>");
|
|
Environment.Exit(1);
|
|
}
|
|
|
|
static void order_help() {
|
|
Console.WriteLine("order <exchange> <security_type> <symbol> <security_desc> <signed_quantity> [<price>] [<user>] [<account>]");
|
|
Console.WriteLine("This command sends a limit or market DAY order for the specified security_desc. Leave off the price for a market order");
|
|
Console.WriteLine(" example: order \"ACC A1\" GLBX Future ES ESM8 10 2650.0");
|
|
Console.WriteLine(" order \"ACC A1\" SIM Future ES SIM:ESM8 -10 2650.0");
|
|
Console.WriteLine(" order \"ACC A1\" GLBX Future ES ESM8 10 (this is a market order)");
|
|
Environment.Exit(1);
|
|
}
|
|
|
|
static void md_help() {
|
|
Console.WriteLine("md <exchange> <security_type> <symbol>");
|
|
Console.WriteLine("This command subscribes to market data for an exchange-type-symbol channel and writes to console.");
|
|
Console.WriteLine("Marketdata is read off the wire on the same thread as is used to read and print");
|
|
Console.WriteLine(" example: md GLBX Future ES --user=bts --pass=password");
|
|
Console.WriteLine(" md SIM Future ES SIM:ESZ8 --user=bts --pass=password");
|
|
Console.WriteLine(" md GLBX Spread ES --user=bts --pass=password");
|
|
Environment.Exit(1);
|
|
}
|
|
|
|
}
|
|
}
|