python - Pandas and finance calculations -


i new using python, , working on stock analysis script. idea script take in stock symbol, , script calculate sharpe ratio, treynor ratio, , other financial information. right now, having trouble getting pandas work properly. unable access column dataframe calculate yield stock.

from pandas.io.data import datareader datetime import date, timedelta    def calc_yield(now, old):     return (now-old)/old   def yield_array(cl):     array = []     count = 0     in cl:         old = cl[count]         count += 1         new = cl[count]         array.append(calc_yield(new, old))     return array   market = '^gspc' ticker = "aapl" days = 10  # set start , end dates edate = date.today() - timedelta(days=1) sdate = edate - timedelta(days=days)  # read stock price data yahoo data = datareader(ticker, 'yahoo', start=sdate, end=edate)  close = data['adj close']   print yield_array(close) 

error:

/users/tim/anaconda/bin/python "/users/tim/pycharmprojects/test2/module tests.py" traceback (most recent call last):   file "/users/tim/pycharmprojects/test2/module tests.py", line 35, in <module>     print yield_array(close)   file "/users/tim/pycharmprojects/test2/module tests.py", line 16, in yield_array     new = cl[count]   file "/users/tim/anaconda/lib/python2.7/site-packages/pandas/core/series.py", line 484, in __getitem__     result = self.index.get_value(self, key)   file "/users/tim/anaconda/lib/python2.7/site-packages/pandas/tseries/index.py", line 1243, in get_value     return _maybe_box(self, index.get_value(self, series, key), series, key)   file "/users/tim/anaconda/lib/python2.7/site-packages/pandas/core/index.py", line 1202, in get_value     return tslib.get_value_box(s, key)   file "tslib.pyx", line 540, in pandas.tslib.get_value_box (pandas/tslib.c:11833)   file "tslib.pyx", line 555, in pandas.tslib.get_value_box (pandas/tslib.c:11680) indexerror: index out of bounds  process finished exit code 1 

i think see problem. given function:

def yield_array(cl):     array = []     count = 0     in cl:         old = cl[count]         count += 1         print count         new = cl[count]         array.append(calc_yield(new, old))         print old         print new     return array 

the problem on last item of cl, add 1 count, result in index 1 greater maximum index of cl. results in error, because trying access index doesn't exist. need for in cl[:-1], skip last element.

however, there simpler way vectorizing. can reduce whole function to:

close = data['adj close'] yield_data = close.diff()/close.shift(1) 

or better yet, can put result in dataframe later use:

close = data['adj close'] data['yield'] = close.diff()/close.shift(1) 

Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -