def 下载板块数据(self):
     # 获取不同分类数据
     ts.get_industry_classified().to_csv('./股票数据/基本信息/行业分类.csv')
     ts.get_concept_classified().to_csv('./股票数据/基本信息/概念分类.csv')
     ts.get_area_classified().to_csv('./股票数据/基本信息/地域分类.csv')
     ts.get_sme_classified().to_csv('./股票数据/基本信息/中小板.csv')
     ts.get_gem_classified().to_csv('./股票数据/基本信息/创业板.csv')
     ts.get_st_classified().to_csv('./股票数据/基本信息/风险警示板.csv')
     ts.get_hs300s().to_csv('./股票数据/基本信息/沪深300.csv')
     ts.get_sz50s().to_csv('./股票数据/基本信息/上证50.csv')
     ts.get_zz500s().to_csv('./股票数据/基本信息/中证500.csv')
Exemple #2
0
def save_gem_classified():
    '''
    更新创业板分类
    :return:
    '''
    cur = mysql_connect.cursor()
    try:
        sta = cur.execute("delete from py_db.gp_gem_classified")
        print(sta)
        print("删除创业板分类gp_gem_classified:", sta, "条数据完成")
    except Exception as e:
        print(e)

    mysql_connect.commit()
    cur.close()
    df = tushare.get_gem_classified()
    try:
        pd.io.sql.to_sql(df,
                         'gp_gem_classified',
                         yconnect,
                         schema='py_db',
                         if_exists='append',
                         index=df['code'])
        print("创业板分类", len(df), "更新完成")
        return len(df)
    except ValueError as e:
        print(e)
        return False
    return -1
def classification(class_types):
    if class_types == 'industry':
        industry_classified = ts.get_industry_classified()
        industry_classified.to_csv('D:\\ts\\classification\\industry_classified.csv', encoding='gbk')
    elif class_types == 'concept':
        concept_classified = ts.get_concept_classified()
        concept_classified.to_csv('D:\\ts\\classification\\concept_classified.csv', encoding='gbk')
    elif class_types == 'area':
        area_classified = ts.get_area_classified()
        area_classified.to_csv('D:\\ts\\classification\\area_classified.csv', encoding='gbk')
    elif class_types == 'sme':
        sme_classified = ts.get_sme_classified()
        sme_classified.to_csv('D:\\ts\\classification\\sme_classified.csv', encoding='gbk')
    elif class_types == 'gem':
        gem_classified = ts.get_gem_classified()
        gem_classified.to_csv('D:\\ts\\classification\\gem_classified.csv', encoding='gbk')
    elif class_types == 'st':
        st_classified = ts.get_st_classified()
        st_classified.to_csv('D:\\ts\\classification\\st_classified.csv', encoding='gbk')
    elif class_types == 'hs300':
        hs300s = ts.get_hs300s()
        hs300s.to_csv('D:\\ts\\classification\\hs300s.csv', encoding='gbk')
    elif class_types == 'sz50':
        sz50s = ts.get_sz50s()
        sz50s.to_csv('D:\\ts\\classification\\sz50s.csv', encoding='gbk')
    elif class_types == 'zz500':
        zz500s = ts.get_zz500s()
        zz500s.to_csv('D:\\ts\\classification\\zz500s.csv', encoding='gbk')
    elif class_types == 'terminated':
        terminated = ts.get_terminated()
        terminated.to_csv('D:\\ts\\classification\\terminated.csv', encoding='gbk')
    elif class_types == 'suspended':
        suspended = ts.get_suspended()
        suspended.to_csv('D:\\ts\\classification\\suspended.csv', encoding='gbk')
