125 lines
4.7 KiB
C#
125 lines
4.7 KiB
C#
using System;
|
|
using System.Linq;
|
|
using System.Collections.Generic;
|
|
using System.Collections.Concurrent;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using System.Reactive.Linq;
|
|
|
|
using bts.api;
|
|
using bts.md;
|
|
using bts.api.core;
|
|
using bts.api.orderman;
|
|
using bts.api.user;
|
|
using bts.api.admin;
|
|
using bts.co2;
|
|
|
|
namespace bts.sdk
|
|
{
|
|
|
|
public class OrderSample : IDisposable
|
|
{
|
|
readonly MarketDataClient _md;
|
|
readonly List<IDisposable> _disps = new List<IDisposable>();
|
|
public OrderSample(MarketDataClient client)
|
|
{
|
|
_md = client;
|
|
}
|
|
|
|
readonly static Lazy<OrderService> _orders = new Lazy<OrderService>(() => Locator.locate<OrderService>(), true);
|
|
readonly static Lazy<AuthService> _users = new Lazy<AuthService>(() => Locator.locate<AuthService>(), true);
|
|
readonly static Lazy<AdminService> _admin = new Lazy<AdminService>(() => Locator.locate<AdminService>(), true);
|
|
|
|
void print_update<T>(T update)
|
|
{
|
|
Console.WriteLine(update.ToString());
|
|
Console.WriteLine();
|
|
}
|
|
|
|
public Response send_order(string user, string account ,
|
|
string exch, string sectype, string symbol, string security_desc, string qty, string price = null)
|
|
{
|
|
Venue ven = MarketDataSample.parse_venue(exch);
|
|
SecurityType st = MarketDataSample.parse_security_type(sectype);
|
|
Product prod = new Product() { Exchange = ven, Type = st, Symbol = symbol };
|
|
|
|
double px = 0.0;
|
|
if (price != null)
|
|
px = double.Parse(price);
|
|
int quan = int.Parse(qty);
|
|
|
|
Create cr = new Create();
|
|
order_setup(cr, user, account);
|
|
cr.Price = px;
|
|
cr.Quantity = Convert.ToUInt32(Math.Abs(quan));
|
|
cr.NewOrder.Side = quan < 0 ? Side.Sell : Side.Buy;
|
|
if(px != 0.0)
|
|
cr.NewOrder.CustomDef = new CustomOrderDefinition() { Limit = new LimitOrderDefinition() };
|
|
else
|
|
cr.NewOrder.CustomDef = new CustomOrderDefinition() { Market = new MarketOrderDefinition() };
|
|
Console.WriteLine("Fetching security details...");
|
|
var sec = _md.find_security(x => x.Glbx.SecurityDesc == security_desc.ToUpper(), TimeSpan.FromSeconds(30));
|
|
if (!sec.HasValue) {
|
|
Console.WriteLine($"Failed to find security {security_desc}.");
|
|
return null;
|
|
}
|
|
cr.NewOrder.SecurityKey = sec.Value.Id;
|
|
Response resp = _orders.Value.create_order(cr);
|
|
print_update(resp);
|
|
//subscribei_ subscribes to a stream of Immutable (in this case FillImmutable) which is nice if you know you don't want to change the fills, which you dont'
|
|
_disps.Add(_orders.Value.subscribei_fills().Subscribe(x =>
|
|
{
|
|
foreach(var xx in x.added)
|
|
print_update(xx);
|
|
}));
|
|
|
|
_disps.Add(_orders.Value.subscribei_orders().Subscribe(x =>
|
|
{
|
|
foreach (var xx in x.removed)
|
|
print_update(xx);
|
|
foreach (var xx in x.added)
|
|
print_update(xx);
|
|
}));
|
|
return resp;
|
|
}
|
|
|
|
static void order_setup(Create cr, string username, string accountname)
|
|
{
|
|
cr.NewOrder = new OrderDefinition();
|
|
cr.NewOrder.ManualOrderIndicator = ManualOrderIndicator.Yes;
|
|
cr.NewOrder.CtiCode = CTICode.CtiCode1;
|
|
cr.NewOrder.CustomerOrFirm = CustomerOrFirm.Firm;
|
|
cr.NewOrder.SubType = OrderSubType.None;
|
|
cr.NewOrder.TimeInForce = TimeInForce.Day;
|
|
var admin = _admin.Value.get_admin_data(new VoidMessage());
|
|
BTSUser user = admin.Users.FirstOrDefault( x=> username != null ? x.Name.ToLowerInvariant() == username.ToLowerInvariant() : true );
|
|
if (user != null)
|
|
{
|
|
cr.NewOrder.UserId = (uint)user.Id;
|
|
cr.NewOrder.User = user.Name;
|
|
if (accountname == null)
|
|
{
|
|
var accid = user.UserData.AccountPermissions.AccountPermissions.FirstOrDefault();
|
|
cr.NewOrder.AccountId = (uint)accid.Key;
|
|
cr.NewOrder.Account = admin.Accounts.First(x => x.Id == accid.Key).Name;
|
|
}
|
|
else
|
|
{
|
|
var acc = admin.Accounts.FirstOrDefault(x => x.Name.ToLowerInvariant() == accountname.ToLowerInvariant());
|
|
if (acc != null)
|
|
{
|
|
cr.NewOrder.Account = acc.Name;
|
|
cr.NewOrder.AccountId = (uint)acc.Id;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
_disps.ForEach(x => x.Dispose());
|
|
}
|
|
}
|
|
}
|
|
|