def get_hist_data_cache(code, date_start, date_end):
cache_file = bash_stock_tmp + "%s^%s.gzip.pickle" % (date_end, code)
# ?????????????????????
if os.path.isfile(cache_file):
print("######### read from cache #########", cache_file)
return pd.read_pickle(cache_file, compression="gzip")
else:
stock = ts.get_hist_data(code, start=date_start, end=date_end)
stock = stock.sort_index(0) # ???????????
stock.to_pickle(cache_file, compression="gzip")
return stock
python类get_hist_data()的实例源码
def getDateFromMarket(id):
price = {}
# 002060
data = ts.get_hist_data(id)
for p in data.iterrows():
# high
high = float(p[1]['high'])
# low
low = float(p[1]['low'])
price[p[0].encode("utf-8")] = (high + low) / 2
return price
def getDateFromMarket(id):
price = {}
# 002060
data = ts.get_hist_data(id)
for p in data.iterrows():
# high
high = float(p[1]['high'])
# low
low = float(p[1]['low'])
price[p[0].encode("utf-8")] = (high + low) / 2
return price
def get_basic_info(show_progress=True):
# ?????
if show_progress:
click.echo("????????")
ts_symbols = ts.get_stock_basics()
if show_progress:
click.echo("??????")
symbols = []
histories = {}
# ??????
i = 0
total = len(ts_symbols)
for index, row in ts_symbols.iterrows():
i = i +1
if i > 10:
break
srow = {}
# ????????
click.echo("??????%s(%s)??????? (%d/%d)" % (index, row['name'], i, total))
histories[index] = ts.get_hist_data(index)
srow['start_date'] = histories[index].index[-1]
srow['end_date'] = histories[index].index[0]
srow['symbol'] = index
srow['asset_name'] = row['name']
symbols.append(srow)
df_symbols = pd.DataFrame(data=symbols).sort_values('symbol')
symbol_map = pd.DataFrame.copy(df_symbols.symbol)
# fix the symbol exchange info
df = df_symbols.apply(func=convert_symbol_series, axis=1)
return df, histories, symbol_map
def get_hist_data(symbol_map, histories, start_session, end_session):
for sid, index in symbol_map.iteritems():
history = histories[index]
"""
writer needs format with
[index], open, close, high, low, volume
so we do not need to change the format from tushare
but we need resort it
"""
yield sid, history.sort_index()
def get_hist_data(self, code, start = None, end = None, ktype='D'):
if end is None:
end = dataTime.datetimeRelative(delta = 1)
if start is None:
start = dataTime.datetimeRelative(base = end, delta = -20)
df = ts.get_hist_data(code, start, end, ktype)
df = self.format_date_index(df)
df = df.sort_values(['date'], ascending=[True])
return df
# ????
# ?????
# code:string,???? e.g. 600848
# start:string,???? format?YYYY-MM-DD ????????
# end:string,???? format?YYYY-MM-DD ????????
# autype:string,?????qfq-??? hfq-??? None-???????qfq
# index:Boolean????????????False
# retry_count : int, ??3,??????????????
# pause : int, ?? 0,??????????????????????????????
# ??????
# date : ???? (index)
# open : ???
# high : ???
# close : ???
# low : ???
# volume : ???
# amount : ????
def QA_fetch_get_stock_day(name, startDate=None, endDate=None):
if (len(name) != 6):
name = str(name)[0:6]
data = QATs.get_hist_data(str(name), startDate, endDate).sort_index()
data_json = json.loads(data.to_json(orient='records'))
for j in range(0, len(data_json), 1):
data_json[j]['date_stamp'] = QA_util_date_stamp(
list(data.index)[j])
data_json[j]['date'] = list(data.index)[j]
data_json[j]['code'] = str(name)
return data_json
def get_open_close_hist_price(code, start_date=None, end_date=None):
'''
:param code: ????
:param date: ??????
:return: ?????open close ???
'''
import tushare as ts
if start_date != None and end_date != None:
if type(code) is not str or type(start_date) is not str:
code = str(code)
start_date = str(start_date)
end_date = str(end_date)
df = ts.get_hist_data(code, start_date,end_date)
df = df.head(1)
dtime = df.set_index('time')
price = dtime['price']
if price.shape == (0,):
print code, "can't get ", start_date, "am_pm data!"
return float('nan'), float('nan')
else:
return price[-1], price[len(df.time)/4]
else:
df = ts.get_hist_data(code)
df = df.head(1)
dtime = df.set_index('time')
price = dtime['price']
if price.shape == (0,):
print code, "can't get ", start_date, "am_pm data!"
return float('nan'), float('nan')
else:
return price[-1], price[len(df.time)/4]
def download_from_tushare(code):
'''
#??????,????1:?????
month :????
m2 :???????????M2?(??)
m2_yoy:???????????M2?????(%)
m1:??(????M1)(??)
m1_yoy:??(????M1)????(%)
m0:?????(M0)(??)
m0_yoy:?????(M0)????(%)
cd:????(??)
cd_yoy:????????(%)
qm:???(??)
qm_yoy:???????(%)
ftd:????(??)
ftd_yoy:????????(%)
sd:????(??)
sd_yoy:????????(%)
rests:????(??)
rests_yoy:????????(%)
'''
import tushare as ts
path = './data/'
# 1 day line
hist_data = ts.get_hist_data(str(code))
if hist_data is not None:
hist_data.to_csv(path+"stock_data/"+str(code)+'.csv')
return True
else:
return False
# 30 day lines
# ts.get_hist_data(str(code), ktype='M').to_csv(path+"stock_data/"+str(code)+'_month.csv')
def data_to_csv(self, path_input):
"""
Output the data of your asset portfolio with the format '.csv' to the place you want.
"""
try:
for i in range(len(self.stocklist)):
self.stockdata[self.stocklist[i]]
except:
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']
data = self.stockdata
data.to_csv(path_input+'data.csv')
print("The file has been generated at ", path_input, ".")
def profile(self, path_input=None):
"""
Calculate the profile made by your portfolio. It will give out the number of money you made, instead of pecentage.
"""
profile = []
profileratio = []
sum_of_profile = 0
for stock_single in self.stocklist:
print("Now processing:", stock_single)
self.stockdata[stock_single] = ts.get_hist_data(stock_single, self.start, self.end)['close']
profileratio.append((self.stockdata[stock_single].loc[self.end]-self.stockdata[stock_single].loc[self.start])/self.stockdata[stock_single].loc[self.start])
for i in range(0, len(self.stocklist)):
profile.append(self.moneyallocation[i] * float(profileratio[i]))
if math.isnan(profile[i]) != True:
sum_of_profile = profile[i] + sum_of_profile
else:
pass
for i in range(0, len(profile)):
print(self.stocklist[i], profile[i],"\n")
print("Totel profile is", sum_of_profile)
# Update: 2017-08-12
try:
if path_input != None:
_profile_stock_list = self.stocklist.append('Totel profile')
_profile_single_stock = profile.append(sum_of_profile)
_profile_data = {'List':_profile_stock_list, 'Profile':_profile_stock_list}
_profile = pd.DataFrame(_profile_data)
_profile.to_csv(path_input+'profile.csv')
elif self.defaultpath:
_profile_stock_list = self.stocklist.append('Totel profile')
_profile_single_stock = profile.append(sum_of_profile)
_profile_data = {'List':_profile_stock_list, 'Profile':_profile_stock_list}
_profile = pd.DataFrame(_profile_data)
_profile.to_csv(self.defaultpath+'profile.csv')
else:
pass
except IOError:
print('Path not exists!')
def trade_data(code):
data = ts.get_hist_data(code, end=today_str_Ymd, retry_count=10)
data[u'date'] = data.index
data[u'code'] = code
data = data.sort_index(ascending=True)
return data
def Get_TA(df_Code,Dist):
operate_array1=[]
operate_array2=[]
operate_array3=[]
count = 0
for code in df_Code.index:
# index,0 - 6 date??? open???? high???? close???? low???? volume???? price_change????? p_change????
# 7-12 ma5?5??? ma10?10??? ma20:20??? v_ma5:5???v_ma10:10??? v_ma20:20???
df = ts.get_hist_data(code,start='2014-11-20')
dflen = df.shape[0]
count = count + 1
if dflen>35:
try:
(df,operate1) = Get_MACD(df)
(df,operate2) = Get_KDJ(df)
(df,operate3) = Get_RSI(df)
except Exception, e:
Write_Blog(e,Dist)
pass
operate_array1.append(operate1) #round(df.iat[(dflen-1),16],2)
operate_array2.append(operate2)
operate_array3.append(operate3)
if count == 0:
Write_Blog(str(count),Dist)
df_Code['MACD']=pd.Series(operate_array1,index=df_Code.index)
df_Code['KDJ']=pd.Series(operate_array2,index=df_Code.index)
df_Code['RSI']=pd.Series(operate_array3,index=df_Code.index)
return df_Code
#??MACD??????
def getSzzs():
try:
data = ts.get_hist_data(code='000001', start='2016-01-01', end='2016-12-31')
# print (data)
number = '000001'
name = '????'
date = list(data.index) # ??
open = list(data.open) # ???
high = list(data.high) # ???
close = list(data.close) # ???
low = list(data.low) # ???
change = list(data.p_change) # ???
i = '000001_????_org'
cur1 = conn.cursor()
cur1.execute("DROP TABLE IF EXISTS %s;" % (i)) # ??????????
cur1.execute("""create table %s
(b_id int comment '??',
b_index varchar(100) comment '????',
b_name varchar(100) comment '????',
b_date date comment '??',
b_open float comment '???',
b_high float comment '???',
b_close float comment '???',
b_low float comment '???',
b_change float comment '???',
PRIMARY key(b_id));""" % (i))
for j in range(0, len(date)):
# print (j + 1, number, name, date[j], open[j], high[j], close[j], low[j], change[j])
cur1.execute("INSERT INTO %s(b_id, b_index, b_name, b_date, b_open, b_high, b_close, b_low, b_change) "
"VALUES('%d', '%s', '%s', '%s', '%f', '%f', '%f', '%f', '%f');"
% (i, j + 1, number, name, date[j], open[j], high[j], close[j], low[j], change[j]))
cur1.execute("commit;")
except Exception:
pass
def is_break_high(stockID, days, fast_type=True):
end_day = datetime.date(datetime.date.today().year, datetime.date.today().month, datetime.date.today().day-2)
days = days * 7 / 5
#?????????
print stockID
start_day = end_day - datetime.timedelta(days)
start_day = start_day.strftime("%Y-%m-%d")
end_day = end_day.strftime("%Y-%m-%d")
if fast_type:
df = ts.get_h_data(stockID, start=start_day, end=end_day, retry_count=10, pause=10)
else:
df = ts.get_hist_data(stockID, start=start_day, end=end_day, retry_count=10, pause=10)
if df is None:
print "None len==0"
return False
if df.empty:
print "%s Trading halt" % info.ix[stockID]['name'].decode('utf-8')
return False
period_high = df['high'].min()
#print period_high
curr_day = df[:1]
today_high = curr_day.iloc[0]['high']
#??????? .values
#????df??1? ????.values
#print today_high
if today_high >= period_high:
stock_h = []
day = curr_day.index.values[0]
#print curr_day
name = info.ix[stockID]['name'].decode('utf-8')
if fast_type:
turnover = 0
p_change = 0
else:
turnover = curr_day.iloc[0]['turnover']
p_change = curr_day.iloc[0]['p_change']
print day
print stockID
print p_change
print turnover
#print day
#date=curr_day['date']
stock_h.append(day)
stock_h.append(stockID)
stock_h.append(name)
stock_h.append(p_change)
stock_h.append(turnover)
#print name.decode('utf-8')
#print date
#all_high_stock.append(stock)
sql_db_h.store_break(stock_h)
return True
else:
return False
def is_break_low(stockID, days, fast_type=True):
end_day = datetime.date(datetime.date.today().year, datetime.date.today().month, datetime.date.today().day-2)
days = days * 7 / 5
#?????????
print stockID
start_day = end_day - datetime.timedelta(days)
start_day = start_day.strftime("%Y-%m-%d")
end_day = end_day.strftime("%Y-%m-%d")
if fast_type:
df = ts.get_h_data(stockID, start=start_day, end=end_day, retry_count=10, pause=10)
else:
df = ts.get_hist_data(stockID, start=start_day, end=end_day, retry_count=10, pause=10)
if df is None:
print "None len==0"
return False
if df.empty:
print "%s Trading halt" % info.ix[stockID]['name'].decode('utf-8')
return False
period_low = df['low'].max()
#print period_high
curr_day = df[:1]
today_low = curr_day.iloc[0]['low']
#??????? .values
#????df??1? ????.values
#print today_high
if today_low <= period_low:
stock_l= []
day = curr_day.index.values[0]
#print curr_day
name = info.ix[stockID]['name'].decode('utf-8')
if fast_type:
turnover = 0
p_change = 0
else:
turnover = curr_day.iloc[0]['turnover']
p_change = curr_day.iloc[0]['p_change']
print day
print stockID
print p_change
print turnover
#print day
#date=curr_day['date']
stock_l.append(day)
stock_l.append(stockID)
stock_l.append(name)
stock_l.append(p_change)
stock_l.append(turnover)
#print name.decode('utf-8')
#print date
#all_high_stock.append(stock)
sql_db_l.store_break(stock_l)
return True
else:
return False
def main():
#?????????????????, ?????????
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")
#code:?????start:?????end:????
#print(zsyh) #???????????
make_end_line()
print(zsyh.head())
make_end_line()
print(zsyh.columns)
make_end_line()
"""
????
date???
open????
high????
close????
low????
volume????
price_change?????
p_change????
ma5?5???
ma10?10???
ma20: 20???
v_ma5: 5???
v_ma10: 10???
v_ma20: 20???
turnover:???[???????]
"""
print(zsyh.describe())
make_end_line()
print(zsyh.info())
make_end_line()
plt.show(zsyh["close"].plot(figsize=(12,8))) #???????????
#pd.set_option("display.float_format", lambda x: "%10.3f" % x)
plt.show(zsyh["volume"].plot(figsize=(12,8)))
zsyh[["close","ma5","ma10","ma20"]].plot(subplots = True)
plt.show()
plt.show(zsyh[["close","ma5","ma10","ma20"]].plot(figsize=(12,8),linewidth=2))
plt.show(zsyh["p_change"].plot())
plt.show(zsyh["p_change"].plot(figsize=(10,4),legend=True,linestyle="--",marker="o"))
#???????????
plt.show(zsyh["p_change"].hist(bins=20))
plt.show(zsyh["p_change"].plot.kde()) #?????
#?????(kernel density estimation)?????????????????
plt.show(sns.kdeplot(zsyh["p_change"].dropna()))
plt.show(sns.distplot(zsyh["p_change"].dropna())) #??????????????????????