Python tushare.get_stock_basics() Examples

The following are 19 code examples of tushare.get_stock_basics(). 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: save_tushare.py    From QUANTAXIS with MIT License 6 votes vote down vote up
def QA_save_stock_day_all(client=DATABASE):
    df = ts.get_stock_basics()
    __coll = client.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, start='1990-01-01')

            __coll.insert_many(data_json)
        except Exception as e:
            print(e)
            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') 
Example #2
Source File: Detail_Stock_Selector.py    From anack with GNU General Public License v3.0 6 votes vote down vote up
def stock_select_to_sql(PE,TotalAssists):
    create_stock_select_table()    
    
    df=ts.get_stock_basics()
    #df.to_excel('c:/python/all_stock_list.xlsx')
    df= df[df['pe'] < PE]
    df= df[df['pe'] > 0]
    print(df)
    #df.to_excel('c:/python/all_stock_pe50.xlsx')
    df= df[df['totalAssets'] >= TotalAssists]
    df= df[df['rev'] >= 0]
    df= df[df['profit'] >= 0]
    #df.to_excel('c:/python/all_stock_assets100.xlsx')
    print(df)  
    print('...........................before')
    #df=df.iloc[1:]
    #df.to_excel('c:/python/all_stock_assets100head.xlsx')
    #sql.df_to_mysql('all_stock_select',df)
    df_to_mysql('all_stock_select',df)
    print('...........................after') 
Example #3
Source File: QABacktest_Test.py    From QUANTAXIS with MIT License 6 votes vote down vote up
def __init__(self):
        super().__init__()
        self.frequence = FREQUENCE.DAY
        self.market_type = MARKET_TYPE.STOCK_CN

        # self.stock_basics = QATs.get_stock_basics()
        # self.time_to_Market_300439 = self.stock_basics.loc['300439', 'timeToMarket']
        # self.time_to_Market_300439 = QA.QA_util_date_int2str(self.time_to_Market_300439)
        # self.time_to_day = QA_util_datetime_to_strdate(QA.QA_util_date_today())
        # print(self.time_to_Market_300439)
        # print(self.time_to_day)

        self.time_to_Market_300439 = '2015-04-22'
        self.time_to_day = '2018-05-01'

        self.df_from_Tdx = QA_fetch_stock_day(
            '300439', self.time_to_Market_300439, self.time_to_day, 'pd')
        # print(self.df_from_Tdx)

        self.ma05 = QA_indicator_MA(self.df_from_Tdx, 5)

        self.ma10 = QA_indicator_MA(self.df_from_Tdx, 10)
        self.ma15 = QA_indicator_MA(self.df_from_Tdx, 15)
        self.ma20 = QA_indicator_MA(self.df_from_Tdx, 20)
        # print(self.df5) 
Example #4
Source File: QABacktest_Test.py    From QUANTAXIS with MIT License 6 votes vote down vote up
def run_daybacktest(self):
        #import QUANTAXIS as QA
        # print(QA.QA_fetch_stock_block_adv().code[0:5])
        # self.stock_basics = QATs.get_stock_basics()
        # self.time_to_Market_300439 = self.stock_basics.loc['300439', 'timeToMarket']
        # self.time_to_Market_300439 = QA.QA_util_date_int2str(self.time_to_Market_300439)
        # self.time_to_day = QA_util_datetime_to_strdate(QA.QA_util_date_today())
        # print(self.time_to_Market_300439)
        # print(self.time_to_day)
        # QA.QA_util_time_now()

        self.time_to_Market_300439 = '2015-04-22'
        self.time_to_day = '2018-05-01'

        backtest = Backtest(market_type=MARKET_TYPE.STOCK_CN,
                            frequence=FREQUENCE.DAY,
                            start=self.time_to_Market_300439,
                            end=self.time_to_day,
                            code_list=['300439'],
                            commission_fee=0.00015)
        backtest.start_market()

        backtest.run()
        backtest.stop()
        print("结束回测!") 
