Files
spark/src/client/spark_sdk/samples/SDKMain.cs
T

153 lines
5.5 KiB
C#

using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Reactive.Disposables;
using bts.api;
using bts.api.core;
using bts.md;
namespace bts.sdk
{
public class SDKMain
{
static async Task<LoginResult> login(string user, string pass)
{
return await Locator.login(user, pass);
}
private static void user_test()
{
var s = Locator.locate<api.user.AuthService>();
var r = new api.user.GetUsersRequest();
var resp = s.get_users(r);
Console.WriteLine("{0}", resp);
}
public async static Task Main(string[] args)
{
BTS.init();
Log.ClientSdk.info("Welcome to the BTS Client Side SDK example");
string u = args.FirstOrDefault(x => x.Contains("--user="));
string p = args.FirstOrDefault(x => x.Contains("--pass="));
u = u?.Replace("--user=", string.Empty) ?? string.Empty;
p = p?.Replace("--pass=", string.Empty) ?? string.Empty;
var lr = await login(u, p);
if (!lr.success)
{
Console.WriteLine("Login failure: " + lr.reason);
help();
}
if (args.FirstOrDefault() == "users")
{
user_test();
}
else
{
uint ms = 100;
string pu = args.FirstOrDefault(x => x.Contains("--pump="));
if (pu != null)
ms = uint.Parse(p.Replace("--pump=", string.Empty));
MarketDataClient client = new MarketDataClient(ms, Locator.token);
CompositeDisposable disp = new CompositeDisposable(client);
if (args.Length > 0 && args.FirstOrDefault().Contains("md"))
{
var s = new MarketDataSample(client);
if (args.FirstOrDefault() == "md")
{
if (args.Length >= 5)
{
s.print_security(args[1], args[2], args[3], args[4]);
}
else if (args.Length >= 4)
{
s.print_product(args[1], args[2], args[3]);
}
else
md_help();
}
else
md_help();
}
else if (args.FirstOrDefault() == "order")
{
OrderSample os = new OrderSample(client);
disp.Add(os);
order_sample(os, u, args);
}
else
help();
wait_for_stop(disp);
}
}
static async void order_sample(OrderSample os, string user, string[] args)
{
if (args.Length >= 10) //--user and --pass are in args[]
{
await os.send_order(user, args[1], args[2], args[3], args[4], args[5], args[6], args[7]);
}
else if (args.Length >= 9)
{
await os.send_order(user, args[1], args[2], args[3], args[4], args[5], args[6]);
}
else
order_help();
}
static void wait_for_stop(IDisposable disp)
{
Console.WriteLine("Hit q to stop");
ConsoleKeyInfo c;
do
{
c = Console.ReadKey();
Console.WriteLine();
} while (c.KeyChar != 'q');
disp.Dispose();
}
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);
}
}
}