Exemple #4
0
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
Exemple #5
0
    def refresh_stock_list(self):
        engine = self.__db.get_engine()

        logging.info('fetching stock_basics')
        df = ts.get_stock_basics()
        df.to_sql('stock_basics', engine, if_exists='replace', index=True,
                  dtype={'code': String(10)})

        logging.info('fetching class_list_gem')
        gem = ts.get_gem_classified()
        gem.to_sql('class_list_gem', engine, if_exists='replace', index=True,
                   dtype={'code': String(10)})

        logging.info('fetching class_list_hs300')
        hs300 = ts.get_hs300s()
        hs300.to_sql('class_list_hs300', engine, if_exists='replace', index=True,
                     dtype={'code': String(10)})

        logging.info('fetching class_list_sz50')
        sz50 = ts.get_sz50s()
        sz50.to_sql('class_list_sz50', engine, if_exists='replace', index=True,
                    dtype={'code': String(10)})

        logging.info('fetching class_list_zz500')
        zz500 = ts.get_zz500s()
        zz500.to_sql('class_list_zz500', engine, if_exists='replace', index=True,
                     dtype={'code': String(10)})

        self.__db.update_log_time('stock_list_fetch_time')
Exemple #6
0
def get_gme_info():
    # 创业板分类
    df = ts.get_gem_classified()
    res = df.to_sql(all_gme_table, engine, if_exists='replace')
    msg = 'ok' if res is None else res
    print('获取创业板分类: ' + msg + '\n')
    print(msg)
Exemple #7
0
 def core_function(self, func):
     self.set_data()
     mongo = MongoClient("127.0.0.1", 27017)
     if (func == "industry_classified"):
         df = ts.get_industry_classified()
     elif (func == "concept_classified"):
         df = ts.get_concept_classified()
     elif (func == "area_classified"):
         df = ts.get_area_classified()
     elif (func == "gem_classified"):
         df = ts.get_gem_classified()
     elif (func == "sme_classified"):
         df = ts.get_sme_classified()
     elif (func == "st_classified"):
         df = ts.get_st_classified()
     elif (func == "hs300s"):
         df = ts.get_hs300s()
     elif (func == "sz50s"):
         df = ts.get_sz50s()
     elif (func == "zz500s"):
         df = ts.get_zz500s()
         print(df)
     elif (func == "terminated"):
         df = ts.get_terminated()
     else:
         df = {}
     insert_string = df.to_json(orient='records')
     items = json.loads(insert_string)
     coll = mongo.classify[func]
     coll.insert(items)
def get_stockcodes(stocktype):  #stocktype 股票类型:sh 上证 sz 深证 cyb 创业板
    if(stocktype=='cyb'): #创业板
        codes=ts.get_gem_classified().code.tolist()
    elif (stocktype=='sz'): #深证
        codes=ts.get_sme_classified().code.tolist()
    elif(stocktype=='sh'): #上证
        codes=ts.get_sz50s().code.tolist() #目前只调用了上证50
    return codes #返回的股票代码列表
Exemple #9
0
 def get_002_300(self):
     g002 = ts.get_sme_classified()
     g300 = ts.get_gem_classified()
     engine = create_engine(
         'mysql://*****:*****@127.0.0.1/tushareapp?charset=utf8')
     g002.to_sql('g002', engine, if_exists='append')
     time.sleep(60)
     g300.to_sql('g300', engine, if_exists='append')
Exemple #10
0
def get_gem_classified():
    try:
        logger.info('get gem data starting...')
        df = ts.get_gem_classified()
        logger.info('get gem data end...')
    except:
        logger.exception('some errors between get gem data end...')
    return df
def getGemClassified():
    '''
    获取创业板分类。获取创业板股票数据,即查找所有300开头的股票
    参数说明:
    file_path:文件路径,默认为None即由TuShare提供,可以设定自己的股票文件路径。
    返回值说明:
    code:股票代码
    name:股票名称
    '''
    return ts.get_gem_classified()
Exemple #12
0
def fibonacci():
    df_gem = ts.get_gem_classified() #创业板

    for ix, row in df_gem.iterrows():
        code = row['code']
        df_price = dg.get_stock_k_line(code, date_start='2015-01-04')
        print df_price.head()
        returns = df_price['close'].pct_change()
        returns[0] = 0
        print returns
Exemple #13
0
 def get_gem_classified(self):
     """
     创业板分类
     """
     gem = ts.get_gem_classified()
     
     gem['gemName'] = 1
     gem = gem[['code','gemName']]
     gem.columns = ['code','gemName']
     self.data = pd.merge(self.data,gem,on=['code'],how='outer')