Example #5
Source File: Filter_Stock_Cashflow_CHN.py    From StockRecommendSystem with MIT License 5 votes vote down vote up
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") 
Example #6
Source File: data_download.py    From stock with Apache License 2.0 5 votes vote down vote up
def download_stock_basic_info():
    """
    获取股票基本信息
    :return:
    """
    
    try:
        df = ts.get_stock_basics()

        print(df.columns)
        df['code'] = df.index

        print(df.head())
        if len(df):
            engine = db.get_w_engine()
            to_sql(STOCK_BASIC_TABLE, engine, df, type='replace')
            # df.to_sql(STOCK_BASIC_TABLE, engine, if_exists='append', index=False)

        # 添加指数
        indexs = [('sh', '上证指数', '指数','全国','19910715'),
                  ('sz', '深圳成指', '指数','全国','19940720'),
                  ('hs300', '沪深300指数', '指数','全国','20050408'),
                  ('sz50', '上证50指数', '指数','全国','20040102'),
                  ('zxb', '中小板指数', '指数','全国','20050607'),
                  ('cyb', '创业板指数', '指数','全国','20100531'),]
        df = pd.DataFrame(indexs, columns=[KEY_CODE,KEY_NAME, KEY_INDUSTRY, KEY_AREA, KEY_TimeToMarket])
        print(df)
        to_sql(STOCK_BASIC_TABLE, engine, df, type='replace')

           
    except Exception as e:
        print(str(e))

# 获取所有股票的历史K线 
Example #7
Source File: finance.py    From anack with GNU General Public License v3.0 5 votes vote down vote up
def basic_info():
    '''
    获取股票列表
    '''
    return ts.get_stock_basics() 
Example #8
Source File: IndustryEstimation_detail.py    From anack with GNU General Public License v3.0 5 votes vote down vote up
def industry_stat(industry):    
    df = pd.DataFrame(ts.get_stock_basics().values,columns = ts.get_stock_basics().columns)   
    pe_stat = df[df.industry == industry].drop(['name','industry','area'], axis = 1).astype('float')
# =============================================================================
#     print(pe_stat.dtypes)
# =============================================================================
    result_df = pe_stat.describe()
    print(result_df)
    return result_df




#作用:查看行业平均值统计
#输出:所有行业平均统计数(筛选条件:PE <100,pb <10,1000>rev>-1000,1000>profit>-1000,,1000>gpr>-1000,,1000>npr>-1000) 
Example #9
Source File: IndustryEstimation_detail.py    From anack with GNU General Public License v3.0 5 votes vote down vote up
def Estimation(): 
               
    result_df = pd.DataFrame(ts.get_stock_basics().values,columns = ts.get_stock_basics().columns)
    df_to_mysql('industry_estimation_detail',result_df)
    
    return result_df




#作用:查看行业平均值统计
#输入:行业名称
#输出:行业平均统计数 
Example #10
Source File: IndustryEstimation.py    From anack with GNU General Public License v3.0 5 votes vote down vote up
def Get_all_industry_average_data():
    a = ts.get_stock_basics()
    for i in range(0,len(a)):
        print('industry:',a.iloc[i,1])
        test=Estimation(dbconn,a.iloc[i,1],2017)
# App示例代码,用完删掉


#Estimation(dbconn,'家电行业')
#print(GetIndustryName('福耀玻璃')) 
#CreateTable()
#Estimation(dbconn,GetIndustryName('宁沪高速'),2017)  
#Estimation(dbconn,GetIndustryName('格力电器'),2017)   
#Estimation(dbconn,GetIndustryName('福耀玻璃'),2017)   
#Estimation(dbconn,GetIndustryName('隆基股份'),2017)   

#def get_interest_list():
#    '''
#    解析"感兴趣的个股列表.txt",返回list类型的数据供其他模块使用
#    '''
#    list_id = []
#    with open('../SQL/感兴趣的个股列表.txt','r') as fh:
#        s = fh.readline()   #获取更新时间
#        s = fh.readline()   #获取目标长度  
#        
#        lines = fh.readlines()  #获取目标内容
#    for s in lines:
#        code = s[:6]
#        list_id.append(code)    
#    list_id.sort()
#    return list_id  
#
#for s in get_interest_list():
#    Estimation(dbconn,GetIndustryName(s),2017) 
Example #11
Source File: calcu_3year_average_pe.py    From chinese-stock-Financial-Index with Apache License 2.0 5 votes vote down vote up
def calcu_all_stocks_3year_roe_and_average_profit(year):  # 生成3年平均利润列表
    path = os.path.join(current_folder, 'stock_list%s.csv' % today)
    if not os.path.exists(path):
        data = ts.get_stock_basics()
        lie = [
            '名字', '行业', '地区', '市盈率', '流通股本', '总股本', '总资产(万)', '流动资产', '固定资产',
            '公积金', '每股公积金', '每股收益', '每股净资', '市净率', '上市日期', '未分利润', '每股未分配',
            '收入同比(%)', '利润同比(%)', '毛利率(%)', '净利润率(%)', '股东人数'
        ]
        data.columns = lie
        data.index.names = ['代码']
        data = data[data['上市日期'] < three_year_ago()]  # 排除上市不满3年的公司
        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

        data.loc[index, '上4年roe'], data.loc[index, '上3年roe'], data.loc[index, '上2年roe'], \
        data.loc[index, '上1年roe'], data.loc[index, '当年roe'] = last_5_year_roe('%06d' % index, year)
        print('完成%s' % index)
    data.to_csv(
        os.path.join(current_folder, '3年平均利润及其他财务指标%s.csv' % today),
        encoding='utf-8') 
Example #12
Source File: crawler_tushare.py    From Listed-company-news-crawl-and-text-analysis with MIT License 5 votes vote down vote up
def getStockBasicFromTushare(self,dbName,colName):
		db = self._Conn[dbName]
		collection = db.get_collection(colName)
		stock_basic_info = ts.get_stock_basics()
		for i in range(len(stock_basic_info)):
			data = {stock_basic_info.index.name : stock_basic_info.index[i]}
			data.update({'name' : stock_basic_info['name'][i]})
			data.update({'industry' : stock_basic_info['industry'][i]})
			data.update({'area' : stock_basic_info['area'][i]})
			data.update({'pe' : stock_basic_info['pe'][i]})
			data.update({'outstanding' : stock_basic_info['outstanding'][i]})
			data.update({'totals' : stock_basic_info['totals'][i]})
			data.update({'totalAssets' : stock_basic_info['totalAssets'][i]})
			data.update({'liquidAssets' : stock_basic_info['liquidAssets'][i]})
			data.update({'fixedAssets' : stock_basic_info['fixedAssets'][i]})
			data.update({'reserved' : stock_basic_info['reserved'][i]})
			data.update({'reservedPerShare' : stock_basic_info['reservedPerShare'][i]})
			data.update({'esp' : stock_basic_info['esp'][i]})
			data.update({'bvps' : stock_basic_info['bvps'][i]})
			data.update({'pb' : stock_basic_info['pb'][i]})
			data.update({'undp' : stock_basic_info['undp'][i]})
			data.update({'perundp' : stock_basic_info['perundp'][i]})
			data.update({'rev' : stock_basic_info['rev'][i]})
			data.update({'profit' : stock_basic_info['profit'][i]})
			data.update({'gpr' : stock_basic_info['gpr'][i]})
			data.update({'npr' : stock_basic_info['npr'][i]})
			data.update({'holders' : stock_basic_info['holders'][i]})
			#detail = dict(zip(stock_basic_info.columns, [stock_basic_info[j][i] for j in stock_basic_info.columns]))
			collection.insert_one(data) 
Example #13
Source File: QATushare.py    From QUANTAXIS with MIT License 5 votes vote down vote up
def QA_fetch_get_stock_info(name):
    data = ts.get_stock_basics()
    try:
        return data if name == '' else data.loc[name]
    except:
        return None 
