0
$\begingroup$

I have found the following code in the book Python for Finance by Yuxing Yan, in page 267 for estimating Amihud's illiquidity

import numpy as np 
import statsmodels.api as sm 
from matplotlib.finance import quotes_historical_yahoo_ochl as getData 
begdate=(2013,10,1) 
enddate=(2013,10,30) 
ticker='IBM'                   # or WMT  
data= getData(ticker, begdate, enddate,asobject=True, adjusted=True) 
p=np.array(data.aclose) 
dollar_vol=np.array(data.volume*p) 
ret=np.array((p[1:] - p[:-1])/p[1:]) 
illiq=np.mean(np.divide(abs(ret),dollar_vol[1:])) 
print("Aminud illiq for =",ticker,illiq) 

The matplotlib.finance has been deprecated.The new module does not support collection of financial data, so I found an other way to collect them:

import pandas as pd
import pandas_datareader.data as web
end = '2013-10-30'
start = '2013-10-1'
get_px = lambda x: web.DataReader(x, 'yahoo', start=start, end=end)['Adj Close']

symbols = ['IBM']
data = pd.DataFrame({sym:get_px(sym) for sym in symbols})
data = data.rename({'IBM': 'Adj Close'}, axis=1)
p1 = data
p = p1['Adj Close'].ravel()

So far so good.But I don't know from the original code what the data.volume does and how I can translate dollar_vol=np.array(data.volume*p) with the existing functions of any module in Python.

$\endgroup$

1 Answer 1

1
$\begingroup$

From the documentation of matplotlib.finance (under parse_yahoo_historical_ochl(...)) it is specified that the dollars traded/dollar-volume is the unadjusted volume multiplied by the adjusted closing prices of the given ticker (At quotes_yahoo_historical_ochl(...) they refer to the above function in order to understand the output format):

adjusted : bool

If True (default) replace open, close, high, low prices with their adjusted values. The adjustment is by a scale factor, S = adjusted_close/close. Adjusted prices are actual prices multiplied by S.

Volume is not adjusted as it is already backward split adjusted by Yahoo. If you want to compute dollars traded, multiply volume by the adjusted close, regardless of whether you choose adjusted = True|False.

Here, data.volume gets the corresponding "unadjusted" volume array from the getData output. If you want to replicate dollar_vol you need to get the backwards split-adjusted volume (eg. from Yahoo finance) for your corresponding ticker as it seems you already have the adjusted close prices.

If you do something a lá:

from pandas_datareader import data

IBM = data.DataReader("IBM", 
                       start='2013-10-1', 
                       end='2013-10-30', 
                       data_source='yahoo')

dollar_vol = IBM['Volume'] * IBM['Adj Close']

You should get the dollar_volume/dollars_traded as described in the documentation and in your first code snippet.

$\endgroup$
5
  • 1
    $\begingroup$ Thank you very much for your answer.Clear and precise $\endgroup$
    – user57440
    Jan 20 at 1:03
  • $\begingroup$ Somewhere in the book it uses the data.aclose what does this code according to the documentation (I cannot understand it)?Because it says "adjusted : bool , If True (default) replace open, high, low, close prices with their adjusted values.".So are the adjusted values.The data.aclose` does what ? $\endgroup$
    – user57440
    Jan 20 at 10:58
  • $\begingroup$ @HungryHomer data.aclose gets the adjusted close prices from the data output. There are examples here that uses the call data.aclose to construct log-returns. It is fairly standard practice to construct (log-)returns from adjusted close prices. See if it makes sense in terms of the book. $\endgroup$
    – Pleb
    Jan 20 at 11:30
  • $\begingroup$ Agreed.But since it the Adjusted is set to True in the function this doesn't mean that I must have the adjusted returns as the output?Why the data.aclose is needed again? Sorry for the confusion $\endgroup$
    – user57440
    Jan 20 at 11:36
  • $\begingroup$ I believe that adjusted close prices is outputtet regardless of the boolean value of "adjusted" and might be accessed via the call data.aclose in both cases. This is also indirectly specified in the above quote (last paragraph) where they argue that you can calculate dollar traded by multiplying volume with adjusted close prices regardless of the boolean value of "adjusted". However, I haven't verified this, so see this as a qualified guess. $\endgroup$
    – Pleb
    Jan 20 at 12:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy