Пример #1
0
 def insert_mongo(self, stockid, stock_concept, id_v):
     stock_name = common.QueryStockMap(id=stockid)[0]
     self.mongodb.stock.ycj_concept.insert({
         "stockid": stockid,
         "concept": stock_concept,
         "stockname": stock_name,
         "id": str(id_v)
     })
Пример #2
0
 def queryField(self, fieldName):
     print "######## look up #########################################################"
     targets = self.query[fieldName]
     names = [(common.QueryStockMap(id=x))[0] for x in targets]
     names = " ".join(names)
     print "%s\n%s" % (fieldName, names)
     print targets
     print "#########################################################################\n"
Пример #3
0
 def add_L1_element(self, file_dir=u'D:/Money/modeResee/复盘', file_name = u'insert_L1.csv', date=datetime.datetime.today().strftime("%Y%m%d")):
     print "Begin to insert L1 alone using file: %s" % os.path.join(file_dir, file_name)
     dframe = pd.read_csv(os.path.join(file_dir, file_name), encoding='gbk')
     for idx in dframe.index.values:
         print idx
         concept = dframe.loc[idx, 'group']  # 概念
         stockids = [common.QueryStockMap(name=dframe.loc[idx, 'name'])[1]]
         try:
             if str(dframe.loc[idx, 'anotation']) == 'nan' or len(str(dframe.loc[idx, 'anotation'])) < 2:
                 anotation_stockids = []
             else:
                 anotations = dframe.loc[idx, 'anotation'].replace(u' ', u'').replace(u',', u',').split(u',')
                 anotation_stockids = [common.QueryStockMap(name=x)[1] for x in anotations]
         except Exception, err:
                 print "force to set the anotation stockids to be empty for concept:%s" %concept
                 anotation_stockids = []
         stockids.extend(anotation_stockids)     # 该行中,概念相关的股票
         for stockid in stockids:
             self.insertCache(stockid, concept, date, cache=1)     # 往L2中添加股票及对应的概念
Пример #4
0
 def update_L1_from_generated_csv(self, csv_file, date=datetime.datetime.today().strftime("%Y%m%d")):
     dframe = pd.read_csv(csv_file, encoding='gbk')
     dframe.fillna(value=0, inplace=True)
     for idx in dframe.index.values:
         stockid = dframe.loc[idx, 'stockid']
         stockid = common.regulize_stockid(stockid)
         concept = dframe.loc[idx, 'concept']
         stockname = dframe.loc[idx, 'stockname']
         if_delete = dframe.loc[idx, 'if_delete']
         # 如果为1,则删除
         if if_delete:
             self.removeCache(stockid, concept, cache=1)
             print "Delete L1 element, concept:%s, stockid:%s, stockname:%s" % (concept, stockid, stockname)
         else:
             # 如果该行值不在L1,则插入
             if not common.exist_in_cache(stockid, concept, 1):
                 stockname = common.QueryStockMap(id=stockid)[0]
                 self.insertCache(stockid, concept, date)
                 print "Insert new L1 element, concept:%s, stockid:%s, stockname:%s" % (concept, stockid, stockname)
     print "Update L1 cache from L1.csv under each day finished!"
Пример #5
0
 def updateAnotationL2CsvByDate(self, date):
     # 读入daydayup.csv
     dframe = pd.read_csv(os.path.join(self.backsee_dir, u"%s/daydayup.csv" % date), encoding='gbk')
     dframe.dropna(subset= ['group'], inplace=True)
     for idx in dframe.index.values:
         concept = dframe.loc[idx, 'group']  # 概念
         # stockids = [dframe.loc[idx, 'stock']]
         stockids = []       # 不更新csv中stock列, 在另一个函数中更新
         try:
             if str(dframe.loc[idx, 'anotation']) == 'nan' or len(str(dframe.loc[idx, 'anotation'])) < 2:
                 anotation_stockids = []
             else:
                 anotations = dframe.loc[idx, 'anotation'].replace(u' ', u'').replace(u',', u',').split(u',')
                 anotation_stockids = [common.QueryStockMap(name=x)[1] for x in anotations]
         except Exception, err:
             print "force to set the anotation stockids to be empty for concept:%s" %concept
             anotation_stockids = []
         stockids.extend(anotation_stockids)     # 该行中,概念相关的股票
         print "updating concept of:%s ..." %concept
         for stockid in stockids:
             self.insertCache(stockid, concept, date, cache=2)     # 往L2中添加股票及对应的概念
Пример #6
0
 def get_zt_details(self):
     # '''
     # 临时debug
     # '''
     # self.hiList = ['603108', '000929']
     # self.hiList = ['000672']
     # self.ztList = []
     # self.ztList = ['603819', '603987', '000422', '000022']
     targetlist = []
     targetlist.extend(self.ztList)
     targetlist.extend(self.hiList)
     closeZt = {}
     openZt = {}
     for stock in targetlist:
         closetime, nonstable_time = common.zt_time_details(stock, self.day)
         stock_name = common.QueryStockMap(stock)[0]
         if stock_name == "":
             stock_name = stock
         self.add_to_dict(closeZt, closetime, stock_name)
         self.add_to_dict(openZt, nonstable_time, stock_name)
     return closeZt, openZt
Пример #7
0
def study_plot(tmp_array,dirs, name_tail = u'', end_date_interval = 0, start_date_interval = 0):

    ## 日线dir
    daydir = dirs
    minsdir = "%s/mins"%dirs
    bothdir = "%s/both"%dirs

    if os.path.exists(minsdir):
        pass
    else:
        os.mkdir(minsdir)

    if os.path.exists(bothdir):
        pass
    else:
        os.mkdir(bothdir)

    [ddate,dtime,code,direction,price]=tmp_array
    end_date = common.format_date(ddate,"%Y-%m-%d")
    start_date = common.get_lastN_date(end_date, start_date_interval)
    day_end_date = common.get_lastN_date(end_date, end_date_interval)
    code = "0"*(6-len(str(int(code))))+str(int(code))

    # 日线数据
    sql = "SELECT TICKER_SYMBOL, SEC_SHORT_NAME, TRADE_DATE, PRE_CLOSE_PRICE, OPEN_PRICE, HIGHEST_PRICE, LOWEST_PRICE, CLOSE_PRICE, \
    DEAL_AMOUNT from vmkt_equd where TRADE_DATE >= '%s' and TRADE_DATE <='%s' and TICKER_SYMBOL = '%s'"%(start_date,day_end_date,code)
    sub = common.get_mysqlData_sqlquery(sql)
    sub = regulate_data(sub)

    idxcode = "000001"
    idxsql = "SELECT TICKER_SYMBOL, SEC_SHORT_NAME, TRADE_DATE, PRE_CLOSE_INDEX, OPEN_INDEX, HIGHEST_INDEX, LOWEST_INDEX, CLOSE_INDEX, \
    TURNOVER_VOL from vmkt_idxd where TRADE_DATE >= '%s' and TRADE_DATE <='%s' and TICKER_SYMBOL = '%s'"%(start_date,day_end_date,idxcode)
    idx = common.get_mysqlData_sqlquery(idxsql)
    idx.columns=sub.columns

    add_mean(sub)
    add_mean(idx)

    tableTime = common.format_date(ddate,"%Y%m")
    endDate = common.format_date(ddate,"%Y%m%d")
    endDate = int(endDate)
    stockid = int(code)

    # table = "equity_pricefenbi%s"%tableTime
    table = "MarketDataTDB.equity_pricemin%s"%tableTime
    dtsql = "SELECT * from %s where ticker = %s and datadate = %s"%(table,stockid,endDate)
    dtv = common.get_mydb_sqlquery(dtsql)
    if len(dtv) == 0:
        table = "MarketDataL1.equity_pricemin%s"%tableTime
        dtsql = "SELECT * from %s where ticker = %s and datadate = %s"%(table,stockid,endDate)
        dtv = common.get_mydb_sqlquery(dtsql)

    table = "MarketDataTDB.equity_pricemin%s"%tableTime
    zssql = 'SELECT * from %s where datadate = %s and ticker = 1 and shortnm = "上证指数"'%(table,endDate)
    sh = common.get_mydb_sqlquery(zssql)
    if len(sh) == 0:
        table = "MarketDataL1.equity_pricemin%s"%tableTime
        zssql = 'SELECT * from %s where datadate = %s and ticker = 1 and shortnm = "上证指数"'%(table,endDate)
        sh = common.get_mydb_sqlquery(zssql)

    # ##临时增加,需要保存数据
    # tmp_dir = "D:\Money\lilton_code\Market_Mode\study_fresh_fail_Module\data"
    # dtv.to_csv(os.path.join(tmp_dir,"%s_%s.csv"%(ddate,code)),encoding='utf8')

    [name,tmp_id]=common.QueryStockMap(id=code)
    ##日线,daydir
    fig1 = plt.figure(figsize=[12,8])

    ##日线图
    point = 10
    # plt.title("%s"%code)
    plt.title(u"%s_%s_%s_d.png"%(ddate, code, name_tail))
    ax1 = fig1.add_subplot(211)
    plot_candlestick(sub, ax1, point=10, direction=direction, mount_flag=1, mark_date=end_date)

    ax2 = fig1.add_subplot(212)
    plot_candlestick(idx, ax2, point=10, mount_flag=1, mark_date=end_date)

    # plt.savefig(os.path.join(daydir,"%s_%s_%s.pdf"%(ddate,direction,code)),dpi=300)
    plt.savefig(os.path.join(daydir, "%s_%s_%s_%s_m.png"%(ddate, code, name, name_tail)),dpi=300)
    plt.close()

    ##分时图,分钟线,在dir/mins
    fig2 = plt.figure(figsize=(12,8))
    ax3 = fig2.add_subplot(211)


    plt.title("%s"%code)
    plot_dealDetail(dtv,ax3,time=dtime,direction=direction,mount_flag=1)

    ax4 = fig2.add_subplot(212)
    plot_dealDetail(sh,ax4,time=dtime,direction=direction,mount_flag=1)

    # plt.savefig(os.path.join(minsdir,"%s_%s_%s_%s.pdf"%(ddate,direction,code,name)),dpi=300)
    plt.savefig(os.path.join(minsdir,"%s_%s_%s_%s.png"%(ddate, code, name, name_tail)),dpi=300)
    plt.close()
Пример #8
0
    def plot_stocks_html(self):
        n_day_before = common.get_lastN_date(self.running_day, 80)
        concept_frame_list = []
        all_local_frame = common.FindConceptStocks(n_day_before,
                                                   self.running_day)

        # 收集所有关注概念以及对应的股票代码
        for concept in self.concepts:
            concept_stocks = []
            # 云财经中相关股票
            ycj_stocks = common.get_ycj_stocks(concept)
            # 金融界中相关股票
            jrj_stocks = common.get_jrj_stocks(concept)
            # 本地csv中相关股票
            csv_stocks = all_local_frame.filter_by_concept(concept)
            # 合并到一起
            concept_stocks.extend(ycj_stocks)
            concept_stocks.extend(jrj_stocks)
            concept_stocks.extend(csv_stocks.ticker.values)

            concept_stocks = list(set(concept_stocks))
            tmp_concept_frame = pd.DataFrame({"stockid": concept_stocks})
            tmp_concept_frame['concept'] = concept
            concept_frame_list.append(tmp_concept_frame)
        # index stockid, concept
        concept_frame = pd.concat(concept_frame_list, axis=0)

        daily_stock_list = list(np.unique(concept_frame.stockid.values))
        daily_stock_list.extend(self.extra_stocks)
        daily_stock_list = list(set(daily_stock_list))

        # 拿到这些股票,指定日期的所有日线数据
        daily_frame = common.get_mysqlData(
            daily_stock_list, common.getDate(n_day_before, self.running_day))
        # daily_frame.to_csv(u'all_daily_frame.csv', encoding='gbk')
        daily_frame['rate'] = 100 * (
            daily_frame['CLOSE_PRICE'] -
            daily_frame['PRE_CLOSE_PRICE']) / daily_frame['PRE_CLOSE_PRICE']
        daily_frame['rate'] = daily_frame['rate'].round(2)

        daily_frame['lrate'] = 100 * (
            daily_frame['LOWEST_PRICE'] -
            daily_frame['PRE_CLOSE_PRICE']) / daily_frame['PRE_CLOSE_PRICE']
        daily_frame['lrate'] = daily_frame['lrate'].round(2)

        daily_frame['hrate'] = 100 * (
            daily_frame['HIGHEST_PRICE'] -
            daily_frame['PRE_CLOSE_PRICE']) / daily_frame['PRE_CLOSE_PRICE']
        daily_frame['hrate'] = daily_frame['hrate'].round(2)

        daily_frame['TRADE_DATE'] = daily_frame['TRADE_DATE'].apply(
            lambda x: x.strftime("%Y-%m-%d"))

        # 复权数据,以便画k线图
        fq_daily_frame = common.get_mysqlData(daily_stock_list,
                                              common.getDate(
                                                  n_day_before,
                                                  self.running_day),
                                              db_table='vmkt_equd_adj')
        fq_daily_frame['rate'] = 100 * (fq_daily_frame['CLOSE_PRICE'] -
                                        fq_daily_frame['PRE_CLOSE_PRICE']
                                        ) / fq_daily_frame['PRE_CLOSE_PRICE']
        fq_daily_frame['rate'] = fq_daily_frame['rate'].round(2)

        fq_daily_frame['lrate'] = 100 * (fq_daily_frame['LOWEST_PRICE'] -
                                         fq_daily_frame['PRE_CLOSE_PRICE']
                                         ) / fq_daily_frame['PRE_CLOSE_PRICE']
        fq_daily_frame['lrate'] = fq_daily_frame['lrate'].round(2)

        fq_daily_frame['hrate'] = 100 * (fq_daily_frame['HIGHEST_PRICE'] -
                                         fq_daily_frame['PRE_CLOSE_PRICE']
                                         ) / fq_daily_frame['PRE_CLOSE_PRICE']
        fq_daily_frame['hrate'] = fq_daily_frame['hrate'].round(2)

        fq_daily_frame['TRADE_DATE'] = fq_daily_frame['TRADE_DATE'].apply(
            lambda x: x.strftime("%Y-%m-%d"))

        # 筛选出符合条件的股票
        # 连续3天以上涨停
        # condition1_frame = filter_stock_by_zt_condition(daily_frame, condition='ZT', zt_num=3)

        #{"concept":concept_frame, "concept":concept_frame2}
        # concept_frame: index TICKER_SYMBOL 5_chg 25_chg N_chg rank
        self.concept_rank_dict = {}
        for concept in self.concepts:
            print concept
            concept_stock_list = list(
                concept_frame[concept_frame.concept == concept].stockid.values)
            # 去除掉rmv对应的股票,以及加上mst对于的股票
            if concept in self.rmv_dict.keys():
                concept_stock_list = [
                    x for x in concept_stock_list
                    if x not in self.rmv_dict[concept]
                ]

            if concept in self.mst_dict.keys():
                concept_stock_list.extend(self.mst_dict[concept])

            # 概念对应的所有股票的日线数据
            concept_daily_frame = daily_frame[daily_frame.TICKER_SYMBOL.isin(
                concept_stock_list)]
            # 统计出其涨幅排行
            concept_rank_chg_frame = add_rank_chg(concept_daily_frame,
                                                  chg_days=[5, 25],
                                                  sort_v=5)

            # 得到连续N个涨停以上的股票
            condition1_frame = filter_stock_by_zt_condition(
                daily_frame[daily_frame.TICKER_SYMBOL.isin(
                    concept_stock_list)],
                condition='ZT',
                zt_num=self.direct10_num[concept])
            # print concept,self.direct10_num[concept]

            # 得到涨幅排行前N的股票
            if len(condition1_frame) < 3:
                tail_n = 10 - len(condition1_frame)
            else:
                tail_n = 5
            stock_list = list(
                concept_rank_chg_frame.sort_values(
                    by=['rank'],
                    ascending=False).tail(tail_n).TICKER_SYMBOL.values)
            stock_list.extend(condition1_frame.TICKER_SYMBOL.values)

            # 再加上必须关注的股票
            if concept in self.mst_dict.keys():
                stock_list.extend(self.mst_dict[concept])

            stock_list = list(set(stock_list))
            # 记录到mongo中daily_interesting_concept_stocks
            self.write_mongo_concept_day(stock_list, concept, self.running_day)

            # 画图
            k_line_frame_list = []
            for stock in stock_list:
                stock_frame = fq_daily_frame[
                    fq_daily_frame.TICKER_SYMBOL == stock][[
                        u'TRADE_DATE', 'OPEN_PRICE', 'CLOSE_PRICE',
                        'HIGHEST_PRICE', 'LOWEST_PRICE', 'TURNOVER_VOL',
                        'TICKER_SYMBOL', 'rate', 'lrate', 'hrate',
                        'SEC_SHORT_NAME'
                    ]]
                stock_frame.columns = [
                    'date', 'open', 'close', 'high', 'low', 'volume', 'code',
                    'rate', 'lrate', 'hrate', 'name'
                ]
                stock_frame['name'] = stock_frame['name'].apply(
                    lambda x: x.encode('gb2312'))
                # index row,date,open, close, high, low, volume, code
                # stock_frame = stock_frame.sort('date', ascending='True')
                stock_frame = stock_frame.sort_values(by=['date'],
                                                      ascending='True')
                stock_frame.reset_index(inplace=True)
                stock_frame['name'] = stock_frame['name'].apply(
                    lambda x: x.decode("gbk").encode("utf-8"))
                del stock_frame['index']
                k_line_frame_list.append(stock_frame)

            # 只显示感兴趣的股票的排行和涨幅数据
            concept_rank_chg_frame = concept_rank_chg_frame[
                concept_rank_chg_frame.TICKER_SYMBOL.isin(stock_list)]
            concept_rank_chg_frame = concept_rank_chg_frame.sort_values(
                by=['rank'], ascending=True)
            concept_rank_chg_frame['stock_name'] = concept_rank_chg_frame[
                'TICKER_SYMBOL'].apply(lambda x: common.QueryStockMap(x)[0])
            names = [
                x for x in concept_rank_chg_frame.columns
                if x not in ['stock_name', 'TICKER_SYMBOL']
            ]
            valid_name = ['stock_name']
            valid_name.extend(names)
            concept_rank_chg_frame = concept_rank_chg_frame[valid_name]

            # 构造一个OPTION4&6,左边为K线图,右边为条形图,以及下面一系列的k线图
            plot_frame_list = [[k_line_frame_list[0], concept_rank_chg_frame]]
            plot_frame_list.extend(k_line_frame_list[1:])

            # 构造对应的stock_list,标识对应的股票代码
            plot_stock_list = []
            for kline_frame in k_line_frame_list:
                plot_stock_list.append(kline_frame['code'].values[0])

            html_types = ['4and6']
            html_types.extend([4] * len(k_line_frame_list[1:]))

            title_list = [['A_stock', 'accum_ratio']]
            title_list.extend(['A_stocks'] * len(k_line_frame_list[1:]))

            # 在加一个统计区间涨跌幅
            # range_ratio = {
            #     concept_name:[[start1, end1],[start2, end2], [start3, end3], ...],
            #     concept_name2:[[start1, end1],[start2, end2], [start3, end3], ...],
            # }

            # 说明该概念需要画出不同时期的涨跌幅对比
            if concept in self.range_ratio_dict.keys():
                range_ratio_frame = pd.DataFrame()
                for [sday, eday] in self.range_ratio_dict[concept]:
                    col_name = "%s-%s" % (common.format_date(
                        sday, "%Y%m%d"), common.format_date(eday, "%Y%m%d"))
                    for stock in stock_list:
                        range_ratio_frame.loc[
                            stock, col_name] = common.time_range_accum_ratio(
                                stock, sday, eday)
                range_ratio_frame.reset_index(inplace=True)
                range_ratio_frame.rename(columns={"index": "stock_name"},
                                         inplace=True)
                range_ratio_frame['stock_name'] = range_ratio_frame[
                    'stock_name'].apply(lambda x: common.QueryStockMap(x)[0])
                # 添加到画图中
                plot_frame_list.append(range_ratio_frame)
                plot_stock_list.append(0)
                html_types.append(7)
                title_list.append('ACCUM_RATIO')

            # 输出html
            common.get_html_curve(plot_frame_list,
                                  '%s' % (concept),
                                  html_types=html_types,
                                  title_list=title_list,
                                  save_dir=os.path.join(
                                      self.dump_dir, self.running_day),
                                  chart_connect=1,
                                  plot_stock_list=plot_stock_list,
                                  generate_date=self.running_day)
            logging.getLogger().info("plot html finished for %s, %s" %
                                     (concept, self.running_day))
        print "Finished for %s" % self.running_day
