Skip to content

Instantly share code, notes, and snippets.

@szs8
Created September 7, 2012 19:44
Show Gist options
  • Save szs8/3669040 to your computer and use it in GitHub Desktop.
Save szs8/3669040 to your computer and use it in GitHub Desktop.
marking quote price levels in pandas dataframe
In [25]: quotes.head()
Out[25]:
bid ask bsize asize
2012-09-06 09:30:00.026000 13.34 13.44 3 16
2012-09-06 09:30:00.043000 13.34 13.44 3 17
2012-09-06 09:30:00.121000 13.36 13.65 1 10
2012-09-06 09:30:00.386000 13.36 13.52 21 1
2012-09-06 09:30:00.440000 13.40 13.44 15 17
In [26]: quotes["mid"] = 0.5 * (quotes.bid + quotes.ask)
In [27]: quotes["levels"] = np.cumsum((quotes.mid.shift(1) != quotes.mid).astype(np.int))
In [29]: quotes.head()
Out[29]:
bid ask bsize asize mid levels
2012-09-06 09:30:00.026000 13.34 13.44 3 16 13.390 1
2012-09-06 09:30:00.043000 13.34 13.44 3 17 13.390 1
2012-09-06 09:30:00.121000 13.36 13.65 1 10 13.505 2
2012-09-06 09:30:00.386000 13.36 13.52 21 1 13.440 3
2012-09-06 09:30:00.440000 13.40 13.44 15 17 13.420 4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment