Rolling median in python
-
14-11-2019 - |
Question
I have some stock data based on daily close values. I need to be able to insert these values into a python list and get a median for the last 30 closes. Is there a python library that does this?
Solution
Have you considered pandas? It is based on numpy
and can automatically associate timestamps with your data, and discards any unknown dates as long as you fill it with numpy.nan
. It also offers some rather powerful graphing via matplotlib.
Basically it was designed for financial analysis in python.
OTHER TIPS
In pure Python, having your data in a Python list a
, you could do
median = sum(sorted(a[-30:])[14:16]) / 2.0
(This assumes a
has at least 30 items.)
Using the NumPy package, you could use
median = numpy.median(a[-30:])
isn't the median just the middle value in a sorted range?
so, assuming your list is stock_data
:
last_thirty = stock_data[-30:]
median = sorted(last_thirty)[15]
Now you just need to get the off-by-one errors found and fixed and also handle the case of stock_data
being less than 30 elements...
let us try that here a bit:
def rolling_median(data, window):
if len(data) < window:
subject = data[:]
else:
subject = data[-30:]
return sorted(subject)[len(subject)/2]