def general_data(start_date, end_date, asset, freq, ts_code): df = ts.pro_bar(pro_api=api, ts_code=ts_code, asset=asset, freq='D', start_date=start_date, end_date=end_date) if freq == 'W': df['index_date'] = pd.to_datetime(df['trade_date']) df = df.set_index(['index_date']) rdf = df.resample('W').agg({ 'close': lambda r: r.tail(1), 'open': lambda r: r.head(1), 'high': 'max', 'low': 'min', 'trade_date': lambda r: r.tail(1), }) rdf = rdf.dropna(axis=0) rdf = rdf.reset_index(drop=False) # rdf['trade_date'] = rdf['trade_date'].apply(lambda x: datetime.strftime(x,"%Y-%m-%d")) return rdf else: if df is None: raise Exception('date from %s -> %s is null' % (start_date, end_date)) return df[::][::-1]
def tu_hist_data(folder, codes, start=None, end=None, freq='D', sample=0, persist=True): os.makedirs(folder, exist_ok=True) hist_data = {} codes_selected = codes if sample == 0 else random.sample(codes, sample) for code in codes_selected: path = os.path.join(folder, '%s.csv' % code) if not (persist and os.path.isfile(path)): try: df = ts.pro_bar(ts_code=code, asset='E', adj='qfq', start_date=start.strftime('%Y%m%d'), end_date=end.strftime('%Y%m%d'), freq=freq) hist_data[code] = df if persist: df.to_csv(path) print('retrieved hist data for %s' % code) except Exception as ex: try: print('error occurred in retrieving {}: {}'.format( code, ex)) except Exception as innerex: print('exception: {}'.format(innerex)) # return pd.Panel(hist_data) return True
def imp_get_data_from_tushare(task: Tuple) -> Optional[pd.DataFrame]: if task is None: return None ts.set_token(config.tushare_token) func: Dict = { db_config().tbl_finance_indicator_statement: ts.pro_api().fina_indicator, db_config().tbl_income_statement: ts.pro_api().income, db_config().tbl_balance_sheet: ts.pro_api().balancesheet, db_config().tbl_cash_flow_statement: ts.pro_api().cashflow, db_config().tbl_daily_basic: ts.pro_api().daily_basic } tbl_name = task[0] if tbl_name in func: return func[tbl_name](ts_code=task[1], start_date=task[2], end_date=task[3]) elif tbl_name == db_config().tbl_daily_trading_data: return ts.pro_bar(ts_code=task[1], start_date=task[2], end_date=task[3], adj='qfq') elif tbl_name == db_config().tbl_name_history: return ts.pro_api().namechange(ts_code=task[1]) else: return None
def _get_daily_k_data_from_ts(self, code, update=True): start_date = DATA_BEGIN_DATE if update is True: start_date = self.db_date count = 0 while (True): try: df_k_data = ts.pro_bar(ts_code=code, adj='qfq', start_date=start_date, end_date=self.date_now) if df_k_data is not None: break else: LOG.info('(%s)调用tushare ts.pro_bar失败,空数据' % (code)) break except: count += 1 LOG.info('(%s)调用tushare ts.pro_bar失败,重试次数:%s' % (code, count)) if count > self.count_max_retry: break sleep(self.second_sleep) if df_k_data is None: df_k_data = pd.DataFrame() df_k_data.fillna(0.0, inplace=True) return df_k_data
def get_adj_price(param): """ 获取复权行情 Parameters ------ Dict ts_code: str ts股票代码 start_date: str 开始日期 (格式:YYYYMMDD) end_date: str 结束日期 (格式:YYYYMMDD) adj: 复权类型(只针对股票):None未复权 qfq前复权 hfq后复权 , 默认None freq: 数据频度 :1MIN表示1分钟(1/5/15/30/60分钟) D日线 ,默认D ma: list 均线,支持任意周期的均价和均量,输入任意合理int数值 Return ------- DataFrame 股票列表(DataFrame): ts_code ts股票代码 symbol 市场代码 name 名称 area 上市地区 industry 行业 list_date 上市日期 """ ts.set_token(ct.conf('TOKEN')) ts.pro_api() df = ts.pro_bar(ts_code=param['ts_code'], adj=param['adj'], start_date=param['start_date'], asset=param['asset'], freq=param['freq'], end_date=param['end_date']) return df
def ts_get_oms_price(self, ts_code, start_date, end_date): # get tick data from tushare # 一次最多获取7000条数据 # 假设从20140112-20130111,从20140112开始往后获取7000条数据,最多到20131129,中间跨度最多30个交易日 # 所以我们保守可以一次下载15天,最多不超过30天 ''' :param ts_code: symbol 000000.SZ :param start_date: YYYYMMDD :param end_date: YYYYMMDD :return: df ''' if self.check_token() == False: return None end_date = tmd.date_add_days(end_date, 1) try: df1 = ts.pro_bar(ts_code=ts_code, start_date=start_date, end_date=end_date, asset='E', freq='1min') except Exception as msg: print(msg) return None return df1
def kxian4_fun(): from pyecharts import Kline api = ts.pro_api( 'acfe91c33583e4f34c757cfc5360f7e51eefd9f0c259f4204ea4720f') # 取000001的前复权行情 df = ts.pro_bar(pro_api=api, ts_code='000001.SZ', adj='qfq', start_date='20181201', end_date='20181231') df = df.sort_index(axis=0) df1 = df[['open', 'high', 'open', 'close']] v1 = df1.values.tolist() kline = Kline("K 线图示例") aa = [i for i in df1.index.format()] kline.add( "日K", aa, v1, mark_point=["max"], is_datazoom_show=True, datazoom_orient="vertical", ) return kline
def download_hist_data(stock_symbol, start_date, end_date, freq='D', asset='E'): ''' 将每次的收盘历史数据按照10年分隔从tushare接口获取 再按照时间先后顺序拼接 ''' if start_date is not None: split_cal_list = split_trade_cal(start_date, end_date) print(split_cal_list) df_list = [] for trade_cal in split_cal_list: # 增加指数数据 print(stock_symbol) print(asset) print(freq) print(trade_cal[0].strftime('%Y%m%d')) print(trade_cal[1].strftime('%Y%m%d')) df = ts.pro_bar(ts_code=stock_symbol, asset=asset, freq=freq, start_date=trade_cal[0].strftime('%Y%m%d'), end_date=trade_cal[1].strftime('%Y%m%d')) # df = df.iloc[::-1] # 将数据按照时间顺序排列 df_list.append(df) return pd.concat(df_list)
def cal_net_value(stock_pool, start_date, end_date, adj="qfq"): # 计算组合的净值数据 # 输入股票代码列表,起止日期 # 输出考察区间内的净值表格 col_list = list() col_list.append("trade_date") df_net = pd.DataFrame(columns=col_list) for stock_item in stock_pool: s_code = six_code_to_ts_code(stock_item) df_item = ts.pro_bar(ts_code=s_code, adj=adj, start_date=start_date, end_date=end_date) df_item["close"] = round( df_item["close"] / df_item["close"].tolist()[-1] * 0.2, 6) df_net = pd.merge(df_net, df_item[["trade_date", "close"]], how="outer", on="trade_date") col_list.append(s_code.split(".")[0]) # 根据日期排序 df_net.sort_values(by="trade_date", inplace=True) # 根据前值替换NaN值 df_net.fillna(method='ffill', inplace=True) df_net.reset_index(inplace=True, drop=True) df_net.columns = col_list temp = df_net[col_list[1:]] df_net["net_value"] = temp.sum(axis=1) print(print_info(), end=" ") print("The dataframe tail is:\n {}".format(df_net.tail())) return df_net
def get_stock_price_df(stock_code: str, start_date: str, end_date: str, freq='M') -> pd.DataFrame: price_df = ts.pro_bar(ts_code=stock_code, adj='qfq', start_date=start_date, end_date=end_date, freq=freq) # price_df = pro.price_df(ts_code='600958.SH', start_date='20200101', end_date='20210130') price_df.info() # 其中的open、high、low、close、和vol几列信息是我们需要的,其余的数据列都可以删除掉, # 另外,这些数据的Index并不是时间序列,交易日期是以字符串的形式存储在trade_date列中的, # 需要将日期作为DataFrame的Index,并把它们转化为pandas.Timestamp格式。 price_df.index = price_df.trade_date price_df = price_df.rename(index=pd.Timestamp) price_df.drop(columns=[ 'ts_code', 'trade_date', 'pre_close', 'change', 'pct_chg', 'amount' ], inplace=True) price_df.columns = ['close', 'open', 'high', 'low', 'volume'] price_df.sort_index(inplace=True) price_df.info() return price_df
def downloadMinutesToCsv(startDate, endDate, filePath): listStartCanlender = [] listEndCanlender = [] startTime = convertDate(startDate) endTime = convertDate(endDate) startTime = startTime + ' 09:30:00' endTime = endTime + ' 15:00:00' startDate, endDate, listTradeCalendar = getTradeCalendarFromWeb( startDate, endDate) listAllStocks = getAllStocks(startDate) for i in range(len(listAllStocks)): #轮询所有股票 fileName = filePath + listAllStocks[i] + '.csv' if True == os.path.isfile(fileName): os.remove(name) df = ts.pro_bar(ts_code=listAllStocks[i], freq='1min', start_date=startTime, end_date=endTime) df = df.sort_index(ascending=False) df.to_csv( fileName, mode='a', header=True, columns=['ts_code', 'trade_time', 'open', 'close', 'high', 'low'])
def save_stock_data(stock, start_date, end_date): # does not take care of directory creating pro = ts.pro_api() daily = pro.daily(ts_code=stock, start_date=start_date, end_date=end_date) daily.to_json(f'data/{stock}/daily.json') ex_date = pro.dividend(ts_code=stock, fields='ex_date, stk_div') ex_date.to_json(f'data/{stock}/ex_date.json') qfq_daily = ts.pro_bar(stock, adj='qfq', start_date=start_date, end_date=end_date) qfq_daily.to_json(f'data/{stock}/qfq_daily.json') daily_basic = pro.daily_basic( ts_code=stock, start_date=start_date, end_date=end_date, fields='ts_code,trade_date,close,turnover_rate_f,volume_ratio') daily_basic.to_json(f'data/{stock}/daily_basic.json') moneyflow = pro.moneyflow(ts_code=stock, start_date=start_date, end_date=end_date) moneyflow.to_json(f'data/{stock}/moneyflow.json')
def AStockHisDataPost(self, stockCodes, start_date, end_date, step=0): print(self.getCurrentTime(), ": Download A Stock Starting:") for row in stockCodes.itertuples(index=True, name='Pandas'): stockCode = getattr(row, "ts_code") startDate = getattr(row, "list_date") try: starttime = time.time() df = ts.pro_bar(ts_code=stockCode, adj='hfq', start_date=startDate, end_date=end_date) endtime = time.time() print("Get Data from Pro daily spend time = ", endtime - starttime) try: starttime = time.time() # df.to_sql('STOCKDAILYDATA',self.engine,if_exists='append'); endtime = time.time() print("Write data to mysql spend time = ", endtime - starttime) filename = 'C:\\E\\quant\\winddata\\history\\csv\\post\\' + stockCode + '.csv' starttime = time.time() df.to_csv(filename, sep=',', header=True, index=True) endtime = time.time() print("Write data to csv spend time = ", endtime - starttime) # filename = 'C:\\E\\quant\\winddata\\history\\json\\'+stockCode+'.json' # starttime = time.time() # df.to_json(filename) # endtime = time.time() # print("Write data to json spend time = ",endtime-starttime) except Exception as e: #如果写入数据库失败,写入日志表,便于后续分析处理 error_log_dic = {} error_log_dic['start_date'] = start_date error_log_dic['end_date'] = end_date error_log_dic['symbol'] = stockCode error_log = pd.DataFrame(error_log_dic, index=[0]) # error_log.to_sql('stockerrorlog',self.engine,if_exists='append') print(self.getCurrentTime(), ": SQL Exception :%s" % (e)) continue except Exception as e: #如果读取处理失败,可能是网络中断、频繁访问被限、历史数据缺失等原因。写入相关信息到日志表,便于后续补充处理 error_log_dic = {} error_log_dic['start_date'] = start_date error_log_dic['end_date'] = end_date error_log_dic['symbol'] = stockCode error_log = pd.DataFrame(error_log_dic, index=[0]) # error_log.to_sql('stockerrorlog',self.engine,if_exists='append') print( self.getCurrentTime(), ":get data from tushare %s : Exception :%s" % (stockCode, e)) time.sleep(self.sleepTime) continue print(self.getCurrentTime(), ": Downloading [", stockCode, "] From " + startDate + " to " + end_date) print(self.getCurrentTime(), ": Download A Stock Has Finished .")
def pro_get_k_data_daily(self, code, index, start, end): df, cached_start, cached_end = self.get_k_data_daily_cached( code, index, start, end) if df is None: begin_date = START_DATE if cached_end is None else cached_end #print('load from internet') #print(cached_end) #print(begin_date) if index: df = self.pro.index_daily( ts_code=code, start_date=format_date_ts_pro(begin_date)) else: df = ts.pro_bar(pro_api=self.pro, ts_code=code, adj='qfq', start_date=format_date_ts_pro(begin_date)) if df is None: return None if df is not None: self.pro_cache_data_daily(df, index, cached_end) if df.shape[0] == 0: return df else: df = df[(df.trade_date >= format_date_ts_pro(start)) & (df.trade_date <= format_date_ts_pro(end))] return pro_opt_stock_k(df)
def saveTotalShareBelow5ToEachFile(_freq): print('开始保存所有股票的前复权数据......') cnt = 0 if _freq == 'D': dir_path = 'C:/python/csv/' elif _freq == 'W': dir_path = 'C:/python/csv_wk/' elif _freq == 'M': dir_path = 'C:/python/csv_mon/' with open('C:/python/csv/oneDayAllStock.csv', 'r', encoding='utf-8') as f: reader = csv.DictReader(f) for row in reader: df = ts.pro_bar(ts_code=row['ts_code'], adj='qfq', freq = _freq, start_date=downloadStartDate, end_date=downloadEndDate) df = df.sort_values(by='trade_date' , ascending=True) df.to_csv(dir_path +row['ts_code'][0:6]+'.csv', columns=['trade_date','open', 'high','low','close']) cnt =cnt +1 print('完成保存所有股票的前复权数据 '+dir_path, row['ts_code'][0:6], cnt) time.sleep(1) #周线和月线1秒只能下载2次 ## if (float(row['total_share']) < 500000000000): #and re.match('300', row['ts_code']): ## try: ## df = ts.get_hist_data(row['ts_code'][:6], start=dwnldStartDate, end=dwnldEndDate) ## df = df.sort_index(ascending=True) ## df.to_csv('C:/python/csv/'+ row['ts_code'][:6] + '.csv', columns=['open','high','low','close','volume','p_change','ma5','ma20']) ## except Exception as e: ## print(row['ts_code'],end=':') ## print(e) print('完成保存所有股票的前复权数据 '+dir_path)
def get_data(code, start='20190101', end='20190712'): df = ts.pro_bar(ts_code=code, asset='I', adj='qfq', start_date=start, end_date=end) return df
def get_data(code, endtime, type=''): df = ts.pro_bar(ts_code=code, adj='qfq', start_date='20150101', end_date=endtime).sort_values(by="trade_date", ascending=True) high = df['high'].tolist() low = df['low'].tolist() price = df['close'].tolist() amount = df['amount'].tolist() vol = df['vol'].tolist() dateList = df['trade_date'].tolist() if df['trade_date'][0] != endtime: url = 'http://d.10jqka.com.cn/v6/line/hs_{}/01/today.js'.format( code[:6]) headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36', 'Referer': 'http://m.10jqka.com.cn/stockpage/hs_{}/'.format(code[:6]) } r = requests.get(url, headers=headers) try: js = re.findall(':({[\S\s]+})}', r.text)[0] js = json.loads(js) high.append(js['8']) low.append(js['9']) price.append(js['11']) amount.append(float(js['19']) / 1000) vol.append(float(js['13']) / 100) dateList.append(endtime) except: print(r.text) return high, low, price, amount, vol, dateList
def trade_day(stock_code, asset='E'): start_day = datetime.datetime.strptime(str( datetime.date.today()), '%Y-%m-%d') + datetime.timedelta(days=-200) start_date = start_day.date().strftime('%Y%m%d') day_df = ts.pro_bar(ts_code=stock_code, start_date=start_date, asset=asset, freq='D', adj='qfq') day_df.sort_values(by='trade_date', inplace=True) my_BOLL(day_df) my_MA(day_df) my_MA(day_df, item='vol', timeperiod=(10, )) my_swing(day_df) my_MACD(day_df) my_KDJ(day_df) # day_df.to_csv('kdj.csv') price_res = price_section(day_df, period='day') trend_res = trend(day_df) probab_res = probability(day_df) deviation_res = deviation(day_df) if trend_res.get('ma_trend') == '下跌': return False return trade_period_return(deviation_res, price_res, probab_res, period='day')
def stock_tushare(id, start_time, end_time): ts.set_token('你的token,没有的去https://tushare.pro/register?reg=92 申请') pro = ts.pro_api() data1 = ts.pro_bar(ts_code=id, adj='qfq', start_date=start_time, end_date=end_time) data2 = pro.daily_basic( ts_code=id, start_date=start_time, end_date=end_time, fields='ts_code,trade_date,turnover_rate,total_mv,circ_mv') data1['turnover_rate'] = data2.turnover_rate.values data1['total_mv'] = data2.total_mv.values data1['circ_mv'] = data2.circ_mv.values order = [ 'trade_date', 'ts_code', 'close', 'high', 'low', 'open', 'pre_close', 'change', 'pct_chg', 'turnover_rate', 'vol', 'amount', 'total_mv', 'circ_mv' ] data = data1[order] data = data.iloc[:-5] vol = data.vol.values * 100 amount = data.amount.values * 1000 total_mv = data.total_mv.values * 10000 circ_mv = data.circ_mv.values * 10000 data['vol'] = vol data['amount'] = amount data['total_mv'] = total_mv data['circ_mv'] = circ_mv data.to_csv("data/%s.csv" % id[:-3], index=0) # print(id[:-3],':','完成') time.sleep(1)
def trade_week(stock_code, asset='E'): # df=ts.pro_bar(ts_code='002701.SZ',freq='D', end_date='20190531',adj='qfq',factors=['tor']) start_day = datetime.datetime.strptime(str( datetime.date.today()), '%Y-%m-%d') + datetime.timedelta(days=-1200) start_date = start_day.date().strftime('%Y%m%d') # month_df = ts.pro_bar(ts_code=stock_code, freq='M', asset=asset, adj='qfq') # month_df.sort_values(by='trade_date', inplace=True) # my_BOLL(month_df) week_df = ts.pro_bar(ts_code=stock_code, start_date=start_date, asset=asset, freq='W', adj='qfq') week_df.sort_values(by='trade_date', inplace=True) my_BOLL(week_df) my_MA(week_df) my_MA(week_df, item='vol', timeperiod=(10, )) my_swing(week_df) my_MACD(week_df) my_KDJ(week_df) price_res = price_section(week_df, period='week') # print(price_res) trend_res = trend(week_df) # print(trend_res) probab_res = probability(week_df) # print(probab_res) deviation_res = deviation(week_df) # print(deviation_res) if trend_res.get('ma_trend') == '下跌': return False return trade_period_return(deviation_res, price_res, probab_res, period='week')
def tushare_data(tick, start_date, end_date): """ Download daily data via tushare API for one tick. Price are non-adjusted. Symbols with download error will be appended to global errList. """ try: data0 = ts.pro_bar(ts_code=tick, start_date=start_date, end_date=end_date,adj=None) data1 = ts.pro_api().adj_factor(ts_code=tick, start_date=start_date, end_date=end_date) except Exception as e: print( "Could not download Tushare data for %s ticker " "(%s)...skipping." % (tick, e)) errList.append(tick) return [] else: data = pd.merge(data0[['ts_code','trade_date','open','high','low','close','vol']], data1[['trade_date','adj_factor']], how='left', left_on='trade_date', right_on='trade_date') data.vol = data.vol.apply(lambda x: int(x * 100)) data.trade_date = pd.to_datetime(data.trade_date) data['adj_factor'].fillna(method='pad',inplace=True) prices = [] for i, day in enumerate(data.trade_date): prices.append( ( data.iat[i,1].to_pydatetime(), # d0, trade_date, change into datetime object rather than pd.TimeStamp Object data.iat[i,2], # d1, open_price data.iat[i,3], # d2, high_price data.iat[i,4], # d3, low_price data.iat[i,5], # d4, close_price int(data.iat[i,6]), # d5, volume data.iat[i,7] # d6, adj_factor )) return prices
def trade_hour(stock_code, asset='E'): start_day = datetime.datetime.strptime(str( datetime.date.today()), '%Y-%m-%d') + datetime.timedelta(days=-70) start_date = start_day.date().strftime('%Y%m%d') hour_df = ts.pro_bar(ts_code=stock_code, freq='60min', asset=asset, start_date=start_date, adj='qfq') # print(hour_df.shape) hour_df.sort_values(by='trade_time', inplace=True) # 删除 9:30时刻的数据 hour_df = hour_df.drop(hour_df[hour_df['trade_time'].map( lambda x: x.endswith('09:30:00'))].index) my_KDJ(hour_df) my_MACD(hour_df) my_MA(hour_df, timeperiod=(20, 60)) my_BOLL(hour_df) my_MA(hour_df, item='vol', timeperiod=(10, )) my_swing(hour_df) trend_res_20 = trend(hour_df, n=20, period='hour') trend_res_60 = trend(hour_df, n=60, period='hour') probab_res = probability(hour_df) price_res = price_section(hour_df, period='hour') deviation_res = deviation(hour_df) if trend_res_20.get('ma_trend') == '下跌' and trend_res_60.get( 'ma_trend') == '下跌': return False return trade_period_return(deviation_res, price_res, probab_res, period='hour')
def insertSpecificHistData(code, startDate, endDate, isOneDay): logger.info('code is %s, start date: %s, end date: %s' % (code, startDate, endDate)) pro = ts.pro_api() df = ts.pro_bar(pro_api=pro, ts_code=code, adj='qfq', start_date=startDate, end_date=endDate, ma=[5, 10, 20, 60, 120, 250]) if (df is None): logger.info('Can not find hist data for %s' % code) return df = df.sort_index() df['code'] = code #年线是250日均线,半年线是120日均线,月线是30日均线 #ma_list = [30,60,120,250] #for ma in ma_list: # df['ma' + str(ma)] = pd.Series(df.close).rolling(window=ma).mean() # df['v_ma' + str(ma)] = pd.Series(df.amount).rolling(window=ma).mean() if (isOneDay == "True"): logger.info("Only save today's data, remove rest of data") indexNotEndDate = df[(df.index != endDate)].index.tolist() df = df.drop(indexNotEndDate) insertAndPrintResult('stocksHistData_' + endDate[0:4], df)
def get_two_trade_date_open_difference(stock_code, date, trade_date_t1, trade_date_t2): price_df = ts.pro_bar(ts_code=stock_code, adj='qfq', start_date=trade_date_t1, end_date=trade_date_t2, freq='D') try: open_t2 = price_df[price_df['trade_date'] == trade_date_t2]['open'].iloc[0] open_t1 = price_df[price_df['trade_date'] == trade_date_t1]['open'].iloc[0] except IndexError as e: raise CustomizeException(None, '查询日期:[' + trade_date_t2 + ']尚未得到开盘价') open_different = open_t2 - open_t1 open_info = { 'stock_code': [stock_code], 'query_date': [date], 'open_t1': [open_t1], 'open_t2': [open_t2], 'open_different': [open_different] } open_info_df = pd.DataFrame(open_info) print('查询日期:[' + date + '], 股票代码:[' + stock_code + '], T+2开盘价 - T+1开盘价:[%.4f' % open_different + ']') return open_info_df
def fetchStockHistoryByMonth(pro, ts_code, stockID, year, month, folder, adj='qfq', asset='E'): pro1 = ts.pro_api(MY_TOKEN) start_date = '%04d%02d01' % (year, month) end_date = '%04d%02d31' % (year, month) print(start_date, end_date, "BBB") print(ts_code, stockID, year, month, folder, adj, asset) df = None for _ in range(3): try: df = ts.pro_bar(ts_code=ts_code, api=pro1, adj=adj, start_date=start_date, end_date=end_date, asset=asset) break except Exception as e: print(e) time.sleep(1) filename = '%s_%04d%02d.xlsx' % (stockID, year, month) fullname = os.path.join(folder, filename) print(fullname) df.to_excel(fullname, encoding="utf_8_sig", index=False)
def _get_index_data(from_data='2005-01-01', end_data='2018-12-31'): end_data = datetime.strptime(end_data, "%Y-%m-%d") start_date_term = datetime.strptime("2005-01-01", "%Y-%m-%d") end_date_term = datetime.strptime("2005-12-31", "%Y-%m-%d") pre_data = None while start_date_term < end_data: df = ts.pro_bar(pro_api=api, ts_code='000300.sh', asset='I', freq='D', start_date=toString(start_date_term), end_date=toString(end_date_term)) if df is None: raise Exception('date from %s -> %s is null' % (start_date_term, end_date_term)) df = df[::][::-1] # df = df[['close', 'trade_date', 'open', 'high', 'low']][::-1] if pre_data is None: pre_data = df else: pre_data = pre_data.append(df) pre_year = start_date_term.year start_date_term = start_date_term.replace(year=pre_year + 1) end_date_term = end_date_term.replace(year=pre_year + 1) return pre_data
def get_stock_k(self, code: str, start_date=None, freq='D'): """ 从tushare获取指定股票的k线数据并存入到Mysql数据库中,在原先数据的基础上添加每日指标数据 :param code: 股票代码 :param start_date: 数据的起始日期 :param freq: 数据频率,'D','W','M','Y'分别表示日、周、月、年 :return: """ # table_name = code[:6] + '_' + fq_trans(freq) table_name = 'stock_daily' # df = ts.pro_bar(ts_code=code, start_date=start_date, adj='qfq', ma=[5, 10, 20, 30, 60, 120, 250], factors=['tor', 'vr'], adjfactor=True) if df is None: return if freq == 'D': indicator_df = ts.pro_api().daily_basic(ts_code=code, start_date=start_date) # print(indicator_df.drop(['ts_code', 'close', 'turnover_rate', 'volume_ratio'], axis=1)) if indicator_df is not None and len(indicator_df) > 0: df = pd.merge(left=df, right=indicator_df.drop(['ts_code', 'close', 'turnover_rate', 'volume_ratio'], axis=1), on='trade_date', how='left') # print(df.columns) df.to_sql(name=table_name, con=self.engine, schema='stock', if_exists='append', index=False, dtype={'trade_date': sa.DateTime()})
def qfq_comparson(df, ts_code, start_dt, end_dt): """ This script is to compare the calcuated qfq close value with the ones directly download from tushare. """ #-------通用行情接口获取前复权数据------ dt = ts.pro_bar(ts_code=ts_code, asset='E', api=pro, adj='qfq', freq='D', adjfactor=False, start_date=start_dt, end_date=end_dt) dt = dt.sort_values(by='trade_date', ascending=True) dt = dt.reset_index(drop=True) #du = dt.iloc[ (ex_date_index - 5 ) : ( ex_date_index + 5 ) ] dx = pd.DataFrame() dx['ts_code'] = [ts_code] * dt.shape[0] dx['trade_date'] = dt['trade_date'] dx['close'] = df['close'] dx['ts_qfq'] = dt['close'] dx['me_qfq'] = df['close_qfq'] dx['diff'] = dx['ts_qfq'] - dx['me_qfq'] return dx
def extract_one_share(self, share_id, start_date, end_date): """ :param share_id: :param start_date: :param end_date: :return: """ bar_df = ts.pro_bar(pro_api=context.tushare, ts_code=share_id, start_date=start_date, end_date=end_date, adj='qfq') # raw_close_df = self._extract_one_share_n_days_price(share_id=share_id, bar_df=bar_df, field="close") close_df = self._extract_one_share_n_days_price_ratio( share_id=share_id, bar_df=bar_df, field="close") open_df = self._extract_one_share_n_days_price_ratio(share_id=share_id, bar_df=bar_df, field="open") high_df = self._extract_one_share_n_days_price_ratio(share_id=share_id, bar_df=bar_df, field="high") low_df = self._extract_one_share_n_days_price_ratio(share_id=share_id, bar_df=bar_df, field="low") vol_df = self._extract_one_share_n_days_price_ratio(share_id=share_id, bar_df=bar_df, field="vol") # ror_df = self._extract_one_share_n_days_ror(bar_df, share_id) # assert ror_df is not None, "share_id = {} ror_df is None".format(share_id) self._merge_data_to_commit(close_df, open_df, high_df, low_df, vol_df)
def __init__(self, start_date, end_date, index): # security = get_index_stocks(index) # price = get_price(security, None, end_date, '1d', # ['open', 'close', 'low', 'high', 'avg_price', 'prev_close', 'volume', 'turnover'], False, # None, 250, is_panel=1) # benchmark_price = get_price(index, None, end_date, '1d', # ['open', 'close', 'low', 'high', 'avg_price', 'prev_close', 'volume'], False, None, # 250, is_panel=1) price = ts.pro_bar(ts_code=index, adj='qfq', start_date=start_date, end_date=end_date) ## 设置索引 price.set_index(['trade_date'], inplace=True) ## 升序排序 self.price = price.sort_index() ###分别取开盘价,收盘价,最高价,最低价,最低价,均价,成交量####### # self.open_price = price.loc['open', :, :].dropna(axis=1, how='any') self.open_price = price['open'] self.close = price['close'] self.low = price['low'] self.high = price['high'] self.pct_chg = price['pct_chg'] self.volume = price['vol'] self.ts_code = index
def ingest(environ, asset_db_writer, minute_bar_writer, daily_bar_writer, adjustment_writer, calendar, start_session, end_session, cache, show_progress, output_dir): symbols = ['600019.SH'] pro_api = ts.pro_api(tushare_token) dtype = [('start_date', 'datetime64[ns]'), ('end_date', 'datetime64[ns]'), ('auto_close_date', 'datetime64[ns]'), ('symbol', 'object')] metadata = DataFrame(empty(len(symbols), dtype=dtype)) with maybe_show_progress(symbols, show_progress, label='Loading CN A %s pricing data: ' % (symbols)) as it: for sid, symbol in enumerate(it): tushare_daily = ts.pro_bar(pro_api=pro_api, ts_code=symbol, asset='E', start_date=start_session.strftime('%Y%m%d'), end_date=end_session.strftime('%Y%m%d'), adj='qfq') tushare_daily['day'] = pd.to_datetime(tushare_daily['trade_date']) tushare_daily['volume'] = tushare_daily['vol'] tushare_daily['id'] = tushare_daily['ts_code'] tushare_daily = tushare_daily.filter(items=['day', 'open', 'high', 'low', 'close', 'volume']) tushare_daily = tushare_daily.set_index('day').sort_index() start_date = tushare_daily.index[0] end_date = tushare_daily.index[-1] end_date = start_date if start_date > end_date else end_date # The auto_close date is the day after the last trade. ac_date = end_date + Timedelta(days=1) metadata.iloc[sid] = start_date, end_date, ac_date, symbol daily_bar_writer.write([(sid, tushare_daily)], show_progress=show_progress) metadata['exchange'] = 'SSE' asset_db_writer.write(equities=metadata) adjustment_writer.write(None)
def fetch_data(): data = None try: time.sleep(0.002) pro = get_pro() data = ts.pro_bar( pro_api=pro, ts_code=str(name), asset='E', adj=if_fq, start_date=start, end_date=end, freq='D', factors=['tor', 'vr'] ).sort_index() print('fetch done: ' + str(name)) except Exception as e: print(e) print('except when fetch data of ' + str(name)) time.sleep(1) data = fetch_data() return data