Exemple #14
0
def load_data():
    try:
        rs = ts.get_gem_classified()
        pd.DataFrame.to_sql(rs,
                            table_name,
                            con=conn,
                            flavor='mysql',
                            if_exists='replace',
                            index=True)
    except Exception as e:
        print(e.message)
        print("创业板信息数据出错")
Exemple #15
0
def getstockclassifieddata():
    mylogger = getmylogger()
    print("start")
    scdict = {
        'industry': '行业分类',
        'concept': '概念分类',
        'area': '地域分类',
        'sme': '中小板分类',
        'gem': '创业板分类',
        'st': '风险警示板分类',
        'hs300s': '沪深300成份及权重',
        'sz50s': '上证50成份股',
        'zz500s': '中证500成份股',
        'terminated': '终止上市股票列表',
        'suspended': '暂停上市股票列表'
    }

    for sc in scdict:
        sctbname = sc
        scinfo = scdict[sctbname]
        try:
            if sc == "industry":
                df = ts.get_industry_classified()
            elif sc == "concept":
                df = ts.get_concept_classified()
            elif sc == "area":
                df = ts.get_area_classified()
            elif sc == "sme":
                df = ts.get_sme_classified()
            elif sc == "gem":
                df = ts.get_gem_classified()
            elif sc == "st":
                df = ts.get_st_classified()
            elif sc == "hs300s":
                df = ts.get_hs300s()
            elif sc == "zz500s":
                df = ts.get_zz500s()
            elif sc == "sz50s":
                df = ts.get_sz50s()
            elif sc == "terminated":
                df = ts.get_terminated()
            elif sc == "suspended":
                df = ts.get_suspended()
            else:
                mylogger.info("没有执行命令。")
            if df is not None:
                tosql(df, sctbname, "replace", scinfo, mylogger)
            else:
                mylogger.info("没有%s数据。" % scinfo)
        except Exception:
            tracelog = traceback.format_exc()
            mylogger.info("获取数据异常。")
            mylogger.info(tracelog)
Exemple #16
0
def updategem():
    gemdatalist=ts.get_gem_classified()
    gemdata=pd.DataFrame(gemdatalist)
    conn= ms.connect(host='localhost',port = 3306,user='******', passwd='123456',db ='investment',charset="utf8")
    cur = conn.cursor()
    values=[]
    for index,row in gemdata.iterrows():
        values.append((row['code'],row['name']))
    cur.executemany('insert into gem (code,name) values(%s,%s)',values)
    conn.commit()
    cur.close()
    conn.close()
def load_data():
    try:
        rs = ts.get_gem_classified()
        pd.DataFrame.to_sql(rs,
                            name=table_name,
                            con=con,
                            schema=db,
                            if_exists='replace',
                            index=True)
    except Exception as e:
        print(e)
        print("创业板信息数据出错")
def save_gem_classified():
    """获取创业板股票数据,即查找所有300开头的股票"""
    logger.info('Begin get and save gem 创业板股票数据clssified.')
    try:
        data_df = ts.get_gem_classified()
        if data_df is not None and not data_df.empty:
            data = data_df.values
            data_dicts = [{'code': row[0], 'name': row[1]} for row in data]
            GemClassified.insert_many(data_dicts).execute()
            logger.info('Success get and save gem 创业板股票数据classified.')
        else:
            logger.warn('Empty get and save gem 创业板股票数据classified.')
    except Exception as e:
        logger.exception('Error get and save gem 创业板股票数据classified.')
