writes result to a file:

This commit is contained in:
Taha Afzal committed 2019-12-06 18:19:29 -05:00
1 parent b4beb7c0db
commit 8ee78c83d2
3 files changed
+96 -28

No files matched your search

@@ -10,7 +10,37 @@ using mbts.co2;
namespace bts.history{
class MarketQueries{
// works only for glbx
public static IEnumerable<BookEntry> get_synthetic_prices(
public class SyntheticPrice{
public readonly IEnumerable<BookEntry> leg_book;
public readonly BookEntry synthetic_book;
public SyntheticPrice(IEnumerable<BookEntry> leg_book_a,
BookEntry synthetic_book_a){
leg_book = leg_book_a;
synthetic_book = synthetic_book_a;
}
}
public static void get_synthetic_prices(
SpreaderOrderDefinition synth,
DateTime date,
Action<IEnumerable<BookEntry>, BookEntry> on_price){
var sspr_name = synth.Security.Synthetic.Name;
List<BookSeries> leg_book_series = new List<BookSeries>();
foreach (ContractLeg l in synth.Security.Synthetic.Legs){
var bser = MarketQueriesHelper.get_book_state(
l.Security.Symbol,
l.Security.Glbx.SecurityDesc,
date);
leg_book_series.Add(bser);
}
align_bookseries_fast(
(abooks) => {
var sbe = _compute_synthetic_top_book(synth, abooks.ToArray());
on_price(abooks,sbe);
},
leg_book_series.ToArray());
}
public static IEnumerable<SyntheticPrice> get_synthetic_prices(
SpreaderOrderDefinition synth,
DateTime date){
var sspr_name = synth.Security.Synthetic.Name;
@@ -23,34 +53,35 @@ namespace bts.history{
leg_book_series.Add(bser);
}
// assert >= 2 and <= 3
var rv = new List<BookEntry>();
var rv = new List<SyntheticPrice>();
var leg_books = new List<Tuple<BookEntry,BookEntry,BookEntry>>();
align_3_bookseries(leg_book_series.ElementAt(0),
leg_book_series.ElementAt(1),
leg_book_series.ElementAt(2),
(b1,b2,b3) => leg_books.Add(Tuple.Create(b1,b2,b3)));
(b1,b2,b3) => leg_books.Add(Tuple.Create(b1,b2,b3)));
foreach (var p in leg_books){
var b = _compute_synthetic_top_book(synth,p.Item1,p.Item2,p.Item3);
rv.Add(b);
b.PBook.BookSeqNum = Convert.ToUInt32(rv.Count());
rv.Add(new SyntheticPrice(new BookEntry[] {p.Item1,p.Item2,p.Item3},b));
}
Console.WriteLine("{0} {1} synth books computed",sspr_name, rv.Count);
return rv;
}
public static BookEntry compute_synthetic_book(
SpreaderOrderDefinition order_def,
params BookEntry[] leg_books){
foreach (var be in leg_books){
var pbm = be.PBook.Book.Bids[0];
if(be.PBook.Book.Bids[0].Size == 0 &&
be.PBook.Book.Asks[0].Size == 0)
return _synth_book_to_entry(new BookMessage(),0,leg_books);
}
var rv =
Co2NativeTestMethods.co2_compute_synthetic_book(
order_def.Security.immutable(),
leg_books.Select(i=>i.PBook.Book.immutable()).ToArray());
return _synth_book_to_entry(rv.CloneToMutable(),0,leg_books);
}
// public static BookEntry compute_synthetic_book(
// SpreaderOrderDefinition order_def,
// params BookEntry[] leg_books){
// foreach (var be in leg_books){
// var pbm = be.PBook.Book.Bids[0];
// if(be.PBook.Book.Bids[0].Size == 0 &&
// be.PBook.Book.Asks[0].Size == 0)
// return _synth_book_to_entry(new BookMessage(),0,leg_books);
// }
// var rv =
// Co2NativeTestMethods.co2_compute_synthetic_book(
// order_def.Security.immutable(),
// leg_books.Select(i=>i.PBook.Book.immutable()).ToArray());
// return _synth_book_to_entry(rv.CloneToMutable(),0,leg_books);
// }
// stop gap, till the above function gets faster
private static BookEntry _compute_synthetic_top_book(
SpreaderOrderDefinition order_def,
@@ -82,15 +113,17 @@ namespace bts.history{
BookMessage sbm = new BookMessage();
sbm.Bids.Add(bid_psm);
sbm.Asks.Add(ask_psm);
return _synth_book_to_entry (sbm,0,leg_books);
return _synth_book_to_entry (order_def,sbm,0,leg_books);
}
private static BookEntry _synth_book_to_entry(
SpreaderOrderDefinition order_def,
BookMessage bm,
UInt32 seq_no,
params BookEntry[] leg_books){
Timerange r = MarketQueriesHelper.intersection_range(
leg_books.Select(l=>l.TimeRange).ToArray());
ProcessedBook pb = new ProcessedBook();
pb.Security = order_def.Security.Synthetic.Name;
pb.BookSeqNum = pb.MsgSeqNum = seq_no;
pb.Book = bm;
return new BookEntry {PBook = pb,TimeRange = r};
@@ -241,7 +241,6 @@ namespace bts.history{
}
#endregion
#region basic book accessors
@@ -283,5 +282,34 @@ namespace bts.history{
return rv;
}
#endregion
}
private static string _format_timestamp(Timestamp t){
string rv = t.ToString();
return rv.Split('T')[1];
}
public static string book_entry_to_csv(BookEntry b){
return string.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9}",
b.PBook.Security,
b.PBook.MsgSeqNum,
b.PBook.BookSeqNum,
_format_timestamp(b.TimeRange.T1),
_format_timestamp(b.TimeRange.T2),
MarketQueriesHelper.period_ns(b.TimeRange),
MarketQueriesHelper.book_price(b,true,0),
MarketQueriesHelper.book_qty(b,true,0),
MarketQueriesHelper.book_price(b,false,0),
MarketQueriesHelper.book_qty(b,false,0));
}
public static string synthetic_price_to_csv(MarketQueries.SyntheticPrice p){
string rv = String.Empty;
foreach (var l in p.leg_book)
rv += String.Format("{0},",book_entry_to_csv(l));
rv += book_entry_to_csv(p.synthetic_book);
return rv;
}
}
}
@@ -21,34 +21,41 @@ namespace bts.history{
Log.assert(args.Count()==2);
DateTime dt;
Log.assert(DateTime.TryParse(args[0],out dt));
string symbol = args[1];
string path = MarketQueriesHelper.get_algo_path("sniper");
var ssprs = MarketQueriesHelper.generate_3_legged_circulars("CL",dt);
var ssprs = MarketQueriesHelper.generate_3_legged_circulars(symbol,dt);
var spread_map = ssprs.ToDictionary(k=>k,k=>MarketQueriesHelper.parse_synthetic(k));
List<BookEntry> opps = new List<BookEntry>();
// compute the synthetic prices now
// compute the synthetic prices now
StreamWriter tw = new StreamWriter(
String.Format("{0}/{1}_{2}.csv",
path,
symbol,
dt.ToShortDateString().Replace("/","_")));
foreach (var kv in spread_map){
var s = kv.Value;
var n = s.Security.Synthetic.Name;
var spr_prices = MarketQueries.get_synthetic_prices(s,dt);
foreach (var p in spr_prices){
foreach (var spr in spr_prices){
BookEntry p = spr.synthetic_book;
double b = MarketQueriesHelper.book_price(p,true,0),
a = MarketQueriesHelper.book_price(p,false,0);
UInt32 bq = MarketQueriesHelper.book_qty(p,true,0),
aq = MarketQueriesHelper.book_qty(p,false,0);
if(bq>0 && b>0){
opps.Add(p);
Console.WriteLine("SELL:{0} t:{1} active_for:{2} ns {3}:{4}",
n, p.TimeRange.T1,
MarketQueriesHelper.period_ns(p.TimeRange),b,bq);
tw.WriteLine(MarketQueriesHelper.synthetic_price_to_csv(spr));
}
if(aq>0 && a<0){
opps.Add(p);
Console.WriteLine("BUY:{0} t:{1} active_for:{2} ns {3}:{4}",
n, p.TimeRange.T1, MarketQueriesHelper.period_ns(p.TimeRange),
a,aq);
tw.WriteLine(MarketQueriesHelper.synthetic_price_to_csv(spr));
}
}
}
tw.Close();
}
public void analyze(){