def get_tick_feng(_stock_id, _trade_date): df = None try: df = ts.get_tick_data(_stock_id, date=_trade_date, retry_count=5, pause=1) except Exception: log_error("warn: %s get ticks exception!", _stock_id) time.sleep(5) return None if df is None : log_error("warn: stock %s, %s is None, next", _stock_id, _trade_date) return None if df.empty: log_error("warn: stock %s, %s is empty, next", _stock_id, _trade_date) return None if len(df) <= 5: log_error("warn: stock %s, %s is short %d, next", _stock_id, _trade_date, len(df)) return None df = df.set_index('time').sort_index() return df
def xtick_down100(qx, ftg): ''' 根据指定的日期,股票代码,数据文件名:ftg 下载指定股票指定日期的ticks数据,并保存到ftg [输入] qx.code,股票代码 qx.xtimSgn,当前日期的字符串 ftg,保存tick数据的文件名 ''' df, dn = [], 0 try: df = ts.get_tick_data(qx.code, date=qx.xtimSgn) # print(df.head()) except IOError: pass # skip,error datFlag, dn = False, len(df) print(' n', dn, ftg) # 跳过无数据 日期 # if zwt.xin(dn,0,9):print('n2',dn,ftg) if dn > 10: df['type'] = df['type'].str.replace(u'中性盘', 'norm') df['type'] = df['type'].str.replace(u'买盘', 'buy') df['type'] = df['type'].str.replace(u'卖盘', 'sell') df.to_csv(ftg, index=False, encoding='utf') datFlag = True # return datFlag, dn
def get_tick_history(exsymbol, days=10): store = get_store(store_type) dates = store.get('id000001').index symbol = exsymbol_to_symbol(exsymbol) df_res = pd.DataFrame(columns=["kaipan_vol", "kaipan_money", "shoupan_vol", "shoupan_money"]) for i in range(days, 0, -1): date = dates[len(dates)-i].strftime("%Y-%m-%d") df = ts.get_tick_data(symbol, date=date, src="tt") if df is None or len(df) == 0: continue df["time"] = pd.to_datetime(df["time"], format='%H:%M:%S') kaipan_time = df.iloc[0].time kaipan_vol = 0 kaipan_money = 0 if kaipan_time.hour == 9 and kaipan_time.minute < 30: kaipan_vol = df.iloc[0].volume kaipan_money = df.iloc[0].amount shoupan_time = df.iloc[len(df)-1].time shoupan_vol = 0 shoupan_money = 0 if shoupan_time.hour >= 15 and shoupan_time.minute >= 0: shoupan_vol = df.iloc[len(df)-1].volume shoupan_money = df.iloc[len(df)-1].amount df_res.loc[date] = [kaipan_vol, kaipan_money, shoupan_vol, shoupan_money] return df_res
def __init__(self, tableName): ''' Constructor ''' bdl.BaseDataLoader.__init__(self, tableName) #self.db = msql.MySQLConnection.GetConnection() #self.ml = mlg.MyLogger.getLogger() #tradingday = datetime.strftime(datetime.today(), "%Y-%m-%d") code = '600848' tradingday = '2015-11-30' self.ml.info("Loading Data") data = ts.get_tick_data(code=code, date=tradingday) data['tradingday'] = tradingday data['code'] = code indexdata = data.set_index(['tradingday', 'code']) self.ml.info("Saving Data") indexdata.to_sql(self.tableName, self.db, flavor="mysql", if_exists="append") #关闭连接 #db.close() self.ml.info("Saving Success")
def get_in_out(symbol, date): df = ts.get_tick_data(symbol, date=date, src='tt') thre = df.amount.quantile(0.99) thre = np.max([thre, 1e6]) in_amount = df[(df.amount>thre)&(df.type=="买盘")].amount.sum()/1e8 out_amount = df[(df.amount>thre)&(df.type=="卖盘")].amount.sum()/1e8 return [in_amount, out_amount]
def db(): df = ts.get_tick_data('600848', date='2014-12-22') engine = create_engine('mysql://*****:*****@127.0.0.1/mystock?charset=utf8') # db = MySQLdb.connect(host='127.0.0.1',user='******',passwd='jimmy1',db="mystock",charset="utf8") # df.to_sql('TICK_DATA',con=db,flavor='mysql') # db.close() df.to_sql('tick_data', engine, if_exists='append')
def get_sd(id, label_start_date, fea_delta = 240): # delta 指的是 确定label;的时候 是一个星期的涨幅还是什么 # fea delta 指的是, 我要准备多久的数据 ## 获取分笔数据 # get all time ticks # 创建一个日期列表 ticks = None date_list = dt_tool.dt_range(label_start_date, -fea_delta) for date in date_list: try: tick = ts.get_tick_data(id, date) except Exception, e: print e continue if tick is None: continue tick.type = tick.type.apply(lambda x : type2id(x)) ft = tick.sort('amount', ascending=False).head(10).reset_index().drop(['index', 'time', 'change'], 1).stack().reset_index() ft.index = ft.level_0.map(str) + '_' + ft.level_1 fT = ft.drop(['level_0', 'level_1'], 1).T fT['date'] = dt_tool.format(date) if ticks is None: ticks = fT else: ticks = ticks.append(fT) ticks.to_csv('data/ticks.csv', index=None)
def get_tick(code, start, end=None, file=None): date = get_dates(start, end) if not file: file = '-'.join([code, start.replace('-', ''), str(len(date))]) + r'.csv' for d in date: df = ts.get_tick_data(code, date=d) df.to_csv(file, mode='a')
def deal_list_download(self, stock, engine, start='2014-10-16'): ''' 分时数据下载 ''' deals = ts.get_tick_data(stock, date='2015-10-16') table_name = stock + '_deal' deals.to_sql(table_name, engine)
def multi_write((start_date,end_date)): startdate=datetime.datetime.strptime(start_date,'%Y-%m-%d').date() enddate = datetime.datetime.strptime(end_date, '%Y-%m-%d').date() #string-to-dateobject conversion date_list=date_loop.date_loop(start_date= startdate,end_date= enddate) #return a list of date #engine=create_engine('mysql+mysqldb://root:15921998273@localhost:3306/tusharedb?charset=utf8') DTS=[] for dates in date_list: preDTS = ts.get_tick_data('600640', date=dates) #DTS=Data To be Saved try: preDTS['type'][0].split() #throw an AttributeError when no data is available preDTS.sort_index(axis=0, ascending=True) #reverse the dataframe with latest data placed at the bottom preDTS['date'] = dates #insert date column #sql table will be named like 'hist_Part0,1,2,3,4...' DTS.append(preDTS) except AttributeError: print 'No available data for ', dates try: DTS = pd.concat(DTS) except ValueError : DTS=None return DTS
def get_tick_data(self, code, t_date = None): if t_date is None: t_date = dataTime.datetimeRelative(delta = 0) t_date = t_date.replace(' 00:00:00', '') df = ts.get_tick_data(code, date = t_date) df = self.format_date_to_datetime(df, t_date = t_date) return df
def pankou(name): # rang =pd.date_range('20150101', periods=5) # a = ts.get_tick_data(code, rang) a = ts.get_tick_data(name, date='2017-03-09') buy_sum = 0 sell_sum = 0 other_sum = 0 for item in range(0, len(a)): row = a[item:item + 1] type = row['type'] if type[item] == '卖盘': sell_sum += row['amount'][item] elif type[item] == '买盘': buy_sum += row['amount'][item] else: other_sum += row['amount'][item] pan_sum = sum([sell_sum, buy_sum, other_sum]) mp = buy_sum / pan_sum sp = sell_sum / pan_sum zx = other_sum / pan_sum # print('股票',code,(sell_sum, buy_sum, other_sum)/pan_sum) print('股票', name, mp, sp, zx) if isnan(mp): return with UseDatabase(config) as cursor: _SQL = """insert into pankou (name, mp, sp, zx) values (%s, %s, %s, %s)""" cursor.execute(_SQL, (name, mp, sp, zx))
def request_test_tick(code, engine, start_date, end_date): create_test_talbe(code) cur_day = start_date logging.info('requesting tick, code: ' + code + str(threading.currentThread())) while cur_day != end_date: try: #logging.info('cur_day: ' + str(cur_day) + str(threading.currentThread())) tick = ts.get_tick_data(code, date=cur_day.date(), retry_count=500, src='tt') if not tick.empty: if tick.time[0] != 'alert("当天没有数据");': tick['type'] = tick['type'].apply(lambda x: trade_type_dic[x]) tick['change'] = tick['change'].apply(change_dic) tick['time'] = str(cur_day.date()) + ' '+ tick['time'] tick.to_sql('test_' + code, engine, if_exists='append', dtype={'time': DateTime}) logging.info('save to test_' + code + ' on '+ str(cur_day) + ' thread ' + str(threading.currentThread())) except Exception: logging.error(str(code) + ' request tick failed on ' + str(cur_day) + str(threading.currentThread())) delta = datetime.timedelta(days=1) cur_day = cur_day + delta
def tick(tstamp): date = tstamp.split(" ")[0] tick_data = ts.get_tick_data(code,date=date,retry_count=10,pause=5) nrows = tick_data.shape[0] tick_data['date'] = pd.Series([date]*nrows) return tick_data
def check_if_reach_limit(code, date=datetime.now().strftime("%Y-%m-%d")): """ """ kdata = ts.get_hist_data(code,date,date,ktype='D') if kdata.ix[0]['p_change'] < 9.9: return False stock = ts.get_tick_data(code,date)[['time','price']] indx, = np.where(np.diff(list(reversed(stock['price'])))==0) # print "length: ", len(indx) arr = [] beg = 0 cur = indx[0]-1 for i,val in enumerate(indx): if cur+1 == val: cur += 1 else: if i > (beg+1): arr.append((indx[beg], indx[i-1])) beg = i cur = val arr.append((indx[beg],cur)) mindx = np.argmax([x[1]-x[0] for x in arr]) print "length of arr: ", len(arr) p = arr[mindx][0] print 'starting index: ', p print 'starting time: ', stock.ix[stock.shape[0]-p-1]['time'] return stock.ix[stock.shape[0]-p-1]['time']
def multi_write((code,start_date,end_date)): startdate=datetime.datetime.strptime(start_date,'%Y-%m-%d').date() enddate = datetime.datetime.strptime(end_date, '%Y-%m-%d').date() #string-to-dateobject conversion date_list=date_loop.date_loop(start_date= startdate,end_date= enddate) #return a list of dates DTS=[] for dates in date_list: preDTS = ts.get_tick_data(code, date=dates) #DTS=Data To be Saved try: preDTS['type'][0].split() #throw an AttributeError when no data is available preDTS['date'] = dates #insert date column preDTS=preDTS.sort_values(by='time',ascending= True) #sql table will be named like 'hist_Part0,1,2,3,4...' DTS.append(preDTS) except AttributeError: print 'No available data for ', dates try: DTS = pd.concat(DTS) except ValueError : DTS=None return DTS
def get_multiday_ave_compare_silent(code, dayl='10'): dtick = ts.get_today_ticks(code) d_hist = ema.getdata_ema_trend_silent(code, dayl, 'd') # print d_hist day_t = ema.get_today() if day_t in d_hist.index: dl = d_hist.drop(day_t).index else: dl = d_hist.index # print dl # print dl ep_list = [] for da in dl.values: # print code,da td = ts.get_tick_data(code, da) # print td if not type(td) == types.NoneType: ep = td['amount'].sum() / td['volume'].sum() ep_list.append(ep) # print ("D: %s P: %s" % (da[-5:], ep)) ave = ema.less_average(ep_list) if len(dtick.index) > 0: ep = dtick['amount'].sum() / dtick['volume'].sum() p_now = dtick['price'].values[0] * 100 if p_now > ave or ep > ave: print ("GOLD:%s ep:%s UP:%s!!! A:%s %s !!!" % (code, ep, p_now, ave, get_now_time())) # elif p_now > ave and ep < ave: # print ("gold:%s ep:%s UP:%s! A:%s %s !" % (code, ep, p_now, ave, get_now_time())) # elif p_now < ave and ep > ave: # print ("down:%s ep:%s Dow:%s? A:%s %s ?" % (code, ep, p_now, ave, get_now_time())) return True else: if p_now < ave and ep < ave: print ("DOWN:%s ep:%s now:%s??? A:%s %s ???" % (code, ep, p_now, ave, get_now_time())) return False
def nosql(): import pymongo import json conn = pymongo.Connection('127.0.0.1', port=27017) df = ts.get_tick_data('600848', date='2014-12-22') print(df.to_json(orient='records')) conn.db.tickdata.insert(json.loads(df.to_json(orient='records')))
def virance_a_day(day): try: datas = tushare.get_tick_data(code, date=day, retry_count = 5) prices = [value[1] for value in datas.values] ave_price = sum(prices) / len(prices) virance = sum([(price - ave_price) ** 2 for price in prices]) return virance except: return None
def get_zhangting_sell(symbol, date): df = ts.get_tick_data(symbol, date=date, src='tt') high = df.price.max() df_zt = df[(df.price==high) & (df.type=="卖盘")] thre = df_zt.amount.quantile(0.99) thre = np.max([thre, 1e6]) inst_amount = df_zt[df_zt.amount >= thre].amount.sum() / 1e8 total_amount = df_zt.amount.sum() / 1e8 return [inst_amount, total_amount]
def tick(code,date): dftick = ts.get_tick_data(code,date=date) dftick.insert(0,'uploadtime',nowtime) dftick.insert(0,'code',code) dftick.to_sql('mkt_tick',engine,if_exists='append') counttick = cursor.execute('show columns from mkt_tick like \'tick_id\'') if counttick == 0: insertidtick()
def get_acf(code): day = get_last_day() print str(day) today_prices = tushare.get_tick_data(code=code, date=str(day-datetime.timedelta(days=3)), retry_count=50) today_prices = [(value[0], value[1]) for value in today_prices.values] train_prices = [price for time, price in today_prices if time > "13:00:00"] return ar_model.AR.fit(train_prices)
def _fill_history_bars(self, replay_days): now = datetime.datetime.now() for day in get_trading_days(now, replay_days): bars_dict = {} for identifier in self.__identifiers: df = ts.get_tick_data(identifier, date=day.date().isoformat()) bars_dict[identifier] = get_bar_list(df, self.__frequency, day) self.__fill_bars(bars_dict)
def get_acf(code): print code today = datetime.date.today() print str(today) today_prices = tushare.get_tick_data(code=code, date=str(today), retry_count=50) print today_prices today_prices = [(value[0], value[1]) for value in today_prices.values] train_prices = [price for time, price in today_prices if time > "13:00:00"] print train_prices
def get_stock_intraday_data(code, date): """ 获取一只股票的历史某天的日内交易数据,让后保存到csv文件中 """ data_frame = ts.get_tick_data(code, date=date) current_dir = os.path.dirname(__file__) ab_path = os.path.join( current_dir, "..\\", "..\\" ) data_root_path = os.path.abspath( ab_path ) + "\\intradate_data\\" code_path = data_root_path + code if not os.path.exists(code_path): os.makedirs(code_path) save_path = code_path + "\\" + date + ".csv" data_frame.to_csv(save_path, encoding='utf-8', index=False)
def Get_stock_ticks(code, time_to_market): import tushare as ts import pandas as pd import logging import datetime as dt import os import socket import pydoop.hdfs as hdfs import shutil if time_to_market !=0: logger = logging.getLogger("D_stock") logger_handler=logging.FileHandler("/tmp/D_stock.log") logger_handler.setFormatter(logging.Formatter("%(asctime)s -- %(message)s")) logger_handler.setLevel(logging.DEBUG) logger.setLevel(logging.DEBUG) logger.addHandler(logger_handler) logger.info(">"*15+code+">"*15) all_days=pd.date_range(start=str(time_to_market),end=dt.date.today(),freq="B") all_days=[x.date() for x in all_days] for day in all_days[::-1]: logger.info("Saving "+code+"@"+str(day)+"...") while True: try: df=ts.get_tick_data(code,date=day) except Exception as e: print e continue break if df.index.size >3: dir_name="/tmp/ticks/"+str(code) if not os.path.exists(dir_name): os.makedirs(dir_name) file_name=dir_name+"/"+str(day)+".csv" df.to_csv(file_name) """ Write to HDFS """ if os.path.exists(dir_name): s=hdfs.hdfs(host="spark-1",port=9000) if not s.exists("ticks"): s.create_directory("ticks") hdfs.put(dir_name,"./ticks/") shutil.rmtree(dir_name) logger.info("<"*15+code+"<"*15) return (socket.gethostname(),code)
def get_tick(stockCode=None, date=None): """ 根据股票列表的股票代码获取当日/指定日期历史分笔 Return -------- DataFrame """ tick_data = '' if date != None and date != '': tick_data = ts.get_tick_data(stockCode, date) else: tick_data = ts.get_today_ticks(stockCode) if not tick_data.dropna(axis=0, how='any', thresh=None).empty: tick_data.insert(0, 'code', stockCode) #插入股票代码字段 return tick_data
def get_data(stock_id,date): if os.path.exists(stock_id+'_'+date+'.xlsx'): df = pd.read_excel(stock_id+'_'+date+'.xlsx') else: df = ts.get_tick_data(stock_id,date=date) df['type'] = df['type'].apply(lambda x:x.decode('utf8','ignore')) df.to_excel(stock_id+'_'+date+'.xlsx') tmp = df[['time','price']] tmp = df.sort_index(by=['time'],ascending=True) X = tmp['price'].values #plt.figure() plt.plot(X) plt.plot(quad_smooth(X,50))
def get_stock_data(): while True: code = random.choice(codes) prices = [] days = ["2015-12-05", "2015-12-06", "2015-12-07", "2015-12-08", "2015-12-09"] prices = [] for day in days: df = tushare.get_tick_data(code, day) for p in df['price']: if not math.isnan(p): prices.append(p) if len(prices) >= 2000: max_p = max(prices) min_p = min(prices) return map(lambda x: -1.0+(x-min_p)/(max_p-min_p), prices)
def download_stock_quotes(code, date_start='', date_end=str(datetime.date.today())): code = util.getSixDigitalStockCode(code) try: if date_start == '': date = datetime.datetime.today().date() + datetime.timedelta(-365*3) date_start = str(date) dateStart = datetime.datetime.strptime(str(date_start), "%Y-%m-%d") for i in range(du.diff_day(date_start, date_end)): date = dateStart + datetime.timedelta(i) strDate = date.strftime("%Y-%m-%d") df = ts.get_tick_data(code, strDate) print df except Exception as e: print str(e)
# -*- coding: utf-8 -*- """ Created on Tue Feb 9 18:02:42 2016 @author: Administrator """ #============================================================================== # 主力资金流数据 #============================================================================== import tushare as ts stock_data = ts.get_tick_data('600428', date='2016-02-05') # 计算平均每笔成交量 print('平均每笔成交量:%d手' % stock_data['volume'].mean()) # 计算资金流入流出 data = stock_data.groupby('type')['amount'].sum() if '买盘' in data.index: print('资金流入:%d元' % data['买盘']) if '卖盘' in data.index: print('资金流出:%d元' % data['卖盘']) # 计算主力资金流入流出 data = stock_data[stock_data['volume'] > 500].groupby('type')['amount'].sum() if '买盘' in data.index: print('主力资金流入:%d元' % data['买盘']) if '卖盘' in data.index: print('主力资金流出:%d元' % data['卖盘'])
mp = 446.74 * 100000000 b_r = 0.0002 m_r = b_r / 2 code, time_to_market = data time_to_market = '20170104' lastdate = date_add(str(time_to_market), 0, '%Y%m%d') while lastdate <= today_date: print lastdate try: open_price, close_price = session.execute( 'select open,close from day_k_data where code in ("002230") and date="' + lastdate + ' 00:00:00.000000";').fetchall()[0] except: lastdate = date_add(lastdate, 1, '%Y-%m-%d') continue df = ts.get_tick_data(code, date=lastdate, pause=2) df.insert(0, 'date', lastdate) df.insert(0, 'code', code) df = df.set_index('date') fq = round(df.iloc[0]['price'] / close_price, 3) df['change'][df['change'] == '--'] = 0.0 df['change'] = df['change'].apply(float) df['type'][df['change'] == 0.0] = 'equal' df['type'][df['change'] > 0] = 'buy' df['type'][df['change'] < 0] = 'sell' df['price'] = (df['price'] / fq).apply(round, args=(3, )) df['change'] = df['change'] / fq df['change'] = df['change'].apply(round, args=(3, )) df_count = {} print b_r * mp df_count['big_buy'] = df['amount'][(df['type'] == 'buy')
def to_mongodb(): import pymongo import json conn=pymongo.Connection ('127.0.0.1',port =27017) df=ts.get_tick_data('600848',date='2014-12-22') conn.db.tickdata.insert(json.loads(df.to_json(orient ='records')))
股票数据:名称、上市交易所 上市时间, 历史日数据 历史某天的分笔数据 ''' #DataAPI.TickRTIntraDayMoneyFlowOrderGet(securityID=u"000001.XSHE",startTime=u"09:30",endTime=u"11:00",field=u"",pandas="1") eq_mt = ts.Master() SecID = eq_mt.SecID(cnSpell='gxgk') eq_tl = ts.Equity() candi_stock_ipo = eq_tl.EquIPO(secID='002074.XSHE') candi_stock_d = ts.get_h_data('002074', start='2006-10-18', end='2016-04-12') ## 获取历史分笔数据 candi_stock_fb = ts.get_tick_data('002074', date='2016-04-12') candi_stock_fb.head(10) dates = pd.to_datetime(shz.index) ## 第一种作图方法 def simple_plot(dates, data): fig = plt.figure(figsize=(10, 4)) plt_sh = fig.add_subplot(1, 1, 1) daysFmt = mdates.DateFormatter('%m-%d-%Y') plt_sh.plot(dates, data, color='b', linewidth=1.5) plt_sh.xaxis.set_major_formatter(daysFmt) plt_sh.autoscale_view() plt_sh.fmt_xdata = mdates.DateFormatter('%Y-%m-%d') fig.autofmt_xdate()
df.to_csv('603299_60_Mins.csv') open('603299_30_Mins.csv','w') df=ts.get_hist_data('603299',ktype='30') #获取上证指数k线数据 df.to_csv('603299_30_Mins.csv') open('603299_15_Mins.csv','w') df=ts.get_hist_data('603299',ktype='15') #获取上证指数k线数据 df.to_csv('603299_15_Mins.csv') open('603299_5_Mins.csv','w') df=ts.get_hist_data('603299',ktype='5') #获取上证指数k线数据 df.to_csv('603299_5_Mins.csv') # 历史分笔数据 获取2016-01-11以及2016-01-12的分笔数据 open('603299_fb060111.csv','w') df = ts.get_tick_data('603299',date='2016-01-11') df.to_csv('603299_fb060111.csv') open('603299_fb060112.csv','w') df = ts.get_tick_data('603299',date='2016-01-12') df.to_csv('603299_fb060112.csv') # # 大单交易数据 获取2016-01-11以及2016-01-12的大于600手的数据,目前这个数据可能出现了bug # open('603299_ddsj060111.csv','w') # df = ts.get_sina_dd('603299', date='2016-01-11', vol=500) #指定大于等于500手的数据 # df.to_csv('603299_ddsj060111.csv') # # open('603299_ddsj060112.csv','w') # df = ts.get_sina_dd('603299', date='2016-01-12', vol=500) #指定大于等于500手的数据 # df.to_csv('603299_ddsj060112.csv')
def ts_handle_data(addcsv, prepath, bhist, url, code, qdate, sarr): todayUrl = "http://hq.sinajs.cn/list=" + code #if Handle_Mid==0: # print "Message: Ignore 中性盘" if not os.path.isdir(prepath): os.makedirs(prepath) dataObj = [] if cmp(sarr, '')==0: sarr = dftsarr volObj = sarr.split(',') arrlen = len(volObj) for i in range(0,arrlen): obj = fitItem(int(volObj[i])) dataObj.append(obj) wb = Workbook() # grab the active worksheet ws = wb.active totalline = 0 filename = code+ '_' + qdate fctime = '' todayData = [] todayDataLen = 0 bGetToday = 0 last_close = 0 cur=datetime.datetime.now() if bhist==0: fctime = '%02d:%02d' %(cur.hour, cur.minute) filename = '%s_%02d-%02d' %(filename, cur.hour, cur.minute) bGetToday = 1 if (bhist==2 and cur.hour>=15 and cur.minute>0): bGetToday = 1 if (bGetToday==1): try: req = urllib2.Request(todayUrl) stockData = urllib2.urlopen(req, timeout=5).read() except: loginfo(1) print "URL timeout" else: stockObj = stockData.split(',') if len(stockObj)<10: print code, ": No trade data" return -1 closePrice = float(stockObj[3]) lastClsPrice = float(stockObj[2]) last_close = lastClsPrice openPrice = float(stockObj[1]) highPrice = float(stockObj[4]) lowPrice = float(stockObj[5]) exVolume = int(stockObj[8])/100 exAmount = float(stockObj[9]) f1 = '%02.02f'%( ((closePrice-lastClsPrice)/lastClsPrice)*100 ) exFluc = float(f1) todayData.append(closePrice) todayData.append(exFluc) todayData.append(lastClsPrice) todayData.append(openPrice) todayData.append(highPrice) todayData.append(lowPrice) todayData.append(exVolume) todayData.append(exAmount) todayDataLen = len(todayData) if addcsv==1: filecsv = prepath + filename + '.csv' fcsv = open(filecsv, 'w') strline = '成交时间,成交价,涨跌幅,价格变动,成交量,成交额,性质' fcsv.write(strline) fcsv.write("\n") strline = u'成交时间,成交价,涨跌幅,价格变动,成交量,成交额,性质,收盘价,涨跌幅,前收价,开盘价,最高价,最低价,成交量,成交额' strObj = strline.split(u',') ws.append(strObj) stockInfo = [] #每一页的数据,如果找到匹配数据则设置为1;解决有时候页面有数据但是收不到, #count为0,重新加载尝试再次获取;如果解析到数据的页面,如果count为0就不再继续解析数据 matchDataFlag = 0 excecount = 0 savedTrasData = [] savedTrasData2 = [] largeTrasData = [] i = 1 curcode = code if len(code)==8: curcode = code[2:8] #历史当天行情数据通过此方法获得 if bhist==1: df = ts.get_hist_data(curcode, start=qdate, end=qdate) if df is None or df.empty or len(df)!=1: print qdate, ": No data" return -1 #print qdate, df for index,row in df.iterrows(): last_close = float(row['close'])-float(row['price_change']) stockInfo.append(row['close']) stockInfo.append(row['p_change']) stockInfo.append(last_close) stockInfo.append(row['open']) stockInfo.append(row['high']) stockInfo.append(row['low']) stockInfo.append(row['volume']) stockInfo.append(row['turnover']) while excecount<=3: if bhist==0: df = ts.get_today_ticks(curcode) else: df = ts.get_tick_data(curcode, qdate) #print df if df is None: excecount += 1 continue; if df.size==18: excecount += 1 continue; else: break; #尝试3次,检查结果 if df is None: print qdate, ": None Object" return -1 if df.size==18: print qdate, ": Fail to get data" return -1 for index,row in df.iterrows(): curtime = row['time'] curprice = row['price'] range_per = '' if last_close!=0: range_val = ((float(curprice)-last_close) * 100) / last_close range_per = round(range_val, 2) fluctuate = row['change'] curvol = int(row['volume']) volume = curvol amount = row['amount'] if bhist==0: state = row['type'] else: state = row['type'].decode('utf-8') #print state.decode('utf8') ret,hour,minute,second = parseTime(curtime) if (ret==-1): continue if (int(amount)==0 and not (hour==15 and minute==0)): continue bAddVolumn = 1 if (hour==9 and minute==25) or (hour==15 and minute==0): bAddVolumn = 0 stateStr = state st_buy = '买盘'.decode('gbk') st_sell = '卖盘'.decode('gbk') st_mid = '中性盘'.decode('gbk') if cmp(state, st_sell)==0: if bAddVolumn==1: handle_volumn(volume, dataObj, 2) stateStr = 'SELL卖盘'.decode('gbk') elif cmp(state, st_buy)==0: if bAddVolumn==1: handle_volumn(volume, dataObj, 1) #目前中性盘没有处理 elif cmp(state, st_mid)==0: if bAddVolumn==1: ret = handle_middle_volumn(volume, dataObj, curtime, fluctuate, 0) else: ret = 0 if ret==1: stateStr = st_buy elif ret==2: stateStr = st_sell if addcsv==1: strline = curtime +","+ curprice +","+ range_per +","+ fluctuate +","+ curvol +","+ amount +","+ stateStr + "\n" fcsv.write(strline) totalline += 1 row = totalline+1 price = float(curprice) cell = 'A' + str(row) ws[cell] = curtime cell = 'B' + str(row) ws[cell] = price cell = 'C' + str(row) ws[cell] = range_per cell = 'D' + str(row) ftfluct = fluctuate if (fluctuate=='--'): ws[cell] = fluctuate else: ftfluct = float(fluctuate) ws[cell] = ftfluct cell = 'E' + str(row) ws[cell] = curvol cell = 'F' + str(row) ws[cell] = int(amount) cell = 'G' + str(row) s1 = stateStr ws[cell] = s1 #将当天的数据在Sheet页面更新 if (row==2 and (bhist==0 or bhist==1 or (bhist==2 and cur.hour>=15)) and todayDataLen>0): ascid = 72 for k in range(0, todayDataLen): cell = chr(ascid+k) + str(row) ws[cell] = todayData[k] #将开始和最后成交数据保存 bSaveFlag = 0 if (totalline==1 or (totalline<4 and curvol>100)): bSaveFlag = 1 elif (hour==9 and minute==30 and curvol>300) or (hour==9 and minute<30): bSaveFlag = 2 if bSaveFlag==1 or bSaveFlag==2: rowData = [] rowData.append(curtime) rowData.append(price) rowData.append(range_per) rowData.append(ftfluct) rowData.append(curvol) rowData.append(int(amount)) rowData.append(s1) if bSaveFlag==1: savedTrasData.append(rowData) elif bSaveFlag==2: savedTrasData2.append(rowData) #增加大单成交记录 if (curvol>=Large_Volume): rowData = [] rowData.append(curtime) rowData.append(price) rowData.append(range_per) rowData.append(ftfluct) rowData.append(curvol) rowData.append(int(amount)) rowData.append(s1) largeTrasData.append(rowData) if (row==2 and bhist==1): ascid = 72 number = len(stockInfo) for k in range(0,number): cell = chr(ascid+k) + str(row) ws[cell] = stockInfo[k] ws.auto_filter.ref = "A1:G1" if addcsv==1: fcsv.close() if (totalline==0): os.remove(filecsv) if totalline>0: startIdx = 0 savedTrasLen = len(savedTrasData2) if savedTrasLen>0: if savedTrasLen>Tras_Count: startIdx = savedTrasLen-Tras_Count for j in range(startIdx, savedTrasLen): savedTrasData.append(savedTrasData2[j]) ws = wb.create_sheet() write_statics(ws, fctime, dataObj, qdate, savedTrasData, largeTrasData) filexlsx = prepath +filename+ '.xlsx' if (os.path.exists(filexlsx) and bhist==0): j = 1 while True: filexlsx = prepath + filename + '_' + str(j) + '.xlsx' j += 1 if not os.path.exists(filexlsx): break; wb.save(filexlsx) return 0
def add_money_flow(): """ 沪深300生成小单统计数据 tips:成交明细列表中的买盘/卖盘:“买盘”表示以比市价高的价格进行委托买入,并已经“主动成交”,代表外盘; “卖盘”表示以比市价低的价格进行委托卖出,并已经“主动成交”,代表内盘 :return: """ print('插入历史现金流信息开始......') try: hs300_index = tushare.get_hs300s() ts_codes = hs300_index['code'].apply(util.stock_code_change) for ts_code in ts_codes: daily_info = tushare.pro_bar(ts_code, api=tushare_data.get_tushare_pro()) if daily_info is None: continue for index, row in daily_info.iterrows(): date = row['trade_date'] date = date[:4] + '-' + date[4:6] + '-' + date[6:] code = row['ts_code'][:6] if date > '2018-06-30': df = tushare.get_tick_data(code, date=date, src='tt') if df is None: continue total_amt = df['amount'].sum() total_vol = df['volume'].sum() sell_trade = df.loc[(df['type'] == '卖盘')] total_sell_vol = sell_trade['volume'].sum() total_sell_amt = sell_trade['amount'].sum() small_trade_amount = util.trade_scale(row['close']) sell_sm_trade = df.loc[(df['type'] == '卖盘') & (df['amount'] < small_trade_amount)] sell_sm_vol = sell_sm_trade['volume'].sum() sell_sm_amt = sell_sm_trade['amount'].sum() buy_trade = df.loc[(df['type'] == '买盘')] total_buy_vol = buy_trade['volume'].sum() total_buy_amt = buy_trade['amount'].sum() buy_sm_trade = df.loc[(df['type'] == '买盘') & (df['amount'] < small_trade_amount)] buy_sm_vol = buy_sm_trade['volume'].sum() buy_sm_amt = buy_sm_trade['amount'].sum() total_sm_trade = df.loc[df['amount'] < small_trade_amount] total_sm_amt = total_sm_trade['amount'].sum() total_sm_vol = total_sm_trade['volume'].sum() id = code + row['trade_date'] tables.add_money_flow(id=id, code=code, date=row['trade_date'], sell_sm_vol=sell_sm_vol, sell_sm_amt=sell_sm_amt, buy_sm_vol=buy_sm_vol, buy_sm_amt=buy_sm_amt, total_sell_vol=total_sell_vol, total_sell_amt=total_sell_amt, total_buy_vol=total_buy_vol, total_buy_amt=total_buy_amt, total_amt=total_amt, total_vol=total_vol, total_sm_amt=total_sm_amt, total_sm_vol=total_sm_vol) print('插入历史现金流信息完成......') except Exception as err: print('插入历史现金流信息失败......') raise err
def generateline(stocknumber,Type,startdate,enddate,interval): startdata = startdate.encode("ascii").replace("/","-").replace("\n","") #convert to tushare readable date enddata = enddate.encode("ascii").replace("/","-").replace("\n","") #print startdata #print enddata current_time = time.strftime("%Y/%m/%d") if Type == "分笔".decode("utf-8"): if startdate!=current_time: array = ts.get_tick_data(stocknumber, date = startdata)#分笔 if array is None: return array = array.sort_values("time") date = array["time"].tolist() amount = array["amount"].tolist() atype = array["type"].tolist() price = array["price"].tolist() flag = ["bar" for i in date] for idx,val in enumerate(atype):#if卖盘,交易变成负数 if val == "卖盘": amount[idx] = -amount[idx] if val == "中性盘":#if中性盘,则忽略. Might have a problem with this part?? amount[idx] = 0 returnarray = zip(date,amount,flag,price) return returnarray else: array = ts.get_today_ticks(stocknumber)#Tushare里今日分笔和历史分笔需要分别对待 if array is None: return array = array.sort_values("time") date = array["time"].tolist() amount = array["amount"].tolist() atype = array["type"].tolist() flag = ["bar" for i in date] for idx, val in enumerate(atype): if val == "卖盘".decode("utf-8"): amount[idx] = -amount[idx] if val == "中性盘".decode("utf-8"): amount[idx] = 0 returnarray = zip(date, amount, flag) return returnarray if Type=="季度饼图".decode("utf-8"): datestr = startdate.split("/") thisyear = datestr[0] df2 = ts.top10_holders(code=stocknumber, gdtype="1") test = df2[1]["quarter"].tolist() df_ready = df2[1] idxlist = [] for idx, val in enumerate(test): a = val.split("-") if a[0] == thisyear: # print a[0],idx idxlist.append(idx) thing = df_ready.loc[idxlist] thing = thing.sort_values(["quarter", "name"]) # print a[0],id name = thing["name"].tolist() value = thing["hold"].tolist() quarter = thing["quarter"].tolist() namearray = [name[i:i + 10] for i in xrange(0, len(name), 10)] valuearray = [value[j:j + 10] for j in xrange(0, len(value), 10)] quarterarray = [quarter[k:k + 10] for k in xrange(0, len(quarter), 10)] flag = ["pie" for i in namearray] num = [len(value) for k in namearray] returnarray = zip(namearray,valuearray,quarterarray,flag,num) return returnarray if interval!="qfq" and interval!="hfq": if interval=="1min" or interval=="5min" or interval=="15min" or interval=="30min" or interval=="60min": df = ts.get_tick_data(stocknumber, date=startdata) df.sort_values("time") a = startdata + " " + df["time"] df["time"] = a df["time"] = pd.to_datetime(a) df = df.set_index("time") price_df = df["price"].resample(interval).ohlc() price_df = price_df.dropna() vols = df["volume"].resample(interval).sum() #relevant data processing algorithm taken from Jimmy, Creator of Tushare vols = vols.dropna() vol_df = pd.DataFrame(vols, columns=["volume"]) amounts = df["amount"].resample(interval).sum() amounts = amounts.dropna() amount_df = pd.DataFrame(amounts, columns=["amount"]) newdf = price_df.merge(vol_df, left_index=True, right_index=True).merge(amount_df, left_index=True, right_index=True) if Type != "Kline": Type1 = firstletter(Type).encode("ascii") target = newdf[Type1].tolist() date = newdf.index.format() returnarray = zip(date, target) return returnarray else: Date = newdf.index.format() Open = newdf["open"].tolist() Close = newdf["close"].tolist() High = newdf["high"].tolist() Low = newdf["low"].tolist() Candlestick = zip(*[Date, Open, Close, Low, High]) return Candlestick #正常历史k线 if Type == "差价图".decode("utf-8"): global array300 if array300 is None: array300 = ts.get_k_data("399300", start=startdata, end=enddata, ktype=interval) array = ts.get_k_data(stocknumber, start=startdata, end=enddata, ktype=interval, autype='qfq') if array is None or array300 is None: return None target = [] benchmark_list = [] try: close_list = array["close"].tolist() date = array["date"].tolist() date300 = array300["date"].tolist() close300 = array300["close"].tolist() if len(close_list) is 0: return None data_price_300_map = dict() benchmark300 = 100 / (close300[0]) benchmark = 100 / (close_list[0]) for index, day in zip(range(0, len(date300)), date300): data_price_300_map[day] = close300[index] for index, close in zip(range(1, len(close_list)), close_list): price300 = data_price_300_map[date[index]] * benchmark300 target.append(round(close * benchmark - price300, 3)) benchmark_list.append(0) returnarray = zip(date, target, benchmark_list) if checkValid(returnarray) is False: return None return returnarray except Exception as e: print e return None elif Type == "价格指数".decode("utf-8"): global array300 if array300 is None: array300 = ts.get_k_data("399300", start=startdata, end=enddata, ktype=interval) array = ts.get_k_data(stocknumber, start=startdata, end=enddata, ktype=interval, autype='qfq') if array is None or array300 is None: return None price_list = [] price300_list = [] try : close_list = array["close"].tolist() date = array["date"].tolist() close300_list = array300["close"].tolist() if len(close_list) is 0: return None benchmark300 = 100 / (close300_list[0]) benchmark = 100 / (close_list[0]) for close in close_list: price_list.append(close * benchmark) for close300 in close300_list: price300_list.append(close300 * benchmark300) returnarray = zip(date, price_list, price300_list) if checkValid(returnarray) is False: return None return returnarray except Exception as e: print e return None elif Type!="Kline": array = ts.get_k_data(stocknumber, start=startdata, end=enddata, ktype=interval) if array is None: return Type1 = firstletter(Type).encode("ascii") target = array[Type1].tolist() date = array["date"].tolist() returnarray = zip(date,target) return returnarray else: array = ts.get_k_data(stocknumber, start=startdata, end=enddata, ktype=interval) if array is None: return Date = array["date"].tolist() Open = array["open"].tolist() Close = array["close"].tolist() High = array["high"].tolist() Low = array["low"].tolist() Candlestick = zip(*[Date,Open,Close,Low,High]) return Candlestick else: if Type!="Kline": # 复权 array = ts.get_h_data(stocknumber, start = startdata, end = enddata, autype= interval) if array is None: return Type1 = firstletter(Type).encode("ascii") array = array.sort_index() target = array[Type1].tolist() date = array.index.format() returnarray = zip(date, target) return returnarray else : array = ts.get_h_data(stocknumber, start=startdata, end=enddata, autype=interval) if array is None: return array = array.sort_index() Date = array.index.format() Open = array["open"].tolist() Close = array["close"].tolist() High = array["high"].tolist() Low = array["low"].tolist() Candlestick = zip(*[Date, Open, Close, Low, High]) return Candlestick
def QA_fetch_get_stock_tick(name, date): if (len(name) != 6): name = str(name)[0:6] return QATs.get_tick_data(name, date)
spam_writer = csv.writer(file_csv, dialect='excel') spam_writer.writerow([ 'code', 'name', 'average', "close", 'open', 'high', 'low', "growth", 'average_growth' ]) today_all = ts.get_today_all() for index, row in today_all.iterrows(): _code = row['code'] _amount = row['amount'] if _amount < 1: spam_writer.writerow( [_code, row['name'].encode('gbk'), 0, 0, 0, 0, 0, 0, 0]) continue print '\nget' + str(_code) + '\'s tick data' _tick_data = ts.get_tick_data(_code, date=format_time, pause=3) _total_volume = 0 _total_amount = 0 for _tick_index, _tick_row in _tick_data.iterrows(): _total_volume += _tick_row['volume'] _total_amount += _tick_row['price'] * _tick_row['volume'] if _total_volume == 0: print '\ntotal volume is zero' continue _average_price = _total_amount / _total_volume _close = row['trade'] _open = row['open'] _high = row['high'] _low = row['low'] if _close == 0: _average_growth = 0
def get_ticks(self, code, date): df = ts.get_tick_data(code, date=date, src='tt') # today = datetime.date.today().strftime('%Y-%m-%d') df['time'] = df['time'].map(lambda x: date + ' ' + x) return df
import tushare as ts df = ts.get_tick_data('000965', date='2017-08-03') p = df.amount p[1] s = 0 x = len(p) for i in range(0, x): s = p[i] + s print(s) b = p.index print(b)
def tick_insert1(code, date): stock_tick_date = tushare.get_tick_data(code=code, date=date, src='tt') stock_tick_date['code'] = code stock_tick_date['date'] = date return pandas.DataFrame(stock_tick_date)
def fetchAllStocksHistoryTickData(): """ 获取所有的股票的成交明细历史数据, 同时把数据存到数据库。 这个函数应该只运行一次 """ str_price_json={ 'metric':'security.price', 'time':'time', 'value':'price', 'code':'code', 'tags':{ 'period': 'tick' } } str_volume_json={ 'metric':'security.volume', 'time':'time', 'value':'volume', 'code':'code', 'tags':{ 'period':'tick', 'type': True } } today=dt.now() label=False today=dt(today.year, today.month, today.day) delta=td(1,0,0) #间隔一天 for i in stolist: date=dt(2004,10,5) label=False while date<today: if label: pool=Pool(14) for d in range(14): timestr=dt.strftime(date,'%Y-%m-%d') print(timestr+' fast mood') pool.apply_async(fetchDt, args=(i,timestr,), error_callback=printError) date=date+delta pool.close() pool.join() continue timestr=dt.strftime(date,'%Y-%m-%d') print(timestr+' not fast mood') df=ts.get_tick_data(i,date=timestr) if df.empty or df.iloc[0,0].startswith('alert'): print('no data or failed') date=date+delta continue # print(timestr) timestr=timestr+' ' if not label: #提取tickStart if not df.iloc[0,0].startswith('alert'): label=True try: sec=Securities.objects.get({'code':i}) except Securities.DoesNotExist: sec=Securities(config.StocksList[i], i, exchange).save() TimeSeries(sec, 'security.price','tick', timestr+df.iloc[-1,0]) # MongodbJson[i]['tickStart']=timestr+df.iloc[-1,0] result=_dataFrame2MetricsList(df,str_volume_json,date=timestr,code=i) database.insertList(result) result=_dataFrame2MetricsList(df,str_price_json,date=timestr,code=i) database.insertList(result) date=date+delta pass with open('progress.ini','w') as f: f.write(str(i)) pass
# ts.get_hist_data('600848', ktype='15') #获取15分钟k线数据 # ts.get_hist_data('600848', ktype='30') #获取30分钟k线数据 # ts.get_hist_data('600848', ktype='60') #获取60分钟k线数据 # ts.get_hist_data('sh')#获取上证指数k线数据,其它参数与个股一致,下同 # ts.get_hist_data('sz')#获取深圳成指k线数据 # ts.get_hist_data('hs300')#获取沪深300指数k线数据 # ts.get_hist_data('sz50')#获取上证50指数k线数据 # ts.get_hist_data('zxb')#获取中小板指数k线数据 # ts.get_hist_data('cyb')#获取创业板指数k线数据 History_bar_df1 = ts.get_h_data(target_stock_code, start='2015-01-01', end='2015-03-16') #两个日期之间的前复权数据 '''获取全部历史数据''' # ts.get_h_data('002337') #前复权 # ts.get_h_data('002337', autype='hfq') #后复权 # ts.get_h_data('002337', autype=None) #不复权 # ts.get_h_data('399106', index=True) #深圳综合指数 History_tick_df = ts.get_tick_data(target_stock_code,date='2014-01-09') '''历史分笔数据明细(无bidask)''' #==============================================================================即时数据============================================================================== realTimeAll = ts.get_today_all() '''当前交易所有股票的行情数据''' realTimeIndex = ts.get_index() '''当前大盘的行情数据''' realtime_tran_data = ts.get_realtime_quotes(target_stock_code) '''实时分笔数据''' #ts.get_realtime_quotes(['600848','000980','000981']) #symbols from a list #ts.get_realtime_quotes('sh')#上证指数 #ts.get_realtime_quotes(['sh','sz','hs300','sz50','zxb','cyb'])#上证指数 深圳成指 沪深300指数 上证50 中小板 创业板 #ts.get_realtime_quotes(['sh','600848'])#或者混搭 realday_deal_data = ts.get_today_ticks(target_stock_code) '''当日历史分笔''' big_deal_data = ts.get_sina_dd(target_stock_code, date=transaction_day)
# -*- coding: utf-8 -*- import tushare as ts df = ts.get_hist_data('600848') ts.get_hist_data('600848', ktype='W') #获取周k线数据 ts.get_hist_data('600848', ktype='M') #获取月k线数据 ts.get_hist_data('600848', ktype='5') #获取5分钟k线数据 ts.get_hist_data('600848', ktype='15') #获取15分钟k线数据 ts.get_hist_data('600848', ktype='30') #获取30分钟k线数据 ts.get_hist_data('600848', ktype='60') #获取60分钟k线数据 ts.get_hist_data('sh') #获取上证指数k线数据,其它参数与个股一致,下同 ts.get_hist_data('sz') #获取深圳成指k线数据 ts.get_hist_data('hs300')#获取沪深300指数k线数据 ts.get_hist_data('sz50') #获取上证50指数k线数据 ts.get_hist_data('zxb') #获取中小板指数k线数据 ts.get_hist_data('cyb') #获取创业板指数k线数据 #Python财经数据接口包TuShare的使用 #获取历史分笔数据 df = ts.get_tick_data('000756', '2015-03-27') #print(df.head(10))
if r != 0: return df_yestoday cur_datetime = yestoday_datetime #python main.py -s 603611 -d 2018-04-09 if __name__ == "__main__": opts, args = getopt.getopt(sys.argv[1:], "s:d:", ["sid=", "date="]) sid = getOptvalue(opts, '-s') date = getOptvalue(opts, '-d') cur_datetime = datetime.strptime(date, '%Y-%m-%d') df_yestoday = getBeforeData(sid, cur_datetime) print(df_yestoday) close_yestoday = float(df_yestoday.close) df = ts.get_tick_data(sid, date=date) filename = sid + ".csv" df.to_csv(filename) all_datas = [] with codecs.open(filename, 'r', 'utf-8') as f: f_csv = csv.reader(f) headers = next(f_csv) print(headers) for row in f_csv: all_datas.insert(0, row) predatetime = None prePrice = 0 dis = 0 suList = [] for data in all_datas: dtstr = date + ' ' + data[1]
import tushare as ts import pandas as pd all_pd = [] calData = ts.get_k_data('600036', start='2016-01-01', end='2016-12-31', autype='qfq') #calData = ts.get_h_data('600036', start='2016-01-01', end='2016-12-31') for i in range(len(calData)): #list(calData.index): time_list = [] one_date = calData.at[i, 'date'] print one_date df = ts.get_tick_data('600036', date=one_date) try: df['time'] = one_date + ' ' + df['time'] df['time'] = pd.to_datetime(df['time']) except: print df continue df = df.set_index('time') #进行采样得到开始的价格,最高价,最低价,结束的价格 price_df = df['price'].resample('1min').ohlc() price_df = price_df.dropna() #删除空值 all_pd.append(price_df) result = pd.concat(all_pd)
def add_money_flow_today(): """ 沪深300当日生成小单统计数据 tips:成交明细列表中的买盘/卖盘:“买盘”表示以比市价高的价格进行委托买入,并已经“主动成交”,代表外盘; “卖盘”表示以比市价低的价格进行委托卖出,并已经“主动成交”,代表内盘 :return: """ print('插入当日现金流信息开始......') hs300_index = tushare.get_hs300s() ts_codes = hs300_index['code'].apply(util.stock_code_change) for ts_code in set(ts_codes): try: date = datetime.datetime.now().strftime('%Y%m%d') daily_info = tushare_data.get_tushare_pro().daily(ts_code=ts_code, trade_date=date) close = daily_info['close'] df = tushare.get_tick_data(str(ts_code[:6]), date=date, src='tt') if df is None: continue total_amt = df['amount'].sum() total_vol = df['volume'].sum() sell_trade = df.loc[(df['type'] == '卖盘')] total_sell_vol = sell_trade['volume'].sum() total_sell_amt = sell_trade['amount'].sum() small_trade_amount = util.trade_scale(close) sell_sm_trade = df.loc[(df['type'] == '卖盘') & (df['amount'] < small_trade_amount)] sell_sm_vol = sell_sm_trade['volume'].sum() sell_sm_amt = sell_sm_trade['amount'].sum() buy_trade = df.loc[(df['type'] == '买盘')] total_buy_vol = buy_trade['volume'].sum() total_buy_amt = buy_trade['amount'].sum() buy_sm_trade = df.loc[(df['type'] == '买盘') & (df['amount'] < small_trade_amount)] buy_sm_vol = buy_sm_trade['volume'].sum() buy_sm_amt = buy_sm_trade['amount'].sum() total_sm_trade = df.loc[df['amount'] < small_trade_amount] total_sm_amt = total_sm_trade['amount'].sum() total_sm_vol = total_sm_trade['volume'].sum() id = ts_code[:6] + date tables.add_money_flow(id=id, code=str(ts_code[:6]), date=date, sell_sm_vol=sell_sm_vol, sell_sm_amt=sell_sm_amt, buy_sm_vol=buy_sm_vol, buy_sm_amt=buy_sm_amt, total_sell_vol=total_sell_vol, total_sell_amt=total_sell_amt, total_buy_vol=total_buy_vol, total_buy_amt=total_buy_amt, total_amt=total_amt, total_vol=total_vol, total_sm_amt=total_sm_amt, total_sm_vol=total_sm_vol) print('插入当日现金流信息完成......') except Exception as err: print('插入当日现金流信息失败......') raise err
end_date = '2020-10-28' stock_k = ts.get_hist_data(stock_code, start=start_date, end=end_date) print(stock_k) # print(stock_k.index) # Index(['2020-10-28', '2020-10-27'], dtype='object', name='date') # 建立一个空的DataFrame,用于存储当前股票的信息 stock_table = pd.DataFrame() for current_date in stock_k.index: # # 通过loc定位K线图中对应current_date这天的行数据 current_k_line = stock_k.loc[current_date] df = ts.get_tick_data(stock_code, date=current_date, src='tt') df['time'] = pd.to_datetime(current_date + ' ' + df['time']) t = pd.to_datetime(current_date).replace(hour=9, minute=40) df_10 = df[df['time'] < t] #vol=df_10.volume.sum() vol = df_10['volume'].sum() # 存入字典 current_stock_info = { '名称': stock_name, '日期': pd.to_datetime(current_date), '开盘价': current_k_line.open,
def generateline(stocknumber, Type, startdate, enddate, interval): startdata = startdate.encode("ascii").replace("/", "-").replace( "\n", "") #convert to tushare readable date enddata = enddate.encode("ascii").replace("/", "-").replace("\n", "") array = df() #print startdata #print enddata current_time = time.strftime("%Y/%m/%d") if Type == "分笔".decode("utf-8"): if startdate != current_time: array = ts.get_tick_data(stocknumber, date=startdata) #分笔 if array is None: return array = array.sort_values("time") date = array["time"].tolist() amount = array["amount"].tolist() atype = array["type"].tolist() price = array["price"].tolist() flag = ["bar" for i in date] for idx, val in enumerate(atype): #if卖盘,交易变成负数 if val == "卖盘": amount[idx] = -amount[idx] if val == "中性盘": #if中性盘,则忽略. Might have a problem with this part?? amount[idx] = 0 returnarray = zip(date, amount, flag, price) return returnarray else: array = ts.get_today_ticks(stocknumber) #Tushare里今日分笔和历史分笔需要分别对待 if array is None: return array = array.sort_values("time") date = array["time"].tolist() amount = array["amount"].tolist() atype = array["type"].tolist() flag = ["bar" for i in date] for idx, val in enumerate(atype): if val == "卖盘".decode("utf-8"): amount[idx] = -amount[idx] if val == "中性盘".decode("utf-8"): amount[idx] = 0 returnarray = zip(date, amount, flag) return returnarray if interval != "qfq" and interval != "hfq": #正常历史k线 if Type != "Kline": array = ts.get_k_data(stocknumber, start=startdata, end=enddata, ktype=interval) if array is None: return Type1 = firstletter(Type).encode("ascii") target = array[Type1].tolist() date = array["date"].tolist() returnarray = zip(date, target) return returnarray else: array = ts.get_k_data(stocknumber, start=startdata, end=enddata, ktype=interval) if array is None: return Date = array["date"].tolist() Open = array["open"].tolist() Close = array["close"].tolist() High = array["high"].tolist() Low = array["low"].tolist() Candlestick = zip(*[Date, Open, Close, Low, High]) return Candlestick else: if Type != "Kline": # 复权 array = ts.get_h_data(stocknumber, start=startdata, end=enddata, autype=interval) if array is None: return Type1 = firstletter(Type).encode("ascii") array = array.sort_index() target = array[Type1].tolist() date = array.index.format() returnarray = zip(date, target) return returnarray else: array = ts.get_h_data(stocknumber, start=startdata, end=enddata, autype=interval) if array is None: return array = array.sort_index() Date = array.index.format() Open = array["open"].tolist() Close = array["close"].tolist() High = array["high"].tolist() Low = array["low"].tolist() Candlestick = zip(*[Date, Open, Close, Low, High]) return Candlestick
import tushare as ts ts.set_token('7483f126babf5e250781d90a96d28f057961392f482e10b98ef2914c') pro = ts.pro_api() # 历史名称变更记录 df = pro.namechange(ts_code='600848.SH', fields='ts_code,name,start_date,end_date,change_reason') print(df) df = ts.get_tick_data('600848', date='2018-12-12', src='tt') print(df.head(10))
import tushare as ts myStockData = ts.get_realtime_quotes("300445") ts.get_tick_data("600096",date= "2017-06-26") # # some thing that is really useful # data = ts.get_hist_data("600096",start = '2017-06-23',end="2017-06-26") # return type # 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 myStockData.value for key in myStockData: print(myStockData[key])
def test2(): import tushare as ts df = ts.get_tick_data('603106', date='2019-05-13', src='tt') print(df)
# -*- coding: utf-8 -*- """ @author: @file: .py @date: """ import tushare as ts data = ts.get_hist_data('600848', start='2015-01-05', end='2015-01-09') df = ts.get_tick_data('600848', date='2015-12-24') ns = ts.new_stocks() print data print df
def get_deal_detail(code, date): df = ts.get_tick_data(code, date=date) df = df.head(100) df.columns = ["交易时间", "成交价格", "价格变动", "成交手", "成交金额", "买卖类型"] return df
def tick2period(code, period, start, end): """ get tick data from tushare and resample to certain period data selected by input: period """ import tushare as ts import numpy as np import pandas as pd dfout = None #get valid trade date valid_dates = ts.get_hist_data(code, start=start, end=end).index for date in valid_dates: #date=date.strftime('%Y-%m-%d') rng = pd.date_range( date + ' 9:30:00', date + ' 15:00', closed='right', freq=period) #setup trade time grid by period selected sr = pd.Series(np.nan, index=rng) df = ts.get_tick_data(code, date=date) df.loc[df.time < '09:30:00', 'time'] = '09:30:01' #process open call auction df.loc[df.time > '15:00:00', 'time'] = '14:59:59' #process close call auction df['time'] = date + ' ' + df['time'] df = df.rename(columns={'time': 'datetime'}) df['datetime'] = pd.to_datetime(df['datetime']) df = df.set_index('datetime').sort() df2 = df['volume'].resample(period, how='sum', closed='right', label='right') df2, dummy = df2.align(sr, axis=0) #align to standard time df3 = df2.truncate(before=date + ' 13:00:01', after=date + ' 15:00') df2 = df2.truncate(before=date + ' 9:30:01', after=date + ' 11:30') #remove non-trade time df2 = df2.append(df3).fillna( 0) #fill with 0 for period without valid deal df1 = df['price'].resample(period, how='ohlc', closed='right', label='right') df1, dummy = df1.align(sr, axis=0) #align to standard time df3 = df1.truncate(before=date + ' 13:00:01', after=date + ' 15:00') df1 = df1.truncate(before=date + ' 9:30:01', after=date + ' 11:30') #remove non-trade time df1 = df1.append(df3) if np.isnan( df1.ix[0, 'close'] ): #use last day's close as initial price if there is no deal after open from datetime import timedelta aDay = timedelta( days=-10 ) #get enough days to ensure at least one trading day is involved pre = (pd.to_datetime(date) + aDay).strftime('%Y-%m-%d') df1.ix[0, 'close'] = ts.get_hist_data(code, start=pre, end=date).ix[-2, 'close'] df1['close'].fillna( method='pad', inplace=True ) #use price before if there is no deal during current period df1.fillna( method='bfill', inplace=True, axis=1 ) #use close as open,high,low if there is no deal during current period df1['volume'] = df2.values dfout = pd.concat([dfout, df1]) #print dfout #assert(False) return dfout
def _get_tick_history(self, day): return ts.get_tick_data(self.code, date=day)
def tushare_to_mongo(): conn = pymongo.Connection('127.0.0.1', port=27017) df_index = ts.get_tick_data('600848', date='2014-12-22') df = df_index.reset_index() conn.db.tickdata.insert(json.loads(df.to_json(orient='records')))
import tushare as ts import cn.apking.config as config ts.set_token(config.token) pro = ts.pro_api() # wanke = ts.get_k_data('000002', start='2016-01-01', end='2019-01-31') # print(wanke) # df = pro.new_share(start_date='20210210', end_date='20210220') # print(df) from sqlalchemy import create_engine import tushare as ts df = ts.get_tick_data('600848', date='2014-12-22') engine = create_engine('mysql://*****:*****@127.0.0.1/db_name?charset=utf8') #存入数据库 df.to_sql(df, engine) #追加数据到现有表 #df.to_sql('tick_data',engine,if_exists='append')