Example #1
0
def fetch_magic_candidators(engine, t_day):
    # 中证价值回报量化策略指数的样本空间由满足以下条件的沪深 A 股构成:
    # (1)非 ST、*ST 股票,非暂停上市股票; (2)非金融类股票。

    all_stocks = list(jq.get_all_securities(types=['stock'], date=t_day).index)
    #all_stocks = all_stocks[:100]

    # 排除金融类的股票
    banks = jq.get_industry_stocks('J66', date=t_day)
    brokers = jq.get_industry_stocks('J67', date=t_day)
    insurances = jq.get_industry_stocks('J68', date=t_day)
    others = jq.get_industry_stocks('J69', date=t_day)
    exclude = banks + brokers + insurances + others

    filtered_1 = []

    for code in all_stocks:
        if (code in exclude):
            print "  ... %s 是金融类, 排除..." % code
            continue

        filtered_1.append(code)

    # 排除 ST
    st = jq.get_extras('is_st',
                       filtered_1,
                       start_date=t_day,
                       end_date=t_day,
                       df=False)

    filtered_2 = []
    for code in filtered_1:
        if st[code][0]:
            print "  ...  %s 是ST,排除" % code
            continue

        filtered_2.append(code)

    filtered_3 = []
    # 排除停牌
    for code in filtered_2:
        if data_fetcher.is_paused(engine, code, t_day):
            print "  ...  %s 停牌,排除" % code
            continue

        filtered_3.append(code)

    return filtered_3
Example #2
0
def predict(train_cfg):
    with open(train_cfg,"r") as f:
        train_cfg = json.load(f)

    feature_cfg = train_cfg["feature_cfg"]
    model_name = train_cfg["model"]
    model_cfg = train_cfg["model_cfg"]

    #feature_cfg = "./config/feature_create_cfg.json"
    with open(feature_cfg,"r") as f:
        feature_cfg = json.load(f)

    stock_list = jq.get_industry_stocks('I64')
    feature_creator = Feature(feature_cfg,stock_list)
    #label_creator = Label(feature_cfg,stock_list)
    model = restore_model_from_file(model_name,model_cfg)

    if not buffered_feature:
        feature = []
        label = []
        for date in feature_creator.date_list:
            print ("creating feature, date:",date)
            feature_per_day = feature_creator.creatFeatureByDate(date)
            feature.append(feature_per_day)

        feature = np.array(feature).reshape(-1,feature[0].shape[-1])
        print (feature.shape)
        np.save("./DATA/buffer/feature_for_prediction.npy",feature)
    else:
        feature = np.load("./DATA/buffer/feature_for_prediction.npy")



    y = model.predict(feature)
    print (y.shape)
Example #3
0
    def record(self, entity, start, end, size, timestamps):
        try:
            industry_stocks = get_industry_stocks(entity.code, date=now_pd_timestamp())
        except:
            industry_stocks = get_concept_stocks(entity.code, date=now_pd_timestamp())
        if len(industry_stocks) == 0:
            return None
        df = pd.DataFrame({"stock": industry_stocks})
        df["stock_id"] = df.stock.apply(lambda x: to_entity_id(x, "stock"))
        df["stock_code"] = df.stock_id.str.split("_", expand=True)[2]
        df["stock_name"] = df.stock_id.apply(lambda x:get_data(data_schema=Stock, entity_id=x, provider='joinquant').name)
        df["block_type"] = entity.block_type
        df["code"] = entity.code
        df["name"] = entity.name
        df["exchange"] = entity.exchange
        df["timestamp"] = now_pd_timestamp()
        df["entity_id"] = entity.id
        df["entity_type"] = "block"
        df["id"] = df.apply(lambda x: x.entity_id + "_" + x.stock_id, axis=1)
        if df.empty:
            return None
        df_to_db(data_schema=self.data_schema, df=df, provider=self.provider,
                 force_update=True)

        self.logger.info('finish recording BlockStock:{},{}'.format(entity.category, entity.name))
