From 8ee78c83d272a45ca7b605003ff2ca7d88fe17f6 Mon Sep 17 00:00:00 2001 From: Taha Afzal Date: Fri, 6 Dec 2019 18:19:29 -0500 Subject: [PATCH] writes result to a file: --- .../StaticAnalysis/MarketQueries.cs | 73 ++++++++++++++----- .../StaticAnalysis/MarketQueriesHelper.cs | 32 +++++++- .../SpreaderTickSniperAnalyzer.cs | 19 +++-- 3 files changed, 96 insertions(+), 28 deletions(-) diff --git a/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/MarketQueries.cs b/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/MarketQueries.cs index c29760ece..899f5f1aa 100644 --- a/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/MarketQueries.cs +++ b/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/MarketQueries.cs @@ -10,7 +10,37 @@ using mbts.co2; namespace bts.history{ class MarketQueries{ // works only for glbx - public static IEnumerable get_synthetic_prices( + public class SyntheticPrice{ + public readonly IEnumerable leg_book; + public readonly BookEntry synthetic_book; + public SyntheticPrice(IEnumerable 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, BookEntry> on_price){ + var sspr_name = synth.Security.Synthetic.Name; + List leg_book_series = new List(); + 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 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(); + var rv = new List(); var leg_books = new List>(); 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}; diff --git a/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/MarketQueriesHelper.cs b/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/MarketQueriesHelper.cs index 71f0e7c25..87c52bcc8 100644 --- a/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/MarketQueriesHelper.cs +++ b/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/MarketQueriesHelper.cs @@ -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; + } + + + } } \ No newline at end of file diff --git a/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/SpreaderTickSniperAnalyzer.cs b/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/SpreaderTickSniperAnalyzer.cs index 92996270f..e25cad52b 100644 --- a/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/SpreaderTickSniperAnalyzer.cs +++ b/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/SpreaderTickSniperAnalyzer.cs @@ -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 opps = new List(); - // 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(){