Exemple #19
0
 def list(self, stock_block_type):
     stock_block = None
     if stock_block_type == self.industry:
         stock_block = db.get(STOCK_BLOCK_INDUSTRY)
         if stock_block is None:
             stock_block = ts.get_industry_classified()
             db.save(STOCK_BLOCK_INDUSTRY, stock_block)
     elif stock_block_type == self.concept:
         stock_block = db.get(STOCK_BLOCK_CONCEPT)
         if stock_block is None:
             stock_block = ts.get_concept_classified()
             db.save(STOCK_BLOCK_CONCEPT, stock_block)
     elif stock_block_type == self.area:
         stock_block = db.get(STOCK_BLOCK_AREA)
         if stock_block is None:
             stock_block = ts.get_area_classified()
             db.save(STOCK_BLOCK_AREA, stock_block)
     elif stock_block_type == self.sme:
         stock_block = db.get(STOCK_BLOCK_SME)
         if stock_block is None:
             stock_block = ts.get_sme_classified()
             db.save(STOCK_BLOCK_SME, stock_block)
     elif stock_block_type == self.gem:
         stock_block = db.get(STOCK_BLOCK_GEM)
         if stock_block is None:
             stock_block = ts.get_gem_classified()
             db.save(STOCK_BLOCK_GEM, stock_block)
     elif stock_block_type == self.st:
         stock_block = db.get(STOCK_BLOCK_ST)
         if stock_block is None:
             stock_block = ts.get_st_classified()
             db.save(STOCK_BLOCK_ST, stock_block)
     elif stock_block_type == self.hs300s:
         stock_block = db.get(STOCK_BLOCK_HS300S)
         if stock_block is None:
             stock_block = ts.get_hs300s()
             db.save(STOCK_BLOCK_HS300S, stock_block)
     elif stock_block_type == self.sz50s:
         stock_block = db.get(STOCK_BLOCK_SZ50S)
         if stock_block is None:
             stock_block = ts.get_sz50s()
             db.save(STOCK_BLOCK_SZ50S, stock_block)
     elif stock_block_type == self.zz500s:
         stock_block = db.get(STOCK_BLOCK_ZZ500S)
         if stock_block is None:
             stock_block = ts.get_zz500s()
             db.save(STOCK_BLOCK_ZZ500S, stock_block)
     else:
         return None
     return stock_block
Exemple #20
0
def update_gem(request):
    '''更新创业板'''
    stock_gem.objects.all().delete()

    rltobj = ts.get_gem_classified()
    objlist = []
    for i in range(len(rltobj)):
        tmpobj = stock_gem(code=rltobj.loc[i]['code'],
                           name=rltobj.loc[i]['name'])
        objlist.append(tmpobj)

    stock_gem.objects.bulk_create(objlist)

    return HttpResponse('succ.{0}条!'.format(len(objlist)))
Exemple #21
0
def getCYCodeList():
    df_cy = None
    try:
        df_cy = pd.read_sql_table('cy', con=engine)
    except:
        pass
    if df_cy is None or df_cy.empty:
        df_cy = ts.get_gem_classified()
        df_cy.to_sql('cy',
                     con=engine,
                     if_exists='replace',
                     index=False,
                     index_label='code')
    return df_cy['code'].tolist()
Exemple #22
0
def gme_classified(request):
    try:
        data = ts.get_gem_classified()

        res = {
            'columns': [
                '股票代码',
                '股票名称',
            ],
            'data': json.loads(json.dumps(data.values, cls=DateEncoder))
        }
    except (KeyError):
        return HttpResponse(KeyError)
    else:

        return HttpResponse(json.dumps(res), content_type="application/json")
def get_stock_codes(markets=['zz500s'], ipo_date=None):
    '''
    markets: list
    e.g.['sme','gem','st','hs300s','sz50s','zz500s','general'];
    ipo_date: str
    e.g.'2015-03-30'
    '''
    code_list = []
    if 'sme' in markets:  #中小板
        df = ts.get_sme_classified()
        codes = list(df.code)
        code_list.extend(codes)
    if 'gem' in markets:  #创业板
        df = ts.get_gem_classified()
        codes = list(df.code)
        code_list.extend(codes)
    if 'st' in markets:  #风险板
        df = ts.get_st_classified()
        codes = list(df.code)
        code_list.extend(codes)
    if 'hs300s' in markets:  #沪深300
        df = ts.get_hs300s()
        codes = list(df.code)
        code_list.extend(codes)
    if 'sz50s' in markets:  #上证50
        df = ts.get_sz50s()
        codes = list(df.code)
        code_list.extend(codes)
    if 'zz500s' in markets:  #中证500
        df = ts.get_zz500s()
        codes = list(df.code)
        code_list.extend(codes)

    if ipo_date:
        new_stock_df = ts.new_stocks()
        new_stock_df = new_stock_df[new_stock_df['ipo_date'] > ipo_date]
        new_stock_codes = list(new_stock_df.code)
        #得到输入时间之后发行的股票

        code_list = list(set(code_list))
        desired_codes = list(set(code_list) - set(new_stock_codes))
    #剔除新股

    desired_codes = list(set(code_list))

    return desired_codes
