From 38b29f0afbd7963404bdf0c40c95bcea86d6ca9d Mon Sep 17 00:00:00 2001 From: Kevin Darby Date: Wed, 27 Nov 2019 17:14:29 -0500 Subject: [PATCH] more pg --- .vscode/launch.json | 12 ++-- .../Cmd/Historical/Analyzers/OrderBook.cs | 13 ++++- .../Historical/HistoricalDatabaseGenerator.cs | 55 +++++++++++++------ .../sparkcmd/Cmd/HistoricalDataCommand.cs | 6 +- 4 files changed, 56 insertions(+), 30 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 0dac8ebdd..c64ace030 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -105,14 +105,12 @@ "program": "${workspaceRoot}/build/Debug/bin/net471/sparkcmd.exe", "args": [ "historical", - "--rskip", "11/20/2019 10:00AM", - "--rstart", "11/20/2019 02:00PM", - "--rend", "11/20/2019 11:00PM", + "--rstart", "11/20/2019", + "--rend", "11/20/2019", "--rsymbol", "CL", "--rsecuritytype", "Spread", - "--algo_security", "CLZ9-CLF0;CLF0-CLG0;CLZ9-CLG0", - "--algo_type", "RoundTrip", - "--algo_params", "size=1;profit_ticks=1;tick_size=1,1,1;;hedge_factors=1,1,-1", + "--post_process_db", "mbo_history", + "--post_process_db_host", "sparkvm", ], "cwd": "${workspaceRoot}", "stopAtEntry": false, @@ -373,7 +371,7 @@ "--rend", "11/20/2019", "--rsymbol", "CL", "--rsecuritytype", "Spread", - "--post_process_db", "cl_history", + "--post_process_db", "mbo_history", ], "env": {"LD_LIBRARY_PATH" : "."}, "stopAtEntry": false, diff --git a/src/client/sparkcmd/Cmd/Historical/Analyzers/OrderBook.cs b/src/client/sparkcmd/Cmd/Historical/Analyzers/OrderBook.cs index 10fb9b63f..e772bf44f 100644 --- a/src/client/sparkcmd/Cmd/Historical/Analyzers/OrderBook.cs +++ b/src/client/sparkcmd/Cmd/Historical/Analyzers/OrderBook.cs @@ -337,13 +337,20 @@ namespace bts.history public bool cancel_my_order(ulong my_order_id) { return remove_my_order(my_order_id); - } - + } + + //https://www.cmegroup.com/confluence/display/EPICSANDBOX/MDP+3.0+-+Market+Data+Incremental+Refresh + //yes, but they make it a big-endian string public static bool quote_event_done(MarketDataIncrementalRefresh md) { return md.MatchEventIndicator[0] == '1' || md.MatchEventIndicator[5] == '1'; } - + + public static bool implied_quote_event_done(MarketDataIncrementalRefresh md) + { + return md.MatchEventIndicator[0] == '1' || md.MatchEventIndicator[3] == '1'; + } + public void process_md(MarketDataIncrementalRefresh md, Func filter, string debug_fix = null) { bool bidoffer = false, trade = false; diff --git a/src/client/sparkcmd/Cmd/Historical/HistoricalDatabaseGenerator.cs b/src/client/sparkcmd/Cmd/Historical/HistoricalDatabaseGenerator.cs index af5952921..9491a4850 100644 --- a/src/client/sparkcmd/Cmd/Historical/HistoricalDatabaseGenerator.cs +++ b/src/client/sparkcmd/Cmd/Historical/HistoricalDatabaseGenerator.cs @@ -13,15 +13,15 @@ namespace bts.history readonly HistoricalDataStreamer _stream; MarketDataIncrementalRefresh _last = new MarketDataIncrementalRefresh() { SendingTime = new Timestamp(), TransactTime = new Timestamp() }; readonly string _post_process_db; - readonly string _post_process_db_ip; + readonly string _post_process_db_host; Npgsql.NpgsqlConnection _sql; - public HistoricalDatabaseGenerator(string post_process_db, string post_process_db_ip) + public HistoricalDatabaseGenerator(string post_process_db, string post_process_db_host) { _post_process_db = post_process_db; - _post_process_db_ip = post_process_db_ip; + _post_process_db_host = post_process_db_host; _stream = new HistoricalDataStreamer(); - var conn = $@"Server={_post_process_db_ip};Port=5432; User Id=postgres;Password=enter;"; + var conn = $@"Server={_post_process_db_host};Port=5432; User Id=postgres;Password=enter;"; _sql = new Npgsql.NpgsqlConnection(conn); var create = new Npgsql.NpgsqlCommand( $@"CREATE DATABASE {_post_process_db} @@ -68,7 +68,7 @@ namespace bts.history => x.timestamp().ToDateTime().CompareTo(y.timestamp().ToDateTime())); await _stream.process_md_stream(query, (sectype, md, line) => { - if(count++ % 50000 == 1) + if(++count % 10000 == 1) Console.WriteLine($"preprocessed {count} lines"); if(md.timestamp().ToDateTime() > end) throw new System.IO.EndOfStreamException(); @@ -83,7 +83,8 @@ namespace bts.history _books[entry.security_desc()] = book; } OrderBook.keep_book(book, entry); - write_book(tname, book, entry, md); + if(OrderBook.quote_event_done(md)) + write_book_with_prepare(tname, book, entry, md); } else if (entry.MdEntryType == MDEntryType.Trade) { @@ -96,7 +97,7 @@ namespace bts.history private string maybe_create_table(DateTime start, DateTime end, string symbol, SecurityType st, HistoricalDataOptions o) { - var conn = $@"Server={_post_process_db_ip};Port=5432; User Id=postgres;Password=enter;Database={_post_process_db}"; + var conn = $@"Server={_post_process_db_host};Port=5432; User Id=postgres;Password=enter;Database={_post_process_db}"; _sql = new Npgsql.NpgsqlConnection(conn); var name = $"{symbol}_{st.ToString()}_history"; var cmd = new Npgsql.NpgsqlCommand( @@ -113,8 +114,10 @@ namespace bts.history { var json = Google.Protobuf.JsonFormatter.Default.Format(tm); var cmd = new Npgsql.NpgsqlCommand( - $"INSERT INTO {tname} (seqnum, transact_time, sending_time, security_desc, data_type, data ) " + - $"VALUES ({md.MsgSeqNum}, '{md.TransactTime.ToString().Replace('T', ' ').Replace("Z", "+00")}', '{md.SendingTime.ToString().Replace('T', ' ').Replace("Z", "+00")}', '{entry.security_desc()}', '{nameof(TradeMessage)}', '{ json }' ) ", + $@"INSERT INTO {tname} (seqnum, transact_time, sending_time, security_desc, data_type, data ) + VALUES ({md.MsgSeqNum}, '{md.TransactTime.ToString().Replace('T', ' ').Replace("Z", "+00")}', + '{md.SendingTime.ToString().Replace('T', ' ').Replace("Z", "+00")}', '{entry.security_desc()}', + '{nameof(TradeMessage)}', '{ json }' )", _sql); try { @@ -126,26 +129,44 @@ namespace bts.history } } - private void write_book(string tname, BookMessage book, MDEntry entry, MarketDataIncrementalRefresh md) + bool _book_prepared = false; + private void write_book_with_prepare(string tname, BookMessage book, MDEntry entry, MarketDataIncrementalRefresh md) { - if(!book.Bids.Any( x=>x.Size > 0) && !book.Asks.Any(x => x.Size > 0)) + if (!book.Bids.Any(x => x.Size > 0) && !book.Asks.Any(x => x.Size > 0)) return; - + + if(!_book_prepared) + { + var prep = new Npgsql.NpgsqlCommand( + $@"PREPARE bookplan (integer, timestamptz, timestamptz, text, text, jsonb) AS + INSERT INTO {tname} (seqnum, transact_time, sending_time, security_desc, data_type, data ) + VALUES($1, $2, $3, $4, $5, $6) ", _sql); + try + { + prep.ExecuteNonQuery(); + } + catch (Exception ex) + { + throw ex; + } + _book_prepared = true; + } var json = Google.Protobuf.JsonFormatter.Default.Format(book); var cmd = new Npgsql.NpgsqlCommand( - $"INSERT INTO {tname} (seqnum, transact_time, sending_time, security_desc, data_type, data ) " + - $"VALUES ({md.MsgSeqNum}, '{md.TransactTime.ToString().Replace('T', ' ').Replace("Z", "+00")}', '{md.SendingTime.ToString().Replace('T', ' ').Replace("Z", "+00")}', '{entry.security_desc()}', '{nameof(BookMessage)}', '{ json }' ) ", + $@"EXECUTE bookplan ({md.MsgSeqNum}, '{md.TransactTime.ToString().Replace('T', ' ').Replace("Z", "+00")}', + '{md.SendingTime.ToString().Replace('T', ' ').Replace("Z", "+00")}', '{entry.security_desc()}', + '{nameof(BookMessage)}', '{ json }' ) ", _sql); try { cmd.ExecuteNonQuery(); } - catch(Exception ex) + catch (Exception ex) { throw ex; } - } - + } + public void Dispose() { if(_sql != null) diff --git a/src/client/sparkcmd/Cmd/HistoricalDataCommand.cs b/src/client/sparkcmd/Cmd/HistoricalDataCommand.cs index 225b97df3..e660f6c22 100644 --- a/src/client/sparkcmd/Cmd/HistoricalDataCommand.cs +++ b/src/client/sparkcmd/Cmd/HistoricalDataCommand.cs @@ -95,8 +95,8 @@ namespace bts.history [Option('5', "post_process_db", Required = false, Default = "", HelpText = "Name of the postgres db to generate from post processing")] public string post_process_db { get; set; } - [Option('6', "post_process_db_ip", Required = false, Default = "127.0.0.1", HelpText = "IP of the postgres db to generate from post processing")] - public string post_process_db_ip { get; set; } + [Option('6', "post_process_db_host", Required = false, Default = "localhost", HelpText = "IP of the postgres db to generate from post processing")] + public string post_process_db_host { get; set; } public override string ToString() @@ -129,7 +129,7 @@ namespace bts.history st = (SecurityType)Enum.Parse(typeof(SecurityType), o.replaysecuritytype, true); if(!string.IsNullOrEmpty(o.post_process_db)) { - var rep = new HistoricalDatabaseGenerator(o.post_process_db, o.post_process_db_ip); + var rep = new HistoricalDatabaseGenerator(o.post_process_db, o.post_process_db_host); rep.generate(start, end, o.replaysymbol.ToUpper(), st, o).GetAwaiter().GetResult(); } else