Python tushare.get_hist_data() Examples
The following are 25
code examples of tushare.get_hist_data().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
tushare
, or try the search function
.
Example #1
Source File: tushare.py From backtrader-cn with GNU General Public License v3.0 | 6 votes |
def _init_coll(self): """ Get all the history data when initiate the library. 1. Connect to arctic and create the library. 2. Get all the history data from tushare and strip the unused columns. 3. Store the data to arctic. :return: None """ # if collection is not initialized if self._coll_name not in self._library.list_symbols(): self._new_added_colls.append(self._coll_name) his_data = ts.get_hist_data(code=self._coll_name, retry_count=5).sort_index() if len(his_data) == 0: logger.warning( f'data of stock {self._coll_name} when initiation is empty' ) return his_data = bdu.Utils.strip_unused_cols(his_data, *self._unused_cols) logger.debug(f'write history data for stock: {self._coll_name}.') self._library.write(self._coll_name, his_data)
Example #2
Source File: common.py From stock with Apache License 2.0 | 6 votes |
def get_hist_data_cache(code, date_start, date_end): cache_dir = bash_stock_tmp % (date_end[0:7], date_end) # 如果没有文件夹创建一个。月文件夹和日文件夹。方便删除。 # print("cache_dir:", cache_dir) if not os.path.exists(cache_dir): os.makedirs(cache_dir) cache_file = cache_dir + "%s^%s.gzip.pickle" % (date_end, code) # 如果缓存存在就直接返回缓存数据。压缩方式。 if os.path.isfile(cache_file): print("######### read from cache #########", cache_file) return pd.read_pickle(cache_file, compression="gzip") else: print("######### get data, write cache #########", code, date_start, date_end) stock = ts.get_hist_data(code, start=date_start, end=date_end) if stock is None: return None stock = stock.sort_index(0) # 将数据按照日期排序下。 stock.to_pickle(cache_file, compression="gzip") return stock
Example #3
Source File: cache.py From klineyes with MIT License | 6 votes |
def _cache_monthly(self, date): ''' 缓存date所在月的数据,等缓存成功后返回True :param code: :param ktype: :param date: :return: ''' print 'caching...' start, end = self._get_date_range_of_month(date, 'str') df = ts.get_hist_data(code=self.code, ktype=self.ktype, start=start, end=end, retry_count=6) if df is not None: df.to_csv(self._get_cache_filename(date)) waiting_seconds = 0 while not self._in_cache(date=date): sleep(1) waiting_seconds += 1 if waiting_seconds > 30: self._cache_monthly(date=date) return True
Example #4
Source File: views.py From StockPredict with Apache License 2.0 | 6 votes |
def home(request): stock_his_data = ts.get_hist_data('sh000001') stock_name = get_stock_name('sh000001') date = stock_his_data.index.tolist() open = stock_his_data['open'].tolist() close = stock_his_data['close'].tolist() high = stock_his_data['high'].tolist() low = stock_his_data['low'].tolist() volume = stock_his_data['volume'].tolist() dataMA5 = stock_his_data['ma5'].tolist() dataMA10 = stock_his_data['ma10'].tolist() dataMA20 = stock_his_data['ma20'].tolist() return render(request, 'home.html', {'date': json.dumps(date), 'open': json.dumps(open), 'close': json.dumps(close), 'high': json.dumps(high), 'low': json.dumps(low), 'volume': json.dumps(volume), 'dataMA5': json.dumps(dataMA5), 'dataMA10': json.dumps(dataMA10), 'dataMA20': json.dumps(dataMA20), 'stock_name': json.dumps(stock_name)})
Example #5
Source File: views.py From StockPredict with Apache License 2.0 | 6 votes |
def dash_index(request): stock_his_data = ts.get_hist_data('sz') stock_name = get_stock_name('sz') stock_his_data_dic = json.dumps(stock_his_data.to_json(orient='index')) pprint.pprint(stock_his_data_dic) print(type(stock_his_data_dic)) date = stock_his_data.index.tolist() open = stock_his_data['open'].tolist() close = stock_his_data['close'].tolist() high = stock_his_data['high'].tolist() low = stock_his_data['low'].tolist() volume = stock_his_data['volume'].tolist() dataMA5 = stock_his_data['ma5'].tolist() dataMA10 = stock_his_data['ma10'].tolist() dataMA20 = stock_his_data['ma20'].tolist() return render(request, 'base_dash.html', {'stock_his_data_dic': stock_his_data_dic, 'date': json.dumps(date), 'open': json.dumps(open), 'close': json.dumps(close), 'high': json.dumps(high), 'low': json.dumps(low), 'volume': json.dumps(volume), 'dataMA5': json.dumps(dataMA5), 'dataMA10': json.dumps(dataMA10), 'dataMA20': json.dumps(dataMA20), 'stock_name': json.dumps(stock_name)})
Example #6
Source File: views.py From StockSensation with Apache License 2.0 | 6 votes |
def dash_index(request): stock_his_data = ts.get_hist_data('sz') stock_name = get_stock_name('sz') stock_his_data_dic = json.dumps(stock_his_data.to_json(orient='index')) pprint.pprint(stock_his_data_dic) print(type(stock_his_data_dic)) date = stock_his_data.index.tolist() open = stock_his_data['open'].tolist() close = stock_his_data['close'].tolist() high = stock_his_data['high'].tolist() low = stock_his_data['low'].tolist() volume = stock_his_data['volume'].tolist() dataMA5 = stock_his_data['ma5'].tolist() dataMA10 = stock_his_data['ma10'].tolist() dataMA20 = stock_his_data['ma20'].tolist() return render(request, 'base_dash.html', {'stock_his_data_dic': stock_his_data_dic, 'date': json.dumps(date), 'open': json.dumps(open), 'close': json.dumps(close), 'high': json.dumps(high), 'low': json.dumps(low), 'volume': json.dumps(volume), 'dataMA5': json.dumps(dataMA5), 'dataMA10': json.dumps(dataMA10), 'dataMA20': json.dumps(dataMA20), 'stock_name': json.dumps(stock_name)})
Example #7
Source File: main.py From stock with Apache License 2.0 | 5 votes |
def get_stock_price(code, include_realtime_price): """ 获取个股股价 :param code: 股票代码 :param include_realtime_price: 是否含实时股价 :return: """ # 获取历史股价 df = ts.get_hist_data(code) df = df[['close']] df['date'] = df.index if include_realtime_price: df_today = ts.get_today_all() df_code = df_today[df_today['code']==code] df_code = df_code[['trade']] df_code['date'] = GetNowDate() df_code.rename(columns={'trade': 'close'}, inplace=True) df = pd.concat([df, df_code], ignore_index=True) df.sort(columns='date', inplace=True) df = df.drop_duplicates(['date']) df.index = range(len(df)) print '\n' # print df.head() print df.tail() return df
Example #8
Source File: tushare.py From backtrader-cn with GNU General Public License v3.0 | 5 votes |
def download_delta_data(self): """ Get yesterday's data and append it to collection, this method is planned to be executed at each day's 8:30am to update the data. 1. Connect to arctic and get the library. 2. Get today's history data from tushare and strip the unused columns. 3. Store the data to arctic. :return: None """ self._init_coll() if self._coll_name in self._new_added_colls: return # 15:00 PM can get today data # start = latest_date + 1 day latest_date = self.get_data().index[-1] start = latest_date + dt.timedelta(days=1) start = dt.datetime.strftime(start, '%Y-%m-%d') his_data = ts.get_hist_data( code=self._coll_name, start=start, retry_count=5 ) # delta data is empty if len(his_data) == 0: logger.info( f'delta data of stock {self._coll_name} is empty, after {start}') return his_data = bdu.Utils.strip_unused_cols(his_data, *self._unused_cols) logger.info(f'got delta data of stock: {self._coll_name}, after {start}') self._library.append(self._coll_name, his_data)
Example #9
Source File: server.py From mt4plus with GNU General Public License v2.0 | 5 votes |
def get(self): symbol = self.get_argument("symbol")#股票代码 period = self.get_argument("period")#时间周期,单位-分钟 period_allow_list = ["5","15","30","60","1440","M","W"] if period not in period_allow_list: return data = None if period =="1440": data = ts.get_hist_data(symbol) else: data = ts.get_k_data(symbol,ktype=period) print "=========",symbol,":",period resultlist = [] lens = len(data) for unit in data.iterrows(): obj = {} dates = None if period =="1440": dates = unit[1].name else: dates = unit[1]['date'] print "len",len(dates) # 长度等于10的是%Y-%m-%d格式,16的是%Y-%m-%d %H:%M 格式 dataformate = "%Y-%m-%d %H:%M" date_len = len(dates) if date_len == 10 : dataformate = "%Y-%m-%d" d=datetime.datetime.strptime(dates,dataformate) obj["date"]=int(time.mktime(d.timetuple())) obj["open"]=unit[1]['open'] obj["close"]=unit[1]['close'] obj["high"]=unit[1]['high'] obj["low"]=unit[1]['low'] obj["volume"]=unit[1]['volume'] resultlist.append(obj) resultlist.sort(key=lambda obj:obj.get('date'), reverse=False) s = json.dumps(resultlist) self.write(s)
Example #10
Source File: backtest_data_feed_tushare.py From EliteQuant_Python with Apache License 2.0 | 5 votes |
def _retrieve_online_historical_data(self, symbol): """ Retrieve historical data from web """ data = ts.get_hist_data(symbol, start=self._start_date.strftime('%Y-%m-%d'), end=self._end_date.strftime('%Y-%m-%d')) data = data.sort_index() data.index = data.index.to_datetime() #data.index = data.index.tz_localize('UTC') self._hist_data[symbol] = data self._hist_data[symbol]["FullSymbol"] = symbol # attach symbol to data; it will be merged into _data_stream
Example #11
Source File: views.py From StockPredict with Apache License 2.0 | 5 votes |
def stock_his_data2json(stock_code): stock_name = get_stock_name(stock_code) stock_his_data_json = json.dumps(ts.get_hist_data(stock_code).to_json(orient='index')) return stock_name,stock_his_data_json #----------------------------- Request Methos -----------------------------#
Example #12
Source File: spyder_tushare.py From quantproject with Apache License 2.0 | 5 votes |
def __init__(self,code=None,start=None,end=None,save=True): """ get symbol history data @@param:code:股票代码,如果code is None,或者全部股票 @@parm:start:开始时间 @@end:结束时间 """ now = dt.datetime.now() if end is None: end = (now -dt.timedelta(1)).strftime('%Y-%m-%d') if start is None: start = '1990-01-01' if code is None: mongo =mongodb()##获取股票代码 collectname = 'SymbolMdm' where = {} kwargs = {'code':1}##获取code代码字断 codes = mongo.select(collectname,where,kwargs) for code in codes: HistData(code.get('code'),HistData,end)##递归请求历史数据 else: try: data = ts.get_hist_data(code,start=start,end=end) data['datatime'] = now.strftime('%Y-%m-%d') data['datatimestramp'] = now.strftime('%H:%M:%S') data['date'] =data.index data['code'] = code indexlist = ['code','date']##数据库索引 tableName = 'HistData' database(data,indexlist,tableName,save) print 'info:{} downloaded is ok,num:{}! '.format(code,len(data)) except: pass
Example #13
Source File: storing_test.py From TuShare with BSD 3-Clause "New" or "Revised" License | 5 votes |
def appends(): filename = 'c:/day/bigfile.csv' for code in ['000875', '600848', '000981']: df = ts.get_hist_data(code) if os.path.exists(filename): df.to_csv(filename, mode='a', header=None) else: df.to_csv(filename)
Example #14
Source File: storing_test.py From TuShare with BSD 3-Clause "New" or "Revised" License | 5 votes |
def json(): df = ts.get_hist_data('000875') df.to_json('c:/day/000875.json',orient='records') #或者直接使用 print(df.to_json(orient='records'))
Example #15
Source File: storing_test.py From TuShare with BSD 3-Clause "New" or "Revised" License | 5 votes |
def hdf(): df = ts.get_hist_data('000875') # df.to_hdf('c:/day/store.h5','table') store = HDFStore('c:/day/store.h5') store['000875'] = df store.close()
Example #16
Source File: storing_test.py From TuShare with BSD 3-Clause "New" or "Revised" License | 5 votes |
def xls(): df = ts.get_hist_data('000875') #直接保存 df.to_excel('c:/day/000875.xlsx', startrow=2,startcol=5)
Example #17
Source File: storing_test.py From TuShare with BSD 3-Clause "New" or "Revised" License | 5 votes |
def csv(): df = ts.get_hist_data('000875') df.to_csv('c:/day/000875.csv',columns=['open','high','low','close'])
Example #18
Source File: views.py From StockSensation with Apache License 2.0 | 5 votes |
def home(request): stock_his_data = ts.get_hist_data('sh000001') stock_name = get_stock_name('sh000001') date = stock_his_data.index.tolist() open = stock_his_data['open'].tolist() close = stock_his_data['close'].tolist() high = stock_his_data['high'].tolist() low = stock_his_data['low'].tolist() volume = stock_his_data['volume'].tolist() dataMA5 = stock_his_data['ma5'].tolist() dataMA10 = stock_his_data['ma10'].tolist() dataMA20 = stock_his_data['ma20'].tolist() return render(request, 'home.html', {'date': json.dumps(date), 'open': json.dumps(open), 'close': json.dumps(close), 'high': json.dumps(high), 'low': json.dumps(low), 'volume': json.dumps(volume), 'dataMA5': json.dumps(dataMA5), 'dataMA10': json.dumps(dataMA10), 'dataMA20': json.dumps(dataMA20), 'stock_name': json.dumps(stock_name)})
Example #19
Source File: views.py From StockSensation with Apache License 2.0 | 5 votes |
def stock_his_data2json(stock_code): stock_name = get_stock_name(stock_code) stock_his_data_json = json.dumps(ts.get_hist_data(stock_code).to_json(orient='index')) return stock_name,stock_his_data_json #----------------------------- Request Methos -----------------------------#
Example #20
Source File: storing_test.py From tushare with BSD 3-Clause "New" or "Revised" License | 5 votes |
def appends(): filename = 'c:/day/bigfile.csv' for code in ['000875', '600848', '000981']: df = ts.get_hist_data(code) if os.path.exists(filename): df.to_csv(filename, mode='a', header=None) else: df.to_csv(filename)
Example #21
Source File: storing_test.py From tushare with BSD 3-Clause "New" or "Revised" License | 5 votes |
def json(): df = ts.get_hist_data('000875') df.to_json('c:/day/000875.json',orient='records') #或者直接使用 print(df.to_json(orient='records'))
Example #22
Source File: storing_test.py From tushare with BSD 3-Clause "New" or "Revised" License | 5 votes |
def hdf(): df = ts.get_hist_data('000875') # df.to_hdf('c:/day/store.h5','table') store = HDFStore('c:/day/store.h5') store['000875'] = df store.close()
Example #23
Source File: storing_test.py From tushare with BSD 3-Clause "New" or "Revised" License | 5 votes |
def xls(): df = ts.get_hist_data('000875') #直接保存 df.to_excel('c:/day/000875.xlsx', startrow=2,startcol=5)
Example #24
Source File: storing_test.py From tushare with BSD 3-Clause "New" or "Revised" License | 5 votes |
def csv(): df = ts.get_hist_data('000875') df.to_csv('c:/day/000875.csv',columns=['open','high','low','close'])
Example #25
Source File: dividend_rate_v2.py From anack with GNU General Public License v3.0 | 4 votes |
def divident_rate(self): stock = ts.get_hist_data(self.id) df = dividend_rate.get_bonus_table(self) df_dividend = df[['年度','派息','登记日']] # print(df_dividend) stock_close_price = stock["close"] sIndex = stock_close_price.index.tolist() # 获取登记日 regis = df_dividend['登记日'].tolist() # print(sIndex) # print(regis) close_price = [] diVi = [] aPe = [] bonus = [] div_year = [] for i in regis: if i != "--" and i in sIndex: cprice = stock_close_price.loc[i] close_price.append(cprice) aDiv = df_dividend[df_dividend['登记日'] == i]['派息'].tolist()[0] year = df_dividend[df_dividend['登记日'] == i]['年度'].values #获得年份 div_year.append(year[0]) #此处的bonus暂时通过ts获得,以后可以直接搜索本地数据库 profit_table = ts.get_report_data(year[0],4) #获取年度eps print('') target_eps = profit_table[profit_table['code'] == self.id]['eps'].values eps = target_eps[0].item() #numpy.float64 -> float per_bonus = round(float(aDiv) / 10 / eps * 100, 2) # per_bonus = 1 #测试时开启 bonus.append(per_bonus) diVi.append(float(aDiv)/10) #10股派息转1股派息 div_ratio = [] for i,j in zip(diVi,close_price): adivr = float(i) / float(j) * 100 div_ratio.append(round(adivr,2)) aPe.append(round(100/adivr,2)) reDf = pd.DataFrame({"cash_div":diVi, #每股派现方案 "div_ratio(%)":div_ratio, #股息率 'ape':aPe, #真实市盈率 'bonus_ratio(%)':bonus #分红率 },index = div_year) # 统计输出 print(self.id + '分红情况统计如下:') avg_bonus = round(sum(bonus)/len(bonus),2) print('1.平均分红率:',avg_bonus,'%') avg_div = round(sum(div_ratio)/len(div_ratio),2) print('2.平均股息率:',avg_div,'%') print('3.详细列表如下所示') return reDf ##############################################