{ "cells": [ { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "Testing Mean Reversion Code" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ ], "source": [ "from datetime import datetime" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ ], "source": [ "from pandas_datareader import data" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ ], "source": [ "import pandas as pd" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ ], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ ], "source": [ "from numpy import log, polyfit, sqrt, std, subtract" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/ext/anaconda3/lib/python3.5/site-packages/statsmodels/compat/pandas.py:56: FutureWarning: The pandas.core.datetools module is deprecated and will be removed in a future version. Please use the pandas.tseries module instead.\n", " from pandas.core import datetools\n" ] } ], "source": [ "import statsmodels.tsa.stattools as ts" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ ], "source": [ "import statsmodels.api as sm" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ ], "source": [ "import matplotlib.pyplot as plt" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ ], "source": [ "import seaborn as sns" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ ], "source": [ "import pprint" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ ], "source": [ "symbList = ['GOOG','FB']" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [ ], "source": [ "start_date = '2008/01/01'" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ ], "source": [ "end_date = '2018/01/01'" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
DateSPXAAPLMSFTGOOGAMZNFBSBUX
02000-08-311517.6800542.93525525.539095NaN41.5000NaN3.735864
12000-09-011520.7700203.05567625.676279NaN41.5000NaN3.844242
22000-09-051507.0799563.00750825.653412NaN45.6875NaN3.837867
32000-09-061492.2500002.81483525.401903NaN45.8750NaN3.767739
42000-09-071502.5100102.98643425.630543NaN43.5000NaN3.837867
\n", "
" ] }, "execution_count": 20, "metadata": { }, "output_type": "execute_result" } ], "source": [ "# Fetching data and initializing the IV column\n", "#here's the way to add a new column: Initialize it, ha!\n", "\n", "dataAll = pd.read_csv(\"CompaniesSP.csv\")\n", "#create a new column and give value 0, just to do something new in the data file\n", "dataAll['IV'] = 0\n", "\n", "#take the first8 columns of the file, iterating name range\n", "data=dataAll.loc[:, 'Date':'SBUX']\n", "\n", "data.head()" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [ ], "source": [ "tickers = ['AAPL', 'MSFT']" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] }, { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
SPXAAPLMSFTGOOGAMZNFBSBUXSPX_retAAPL_retMSFT_ret...AMZN_ret_HV21_vol_premFB_ret_HV21_vol_premSBUX_ret_HV21_vol_premSPX_ret_VcorrAAPL_ret_VcorrMSFT_ret_VcorrGOOG_ret_VcorrAMZN_ret_VcorrFB_ret_VcorrSBUX_ret_Vcorr
count4442.0000004442.0000004442.0000003448.0000004442.0000001496.0000004442.0000004441.0000004441.0000004441.000000...3723.0000001136.0000003723.0000004.190000e+034190.0000004190.0000003196.0000004190.0000001244.0000004190.000000
mean1458.84083341.35900329.691175406.808014239.16095090.67563520.9031470.0001980.0012410.000466...21.95920419.14991411.3698731.000000e+000.5359580.6475270.5734600.4992910.4731030.574429
std466.50538248.12396516.563054256.515001309.53162649.05033118.2692190.0120500.0251530.018710...9.67475310.1345657.0603191.295721e-140.1297060.1212250.1299510.0923320.1498390.106975
min676.5300290.63196812.00209149.6818665.97000017.7300002.856087-0.090350-0.518691-0.118155...4.8315704.221414-0.4770961.000000e+000.1939320.3646240.0863750.2987100.0823680.234126
25%1131.4400333.42621020.062912229.44145238.90500149.6899996.667941-0.004764-0.010371-0.008068...14.34920211.5268876.8278201.000000e+000.4341080.5770420.5051720.4268520.4423050.500515
50%1309.82501217.59925122.606724298.44639589.00500181.97500312.4749780.0005380.0007650.000117...21.42819416.1176659.1675231.000000e+000.5464150.6655090.5898890.4874280.5236600.571625
75%1782.16497866.53313832.544173555.451157306.270004125.40500033.6790040.0056160.0129220.008630...27.53177932.49646014.9036051.000000e+000.6513330.7488570.6591550.5556570.5649860.648543
max2872.870117181.02195796.3523711175.8399661598.390015193.08999663.2653200.1158000.1390500.195652...45.25038338.78854633.3399791.000000e+000.7509710.8193690.8254430.7612140.6731410.786925
\n", "

8 rows × 35 columns

\n", "
" ] }, "execution_count": 22, "metadata": { }, "output_type": "execute_result" } ], "source": [ "from pandas import read_csv\n", "series = read_csv('CompaniesSP.csv', header=0, parse_dates=[0], index_col=0, squeeze=True)\n", "print(type(series))\n", "series.describe()" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Populating the interactive namespace from numpy and matplotlib\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "/ext/anaconda3/lib/python3.5/site-packages/IPython/core/magics/pylab.py:160: UserWarning: pylab import has clobbered these variables: ['datetime']\n", "`%matplotlib` prevents importing * from pylab and numpy\n", " \"\\n`%matplotlib` prevents importing * from pylab and numpy\"\n" ] }, { "data": { "text/plain": [ "" ] }, "execution_count": 23, "metadata": { }, "output_type": "execute_result" }, { "name": "stderr", "output_type": "stream", "text": [ "WARNING: Some output was deleted.\n" ] } ], "source": [ "# This line is necessary for the plot to appear in a Jupyter notebook (you don't need it if you're working in Rodeo)\n", "%matplotlib inline\n", "# Control the default size of figures in this Jupyter notebook (n/a if working in Rodeo)\n", "%pylab inline\n", "pylab.rcParams['figure.figsize'] = (15, 9) # Change the size of plots\n", "\n", "# Plot the adjusted closing price of AAPL\n", "series.loc[:, 'AAPL':'SBUX'].plot(grid = True)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 24, "metadata": { }, "output_type": "execute_result" }, { "name": "stderr", "output_type": "stream", "text": [ "WARNING: Some output was deleted.\n" ] } ], "source": [ "series.loc[:, 'AAPL'].plot(grid = True)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 25, "metadata": { }, "output_type": "execute_result" }, { "name": "stderr", "output_type": "stream", "text": [ "WARNING: Some output was deleted.\n" ] } ], "source": [ "#create changes and add the corresponding new columns\n", "series['GOOG_Ret'] = series['GOOG'].pct_change().dropna()\n", "series['SPX_Ret'] = series['SPX'].pct_change().dropna()\n", "series['AAPL_Ret'] = series['AAPL'].pct_change().dropna()\n", "\n", "#rolling correlation of google and sp500 of returns yearly (250 days)\n", "series['GOOG_Ret'].rolling(252).corr(series['SPX_Ret']).plot(figsize=(15,4))" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 26, "metadata": { }, "output_type": "execute_result" }, { "name": "stderr", "output_type": "stream", "text": [ "WARNING: Some output was deleted.\n" ] } ], "source": [ "#create the historical 30day vol\n", "series['SPX_HV21'] = series['SPX_Ret'].rolling(21).std() * np.sqrt(252) * 100\n", "series['GOOG_HV21'] = series['GOOG_Ret'].rolling(21).std() * np.sqrt(252) * 100\n", "series['AAPL_HV21'] = series['AAPL_Ret'].rolling(21).std() * np.sqrt(252) * 100\n", "\n", "series[['GOOG_HV21','AAPL_HV21','SPX_HV21']].plot(figsize=(15,8), grid= True)" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": false }, "outputs": [ ], "source": [ "AAPL_TS=series['AAPL_Ret']" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "collapsed": false }, "outputs": [ ], "source": [ "def hurst(AAPL_TS):\n", " # Create the range of lag values\n", " lags= range (2,100)\n", " # Calculate the array of variances of the lagged differences\n", " tau=[sqrt(std(subtract(AAPL_TS[lag:], AAPL_TS[-lag]))) for lag in lags]\n", " # Use a linear fit to estimate the hurst exponent\n", " poly = polyfit(log(lags), log(tau), 1)\n", " # Return the hurst exponent from the polyfit output\n", " return poly[0]*2.0" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "-0.029860508371503839" ] }, "execution_count": 29, "metadata": { }, "output_type": "execute_result" } ], "source": [ "hurst(AAPL_TS)" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 30, "metadata": { }, "output_type": "execute_result" }, { "name": "stderr", "output_type": "stream", "text": [ "WARNING: Some output was deleted.\n" ] } ], "source": [ "series.loc[:, 'AAPL_Ret'].plot(grid = True)" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "collapsed": false }, "outputs": [ ], "source": [ "AAPL_Price=series['AAPL']" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "collapsed": false }, "outputs": [ ], "source": [ "def hurst(AAPL_Price):\n", " # Create the range of lag values\n", " lags= range (2,100)\n", " # Calculate the array of variances of the lagged differences\n", " tau=[sqrt(std(subtract(AAPL_Price[lag:], AAPL_Price[-lag]))) for lag in lags]\n", " # Use a linear fit to estimate the hurst exponent\n", " poly = polyfit(log(lags), log(tau), 1)\n", " # Return the hurst exponent from the polyfit output\n", " return poly[0]*2.0" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "0.0010023252339726358" ] }, "execution_count": 34, "metadata": { }, "output_type": "execute_result" } ], "source": [ "hurst(AAPL_Price)" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "collapsed": false }, "outputs": [ ], "source": [ "AAPL_Price2=series['2000-08-31':'2008-08-31']['AAPL']" ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "Date\n", "2000-08-31 2.935255\n", "2000-09-01 3.055676\n", "2000-09-05 3.007508\n", "2000-09-06 2.814835\n", "2000-09-07 2.986434\n", "Name: AAPL, dtype: float64" ] }, "execution_count": 36, "metadata": { }, "output_type": "execute_result" } ], "source": [ "AAPL_Price2.head()" ] }, { "cell_type": "code", "execution_count": 37, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "Date\n", "2008-08-25 16.622875\n", "2008-08-26 16.727882\n", "2008-08-27 16.827114\n", "2008-08-28 16.737513\n", "2008-08-29 16.331944\n", "Name: AAPL, dtype: float64" ] }, "execution_count": 37, "metadata": { }, "output_type": "execute_result" } ], "source": [ "AAPL_Price2.tail()" ] }, { "cell_type": "code", "execution_count": 38, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "0.00350593165566435" ] }, "execution_count": 38, "metadata": { }, "output_type": "execute_result" } ], "source": [ "hurst(AAPL_Price2)" ] }, { "cell_type": "code", "execution_count": 39, "metadata": { "collapsed": false }, "outputs": [ ], "source": [ "AAPL_Price3=series['2008-08-31':'2018-04-30']['AAPL']" ] }, { "cell_type": "code", "execution_count": 40, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "-0.0052954428489820054" ] }, "execution_count": 40, "metadata": { }, "output_type": "execute_result" } ], "source": [ "hurst(AAPL_Price3)" ] }, { "cell_type": "code", "execution_count": 41, "metadata": { "collapsed": false }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "WARNING: Some output was deleted.\n" ] } ], "source": [ "AAPL_Price3=series['2008-08-31':'2018-04-30']['AAPL'].plot()" ] }, { "cell_type": "code", "execution_count": 42, "metadata": { "collapsed": false }, "outputs": [ ], "source": [ "AAPL_Price16=series['2008-08-31':'2016-04-30']['AAPL']" ] }, { "cell_type": "code", "execution_count": 43, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "-0.0067036462086532758" ] }, "execution_count": 43, "metadata": { }, "output_type": "execute_result" } ], "source": [ "hurst(AAPL_Price16)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "collapsed": false }, "outputs": [ ], "source": [ ] } ], "metadata": { "kernelspec": { "display_name": "SageMath (stable)", "language": "sagemath", "metadata": { "cocalc": { "description": "Open-source mathematical software system", "priority": 10, "url": "https://www.sagemath.org/" } }, "name": "sagemath" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.15" } }, "nbformat": 4, "nbformat_minor": 0 }