is not median only average value in sorted range?
so if your list is stock_data :
last_thirty = stock_data[-30:] median = sorted(last_thirty)[15]
Now you just need to find the errors found and corrected, as well as handle the case of stock_data less than 30 elements ...
let's try a little here:
def rolling_median(data, window): if len(data) < window: subject = data[:] else: subject = data[-30:] return sorted(subject)[len(subject)/2]
source share