def load_data(indexes=None,stockList=None,start=None,end=None,adjusted=True):
"""
load stocks from Mongo
"""
assert indexes is not None or stockList is not None, """
must specify stockList or indexes"""
if start is None:
start = "1990-01-01"
if start is not None and end is not None:
startdate = datetime.datetime.strptime(start, "%Y-%m-%d")
enddate=datetime.datetime.strptime(end, "%Y-%m-%d")
assert startdate < enddate, "start date is later than end date."
data = OrderedDict()
l=LoadDataCVS(constants.IP,constants.PORT)
l.Conn()
if stockList=="hs300" or stockList=="zz500" or stockList=="sz50" or stockList=="all":
stocks=l.getstocklist(stockList)
else:
stocks=stockList
#print stocks
if stocks is not None:
for stock in stocks:
stkd= l.getstockdaily(stock,start,end)
data[stock] = stkd
if indexes is not None:
for name, ticker in iteritems(indexes):
logger.info('Loading index: {} ({})'.format(name, ticker))
stkd= l.getindexdaily(indexes,start,end)
data[name] = stkd
panel = pd.Panel(data)
panel.minor_axis = ['open', 'high', 'low', 'close', 'volume', 'price','change','code']
panel.major_axis = panel.major_axis.tz_localize(pytz.utc)
#close the connection
l.Close()
# Adjust data
if adjusted:
adj_cols = ['open', 'high', 'low', 'close']
for ticker in panel.items:
ratio = (panel[ticker]['price'] / panel[ticker]['close'])
ratio_filtered = ratio.fillna(0).values
for col in adj_cols:
panel[ticker][col] *= ratio_filtered
return panel
评论列表
文章目录