Skip to content

Instantly share code, notes, and snippets.

Created January 16, 2016 06:39
Show Gist options
  • Save anonymous/da8611d384b7beccf32d to your computer and use it in GitHub Desktop.
Save anonymous/da8611d384b7beccf32d to your computer and use it in GitHub Desktop.
order_book.ipynb
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": "",
"signature": "sha256:6f0f89ee8cde844aa4eeea3d2aff4fecf57830930690f6981ae14862c51584bf"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"import numpy as np\n",
"import pandas as pd"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"columns = \"timestamp, id, price, exchange, size\".split(\", \")\n",
"records = [l.strip().split(\", \") for l in\n",
" \"\"\"9:00:25.123, 1, 1.02, N, 100\n",
" 9:00:25.123, 2, 1.02, N, -50\n",
" 9:00:25.129, 3, 1.03, X, 50\n",
" 9:00:25.130, 4, 1.02, X, 150\n",
" 9:00:25.131, 5, 1.02, X, -5\n",
" 9:00:25.132, 6, 1.03, X, 50\"\"\".split(\"\\n\")]"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"data = pd.DataFrame.from_records(records, columns=columns)\n",
"data[\"id\"] = data[\"id\"].astype(np.int64)\n",
"#data[\"timestamp\"] = data[\"timestamp\"].apply(pd.Timestamp)\n",
"for column in [\"price\", \"size\"]:\n",
" data[column] = data[column].astype(np.float64)\n",
"#data = data.set_index(\"timestamp\")"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"data"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>timestamp</th>\n",
" <th>id</th>\n",
" <th>price</th>\n",
" <th>exchange</th>\n",
" <th>size</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td> 9:00:25.123</td>\n",
" <td> 1</td>\n",
" <td> 1.02</td>\n",
" <td> N</td>\n",
" <td> 100</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td> 9:00:25.123</td>\n",
" <td> 2</td>\n",
" <td> 1.02</td>\n",
" <td> N</td>\n",
" <td> -50</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td> 9:00:25.129</td>\n",
" <td> 3</td>\n",
" <td> 1.03</td>\n",
" <td> X</td>\n",
" <td> 50</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td> 9:00:25.130</td>\n",
" <td> 4</td>\n",
" <td> 1.02</td>\n",
" <td> X</td>\n",
" <td> 150</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td> 9:00:25.131</td>\n",
" <td> 5</td>\n",
" <td> 1.02</td>\n",
" <td> X</td>\n",
" <td> -5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td> 9:00:25.132</td>\n",
" <td> 6</td>\n",
" <td> 1.03</td>\n",
" <td> X</td>\n",
" <td> 50</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 4,
"text": [
" timestamp id price exchange size\n",
"0 9:00:25.123 1 1.02 N 100\n",
"1 9:00:25.123 2 1.02 N -50\n",
"2 9:00:25.129 3 1.03 X 50\n",
"3 9:00:25.130 4 1.02 X 150\n",
"4 9:00:25.131 5 1.02 X -5\n",
"5 9:00:25.132 6 1.03 X 50"
]
}
],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"df = data.set_index([\"timestamp\"])"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 5
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"df"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>id</th>\n",
" <th>price</th>\n",
" <th>exchange</th>\n",
" <th>size</th>\n",
" </tr>\n",
" <tr>\n",
" <th>timestamp</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>9:00:25.123</th>\n",
" <td> 1</td>\n",
" <td> 1.02</td>\n",
" <td> N</td>\n",
" <td> 100</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9:00:25.123</th>\n",
" <td> 2</td>\n",
" <td> 1.02</td>\n",
" <td> N</td>\n",
" <td> -50</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9:00:25.129</th>\n",
" <td> 3</td>\n",
" <td> 1.03</td>\n",
" <td> X</td>\n",
" <td> 50</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9:00:25.130</th>\n",
" <td> 4</td>\n",
" <td> 1.02</td>\n",
" <td> X</td>\n",
" <td> 150</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9:00:25.131</th>\n",
" <td> 5</td>\n",
" <td> 1.02</td>\n",
" <td> X</td>\n",
" <td> -5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9:00:25.132</th>\n",
" <td> 6</td>\n",
" <td> 1.03</td>\n",
" <td> X</td>\n",
" <td> 50</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 6,
"text": [
" id price exchange size\n",
"timestamp \n",
"9:00:25.123 1 1.02 N 100\n",
"9:00:25.123 2 1.02 N -50\n",
"9:00:25.129 3 1.03 X 50\n",
"9:00:25.130 4 1.02 X 150\n",
"9:00:25.131 5 1.02 X -5\n",
"9:00:25.132 6 1.03 X 50"
]
}
],
"prompt_number": 6
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"states = []\n",
"current_timestamp = None\n",
"current_state = {}"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 7
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"for timestamp, (id_, price, exch, size) in df.iterrows():\n",
" if current_timestamp is None:\n",
" current_timestamp = timestamp\n",
" if current_timestamp != timestamp:\n",
" for key in list(current_state):\n",
" if current_state[key] == 0.:\n",
" del current_state[key]\n",
" states.append((current_timestamp, dict(**current_state)))\n",
" current_timestamp = timestamp\n",
" key = (exch, price)\n",
" current_state.setdefault(key, 0.)\n",
" current_state[key] += size\n",
"states.append((timestamp, dict(**current_state)))"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 8
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"states"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 9,
"text": [
"[('9:00:25.123', {('N', 1.02): 50.0}),\n",
" ('9:00:25.129', {('N', 1.02): 50.0, ('X', 1.03): 50.0}),\n",
" ('9:00:25.130', {('N', 1.02): 50.0, ('X', 1.02): 150.0, ('X', 1.03): 50.0}),\n",
" ('9:00:25.131', {('N', 1.02): 50.0, ('X', 1.02): 145.0, ('X', 1.03): 50.0}),\n",
" ('9:00:25.132', {('N', 1.02): 50.0, ('X', 1.02): 145.0, ('X', 1.03): 100.0})]"
]
}
],
"prompt_number": 9
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"order_book = pd.DataFrame.from_items(states).T"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 10
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"order_book"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr>\n",
" <th></th>\n",
" <th>N</th>\n",
" <th colspan=\"2\" halign=\"left\">X</th>\n",
" </tr>\n",
" <tr>\n",
" <th></th>\n",
" <th>1.02</th>\n",
" <th>1.02</th>\n",
" <th>1.03</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>9:00:25.123</th>\n",
" <td> 50</td>\n",
" <td> NaN</td>\n",
" <td> NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9:00:25.129</th>\n",
" <td> 50</td>\n",
" <td> NaN</td>\n",
" <td> 50</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9:00:25.130</th>\n",
" <td> 50</td>\n",
" <td> 150</td>\n",
" <td> 50</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9:00:25.131</th>\n",
" <td> 50</td>\n",
" <td> 145</td>\n",
" <td> 50</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9:00:25.132</th>\n",
" <td> 50</td>\n",
" <td> 145</td>\n",
" <td> 100</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 11,
"text": [
" N X \n",
" 1.02 1.02 1.03\n",
"9:00:25.123 50 NaN NaN\n",
"9:00:25.129 50 NaN 50\n",
"9:00:25.130 50 150 50\n",
"9:00:25.131 50 145 50\n",
"9:00:25.132 50 145 100"
]
}
],
"prompt_number": 11
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"order_book.loc[\"9:00:25.130\"]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 12,
"text": [
"N 1.02 50\n",
"X 1.02 150\n",
" 1.03 50\n",
"Name: 9:00:25.130, dtype: float64"
]
}
],
"prompt_number": 12
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment