def _cache_monthly(self, date):
'''
??date???????????????True
:param code:
:param ktype:
:param date:
:return:
'''
print 'caching...'
start, end = self._get_date_range_of_month(date, 'str')
df = ts.get_hist_data(code=self.code, ktype=self.ktype, start=start, end=end, retry_count=6)
if df is not None:
df.to_csv(self._get_cache_filename(date))
waiting_seconds = 0
while not self._in_cache(date=date):
sleep(1)
waiting_seconds += 1
if waiting_seconds > 30:
self._cache_monthly(date=date)
return True
python类get_hist_data()的实例源码
def plot_test():
df = ts.get_hist_data('600415', start='2015-04-01', end='2015-06-18')
# ???????
df.plot()
# ??stock???????
df.high.plot()
# ????????????????
with pd.plot_params.use('x_compat', True):
df.open.plot(color='g')
df.close.plot(color='y')
df.high.plot(color='r')
df.low.plot(color='b')
# ??????????????
with pd.plot_params.use('x_compat', True):
df.high.plot(color='r', figsize=(10, 4), grid='on')
df.low.plot(color='b', figsize=(10, 4), grid='on')
def turnover_check(self):
delta_day = 60 * 7 / 5
end_day = datetime.date(datetime.date.today().year, datetime.date.today().month, datetime.date.today().day)
start_day = end_day - datetime.timedelta(delta_day)
start_day = start_day.strftime("%Y-%m-%d")
end_day = end_day.strftime("%Y-%m-%d")
print start_day
print end_day
for each_code in self.all_code:
try:
df = ts.get_hist_data(code=each_code, start=start_day, end=end_day)
except:
print "Failed to get data"
continue
mv5 = df['v_ma5'][-1]
mv20 = df['v_ma20'][-1]
mv_all = df['volume'].mean()
print
# ??csv??
def get_date_return(dt=None, max_day_try=10):
"""
given a date, return the change value of date dt
:param dt: type datetime
:param max_day_try: type int, to skip stock breaks, default 10
:return: None if invalid, return_next_day otherwise
"""
if type(dt) is not datetime:
return None
assert max_day_try >= 1, 'at least one day'
dt1 = dt
dt2 = dt + timedelta(days=max_day_try)
stock_data = ts.get_hist_data('sh', start=formatDateString(dt1),
end=formatDateString(dt2), retry_count=10)
if stock_data.empty:
return None
return stock_data.as_matrix(['p_change'])[-1]
# since the return value is reversed ordered
def tushare_bundle(environ,
asset_db_writer,
minute_bar_writer,
daily_bar_writer,
adjustment_writer,
calendar,
start_session,
end_session,
cache,
show_progress,
output_dir):
metadata, histories, symbol_map = get_basic_info()
# ????????
asset_db_writer.write(metadata)
# ????dailybar
daily_bar_writer.write(get_hist_data(symbol_map, histories, start_session, end_session), show_progress=show_progress)
# ??,????, ?squant ??
splits, dividends = zipline_splits_and_dividends(symbol_map)
adjustment_writer.write(
splits=pd.concat(splits, ignore_index=True),
dividends=pd.concat(dividends, ignore_index=True),
)
def get_stock(stock_list,start,end,type):
while len(stock_list)>0:
stock_code=stock_list.pop()
df = tushare.get_hist_data(code=stock_code,start=start,end=end,ktype=type)
if(type == 'D'):
stock_code = 'D_'+stock_code
elif(type == 'W'):
stock_code = 'W_'+stock_code
elif(type == 'M'):
stock_code = 'M_'+stock_code
elif(type == '5'):
stock_code = '5_'+stock_code
elif(type == '15'):
stock_code = '15_'+stock_code
elif(type == '30'):
stock_code = '30_'+stock_code
elif(type == '60'):
stock_code = '60_'+stock_code
df.to_csv('E:/Project/Python/STOCK/DataSpider/DataInit/download/'+stock_code+'.csv')
print("save "+stock_code)
#???????????????
#????????????????
def get_stock(stock_list,start,end,type):
while len(stock_list)>0:
stock_code=stock_list.pop()
df = tushare.get_hist_data(code=stock_code,start=start,end=end,ktype=type)
if(type == 'D'):
stock_code = 'D_'+stock_code
elif(type == 'W'):
stock_code = 'W_'+stock_code
elif(type == 'M'):
stock_code = 'M_'+stock_code
elif(type == '5'):
stock_code = '5_'+stock_code
elif(type == '15'):
stock_code = '15_'+stock_code
elif(type == '30'):
stock_code = '30_'+stock_code
elif(type == '60'):
stock_code = '60_'+stock_code
df.to_csv('E:/Project/Python/STOCK/DataSpider/DataUpdate/daily_update_d_download2/'+stock_code+'.csv')
print("save "+stock_code)
#???????????????
#????????????????
def download_economy():
import tushare as ts
path = './data/'
ts.get_money_supply().to_csv(path+'money_supply.csv')
ts.get_gdp_quarter().to_csv(path+'gdp_quarter.csv')
ts.get_gdp_year().to_csv(path + 'gdp_year.csv')
ts.get_cpi().to_csv(path+'cpi.csv')
# ts.get_hist_data('sz').to_csv(path + 'sz.csv')
# ts.get_hist_data('sh').to_csv(path + 'sh.csv')
# import time
import datetime
# now_year = time.localtime().tm_year
# now_mon = time.localtime().tm_mon
# now_day = time.localtime().tm_mday
years = 3
start = datetime.datetime.today().date() + datetime.timedelta(-365*years)
end = datetime.datetime.today().date()
ts.get_k_data('399001', start=str(start), index=True).to_csv(path + 'sz.csv') #??2? ,
ts.get_k_data('000001', start=str(start), index=True).to_csv(path + 'sh.csv')
#??????
ts.get_rrr().to_csv(path + 'rrr.csv')
def index_model(self):
"""
Build a asset protfolio using index model.
"""
hs300_data = ts.get_hist_data('hs300', self.start, self.end)
self.stockdata['hs300'] = hs300_data['close']
for stock_single in self.stocklist:
self.stockdata[stock_single] = ts.get_hist_data(stock_single, self.start, self.end)['close']
returns = (self.stockdata/self.stockdata.shift(1))-1
print('This function will be completed later!')
# To be continued
def csv():
df = ts.get_hist_data('000875')
df.to_csv('c:/day/000875.csv',columns=['open','high','low','close'])
def xls():
df = ts.get_hist_data('000875')
#????
df.to_excel('c:/day/000875.xlsx', startrow=2,startcol=5)
def hdf():
df = ts.get_hist_data('000875')
# df.to_hdf('c:/day/store.h5','table')
store = HDFStore('c:/day/store.h5')
store['000875'] = df
store.close()
def json():
df = ts.get_hist_data('000875')
df.to_json('c:/day/000875.json',orient='records')
#??????
print(df.to_json(orient='records'))
def appends():
filename = 'c:/day/bigfile.csv'
for code in ['000875', '600848', '000981']:
df = ts.get_hist_data(code)
if os.path.exists(filename):
df.to_csv(filename, mode='a', header=None)
else:
df.to_csv(filename)
def check_type():
df = ts.get_hist_data('300141', start=day30, end=day0)
print df.dtypes
print df.index
t1 = df.iloc[0]
print type(t1)
t2 = df[:1]
print type(t2)
print t2.index.values[0]
def empty_type():
id = "300527"
df = ts.get_hist_data(id)
print type(df)
if df is None:
print "None"
else:
print "Not Empty"
def exception_test():
#???????
stockid = '002316'
df = ts.get_hist_data(stockid, start='20160601', end='20160701')
if df.empty:
print "empty"
def plot_test2():
fig = matplotlib.pyplot.gcf()
df = ts.get_hist_data('600415', start='2015-04-01', end='2015-06-18')
with pd.plot_params.use('x_compat', True):
df.high.plot(color='r', figsize=(10, 4), grid='on')
df.low.plot(color='b', figsize=(10, 4), grid='on')
fig.savefig('graph.png')
def get_volume():
code = '600874'
df = ts.get_hist_data(code=code, start='2017-01-01')
vol = df['ma20']
print vol
def two_in_one_canvas():
#fig,ax=plt.subplots(211)
df=ts.get_hist_data('300333','2015-01-01','2016-12-30')
closed=df.close
vol=df.volume/10000
print closed
print vol
#closed.plot()
closed.plot()
vol.plot()
plt.show()
def load_data(self, pcontract, dt_start=None, dt_end=None):
""" ??????????.
Args:
pcontract (PContract): ????
Returns:
DataFrame.
Raises:
FileDoesNotExist
"""
if pcontract.contract.exch_type == 'stock':
import tushare as ts
# ??tushare??
print "load stock data with tushare..."
data = ts.get_hist_data(pcontract.contract.code)
return process_tushare_data(data)
else:
# ????
fname = ''.join([str(pcontract), ".csv"])
try:
data = pd.read_csv(fname, index_col=0, parse_dates=True)
assert data.index.is_unique
except Exception:
#print u"**Warning: File \"%s\" doesn't exist!"%fname
raise FileDoesNotExist(file=fname)
else:
return data
def getHistory(self,id):
data=ts.get_hist_data(id)
print data
def calc_open_day(self,code):
cont=100000000
#total_vol=self.bases[self.bases['code']==code]['totals'].values[0]
acutal_vol=self.bases[self.bases['code']==code]['outstanding'].values[0]
all_vol= acutal_vol*cont
#df= ts.get_hist_data(code)
df1=ts.get_k_data(code)
if len(df1)<3:
return None
#print df1.info()
#df1=df.reset_index()
#print df1
start=df1['date'].values[0]
print 'Start day:', start
df2= df1[(df1['close']==df1['low']) & (df1['high']==df1['low'])]
print self.bases[self.bases['code']==code]['name'].values[0]
end=df2['date'].values[-1]
print "Break day" , end
df3=df1[(df1['date']>=start) & (df1['date']<=end)]
v_total_break=df3['volume'].sum()
l=len(df3)
print l
print v_total_break
rate=v_total_break*100*100.00/all_vol #??? ??
print round(rate,6)
return rate,l
def base_function(self, id):
if id == None:
print "Input stock id please "
return
stockInfo = ts.get_hist_data(id)
# print type(stockInfo)
# print stockInfo.head()
# print stockInfo.dtypes
df = ts.get_stock_basics()
data = df.ix[id]['timeToMarket']
print data
all_data = ts.get_today_all()
print all_data.ix[id]['name']
def date_store(self):
df = ts.get_hist_data("300333")
# df.to_csv("300333.cvs")
df.to_excel("300333.xlsx")
def main():
now = time.strftime("%Y-%m-%d")
# print now
token = '60517739976b768e07823056c6f9cb0fee33ed55a1709b3eaa14a76c6a1b7a56'
sb = StockBox()
# sb.looper(id)
id = '300333'
# sb.realtime(id)
sb.base_function("300333")
# pandas_test=Pandas_test()
# pandas_test.test_function()
# sb.longhuban('2016-04-05')
# sb.getNews()
# sb.fund()
#sb.get_stock_chengfeng()
#sb.date_store()
#sb.profit_test()
#sb.daily_longhu()
# ?????? ?3????
history = ts.get_hist_data(id)
print u"??3????"
print history.head(10)
history_all = ts.get_h_data(id, '20015101', '20160101')
print u'???????'
print history_all
# print type(stockInfo)
# print stockInfo.head()
# print stockInfo.dtypes
#df = ts.get_stock_basics()
#data = df.ix[id]['timeToMarket']
#print data
#ts.get_today_all()
def _break_line(self, codes, k_type):
delta_day = 60 * 7 / 5
end_day = datetime.date(datetime.date.today().year, datetime.date.today().month, datetime.date.today().day)
start_day = end_day - datetime.timedelta(delta_day)
start_day = start_day.strftime("%Y-%m-%d")
end_day = end_day.strftime("%Y-%m-%d")
print start_day
print end_day
all_break = []
for i in codes:
try:
df = ts.get_hist_data(code=i, start=start_day, end=end_day)
if len(df)==0:
continue
except Exception,e:
print e
continue
else:
self.working_count=self.working_count+1
current = df['close'][0]
ma5 = df['ma5'][0]
ma10 = df['ma10'][0]
ma20 = df['ma20'][0]
ma_dict = {'5': ma5, '10': ma10, '20': ma20}
ma_x = ma_dict[k_type]
#print ma_x
if current < ma_x:
print u'??'
print i, " current: ", current
print self.base[self.base['code'] == i]['name'].values[0], " "
print "holding place: " , ma_x
print "Break MA", k_type, "\n"
all_break.append(i)
return all_break
# ????????????????
def _break_line_thread(self,codes,k_type='5'):
delta_day = 60 * 7 / 5
end_day = datetime.date(datetime.date.today().year, datetime.date.today().month, datetime.date.today().day)
start_day = end_day - datetime.timedelta(delta_day)
start_day = start_day.strftime("%Y-%m-%d")
end_day = end_day.strftime("%Y-%m-%d")
print start_day
print end_day
all_break = []
for i in codes:
try:
df = ts.get_hist_data(code=i, start=start_day, end=end_day)
if len(df)==0:
continue
except Exception,e:
print e
continue
else:
self.working_count=self.working_count+1
current = df['close'][0]
ma5 = df['ma5'][0]
ma10 = df['ma10'][0]
ma20 = df['ma20'][0]
ma_dict = {'5': ma5, '10': ma10, '20': ma20}
ma_x = ma_dict[k_type]
#print ma_x
if current > ma_x:
print i, " current: ", current
print self.base[self.base['code'] == i]['name'].values[0], " "
print "Break MA", k_type, "\n"
all_break.append(i)
q.put(all_break)
def stock():
#?????????????????, ?????????
stock_list = {"zsyh":"600036","jsyh":"601939","szzs":"000001","pfyh":"600000","msyh":"600061"}
for stock, code in stock_list.items():
globals()[stock] = tsh.get_hist_data(code,start="2015-01-01",end="2016-04-16")
stock_list2 = stock_list.keys()
#print(stock_list2)
sl = [globals()[st]["close"] for st in stock_list2]
df_close = pd.concat(sl,axis=1,join='inner')
df_close.columns = stock_list2
#print(df_close)
df_close.sort_index(ascending=True,inplace=True) #ascending ??????????????????
pc_ret = df_close.pct_change() #????????????????
print(pc_ret)
make_end_line()
print(pc_ret.mean())
make_end_line()
#????????????
plt.show(sns.jointplot("zsyh","jsyh",pc_ret,kind="hex")) #?? ????????1?????????? 0????? -1????????
plt.show(sns.jointplot("zsyh","jsyh",pc_ret,kind="scatter"))
plt.show(sns.jointplot("zsyh","szzs",pc_ret,kind="scatter"))
plt.show(sns.pairplot(pc_ret[["jsyh","zsyh","pfyh","msyh"]].dropna())) #??????????
print(pc_ret.std()) #????????????????????????????
make_end_line()
rets = pc_ret.dropna()
print(rets.mean())
make_end_line()
area = np.pi *20 #????
plt.scatter(rets.mean(),rets.std()) #???rets?????????xy?
plt.xlabel("Expected Return")#????xy????
plt.ylabel("Risk")
for label,x,y in zip(rets.columns,rets.mean(),rets.std()):
plt.annotate(
label,
xy = (x,y),xytext = (50,50),
textcoords = "offset points",ha = "right",va = "bottom",
arrowprops = dict(arrowstyle = "-",connectionstyle = "arc3,rad=-0.3"))
plt.show()
def get(self):
symbol = self.get_argument("symbol")#????
period = self.get_argument("period")#????,??-??
period_allow_list = ["5","15","30","60","1440","M","W"]
if period not in period_allow_list:
return
data = None
if period =="1440":
data = ts.get_hist_data(symbol)
else:
data = ts.get_k_data(symbol,ktype=period)
print "=========",symbol,":",period
resultlist = []
lens = len(data)
for unit in data.iterrows():
obj = {}
dates = None
if period =="1440":
dates = unit[1].name
else:
dates = unit[1]['date']
print "len",len(dates)
# ????10??%Y-%m-%d??,16??%Y-%m-%d %H:%M ??
dataformate = "%Y-%m-%d %H:%M"
date_len = len(dates)
if date_len == 10 :
dataformate = "%Y-%m-%d"
d=datetime.datetime.strptime(dates,dataformate)
obj["date"]=int(time.mktime(d.timetuple()))
obj["open"]=unit[1]['open']
obj["close"]=unit[1]['close']
obj["high"]=unit[1]['high']
obj["low"]=unit[1]['low']
obj["volume"]=unit[1]['volume']
resultlist.append(obj)
resultlist.sort(key=lambda obj:obj.get('date'), reverse=False)
s = json.dumps(resultlist)
self.write(s)