Exemple #24
0
def updategem():
    gemdatalist = ts.get_gem_classified()
    gemdata = pd.DataFrame(gemdatalist)
    conn = ms.connect(host='localhost',
                      port=3306,
                      user='******',
                      passwd='123456',
                      db='investment',
                      charset="utf8")
    cur = conn.cursor()
    values = []
    for index, row in gemdata.iterrows():
        values.append((row['code'], row['name']))
    cur.executemany('insert into gem (code,name) values(%s,%s)', values)
    conn.commit()
    cur.close()
    conn.close()
 def get_stocklist_by_type(self, trade_date, type):
     print 'get_stocklist_by_type-------' + type
     df = None
     if type == '000016.SH':
         df = ts.get_sz50s()
     elif type == '000300.SH':
         df = ts.get_hs300s()
     elif type == '399006.SZ':
         df = ts.get_gem_classified()
     elif type == '000905.SH':
         df = ts.get_zz500s()
     print '----------------------------------------------------------'
     print df
     print '----------------------------------------------------------'
     if not df is None:
         stocklist = df.code.tolist()
     else:
         stocklist = []
     return stocklist
Exemple #26
0
 def preload():
     stock_block = ts.get_industry_classified()
     db.save(STOCK_BLOCK_INDUSTRY, stock_block)
     stock_block = ts.get_concept_classified()
     db.save(STOCK_BLOCK_CONCEPT, stock_block)
     stock_block = ts.get_area_classified()
     db.save(STOCK_BLOCK_AREA, stock_block)
     stock_block = ts.get_sme_classified()
     db.save(STOCK_BLOCK_SME, stock_block)
     stock_block = ts.get_gem_classified()
     db.save(STOCK_BLOCK_GEM, stock_block)
     stock_block = ts.get_st_classified()
     db.save(STOCK_BLOCK_ST, stock_block)
     stock_block = ts.get_hs300s()
     db.save(STOCK_BLOCK_HS300S, stock_block)
     stock_block = ts.get_sz50s()
     db.save(STOCK_BLOCK_SZ50S, stock_block)
     stock_block = ts.get_zz500s()
     db.save(STOCK_BLOCK_ZZ500S, stock_block)
Exemple #27
0
def get_universe(symbol):
    '''
    获取**当前**全A、指定板块、指数、ST的成分股代码。
    
    Parameters
    -----------
        symbol 获取类型
    Returns
    ----------
        list [ticker,...]
    Notes
    ---------
    'A' 
        全A股
    'st' 
        st股票
    'hs300' 
        沪深300成分股
    'cyb' 
        创业板成分股
    'sz50' 
        上证50成分股
    'A-st' 
        剔除st股票后的全A股
    
    '''
    if symbol == 'A':
        return ts.get_stock_basics().index.values.tolist()
    if symbol == 'st':
        return ts.get_st_classified()['code'].values.tolist()
    if symbol == 'hs300':
        return ts.get_hs300s()['code'].values.tolist()
    if symbol == 'cyb':
        return ts.get_gem_classified()['code'].values.tolist()
    if symbol == 'sz50':
        return ts.get_sz50s()['code'].values.tolist()
    if symbol == 'A-st':
        A = set(ts.get_stock_basics().index.values.tolist())
        ST = set(ts.get_st_classified()['code'].values.tolist())
        for st in ST:
            A.discard(st)        
        return list(A)