Example #4
0
def fetch_fundamentals_1_year_300(engine, the_year):
    #
    # 沪深300 样本股调整实施时间分别是每年 6 月和 12 月的第二个星期五的下一交易日
    # 我们假设每年五月(年报出来)换仓
    print "fetch fetch_fundamentals for %d" % the_year

    yyyymmdd = "%d-05-01" % the_year
    # 取当年沪深300成份
    hs300 = jq.get_index_stocks('000300.XSHG', date=yyyymmdd)
    #print hs300

    #J66 货币金融服务    1991-04-03
    #J67 资本市场服务    1994-01-10
    #J68 保险业  2007-01-09
    #J69 其他金融业

    # 排除不适用魔法公式的行业
    banks = jq.get_industry_stocks('J66', date=yyyymmdd)
    brokers = jq.get_industry_stocks('J67', date=yyyymmdd)
    insurances = jq.get_industry_stocks('J68', date=yyyymmdd)
    others = jq.get_industry_stocks('J69', date=yyyymmdd)
    exclude = banks + brokers + insurances + others

    #print banks
    #print insurances

    for code in hs300:
        if (code in exclude):
            print "  ... skip %s ..." % code
            continue

        print "  fetching %s" % code
        fetch_target_stock_fundamentals(engine, code, the_year)
        #break

    print "finished fetching fetch_fundamentals for %d" % the_year
    print
def get_industry_stocks():
    df = jq.get_industries(name='zjw')

    s2i = {}

    row_num = len(df.index)
    for i in range(row_num):
        industry_code = df.index[i]

        industry_name = df.iloc[i]['name']

        #print industry_code, industry_name

        stocks = jq.get_industry_stocks(industry_code)

        for stock_code in stocks:
            if stock_code in s2i:
                s2i[stock_code].append(industry_name)
            else:
                s2i[stock_code] = [industry_name]

    return s2i
Example #6
0
        daily_label_info = {"info":label,"valid_index":valid_index}
        return daily_label_info

    def creatDailyLabel(self,date,stock_list):
        daily_label_dict = dict()
        daily_label_dict["label_all"] = dict()
        daily_label_dict["label_all"][date] = self.creatLabelByDate(date,stock_list)
        daily_label_dict["date_index"] = {0:date}
        return daily_label_dict

    def checkLabel():
        for creator in self.label_creator_list:
            creator.check(self.start_date)


if __name__ == "__main__":
    import json
    from util.jq_init import login
    login()

    from data_interface.data_api import UserDataApi
    UserDataApi = UserDataApi()
    feature_cfg = "./config/feature_create_cfg.json"
    with open(feature_cfg,"r") as f:
        feature_cfg = json.load(f)

    stock_list = jq.get_industry_stocks('I64')

    f = Label(feature_cfg,UserDataApi)
    label_all = f.createLabelAll(stock_list)
    print (label_all["label_all"]["2020-03-04"]["valid_index"])
Example #7
0
    3.然后观察2021年的市盈率表现
"""
#设置benchmark_date
benchmark_date = '2020-12-31'
# 设置股票范围,000018.XSHG 上证180金融股,XSHG 上海证券交易所
#pool = jq.get_index_stocks('000018.XSHG')

# 获取金融行业板块股票
"""
pool1 = jq.get_industry_stocks('J69', date=benchmark_date)
pool2 = jq.get_industry_stocks('J66', date=benchmark_date)
# 得到所有金融业的股票
pool = pool1 + pool2
"""
# 获取计算机行业板块股票
pool = jq.get_industry_stocks('C39', date=benchmark_date)
pool


# In[19]:


q = jq.query(jq.valuation.code, jq.valuation.pe_ratio, jq.valuation.market_cap)    .filter(jq.valuation.pe_ratio > 0, jq.valuation.code.in_(pool))    .order_by(jq.valuation.pe_ratio.asc())
q
df = jq.get_fundamentals(q)
df


# In[20]:

# encoding=utf-8
"""
本脚本用来分析一个行业中龙头股与非龙头股的走势区别
"""

# from JQData_Test.JQ_Industry_Analysis_Sub import plt,jqdatasdk,np
# from JQData_Test.auth_info import *
import jqdatasdk
import numpy as np
from pylab import *

# 下载医药板块的所有stk
stk_list_yiyao = jqdatasdk.get_industry_stocks(industry_code='801150')

# 下载该板块所有stk的close数据
df_yiyao = jqdatasdk.get_price(stk_list_yiyao,
                               start_date='2010-01-01',
                               end_date='2018-10-01',
                               frequency='daily')
df_yiyao_close = df_yiyao['close']

# 龙头股与非龙头股
longtou = ['601607.XSHG', '000623.XSHE']
nolongtou = [x for x in stk_list_yiyao if x not in longtou]

# 非龙头每支stk起始投资1000块
for stk in nolongtou:
    df_yiyao_close[stk + '_std'] = df_yiyao_close.apply(
        lambda x: x[stk] * (1000 / df_yiyao_close.head(1)[stk].values[0]),
        axis=1)
Example #9
0
 def get_industry_stocks(self, industry_id: str):
     return jq.get_industry_stocks(industry_id)
Example #10
0
def fetch_brk_candidators(engine,t_day):

    brokers    = jq.get_industry_stocks( 'J67' , date= t_day )

    return brokers