Пример #9
0
    def read_concepts_excel(self):
        # 读取关注的概念列表
        concept_frame = pd.read_excel(os.path.join(self.path, u'战区.xlsx'),
                                      encoding='gbk',
                                      sheetname=u'战区位置')
        concept_frame = concept_frame[concept_frame[u'开战状态'] == 1]
        ini_concepts = concept_frame[u'战区'].values
        top_dns_concept = []

        # 将concept化为一级域名的concept
        for concept in ini_concepts:
            concept = common.get_top_dns_concept(concept)
            top_dns_concept.append(concept)
        self.concepts = top_dns_concept

        # 设置不同概念对应的连板数
        self.direct10_num = {}
        for idx in concept_frame.index.values:
            concept_name = common.get_top_dns_concept(concept_frame.loc[idx,
                                                                        u'战区'])
            self.direct10_num[concept_name] = int(concept_frame.loc[idx,
                                                                    u'连板数'])

        # 读取关注概念统计涨跌幅的时间区间
        range_ratio_dict = {}
        dframe = pd.read_excel(os.path.join(self.path, u'战区.xlsx'),
                               encoding='gbk',
                               sheetname=u'历史战况',
                               index_col=0)
        dframe.fillna(0, inplace=True)
        dframe.reset_index(inplace=True)
        dframe[u'战区'] = dframe[u'战区'].apply(
            lambda x: common.get_top_dns_concept(x))
        dframe.set_index(u'战区', inplace=True)
        columns = dframe.columns
        for concept in dframe.index.values:
            if concept == 0:
                continue
            tcount = 0
            concept_list = []
            tmp_list = []
            for col in columns:
                tdate = str(int(dframe.loc[concept, col]))
                if tdate == '0':
                    if len(tmp_list) > 0:
                        concept_list.append(tmp_list)
                    break
                if tcount % 2 == 0 and tcount != 0:
                    concept_list.append(tmp_list)
                    tmp_list = []
                    tmp_list.append(tdate)
                else:
                    tmp_list.append(tdate)
                tcount += 1
            if len(concept_list) > 0:
                range_ratio_dict[concept] = concept_list

        # range_ratio = {
        #     concept_name:[[start1, end1],[start2, end2], [start3, end3], ...],
        #     concept_name2:[[start1, end1],[start2, end2], [start3, end3], ...],
        # }
        self.range_ratio_dict = range_ratio_dict

        # 读取特殊关照股票
        dframe = pd.read_csv(os.path.join(self.path, u'增减排除.csv'),
                             encoding='gbk')
        dframe.fillna(0, inplace=True)

        self.extra_stocks = [
            common.QueryStockMap(name=x)[1]
            for x in dframe[dframe.status != 'spy']['name'].values
        ]
        rmv_frame = dframe[dframe.status == 'spy']

        # rmv_frame 再加上程序自动生成的list, 有时间再弄
        # rmv_list =

        # rmv_dict = {
        #     concept_name:[stockid1, stockid2, stockid3, ...]
        # }
        self.rmv_dict = {}
        for idx in rmv_frame.index.values:
            concept = rmv_frame.loc[idx, u'concept']
            concept = common.get_top_dns_concept(concept)
            stockname = rmv_frame.loc[idx, u'name']
            if concept not in self.rmv_dict.keys():
                self.rmv_dict[concept] = [
                    common.QueryStockMap(name=stockname)[1]
                ]
            else:
                self.rmv_dict[concept].append(
                    common.QueryStockMap(name=stockname)[1])

        mst_frame = dframe[dframe.status != 'spy']
        # mst_dict = {
        #     concept_name:[stockid1, stockid2, stockid3, ...]
        # }
        self.mst_dict = {}
        for idx in mst_frame.index.values:
            concept = mst_frame.loc[idx, u'concept']
            concept = common.get_top_dns_concept(concept)
            stockname = mst_frame.loc[idx, u'name']
            if concept not in self.mst_dict.keys():
                self.mst_dict[concept] = [
                    common.QueryStockMap(name=stockname)[1]
                ]
            else:
                self.mst_dict[concept].append(
                    common.QueryStockMap(name=stockname)[1])

        # 读取新增的云财经或者金融界的crawl id和对应的concept
        dframe = pd.read_excel(os.path.join(self.path, u'战区.xlsx'),
                               encoding='gbk',
                               sheetname=u'侦查信号')
        dframe.fillna(0, inplace=True)
        ycj_dframe = dframe[dframe.source == 'YCJ']
        jrj_dframe = dframe[dframe.source == 'JRJ']

        # detect_dict = {
        # "YCJ": {conceptname1: conceptid1, conceptname2: conceptid2},
        # "JRJ": {conceptname1: conceptid1, conceptname2: conceptid2}
        # }
        detect_dict = {"YCJ": {}, "JRJ": {}}
        for idx in ycj_dframe.index.values:
            concept = ycj_dframe.loc[idx, u'概念名']
            conceptid = ycj_dframe.loc[idx, u'网站id']
            concept = common.get_top_dns_concept(concept)
            detect_dict['YCJ'][concept] = conceptid

        for idx in jrj_dframe.index.values:
            concept = jrj_dframe.loc[idx, u'概念名']
            conceptid = jrj_dframe.loc[idx, u'网站id']
            concept = common.get_top_dns_concept(concept)
            detect_dict['JRJ'][concept] = conceptid

        self.detect_dict = detect_dict