Example #14
Source File: save_tushare.py    From QUANTAXIS with MIT License 5 votes vote down vote up
def QA_save_stock_day_with_fqfactor(client=DATABASE):
    df = ts.get_stock_basics()

    __coll = client.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,
                start='1990-01-01',
                if_fq='02',
                type_='pd'
            )
            data_json = QA_util_to_json_from_pandas(data_hfq)
            __coll.insert_many(data_json)
        except Exception as e:
            print(e)
            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 
Example #15
Source File: save_tushare.py    From QUANTAXIS with MIT License 5 votes vote down vote up
def QA_save_stock_day_all_bfq(client=DATABASE):
    df = ts.get_stock_basics()

    __coll = client.stock_day_bfq
    __coll.ensure_index('code')

    def saving_work(i):
        QA_util_log_info('Now Saving ==== %s' % (i))
        try:
            df = QA_fetch_get_stock_day(i, start='1990-01-01', if_fq='bfq')

            __coll.insert_many(json.loads(df.to_json(orient='records')))
        except Exception as e:
            print(e)
            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') 
Example #16
Source File: save_tushare.py    From QUANTAXIS with MIT License 5 votes vote down vote up
def QA_SU_save_stock_info_tushare(client=DATABASE):
    '''
        获取 股票的 基本信息,包含股票的如下信息

        code,代码
        name,名称
        industry,所属行业
        area,地区
        pe,市盈率
        outstanding,流通股本(亿)
        totals,总股本(亿)
        totalAssets,总资产(万)
        liquidAssets,流动资产
        fixedAssets,固定资产
        reserved,公积金
        reservedPerShare,每股公积金
        esp,每股收益
        bvps,每股净资
        pb,市净率
        timeToMarket,上市日期
        undp,未分利润
        perundp, 每股未分配
        rev,收入同比(%)
        profit,利润同比(%)
        gpr,毛利率(%)
        npr,净利润率(%)
        holders,股东人数

        add by tauruswang

    在命令行工具 quantaxis 中输入 save stock_info_tushare 中的命令
    :param client:
    :return:
    '''
    df = QATs.get_stock_basics()
    print(" Get stock info from tushare,stock count is %d" % len(df))
    coll = client.stock_info_tushare
    client.drop_collection(coll)
    json_data = json.loads(df.reset_index().to_json(orient='records'))
    coll.insert(json_data)
    print(" Save data to stock_info_tushare collection, OK") 
Example #17
Source File: Fetch_Data_Stock_CHN_StockList.py    From StockRecommendSystem with MIT License 5 votes vote down vote up
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 
Example #18
Source File: spyder_tushare.py    From quantproject with Apache License 2.0 4 votes vote down vote up
def __init__(self,save=True):
        """
        code,代码
        name,名称
        industry,所属行业
        area,地区
        pe,市盈率
        outstanding,流通股本(亿)
        totals,总股本(亿)
        totalAssets,总资产(万)
        liquidAssets,流动资产
        fixedAssets,固定资产
        reserved,公积金
        reservedPerShare,每股公积金
        esp,每股收益
        bvps,每股净资
        pb,市净率
        timeToMarket,上市日期
        undp,未分利润
        perundp, 每股未分配
        rev,收入同比(%)
        profit,利润同比(%)
        gpr,毛利率(%)
        npr,净利润率(%)
        holders,股东人数
        datatime:获取数据日期
        datatimestramp:获取数据时间戳
        """
        
        try:
            stockBasics = ts.get_stock_basics()
        except:
            stockBasics  = None
            
        if  stockBasics  is not None:
            now = dt.datetime.now()
            stockBasics['datatime'] = now.strftime('%Y-%m-%d')
            stockBasics['datatimestramp'] = now.strftime('%H:%M:%S')
            stockBasics['code'] = stockBasics.index
            indexlist = ['code','datatime']##数据库索引
            tableName = 'SymbolMdm'
            print self.__name__
            database(stockBasics,indexlist,tableName,save) 
Example #19
Source File: basic_job.py    From stock with Apache License 2.0 4 votes vote down vote up
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`")


# 创建新数据库。