def Get_5_10_20_data(self, code, start, end): data_5_data = ts.get_hist_data(code, start=start, end=end) #防止过多拉取数据源 网站屏蔽IP cons = ts.get_apis() # 增加前复权 adj='qfq' 后复权 adj='hfq'(bob_jie : 2017-11-01) data_5_data = ts.bar(code, conn=cons, start_date=start, adj='qfq', end_date=end, ma=[5, 10, 20], factors=['vr', 'tor']) try: data_5_data.values[0][0] except Exception as e: time.sleep(2) #防止过多拉取数据源 网站屏蔽IP cons = ts.get_apis() # 增加前复权 adj='qfq' 后复权 adj='hfq'(bob_jie : 2017-11-01) data_5_data = ts.bar(code, conn=cons, start_date=start, adj='qfq', end_date=end, ma=[5, 10, 20], factors=['vr', 'tor']) ts.get_h_data('002337', autype='qfq') Mysql().SaveMySqlTWO(data_5_data, 'Stock_Basics_Info_All', code + 'stock_basics', code)
def break_monitor(code, warning_vol): start = True start_monitor = True waiting_time = datetime.datetime.now() conn = ts.get_apis() while 1: current = datetime.datetime.now() try: if start_monitor: try: df = ts.quotes(code, conn=conn) except Exception as e: print(e) time.sleep(EXCEPTION_TIME_OUT) conn = ts.get_apis() continue print('under monitor {}'.format(current)) if df['bid_vol1'].values[0] < warning_vol and start: title=code+' Buy +1 : '+str(df['bid_vol1'].values[0]) sendmail(title,title) # msg.send_ceiling(code, df['bid_vol1'].values[0]) start = False start_monitor = False waiting_time = current + datetime.timedelta(seconds=TIME_RESET) time.sleep(NORMAL_TIME_OUT) except Exception as e: print(e) time.sleep(EXCEPTION_TIME_OUT) conn = ts.get_apis() continue if current > waiting_time: start = True start_monitor = True
def _get_cons(self): con1 = ts.get_apis() con2 = ts.get_apis() con3 = ts.get_apis() con4 = ts.get_apis() con5 = ts.get_apis() return [con1, con2, con3, con4, con5]
def get_realtime_info(self, codes, has_sent, types, stock, yjl, percent): try: price_df = ts.quotes(codes, conn=self.api) except Exception as e: logger.error('获取可转债异常 >>>> {}'.format(e)) try: self.api = ts.get_apis() except Exception as e: logger.error('异常中存在异常{}'.format(e)) time.sleep(EXECEPTION_TIME) else: if len(price_df) != 0: price_df = price_df[price_df['cur_vol'] != 0] price_df['percent'] = (price_df['price'] - price_df['last_close']) / price_df[ 'last_close'] * 100 price_df['percent'] = price_df['percent'].map(lambda x: round(x, 2)) ret_dt = \ price_df[ (price_df['percent'] > percent) | (price_df['percent'] < -1 * percent)][ ['code', 'price', 'percent']] if len(ret_dt) > 0: # 提醒一次后,下一次的间隔为DELTA_TIME分钟后 # sent_list = [] for i in ret_dt['code']: if has_sent[i] <= datetime.datetime.now(): name_list = [] yjl_list = [] name_list.append(stock[i]) yjl_list.append(yjl[i]) has_sent[i] = datetime.datetime.now() + datetime.timedelta(minutes=DELTA_TIME) ret_dt1 = ret_dt[ret_dt['code'] == i] ret_dt1['名称'] = name_list ret_dt1['溢价率'] = yjl_list name=ret_dt1['名称'].values[0] price=ret_dt1['price'].values[0] percent=ret_dt1['percent'].values[0] yjl_v = ret_dt1['溢价率'].values[0] now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') content0='{t}\n{name}:价格:{price} 涨幅:{percent},溢价率:{yjl}'.format(name=name,price=price,percent=percent,yjl=yjl_v,t=now) logger.info(content0) try: wechat.send_content(content0) except Exception as e: logger.info('发送微信失败') logger.info(e)
def write_all_stock(allAshare, lib=None): """ :param allAshare: List,所有股票 :param lib: arctic.store.version_store.VersionStore :return: succ: List, written stocks; fail: List, failed written stocks """ succ = [] fail = [] cons = ts.get_apis() if not os.path.exists(hqdir): os.mkdir(hqdir) for symbol in allAshare: try: df = ts.bar(symbol, cons, freq='D', start_date='', end_date='').sort_index(ascending=True) if MONGO and lib: lib.write(symbol, df, metadata={'source': 'Tushare'}) if CSV: df.to_csv(os.path.abspath("{}/{}.csv".format(hqdir, symbol))) print(symbol + '写入完成') succ.append(symbol) except Exception as e: fail.append(symbol) print("Failed for ", symbol, str(e)) sleep(0.1) if SFL: write_list(succ, os.path.abspath('./hq_succ_list.txt')) write_list(fail, os.path.abspath('./hq_fail_list.txt'))
def gjzq(file): df = pd.read_table(file, encoding='gbk', dtype={'证券代码': np.str}) del df['Unnamed: 15'] code_list = list(df['证券代码'].values) api = ts.get_apis() # 移除非法证券代码 中签 t=[code_list.remove(i) for i in code_list.copy() if i.startswith('7') or i[:2] == '07'] price_df = ts.quotes(code_list, conn=api) # 去除不合法的数据 price_df=price_df.dropna() filter_df = price_df[price_df['last_close'] != 0] filter_df = filter_df.reset_index(drop=True) filter_df['percent'] = (filter_df['price'] - filter_df['last_close']) / filter_df['last_close'] * 100 filter_df['percent'] = filter_df['percent'].map(lambda x: round(x, 2)) ret_df = filter_df[(filter_df['percent'] > ALERT_PERCENTAGE) | (filter_df['percent'] < ALERT_PERCENTAGE*-1)] d = dict(zip(list(df['证券代码'].values), list(df['证券名称']))) ret_df['name']=ret_df['code'].map(lambda x:d.get(x)) ret_df['amount']=ret_df['amount'].map(lambda x:round(x/10000,1)) rename_column = {'code':'证券代码','name':'证券名称','price':'当前价格','percent':'涨幅','amount':'成交金额(万)'} ret_df=ret_df.rename(columns=rename_column) ret = ret_df[list(rename_column.values())] ret=ret.reset_index(drop=True) # 发送推送 print(ret) ts.close_apis(api)
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 get_quotes(self, code): # ohl+price+volume+amount+ba1-5vp ts_api = ts.get_apis() df = ts.quotes(code.split('.')[0], conn=ts_api, asset='E') if df is None: return pd.DataFrame() df['close'] = df.loc[:, 'price'] return df
def __init__(self): app = QtWidgets.QApplication(sys.argv) main_window = QtWidgets.QMainWindow() self.ui = Ui_dog() self.ui.setupUi(main_window) # 银行基本信息目录及文件名 self.bank_basic_info_dir = r"C:\quanttime\src\watch_time\bank_info.csv" # tushare connect context token = "17e7755e254f02cc312b8b7e22ded9a308924147f8546fdfbe653ba1" ts.set_token(token) self.cons = ts.get_apis() # ts 授权 self.pro = ts.pro_api() # 设定默认的行情数据, 通达信默认行情源,tushare,joinquant可选 self.ui.checkBox_4.setCheckState(QtCore.Qt.Unchecked) self.ui.checkBox_5.setCheckState(QtCore.Qt.Checked) self.ui.checkBox_7.setCheckState(QtCore.Qt.Unchecked) # 默认只分析重点关注的券商标的 self.ui.checkBox.setCheckState(QtCore.Qt.Checked) # 银行基本信息更新按钮信号连接 self.ui.pushButton.clicked.connect(self.update_bank_industry_table) # 银行分红信息及系统排名表刷新信号连接 self.ui.pushButton_2.clicked.connect(self.update_bank_dividend) # 券商估值分析pushbutton按钮signal连接 self.ui.pushButton_3.clicked.connect(self.analyze_security_valuation) main_window.show() sys.exit(app.exec_())
def get_bar_realtime_code(): ap = ts.get_apis() data = ts.bar('600570', ap, start_date='20190516', end_date='20190517', freq='1min') # if os.path.exists(filename): # data.to_csv( # filename, # mode='a', # header=False, # encoding='utf-8', # na_rep='NA') # else: # data.to_csv(filename, encoding='utf-8', na_rep='NA') # pcolumns = data.columns # pindex = data.index # pdata = data[data.columns[0:10]] # pdata2 = data['首字符'] = data['code'].str[0:1] data['datetime'] = data.index # ps = data['time'] # print(ps) # print(type(data.index)) # data['time'] = pd.to_datetime(data['time'], format='%d.%m.%Y') # pddata = data.loc[-1] = ptime # data.loc[data['首字符'] == '6', '市场'] = '1' # ss = data.to_json(orient='split') content = data_to_json(data) # resp = Response_headers(content) return content
def start(): #获取连接备用 cons = ts.get_apis() # 导入个股 print("导入个股") # codes,names,timeToMarket = importStockList() # codes = ["000801"] # names = ["四川九洲"] # timeToMarket = [20171215] #000002.SZ errcode: -40521008 start_date:1993-01-23,end_date:1993-01-26 codes = ["300251"] names = ["万科A"] timeToMarket = [19910102] print(len(codes), len(names), len(timeToMarket)) importStockDataDailyWind(codes, names, timeToMarket) # test(codes) ''' #导入指数 print("导入指数") df = ts.get_index() codes = df["code"].values names = df["name"].values import import_Stock_Data_Daily import_Stock_Data_Daily.importStockDataDaily(codes,names,asset='INDEX',adj='None') ''' ts.close_apis(conn=cons) print("导入完成")
def insert_kdata(code, asset, freq, start, end): sheet = get_sheet(freq) cons = ts.get_apis() k_data = ts.bar(code, conn=cons, freq='5min', start_date='2016-01-01', end_date='', asset=asset) # 对DataFrame的个别列进行重命名 k_data = k_data.rename(columns={'code': 'code_origin'}) # 代码 k_data['code'] = code # 周期 k_data['freq'] = freq # 是否指数 k_data['asset'] = asset k_data = k_data[[ 'code', 'index', 'date', 'ktype', 'open', 'close', 'high', 'low', 'volume', 'code_origin' ]] # inplace默认为False,如果该值为False,那么原来的pd顺序没变,只是返回的是排序的 k_data.sort_values("date", ascending=False, inplace=True) records = pandas.io.json.loads(k_data.T.to_json()).values() global insert_result insert_result = sheet.insert_many(records, ordered=False) # except BulkWriteError as e: # print("insert_many出现重复,忽略错误") return insert_result
def tushare_bar(code, start, end, freq, **kwargs): df = ts.bar(code=code, conn = ts.get_apis(), start_date = start, end_date = end, \ freq = freq, asset='E', adj = 'qfq', **kwargs) if isinstance(df, pd.DataFrame): return df else: raise TypeError('df is unknown type %s' % type(df))
def __init__(self): self.conn=ts.get_apis() self.available_bonds = pd.read_sql('tb_bond_jisil', engine, index_col='index')['可转债代码'].values self.allBonds=ts.new_cbonds(default=0,pause=2) self.onSellBond=self.allBonds.dropna(subset=['marketprice']) self.today=datetime.datetime.now().strftime('%Y-%m-%d %H:%M') self.total = self.onSellBond[self.onSellBond['bcode'].isin(self.available_bonds)]
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 stock_plot(): left, width = 0.1, 0.8 rect_vol = [left, 0.1, width, 0.3] rect_main = [left, 0.4, width, 0.5] fig = plt.figure() conn = ts.get_apis() df = ts.bar('300333', conn=conn, start_date='2018-01-01') del df['code'] df.reset_index(inplace=True) dates=df['datetime'].values df['datetime'] = df['datetime'].map(mdates.date2num) # dates=df['datetime'].values vol = df['vol'].values ax_vol = fig.add_axes(rect_vol) ax_vol.fill_between(dates, vol,color = 'y') # 横坐标时间旋转 plt.setp(ax_vol.get_xticklabels(), rotation=30, horizontalalignment='right') ax_main = fig.add_axes(rect_main) candlestick_ochl(ax_main,df.values,width=0.6,colordown='g',colorup='r') ax_main.axes.get_xaxis().set_visible(False) ax_main.set_title("STOCK LINE") ts.close_apis(conn) plt.show()
def __init__(self): self.conn=ts.get_apis() self.available_bonds = pd.read_sql('tb_bond_jisilu', engine, index_col='index')['可转债代码'].values self.allBonds=ts.new_cbonds(default=0,pause=2) self.onSellBond=self.allBonds.dropna(subset=['marketprice']) self.today=datetime.datetime.now().strftime('%Y-%m-%d %H:%M') self.total = self.onSellBond[self.onSellBond['bcode'].isin(self.available_bonds)]
def get_price_diff(self, codes, has_sent_, types, kzz_stocks, kzz_stocks_yjl): # 针对可转债 try: df = ts.quotes(codes, conn=self.api) except Exception as e: logger.error('获取可转债异常 >>>> {}'.format(e)) try: self.api = ts.get_apis() except Exception as e: logger.error('异常中存在异常{}'.format(e)) time.sleep(EXECEPTION_TIME) else: df['bid1'] = df['bid1'].astype(float) df['ask1'] = df['ask1'].astype(float) df['diff'] = np.abs(df['bid1'] - df['ask1']) result = df[df['diff'] >= DIFF_V] if result.empty: # continue return else: for j in result['code']: if has_sent_[j] <= datetime.datetime.now(): has_sent_[j] = datetime.datetime.now( ) + datetime.timedelta(minutes=DIFF_DELTA_TIME) name_list = [] yjl_list = [] name_list.append(kzz_stocks[j]) yjl_list.append(kzz_stocks_yjl[j]) ret_dt1 = result[result['code'] == j] ret_dt1['名称'] = name_list ret_dt1['溢价率'] = yjl_list # ret_dt1 = ret_dt1.set_index('code', drop=True) code = j name = ret_dt1['名称'].values[0] price = ret_dt1['price'].values[0] bid = ret_dt1['bid1'].values[0] ask = ret_dt1['ask1'].values[0] diff = round(ret_dt1['diff'].values[0], 2) now = datetime.datetime.now().strftime( '%Y-%m-%d %H:%M:%S') content0 = '{t}\n{code}::{name}:价格:{price} 买1:{bid} 卖1:{ask}差价:{diff}'.format( code=code, name=name, price=price, bid=bid, ask=ask, diff=diff, t=now) logger.info(content0) try: wechat.send_content(content0) except Exception as e: logger.info('发送微信失败') logger.info(e)
def downMinuteBarBySymbol(symbol): """下载某一合约的分钟线数据""" start = time() cl = db[symbol] cl.ensure_index([('datetime', ASCENDING)], unique=True) # 添加索引 cons = ts.get_apis() df = ts.bar(symbol, cons, start_date='2018-01-10', end_date='2018-04-09', freq='1min', asset='X') df = df.sort_index() for ix, row in df.iterrows(): bar = generateVtBar(row) d = bar.__dict__ flt = {'datetime': bar.datetime} cl.replace_one(flt, d, True) end = time() cost = (end - start) * 1000 print u'合约%s数据下载完成%s - %s,耗时%s毫秒' % (symbol, df.index[0], df.index[-1], cost)
def GetstockData(code,start,end=None): api=ts.get_apis() if not end: end = datetime.datetime.now().strftime('%Y-%m-%d') print(end) df = ts.bar(code,conn=api,freq='D',start_date=start,end_date=end) df.to_excel('data/'+code+'.xls')
def downBarBySymbol(symbol, start_date=None, end_date=None, freq='D'): """下载某一合约的分钟线数据""" start = time() db_freq = freq #if freq[0] in ['0', '1', '2','3','4','5','6','7','8','9']: #db_freq = freq + 'MIN' db = mc[DB_NAME_DICT[db_freq]] # 数据库 cl = db[symbol] cl.ensure_index([('datetime', ASCENDING)], unique=True) # 添加索引 if generateExchange(symbol)=="QH": asset = 'X' else: asset = 'E' asset = 'INDEX' df = ts.bar(symbol, conn=ts.get_apis(), freq=freq, asset=asset, start_date=start_date, end_date=end_date, adj='qfq') #df = ts.get_hist_data(symbol,start=start_date, end=end_date, ktype=freq) df = df.sort_index() for ix, row in df.iterrows(): bar = generateVtBar(row) d = bar.__dict__ flt = {'datetime': bar.datetime} cl.replace_one(flt, d, True) end = time() cost = (end - start) * 1000 print u'合约%s 周期%s数据下载完成%s - %s,耗时%s毫秒' %(symbol, freq, df.index[0], df.index[-1], cost)
def stock_plot(): left, width = 0.1, 0.8 rect_vol = [left, 0.1, width, 0.3] rect_main = [left, 0.4, width, 0.5] fig = plt.figure() conn = ts.get_apis() df = ts.bar('300333', conn=conn, start_date='2018-01-01') del df['code'] df.reset_index(inplace=True) dates = df['datetime'].values df['datetime'] = df['datetime'].map(mdates.date2num) # dates=df['datetime'].values vol = df['vol'].values ax_vol = fig.add_axes(rect_vol) ax_vol.fill_between(dates, vol, color='y') # 横坐标时间旋转 plt.setp(ax_vol.get_xticklabels(), rotation=30, horizontalalignment='right') ax_main = fig.add_axes(rect_main) candlestick_ochl(ax_main, df.values, width=0.6, colordown='g', colorup='r') ax_main.axes.get_xaxis().set_visible(False) ax_main.set_title("STOCK LINE") ts.close_apis(conn) plt.show()
def __init__(self): self.engine = get_engine('db_stock') self.cb_code, self.name, self.yjl = self.bond() self.stocks = dict(zip(self.cb_code, self.name)) self.stocks_yjl = dict(zip(self.cb_code, self.yjl)) self.api = ts.get_apis() # python3 这个返回的不是list,需要手工转换 self.kzz_code_list = list(self.stocks.keys()) pool_code, pool_name = self.stock_pool() self.pool_dict = dict(zip(pool_code, pool_name)) self.pool_list = list(self.pool_dict.keys()) # 添加一部分持仓数据 或者 监测仓 self.df = pd.read_table(file, encoding='gbk', dtype={'证券代码': np.str}) del self.df['Unnamed: 15'] code_list = list(self.df['证券代码'].values) # 移除非法证券代码 中签 t = [ code_list.remove(i) for i in code_list.copy() if i.startswith('7') or i[:2] == '07' ] self.code_lists = code_list
def downMinuteBarBySymbol(symbol, freq): """下载某一合约的分钟线数据""" start = time() cl = db[symbol] cl.ensure_index([('datetime', ASCENDING)], unique=True) # 添加索引 df = ts.bar(symbol, ts.get_apis(), freq=freq, asset='X') #df = ts.bar(symbol, ktype='1min') df = df.sort_index() for ix, row in df.iterrows(): bar = generateVtBar(row) d = bar.__dict__ #有的合约字母变成大写,可以自己放开这个地方 #d['symbol'] = d['symbol'].lower() flt = {'datetime': bar.datetime} cl.replace_one(flt, d, True) end = time() cost = (end - start) * 1000 print u'合约%s数据下载完成%s - %s,耗时%s毫秒' % (symbol, df.index[0], df.index[-1], cost)
def plot_stock_line(code, name, table_name, start='2017-10-01', save=False): today = datetime.datetime.now().strftime('%Y-%m-%d') title = u'{} {} {} {}'.format(today, code, name, table_name) if os.path.exists(title + '.png'): return engine = get_engine('db_stock', local=True) fig = plt.figure(figsize=(10, 8)) base_info = pd.read_sql('tb_basic_info', engine, index_col='index') # fig,(ax,ax2)=plt.subplots(2,1,sharex=True,figsize=(16,10)) ax = fig.add_axes([0, 0.3, 1, 0.55]) ax2 = fig.add_axes([0, 0.1, 1, 0.25]) if code is None and name is not None: code = base_info[base_info['name'] == name]['code'].values[0] print code df = None for _ in range(4): api = ts.get_apis() try: df = ts.bar(code, conn=api, start_date=start) break except Exception, e: print e ts.close_apis(api) time.sleep(random.random() * 3) continue
def get_realtime_info(self, codes, has_sent, types, stock, yjl, percent): try: price_df = ts.quotes(codes, conn=self.api) except Exception as e: logger.error('获取可转债异常 >>>> {}'.format(e)) try: self.api = ts.get_apis() except Exception as e: logger.error('异常中存在异常{}'.format(e)) time.sleep(EXECEPTION_TIME) else: if len(price_df) != 0: price_df = price_df[price_df['cur_vol'] != 0] price_df['percent'] = (price_df['price'] - price_df['last_close']) / price_df[ 'last_close'] * 100 price_df['percent'] = price_df['percent'].map(lambda x: round(x, 2)) ret_dt = \ price_df[ (price_df['percent'] > percent) | (price_df['percent'] < -1 * percent)][ ['code', 'price', 'percent']] if len(ret_dt) > 0: # 提醒一次后,下一次的间隔为5分钟后 # sent_list = [] for i in ret_dt['code']: if has_sent[i] <= datetime.datetime.now(): name_list = [] yjl_list = [] name_list.append(stock[i]) yjl_list.append(yjl[i]) has_sent[i] = datetime.datetime.now() + datetime.timedelta(minutes=DELTA_TIME) ret_dt1 = ret_dt[ret_dt['code'] == i] ret_dt1['名称'] = name_list ret_dt1['溢价率'] = yjl_list name=ret_dt1['名称'].values[0] price=ret_dt1['price'].values[0] percent=ret_dt1['percent'].values[0] yjl_v = ret_dt1['溢价率'].values[0] now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') content0='{t}\n{name}:价格:{price} 涨幅:{percent},溢价率:{yjl}'.format(name=name,price=price,percent=percent,yjl=yjl_v,t=now) logger.info(content0) try: wechat.send_content(content0) except Exception as e: logger.info('发送微信失败') logger.info(e)
def update_daily(self): add_cols = u'ALTER TABLE `{}` ADD `{}` FLOAT;'.format( self.table_name, self.today) self._exe(add_cols) # self.conn.commit() api = ts.get_apis() cmd = 'SELECT * FROM `{}`'.format(self.table_name) cur = self._exe(cmd) for i in cur.fetchall(): (code, name, safe_price, count, profit_ratio, profit, values, current_price, earn) = i[:9] df = ts.quotes(code, conn=api) current_price = round(float(df['price'].values[0]), 2) values = current_price * count last_close = df['last_close'].values[0] earn = (current_price - last_close) * count profit = (current_price - safe_price) * count profit_ratio = round( float(current_price - safe_price) / safe_price * 100, 2) update_cmd = u'UPDATE {} SET `盈亏比例`={} ,`盈亏`={}, `市值` ={}, `现价` = {},`{}`={} where `证券代码`= {};'.format( self.table_name, profit_ratio, profit, values, current_price, self.today, earn, code) # print update_cmd self._exe(update_cmd) ts.close_apis(api)
def __init__(self): # 初始化日志 self.regularlog = logging.getLogger("dividend_maintenance") self.cmdconsole = logging.StreamHandler() self.cmdconsole.setLevel(logging.DEBUG) self.formatter = logging.Formatter( "%(asctime)s - %(name)s - %(levelname)s - %(message)s") self.cmdconsole.setFormatter(self.formatter) self.regularlog.addHandler(self.cmdconsole) self.tushare_dir = "C:\\quanttime\\data\\dividend\\tushare\\" self.errorCodeList = [] # jqdata context 暂时不用先注释 # auth('13811866763', "sam155") # mongodb client self.mongo_client = pymongo.MongoClient('mongodb://localhost:27017/') # tushare connect context token = "17e7755e254f02cc312b8b7e22ded9a308924147f8546fdfbe653ba1" ts.set_token(token) self.cons = ts.get_apis() # ts 授权 self.pro = ts.pro_api()
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 collect_index_china_full(): conn = ts.get_apis() collect_single_index_from_ts('000001', conn=conn) collect_single_index_from_ts('399001', conn=conn) collect_single_index_from_ts('000300', conn=conn) collect_single_index_from_ts('000905', conn=conn) ts.close_apis(conn)
def fetch(code_list): tsconn = ts.get_apis() sqlconn = db.connect() cursor = sqlconn.cursor() try: for code in code_list: try: data = ts.bar(code, conn=tsconn, start_date="1970-01-01", freq="5min") count = data.shape[0] i = 0 for index, row in data.iterrows(): cursor.execute( "insert into stock_price (code, time, price) values (%s, %s, %s)", (code, str(index), float(row["close"]))) i += 1 if i % 1000 == 0: print("{0} {1}%".format(code, int(i / count * 100))) sqlconn.commit() print("done") except IOError as err: print(code) print(err) finally: sqlconn.close() ts.close_apis(tsconn)
def __init__(self): self.cb_code, self.name, self.yjl = self.bond() self.stocks = dict(zip(self.cb_code, self.name)) self.stocks_yjl = dict(zip(self.cb_code, self.yjl)) self.api = ts.get_apis() # python3 这个返回的不是list,需要手工转换 self.code_list = list(self.stocks.keys())
def monitor(): engine = get_engine('db_zdt') table = '20180409zdt' api = ts.get_apis() df = pd.read_sql(table, engine, index_col='index') # print df price_list = [] percent_list = [] amplitude_list = [] start = datetime.datetime.now() for i in df[u'代码'].values: try: curr = ts.quotes(i, conn=api) last_close = curr['last_close'].values[0] curr_price = curr['price'].values[0] amplitude = round( ((curr['high'].values[0] - curr['low'].values[0]) * 1.00 / last_close) * 100, 2) # if last_close>=curr_price: # print i, # print df[df[u'代码']==i][u'名称'].values[0], # print percent except Exception, e: print e curr_price = 0 if last_close == 0: percent = np.nan percent = round((curr_price - last_close) * 100.00 / last_close, 2) percent_list.append(percent) price_list.append(curr_price) amplitude_list.append(amplitude)
def fetch_kdata_lazy(code, asset, freq, end_date=''): # global在第一行声明,养成好习惯 global cons start_date = get_date_max(code, asset, freq) + relativedelta(minutes=1) if isinstance(end_date, datetime.datetime): end_date_str = end_date.strftime(DT_FMT).replace( "11:30:00", "13:00:00") end_date = datetime.datetime.strptime(end_date_str, DT_FMT) kdata = ts.bar(code, conn=cons, asset=asset, adj='qfq', freq=freq, start_date=start_date, end_date=end_date) result = 0 try: result = insert_kdata(asset, freq, kdata) except pymongo.errors.BulkWriteError as err: logger.error(err) except StockError as err: logger.error(err) except TypeError as e: cons = ts.get_apis() logger.error(e) except Exception as e: logger.error(e) return result
def store_data(): code = '600050' conn = ts.get_apis() file_name = os.path.join(data_path, code + '.xls') df = ts.bar(code, conn=conn, start_date='2015-01-01') # print(df.head(10)) print(file_name) df.to_excel(file_name)
def monitor(): engine = get_engine('db_zdt') table = '20180409zdt' api = ts.get_apis() df = pd.read_sql(table, engine, index_col='index') price_list = [] percent_list = [] amplitude_list = [] start = datetime.datetime.now() for i in df['代码'].values: try: curr = ts.quotes(i, conn=api) last_close = curr['last_close'].values[0] curr_price = curr['price'].values[0] amplitude = round(((curr['high'].values[0] - curr['low'].values[0]) * 1.00 / last_close) * 100, 2) # if last_close>=curr_price: # print(i,) # print(df[df['代码']==i]['名称'].values[0],) # print( percent) except Exception as e: print('this point') print(e) api=ts.get_apis() curr_price = 0 if last_close == 0: percent = np.nan percent = round((curr_price - last_close) * 100.00 / last_close, 2) percent_list.append(percent) price_list.append(curr_price) amplitude_list.append(amplitude) df['今日价格'] = price_list df['今日涨幅'] = percent_list df['今日振幅'] = amplitude_list df['更新时间'] = datetime.datetime.now().strftime('%Y %m %d %H:%M%S') end = datetime.datetime.now() print('time use {}'.format(end - start)) df.to_sql(table + 'monitor', engine, if_exists='replace') ts.close_apis(api)
def __init__(self): self.engine = get_engine('db_stock') self.kzz_code, self.kzz_name, self.zg_code, self.name, self.yjl = self.zg_bond() self.kzz_stocks = dict(zip(self.kzz_code, self.kzz_name)) self.zg_stocks = dict(zip(self.zg_code, self.name)) self.kzz_stocks_yjl = dict(zip(self.kzz_code, self.yjl)) self.zg_stocks_yjl = dict(zip(self.zg_code, self.yjl)) self.api = ts.get_apis()
def get_price_diff(self, codes, has_sent_, types): # 针对可转债 try: df = ts.quotes(codes, conn=self.api) except Exception as e: logger.error('获取可转债异常 >>>> {}'.format(e)) try: self.api = ts.get_apis() except Exception as e: logger.error('异常中存在异常{}'.format(e)) time.sleep(EXECEPTION_TIME) else: df['bid1'] = df['bid1'].astype(float) df['ask1'] = df['ask1'].astype(float) df['diff'] = np.abs(df['bid1'] - df['ask1']) result = df[df['diff'] >= DIFF_V] if result.empty: # continue return else: for j in result['code']: if has_sent_[j] <= datetime.datetime.now(): has_sent_[j] = datetime.datetime.now()+ datetime.timedelta(minutes=DIFF_DELTA_TIME) name_list = [] yjl_list = [] name_list.append(self.kzz_stocks[j]) yjl_list.append(self.kzz_stocks_yjl[j]) ret_dt1 = result[result['code'] == j] ret_dt1['名称']=name_list ret_dt1['溢价率']=yjl_list # ret_dt1 = ret_dt1.set_index('code', drop=True) code=j name = ret_dt1['名称'].values[0] price = ret_dt1['price'].values[0] bid = ret_dt1['bid1'].values[0] ask = ret_dt1['ask1'].values[0] diff = round(ret_dt1['diff'].values[0],2) now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') content0 = '{t}\n{code}::{name}:价格:{price} 买1:{bid} 卖1:{ask}差价:{diff}'.format(code=code,name=name, price=price, bid=bid, ask=ask, diff=diff, t=now) logger.info(content0) try: wechat.send_content(content0) except Exception as e: logger.info('发送微信失败') logger.info(e)
def stock_line(): api = ts.get_apis() df = ts.bar('300141', conn=api, start_date='2018-01-01') # print(df) del df['code'] df.reset_index(inplace=True) print(df) df['datetime'] = df['datetime'].map(mdates.date2num) ax1 = plt.subplot2grid((6, 1), (0, 0), rowspan=5) ax2 = plt.subplot2grid((6, 1), (5, 0), rowspan=5, sharex=ax1) ax1.xaxis_date() candlestick_ochl(ax1, df.values, width=1, colorup='r', colordown='g') # ax2.fill_between(ax1,df['vol'].values,0) plt.show() ts.close_apis(api)
def ban_share(code,name): conn =ts.get_apis() year_2017 = [2629.218,3970.902,2083.032,1720.327,1999.456,1771.074,2417.082,2904.992,2910.946,2971.483,2350.122,3874.328] df = ts.bar(code, conn=conn, freq='M', start_date='2016-12-30', end_date='2017-11-01', asset='INDEX') series = df['close'] ''' diff_series=[] print series l = len(series) for i in range(l-1): print series[i] d= series[i]-series[i+1] diff_series.append(d) #print len(diff_series) ''' #s2=Series(series) s2=series[:len(series)-1] s3 = s2.sort_index(ascending=True) #print s3 s1 = Series(year_2017[0:len(s3)]) s3=s3.reset_index(drop=True) #print s3 #print s1 cor = s3.corr(s1) #print len(s3) #print len(s1) print cor plt.figure() plt.subplot(2,1,1) s1.plot() plt.subplot(2,1,2) s3.plot(title=name) plt.show() if abs(cor) >0.5: print 'Great factor: ',code
def api_close_case(): api = ts.get_apis() print('exit function') ts.close_apis(api) exit(0)
from matplotlib import rcParams import tushare as ts import matplotlib import os import talib import matplotlib.dates as mdates from matplotlib.finance import candlestick2_ochl,volume_overlay # from mpl_finance import candlestick_ochl # from mpl_finance import candlestick2_ochl matplotlib.style.use('ggplot') data_path = os.path.join(os.getcwd(), 'data') if not os.path.exists(data_path): os.mkdir(data_path) os.chdir(data_path) api = ts.get_apis() def plot_test1(): x = [1, 2] y = [2, 4] # plt.scatter(x,y) plt.scatter(x, y, color='red', marker='x') plt.axis([0, 10, 0, 10]) plt.show() def from_book(): fig = plt.figure() ax1 = fig.add_subplot(2, 2, 1)
# -*-coding=utf-8-*- # 获取 不同形态的k线 import random import time import tushare as ts import pandas as pd import os, datetime, math import numpy as np import logging from setting import get_engine, MYSQL_HOST, MYSQL_PORT, MYSQL_USER, MYSQL_PASSWORD, REDIS_HOST, get_mysql_conn import redis from threading import Thread import MySQLdb from collect_data import SaveData engine = get_engine('history') conn = ts.get_apis() MYSQL_DB = 'history' cursor = get_mysql_conn(MYSQL_DB).cursor() # pd.set_option('display.max_rows', None) class Kline(): def __init__(self): logging.info('tushare version: {}'.format(ts.__version__)) path = os.path.join(os.getcwd(), 'data') self.today_date = datetime.datetime.now().strftime('%Y-%m-%d') logging.info(self.today_date) if not os.path.exists(path): os.mkdir(path) os.chdir(path)
def plot_stock_line(api,code, name, table_name, current, start='2017-10-01', save=False): title = '{} {} {} {}'.format(current, code, name, table_name) title = title.replace('*', '_') if os.path.exists(title + '.png'): return if code is None and name is not None: code = base_info[base_info['name'] == name]['code'].values[0] df = None for _ in range(4): try: df = ts.bar(code, conn=api, start_date=start) except Exception as e: logger.info(e) ts.close_apis(api) time.sleep(random.random() * 30) api = ts.get_apis() else: break if df is None: return df = df.sort_index() if name is None: name = base_info[base_info['code'] == code]['name'].values[0] df = df.reset_index() df['datetime'] = df['datetime'].dt.strftime('%Y-%m-%d') sma5 = talib.SMA(df['close'].values, 5) sma20 = talib.SMA(df['close'].values, 10) # ax.set_xticks(range(0,len(df),20)) # # ax.set_xticklabels(df['date'][::5]) # ax.set_xticklabels(df['datetime'][::20]) fig = plt.figure(figsize=(10, 8)) # fig,(ax,ax2)=plt.subplots(2,1,sharex=True,figsize=(16,10)) ax = fig.add_axes([0, 0.3, 1, 0.50]) ax2 = fig.add_axes([0, 0.1, 1, 0.20]) candlestick2_ochl(ax, df['open'], df['close'], df['high'], df['low'], width=1, colorup='r', colordown='g', alpha=0.6) ax.grid(True) ax.set_title(title) ax.plot(sma5, label='MA5') ax.legend() ax.plot(sma20, label='MA20') ax.legend(loc=2) ax.grid(True) # df['vol'].plot(kind='bar') volume_overlay(ax2, df['open'], df['close'], df['vol'], width=1, alpha=0.8, colordown='g', colorup='r') ax2.set_xticks(range(0, len(df), 5)) # ax.set_xticklabels(df['date'][::5]) ax2.set_xticklabels(df['datetime'][::5]) plt.setp(ax2.get_xticklabels(), rotation=30, horizontalalignment='right') ax2.grid(True) plt.subplots_adjust(hspace=0.3) if save: # path = os.path.join(os.path.dirname(__file__),'data',today) fig.savefig(title + '.png') else: plt.show() plt.close()
import tushare as ts #中证500 #dt = ts.get_zz500s() #print(dt); #上证50 #print(ts.get_sz50s()); #沪深300 #print(ts.get_hs300s()); #获取连接备用 cons = ts.get_apis() df = ts.bar('000300', conn=cons, asset='INDEX', start_date='', end_date='') df = df.reset_index(); df['value'] = df['open']/2000; print(df.head(5)); print(df.dtypes); df1 = ts.bar('000016', conn=cons, asset='INDEX', start_date='', end_date='') df1 = df1.reset_index(); df1['value'] = df1['open']/2000; df1['odc'] = df1['open']>df1['close']; df = pd.concat([df,df1]); ''' df1 = ts.bar('000905', conn=cons, asset='INDEX', start_date='2016-01-01', end_date='') df1 = df1.reset_index();
def __init__(self): self.cons = ts.get_apis() self.engine = create_engine('mysql+pymysql://root:password@localhost:3306/stock?charset=utf8') self.all_info = ts.get_stock_basics() self.all_codes = self.all_info.index
from src.models.Company import Trend, Fundamental from src.const import BF file_path = "%s%s%s" % (BASE_DIR, BF['dir']['uploads_dir'], BF['filename']['fundamental']) if not os.path.exists(file_path): fun_df = Fundamental.set_frame(file_path) else: fun_df = pandas.read_csv(file_path, encoding='gbk') fun_df.code = fun_df.code.apply(lambda x: str(x).rjust(6, '0')) con = tushare.get_apis() code_list = ['600703', '300545', '000014', '300696', '300116', '300094', '002340', '000861', '601901', '601200', '600079'] for _code in code_list: _fun_df = fun_df.loc[fun_df['code'] == _code].reset_index(drop=True) if _fun_df.index.size == 0: print(_code) continue name = _fun_df.at[0, 'name'] name = "%s%s%s" % (BASE_DIR, BF['dir']['company_dir'], name) Trend.set_frame(con, _code, name) com_df = pandas.DataFrame(Trend.company_list) com_df.to_csv("company.csv", index=False)