Пример #10
0
mongourl = "localhost"
mongodb = pymongo.MongoClient(mongourl)
results = mongodb.stock.ZDT_by_date.find({"date": {"$gte": "20160101"}})
count = 0
dframe = DataFrame()
mysqls = common.mysqldb

global diOut_dict, backTen_dict, tenMonitor_dict
diOut_dict = {}  # 开板没有回封的记录
backTen_dict = {}  # 专门记录回封情况的dict
tenMonitor_dict = {}  # 回封后的每a日监控

for result in results:
    dates = result['date']
    if "Add_newStocks" in result.keys():
        stocks = result['Add_newStocks'].keys()
        for stock in stocks:
            dframe.loc[count, 'startDate'] = dates
            dframe.loc[count, 'stockid'] = stock
            dframe.loc[count, 'name'] = common.QueryStockMap(stock)[0]
            tmp_result = get_stopDetail(stock, dates, mysqls)
            (dframe.loc[count, 'numTen'], dframe.loc[count, 'openDate'],
             dframe.loc[count, 'backTen?'], dframe.loc[count, 'backDuring'],
             dframe.loc[count,
                        'stopDate'], dframe.loc[count,
                                                'stopRatio']) = tmp_result
            count += 1

print 'will finish'
dframe.to_csv(u"新股开板情况.csv", encoding='GBK')
Пример #11
0
 def insertCache(self, stockid, concept, date, cache=1):
     stockid = common.regulize_stockid(stockid)
     if cache == 1:
         self.mongodb.concepts.L1.update({"stockid":stockid, "concept":concept},
                                     {"$set":{"stockid":stockid, "concept":concept, "name":common.QueryStockMap(id=stockid)[0], "updatetime": date}},upsert=True)
     else:
         self.mongodb.concepts.L2.update({"stockid":stockid, "concept":concept},
                                     {"$set":{"stockid":stockid, "concept":concept, "name":common.QueryStockMap(id=stockid)[0], "updatetime": date}},upsert=True)