Exemple #28
0
def initial_pool():
   concept_df = ts.get_concept_classified()
   for idx in concept_df.index:
       if(concept_df['c_name'][idx] == "汽车电子"):
           a_concept_list.append(concept_df['code'][idx])
   
   print("汽车电子 concept is ")
   #concept_df.to_csv('concept.csv')
   
   industry_df = ts.get_industry_classified()
   for idx in industry_df.index:
       if(concept_df['c_name'][idx] == "电子器件"):
           a_concept_list.append(concept_df['code'][idx])
       if(concept_df['c_name'][idx] == "电子信息"):
           a_concept_list.append(concept_df['code'][idx])
   print(a_concept_list)
   # print(industry_df)
   #industry_df.to_csv('industry.csv')
   # small and chuangye
   try:
       startup_df = ts.get_gem_classified()
       sm_df = ts.get_sme_classified()
       basic_df = ts.get_stock_basics()
   except:
       traceback.print_exc()
       pass
   print(startup_df)
   
   print("start to parse startup stocks\n") ;
   
   for idx in startup_df.index:
   	pe = basic_df['pe'][startup_df['code'][idx]]
   	if(pe < 80 and pe != 0):
                   startup_pool.append(startup_df['code'][idx])
   #                print(str(startup_df['code'][idx])+" pe is "+str(pe))
   
   print(startup_pool)
   print("start to parse small and middle stocks\n") ;
   for idx in sm_df.index:
   	pe = basic_df['pe'][sm_df['code'][idx]]
   	if(pe < 50 and pe != 0):
                   sm_pool.append(sm_df['code'][idx])
Exemple #29
0
def get_stockcode_list(dataset=None, update=False):

    filepath = dataset + '.csv'
    codefilepath = os.path.join(rootDir, 'codelist', filepath)

    if (os.path.exists(codefilepath) and update == False):
        codelist = pd.read_csv(codefilepath, encoding='gbk')
        return codelist

    if (dataset == 'zxb'):
        codelist = ts.get_sme_classified()
        codelist = codelist[['code', 'name']]
        codelist.to_csv(codefilepath)
        return codelist
    elif (dataset == 'cyb'):
        codelist = ts.get_gem_classified()
        codelist = codelist[['code', 'name']]
        codelist.to_csv(codefilepath)
        return codelist
    elif (dataset == 'hs300'):
        codelist = ts.get_hs300s()
        codelist = codelist[['code', 'name']]
        codelist.to_csv(codefilepath)
        return codelist
    elif (dataset == 'sz50'):
        codelist = ts.get_sz50s()
        codelist = codelist[['code', 'name']]
        codelist.to_csv(codefilepath)
        return codelist
    elif (dataset == 'zz500'):
        codelist = ts.get_zz500s()
        codelist = codelist[['code', 'name']]
        codelist.to_csv(codefilepath)
        return codelist
    elif (dataset == 'whole'):
        codelist = ts.get_today_all()
        codelist = codelist[['code', 'name']]

        codelist.to_csv(codefilepath)

        return codelist
Exemple #30
0
def getStockClassfied():
    filepath = 'f:\\stockdata\\股票分类数据\\'
    index = 0
    # 行业分类
    df = ts.get_industry_classified()
    saveCsv(df, filepath, stockClassList, index)
    index += 1

    # 概念分类
    df = ts.get_concept_classified()
    saveCsv(df, filepath, stockClassList, index)
    index += 1

    # 地域分类
    df = ts.get_area_classified()
    saveCsv(df, filepath, stockClassList, index)
    index += 1
    # 中小板分类
    df = ts.get_sme_classified()
    saveCsv(df, filepath, stockClassList, index)
    index += 1
    # 创业板分类
    df = ts.get_gem_classified()
    saveCsv(df, filepath, stockClassList, index)
    index += 1
    # 风险警示板分类
    df = ts.get_st_classified()
    saveCsv(df, filepath, stockClassList, index)
    index += 1
    # 沪深300成份及权重
    df = ts.get_hs300s()
    saveCsv(df, filepath, stockClassList, index)
    index += 1
    # 上证50成份股
    df = ts.get_sz50s()
    saveCsv(df, filepath, stockClassList, index)
    index += 1
    # 中证500成份股
    df = ts.get_zz500s()
    saveCsv(df, filepath, stockClassList, index)
    index += 1
Exemple #31
0
def get_gem():
    '''
    函数功能:
    --------
    获取所有创业板上市的公司。

    输入参数:
    --------
    无

    输出参数:
    --------
    DataFrame
        code,代码
        name,名称
    '''
    # Download data
    gem = ts.get_gem_classified()

    # Return Dataframe
    return gem
