diff --git a/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/BookEntry.cs b/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/BookEntry.cs index b870cb29d..e9b1c3e9b 100644 --- a/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/BookEntry.cs +++ b/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/BookEntry.cs @@ -13,11 +13,9 @@ namespace bts.history{ Timerange time_range_a){ _pbook = pbook_a; time_range = time_range_a; - // temp - foreach (var b in pbook_a.Book.Bids) - b.Exponent = -2; - foreach (var a in pbook_a.Book.Asks) - a.Exponent = -2; + } + public ProcessedBook pbook(){ + return _pbook; } public double price (bool is_bid, UInt16 level){ var vals = is_bid ? _pbook.Book.Bids : _pbook.Book.Asks; diff --git a/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/BookSeries.cs b/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/BookSeries.cs index dab9e4311..a9b1f663f 100644 --- a/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/BookSeries.cs +++ b/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/BookSeries.cs @@ -19,16 +19,26 @@ namespace bts.history{ MarketQueriesHelper.fill_exponent(symbol,pb); pbooks.Add(pb); } - for (int i=0; i= pbooks.Count() ? null : - pbooks.ElementAt(i+1); - Timerange r = new Timerange(timestamp(pb_i),timestamp(pb_i_next)); - _books.Add(new BookEntry(pb_i,r)); - } + _books = _to_book_entries(pbooks); } } + public BookSeries(IEnumerable pbooks){ + _books = _to_book_entries(pbooks); + } + + static List _to_book_entries(IEnumerable pbooks){ + var rv = new List(); + for (int i=0; i= pbooks.Count() ? null : + pbooks.ElementAt(i+1); + Timerange r = new Timerange(timestamp(pb_i),timestamp(pb_i_next)); + rv.Add(new BookEntry(pb_i,r)); + } + return rv; + } + public int count(){ return _books.Count; } diff --git a/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/MarketQueries.cs b/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/MarketQueries.cs index 9f84ff0be..d4910c38a 100644 --- a/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/MarketQueries.cs +++ b/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/MarketQueries.cs @@ -45,23 +45,18 @@ namespace bts.history{ } #region Alligning Books - public static IEnumerable find_coexisting_books ( - BookSeries bser, - BookEntry other_be){ - return bser.find_books_in_time_range(other_be.time_range); - } - // finds all coexisting books in b1_ser and b2_ser + // that exist in tr public static IEnumerable> find_coexisting_books ( - BookSeries b1_ser, - BookSeries b2_ser, + BookSeries bser_1, + BookSeries bser_2, Timerange tr){ var rv = new List>(); - var b1_coex = b1_ser.find_books_in_time_range(tr); - foreach (BookEntry b1_e in b1_coex){ + var b1_in_tr = bser_1.find_books_in_time_range(tr); + foreach (BookEntry b1_e in bser_1.find_books_in_time_range(tr)){ Timerange b1_e_strict_tr = Timerange.intersection(tr, b1_e.time_range); - var b2_coex = b2_ser.find_books_in_time_range(b1_e_strict_tr); + var b2_coex = bser_2.find_books_in_time_range(b1_e_strict_tr); foreach (var b2_e in b2_coex) rv.Add(Tuple.Create(b1_e, b2_e)); } @@ -78,13 +73,12 @@ namespace bts.history{ on_book(be_1, be_2); } } - public static void align_3_bookseries(BookSeries bser_1, - BookSeries bser_2, - BookSeries bser_3, - Action on_book){ - int i1=0; + public static void align_3_bookseries( + BookSeries bser_1, + BookSeries bser_2, + BookSeries bser_3, + Action on_book){ foreach (var be_1 in bser_1.book_entries()){ - i1++; var be_1_coex = find_coexisting_books(bser_2, bser_3, be_1.time_range); foreach (var t in be_1_coex) on_book(be_1, t.Item1, t.Item2); diff --git a/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/SanityTests.cs b/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/SanityTests.cs new file mode 100644 index 000000000..7106370d0 --- /dev/null +++ b/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/SanityTests.cs @@ -0,0 +1,176 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using bts.spark; +using bts.api; +using bts.api.orderman; +using bts.co2; +using Google.Protobuf.WellKnownTypes; + +namespace bts.history{ + class SanityTest{ + public static void test_books_in_range(){ + DateTime base_time = new DateTime(2020,1,1,9,30,45,0,DateTimeKind.Utc); + List bseries_list = new List(); + + Action add_b = (UInt32 usecs) => { + ProcessedBook b = new ProcessedBook(); + b.MsgSeqNum = b.BookSeqNum = Convert.ToUInt32(bseries_list.Count()); + b.SendingTime = b.TransactTime = from_datetime(base_time, usecs); + bseries_list.Add(b); + }; + + add_b(1000); + add_b(1020); + add_b(1025); + add_b(1035); + add_b(1050); + add_b(1055); + add_b(1060); + add_b(1065); + add_b(1080); + add_b(1110); + + BookSeries bser_1 = new BookSeries (bseries_list); + IReadOnlyCollection calc_bes = bser_1.book_entries(); + Log.assert(calc_bes.Count()==10); + Log.assert(calc_bes.ElementAt(0).time_range.period_ns() == 20000); + Log.assert(calc_bes.ElementAt(1).time_range.period_ns() == 5000); + Log.assert(calc_bes.ElementAt(2).time_range.period_ns() == 10000); + Log.assert(calc_bes.ElementAt(3).time_range.period_ns() == 15000); + Log.assert(calc_bes.ElementAt(4).time_range.period_ns() == 5000); + Log.assert(calc_bes.ElementAt(5).time_range.period_ns() == 5000); + Log.assert(calc_bes.ElementAt(6).time_range.period_ns() == 5000); + Log.assert(calc_bes.ElementAt(7).time_range.period_ns() == 15000); + Log.assert(calc_bes.ElementAt(8).time_range.period_ns() == 30000); + Log.assert(calc_bes.ElementAt(9).time_range.period_ns() > 1000000000); + + // find in range ///////////////////////////////////////////////////// + // test find_books_in_time_range + // 1005-1053 + var bitems = bser_1.find_books_in_time_range( + new Timerange( + from_datetime(base_time, 1005), + from_datetime(base_time, 1053))); + Log.assert(bitems.Count()==5); + for (int i=0; i { + var r = b1.time_range.intersection(b2.time_range); + Log.assert(r == TimerangeIntersection.INTERSECTION); + }); + test_random_intersection(); + Console.WriteLine("Tests passed!"); + } + + public static void test_random_intersection(){ + // 3 lists + // generate 3 random lists, find their interection I + // 1. make sure each tuple in I has overlap + // 2. through exhaustive search make sure no other + // overlapping tuples thta are not in I can be found + Func cons_series = (n_items,min_i,max_i,r) => { + List book_ser_list = new List(); + Random rnd = new Random(DateTime.Now.Millisecond); + int moving_usecs = 0; + for(int i = 0; i < n_items; i++){ + moving_usecs += r.HasValue ? r.Value : rnd.Next(min_i,max_i); + _add_to_ser(ref book_ser_list, + Convert.ToUInt32(moving_usecs)); + } + return new BookSeries(book_ser_list); + }; + BookSeries bser_1 = cons_series(100,1,20,20), + bser_2 = cons_series(100,2,15,10), + bser_3 = cons_series(100,2,15,5); + var res = new List>(); + MarketQueries.align_3_bookseries( + bser_1, + bser_2, + bser_3, + (b1,b2,b3) => res.Add(Tuple.Create(b1,b2,b3))); + // 1. make sure each tuple in I has overlap + foreach (var t in res){ + Timerange tr = Timerange.intersection( + t.Item1.time_range, + t.Item2.time_range, + t.Item3.time_range); + Console.WriteLine("overlap={0}",tr.period_ns()); + Log.assert(tr.period_ns()>=0); + } + // 2. through exhaustive search make sure no other + // overlapping tuples thta are not in I can be found + } + + public static Timestamp from_datetime(DateTime dt, UInt32 usecs){ + if (dt.Millisecond != 0){ + usecs += Convert.ToUInt32(dt.Millisecond) * 1000; + dt = dt.AddMilliseconds(dt.Millisecond*-1.0); + } + Timestamp rv = new Timestamp(); + rv.Seconds = Convert.ToInt64((dt - new DateTime(1970,1,1)).TotalSeconds); + rv.Nanos = Convert.ToInt32(usecs) * 1000; + return rv; + } + + private static void _compare_seq_no (Tuple b, + UInt32 s1, UInt32 s2){ + Log.assert(b.Item1.pbook().BookSeqNum == s1); + Log.assert(b.Item2.pbook().BookSeqNum == s2); + } + + private static void _add_to_ser(ref List bseries_list, + UInt32 usecs){ + DateTime base_time = new DateTime(2020,1,1,9,30,45,0,DateTimeKind.Utc); + ProcessedBook b = new ProcessedBook(); + b.MsgSeqNum = b.BookSeqNum = Convert.ToUInt32(bseries_list.Count()); + b.SendingTime = b.TransactTime = from_datetime(base_time, usecs); + bseries_list.Add(b); + } + + // test find_books_in_time_range + // find_coexisting_books 1 + // find_coexisting_books 2 + // align_2_bookseries + // align_3_bookseries + } +} \ 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 2ee032869..dc9b6d622 100644 --- a/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/SpreaderTickSniperAnalyzer.cs +++ b/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/SpreaderTickSniperAnalyzer.cs @@ -14,6 +14,9 @@ namespace bts.history{ // date, output_file, spreaders public SpreaderTickSniperAnalyzer(HistoricalDataOptions hd_options){ Console.WriteLine("Hit key to continue:"); Console.ReadKey(); + + SanityTest.test_books_in_range(); + string input_file_name = hd_options.algo_params; string path = MarketQueriesHelper.get_algo_input_path("sniper"); input_file_name = String.Format(@"{0}/{1}",path, input_file_name);