Пример #12
0
    def save_pool_dragon(self, strcontent, concept_name, tdate):
        # 分成3类, dragon_list, wind_list, remove_list
        str_list = strcontent.split(";")
        dragon_list = [
            x.split("_")[1] for x in str_list
            if (u'Dragon' in x and u'true' in x)
        ]
        wind_list = [
            x.split("_")[1] for x in str_list
            if (u'Wind' in x and u'true' in x)
        ]
        remove_list = [
            x.split("_")[1] for x in str_list
            if (u'Remove' in x and u'true' in x)
        ]
        catch_dict = {}

        # 读取excel
        dframe = pd.read_excel(os.path.join(u'D:/Money/modeResee/彼战',
                                            u'战区.xlsx'),
                               encoding='gbk',
                               sheetname=u'类型')
        for idx in dframe.index.values:
            code_str = dframe.loc[idx, u'源码']
            tmp_list = [
                x.split("_")[1] for x in str_list
                if (code_str in x and u'true' in x)
            ]
            catch_dict[code_str] = tmp_list

        # 将remove的存到增减排除.csv中
        xframe = pd.read_csv(os.path.join(u'D:/Money/modeResee/彼战',
                                          u'增减排除.csv'),
                             encoding='gbk')
        max_id = max(xframe.index.values) + 1
        for ticker in remove_list:
            xframe.loc[max_id, 'concept'] = concept_name
            xframe.loc[max_id, 'name'] = common.QueryStockMap(id=ticker)[0]
            xframe.loc[max_id, 'status'] = 'spy'
            max_id += 1
        xframe.to_csv(os.path.join(u'D:/Money/modeResee/彼战', u'增减排除.csv'),
                      encoding='gbk',
                      index=False)

        # 将dragon和wind存到mongo中的concepts, dragon_pool表中
        save_dict = {"date": tdate}
        results = self.mongodb.concepts.dragon_pool.find({"date": tdate})
        if results.count() > 0:
            save_dict = results[0]

        save_dict[concept_name] = {}
        if len(dragon_list) > 0:
            save_dict[concept_name]['dragon'] = dragon_list

        if len(wind_list) > 0:
            save_dict[concept_name]['wind'] = wind_list

        self.mongodb.concepts.dragon_pool.update({"date": tdate},
                                                 save_dict,
                                                 upsert=True)

        # 将up, down, flat存放到mongo中的pattern, trend表中
        for code_str in catch_dict.keys():
            tmp_list = catch_dict[code_str]
            for ticker in tmp_list:
                self.mongodb.pattern.trend.update(
                    {
                        "date": tdate,
                        "ticker": ticker
                    }, {
                        "date": tdate,
                        "ticker": ticker,
                        "trend": code_str
                    },
                    upsert=True)