From 4e48bcf6b5b2493601b5cfb70ce8fa63a48ae127 Mon Sep 17 00:00:00 2001 From: Taha Afzal Date: Wed, 11 Dec 2019 13:33:07 -0500 Subject: [PATCH] refactoring --- .../StaticAnalysis/MarketQueries.cs | 76 +++++++++---------- .../StaticAnalysis/MarketQueriesHelper.cs | 15 +++- .../Historical/StaticAnalysis/Microscope.cs | 2 +- .../Historical/StaticAnalysis/SanityTests.cs | 6 +- .../SpreaderTickSniperAnalyzer.cs | 67 +++++++++------- 5 files changed, 92 insertions(+), 74 deletions(-) diff --git a/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/MarketQueries.cs b/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/MarketQueries.cs index f7736eef9..5f09cc72c 100644 --- a/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/MarketQueries.cs +++ b/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/MarketQueries.cs @@ -20,47 +20,47 @@ namespace bts.history{ 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; + public static void get_synthetic_prices( + SpreaderOrderDefinition synth, + DateTime date, + Action on_price){ + var sspr_name = synth.Security.Synthetic.Name; List leg_book_series = new List(); foreach (ContractLeg l in synth.Security.Synthetic.Legs){ - BookSeries bser = - MarketQueriesHelper.get_book_state(l.Security.Symbol, + var bser = MarketQueriesHelper.get_book_series_fast( + l.Security.Symbol, l.Security.Glbx.SecurityDesc, date); leg_book_series.Add(bser); } - var alligned_books = allign_bookseries(leg_book_series.ToArray()); - var rv = new List(); - foreach (var ab in alligned_books){ - var sp = _compute_synthetic_top_book(synth,ab.ToArray()); - rv.Add(new SyntheticPrice(ab,sp)); - } - return rv; - } + align_bookseries( + (abooks) => { + var sbe = _compute_synthetic_top_book(synth, abooks.ToArray()); + on_price(new SyntheticPrice(abooks,sbe)); + }, + leg_book_series.ToArray()); + } + + // public static IEnumerable get_synthetic_prices( + // SpreaderOrderDefinition synth, + // DateTime date){ + // var sspr_name = synth.Security.Synthetic.Name; + // List leg_book_series = new List(); + // foreach (ContractLeg l in synth.Security.Synthetic.Legs){ + // BookSeries bser = + // MarketQueriesHelper.get_book_series(l.Security.Symbol, + // l.Security.Glbx.SecurityDesc, + // date); + // leg_book_series.Add(bser); + // } + // var alligned_books = allign_bookseries(leg_book_series.ToArray()); + // var rv = new List(); + // foreach (var ab in alligned_books){ + // var sp = _compute_synthetic_top_book(synth,ab.ToArray()); + // rv.Add(new SyntheticPrice(ab,sp)); + // } + // return rv; + // } // public static BookEntry compute_synthetic_book( // SpreaderOrderDefinition order_def, // params BookEntry[] leg_books){ @@ -142,10 +142,10 @@ namespace bts.history{ public static IEnumerable> allign_bookseries( params BookSeries[] leg_book_serieses){ var rv = new List>(); - align_bookseries_fast(s => rv.Add(s), leg_book_serieses); + align_bookseries(s => rv.Add(new List(s)), leg_book_serieses); return rv; } - public static void align_bookseries_fast( + public static void align_bookseries( Action> on_book, params BookSeries[] leg_book_serieses){ // book series with all leg books @@ -170,7 +170,7 @@ namespace bts.history{ foreach (var t in all){ curr_vals[t.Item1]=t.Item2; if(is_ready()) - on_book(new List(curr_vals)); + on_book(curr_vals); } } diff --git a/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/MarketQueriesHelper.cs b/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/MarketQueriesHelper.cs index 133bb55d9..da50dc3dc 100644 --- a/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/MarketQueriesHelper.cs +++ b/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/MarketQueriesHelper.cs @@ -264,9 +264,9 @@ namespace bts.history{ } return rv; } - public BookSeries get_book_series (string symbol, - string sec_txt, - DateTime date){ + public BookSeries get_book_series(string symbol, + string sec_txt, + DateTime date){ var key = String.Format("{0}_{1}_{2}",symbol,sec_txt, date.ToShortDateString()); Tuple rv; @@ -301,12 +301,19 @@ namespace bts.history{ return i.Size; } - public static BookSeries get_book_state (string symbol, + public static BookSeries get_book_series (string symbol, string sec_txt, DateTime date){ string fp = get_book_file(symbol,sec_txt,date); return new BookSeries(symbol, fp); } + + private static BookSeriesCacher _bser_cacher = new BookSeriesCacher(); + public static BookSeries get_book_series_fast (string symbol, + string sec_txt, + DateTime date){ + return _bser_cacher.get_book_series(symbol,sec_txt,date); + } public static void verify_book_series_order (IEnumerable books){ for(int i=1;i< books.Count();i++){ diff --git a/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/Microscope.cs b/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/Microscope.cs index c3b6b0a2e..ecca386cc 100644 --- a/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/Microscope.cs +++ b/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/Microscope.cs @@ -35,7 +35,7 @@ namespace bts.history{ string contract = args[3]; UInt32 m1 = UInt32.Parse(args[4]), m2 = UInt32.Parse(args[5]); - BookSeries bser = MarketQueriesHelper.get_book_state(symbol, + BookSeries bser = MarketQueriesHelper.get_book_series(symbol, contract, dt); var books = MarketQueries.find_in_range(bser,m1,m2); diff --git a/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/SanityTests.cs b/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/SanityTests.cs index 0a6df4a56..b2627b5cf 100644 --- a/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/SanityTests.cs +++ b/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/SanityTests.cs @@ -97,7 +97,7 @@ namespace bts.history{ _compare_seq_no (coex.ElementAt(6),3,4); //1035 _compare_seq_no (coex.ElementAt(7),4,4); //1050 - MarketQueries.align_bookseries_fast( + MarketQueries.align_bookseries( l => { var b1 = l.ElementAt(0); var b2 = l.ElementAt(1); @@ -126,7 +126,7 @@ namespace bts.history{ bser_2 = cons_series(200,2,15,10), bser_3 = cons_series(400,2,15,5); var res = new List>(); - MarketQueries.align_bookseries_fast( + MarketQueries.align_bookseries( l => res.Add(Tuple.Create(l.ElementAt(0), l.ElementAt(1), l.ElementAt(2))), @@ -141,7 +141,7 @@ namespace bts.history{ bser_2 = cons_series(100,3,23,null); bser_3 = cons_series(100,5,31,null); res = new List>(); - MarketQueries.align_bookseries_fast( + MarketQueries.align_bookseries( l => res.Add(Tuple.Create(l.ElementAt(0), l.ElementAt(1), l.ElementAt(2))), diff --git a/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/SpreaderTickSniperAnalyzer.cs b/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/SpreaderTickSniperAnalyzer.cs index f2677ee43..34b3eba43 100644 --- a/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/SpreaderTickSniperAnalyzer.cs +++ b/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/SpreaderTickSniperAnalyzer.cs @@ -37,38 +37,49 @@ namespace bts.history{ String.Format("{0}/{1}_{2}.csv", path, symbol, - dt.ToShortDateString().Replace("/","_"))); - System.Diagnostics.Stopwatch w = new System.Diagnostics.Stopwatch(); - foreach (var kv in spread_map){ - var s = kv.Value; - var n = s.Security.Synthetic.Name; - w.Restart();var spr_prices = MarketQueries.get_synthetic_prices(s,dt);w.Stop(); - Console.WriteLine("{0}:{1} {2} msecs", n,spr_prices.Count(),w.ElapsedMilliseconds); - //spr_prices = MarketQueriesHelper.filter_end_entries(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){ - 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){ - 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)); - } - } - } + dt.ToShortDateString().Replace("/","_"))); + foreach (var kv in spread_map) + _evaluate_synthetic(kv.Value,dt,tw); tw.Close(); } public void analyze(){ } + + private void _evaluate_synthetic(SpreaderOrderDefinition s, + DateTime dt, + StreamWriter tw){ + var n = s.Security.Synthetic.Name; + System.Diagnostics.Stopwatch w = new System.Diagnostics.Stopwatch(); + w.Restart(); + int spr_count = 0; + MarketQueries.get_synthetic_prices(s,dt,spr=> + {_detect_opp(n,spr,tw);spr_count++;}); + w.Stop(); + Console.WriteLine("{0}:{1} {2} msecs", n,spr_count,w.ElapsedMilliseconds); + } + + private static void _detect_opp ( + string n, + MarketQueries.SyntheticPrice spr, + StreamWriter tw){ + 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){ + 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){ + 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)); + } + } } } \ No newline at end of file