{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Introduction to Volume, Slippage, and Liquidity\n", "by Delaney Mackenzie and Maxwell Margenot\n", "\n", "Part of the Quantopian Lecture Series:\n", "\n", "* [www.quantopian.com/lectures](www.quantopian.com/lectures)\n", "* [github.com/quantopian/research_public](github.com/quantopian/research_public)\n", "\n", "\n", "\n", "## What is Volume?\n", "Volume is how much trading occurs for a given instrument, or set of instruments, over a given time period. \n", "\n", "### Example Volume Computation\n", "For instance, consider a hypothetical equity asset $A$. If, over the course of a minute, $100,000$ shares of $A$ are bought, then the shares traded volume of $A$ is $100,000$. To find the dollar volume traded for $A$, which is the more often-used statistic, we need to take a weighted average of all the different prices $A$ traded for over the minute times the number of shares that traded at each price. This is equivalent to adding up the dollar volumes of all the individual trasnactions that occured. For instance, let's say in this case there were there separate transactions. One for 30,000 shares, one for $60,000$ shares, and one for $10,000$ shares. The prices were $30$ USD, $31$ USD, and $33$ USD, respectively. Let's model this out." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "3090000" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "num_shares = np.asarray([30000, 60000, 10000])\n", "prices = np.asarray([30, 31, 33])\n", "\n", "np.dot(num_shares, prices)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "So total dollar volume is $3.09$ million USD. Notice that this is equivalent to taking the dollar volume averaged price and multiplying by the number of shares traded over that bar." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Average trade price: 31.3333333333\n", "Volume weighted average trade price: 30.9\n", "Volume: 3090000.0\n" ] } ], "source": [ "# Get the average trade price\n", "print \"Average trade price: %s\" % (np.mean(prices))\n", "\n", "# Get the volume weighted average\n", "vw = np.dot(num_shares, prices) / float(np.sum(num_shares))\n", "print \"Volume weighted average trade price: %s\" % (vw)\n", "\n", "# Go back to volume\n", "v = vw * np.sum(num_shares)\n", "print \"Volume: %s\" % (v)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Often in real datasets you will be given averaged or 'last traded' data rather than individual trades. With averaged data, some average is taken over a bar (time period). With last traded data, only the final observation from that bar is reported. It is important to know if the data is averaged, volume averaged, or simply last traded. All of these will need to be treated differently.\n", "\n", "For pricing data, Quantopian currently (as of April 2017) provides the last traded prices at a minute resolution. The volume is the sum of all volume in that bar. While we do not offer minutely volume-weighted price, daily volume-weighted price can be approximated from the minute bars.\n", "\n", "Let's look at some volume data." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "data = get_pricing('SPY', start_date='2015-6-1', end_date='2015-6-20', frequency='minute')" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", " | open_price | \n", "high | \n", "low | \n", "close_price | \n", "volume | \n", "price | \n", "
---|---|---|---|---|---|---|
2015-06-01 13:31:00+00:00 | \n", "210.914 | \n", "211.033 | \n", "210.834 | \n", "211.013 | \n", "1130598.0 | \n", "211.013 | \n", "
2015-06-01 13:32:00+00:00 | \n", "211.013 | \n", "211.083 | \n", "210.993 | \n", "211.043 | \n", "860265.0 | \n", "211.043 | \n", "
2015-06-01 13:33:00+00:00 | \n", "211.043 | \n", "211.048 | \n", "210.934 | \n", "210.964 | \n", "466341.0 | \n", "210.964 | \n", "
2015-06-01 13:34:00+00:00 | \n", "210.964 | \n", "210.983 | \n", "210.844 | \n", "210.854 | \n", "305119.0 | \n", "210.854 | \n", "
2015-06-01 13:35:00+00:00 | \n", "210.864 | \n", "210.894 | \n", "210.725 | \n", "210.725 | \n", "420599.0 | \n", "210.725 | \n", "