diff --git a/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/BookEntry.cs b/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/BookEntry.cs deleted file mode 100644 index 5d77dc695..000000000 --- a/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/BookEntry.cs +++ /dev/null @@ -1,41 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using bts.co2; - -namespace bts.history{ - class BookEntry{ - // conveniant wrapper around ProcessedBook and its lifetime - // (timerange telling its birth and death) - private readonly ProcessedBook _pbook; - public readonly Timerange time_range; - public BookEntry (ProcessedBook pbook_a, - Timerange time_range_a, - bool copy_top_only=false){ - if(copy_top_only) - { - _pbook = new ProcessedBook(); - _pbook.Book = new BookMessage(); - _pbook.Book.Bids.Add(pbook_a.Book.Bids[0]); - _pbook.Book.Asks.Add(pbook_a.Book.Asks[0]); - } - else - _pbook = pbook_a; - time_range = time_range_a; - } - public ProcessedBook pbook(){ - return _pbook; - } - public double price (bool is_bid, UInt16 level){ - var vals = is_bid ? _pbook.Book.Bids : _pbook.Book.Asks; - var i = vals[level]; - var iprice = new mbts.co2.IntPrice(i.Mantissa, Convert.ToSByte(i.Exponent)); - return iprice.price(); - } - public UInt32 qty (bool is_bid, UInt16 level){ - var vals = is_bid ? _pbook.Book.Bids : _pbook.Book.Asks; - var i = vals[level]; - return i.Size; - } - } -} \ No newline at end of file diff --git a/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/BookSeries.cs b/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/BookSeries.cs index 1d4a88a56..82d5805db 100644 --- a/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/BookSeries.cs +++ b/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/BookSeries.cs @@ -34,8 +34,8 @@ namespace bts.history{ ProcessedBook pb_i = pbooks.ElementAt(i), pb_i_next = i+1 >= 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,top_only)); + Timerange r = new Timerange{T1=timestamp(pb_i),T2=timestamp(pb_i_next)}; + rv.Add(new BookEntry{PBook = pb_i, TimeRange = r}); } return rv; } @@ -49,8 +49,8 @@ namespace bts.history{ public IEnumerable find_books_in_time_range (Timerange r){ var rv = new List(); foreach (var b in _books){ - Timerange b_life = b.time_range; - var intersect = Timerange.intersection_type(b.time_range,r); + Timerange b_life = b.TimeRange; + var intersect = MarketQueriesHelper.intersection_type(b.TimeRange,r); switch(intersect){ case TimerangeIntersection.LEFT_NOINTERSECTION: break; @@ -69,7 +69,7 @@ namespace bts.history{ } #region static helpers public static Timestamp timestamp (ProcessedBook b){ - return b!=null?b.SendingTime:Timerange.max_timestamp(); + return b!=null?b.SendingTime:MarketQueriesHelper.max_timestamp(); } #endregion } diff --git a/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/MarketQueries.cs b/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/MarketQueries.cs index 9cebca518..c29760ece 100644 --- a/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/MarketQueries.cs +++ b/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/MarketQueries.cs @@ -40,15 +40,15 @@ namespace bts.history{ 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) + 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()); + 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 @@ -59,10 +59,10 @@ namespace bts.history{ UInt32 top_bid_q=UInt32.MaxValue, top_ask_q=UInt32.MaxValue; for (int i=0; il.time_range).ToArray()); + Timerange r = MarketQueriesHelper.intersection_range( + leg_books.Select(l=>l.TimeRange).ToArray()); ProcessedBook pb = new ProcessedBook(); pb.BookSeqNum = pb.MsgSeqNum = seq_no; pb.Book = bm; - return new BookEntry (pb,r); + return new BookEntry {PBook = pb,TimeRange = r}; } #region Alligning Books @@ -105,8 +105,8 @@ namespace bts.history{ Timerange tr){ var rv = new List>(); foreach (BookEntry b1_e in bser_1.find_books_in_time_range(tr)) - foreach (var b2_e in bser_2.find_books_in_time_range(b1_e.time_range)) - if(Timerange.intersects(b2_e.time_range,tr)) + foreach (var b2_e in bser_2.find_books_in_time_range(b1_e.TimeRange)) + if(MarketQueriesHelper.intersects(b2_e.TimeRange,tr)) rv.Add(Tuple.Create(b1_e, b2_e)); return rv; } @@ -140,8 +140,8 @@ namespace bts.history{ foreach(var l in curr_vals) if(l==null) return false; - var r = Timerange.intersection_range(curr_vals.Select(i=>i.time_range).ToArray()); - return r.period_ns()>0; + var r = MarketQueriesHelper.intersection_range(curr_vals.Select(i=>i.TimeRange).ToArray()); + return MarketQueriesHelper.period_ns(r)>0; }; uint ord = 0; foreach (var s in leg_book_serieses){ @@ -149,8 +149,8 @@ namespace bts.history{ ord++; } all.Sort((x,y)=>{ - var xt = BookSeries.timestamp(x.Item2.pbook()); - var yt = BookSeries.timestamp(y.Item2.pbook()); + var xt = BookSeries.timestamp(x.Item2.PBook); + var yt = BookSeries.timestamp(y.Item2.PBook); var rv = xt.Seconds.CompareTo(yt.Seconds); return rv != 0 ? rv : xt.Nanos.CompareTo(yt.Nanos); }); diff --git a/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/MarketQueriesHelper.cs b/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/MarketQueriesHelper.cs index e9f4728da..71f0e7c25 100644 --- a/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/MarketQueriesHelper.cs +++ b/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/MarketQueriesHelper.cs @@ -9,14 +9,122 @@ using bts.config; using System.Threading; using System.Threading.Tasks; using System.Collections.Concurrent; +using Google.Protobuf.WellKnownTypes; namespace bts.history{ // secondary level helper functions (parsing, reading files etc) class MarketQueriesHelper{ + // time range helpers /////////////////////////////////////////////////// + ///////////////////////////////////////////////////////////////////////// + #region time range helpers + private readonly static UInt64 _ref_secs = Convert.ToUInt64( + (new DateTime(DateTime.Now.Year, 1,1) - + new DateTime(1970, 1, 1)).TotalSeconds); + public static Int64 period_ns(Timerange r){ + return _diff_ns(r.T2,r.T1); + } + public static bool illformed(Timerange r){ + return period_ns(r)<0; + } + // returns how + public static TimerangeIntersection intersection_type (Timerange x, + Timerange y){ + if (_is_before_or_same(x.T2,y.T1)) + return TimerangeIntersection.LEFT_NOINTERSECTION; + if (_is_before_or_same(y.T2,x.T1)) + return TimerangeIntersection.RIGHT_NOINTERSECTION; + return TimerangeIntersection.INTERSECTION; + } + // conveniance access to the above function + public static bool intersects(Timerange x, Timerange y){ + return intersection_type(x,y) == TimerangeIntersection.INTERSECTION; + } + // finds the overlap between n ranges + public static Timerange intersection_range(params Timerange[] ranges){ + Timerange max_t1 = ranges.MaxElement(r => _from_ref_ns(r.T1)), + min_t2 = ranges.MinElement(r => _from_ref_ns(r.T2)); + //return new Timerange{max_t1.T1,min_t2.T2); + return new Timerange {T1=max_t1.T1,T2=min_t2.T2}; + } + public static Timestamp max_timestamp(){ + Timestamp ts = new Timestamp(); + ts.Seconds = Convert.ToInt64( + (new DateTime(2020, 12, 31) - new DateTime(1970, 1, 1)).TotalSeconds); + ts.Nanos = 0; + return ts; + } + private static bool _is_before (Timestamp x, + Timestamp y){ + return _diff_ns(x,y)<0; + } + private static bool _is_before_or_same (Timestamp x, + Timestamp y){ + return _diff_ns(x,y)<=0; + } + // x-y ns + private static Int64 _diff_ns (Timestamp x, + Timestamp y){ + return Convert.ToInt64(_from_ref_ns(x))- + Convert.ToInt64(_from_ref_ns(y)); + } + private static UInt64 _from_ref_ns (Timestamp ts){ + UInt64 secs = Convert.ToUInt64(ts.Seconds), + ns = Convert.ToUInt64(ts.Nanos); + return (secs - _ref_secs)*1000000000 + ns; + } + #endregion + + // file navigation ////////////////////////////////////////////////////// + ///////////////////////////////////////////////////////////////////////// + #region file navigation + public static string get_algo_path (string algo_name){ + return String.Format(@"{0}{1}/{2}/{3}/", + Configuration.get().data_path, + "trade_analysis", + "algos", + algo_name); + } + public static string get_book_path (string symbol, + DateTime date){ + return String.Format(@"{0}{1}/{2}/{3}/{4}/{5}/", + Configuration.get().data_path, + "trade_analysis", + "market_data", + date.ToShortDateString().Replace("/","_"), + "books", + symbol); + } + public static string get_book_file (string symbol, + string sec_name, + DateTime date){ + return String.Format(@"{0}{1}", + get_book_path(symbol, date), + sec_name); + } + #endregion + + // temp exponent filling //////////////////////////////////////////////// + ///////////////////////////////////////////////////////////////////////// + #region temp exponent filling private static Dictionary _exponent_map = new Dictionary() { {"CL",-2}, }; + public static void fill_exponent(string symbol, ProcessedBook pb){ + if (_exponent_map.ContainsKey(symbol)){ + int exp = _exponent_map[symbol]; + foreach (var b in pb.Book.Bids) + b.Exponent = exp; + foreach (var a in pb.Book.Asks) + a.Exponent = exp; + } + } + #endregion + + // synthetic parsing and generation ///////////////////////////////////// + ///////////////////////////////////////////////////////////////////////// + #region synthetic parsing and generation + // name, tick_size, (symbol, leg, qty, price_mult, q/h)* public static SpreaderOrderDefinition parse_synthetic (string synthetic_text){ SpreaderOrderDefinition rv = new SpreaderOrderDefinition(); @@ -61,64 +169,8 @@ namespace bts.history{ // sec_text list } return rv; - } - public static BookSeries get_book_state (string symbol, - string sec_txt, - DateTime date){ - string fp = get_book_file(symbol,sec_txt,date); - return new BookSeries(symbol, fp); - } - public static string get_algo_input_path (string algo_name){ - return String.Format(@"{0}{1}/{2}/{3}/", - Configuration.get().data_path, - "trade_analysis", - "algos", - algo_name); - } - public static string get_book_path (string symbol, - DateTime date){ - return String.Format(@"{0}{1}/{2}/{3}/{4}/{5}/", - Configuration.get().data_path, - "trade_analysis", - "market_data", - date.ToShortDateString().Replace("/","_"), - "books", - symbol); - } - public static string get_book_file (string symbol, - string sec_name, - DateTime date){ - return String.Format(@"{0}{1}", - get_book_path(symbol, date), - sec_name); - } - public static void fill_exponent(string symbol, ProcessedBook pb){ - if (_exponent_map.ContainsKey(symbol)){ - int exp = _exponent_map[symbol]; - foreach (var b in pb.Book.Bids) - b.Exponent = exp; - foreach (var a in pb.Book.Asks) - a.Exponent = exp; - } - } + } - public static IDictionary get_books (string symbol, - DateTime day, - bool top_only=false){ - var p = get_book_path(symbol,day); - var files = System.IO.Directory.GetFiles(p); - var contracts = files.Select(f => System.IO.Path.GetFileName(f)).Where(n => !n.Contains(".")); - - var rv = new ConcurrentDictionary(); - int i=0, tot = contracts.Count(); - Parallel.ForEach(contracts, c => - //foreach (var c in contracts){ - { - Console.WriteLine("{0} {1}/{2} Reading {3}", DateTime.Now,rv.Count,tot,c); - rv[c] = new BookSeries(symbol,files.ElementAt(i++)); - }); - return rv; - } public static IEnumerable generate_3_legged_circulars( string symbol, DateTime dt){ @@ -187,5 +239,49 @@ namespace bts.history{ } return rv; } - } + #endregion + + + + #region basic book accessors + + public static double book_price(BookEntry b, bool is_bid, UInt16 level){ + var vals = is_bid ? b.PBook.Book.Bids : b.PBook.Book.Asks; + var i = vals[level]; + var iprice = new mbts.co2.IntPrice(i.Mantissa, Convert.ToSByte(i.Exponent)); + return iprice.price(); + } + + public static UInt32 book_qty(BookEntry b, bool is_bid, UInt16 level){ + var vals = is_bid ? b.PBook.Book.Bids : b.PBook.Book.Asks; + var i = vals[level]; + return i.Size; + } + + public static BookSeries get_book_state (string symbol, + string sec_txt, + DateTime date){ + string fp = get_book_file(symbol,sec_txt,date); + return new BookSeries(symbol, fp); + } + + public static IDictionary get_books (string symbol, + DateTime day, + bool top_only=false){ + var p = get_book_path(symbol,day); + var files = System.IO.Directory.GetFiles(p); + var contracts = files.Select(f => System.IO.Path.GetFileName(f)).Where(n => !n.Contains(".")); + + var rv = new ConcurrentDictionary(); + int i=0, tot = contracts.Count(); + Parallel.ForEach(contracts, c => + //foreach (var c in contracts){ + { + Console.WriteLine("{0} {1}/{2} Reading {3}", DateTime.Now,rv.Count,tot,c); + rv[c] = new BookSeries(symbol,files.ElementAt(i++)); + }); + return rv; + } + #endregion + } } \ No newline at end of file diff --git a/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/SanityTests.cs b/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/SanityTests.cs index f9a04d612..2cd48a6c4 100644 --- a/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/SanityTests.cs +++ b/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/SanityTests.cs @@ -10,8 +10,10 @@ using Google.Protobuf.WellKnownTypes; namespace bts.history{ class SanityTest{ public static void run_tests(){ + Console.Write ("Tests started..."); test_books_in_range(); test_random_intersection(); + Console.Write ("Tests completed"); } public static void test_books_in_range(){ DateTime base_time = new DateTime(2020,1,1,9,30,45,0,DateTimeKind.Utc); @@ -38,39 +40,39 @@ namespace bts.history{ 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); + Log.assert(MarketQueriesHelper.period_ns(calc_bes.ElementAt(0).TimeRange) == 20000); + Log.assert(MarketQueriesHelper.period_ns(calc_bes.ElementAt(1).TimeRange) == 5000); + Log.assert(MarketQueriesHelper.period_ns(calc_bes.ElementAt(2).TimeRange) == 10000); + Log.assert(MarketQueriesHelper.period_ns(calc_bes.ElementAt(3).TimeRange) == 15000); + Log.assert(MarketQueriesHelper.period_ns(calc_bes.ElementAt(4).TimeRange) == 5000); + Log.assert(MarketQueriesHelper.period_ns(calc_bes.ElementAt(5).TimeRange) == 5000); + Log.assert(MarketQueriesHelper.period_ns(calc_bes.ElementAt(6).TimeRange) == 5000); + Log.assert(MarketQueriesHelper.period_ns(calc_bes.ElementAt(7).TimeRange) == 15000); + Log.assert(MarketQueriesHelper.period_ns(calc_bes.ElementAt(8).TimeRange) == 30000); + Log.assert(MarketQueriesHelper.period_ns(calc_bes.ElementAt(9).TimeRange) > 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))); + new Timerange{ + T1=from_datetime(base_time, 1005), + T2=from_datetime(base_time, 1053)}); Log.assert(bitems.Count()==5); for (int i=0; i { - var r = Timerange.intersection_type(b1.time_range, b2.time_range); + var r = MarketQueriesHelper.intersection_type(b1.TimeRange, b2.TimeRange); Log.assert(r == TimerangeIntersection.INTERSECTION); }); } @@ -157,8 +159,8 @@ namespace bts.history{ 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); + Log.assert(b.Item1.PBook.BookSeqNum == s1); + Log.assert(b.Item2.PBook.BookSeqNum == s2); } private static void _add_to_ser(ref List bseries_list, @@ -178,9 +180,9 @@ namespace bts.history{ Func, int> count = (t_a) => { int rv = 0; foreach (var t in i_set) - if(t.Item1.pbook().BookSeqNum == t_a.Item1.pbook().BookSeqNum && - t.Item2.pbook().BookSeqNum == t_a.Item2.pbook().BookSeqNum && - t.Item3.pbook().BookSeqNum == t_a.Item3.pbook().BookSeqNum) + if(t.Item1.PBook.BookSeqNum == t_a.Item1.PBook.BookSeqNum && + t.Item2.PBook.BookSeqNum == t_a.Item2.PBook.BookSeqNum && + t.Item3.PBook.BookSeqNum == t_a.Item3.PBook.BookSeqNum) rv++; return rv; }; @@ -190,20 +192,22 @@ namespace bts.history{ Log.assert(count(t)==1); // 2. make sure each tuple in I has overlap foreach (var t in i_set){ - Timerange r = Timerange.intersection_range(t.Item1.time_range, - t.Item2.time_range, - t.Item3.time_range); - Log.assert(r.period_ns()>0); + Timerange r = MarketQueriesHelper.intersection_range( + t.Item1.TimeRange, + t.Item2.TimeRange, + t.Item3.TimeRange); + Log.assert(MarketQueriesHelper.period_ns(r)>0); } // 3. find overlaps throgh exhaustive search and verify // they are found in I foreach (BookEntry b1 in bser_1.book_entries()) foreach (BookEntry b2 in bser_2.book_entries()) foreach (BookEntry b3 in bser_3.book_entries()){ - var intr = Timerange.intersection_range(b1.time_range, - b2.time_range, - b3.time_range); - if(intr.period_ns()>0) + var intr = MarketQueriesHelper.intersection_range( + b1.TimeRange, + b2.TimeRange, + b3.TimeRange); + if(MarketQueriesHelper.period_ns(intr)>0) Log.assert(count(Tuple.Create(b1,b2,b3))==1); } } diff --git a/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/SpreaderTickSniperAnalyzer.cs b/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/SpreaderTickSniperAnalyzer.cs index cb4087d90..92996270f 100644 --- a/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/SpreaderTickSniperAnalyzer.cs +++ b/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/SpreaderTickSniperAnalyzer.cs @@ -16,11 +16,12 @@ namespace bts.history{ // date, output_file, spreaders public SpreaderTickSniperAnalyzer(HistoricalDataOptions hd_options){ Console.WriteLine("Hit key to continue:"); Console.ReadKey(); + SanityTest.run_tests(); string[] args = hd_options.algo_params.Split(','); Log.assert(args.Count()==2); DateTime dt; Log.assert(DateTime.TryParse(args[0],out dt)); - string path = MarketQueriesHelper.get_algo_input_path("sniper"); + string path = MarketQueriesHelper.get_algo_path("sniper"); var ssprs = MarketQueriesHelper.generate_3_legged_circulars("CL",dt); var spread_map = ssprs.ToDictionary(k=>k,k=>MarketQueriesHelper.parse_synthetic(k)); List opps = new List(); @@ -30,19 +31,21 @@ namespace bts.history{ var n = s.Security.Synthetic.Name; var spr_prices = MarketQueries.get_synthetic_prices(s,dt); foreach (var p in spr_prices){ - double b = p.price(true,0), - a = p.price(false,0); - UInt32 bq = p.qty(true,0), - aq = p.qty(false,0); + 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.time_range.t1, p.time_range.period_ns(),b,bq); + n, p.TimeRange.T1, + MarketQueriesHelper.period_ns(p.TimeRange),b,bq); } if(aq>0 && a<0){ opps.Add(p); Console.WriteLine("BUY:{0} t:{1} active_for:{2} ns {3}:{4}", - n, p.time_range.t1, p.time_range.period_ns(),a,aq); + n, p.TimeRange.T1, MarketQueriesHelper.period_ns(p.TimeRange), + a,aq); } } } diff --git a/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/Timerange.cs b/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/Timerange.cs index 049cb119b..625604061 100644 --- a/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/Timerange.cs +++ b/src/client/sparkcmd/Cmd/Historical/StaticAnalysis/Timerange.cs @@ -13,72 +13,4 @@ namespace bts.history{ INTERSECTION, // for later RIGHT_NOINTERSECTION, } - // Wrapper for a start/stop (with ns precision) and helper functions - class Timerange{ - public readonly Timestamp t1, t2; - private readonly static UInt64 _ref_secs = Convert.ToUInt64( - (new DateTime(DateTime.Now.Year, 1,1) - - new DateTime(1970, 1, 1)).TotalSeconds); - public Timerange(Timestamp t1_a, Timestamp t2_a){ - t1=t1_a; - t2=t2_a; - } - public Int64 period_ns(){ - return _diff_ns(t2,t1); - } - public bool illformed(){ - return period_ns()<0; - } - // returns how - public static TimerangeIntersection intersection_type (Timerange x, - Timerange y){ - if (_is_before_or_same(x.t2,y.t1)) - return TimerangeIntersection.LEFT_NOINTERSECTION; - if (_is_before_or_same(y.t2,x.t1)) - return TimerangeIntersection.RIGHT_NOINTERSECTION; - return TimerangeIntersection.INTERSECTION; - } - // conveniance access to the above function - public static bool intersects(Timerange x, Timerange y){ - return intersection_type(x,y) == TimerangeIntersection.INTERSECTION; - } - // finds the overlap between n ranges - public static Timerange intersection_range(params Timerange[] ranges){ - Timerange max_t1 = ranges.MaxElement(r => Timerange._from_ref_ns(r.t1)), - min_t2 = ranges.MinElement(r => Timerange._from_ref_ns(r.t2)); - return new Timerange(max_t1.t1,min_t2.t2); - } - public static Timestamp max_timestamp(){ - Timestamp ts = new Timestamp(); - ts.Seconds = Convert.ToInt64( - (new DateTime(2020, 12, 31) - new DateTime(1970, 1, 1)).TotalSeconds); - ts.Nanos = 0; - return ts; - } - private static bool _is_before (Timestamp x, - Timestamp y){ - return _diff_ns(x,y)<0; - } - private static bool _is_before_or_same (Timestamp x, - Timestamp y){ - return _diff_ns(x,y)<=0; - } - // x-y ns - private static Int64 _diff_ns (Timestamp x, - Timestamp y){ - return Convert.ToInt64(_from_ref_ns(x))- - Convert.ToInt64(_from_ref_ns(y)); - } - private static UInt64 _from_ref_ns (Timestamp ts){ - UInt64 secs = Convert.ToUInt64(ts.Seconds), - ns = Convert.ToUInt64(ts.Nanos); - return (secs - _ref_secs)*1000000000 + ns; - } - public override string ToString(){ - return String.Format("{0} {1),{2} {3},{4}", - t1, - t1.Seconds, t1.Nanos, - t2.Seconds, t2.Nanos); - } - } } \ No newline at end of file diff --git a/src/lib/api/services/HistoricalDataService.proto b/src/lib/api/services/HistoricalDataService.proto index df93fbe0a..3d8149cbf 100644 --- a/src/lib/api/services/HistoricalDataService.proto +++ b/src/lib/api/services/HistoricalDataService.proto @@ -24,7 +24,7 @@ enum MDEntryType TRADE = 50; OPENING_PRICE = 52; SETTLEMENT_PRICE = 54; -} +} message MDEntry { MDUpdateAction md_update_action = 279; //delimiter, this member must be first @@ -155,6 +155,16 @@ message ProcessedBook co2.BookMessage book = 50; } +message Timerange{ + google.protobuf.Timestamp t1 = 10; + google.protobuf.Timestamp t2 = 20; +} + +message BookEntry{ + ProcessedBook p_book = 10; + Timerange time_range = 20; +} + service HistoricalDataService { rpc get_trading_slices(SlicesQuery) returns (Slices);