Exemple #32
0
def choose_code(num=300):
    """分别从中小板,沪深300中随机选择相等数量的股票代码,默认分别选择150个

    Args:
        num: 股票的选择数量,默认为300

    Returns:
        返回随机选择的股票列表
    """
    if not os.path.exists(code_file):
        json.dump([], open(code_file, "w"))

    if os.path.exists(code_file):
        lis = json.load(open(code_file))
        if len(lis) == num:
            return lis

    # 最终股票代码列表
    code_lis = []

    # 获取中小板数据
    zxb_df = ts.get_gem_classified()
    zxb_lis = list(zxb_df.code)

    # 获取沪深三百
    hs300_df = ts.get_hs300s()
    hs300_lis = list(hs300_df.code)

    # 依次从中小板,沪深300中随机选取 num/2支股票代码
    zxb_rand = random.sample(zxb_lis, int(num / 2))
    hs300_rand = random.sample(hs300_lis, int(num / 2))

    # 保存到code_lis并保存
    code_lis.extend(zxb_rand)
    code_lis.extend(hs300_rand)
    with open(code_file, "w") as wf:
        json.dump(code_lis, wf)

    return code_lis
Exemple #33
0
time.sleep(1)
# 概念分类
concepts = ts.get_concept_classified()
concepts.columns = ('code', 'name', 'concept')
time.sleep(1)
# 地域分类
areas = ts.get_area_classified()
areas.columns = ('code', 'name', 'area')

time.sleep(1)
# 中小板块
smalls = ts.get_sme_classified() 

time.sleep(1)
# 创业版
news = ts.get_gem_classified()

time.sleep(1)
# st版块
sts = ts.get_st_classified()

time.sleep(1)
# 沪深300
hss = ts.get_hs300s()

time.sleep(1)
# 上证50
szs = ts.get_sz50s()

time.sleep(1)
# 中证500
def get_gem():
    """获取创业板股票数据,即查找所有300开头的股票"""
    df = ts.get_gem_classified()
    print(df)
Exemple #35
0
 def setGmeClassified(self, isSave=False, tableName=CLASSIFIED_GME):
     df = ts.get_gem_classified()
     if isSave is True:
         df.to_sql(tableName, self.engine_sql, if_exists='append')
     return df
Exemple #36
0
# -*- coding: utf-8 -*-
"""
Created on Mon Apr 11 09:57:55 2016

@author: warriorzhai
"""

import tushare as ts
import os
import pymongo


def df_todict(df):
    d={}
    for colname in df:
        d[colname]=df[colname].tolist()
    return d

data=ts.get_gem_classified().ix[:5]
data=df_todict(data)
client = pymongo.MongoClient('mongodb://*****:*****@ds011281.mlab.com:11281/stkdb')
db = client.stkdb
collection = db.test
collection.insert_one(data)

def get_gem_info(file_path):
    gem_info = ts.get_gem_classified()
    gem_info.to_csv(file_path, encoding='utf-8')
    print '\ndownload gem info finished\n'
