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 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 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 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 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 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 monitor(self): self.has_sent_kzz = dict(zip(self.kzz_code, [datetime.datetime.now()] * len(self.kzz_code))) self.has_sent_diff = dict(zip(self.kzz_code, [datetime.datetime.now()] * len(self.kzz_code))) self.has_sent_zg = dict(zip(self.zg_code, [datetime.datetime.now()] * len(self.zg_code))) while 1: current = trading_time() # current=0 if current == MARKET_OPENING: self.get_realtime_info(self.kzz_code, self.has_sent_kzz, '转债', self.kzz_stocks, self.kzz_stocks_yjl, ZZ_ALERT_PERCENT) self.get_realtime_info(self.zg_code, self.has_sent_zg, '正股', self.zg_stocks, self.zg_stocks_yjl, ZG_ALERT_PERCENT) self.get_price_diff(self.kzz_code, self.has_sent_diff, '差价') time.sleep(LOOP_TIME) elif current == -1: time.sleep(LOOP_TIME) elif current == 1: try: ts.close_apis(self.api) except Exception as e: logger.info('fail to stop monitor {}'.format(datetime.datetime.now())) logger.info(e) exit(0)
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 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 isXR(code=""): global cons shi_jian = gsd.get_code_min_shi_jian(code) if shi_jian == "None": return False data = gsd.get_stock_data_daily_df_time(code=code, start=shi_jian, end=shi_jian) price = data["price"].values[0] tsFaile = 1 while tsFaile != 0: try: # print(code,shi_jian) # if tsFaile >=2 : # cons = ts.get_apis() data = ts.bar(code, conn=cons, adj='qfq', start_date=shi_jian, end_date=shi_jian, retry_count=2) # print("data",data) ''' if len(data["close"].values)<1 : tsFaile += 1 else: tsFaile = 0 ''' tsFaile = 0 if len(data["close"].values) < 1: return False except Exception as e: #获取连接备用 ts.close_apis(conn=cons) cons = ts.get_apis() print(code, shi_jian, "isXR fail:", tsFaile, e) time.sleep(5) if tsFaile <= 3: tsFaile += 1 else: return False # print(code,shi_jian) # data = ts.bar(code, conn=cons, adj='qfq', start_date=shi_jian, end_date=shi_jian,retry_count = 20) # print("data",data) close = data["close"].values[0] if price == close: return False else: return True
def collect_hs300_full(): df = ts.get_hs300s() conn = ts.get_apis() #now = datetime.now().strftime('%Y-%m-%d') for code in df['code'].values: collect_single(code=code, conn=conn) ts.close_apis(conn)
def main(): if ts.is_holiday(today): ts.close_apis(cons) exit() else: weekly_drop_rank(current=True) # 正常运行时current = True ts.close_apis(cons)
def toDB_get_periodata(period): start = datetime.datetime.now() print(start) global CONS CONS = ts.get_apis() if period == 'W': freq = 'W' table = 't_weekdata' command = "ts.bar(row['code'], conn=CONS, freq=freq, start_date=key, end_date=value,adj = 'qfq',factors=['tor']).to_sql(table, c.ENGINE, if_exists='append')" if period == 'M': freq = 'M' table = 't_monthdata' command = "ts.bar(row['code'], conn=CONS, freq=freq, start_date=key, end_date=value, adj = 'qfq',factors=['tor']).to_sql(table, c.ENGINE, if_exists='append')" try: if (period == 'D'): sql = "delete from t_daydata where datetime ='" + c.DATE + "'" _excutesql(sql) def tmp(con, sql): if (con == 'CONN_SH'): sql = "select code from T_StockBasics where code like '6%%' order by code" if (con == 'CONN_SZ'): sql = "select code from T_StockBasics where code like '0%%' order by code" if (con == 'CONN'): sql = "select code from T_StockBasics where code like '3%%' order by code" com = "ts.bar(row['code'], conn=CONS, freq='D', start_date=c.DATE, end_date=c.DATE,adj = 'qfq',factors=['tor']).to_sql('t_daydata', c.ENGINE, if_exists='append')" for row in _excutesql(sql): try: print(row) eval(com) except Exception as e: print('error') continue try: t_sh = threading.Thread(target=tmp, args=("CONN_SH", "sql")) t_sz = threading.Thread(target=tmp, args=("CONN_SZ", "sql")) t_cyb = threading.Thread(target=tmp, args=("CONN", "sql")) for t in [t_sh, t_sz, t_cyb]: t.start() for t in [t_sh, t_sz, t_cyb]: t.join() except Exception as e: print(e) else: for row in _excutesql(c.SQL_CODE): for key, value in c.PERIOD.items(): try: print(row) eval(command) except Exception as e: print(e) continue finally: ts.close_apis(CONS) end = datetime.datetime.now() print("toDB_get_periodata: " + str(end - start))
def get_selected_stock(): with open('../codes.txt') as f: js_data = json.load(f) code_list = js_data.get('example1') for code in code_list: print(code,code_list[code]) plot_stock_line(code,'2018-01-01') ts.close_apis(api)
def main(): # 获取连接备用 cons = ts.get_apis() codes = getCode() # print(codes) getMinData(cons, codes) # 关闭连接 ts.close_apis(cons)
def get_data(self, symbols=None): if symbols is None: stocks = self._get_data() pool = ThreadPool(600) pool.map(self.get_m_data, stocks) if self.cons is not None: for con in self.cons: ts.close_apis(con) return self.data
def monitor(self): while 1: current = trading_time() if current == MARKET_OPENING: try: price_df = ts.quotes(self.code_list, conn=self.api) except Exception as e: logger.info(e) self.api = ts.get_apis() time.sleep(EXECEPTION_TIME) else: 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'] > 2) | (price_df['percent'] < -2)][ ['code', 'price', 'percent']] if len(ret_dt) > 0: name_list = [] yjl_list = [] # 只会提醒一次,下次就不会再出来了 for i in ret_dt['code']: name_list.append(self.stocks[i]) yjl_list.append(self.stocks_yjl[i]) self.code_list.remove(i) ret_dt['name'] = name_list ret_dt[u'溢价率'] = yjl_list ret_dt = ret_dt.sort_values(by='percent', ascending=False) ret_dt = ret_dt.reset_index(drop=True) try: wechat.send_content(ret_dt.to_string()) # sendmail(ret_dt.to_string(), '波动的可转债') # logger.info("Send mail successfully at {}".format(datetime.datetime.now())) except Exception as e: logger.info('sending wechat failed') logger.info(e) time.sleep(LOOP__TIME) elif current == -1: time.sleep(LOOP__TIME) elif current == 1: try: ts.close_apis(self.api) except Exception as e: logger.info('fail to stop monitor {}'.format(datetime.datetime.now())) logger.info(e) exit(0)
def get_selected_stock(): with open('../codes.txt') as f: js_data = json.load(f) code_list = js_data.get('example1') for code in code_list: print(code, code_list[code]) plot_stock_line(code, '2018-01-01') ts.close_apis(api)
def get_hs300_last_date_online(freq='d'): conn = ts.get_apis() benchmark = ts.bar('000300', conn=conn, asset='INDEX', freq=freq) ts.close_apis(conn) if benchmark is None: return None, None last_date0 = benchmark.index[0].strftime('%Y-%m-%d') last_date1 = benchmark.index[1].strftime('%Y-%m-%d') return last_date0, last_date1
def get_trading_days(cls, start=None, end=None, ndays=None, ascending=True): """ 取得交易日列表,分三种方式取得 (1)指定开始、结束日期,即start和end不为None,此时忽略参数ndays (2)指定开始日期和天数,即start和ndays不为None,而end为None (3)指定结束日期和天数,即end和ndays为None,而start为None -------- :param start: datetime-like or str 开始日期,格式:YYYY-MM-DD :param end: datetime-like or str 结束日期,格式:YYYY-MM-DD :param ndays: int 交易日天数 :param ascending: bool,默认True 是否升序排列 :return: -------- Series of pandas.Timestamp,交易日列表,默认按交易日升序排列 """ if len(Utils.utils_trading_days) == 0: ts_conn = ts.get_apis() df_SZZS = ts.bar(code='000001', conn=ts_conn, asset='INDEX') ts.close_apis(ts_conn) Utils.utils_trading_days = Series(df_SZZS.index).sort_values() if start is not None: start = cls.to_date(start) if end is not None: end = cls.to_date(end) if start is not None and end is not None: trading_days = Utils.utils_trading_days[ (Utils.utils_trading_days >= start) & (Utils.utils_trading_days <= end)] elif start is not None and ndays is not None: trading_days = Utils.utils_trading_days[ Utils.utils_trading_days >= start].iloc[:ndays] elif end is not None and ndays is not None: trading_days = Utils.utils_trading_days[ Utils.utils_trading_days <= end].iloc[-ndays:] elif start is not None: trading_days = Utils.utils_trading_days[ Utils.utils_trading_days >= start] elif end is not None: trading_days = Utils.utils_trading_days[ Utils.utils_trading_days <= end] elif ndays is not None: trading_days = Utils.utils_trading_days.iloc[-ndays:] else: trading_days = Utils.utils_trading_days trading_days = trading_days.reset_index(drop=True) if not ascending: trading_days = trading_days.sort_values(ascending=False) return trading_days
def down(tickers=None, asset='E', freq='d', start_date='2015-01-01', end_date=None): if tickers is None: df_basics = ts.get_stock_basics() tickers = df_basics.index.sort_values() df_sql = pd.read_sql(sql_start_date.format(freq), engine, index_col=['code'], parse_dates=['start_date']) df_start_date = pd.DataFrame(index=tickers).join(df_sql) df_start_date = df_start_date.fillna(pd.to_datetime(start_date, format='%Y-%m-%d')).reset_index() conn = ts.get_apis() df_start_date.apply(lambda row:call_back(row, conn, asset, end_date, freq), axis=1) ts.close_apis(conn)
def main(): # 获取连接备用 cons = ts.get_apis() codes = getCode() # print(codes) # 获取结束时间 systemTime = str(time.strftime('%Y%m%d', time.localtime(time.time()))) getMinDataInc(cons, codes, systemTime) # 关闭连接 ts.close_apis(cons)
def collect_hs300_daily(): now = datetime.now().strftime('%Y-%m-%d') is_holiday = ts.is_holiday(now) # 如果是假日, 跳过 if is_holiday: return conn = ts.get_apis() df = ts.get_hs300s() for code in df['code'].values: collect_single_daily(code=code, conn=conn) ts.close_apis(conn)
def main(): # current='20191016' current = datetime.datetime.now().strftime('%Y%m%d') app = PlotYesterdayZT() api = ts.get_apis() for plot_type in ['zrzt', 'zdt']: try: app.plot_yesterday_zt(api, plot_type, current=current) except Exception as e: notify(title='zdt_plot 出错', desp=f'{__name__}') continue ts.close_apis(conn=api)
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 collect_index_china_daily(): now = datetime.now().strftime('%Y-%m-%d') is_holiday = ts.is_holiday(now) # 如果是假日, 跳过 if is_holiday: return conn = ts.get_apis() collect_single_index_daily_from_ts('000001', conn=conn) collect_single_index_daily_from_ts('399001', conn=conn) collect_single_index_daily_from_ts('000300', conn=conn) collect_single_index_daily_from_ts('000905', conn=conn) ts.close_apis(conn)
def monitor(self, total_market=True): ''' total_market 默认监控全市场 total_market = True ''' if total_market: (kzz_stocks, zg_stocks, kzz_yjl, zg_yjl) = self.all_bond_market() else: (kzz_stocks, zg_stocks, kzz_yjl, zg_yjl) = self.get_current_position() zg_code = list(zg_stocks.keys()) kzz_code = list(kzz_stocks.keys()) self.has_sent_kzz = dict( zip(kzz_code, [datetime.datetime.now()] * len(kzz_code))) self.has_sent_diff = dict( zip(kzz_code, [datetime.datetime.now()] * len(kzz_code))) self.has_sent_zg = dict( zip(zg_code, [datetime.datetime.now()] * len(zg_code))) while 1: current = trading_time() if current == MARKET_OPENING: self.get_realtime_info(kzz_code, self.has_sent_kzz, '转债', kzz_stocks, kzz_yjl, ZZ_ALERT_PERCENT) self.get_realtime_info(zg_code, self.has_sent_zg, '正股', zg_stocks, zg_yjl, ZG_ALERT_PERCENT) self.get_price_diff(codes=kzz_code, has_sent_=self.has_sent_diff, types='差价', kzz_stocks=kzz_stocks, kzz_stocks_yjl=kzz_yjl) time.sleep(LOOP_TIME) elif current == -1: time.sleep(LOOP_TIME) elif current == 1: try: ts.close_apis(self.api) except Exception as e: logger.info('fail to stop monitor {}'.format( datetime.datetime.now())) logger.info(e) exit(0)
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 main(current=None): # current='20191016' if current is None: current = datetime.datetime.now().strftime('%Y%m%d') if isinstance(current, int): current = str(current) app = PlotYesterdayZT() api = ts.get_apis() for plot_type in ['zrzt', 'zdt']: try: app.plot_yesterday_zt(api, plot_type, current=current) except Exception as e: print(plot_type, 'error') print(e) notify(title='zdt_plot 出错', desp=f'{__name__}') continue ts.close_apis(conn=api)
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 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_stock_by_code(code, start, end): """ 根据股票代码获取该股票历史行情 """ con = ts.get_apis() try: res = ts.bar(code, conn=con, freq='D', start_date=start, end_date=end, ma=[5, 10, 20, 60], factors=['vr', 'tor'], retry_count=2) return res except Exception as e: print('抓取失败个股行情', e) return None finally: ts.close_apis(con)
def get_index_by_code(code, start, end): """ 获取指数历史行情 """ con = ts.get_apis() try: res = ts.bar(code, conn=con, freq='D', start_date=start, end_date=end, asset='INDEX', ma=[5, 10, 20, 60], factors=['vr', 'tor'], retry_count=2) return res except Exception as e: print('抓取指数行情失败', e) return None finally: ts.close_apis(con)
def downMinuteBarBySymbol(symbol): """下载某一合约的分钟线数据""" start = time() cl = db[symbol] cl.ensure_index([('datetime', ASCENDING)], unique=True) # 添加索引 cons = ts.get_apis() df = ts.bar(symbol, conn=cons, freq='1min') #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 ts.close_apis(cons) print(u'合约%s数据下载完成%s - %s,耗时%s毫秒' % (symbol, df.index[0], df.index[-1], cost))
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()
for i in range(len(df)): code = df.iloc[i]['代码'] name = df.iloc[i]['名称'] plot_stock_line(api,code, name, table_name=table_name, current=current, start='2018-07-01', save=True) if __name__ == '__main__': if is_holiday(): logger.info('Holiday') exit() logger.info("Start") api = ts.get_apis() current = datetime.datetime.now().strftime('%Y%m%d') data_path = DATA_PATH path = os.path.join(DATA_PATH, current) if not os.path.exists(path): os.mkdir(path) os.chdir(path) for plot_type in ['zrzt', 'zdt']: try: plot_yesterday_zt(plot_type, current=current) except Exception as e: continue ts.close_apis(api)
def api_close_case(): api = ts.get_apis() print('exit function') ts.close_apis(api) exit(0)
def closed(self): ts.close_apis(self.conn)