def Signal(coin_symbol, end_dt): cons = ts.get_apis() temp_day = ts.tick(code=coin_symbol, conn=cons, date=end_dt) print(temp_day) amount = [] vol = [] close = [] for i in range(len(temp_day)): temp_list = temp_day.ix[i] amount.append(float(temp_list[1]) * float(temp_list[2])) vol.append(float(temp_list[2])) close.append(float(temp_list[1])) amount = np.array(amount) vol = np.array(vol) close = np.array(close) price_avg_long = [] price_avg_short = [] price_close = [] delt_list = [] for i in range(1, len(amount)): price_avg_long.append(float(amount[:i + 1].mean() / vol[:i + 1].mean())) price_avg_short.append(float(amount[i] / vol[i])) price_close.append(close[i]) delt_list.append( float(amount[:i + 1].mean() / vol[:i + 1].mean()) / float(amount[i - 1:i].mean() / vol[i - 1:i].mean())) return price_avg_long, price_avg_short, price_close, delt_list
def get_ticks(self, code): # time, price, volume, type date = time.strftime('%Y-%m-%d', time.localtime()) ts_api = ts.get_apis() df = ts.tick(code.split('.')[0], conn=ts_api, date=date, asset='E') if df is None: df = pd.DataFrame() return df
def getTickData(self): """ 通过tushare获取分时数据,并存入数据库,避免后面运行重复获取。 """ df = None db = self.connectDb() flt = {'date': self.date} result = db[self.collectionName].find_one(flt) if not result: print(u'数据库无数据,正在尝试从tushare获取数据..') cons = ts.get_apis() df = ts.tick(self.symbol, conn=cons, date=self.date) if df is not None: df['date'] = self.date db[self.collectionName].insert_many(df.to_dict('records')) print(u'成功获取并写入数据库。') else: print(u'获取数据出错,可能接口问题,或者日期设置不正确。') else: print(u'数据库有匹配数据') data = list(db[self.collectionName].find(flt, projection={'_id': False})) df = pd.DataFrame(data) if df is not None: df.drop_duplicates('datetime', keep='last', inplace=True) df.reset_index(inplace=True, drop=True) df.datetime = df.datetime.map(lambda dateStr: dateStr[-5:]) self.tickData = df return df
def get_tick_data_df(adj_code, trading_date): """ type:买卖方向,0-买入 1-卖出 2-集合竞价成交 change:价格变动(元) volume:成交手 amount:成交金额(元) """ df = ts.tick(adj_code, conn=conns, date=trading_date) return df
def get_tick_data(code=None, date=None, retry_count=3, pause=0.001, src='sn', asset='X', data_source='tushare'): """rf tick数据 Parameters: ------------ code:证券代码,支持股票,ETF/LOF,期货/期权,港股 conn:服务器连接 ,通过ts.api()或者ts.xpi()获得 date:日期 asset:证券品种,E:沪深交易所股票和基金, INDEX:沪深交易所指数, X:其他证券品种,大致如下: 支持的扩展行情包括(asset='X'): 郑州商品期权 OZ 大连商品期权 OD 上海商品期权 OS 上海个股期权 QQ 香港指数 FH 郑州商品 QZ 大连商品 QD 上海期货 QS 香港主板 KH 香港权证 KR 开放式基金 FU 货币型基金 FB 招商理财产品 LC 招商货币产品 LB 国际指数 FW 国内宏观指标 HG 中国概念股 CH 美股知名公司 MG B股转H股 HB 股份转让 SB 股指期货 CZ 香港创业板 KG 香港信托基金 KT 国债预发行 GY 主力期货合约 MA 中证指数 ZZ 港股通 GH market:市场代码,通过ts.get_markets()获取 Return ---------- DataFrame date:日期 time:时间 price:成交价 vol:成交量 type:买卖方向,0-买入 1-卖出 2-集合竞价成交 期货 0:开仓 1:多开 -1:空开 期货多一列数据oi_change:增仓数据 """ if is_today(date): return ts.get_today_ticks(code, retry_count, pause) if is_tushare(data_source): return ts.get_tick_data(code, date, retry_count, pause, src) elif is_tdx(data_source): if ac.TDX_CONN is None: ac.TDX_CONN = fs.get_apis() return ts.tick(code, date=date, conn=ac.TDX_CONN, asset=asset)
def update_bigorder(stock_pool, date_seq): cons = ts.get_apis() # 建立数据库连接,剔除已入库的部分 db = pymysql.connect(host='127.0.0.1', user='******', passwd='admin', db='stock', charset='utf8') cursor = db.cursor() for s in range(len(stock_pool)): for d in range(len(date_seq)): try: temp_day2 = ts.tick(code=stock_pool[s], date=date_seq[d], conn=cons) print(temp_day2) ans = len(temp_day2) except Exception as exp: print('Inner Errrr' + str(exp))
def tick_from_tushare( code, conn=None, date='', asset='E', market='', retry_count=3): """ tick数据 Parameters: ------------ code:证券代码,支持股票,ETF/LOF,期货/期权,港股 conn:服务器连接 ,通过ts.api()或者ts.xpi()获得 date:日期 asset:证券品种,E:沪深交易所股票和基金, INDEX:沪深交易所指数, X:其他证券品种,大致如下: 支持的扩展行情包括(asset='X'): 郑州商品期权 OZ 大连商品期权 OD 上海商品期权 OS 上海个股期权 QQ 香港指数 FH 郑州商品 QZ 大连商品 QD 上海期货 QS 香港主板 KH 香港权证 KR 开放式基金 FU 货币型基金 FB 招商理财产品 LC 招商货币产品 LB 国际指数 FW 国内宏观指标 HG 中国概念股 CH 美股知名公司 MG B股转H股 HB 股份转让 SB 股指期货 CZ 香港创业板 KG 香港信托基金 KT 国债预发行 GY 主力期货合约 MA 中证指数 ZZ 港股通 GH market:市场代码,通过ts.get_markets()获取 Return ---------- DataFrame date:日期 time:时间 price:成交价 vol:成交量 type:买卖方向,0-买入 1-卖出 2-集合竞价成交 期货 0:开仓 1:多开 -1:空开 期货多一列数据oi_change:增仓数据 """ return ts.tick(code, conn, date, asset, market, retry_count)
def ticks(code, source="spider", date=None, cons=None): """返回date日期的分笔数据 :param source: :param code: str: 股票代码,如 603655 :param date: str: 日期,如 2018-03-15 :param cons: tushare的api连接 :return: """ if not date: date = datetime.now().date().__str__() TODAY = datetime.now().date().__str__() # 统一 ticks 的输出结果 def _unify_out(ticks, date): ticks = ticks[['time', 'price', 'volume', 'type']] ticks['datetime'] = ticks['time'].apply( lambda x: datetime.strptime(date + " " + x, "%Y-%m-%d %H:%M:%S")) ticks['vol'] = ticks['volume'] type_convert = {"买盘": 0, "卖盘": 1, "中性盘": 2, "0": 2} ticks['type'] = ticks["type"].apply(lambda x: type_convert[str(x)]) ticks.drop(['time', 'volume'], axis=1, inplace=True) ticks.sort_values('datetime', inplace=True) ticks.reset_index(drop=True, inplace=True) return ticks[['datetime', 'price', 'vol', 'type']] if source == "spider" and date == TODAY: ticks = ts.get_today_ticks(code=code) ticks = _unify_out(ticks, date=TODAY) elif source == "spider" and date != TODAY: ticks = ts.get_tick_data(code=code, date=date) ticks = _unify_out(ticks, date=date) else: if not cons: cons = ts.get_apis() ticks = ts.tick(code=code, conn=cons, date=date) return ticks
warnings.filterwarnings("ignore") import tushare as ts price = pd.read_pickle('/Users/harbes/data/xccdata/PV_datetime')['clsprc'].unstack() price.name='close' price.columns=price.columns.str.slice(0,6) price.index=price.index.astype(str) price_nan = np.isnan(price) inti_time=time.time() trade_price=pd.DataFrame(np.nan,index=price.index,columns=price.columns) trade_type=pd.DataFrame(np.nan,index=price.index,columns=price.columns) for d in price.index[50:51]: for stk in price.columns[:10]: if price_nan.loc[d, stk] is not False: tmp = ts.tick(stk, date=d, conn=ts.get_apis()) if tmp is not None: #trade_price.to_pickle('F:/data/xccdata/trade_price') #trade_type.to_pickle('F:/data/xccdata/trade_type') delta_time = time.time() - inti_time; delta_time # 50只股票需要60s左右 # trade_price.loc[price.index[0],'600848'],trade_type.loc[price.index[0],'600848']=ts.tick('600848', date=price.index[0],conn=ts.get_apis()).loc[0][['price','type']] ts.get_tick_data(stk, date=d) tmp = ts.tick('000009', date=price.index[50], conn=ts.get_apis()); tmp
continue sql_check = "select * from future_tick2 a where a.state_dt = '%s' and a.future_code = '%s'" % ( date_seq[i], future_code) cursor.execute(sql_check) done_set_check = cursor.fetchall() db.commit() if len(done_set_check) > 0: print(str(date_seq[i]) + ' Already Exists') continue cnt_try = 0 c_len = -1 while True: try: temp_price = ts.tick(future_code, conn=cons, asset='X', date=date_seq[i]) #print(temp_price) c_len = temp_price.shape[0] if c_len >= 0: break except Exception as bb: cnt_try += 1 cons = ts.get_apis() print(cnt_try) time.sleep(17) continue print(str(date_seq[i]) + ' Tick Len : ' + str(c_len)) if c_len > 0: for j in range(c_len):
def get_big_order(stock_selected, conn, date, vol): df = ts.tick(stock_selected, conn=conn, date=date) if df is None: return None else: return df[df.vol > vol].reset_index() #reset index to row number
#!/usr/bin/env python3 # - * - coding: UTF-8 - * - import tushare as ts cons = ts.get_apis() """ df = ts.bar('600547',conn=cons,freq='D',adj='qfq',start_date='2017-11-01',end_date='') print(df) """ df = ts.tick('600547', conn=cons, date='2017-11-01') df.head(20) print(df)
def TsTickMonitor(L): ''' 如函数报错:'NoneType' object has no attribute 'iat' 请检查个别品种是否不在交易时间,如夜盘时间无夜盘交易的商品期货 ''' TickPrice = OrderedDict() TickTime = OrderedDict() for i in L: if i.isdigit(): if len(i) == 6: if i.startswith('1') or i.startswith('5'): #沪深场内基金 TickPrice[i] = ts.tick( i, conn=cons, date=Today, asset='E').iloc[-1, 1] / 10.0 #倒数第一行第三列元素 TickTime[i] = ts.tick(i, conn=cons, date=Today, asset='E').iloc[-1, 0] #timestamp type else: #沪深股票 TickPrice[i] = ts.tick(i, conn=cons, date=Today, asset='E').iloc[-1, 1] TickTime[i] = ts.tick(i, conn=cons, date=Today, asset='E').iloc[-1, 0] if len(i) == 5 and i.startswith('0'): try: #港股ETF有6列,港股有4列,先try港股ETF TickPrice[i] = ts.tick(i, conn=cons, date=Today, asset='X').iloc[-1,5]*0 + \ ts.tick(i, conn=cons, date=Today, asset='X').iloc[-1,3]/1000.0 TickTime[i] = ts.tick(i, conn=cons, date=Today, asset='X').iloc[-1, 0] except: #港股 TickPrice[i] = ts.tick(i, conn=cons, date=Today, asset='X').iloc[-1, 2] / 1000.0 TickTime[i] = ts.tick(i, conn=cons, date=Today, asset='X').iloc[-1, 0] else: #字母+4位数字,国内商品期货 TickPrice[i] = ts.tick(i, conn=cons, date=Today, asset='X').iloc[-1, 2] / 1000.0 TickTime[i] = ts.tick(i, conn=cons, date=Today, asset='X').iloc[-1, 0] return TickPrice, TickTime
# -*- coding: utf-8 -*- """ Created on Wed Dec 06 14:30:39 2017 @author: zack zhang """ import tushare as ts import os import pandas as pd from pandas import Series from pandas import DataFrame as df import tushare as ts import datetime import time from collections import OrderedDict from decimal import getcontext, Decimal from threading import Timer cons = ts.get_apis() df1 = ts.new_cbonds(default=0) #转债列表,dataframe df2 = ts.new_cbonds(default=1) dfk = ts.bar('128016', conn=cons) #转债日K线,dataframe dftick = ts.tick('128016', conn=cons, date='20171206') #转债tick行情,dataframe dfquotes = ts.quotes('128016', conn=cons) #转债实时切片行情,一行 print dfquotes ''' 1、搞清正股和转债的联动内在逻辑关系(下调转股价等) 2、设定实时行情信号提醒参数 3、技术上实现实时提醒(首选微信) '''
import tushare as ts import os filename = 'c:/day/bigfile.csv' for code in ['CU1801']: #df = ts.get_hist_data(code) #print df cons = ts.get_apis() #df = ts.bar(code,conn=cons, freq='1min', asset='X' ,start_date='2017-10-01', end_date='') df = ts.tick(code, conn=cons, asset='X') df.head(5) # df = df.sort_index() print df if os.path.exists(filename): df.to_csv(filename, mode='a', header=None) else: df.to_csv(filename) ts.close_apis(cons) #http://www.360doc.com/content/17/1030/16/14934540_699467341.shtml #https://github.com/waditu/tushare/tree/master/test
# 建立数据库连接,剔除已入库的部分 db = pymysql.connect(host='127.0.0.1', user='******', passwd='admin', db='stock', charset='utf8') cursor = db.cursor() sql_select = "SELECT state_dt,stock_code FROM stock_all w where w.big_order_cntro is null and w.big_order_delt is null limit 1000" cursor.execute(sql_select) done_set = cursor.fetchall() batch_cnt = 1 # 大单数据统计(30万交易额以上): try: while len(done_set) > 0: for i in range(len(done_set)): try: temp_day2 = ts.tick(code=done_set[i][1],date=done_set[i][0],conn=cons) ans = len(temp_day2) except Exception as exp: print('Inner Errrr' + str(exp)) continue df_buy = temp_day2.ix[temp_day2.type == 0] cnt_buy = df_buy.ix[df_buy.vol > 400].count()[2] vol_buy = df_buy.ix[df_buy.vol > 400].sum()[2] if str(vol_buy) == 'nan': vol_buy = 0 df_sell = temp_day2.ix[temp_day2.type == 1] cnt_sell = df_sell.ix[df_sell.vol > 400].count()[2] vol_sell = df_sell.ix[df_sell.vol > 400].sum()[2] if str(vol_sell) == 'nan': vol_sell = 0 cntro = 0.00
def update_bigorder(stock_pool): cons = ts.get_apis() # 建立数据库连接,剔除已入库的部分 db = pymysql.connect(host='127.0.0.1', user='******', passwd='admin', db='stock', charset='utf8') cursor = db.cursor() for s in range(len(stock_pool)): sql_select = "SELECT state_dt,stock_code FROM stock_all w where w.big_order_cntro is null and w.stock_code = '%s'"%(stock_pool[s]) #sql_select = "SELECT state_dt,stock_code FROM stock_all w where w.stock_code = '%s'"%(stock_pool[s]) cursor.execute(sql_select) done_set = cursor.fetchall() db.commit() # 大单数据统计(30万交易额以上): try: if len(done_set) > 0: for i in range(len(done_set)): try: temp_day2 = ts.tick(code=done_set[i][1], date=done_set[i][0], conn=cons) ans = len(temp_day2) except Exception as exp: print('Inner Errrr' + str(exp)) continue df_buy = temp_day2.ix[temp_day2.type == 0] #cnt_buy = df_buy.ix[df_buy.vol > 400].count()[2] #vol_buy = df_buy.ix[df_buy.vol > 400].sum()[2] #if cnt_buy == 0: cnt_buy = df_buy.ix[df_buy.vol * df_buy.price * 100 > 1000000].count()[2] vol_buy = df_buy.ix[df_buy.vol * df_buy.price * 100 > 1000000].sum()[2] if str(vol_buy) == 'nan': vol_buy = 0 df_sell = temp_day2.ix[temp_day2.type == 1] #cnt_sell = df_sell.ix[df_sell.vol > 400].count()[2] #vol_sell = df_sell.ix[df_sell.vol > 400].sum()[2] #if cnt_sell == 0: cnt_sell = df_sell.ix[df_sell.vol * df_sell.price * 100 > 1000000].count()[2] vol_sell = df_sell.ix[df_sell.vol * df_sell.price * 100 > 1000000].sum()[2] if str(vol_sell) == 'nan': vol_sell = 0 cntro = 0.00 if cnt_buy + cnt_sell > 0: cntro = cnt_buy / (cnt_buy + cnt_sell) delt = vol_buy - vol_sell print('Stock_Seq : ' + str(s+1) + ' Inner_Seq : ' + str(i + 1) + ' of ' + str( len(done_set)) + ' stock_code : ' + str(done_set[i][1]) + ' Date : ' + str(done_set[i][0])) sql_update = "update stock_all w set w.big_order_cntro = '%.2f' where w.state_dt = '%s' and w.stock_code = '%s'" % ( cntro, done_set[i][0], done_set[i][1]) cursor.execute(sql_update) db.commit() sql_update2 = "update stock_all w set w.big_order_delt = '%i' where w.state_dt = '%s' and w.stock_code = '%s'" % ( delt, done_set[i][0], done_set[i][1]) cursor.execute(sql_update2) db.commit() sql_resu = "select state_dt,big_order_cntro,big_order_delt from stock_all a where a.stock_code = '%s' order by state_dt desc limit 5"%(stock_pool[s]) cursor.execute(sql_resu) done_set_resu = cursor.fetchall() db.commit() print('Stock : ' + str(stock_pool[s])) print('Date_Seq : ',end='') print([str(x[0]) for x in done_set_resu][::-1]) print('Big_Order_Cntro : ', end='') print([str(x[1]) for x in done_set_resu][::-1]) print('Big_Order_Delt : ', end='') print([str(x[2]) for x in done_set_resu][::-1]) del done_set_resu del done_set except Exception as excp: # db.rollback() print(str('Errr') + str(i) + str(excp)) db.close() print('ALL Finished!!')
# 7. 期货数据, 设置asset='X' df = ts.bar('CU1801', conn=cons, asset='X', start_date='2016-01-01', end_date='') df.head(5) # 8. 美股数据, 设置asset='X' df = ts.bar('BABA', conn=cons, asset='X', start_date='2016-01-01', end_date='') df.head(5) # __________________________________________________________________________ # 1. 股票tick # type:买卖方向,0-买入 1-卖出 2-集合竞价成交 # 数据里没有增加代码一列,如果有需要可以同多df[‘code’] = code实现 df = ts.tick('600000', conn=cons, date='2017-10-26') df.head(20) # 2. 期货tick # 期货tick,type:买卖方向,0:开仓 1:多开 -1:空开 df = ts.tick('CU1801', conn=cons, asset='X', date='2017-10-25') df.head(20) # 沪/深港通每日资金流向(南向/北向资金) df = ts.moneyflow_hsgt() df.sort_values('date', ascending=False) # __________________________________________________________________________ # broker {hb|ok|chbtc} 中国比特币兑换中心 # ktype {D、W、M、1min、5min、15min、30min、60min}