Exemple #38
0
def load_company_industry_info(): 
    #下载加载行业分类数据
    try:
        rs=ts.get_industry_classified()
        sql.write_frame(rs, "company_industry_classified", con=conn_company_classified , flavor='mysql', if_exists='replace',index=True)
        print("下载公司行业分类信息ok")
    except:
        print("下载公司行业分类信息出错")
    #下载加载概念分类数据
    try:
        rs=ts.get_concept_classified()
        sql.write_frame(rs, "company_concept_classified", con=conn_company_classified , flavor='mysql', if_exists='replace',index=True)
        print("载公司概念分类信息ok")
    except:
        print("下载公司概念分类信息出错")
    #下载加载地域分类数据
    try:
        rs=ts.get_area_classified()
        sql.write_frame(rs, "company_area_classified", con=conn_company_classified , flavor='mysql', if_exists='replace',index=True)
        print("下载公司区域分类信息ok")
    except:
        print("下载公司区域分类信息出错")
    #下载加载中小板分类数据
    try:
        rs=ts.get_sme_classified()
        sql.write_frame(rs, "company_sme_classified", con=conn_company_classified , flavor='mysql', if_exists='replace',index=True)
        print("下载中小板分类数据ok")
    except:
        print("下载中小板分类数据出错")
    #下载加载创业板分类数据
    try:
        rs=ts.get_gem_classified()
        sql.write_frame(rs, "company_gem_classified", con=conn_company_classified , flavor='mysql', if_exists='replace',index=True)
        print("下载创业板分类数据ok")
    except:
        print("下载创业板分类数据出错")
    #下载加载st板分类数据
    try:
        rs=ts.get_st_classified()
        sql.write_frame(rs, "company_st_classified", con=conn_company_classified , flavor='mysql', if_exists='replace',index=True)
        print("下载st板分类数据ok")
    except:
        print("下载st板分类数据出错")
    #下载加载沪深300板分类数据
    try:
        rs=ts.get_hs300s()
        sql.write_frame(rs, "company_hs300_classified", con=conn_company_classified , flavor='mysql', if_exists='replace',index=True)
        print("下载加载沪深300板分类数据ok")
    except:
        print("下载加载沪深300板分类数据出错")
    #下载加载上证50板分类数据
    try:
        rs=ts.get_sz50s()
        sql.write_frame(rs, "company_sz50_classified", con=conn_company_classified , flavor='mysql', if_exists='replace',index=True)
        print("下载加载上证50板分类数据ok")
    except:
        print("下载加载上证50板分类数据出错")
    #下载加载中证500板分类数据
    try:
        rs=ts.get_zz500s()
        sql.write_frame(rs, "company_zz500_classified", con=conn_company_classified , flavor='mysql', if_exists='replace',index=True)
        print("下载加载中证500板分类数据ok")
    except:
        print("下载加载中证500板分类数据出错")
    #下载加载终止上市分类数据
    try:
        rs=ts.get_terminated()
        sql.write_frame(rs, "company_terminated_classified", con=conn_company_classified , flavor='mysql', if_exists='replace',index=True)
        print("下载加载终止上市分类数据ok")
    except:
        print("下载加载终止上市分类数据出错")
    #下载加载暂停上市分类数据
    try:
        rs=ts.get_suspended()
        sql.write_frame(rs, "company_suspended_classified", con=conn_company_classified , flavor='mysql', if_exists='replace',index=True)
        print("下载加载暂停上市分类数据ok")
    except:
        print("下载加载暂停上市分类数据出错")
Exemple #39
0
def get_new_history():
    news = ts.get_gem_classified()
    return get_data_by_column(news)
Exemple #40
0

# Merge
#StockInfo1 = StockInfo.join(Concept, how = 'outer')

# get all stocks
StockName = StockInfo['name']
StockName.to_csv('./ASHR/DATA/StockName.csv', index = True, header = True)

# Small & Medium Enterprise
# Note SME is a pd.series data type
SME = ts.get_sme_classified()
SME.to_csv('./ASHR/DATA/SME.csv', index = False)

# Growth Enterprise Market
GEM = ts.get_gem_classified()
GEM.to_csv('./ASHR/DATA/GEM.csv', index = False)

# ST Enterprise
ST = ts.get_st_classified()
ST.to_csv('./ASHR/DATA/ST.csv', index = False)

ts.get_h_data()

# HS 300
HS300S = ts.get_hs300s()
HS300S.to_csv('./ASHR/DATA/HS300S.csv', index = False)

# SZ 50
SZ50S = ts.get_sz50s()
SZ50S.to_csv('./ASHR/DATA/SZ50S.csv', index = False)
Exemple #41
0
Created on 2015年6月4日

@author: Administrator
'''
import tushare as ts
# 行业分类
ts.get_industry_classified()

# 概念分类
ts.get_concept_classified()

# 地域分类
ts.get_area_classified()

# 获取沪深300当前成份股及所占权重
ts.get_hs300s()

# 获取中小板股票数据,即查找所有002开头的股票
ts.get_sme_classified()

# 获取创业板股票数据,即查找所有300开头的股票
ts.get_gem_classified()

# 获取风险警示板股票数据,即查找所有st股票
ts.get_st_classified()

# 获取上证50成份股
ts.get_sz50s()

# 获取中证500成份股
ts.get_zz500s()