write book with trades

This commit is contained in:
Kevin Darby committed 2019-12-08 14:37:48 -05:00
1 parent b48da514e5
commit 8817b22376
4 files changed
+631 -12

No files matched your search

+3 -3
View File
@@ -105,11 +105,11 @@
"program": "${workspaceRoot}/build/Debug/bin/net471/sparkcmd.exe",
"args": [
"historical",
"--rstart", "11/20/2019",
"--rend", "11/20/2019",
"--rstart", "12/04/2019",
"--rend", "12/05/2019",
"--rsymbol", "CL",
"--rsecuritytype", "Spread",
"--post_process_db", "oil2",
"--post_process_db", "oil_12_4_2019",
"--post_process_db_host", "bigblue",
],
"cwd": "${workspaceRoot}",
@@ -0,0 +1,309 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Invalid: Command: #r C:/Users/darby/.nuget/packages/google.protobuf/3.6.1/lib/netstandard1.0/Google.Protobuf.dllInvalid: Command: #r C:/Users/darby/.nuget/packages/newtonsoft.json/12.0.3/lib/netstandard2.0/Newtonsoft.Json.dllInvalid: Command: #r C:/Users/darby/.nuget/packages/system.linq.dynamic.core/1.0.19/lib/netstandard2.0/System.Linq.Dynamic.Core.dllInvalid: Command: #r C:/Users/darby/.nuget/packages/mongodb.driver/2.9.3/lib/netstandard1.5/MongoDB.Driver.dllInvalid: Command: #r C:/Users/darby/.nuget/packages/mongodb.driver.core/2.9.3/lib/netstandard1.5/MongoDB.Driver.Core.dllInvalid: Command: #r C:/Users/darby/.nuget/packages/mongodb.bson/2.9.3/lib/netstandard1.5/MongoDB.Bson.dll"
]
}
],
"source": [
"//#r \"nuget:Google.Protobuf, 3.6.1\"\n",
"#r \"C:/Users/darby/.nuget/packages/google.protobuf/3.6.1/lib/netstandard1.0/Google.Protobuf.dll\"\n",
"//#r \"nuget:Microsoft.CSharp, 4.5.0\"\n",
"//#r \"C:/Users/darby/.nuget/packages/microsoft.netcore.app.runtime.linux-x64/3.0.0/runtimes/linux-x64/lib/netcoreapp3.0/Microsoft.CSharp.dll\" \n",
"//#r \"nuget:Newtonsoft.Json, 12.0.3\"\n",
"#r \"C:/Users/darby/.nuget/packages/newtonsoft.json/12.0.3/lib/netstandard2.0/Newtonsoft.Json.dll\"\n",
"//#r \"nuget:System.Linq.Dynamic, 1.0.7\"\n",
"#r \"C:/Users/darby/.nuget/packages/system.linq.dynamic.core/1.0.19/lib/netstandard2.0/System.Linq.Dynamic.Core.dll\"\n",
"//#r \"nuget:MongoDB.Driver, 2.9.3\"\n",
"#r \"C:/Users/darby/.nuget/packages/mongodb.driver/2.9.3/lib/netstandard1.5/MongoDB.Driver.dll\" \n",
"#r \"C:/Users/darby/.nuget/packages/mongodb.driver.core/2.9.3/lib/netstandard1.5/MongoDB.Driver.Core.dll\"\n",
"//#r \"nuget:MongoDB.Bson, 2.9.3\"\n",
"#r \"C:/Users/darby/.nuget/packages/mongodb.bson/2.9.3/lib/netstandard1.5/MongoDB.Bson.dll\" "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Invalid: Command: #r c:/code/bts/spark/build/Debug/bin/netstandard2.0/spark_historical.dll"
]
}
],
"source": [
"#r \"c:/code/bts/spark/build/Debug/bin/netstandard2.0/spark_historical.dll\" \n",
"using bts.history;\n",
"using MongoDB.Driver;\n",
"using MongoDB.Driver.Linq;\n",
"using Google.Protobuf.WellKnownTypes;\n",
"using static bts.history.Query;\n",
"using mbts.co2;\n"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"var q = new Query(\"oil_12_4_2019\", \"bigblue\");\n",
"//Timestamp ts = new Timestamp();\n",
"//ts.Seconds = 15;\n",
"//DateTime dt = ts.ToDateTime();\n",
"//display(dt.Kind);"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"var f = q.collection<HistoryRow>(\"CLH0-CLM0\");\n",
"var j = q.collection<HistoryRow>(\"CLM0-CLZ0\");\n",
"var m = q.collection<HistoryRow>(\"CLH0-CLZ0\");\n",
"\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"//var s = q.series<HistoryRow>(\"ZNZ9-ZNH0\").Where( x=> x.Book != null );\n",
"//var s1 = s.Where( x => x.Book.Bids[0].Size > 0);\n",
"//display(s.Count())\n",
"//var s2 = s.Where( x => x.sending_time >= new DateTime(2019, 11, 25));\n",
"//display(s2.Count());"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"var start = new DateTime(2019, 12, 4, 0, 0, 0);\n",
"var end = new DateTime(2019, 12, 5, 23, 59, 59);\n",
"var ft = q.timeseries<HistoryRow>(f, start, end);\n",
"var jt = q.timeseries<HistoryRow>(j, start, end);\n",
"var mt = q.timeseries<HistoryRow>(m,start, end);\n",
"\n",
"//var ft = q.timeseries<HistoryRow>(f, new DateTime(2019, 11, 20, 21, 21, 22), new DateTime(2019, 11, 20, 21, 21, 23));\n",
"//var jt = q.timeseries<HistoryRow>(j, new DateTime(2019, 11, 20, 21, 21, 22), new DateTime(2019, 11, 20, 21, 21, 23));\n",
"//var mt = q.timeseries<HistoryRow>(m, new DateTime(2019, 11, 20, 21, 21, 22), new DateTime(2019, 11, 20, 21, 21, 23));"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/html": [
"223173"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"273808"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"223254"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"display(ft.Count());\n",
"display(jt.Count());\n",
"display(mt.Count());\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"var ff = ft.Where(x => x.Book != null && x.Book.Bids.Count > 0 && x.Book.Asks.Count > 0);\n",
"var jf = jt.Where(x => x.Book != null && x.Book.Bids.Count > 0 && x.Book.Asks.Count > 0);\n",
"var mf = mt.Where(x => x.Book != null && x.Book.Bids.Count > 0 && x.Book.Asks.Count > 0);\n",
"\n",
"var ftop = ff.AsEnumerable().Select(x => new GetTopOfBook(x)).OrderBy(x => x.sending_time);\n",
"var jtop = jf.AsEnumerable().Select(x => new GetTopOfBook(x)).OrderBy(x => x.sending_time);\n",
"var mtop = mf.AsEnumerable().Select(x => new GetTopOfBook(x)).OrderBy(x => x.sending_time);\n",
"\n",
"//display(h0f.Take(1));\n",
"//display(z9f.Take(1));"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"class TopReport<T> where T : IHasTopBook, IHasTimestamp, Google.Protobuf.IDeepCloneable<T>\n",
"{\n",
" readonly T[] _legs;\n",
" readonly T _synth;\n",
" public TopReport(T[] legs, T synth)\n",
" {\n",
" _legs = (T[])legs.Clone();\n",
" _synth = synth.Clone();\n",
" }\n",
" public uint seq_num => _synth.MsgSeqNum;\n",
" public string sending_time => _synth.sending_time.ToString(\"yyyy-MM-dd HH:mm:ss.ffffff\");\n",
" public string transact_time => _synth.transact_time.ToString(\"yyyy-MM-dd HH:mm:ss.ffffff\");\n",
" public uint bidsize => _synth.TopBook.Bid.Size;\n",
" public double bid => _synth.TopBook.Bid.price();\n",
" public double ask => _synth.TopBook.Ask.price();\n",
" public uint asksize => _synth.TopBook.Ask.Size;\n",
" \n",
" public string[] legbids => _legs.Select( x=> x.MsgSeqNum + \" \" + x.TopBook.Bid.Size + \" \" + \n",
" x.TopBook.Bid.price().ToString(\"0.00 |\") ).ToArray();\n",
" public string[] legasks => _legs.Select( x=> x.MsgSeqNum + \" \" + x.TopBook.Ask.Size + \" \" + \n",
" x.TopBook.Ask.price().ToString(\"0.00 |\") ).ToArray();\n",
"}\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
" "
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [],
"source": [
"\n",
"var synth = create_synthetic_top( new []{ftop, jtop, mtop}, new[] {1.0, 1.0, -1.0}, new[] {1.0, 1.0, -1.0}, \n",
" x => x.TopBook.Bid.price() > 0.0 || x.TopBook.Ask.price() < 0.0, \n",
" (legs, synth)=> new TopReport<GetTopOfBook>(legs, synth));"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/html": [
"<table><thead><tr><th><i>index</i></th><th>seq_num</th><th>sending_time</th><th>transact_time</th><th>bidsize</th><th>bid</th><th>ask</th><th>asksize</th><th>legbids</th><th>legasks</th></tr></thead><tbody><tr><td>0</td><td>113288047</td><td>2019-12-04 15:09:12.008300</td><td>2019-12-04 15:09:12.008131</td><td>32</td><td>-5</td><td>-1</td><td>3</td><td>113288047 106 116.00 |112831258 32 200.00 |113288020 3 319.00 |</td><td>113288047 30 117.00 |112831258 17 201.00 |113288020 40 321.00 |</td></tr><tr><td>1</td><td>113288049</td><td>2019-12-04 15:09:12.008331</td><td>2019-12-04 15:09:12.008144</td><td>32</td><td>-5</td><td>-1</td><td>3</td><td>113288049 106 116.00 |112831258 32 200.00 |113288020 3 319.00 |</td><td>113288049 30 117.00 |112831258 17 201.00 |113288020 40 321.00 |</td></tr><tr><td>2</td><td>113288065</td><td>2019-12-04 15:09:12.008554</td><td>2019-12-04 15:09:12.008279</td><td>32</td><td>-5</td><td>-1</td><td>3</td><td>113288065 106 116.00 |112831258 32 200.00 |113288020 3 319.00 |</td><td>113288065 30 117.00 |112831258 17 201.00 |113288020 40 321.00 |</td></tr><tr><td>3</td><td>113288069</td><td>2019-12-04 15:09:12.008614</td><td>2019-12-04 15:09:12.008330</td><td>32</td><td>-5</td><td>-1</td><td>3</td><td>113288069 106 116.00 |112831258 32 200.00 |113288020 3 319.00 |</td><td>113288069 30 117.00 |112831258 17 201.00 |113288020 40 321.00 |</td></tr><tr><td>4</td><td>113288070</td><td>2019-12-04 15:09:12.008633</td><td>2019-12-04 15:09:12.008330</td><td>32</td><td>-5</td><td>-1</td><td>3</td><td>113288070 106 116.00 |112831258 32 200.00 |113288020 3 319.00 |</td><td>113288070 30 117.00 |112831258 17 201.00 |113288020 40 321.00 |</td></tr><tr><td>5</td><td>113288079</td><td>2019-12-04 15:09:12.008763</td><td>2019-12-04 15:09:12.008346</td><td>32</td><td>-5</td><td>-1</td><td>3</td><td>113288070 106 116.00 |112831258 32 200.00 |113288079 3 319.00 |</td><td>113288070 30 117.00 |112831258 17 201.00 |113288079 40 321.00 |</td></tr><tr><td>6</td><td>113288081</td><td>2019-12-04 15:09:12.008783</td><td>2019-12-04 15:09:12.008350</td><td>32</td><td>-5</td><td>-1</td><td>3</td><td>113288081 106 116.00 |112831258 32 200.00 |113288079 3 319.00 |</td><td>113288081 30 117.00 |112831258 17 201.00 |113288079 40 321.00 |</td></tr><tr><td>7</td><td>113288090</td><td>2019-12-04 15:09:12.008903</td><td>2019-12-04 15:09:12.008420</td><td>32</td><td>-5</td><td>-1</td><td>3</td><td>113288090 106 116.00 |112831258 32 200.00 |113288079 3 319.00 |</td><td>113288090 30 117.00 |112831258 17 201.00 |113288079 40 321.00 |</td></tr><tr><td>8</td><td>113288099</td><td>2019-12-04 15:09:12.009058</td><td>2019-12-04 15:09:12.008476</td><td>32</td><td>-5</td><td>-1</td><td>3</td><td>113288090 106 116.00 |112831258 32 200.00 |113288099 3 319.00 |</td><td>113288090 30 117.00 |112831258 17 201.00 |113288099 45 321.00 |</td></tr><tr><td>9</td><td>113288100</td><td>2019-12-04 15:09:12.009071</td><td>2019-12-04 15:09:12.008477</td><td>32</td><td>-5</td><td>-1</td><td>3</td><td>113288090 106 116.00 |112831258 32 200.00 |113288100 3 319.00 |</td><td>113288090 30 117.00 |112831258 17 201.00 |113288100 45 321.00 |</td></tr><tr><td>10</td><td>113288122</td><td>2019-12-04 15:09:12.009381</td><td>2019-12-04 15:09:12.008909</td><td>32</td><td>-5</td><td>-1</td><td>3</td><td>113288122 106 116.00 |112831258 32 200.00 |113288100 3 319.00 |</td><td>113288122 30 117.00 |112831258 17 201.00 |113288100 45 321.00 |</td></tr><tr><td>11</td><td>113288123</td><td>2019-12-04 15:09:12.009399</td><td>2019-12-04 15:09:12.008910</td><td>32</td><td>-5</td><td>-1</td><td>3</td><td>113288123 106 116.00 |112831258 32 200.00 |113288100 3 319.00 |</td><td>113288123 30 117.00 |112831258 17 201.00 |113288100 45 321.00 |</td></tr><tr><td>12</td><td>113288124</td><td>2019-12-04 15:09:12.009407</td><td>2019-12-04 15:09:12.008916</td><td>32</td><td>-5</td><td>-1</td><td>3</td><td>113288124 106 116.00 |112831258 32 200.00 |113288100 3 319.00 |</td><td>113288124 30 117.00 |112831258 17 201.00 |113288100 45 321.00 |</td></tr><tr><td>13</td><td>113288133</td><td>2019-12-04 15:09:12.009536</td><td>2019-12-04 15:09:12.009143</td><td>32</td><td>-5</td><td>-1</td><td>3</td><td>113288133 106 116.00 |112831258 32 200.00 |113288100 3 319.00 |</td><td>113288133 30 117.00 |112831258 17 201.00 |113288100 45 321.00 |</td></tr><tr><td>14</td><td>113288147</td><td>2019-12-04 15:09:12.009857</td><td>2019-12-04 15:09:12.009582</td><td>32</td><td>-5</td><td>-1</td><td>3</td><td>113288147 100 116.00 |112831258 32 200.00 |113288100 3 319.00 |</td><td>113288147 30 117.00 |112831258 17 201.00 |113288100 45 321.00 |</td></tr><tr><td>15</td><td>113288152</td><td>2019-12-04 15:09:12.010024</td><td>2019-12-04 15:09:12.009665</td><td>32</td><td>-5</td><td>-1</td><td>3</td><td>113288152 100 116.00 |112831258 32 200.00 |113288100 3 319.00 |</td><td>113288152 30 117.00 |112831258 17 201.00 |113288100 45 321.00 |</td></tr><tr><td>16</td><td>113288155</td><td>2019-12-04 15:09:12.010201</td><td>201Line truncated
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"//BUY:CL_F0J0M0 t:\"2019-11-20T21:21:22.209544500Z\" active_for:210100 ns -0.01:8\n",
"//BUY:CL_F0J0M0 t:\"2019-11-20T21:21:22.211537800Z\" active_for:11000 ns -0.01:7\n",
"//BUY:CL_F0J0M0 t:\"2019-11-20T21:21:22.211548800Z\" active_for:42400 ns -0.01:7\n",
"//BUY:CL_F0J0M0 t:\"2019-11-20T21:21:22.211591200Z\" active_for:296400 ns -0.01:7\n",
"display(synth.Take(15000).OrderBy(x => x.sending_time));\n",
"//Console.WriteLine(synth.Take(10).Count());"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": ".NET (C#)",
"language": "C#",
"name": ".net-csharp"
},
"language_info": {
"file_extension": ".cs",
"mimetype": "text/x-csharp",
"name": "C#",
"pygments_lexer": "csharp",
"version": "8.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
@@ -0,0 +1,309 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Invalid: Command: #r C:/Users/darby/.nuget/packages/google.protobuf/3.6.1/lib/netstandard1.0/Google.Protobuf.dllInvalid: Command: #r C:/Users/darby/.nuget/packages/newtonsoft.json/12.0.3/lib/netstandard2.0/Newtonsoft.Json.dllInvalid: Command: #r C:/Users/darby/.nuget/packages/system.linq.dynamic.core/1.0.19/lib/netstandard2.0/System.Linq.Dynamic.Core.dllInvalid: Command: #r C:/Users/darby/.nuget/packages/mongodb.driver/2.9.3/lib/netstandard1.5/MongoDB.Driver.dllInvalid: Command: #r C:/Users/darby/.nuget/packages/mongodb.driver.core/2.9.3/lib/netstandard1.5/MongoDB.Driver.Core.dllInvalid: Command: #r C:/Users/darby/.nuget/packages/mongodb.bson/2.9.3/lib/netstandard1.5/MongoDB.Bson.dll"
]
}
],
"source": [
"//#r \"nuget:Google.Protobuf, 3.6.1\"\n",
"#r \"C:/Users/darby/.nuget/packages/google.protobuf/3.6.1/lib/netstandard1.0/Google.Protobuf.dll\"\n",
"//#r \"nuget:Microsoft.CSharp, 4.5.0\"\n",
"//#r \"C:/Users/darby/.nuget/packages/microsoft.netcore.app.runtime.linux-x64/3.0.0/runtimes/linux-x64/lib/netcoreapp3.0/Microsoft.CSharp.dll\" \n",
"//#r \"nuget:Newtonsoft.Json, 12.0.3\"\n",
"#r \"C:/Users/darby/.nuget/packages/newtonsoft.json/12.0.3/lib/netstandard2.0/Newtonsoft.Json.dll\"\n",
"//#r \"nuget:System.Linq.Dynamic, 1.0.7\"\n",
"#r \"C:/Users/darby/.nuget/packages/system.linq.dynamic.core/1.0.19/lib/netstandard2.0/System.Linq.Dynamic.Core.dll\"\n",
"//#r \"nuget:MongoDB.Driver, 2.9.3\"\n",
"#r \"C:/Users/darby/.nuget/packages/mongodb.driver/2.9.3/lib/netstandard1.5/MongoDB.Driver.dll\" \n",
"#r \"C:/Users/darby/.nuget/packages/mongodb.driver.core/2.9.3/lib/netstandard1.5/MongoDB.Driver.Core.dll\"\n",
"//#r \"nuget:MongoDB.Bson, 2.9.3\"\n",
"#r \"C:/Users/darby/.nuget/packages/mongodb.bson/2.9.3/lib/netstandard1.5/MongoDB.Bson.dll\" "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Invalid: Command: #r c:/code/bts/spark/build/Debug/bin/netstandard2.0/spark_historical.dll"
]
}
],
"source": [
"#r \"c:/code/bts/spark/build/Debug/bin/netstandard2.0/spark_historical.dll\" \n",
"using bts.history;\n",
"using MongoDB.Driver;\n",
"using MongoDB.Driver.Linq;\n",
"using Google.Protobuf.WellKnownTypes;\n",
"using static bts.history.Query;\n",
"using mbts.co2;\n"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"var q = new Query(\"oil_12_4_2019\", \"bigblue\");\n",
"//Timestamp ts = new Timestamp();\n",
"//ts.Seconds = 15;\n",
"//DateTime dt = ts.ToDateTime();\n",
"//display(dt.Kind);"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"var f = q.collection<HistoryRow>(\"CLH0-CLM0\");\n",
"var j = q.collection<HistoryRow>(\"CLM0-CLZ0\");\n",
"var m = q.collection<HistoryRow>(\"CLH0-CLZ0\");\n",
"\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"//var s = q.series<HistoryRow>(\"ZNZ9-ZNH0\").Where( x=> x.Book != null );\n",
"//var s1 = s.Where( x => x.Book.Bids[0].Size > 0);\n",
"//display(s.Count())\n",
"//var s2 = s.Where( x => x.sending_time >= new DateTime(2019, 11, 25));\n",
"//display(s2.Count());"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"var start = new DateTime(2019, 12, 4, 0, 0, 0);\n",
"var end = new DateTime(2019, 12, 5, 23, 59, 59);\n",
"var ft = q.timeseries<HistoryRow>(f, start, end);\n",
"var jt = q.timeseries<HistoryRow>(j, start, end);\n",
"var mt = q.timeseries<HistoryRow>(m,start, end);\n",
"\n",
"//var ft = q.timeseries<HistoryRow>(f, new DateTime(2019, 11, 20, 21, 21, 22), new DateTime(2019, 11, 20, 21, 21, 23));\n",
"//var jt = q.timeseries<HistoryRow>(j, new DateTime(2019, 11, 20, 21, 21, 22), new DateTime(2019, 11, 20, 21, 21, 23));\n",
"//var mt = q.timeseries<HistoryRow>(m, new DateTime(2019, 11, 20, 21, 21, 22), new DateTime(2019, 11, 20, 21, 21, 23));"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/html": [
"223173"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"273808"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"223254"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"display(ft.Count());\n",
"display(jt.Count());\n",
"display(mt.Count());\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"var ff = ft.Where(x => x.Book != null && x.Book.Bids.Count > 0 && x.Book.Asks.Count > 0);\n",
"var jf = jt.Where(x => x.Book != null && x.Book.Bids.Count > 0 && x.Book.Asks.Count > 0);\n",
"var mf = mt.Where(x => x.Book != null && x.Book.Bids.Count > 0 && x.Book.Asks.Count > 0);\n",
"\n",
"var ftop = ff.AsEnumerable().Select(x => new GetTopOfBook(x)).OrderBy(x => x.sending_time);\n",
"var jtop = jf.AsEnumerable().Select(x => new GetTopOfBook(x)).OrderBy(x => x.sending_time);\n",
"var mtop = mf.AsEnumerable().Select(x => new GetTopOfBook(x)).OrderBy(x => x.sending_time);\n",
"\n",
"//display(h0f.Take(1));\n",
"//display(z9f.Take(1));"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"class TopReport<T> where T : IHasTopBook, IHasTimestamp, Google.Protobuf.IDeepCloneable<T>\n",
"{\n",
" readonly T[] _legs;\n",
" readonly T _synth;\n",
" public TopReport(T[] legs, T synth)\n",
" {\n",
" _legs = (T[])legs.Clone();\n",
" _synth = synth.Clone();\n",
" }\n",
" public uint seq_num => _synth.MsgSeqNum;\n",
" public string sending_time => _synth.sending_time.ToString(\"yyyy-MM-dd HH:mm:ss.ffffff\");\n",
" public string transact_time => _synth.transact_time.ToString(\"yyyy-MM-dd HH:mm:ss.ffffff\");\n",
" public uint bidsize => _synth.TopBook.Bid.Size;\n",
" public double bid => _synth.TopBook.Bid.price();\n",
" public double ask => _synth.TopBook.Ask.price();\n",
" public uint asksize => _synth.TopBook.Ask.Size;\n",
" \n",
" public string[] legbids => _legs.Select( x=> x.MsgSeqNum + \" \" + x.TopBook.Bid.Size + \" \" + \n",
" x.TopBook.Bid.price().ToString(\"0.00 |\") ).ToArray();\n",
" public string[] legasks => _legs.Select( x=> x.MsgSeqNum + \" \" + x.TopBook.Ask.Size + \" \" + \n",
" x.TopBook.Ask.price().ToString(\"0.00 |\") ).ToArray();\n",
"}\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
" "
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [],
"source": [
"\n",
"var synth = create_synthetic_top( new []{ftop, jtop, mtop}, new[] {1.0, 1.0, -1.0}, new[] {1.0, 1.0, -1.0}, \n",
" x => x.TopBook.Bid.price() > 0.0 || x.TopBook.Ask.price() < 0.0, \n",
" (legs, synth)=> new TopReport<GetTopOfBook>(legs, synth));"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/html": [
"<table><thead><tr><th><i>index</i></th><th>seq_num</th><th>sending_time</th><th>transact_time</th><th>bidsize</th><th>bid</th><th>ask</th><th>asksize</th><th>legbids</th><th>legasks</th></tr></thead><tbody><tr><td>0</td><td>113288047</td><td>2019-12-04 15:09:12.008300</td><td>2019-12-04 15:09:12.008131</td><td>32</td><td>-5</td><td>-1</td><td>3</td><td>113288047 106 116.00 |112831258 32 200.00 |113288020 3 319.00 |</td><td>113288047 30 117.00 |112831258 17 201.00 |113288020 40 321.00 |</td></tr><tr><td>1</td><td>113288049</td><td>2019-12-04 15:09:12.008331</td><td>2019-12-04 15:09:12.008144</td><td>32</td><td>-5</td><td>-1</td><td>3</td><td>113288049 106 116.00 |112831258 32 200.00 |113288020 3 319.00 |</td><td>113288049 30 117.00 |112831258 17 201.00 |113288020 40 321.00 |</td></tr><tr><td>2</td><td>113288065</td><td>2019-12-04 15:09:12.008554</td><td>2019-12-04 15:09:12.008279</td><td>32</td><td>-5</td><td>-1</td><td>3</td><td>113288065 106 116.00 |112831258 32 200.00 |113288020 3 319.00 |</td><td>113288065 30 117.00 |112831258 17 201.00 |113288020 40 321.00 |</td></tr><tr><td>3</td><td>113288069</td><td>2019-12-04 15:09:12.008614</td><td>2019-12-04 15:09:12.008330</td><td>32</td><td>-5</td><td>-1</td><td>3</td><td>113288069 106 116.00 |112831258 32 200.00 |113288020 3 319.00 |</td><td>113288069 30 117.00 |112831258 17 201.00 |113288020 40 321.00 |</td></tr><tr><td>4</td><td>113288070</td><td>2019-12-04 15:09:12.008633</td><td>2019-12-04 15:09:12.008330</td><td>32</td><td>-5</td><td>-1</td><td>3</td><td>113288070 106 116.00 |112831258 32 200.00 |113288020 3 319.00 |</td><td>113288070 30 117.00 |112831258 17 201.00 |113288020 40 321.00 |</td></tr><tr><td>5</td><td>113288079</td><td>2019-12-04 15:09:12.008763</td><td>2019-12-04 15:09:12.008346</td><td>32</td><td>-5</td><td>-1</td><td>3</td><td>113288070 106 116.00 |112831258 32 200.00 |113288079 3 319.00 |</td><td>113288070 30 117.00 |112831258 17 201.00 |113288079 40 321.00 |</td></tr><tr><td>6</td><td>113288081</td><td>2019-12-04 15:09:12.008783</td><td>2019-12-04 15:09:12.008350</td><td>32</td><td>-5</td><td>-1</td><td>3</td><td>113288081 106 116.00 |112831258 32 200.00 |113288079 3 319.00 |</td><td>113288081 30 117.00 |112831258 17 201.00 |113288079 40 321.00 |</td></tr><tr><td>7</td><td>113288090</td><td>2019-12-04 15:09:12.008903</td><td>2019-12-04 15:09:12.008420</td><td>32</td><td>-5</td><td>-1</td><td>3</td><td>113288090 106 116.00 |112831258 32 200.00 |113288079 3 319.00 |</td><td>113288090 30 117.00 |112831258 17 201.00 |113288079 40 321.00 |</td></tr><tr><td>8</td><td>113288099</td><td>2019-12-04 15:09:12.009058</td><td>2019-12-04 15:09:12.008476</td><td>32</td><td>-5</td><td>-1</td><td>3</td><td>113288090 106 116.00 |112831258 32 200.00 |113288099 3 319.00 |</td><td>113288090 30 117.00 |112831258 17 201.00 |113288099 45 321.00 |</td></tr><tr><td>9</td><td>113288100</td><td>2019-12-04 15:09:12.009071</td><td>2019-12-04 15:09:12.008477</td><td>32</td><td>-5</td><td>-1</td><td>3</td><td>113288090 106 116.00 |112831258 32 200.00 |113288100 3 319.00 |</td><td>113288090 30 117.00 |112831258 17 201.00 |113288100 45 321.00 |</td></tr><tr><td>10</td><td>113288122</td><td>2019-12-04 15:09:12.009381</td><td>2019-12-04 15:09:12.008909</td><td>32</td><td>-5</td><td>-1</td><td>3</td><td>113288122 106 116.00 |112831258 32 200.00 |113288100 3 319.00 |</td><td>113288122 30 117.00 |112831258 17 201.00 |113288100 45 321.00 |</td></tr><tr><td>11</td><td>113288123</td><td>2019-12-04 15:09:12.009399</td><td>2019-12-04 15:09:12.008910</td><td>32</td><td>-5</td><td>-1</td><td>3</td><td>113288123 106 116.00 |112831258 32 200.00 |113288100 3 319.00 |</td><td>113288123 30 117.00 |112831258 17 201.00 |113288100 45 321.00 |</td></tr><tr><td>12</td><td>113288124</td><td>2019-12-04 15:09:12.009407</td><td>2019-12-04 15:09:12.008916</td><td>32</td><td>-5</td><td>-1</td><td>3</td><td>113288124 106 116.00 |112831258 32 200.00 |113288100 3 319.00 |</td><td>113288124 30 117.00 |112831258 17 201.00 |113288100 45 321.00 |</td></tr><tr><td>13</td><td>113288133</td><td>2019-12-04 15:09:12.009536</td><td>2019-12-04 15:09:12.009143</td><td>32</td><td>-5</td><td>-1</td><td>3</td><td>113288133 106 116.00 |112831258 32 200.00 |113288100 3 319.00 |</td><td>113288133 30 117.00 |112831258 17 201.00 |113288100 45 321.00 |</td></tr><tr><td>14</td><td>113288147</td><td>2019-12-04 15:09:12.009857</td><td>2019-12-04 15:09:12.009582</td><td>32</td><td>-5</td><td>-1</td><td>3</td><td>113288147 100 116.00 |112831258 32 200.00 |113288100 3 319.00 |</td><td>113288147 30 117.00 |112831258 17 201.00 |113288100 45 321.00 |</td></tr><tr><td>15</td><td>113288152</td><td>2019-12-04 15:09:12.010024</td><td>2019-12-04 15:09:12.009665</td><td>32</td><td>-5</td><td>-1</td><td>3</td><td>113288152 100 116.00 |112831258 32 200.00 |113288100 3 319.00 |</td><td>113288152 30 117.00 |112831258 17 201.00 |113288100 45 321.00 |</td></tr><tr><td>16</td><td>113288155</td><td>2019-12-04 15:09:12.010201</td><td>201Line truncated
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"//BUY:CL_F0J0M0 t:\"2019-11-20T21:21:22.209544500Z\" active_for:210100 ns -0.01:8\n",
"//BUY:CL_F0J0M0 t:\"2019-11-20T21:21:22.211537800Z\" active_for:11000 ns -0.01:7\n",
"//BUY:CL_F0J0M0 t:\"2019-11-20T21:21:22.211548800Z\" active_for:42400 ns -0.01:7\n",
"//BUY:CL_F0J0M0 t:\"2019-11-20T21:21:22.211591200Z\" active_for:296400 ns -0.01:7\n",
"display(synth.Take(15000).OrderBy(x => x.sending_time));\n",
"//Console.WriteLine(synth.Take(10).Count());"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": ".NET (C#)",
"language": "C#",
"name": ".net-csharp"
},
"language_info": {
"file_extension": ".cs",
"mimetype": "text/x-csharp",
"name": "C#",
"pygments_lexer": "csharp",
"version": "8.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
@@ -230,14 +230,14 @@ namespace bts.history
private void write_book_or_trade_entry(MarketDataIncrementalRefresh md, MDEntry entry)
{
if (!_books.TryGetValue(entry.security_desc(), out co2.BookMessage book))
{
book = new co2.BookMessage();
OrderBook.init_book(book);
_books[entry.security_desc()] = book;
}
if (entry.MdEntryType == MDEntryType.Bid || entry.MdEntryType == MDEntryType.Offer)
{
if (!_books.TryGetValue(entry.security_desc(), out co2.BookMessage book))
{
book = new co2.BookMessage();
OrderBook.init_book(book);
_books[entry.security_desc()] = book;
}
OrderBook.keep_book(book, entry);
write_book(book, entry, md);
}
@@ -247,7 +247,7 @@ namespace bts.history
if (!_trades.ContainsKey(entry.security_desc()))
_trades[entry.security_desc()] = tm;
write_trade(tm, entry, md);
write_trade(tm, entry, book, md);
}
}
@@ -256,7 +256,7 @@ namespace bts.history
return secdesc;
}
void write_trade(TradeMessage tm, MDEntry entry, MarketDataIncrementalRefresh md)
void write_trade(TradeMessage tm, MDEntry entry, BookMessage book, MarketDataIncrementalRefresh md)
{
string tname = table_name(entry.security_desc());
HistoryRow hist = new HistoryRow
@@ -266,7 +266,8 @@ namespace bts.history
SendingTime = md.SendingTime,
TransactTime = md.TransactTime,
SecurityDesc = entry.security_desc(),
Trade = tm
Trade = tm,
Book = book,
};
var table = _db.GetCollection<HistoryRow>(tname);
table.InsertOne( hist );