This commit is contained in:
Taha Afzal committed 2019-12-01 16:17:14 -05:00
1 parent f1dbacb989
commit c50133ba02
5 files changed
+210 -29

No files matched your search

@@ -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;
@@ -19,16 +19,26 @@ namespace bts.history{
MarketQueriesHelper.fill_exponent(symbol,pb);
pbooks.Add(pb);
}
for (int i=0; i<pbooks.Count(); i++){
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));
_books.Add(new BookEntry(pb_i,r));
}
_books = _to_book_entries(pbooks);
}
}
public BookSeries(IEnumerable<ProcessedBook> pbooks){
_books = _to_book_entries(pbooks);
}
static List<BookEntry> _to_book_entries(IEnumerable<ProcessedBook> pbooks){
var rv = new List<BookEntry>();
for (int i=0; i<pbooks.Count(); i++){
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));
}
return rv;
}
public int count(){
return _books.Count;
}
@@ -45,23 +45,18 @@ namespace bts.history{
}
#region Alligning Books
public static IEnumerable<BookEntry> 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<Tuple<BookEntry,BookEntry>> find_coexisting_books (
BookSeries b1_ser,
BookSeries b2_ser,
BookSeries bser_1,
BookSeries bser_2,
Timerange tr){
var rv = new List<Tuple<BookEntry,BookEntry>>();
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<BookEntry, BookEntry, BookEntry> on_book){
int i1=0;
public static void align_3_bookseries(
BookSeries bser_1,
BookSeries bser_2,
BookSeries bser_3,
Action<BookEntry, BookEntry, BookEntry> 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);
@@ -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<ProcessedBook> bseries_list = new List<ProcessedBook>();
Action<UInt32> 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<BookEntry> 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<bitems.Count();i++){
BookEntry be = bitems.ElementAt(i);
Log.assert(be.pbook().MsgSeqNum==Convert.ToUInt32(i));
}
// test find_books_in_time_range
// 1030-2000
bitems = bser_1.find_books_in_time_range(
new Timerange(
from_datetime(base_time, 1030),
from_datetime(base_time, 2000)));
Log.assert(bitems.Count()==8);
for (int i=0; i<bitems.Count();i++){
BookEntry be = bitems.ElementAt(i);
Log.assert(be.pbook().MsgSeqNum==Convert.ToUInt32(i+2));
}
// find coexisting 1 ////////////////////////////////////////////////////
bseries_list.Clear();
add_b(900);
add_b(990);
add_b(1010);
add_b(1015); //3
add_b(1030);
BookSeries bser_2 = new BookSeries (bseries_list);
var coex = MarketQueries.find_coexisting_books(
bser_1,
bser_2,
new Timerange(from_datetime(base_time, 999),
from_datetime(base_time, 1051)));
_compare_seq_no (coex.ElementAt(0),0,1); //1000
_compare_seq_no (coex.ElementAt(1),0,2);
_compare_seq_no (coex.ElementAt(2),0,3);
_compare_seq_no (coex.ElementAt(3),1,3); //1020
_compare_seq_no (coex.ElementAt(4),2,3); //1025
_compare_seq_no (coex.ElementAt(5),2,4); //1025
_compare_seq_no (coex.ElementAt(6),3,4); //1035
_compare_seq_no (coex.ElementAt(7),4,4); //1050
MarketQueries.align_2_bookseries(
bser_1,
bser_2,
(b1, b2) => {
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<int,int,int,int?,BookSeries> cons_series = (n_items,min_i,max_i,r) => {
List<ProcessedBook> book_ser_list = new List<ProcessedBook>();
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<Tuple<BookEntry,BookEntry,BookEntry>>();
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<BookEntry,BookEntry> 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<ProcessedBook> 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
}
}
@@ -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);