102 lines
3.9 KiB
C#
102 lines
3.9 KiB
C#
namespace spark_api_server {
|
|
using bts;
|
|
using bts.api.core;
|
|
using bts.md;
|
|
|
|
using mbts;
|
|
using mbts.spark;
|
|
|
|
using Spark.Api.MarketData;
|
|
using Spark.Api.Server;
|
|
using Spark.Core;
|
|
using Spark.Core.Reactive;
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Reactive.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
static class BookDumper {
|
|
public static async Task<int> run(DumpBooksOptions opts) {
|
|
Log.notnull(opts.user);
|
|
Log.notnull(opts.password);
|
|
var service_ctx = ServiceContext.create(opts.sparkd);
|
|
if (!await service_ctx.try_login(opts.user, opts.password)) {
|
|
return 1;
|
|
}
|
|
var md_source = new MDSource(TimeSpan.FromSeconds(0.5));
|
|
var md_client = new MarketDataClient(md_source);
|
|
var name_set = opts.securities.empty_if_null().ToHashSet();
|
|
var books_src = md_client.books(s => name_set.Contains(s.ToString()!))
|
|
.Cache().ToObservable();
|
|
var sub = books_src.Subscribe(on_books);
|
|
await books_src.LastOrDefaultAsync();
|
|
return 0;
|
|
|
|
void on_books(TableUpdate<BookTuple, uint> books) {
|
|
var updated_books = books.added.Concat(books.changed);
|
|
var desc = "\n".join(updated_books.Select(fmt_book));
|
|
Console.WriteLine(desc);
|
|
|
|
string fmt_book(BookTuple book_tuple) {
|
|
var sb = new StringBuilder();
|
|
// Write the 2 line header (the security, followed by a separator line)
|
|
sb.AppendLine(book_tuple.security.ToString());
|
|
var total_width = opts.quantity_width * 2 + opts.price_width + 2;
|
|
sb.AppendLine(new string('*', total_width));
|
|
|
|
// Format the book levels like the ladder. For example (w/ 2 price levels):
|
|
// ask_qty ask_price
|
|
// ask_qty ask_price
|
|
// -------------------------
|
|
// bid_price bid_qty
|
|
// bid_price bid_qty
|
|
var bk = book_tuple.book;
|
|
var ask_lines = bk.asks
|
|
.TakeWhile((l, i) => i < opts.num_levels && l.size > 0)
|
|
.Reverse()
|
|
.Select(fmt_ask);
|
|
var bid_lines = bk.bids
|
|
.TakeWhile((l, i) => i < opts.num_levels && l.size > 0)
|
|
.Select(fmt_bid);
|
|
foreach (var l in ask_lines) {
|
|
sb.AppendLine(l);
|
|
}
|
|
|
|
// write line to separate bids and asks (the length is the sum of col widths:
|
|
// qty + 1 + price + 1 + qty (the 1's represent space between cols)
|
|
sb.AppendLine(new string('-', total_width));
|
|
|
|
// Each bid level begins at the price column. Pad accordingly.
|
|
var bid_padding = new string(' ', opts.quantity_width + 1);
|
|
foreach (var l in bid_lines) {
|
|
sb.AppendLine(bid_padding + l);
|
|
}
|
|
sb.AppendLine();
|
|
return sb.ToString();
|
|
|
|
string fmt_ask(PriceSize ps) {
|
|
var qty_text = fmt_qty(ps).PadRight(opts.quantity_width);
|
|
var price_text = fmt_price(ps).PadRight(opts.price_width);
|
|
return $"{qty_text} {price_text}";
|
|
}
|
|
|
|
string fmt_bid(PriceSize ps) {
|
|
var price_text = fmt_price(ps).PadRight(opts.price_width);
|
|
var qty_text = fmt_qty(ps).PadRight(opts.quantity_width);
|
|
return $"{price_text} {qty_text}";
|
|
}
|
|
|
|
string fmt_price(PriceSize ps) =>
|
|
book_tuple.security.spec_or_default().format(ps.price);
|
|
|
|
string fmt_qty(PriceSize ps) =>
|
|
book_tuple.quantity_display.format((double)ps.size);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|