def watch_history(dateEq='19890928', dateStart='19890928', dateEnd='19890928'): global mongodb dataFrame = pd.DataFrame() dateArr = [] pre_ztm = [] pre_ztpos = [] ztnumArr = [] dtnumArr = [] maxconZt = [] maxconDt = [] querys = common.get_mongoDicts(dateEq=dateEq, dateStart=dateStart, dateEnd=dateEnd) for query in querys: dateArr.append(query['date']) # print query['date'] pre_ztm.append(query['pre_ztm']) if query['pre_ztpos'] > 1: query['pre_ztpos'] = None pre_ztpos.append(query['pre_ztpos']) ztnumArr.append(query['ZT_num']) dtnumArr.append(query['DT_num']) maxu = max_value(query['num_ucont']) maxd = max_value(query['num_dcont']) if maxu == '+6': maxu = 7 else: maxu = int(maxu) if maxd == '+6': maxd = 7 else: maxd = int(maxd) maxconZt.append(maxu) maxconDt.append(maxd) dataFrame = pd.concat([ Series(dateArr), Series(pre_ztm), Series(pre_ztpos), Series(ztnumArr), Series(dtnumArr), Series(maxconZt), Series(maxconDt) ], axis=1) dataFrame = dataFrame.dropna() dataFrame.columns = [ 'date', 'pre_ztm', 'pre_ztpos', 'ZT_num', 'DT_num', 'maxZT', 'maxDT' ] print dataFrame common.plotFrame(dataFrame, x='date', y=[['pre_ztm', 'pre_ztpos'], ['ZT_num', 'DT_num'], ['maxZT', 'maxDT']], titles=['zt_yes', 'ZDT_num', 'maxCon'], point=10)
def update_ZDT_stocksNum_ALL(self, dateEq='19890928',dateStart = '19890928', dateEnd = '19890928'): dateDicts = common.get_mongoDicts(dateEq=dateEq,dateStart=dateStart,dateEnd=dateEnd) if dateDicts == 1: print "Error, can't find the date" return 1 else: for dateDict in dateDicts: cdate=[dateDict['date']] self.update_ZDT_stocksNum([],cdate)
def update_ZDT_yesterday(self,dateEq='19890928',dateStart = '19890928', dateEnd = '19890928'): # step 2 ## 从指定日期开始,计算meat,hole, 以及pre的情况 mongoUrl = "localhost" mongodb = pymongo.MongoClient(mongoUrl) dateDicts = common.get_mongoDicts(dateEq=dateEq,dateStart=dateStart,dateEnd=dateEnd) for dateDict in dateDicts: cdate=dateDict['date'] cdate=datetime.datetime.strptime(cdate,"%Y%m%d").strftime("%Y-%m-%d") predate=common.get_last_date(cdate) self.get_meat(cdate,predate,mongodb)
def update_ZDT_stocksNum_ALL(self, dateEq='19890928', dateStart='19890928', dateEnd='19890928'): dateDicts = common.get_mongoDicts(dateEq=dateEq, dateStart=dateStart, dateEnd=dateEnd) if dateDicts == 1: print "Error, can't find the date" return 1 else: for dateDict in dateDicts: cdate = [dateDict['date']] self.update_ZDT_stocksNum([], cdate)
def update_ZDT_contsNum(self, dateEq='19890928',dateStart = '19890928', dateEnd = '19890928'): # step 2 # 给定日期区间内,计算连续涨跌停个数,并去重zt_stocks, dt_stocks, 重新计算个数 # 需要依赖数据库中已经有的涨跌停股票代码以及连续涨跌停数(如果没有连续值,则从0开始) mongoUrl = "localhost" mongodb = pymongo.MongoClient(mongoUrl) dateDicts = common.get_mongoDicts(dateEq=dateEq,dateStart=dateStart,dateEnd=dateEnd) for dateDict in dateDicts: cdate=dateDict['date'] print "handle continus ZDT number for %s"%cdate cdate=datetime.datetime.strptime(cdate,"%Y%m%d").strftime("%Y-%m-%d") predate=common.get_last_date(cdate) common.update_numZD(cdate,predate,mongodb)
def update_freshStocks(self,dateEq='19890928',dateStart = '19890928', dateEnd = '19890928'): # step 2 # 给定日期区间内,计算连续涨跌停个数,并去重zt_stocks, dt_stocks, 重新计算个数 # 需要依赖数据库中已经有的涨跌停股票代码以及连续涨跌停数(如果没有连续值,则从0开始) mongoUrl = "localhost" mongodb = pymongo.MongoClient(mongoUrl) try: dateDicts = common.get_mongoDicts(dateEq=dateEq,dateStart=dateStart,dateEnd=dateEnd) for dateDict in dateDicts: cdate=dateDict['date'] predate=common.get_last_date(cdate) self.oneDate_freshStocks(cdate,predate,mongodb) except Exception,e: print "Exception error: %s"%e
def update_ZDT_yesterday(self, dateEq='19890928', dateStart='19890928', dateEnd='19890928'): # step 2 ## 从指定日期开始,计算meat,hole, 以及pre的情况 mongoUrl = "localhost" mongodb = pymongo.MongoClient(mongoUrl) dateDicts = common.get_mongoDicts(dateEq=dateEq, dateStart=dateStart, dateEnd=dateEnd) for dateDict in dateDicts: cdate = dateDict['date'] cdate = datetime.datetime.strptime(cdate, "%Y%m%d").strftime("%Y-%m-%d") predate = common.get_last_date(cdate) self.get_meat(cdate, predate, mongodb)
def update_ZDT_contsNum(self, dateEq='19890928', dateStart='19890928', dateEnd='19890928'): # step 2 # 给定日期区间内,计算连续涨跌停个数,并去重zt_stocks, dt_stocks, 重新计算个数 # 需要依赖数据库中已经有的涨跌停股票代码以及连续涨跌停数(如果没有连续值,则从0开始) mongoUrl = "localhost" mongodb = pymongo.MongoClient(mongoUrl) dateDicts = common.get_mongoDicts(dateEq=dateEq, dateStart=dateStart, dateEnd=dateEnd) for dateDict in dateDicts: cdate = dateDict['date'] print "handle continus ZDT number for %s" % cdate cdate = datetime.datetime.strptime(cdate, "%Y%m%d").strftime("%Y-%m-%d") predate = common.get_last_date(cdate) common.update_numZD(cdate, predate, mongodb)
def update_freshStocks(self, dateEq='19890928', dateStart='19890928', dateEnd='19890928'): # step 2 # 给定日期区间内,计算连续涨跌停个数,并去重zt_stocks, dt_stocks, 重新计算个数 # 需要依赖数据库中已经有的涨跌停股票代码以及连续涨跌停数(如果没有连续值,则从0开始) mongoUrl = "localhost" mongodb = pymongo.MongoClient(mongoUrl) try: dateDicts = common.get_mongoDicts(dateEq=dateEq, dateStart=dateStart, dateEnd=dateEnd) for dateDict in dateDicts: cdate = dateDict['date'] predate = common.get_last_date(cdate) self.oneDate_freshStocks(cdate, predate, mongodb) except Exception, e: print "Exception error: %s" % e
def watch_history(dateEq='19890928',dateStart = '19890928', dateEnd = '19890928'): global mongodb dataFrame = pd.DataFrame() dateArr = [] pre_ztm = [] pre_ztpos = [] ztnumArr = [] dtnumArr = [] maxconZt = [] maxconDt = [] querys = common.get_mongoDicts(dateEq=dateEq,dateStart=dateStart,dateEnd=dateEnd) for query in querys: dateArr.append(query['date']) # print query['date'] pre_ztm.append(query['pre_ztm']) if query['pre_ztpos'] > 1: query['pre_ztpos'] = None pre_ztpos.append(query['pre_ztpos']) ztnumArr.append(query['ZT_num']) dtnumArr.append(query['DT_num']) maxu = max_value(query['num_ucont']) maxd = max_value(query['num_dcont']) if maxu== '+6': maxu = 7 else: maxu = int(maxu) if maxd == '+6': maxd = 7 else: maxd = int(maxd) maxconZt.append(maxu) maxconDt.append(maxd) dataFrame=pd.concat([Series(dateArr),Series(pre_ztm),Series(pre_ztpos),Series(ztnumArr),Series(dtnumArr),Series(maxconZt),Series(maxconDt)],axis=1) dataFrame=dataFrame.dropna() dataFrame.columns=['date','pre_ztm','pre_ztpos','ZT_num','DT_num','maxZT','maxDT'] print dataFrame common.plotFrame(dataFrame,x='date',y=[['pre_ztm','pre_ztpos'],['ZT_num','DT_num'],['maxZT','maxDT']],titles=['zt_yes','ZDT_num','maxCon'],point=10)
def parse_content(self, content): content = content.replace(u'var kcSTlIop=', u'').replace(u'pages', u'"pages"').replace(u'data', u'"data"') con_dict = eval(content) # 当前时间点 ttime = time.localtime() thour = ttime.tm_hour tmin = ttime.tm_min day_list = [] if thour >= 18: # 晚上6点以后,可以得到当天收盘价了,关注复牌时间点为[明天,今天,昨天] for n in range(2-self.dayRange, 2): day_list.append(common.get_lastN_date(self.date, n)) else: # 否则,关注时间点为[今天,昨天,前天] for n in range(1-self.dayRange, 1): day_list.append(common.get_lastN_date(self.date, n)) for infos in con_dict['data']: elements = infos.split(",") if len(elements) < 2: continue secid = elements[0] secname = elements[1] tp_time = elements[-2] fp_time = elements[-1] reason = elements[5] db_num = self.mongodb.stock.FP.find({"stcid":"%s" % secid, "tp_time":"%s" % tp_time, "fp_time":"%s" % fp_time, "target_ratio":{"$exists":True}}).count() if db_num > 0: continue else: self.mongodb.stock.FP.update({"stcid":"%s" % secid, "tp_time":"%s" % tp_time}, {"$set":{"stcid":"%s" % secid, "tp_time":"%s" % tp_time, "fp_time":"%s" % fp_time, "reason":"%s" %reason}}, True, True) # 仅仅记录目标日期在关注日期内的信息 if fp_time in day_list: # 期间大盘涨幅 if secid[:2] == '60': dp_stockid = 'ZS000001' else: dp_stockid = 'ZS399006' dp_frame = common.get_mysqlData([dp_stockid], [tp_time, common.get_last_date(fp_time)]) if len(dp_frame) < 2: continue dp_ratio = (dp_frame.loc[1, 'CLOSE_PRICE'] - dp_frame.loc[0, 'CLOSE_PRICE']) / dp_frame.loc[0, 'CLOSE_PRICE'] dp_ratio = round(dp_ratio, 2) # 停牌时价格 stock_price = common.get_mysqlData([secid], [tp_time]).loc[0, 'CLOSE_PRICE'] if dp_ratio > 0: diff = 0.05 else: diff = -0.05 # 目标涨幅 target_ratio = dp_ratio + diff # 目标价格 target_price = round(stock_price*(1+target_ratio), 2) # 停牌时间 delta_days = common.get_mongoDicts(dateStart=tp_time,dateEnd=fp_time).count() self.mongodb.stock.FP.update({"stcid":"%s" % secid, "tp_time":"%s" % tp_time}, {"$set":{"dp_ratio":dp_ratio, "target_ratio":target_ratio, "target_price": target_price, 'delta_days':delta_days}}, True, True)
def parse_content(self, content): content = content.replace(u'var kcSTlIop=', u'').replace( u'pages', u'"pages"').replace(u'data', u'"data"') con_dict = eval(content) # 当前时间点 ttime = time.localtime() thour = ttime.tm_hour tmin = ttime.tm_min day_list = [] if thour >= 18: # 晚上6点以后,可以得到当天收盘价了,关注复牌时间点为[明天,今天,昨天] for n in range(2 - self.dayRange, 2): day_list.append(common.get_lastN_date(self.date, n)) else: # 否则,关注时间点为[今天,昨天,前天] for n in range(1 - self.dayRange, 1): day_list.append(common.get_lastN_date(self.date, n)) for infos in con_dict['data']: elements = infos.split(",") if len(elements) < 2: continue secid = elements[0] secname = elements[1] tp_time = elements[-2] fp_time = elements[-1] reason = elements[5] db_num = self.mongodb.stock.FP.find({ "stcid": "%s" % secid, "tp_time": "%s" % tp_time, "fp_time": "%s" % fp_time, "target_ratio": { "$exists": True } }).count() if db_num > 0: continue else: self.mongodb.stock.FP.update( { "stcid": "%s" % secid, "tp_time": "%s" % tp_time }, { "$set": { "stcid": "%s" % secid, "tp_time": "%s" % tp_time, "fp_time": "%s" % fp_time, "reason": "%s" % reason } }, True, True) # 仅仅记录目标日期在关注日期内的信息 if fp_time in day_list: # 期间大盘涨幅 if secid[:2] == '60': dp_stockid = 'ZS000001' else: dp_stockid = 'ZS399006' dp_frame = common.get_mysqlData( [dp_stockid], [tp_time, common.get_last_date(fp_time)]) if len(dp_frame) < 2: continue dp_ratio = (dp_frame.loc[1, 'CLOSE_PRICE'] - dp_frame.loc[0, 'CLOSE_PRICE'] ) / dp_frame.loc[0, 'CLOSE_PRICE'] dp_ratio = round(dp_ratio, 2) # 停牌时价格 stock_price = common.get_mysqlData( [secid], [tp_time]).loc[0, 'CLOSE_PRICE'] if dp_ratio > 0: diff = 0.05 else: diff = -0.05 # 目标涨幅 target_ratio = dp_ratio + diff # 目标价格 target_price = round(stock_price * (1 + target_ratio), 2) # 停牌时间 delta_days = common.get_mongoDicts(dateStart=tp_time, dateEnd=fp_time).count() self.mongodb.stock.FP.update( { "stcid": "%s" % secid, "tp_time": "%s" % tp_time }, { "$set": { "dp_ratio": dp_ratio, "target_ratio": target_ratio, "target_price": target_price, 'delta_days': delta_days } }, True, True)