我们从Python开源项目中,提取了以下38个代码示例,用于说明如何使用tushare.get_stock_basics()。
def QA_save_stock_day_all(client=QA_Setting.client): df = ts.get_stock_basics() __coll = client.quantaxis.stock_day __coll.ensure_index('code') def saving_work(i): QA_util_log_info('Now Saving ==== %s' % (i)) try: data_json = QA_fetch_get_stock_day( i, startDate='1990-01-01') __coll.insert_many(data_json) except: QA_util_log_info('error in saving ==== %s' % str(i)) for i_ in range(len(df.index)): QA_util_log_info('The %s of Total %s' % (i_, len(df.index))) QA_util_log_info('DOWNLOAD PROGRESS %s ' % str( float(i_ / len(df.index) * 100))[0:4] + '%') saving_work(df.index[i_]) saving_work('hs300') saving_work('sz50')
def QA_save_stock_day_all_bfq(client=QA_Setting.client): df = ts.get_stock_basics() __coll = client.quantaxis.stock_day_bfq __coll.ensure_index('code') def saving_work(i): QA_util_log_info('Now Saving ==== %s' % (i)) try: data_json = QA_fetch_get_stock_day( i, startDate='1990-01-01', if_fq='00') __coll.insert_many(data_json) except: QA_util_log_info('error in saving ==== %s' % str(i)) for i_ in range(len(df.index)): QA_util_log_info('The %s of Total %s' % (i_, len(df.index))) QA_util_log_info('DOWNLOAD PROGRESS %s ' % str( float(i_ / len(df.index) * 100))[0:4] + '%') saving_work(df.index[i_]) saving_work('hs300') saving_work('sz50')
def calcu_all_stocks_3year_average_profit(year): # ??3??????? path = os.path.join(current_folder, '????%s.csv' % today) if not os.path.exists(path): data = ts.get_stock_basics() lie = ['??', '??', '??', '???', '????', '???', '???(?)', '????', '????', '???', '?????', '????', '????', '???', '????', '????', '?????', '????(%)', '????(%)', '???(%)', '????(%)', '????'] data.columns = lie data.index.names = ['??'] data.to_csv(path, encoding='utf-8') data = pd.read_csv(path, encoding='utf-8', index_col=0) # print(data) data['????'] = 0 for index, row in data.iterrows(): try: data.loc[index, '????'] = calcu_3year_average_profit('%06d' % index, year) except Exception as e: print(e) data.loc[index, '????'] = 0 print('??%s' % index) data.to_csv(os.path.join(current_folder, '3????????????%s.csv' % today), encoding='utf-8')
def load_tushare_df(df_type): file = 'ts.' + df_type + '.dat' try: obj = pickle.load(open(file,"rb")) except: #print("---load in the fly",df_type) if df_type == "basic": obj = ts.get_stock_basics() elif df_type == "sme": obj = ts.get_sme_classified() elif df_type == "gem": obj=ts.get_gem_classified() elif df_type == "industry": #print(ts, pickle) obj = ts.get_industry_classified() #?????,??2800??,?????3326,??????? get_stock_basics elif df_type == "st": obj = ts.get_st_classified() else: raise Exception("Error TSshare Type!!!") pickle.dump(obj,open(file,"wb",0)) else: #print("***Read from file %s" % df_type) pass return obj
def __init__(self): #??????????????data???????? current = os.getcwd() folder = os.path.join(current, 'data') if os.path.exists(folder) == False: os.mkdir(folder) os.chdir(folder) #??tushare?????A??? #df0=ts.get_stock_basics() df0=pd.read_csv('bases.csv',dtype={'code':np.str}) self.bases=df0.sort_values('timeToMarket',ascending=False) #????? ???????????? self.cxg=self.bases[(self.bases['timeToMarket']>20170101) & (self.bases['timeToMarket']<20170401)] self.codes= self.cxg['code'].values
def show_name(self): #self.all=ts.get_stock_basics() #self.bases_save=ts.get_stock_basics() #self.bases_save.to_csv('bases.csv') stock_list=self.multi_thread() for st in stock_list: print "code: ",st[0] name=self.base[self.base['code']==st[0]]['name'].values[0] print 'name: ',name print "ratio: ",st[1] if st[1]>30: print "WOW, more than 30" print '\n'
def main(): # ?????? ?3???? history = ts.get_hist_data(id) print u"??3????" print history.head(10) history_all = ts.get_h_data(id, '20015101', '20160101') print u'???????' print history_all # print type(stockInfo) # print stockInfo.head() # print stockInfo.dtypes # df = ts.get_stock_basics() #data = df.ix[id]['timeToMarket'] #print data #ts.get_today_all()
def add_code_redis(): rds = redis.StrictRedis(REDIS_HOST, 6379, db=0) rds_1 = redis.StrictRedis(REDIS_HOST, 6379, db=1) df = ts.get_stock_basics() df = df.reset_index() # ????? if rds.dbsize() != 0: rds.flushdb() if rds_1.dbsize() != 0: rds_1.flushdb() for i in range(len(df)): code, name, timeToMarket = df.loc[i]['code'], df.loc[i]['name'], df.loc[i]['timeToMarket'] # print str(timeToMarket) d = dict({code: ':'.join([name, str(timeToMarket)])}) # print d rds.set(code, name) rds_1.lpush('codes', d)
def QA_save_stock_day_all(): df = ts.get_stock_basics() __setting = QA_Setting() __coll = __setting.client.quantaxis.stock_day __coll.ensure_index('code') def saving_work(i): print('Now Saving ==== %s' % (i)) try: data_json = QATushare.QA_fetch_get_stock_day(i) __coll.insert_many(data_json) except: print('error in saving ==== %s' % str(i)) for item in df.index: saving_work(item) saving_work('hs300') saving_work('sz50')
def get_stocks_basics_info(): file = infoFilePath + "stk_basics_" + today + ".csv" if os.path.exists(file) is not True: try: raw_data = ts.get_stock_basics() except IOError: raise Exception('Can not read stocks basic info through tushare interface!') raw_data = raw_data.sort_index() try: raw_data.to_csv(file, index=True, encoding='gbk') except Exception: print('Write stocks basic info to csv file error!') try: raw_data = pd.read_csv(file, encoding='gbk') except IOError: print('Read stocks basic info from csv file error!') return raw_data
def QA_save_stock_day_with_fqfactor(client=QA_Setting.client): df = ts.get_stock_basics() __coll = client.quantaxis.stock_day __coll.ensure_index('code') def saving_work(i): QA_util_log_info('Now Saving ==== %s' % (i)) try: data_hfq = QA_fetch_get_stock_day( i, startDate='1990-01-01', if_fq='02', type_='pd') data_json = QA_util_to_json_from_pandas(data_hfq) __coll.insert_many(data_json) except: QA_util_log_info('error in saving ==== %s' % str(i)) for i_ in range(len(df.index)): QA_util_log_info('The %s of Total %s' % (i_, len(df.index))) QA_util_log_info('DOWNLOAD PROGRESS %s ' % str( float(i_ / len(df.index) * 100))[0:4] + '%') saving_work(df.index[i_]) saving_work('hs300') saving_work('sz50') QA_util_log_info('Saving Process has been done !') return 0
def QA_fetch_get_stock_info(name): data = QATs.get_stock_basics() data_json = QA_util_to_json_from_pandas(data) for i in range(0, len(data_json) - 1, 1): data_json[i]['code'] = data.index[i] return data_json
def QA_fetch_get_stock_list(): df = QATs.get_stock_basics() return list(df.index)
def get_symbols_save_into_db(mode = 'replace'): ''' ?????????????. Parameters ----------- mode ??,replace????,append??????? ''' now = dt.datetime.now() data = ts.get_stock_basics() con = pymysql.connect( host = db_host, user = db_user, passwd = db_pass, db = db_name, charset = 'utf8') data['created_time'] = now data['last_updated_time'] = now data['id'] = range(1,len(data) + 1) data['ticker'] = data.index data['timeToMarket'] = data['timeToMarket'].apply(date_time_str_convertor) data.to_sql('symbols',con,if_exists = 'replace', index = False,flavor = 'mysql') sql = ''' ALTER TABLE symbols ADD PRIMARY KEY (id); ''' cur = con.cursor() cur.execute(sql) con.close() print('????symbols?(??????)')
def code_issue(): base = ts.get_stock_basics() base.to_excel('111.xls')
def get_stock_basics(): """ ???????? Return -------- DataFrame """ basics = ts.get_stock_basics() return basics
def main(): # reload(sys) # sys.setdefaultencoding('utf8') stock_codes = get_stock_basics() threads = [] try: """ ???????????? """ stocks = Queue.Queue(len(stock_codes)) for code in stock_codes.index: code = str(code) if (len(code) != 6): code = (6 - len(code)) * '0' + code stocks.put(code) """ ??????? """ for n in range(THREADS_NUM): thread = GetStockData(n, stocks) thread.start() threads.append(thread) while not stocks.empty(): pass print u'??????' THREADS_EXITFLAG = 1 for t in threads: t.join() except BaseException as e: print ('Error', e) return
def get_stock_basics(): ''' invoke tushare get_stock_basics() with csv output args: returns: csv format data containing the whole martket information json fomat, df.to_json('basics.json', orient='index') ''' filename = PREFIX + '/' + 'basics.csv' df = ts.get_stock_basics() df.sort_index(inplace=True) return df.to_csv(filename, encoding='UTF-8')
def get_all_stick_inf(self): try: df = ts.get_stock_basics() except Exception as e: print('Err:',e) return cnt = 0; print('begin====== ??????? ????????????? ===============') while cnt < 3: try: self.cur.execute("select count(*) from t_all_stickcode; ") rs = self.cur.fetchall() print('rs:',rs[0][0],len(df)) if rs[0][0] == len(df) : break print('delete') self.cur.execute("delete from t_all_stickcode; ") self.sqlconn.commit() print('insert') df.to_sql('t_all_stickcode',self.engine,if_exists='append') except: print('err') df.to_sql('t_all_stickcode',self.engine,if_exists='append') cnt += 1; #df = ts.get_ self.sqlconn.commit() return
def get_stock_basics(self, saveFile): info = ts.get_stock_basics() if saveFile: info.to_csv("./output/stock_basics.csv") return info
def __init__(self): #self.baseinfo=ts.get_stock_basics() self.getDate()
def save_baseinfo(): df = ts.get_stock_basics() #print df df = df.reset_index() df.to_sql('baseinfo',engine) #?????????????
def main(): now = time.strftime("%Y-%m-%d") # print now token = '60517739976b768e07823056c6f9cb0fee33ed55a1709b3eaa14a76c6a1b7a56' sb = StockBox() # sb.looper(id) id = '300333' # sb.realtime(id) sb.base_function("300333") # pandas_test=Pandas_test() # pandas_test.test_function() # sb.longhuban('2016-04-05') # sb.getNews() # sb.fund() #sb.get_stock_chengfeng() #sb.date_store() #sb.profit_test() #sb.daily_longhu() # ?????? ?3???? history = ts.get_hist_data(id) print u"??3????" print history.head(10) history_all = ts.get_h_data(id, '20015101', '20160101') print u'???????' print history_all # print type(stockInfo) # print stockInfo.head() # print stockInfo.dtypes #df = ts.get_stock_basics() #data = df.ix[id]['timeToMarket'] #print data #ts.get_today_all()
def get_info(self, id): # ??????????? df = ts.get_stock_basics() print df.ix['300333']['timeToMarket']
def __init__(self): self.bases_save=ts.get_stock_basics() self.bases_save.to_csv('bases.csv') # ?????????????? self.today = time.strftime("%Y-%m-%d", time.localtime()) self.base = pd.read_csv('bases.csv', dtype={'code': np.str}) self.all_code = self.base['code'].values self.working_count=0 self.mystocklist = Toolkit.read_stock('mystock.csv') # ???excel ?? ????csv ??,excel??.
def store_base_data(self, target): self.all_info = ts.get_stock_basics() self.all_info = self.all_info.reset_index() print self.all_info if target == 'sql': self.all_info.to_sql('tb_baseinfo', engine,if_exists='replace') elif target == 'csv': self.all_info.to_csv('baseInfo.csv') else: logging.info('sql or csv option. Not get right argument') # ?????????
def getStocksList_CHN(root_path): try: df = queryStockList(root_path, "DB_STOCK", "SHEET_CHN") df.index = df.index.astype(str).str.zfill(6) except Exception as e: df = pd.DataFrame() if df.empty == False: return df stock_info = ts.get_stock_basics() listData = pd.DataFrame(stock_info) listData['daily_update'] = '1970-07-01' listData['weekly_update'] = '1970-07-01' listData['monthly_update'] = '1970-07-01' listData['news_update'] = '1970-07-01' listData.index.name = 'symbol' listData = listData.reset_index() #listData.index.name = 'symbol' #listData.index = listData.index.astype(str).str.zfill(6) #[str(symbol).zfill(6) for symbol in listData.index] #listData.index.astype(str).str.zfill(6) #print(listData.index) #listData['symbol'] = listData['symbol'].str.strip() storeStockList(root_path, "DB_STOCK", "SHEET_CHN", listData) df = queryStockList(root_path, "DB_STOCK", "SHEET_CHN") if df.empty == False: df.index = df.index.astype(str).str.zfill(6) return df
def processing_sector_cashflow_count(root_path, symbols, dates): stock_info = ts.get_stock_basics() sector_columns = list(set(stock_info['industry'].values.tolist())) sector_count = pd.DataFrame(columns=sector_columns, index=dates) sector_count.index.name = 'date' sector_count = sector_count.fillna(0) pbar = tqdm(total=len(symbols)) for symbol in symbols: startTime = time.time() out_file = root_path + "/Data/CSV/cashflow/" + symbol + ".csv" column = stock_info[stock_info.index == symbol]["industry"][0] if os.path.exists(out_file) == False: pbar.update(1) #print(symbol, column) continue df_symbol = pd.read_csv(out_file, index_col=["date"]) df = df_symbol['buy_amount'] - df_symbol["sell_amount"] sector_count[column] = sector_count[column].add(df, fill_value=0) outMessage = '%-*s processed in: %.4s seconds' % (6, symbol, (time.time() - startTime)) pbar.set_description(outMessage) pbar.update(1) pbar.close() sector_count = sector_count.sort_index(ascending=False) sector_count.to_csv("cashflow_sector.csv")
def __init__(self, name): super(TsDatasrc, self).__init__(name) print 'security init begin ...' self.__initFlag__() self.__df_allsecurities__ = ts.get_stock_basics().sort_index() print 'security init end ...'
def get_basic_info(show_progress=True): # ????? if show_progress: click.echo("????????") ts_symbols = ts.get_stock_basics() if show_progress: click.echo("??????") symbols = [] histories = {} # ?????? i = 0 total = len(ts_symbols) for index, row in ts_symbols.iterrows(): i = i +1 if i > 10: break srow = {} # ???????? click.echo("??????%s(%s)??????? (%d/%d)" % (index, row['name'], i, total)) histories[index] = ts.get_hist_data(index) srow['start_date'] = histories[index].index[-1] srow['end_date'] = histories[index].index[0] srow['symbol'] = index srow['asset_name'] = row['name'] symbols.append(srow) df_symbols = pd.DataFrame(data=symbols).sort_values('symbol') symbol_map = pd.DataFrame.copy(df_symbols.symbol) # fix the symbol exchange info df = df_symbols.apply(func=convert_symbol_series, axis=1) return df, histories, symbol_map
def QA_fetch_get_stock_info(name): data = QATs.get_stock_basics() data_json = json.loads(data.to_json(orient='records')) for i in range(0, len(data_json) - 1, 1): data_json[i]['code'] = data.index[i] return data_json
def fetch_stock_basics(conn): logging.debug("Fetch stocks") df = ts.get_stock_basics() df['timeToMarket'] = df['timeToMarket'].map(lambda s: datetime.strptime(str(s), '%Y%m%d') if s > 0 else None) df.to_sql('stock_basics', conn, if_exists="replace", dtype={"code": VARCHAR(32)})
def Get_Stock_List(): df = ts.get_stock_basics() return df #????????????????? #??MACD?KDJ?????
def getCompany(): """code ??, name ??, industry ????, area ??, pe ???, outstanding ????(?), totals ???(?), totalAssets ???(?), liquidAssets ????, fixedAssets ????, reserved ???, reservedPerShare ?????, esp ????, bvps ????, pb ???, timeToMarket ????, undp ????, perundp ?????, rev ????(%), profit ????(%), gpr ???(%), npr ????(%), holders ???? """ try: basics = ts.get_stock_basics() index = list(basics.index) #???? name = list(basics.name) #???? industry = list(basics.industry) #???? indexLen = len(index) cur1 = conn.cursor() cur1.execute("DROP TABLE IF EXISTS basics;") #???????????? sqlCreate = """create table basics (b_id int comment '??', b_index varchar(100) comment '????', b_name varchar(100) comment '????', b_industry varchar(100) comment '????', PRIMARY key(b_id))""" cur1.execute(sqlCreate) for i in range(0, indexLen): print (i+1, index[i], name[i], industry[i]) cur1.execute( "INSERT INTO basics(b_id, b_index, b_name, b_industry) " "VALUES('%d', '%s', '%s', '%s');" % (i+1, index[i], name[i], industry[i])) cur1.execute('commit;') print ('?????') except Exception: pass
def get_all_company(): df = ts.get_stock_basics() # print(pinyin(stockName)) print("?????") return df
def stat_all(tmp_datetime): # ???? data = ts.get_deposit_rate() common.insert_db(data, "ts_deposit_rate", False, "`date`,`deposit_type`") # ???? data = ts.get_loan_rate() common.insert_db(data, "ts_loan_rate", False, "`date`,`loan_type`") # ?????? data = ts.get_rrr() common.insert_db(data, "ts_rrr", False, "`date`") # ????? data = ts.get_money_supply() common.insert_db(data, "ts_money_supply", False, "`month`") # ?????(????) data = ts.get_money_supply_bal() common.insert_db(data, "ts_money_supply_bal", False, "`year`") # ??????(??) data = ts.get_gdp_year() common.insert_db(data, "ts_gdp_year", False, "`year`") # ??????(??) data = ts.get_gdp_quarter() common.insert_db(data, "ts_get_gdp_quarter", False, "`quarter`") # ?????GDP?? data = ts.get_gdp_for() common.insert_db(data, "ts_gdp_for", False, "`year`") # ?????GDP?? data = ts.get_gdp_pull() common.insert_db(data, "ts_gdp_pull", False, "`year`") # ??????? data = ts.get_gdp_contrib() common.insert_db(data, "ts_gdp_contrib", False, "`year`") # ???????? data = ts.get_cpi() common.insert_db(data, "ts_cpi", False, "`month`") # ????????? data = ts.get_ppi() common.insert_db(data, "ts_ppi", False, "`month`") #############################????? http://tushare.org/fundamental.html # ???? data = ts.get_stock_basics() print(data.index) common.insert_db(data, "ts_stock_basics", True, "`code`") # main????