def core(self, location, day, param_sid): stock_df = pd.read_sql_query( "select * from t_stock where location = " + str(location) + " and type = 1 and status = 'Y'", self.conn, index_col='id') #print stock_df.index, stock_df.columns sid_list = [param_sid] if param_sid > 0 else stock_df.index for sid in sid_list: dyn_info = self.update_dyn(day, sid) if dyn_info is None: print format_log("stock_paused", { 'sid': sid, 'location': location, 'day': day }) continue sql = SqlUtil.create_insert_sql("t_stock_dyn", dyn_info) try: db_conn = SqlUtil.get_db(self.db_config) db_conn.query_sql(sql, True) except Exception as e: print "err=insert_dyn sid=" + str(sid) + " day=" + str( day) + " ex=" + str(e) continue print format_log("add_stock_dyn", dyn_info) print format_log("finish_dyn", {'location': location, 'day': day})
def add_stock_pool(db_config, redis_config, sid, day, source, trend_info = dict()): sql = "select id, sid, day, source from t_stock_pool where sid = {sid} and day = {day} and status = 'Y'".format(sid=sid, day=day) try: db_conn = SqlUtil.get_db(db_config) record_list = db_conn.query_sql(sql) if len(record_list) > 0: record_id = record_list[0]['id'] new_source = int(record_list[0]['source']) | source oper_sql = "update t_stock_pool set source = {source} where id = {id}".format(source=new_source, id=record_id) else: fields = {'sid': sid, 'day': day, 'source': source, 'status': 'Y'} fields['create_time'] = time.mktime( datetime.datetime.now().timetuple() ) if trend_info: fields.update(trend_info) # 获取当日行情数据 hqdata = get_hqdata(db_config, redis_config, sid, day) if hqdata: fields['close_price'] = hqdata['daily']['close_price'] if 'policy' in hqdata and hqdata['policy']: fields['volume_ratio'] = round(float(hqdata['policy']['volume_ratio']), 2) fields['rise_factor'] = round(float(hqdata['policy']['rise_factor']), 2) #print fields oper_sql = SqlUtil.create_insert_sql("t_stock_pool", fields) db_conn.query_sql(oper_sql, True) except Exception as e: print e return False return True
def prepare_data(self): # 获取所有的股票列表 stock_list = get_stock_list(self.db_config, 1, self.location) self.datamap['stock_list'] = stock_list #print len(self.datamap['stock_list']) code2id_map = dict() id2scode_map = dict() for sid, stock_info in stock_list.items(): scode = get_scode(stock_info['code'], int(stock_info['ecode']), int(stock_info['location'])) code2id_map[stock_info['code']] = sid id2scode_map[sid] = scode self.datamap['code2id'] = code2id_map self.datamap['id2scode'] = id2scode_map #print len(self.datamap['id2scode']) last_open_day = get_past_openday(str(self.day), 1) #print last_open_day cont_list = [] try: db_conn = SqlUtil.get_db(self.db_config) sql = "select sid from t_stock_cont where day = " + last_open_day + " and status = 'Y'" #print sql record_list = db_conn.query_sql(sql) except Exception as e: print e return for stock_data in record_list: cont_list.append(int(stock_data['sid'])) self.datamap['cont_list'] = cont_list #print len(self.datamap['cont_list']) threshold_list = [] try: db_conn = SqlUtil.get_db(self.db_config) sql = "select sid from t_stock_price_threshold where day = " + last_open_day + " and (high_type = 1 or high_type = 2 or low_type = 1 or low_type = 2) and status = 'Y'" price_record_list = db_conn.query_sql(sql) except Exception as e: print e return for stock_data in price_record_list: threshold_list.append(int(stock_data['sid'])) self.datamap['threshold_list'] = threshold_list #print len(self.datamap['threshold_list']) self.datamap['pool_list'] = list(set(cont_list + threshold_list)) #print self.datamap['pool_list'] return
def get_cont_list(self, sid, past_day, day): sql = "select sid, day, start_day, cont_days, current_price, sum_price_vary_amount, sum_price_vary_portion, max_volume_vary_portion \ from t_stock_cont where sid = {sid} and start_day >= {past_day} and day <= {day} and status = 'Y'".format(sid=sid, past_day=past_day, day=day) print sql try: db_conn = SqlUtil.get_db(self.db_config) record_list = db_conn.query_sql(sql) except Exception as e: print e return None return record_list # 获取指定股票对应日期范围内的价格突破 def get_threshold_list(self, sid, past_day, day): sql = "select sid, day, price, low_type, high_type from t_stock_price_threshold where \ sid = {sid} and day >= {past_day} and day <= {day} and status = 'Y'".format(sid=sid, past_day=past_day, day=day) print sql try: db_conn = SqlUtil.get_db(self.db_config) record_list = db_conn.query_sql(sql) except Exception as e: print e return None return record_list
def add_stock_pool(self, sid, day, info): cur_day = str(day) current_time = datetime.datetime(int(cur_day[0:4]), int(cur_day[4:6]), int(cur_day[6:8])) # 本周一 start_time = current_time + datetime.timedelta(days = -1 * (current_time.isoweekday() - 1)) start_day = '{0:%Y%m%d}'.format(start_time) sql = "select id from t_stock_pool where sid={sid} and status = 'Y' and day >= {start_day} and day < {end_day}"\ .format(sid=sid, start_day=start_day, end_day=day) print sql try: record_list = self.db_conn.query_sql(sql) except Exception as e: print e return False if len(record_list) >= 1: return True info['add_time'] = time.mktime( datetime.datetime.now().timetuple() ) info['status'] = 'Y' sql = SqlUtil.create_insert_sql("t_stock_pool", info) print sql try: self.db_conn.query_sql(sql, True) except Exception as e: print e return False return True
def __init__(self, config_info, datamap): self.config_info = config_info self.datamap = datamap self.redis_conn = redis.StrictRedis(self.config_info['REDIS']['host'], int(self.config_info['REDIS']['port'])) self.db_conn = SqlUtil.get_db(self.config_info["DB"]) self.logger = logging.getLogger("policy")
def __init__(self, sid, config_info): self.sid = sid self.config_info = config_info self.helper = AnalyzerHelper(self.config_info) self.db_config = config_info['DB'] self.db_conn = SqlUtil.get_db(config_info['DB'])
def get_past_data(db_config, redis_config, cur_day, count): key = "pastdata-" + str(cur_day) stock_datamap = dict() redis_conn = redis.StrictRedis(redis_config['host'], redis_config['port']) datamap = redis_conn.hgetall(key) if datamap: return datamap db_conn = SqlUtil.get_db(db_config) start_day = get_past_openday(cur_day, count) try: sql = "select sid, avg(volume) as avg_volume, sum(vary_price) as sum_vary_price, sum(vary_portion) as sum_vary_portion from t_stock_data \ where day >= " + start_day + " and day < " + cur_day + " group by sid" print sql record_list = db_conn.query_sql(sql) except Exception as e: print e return None for record in record_list: item = dict() sid = record['sid'] item['sid'] = int(sid) item['avg_volume'] = round(float(record['avg_volume'])) item['sum_vary_price'] = float(record['sum_vary_price']) item['sum_vary_portion'] = float(record['sum_vary_portion']) stock_datamap[sid] = json.dumps(item) #print stock_datamap redis_conn.hmset(key, stock_datamap) redis_conn.expire(key, 86400) return stock_datamap
def add_stock_pool(self, sid, day, info): cur_day = str(day) current_time = datetime.datetime(int(cur_day[0:4]), int(cur_day[4:6]), int(cur_day[6:8])) # 本周一 start_time = current_time + datetime.timedelta( days=-1 * (current_time.isoweekday() - 1)) start_day = '{0:%Y%m%d}'.format(start_time) sql = "select id from t_stock_pool where sid={sid} and status = 'Y' and day >= {start_day} and day < {end_day}"\ .format(sid=sid, start_day=start_day, end_day=day) print sql try: record_list = self.db_conn.query_sql(sql) except Exception as e: print e return False if len(record_list) >= 1: return True info['add_time'] = time.mktime(datetime.datetime.now().timetuple()) info['status'] = 'Y' sql = SqlUtil.create_insert_sql("t_stock_pool", info) print sql try: self.db_conn.query_sql(sql, True) except Exception as e: print e return False return True
def add_stock_price_threshold(db_config, sid, day, price, high_type, low_type): info = {'sid': sid, 'day': day, 'price': price, 'status': 'Y'} info['create_time'] = time.mktime(datetime.datetime.now().timetuple()) info['high_type'] = high_type info['low_type'] = low_type sql = SqlUtil.create_insert_sql("t_stock_price_threshold", info) print sql try: db_conn = SqlUtil.get_db(db_config) db_conn.query_sql(sql, True) except Exception as e: print e return False return True
def add_stock_price_threshold(db_config, sid, day, price, high_type, low_type): info = {'sid': sid, 'day': day, 'price': price, 'status': 'Y'} info['create_time'] = time.mktime( datetime.datetime.now().timetuple() ) info['high_type'] = high_type info['low_type'] = low_type sql = SqlUtil.create_insert_sql("t_stock_price_threshold", info) print sql try: db_conn = SqlUtil.get_db(db_config) db_conn.query_sql(sql, True) except Exception as e: print e return False return True
def __init__(self, config_info): self.config_info = config_info self.db_config = config_info['DB'] self.redis_config = config_info['REDIS'] self.redis_conn = redis.StrictRedis(self.config_info['REDIS']['host'], int(self.config_info['REDIS']['port'])) self.db_conn = SqlUtil.get_db(self.config_info["DB"]) self.logger = logging.getLogger("ranker")
def core(self, location, day, param_sid): stock_df = pd.read_sql_query("select * from t_stock where location = " + str(location) + " and type = 1 and status = 'Y'", self.conn, index_col='id') #print stock_df.index, stock_df.columns sid_list = [param_sid] if param_sid > 0 else stock_df.index for sid in sid_list: dyn_info = self.update_dyn(day, sid) if dyn_info is None: print format_log("stock_paused", {'sid': sid, 'location': location, 'day': day}) continue sql = SqlUtil.create_insert_sql("t_stock_dyn", dyn_info) try: db_conn = SqlUtil.get_db(self.db_config) db_conn.query_sql(sql, True) except Exception as e: print "err=insert_dyn sid=" + str(sid) + " day=" + str(day) + " ex=" + str(e) continue print format_log("add_stock_dyn", dyn_info) print format_log("finish_dyn", {'location': location, 'day': day})
def get_policy_itemlist(db_config, pid): sql = "select id, name, vid, optor, param, value, pid, parent_id, node_type, logic from t_policy_item where pid = {pid} and status = 'Y'".format(pid = pid) print sql try: db_conn = SqlUtil.get_db(db_config) record_list = db_conn.query_sql(sql) except Exception as e: print e return None return record_list
def get_threshold_list(self, sid, past_day, day): sql = "select sid, day, price, low_type, high_type from t_stock_price_threshold where \ sid = {sid} and day >= {past_day} and day <= {day} and status = 'Y'".format(sid=sid, past_day=past_day, day=day) print sql try: db_conn = SqlUtil.get_db(self.db_config) record_list = db_conn.query_sql(sql) except Exception as e: print e return None return record_list
def get_stock_trendlist(db_config, sid, start_day, end_day): sql = "select sid, type, start_day, end_day, count, high_day, high, low_day, low, start_value, end_value, vary_portion, trend, shave \ from t_stock_trend where status = 'Y' and sid = {sid} and ((start_day >= {start_day} and start_day <= {end_day}) \ or (end_day >= {start_day} and end_day <= {end_day})) order by start_day asc".format(sid=sid, start_day=start_day, end_day=end_day) print sql try: db_conn = SqlUtil.get_db(db_config) record_list = db_conn.query_sql(sql) except Exception as e: print e return None return record_list
def get_stock_info(db_config, sid): sql = "select id, code, name, type, pinyin, ecode, location, alias, company, business, capital, out_capital, profit, assets, dividend, hist_high, hist_low, year_high, year_low, month6_high, \ month6_low, month3_high, month3_low from t_stock where status = 'Y' and id = " + str(sid) try: db_conn = SqlUtil.get_db(db_config) record_list = db_conn.query_sql(sql) except Exception as e: print e return None stock_info = dict() if len(record_list) > 0: stock_info = record_list[0] return stock_info
def get_policy_info(db_config, pid): sql = "select id, type, name, remark, uid, root_item from t_policy where id={pid} and status = 'Y'".format(pid=pid) print sql try: db_conn = SqlUtil.get_db(db_config) record_list = db_conn.query_sql(sql) except Exception as e: print e return None if len(record_list) < 1: return None return record_list[0]
def prepare_data(self, market_config): # 获取所有的股票列表 stock_list = get_stock_list(self.db_config, 0, self.location) self.datamap['stock_list'] = stock_list #print len(self.datamap['stock_list']) code2id_map = dict() id2scode_map = dict() for sid, stock_info in stock_list.items(): scode = get_scode(stock_info['code'], int(stock_info['ecode']), int(stock_info['location'])) code2id_map[stock_info['code']] = sid id2scode_map[sid] = scode self.datamap['code2id'] = code2id_map self.datamap['id2scode'] = id2scode_map #print len(self.datamap['id2scode']) # 美股取过去一周的股票池 past_days = 5 if self.location == 3 else 1 last_open_day = get_past_openday(str(self.day), past_days) print last_open_day pool_list = [] try: db_conn = SqlUtil.get_db(self.db_config) sql = "select sid from t_stock_pool where day >= " + last_open_day + " and status = 'Y'" print sql record_list = db_conn.query_sql(sql) except Exception as e: print e return for stock_data in record_list: sid = int(stock_data['sid']) if sid in stock_list and sid not in pool_list: pool_list.append(sid) if 3 == self.location and 'cnlist' in market_config: cnlist = market_config['cnlist'] for cn_code in cnlist: if cn_code in code2id_map: sid = code2id_map[cn_code] if sid not in pool_list: pool_list.append(sid) self.datamap['pool_list'] = pool_list logging.getLogger("fetch").info("op=scheduler_pool day=%d last_open_day=%s location=%d total_count=%d pool_count=%d", self.day, last_open_day, self.location, len(stock_list), len(pool_list)) return
def get_stock_price_threshold(db_config, sid, start_day, end_day, high_type, low_type): sql = "select sid, day, price, low_type, high_type from t_stock_price_threshold where status = 'Y' and sid = {sid} and day >= {start_day} and day <= {end_day}"\ .format(sid=sid, start_day=start_day, end_day=end_day) if high_type > 0: sql = sql + " and high_type = " + str(high_type) if low_type > 0: sql = sql + " and low_type = " + str(low_type) try: db_conn = SqlUtil.get_db(db_config) record_list = db_conn.query_sql(sql) except Exception as e: print e return None return record_list
def get_varlist(db_config, vid = 0): sql = "select id, code, name, type, expression, add_time from t_policy_var where status = 'Y'" #print sql try: db_conn = SqlUtil.get_db(db_config) record_list = db_conn.query_sql(sql) except Exception as e: print e return None var_list = dict() for record in record_list: var_list[int(record['id'])] = record if vid > 0: return var_list[vid] if vid in var_list else None return var_list
def get_cont_stock(db_config, current_day, day_count, sum_portion, rise = True): start_day = get_past_openday(current_day, day_count) operator = ">" if rise else "<" sql = "select sid from t_stock_data where day >= {start_day} and day <= {current_day} and vary_portion {operator} 0 \ group by sid having count(*) >= {day_count} and sum(vary_portion) >= {low_portion} and sum(vary_portion) <= {high_portion}"\ .format(start_day=start_day, current_day=current_day, operator=operator, day_count=day_count, \ low_portion=sum_portion[0], high_portion=sum_portion[1]) print sql record_list = [] try: db_conn = SqlUtil.get_db(db_config) record_list = db_conn.query_sql(sql) except Exception as e: print e return None return [item['sid'] for item in record_list]
def get_cont_stock(db_config, current_day, day_count, sum_portion, rise=True): start_day = get_past_openday(current_day, day_count) operator = ">" if rise else "<" sql = "select sid from t_stock_data where day >= {start_day} and day <= {current_day} and vary_portion {operator} 0 \ group by sid having count(*) >= {day_count} and sum(vary_portion) >= {low_portion} and sum(vary_portion) <= {high_portion}"\ .format(start_day=start_day, current_day=current_day, operator=operator, day_count=day_count, \ low_portion=sum_portion[0], high_portion=sum_portion[1]) print sql record_list = [] try: db_conn = SqlUtil.get_db(db_config) record_list = db_conn.query_sql(sql) except Exception as e: print e return None return [item['sid'] for item in record_list]
def get_stock_list(db_config, type = 0, location = 1): sql = "select id, code, name, type, pinyin, ecode, location, alias, company, business, capital, out_capital, profit, assets, dividend, hist_high, hist_low, year_high, year_low, month6_high, \ month6_low, month3_high, month3_low from t_stock where status = 'Y' " if type > 0: sql = sql + " and type = " + str(type) if location > 0: sql = sql + " and location = " + str(location) try: db_conn = SqlUtil.get_db(db_config) record_list = db_conn.query_sql(sql) except Exception as e: print e return None stock_list = dict() for stock_info in record_list: stock_list[int(stock_info['id'])] = stock_info return stock_list
def get_stock_data(db_config, day, sid=0): if sid == 0: sql = "select sid, day, open_price, high_price, low_price, close_price, volume, amount, \ vary_price, vary_portion from t_stock_data where day = {day} and status = 'Y'".format( day=day) else: sql = "select sid, day, open_price, high_price, low_price, close_price, volume, amount, \ vary_price, vary_portion from t_stock_data where day = {day} and sid = {sid} and status = 'Y'".format( day=day, sid=sid) try: db_conn = SqlUtil.get_db(db_config) record_list = db_conn.query_sql(sql) except Exception as e: print e return None data = dict() for stock_data in record_list: data[int(stock_data['sid'])] = stock_data return data if 0 == sid else data[sid]
def get_past_data(db_config, redis_config, cur_day, count, location=1): key = "pastdata-" + str(cur_day) stock_datamap = dict() redis_conn = redis.StrictRedis(redis_config['host'], redis_config['port']) datamap = redis_conn.hgetall(key) # 从redis中取出来的key都是string if datamap: return datamap db_conn = SqlUtil.get_db(db_config) start_day = get_past_openday(str(cur_day), count, location) try: sql = "select sid, avg(volume) as avg_volume, sum(vary_price) as sum_vary_price, sum(vary_portion) as sum_vary_portion, \ avg(close_price) as avg_close_price, max(high_price) as high_price, min(low_price) as low_price from t_stock_data \ where day >= " + str(start_day) + " and day < " + str(cur_day) + " group by sid" #print sql record_list = db_conn.query_sql(sql) except Exception as e: print e return None for record in record_list: item = dict() sid = record['sid'] item['sid'] = int(sid) item['avg_volume'] = round(float(record['avg_volume'])) item['sum_vary_price'] = float(record['sum_vary_price']) item['sum_vary_portion'] = float(record['sum_vary_portion']) item['avg_close_price'] = float(record['avg_close_price']) item['high_price'] = float(record['high_price']) item['low_price'] = float(record['low_price']) stock_datamap[sid] = json.dumps(item) #print stock_datamap redis_conn.hmset(key, stock_datamap) return stock_datamap
def chance_return(config_info, location, day, item): hqdata = get_hqdata(config_info['DB'], config_info['REDIS'], day, item['sid']) print hqdata['daily'] chance_item = item['chance'] trend_item = item['trend_item'] # 卖出时用平仓价格减卖出价格, 需要乘-1 factor = 1 if 1 == chance_item['op'] else -1 # 严格一点, 应该用time之后的high_price/low_price计算收益和回撤, 买入点以较大价格进入, 卖出以较低价格卖出 enter_price = chance_item['price_range'][1] if 1 == chance_item['op'] else chance_item['price_range'][0] exit_price = float(hqdata['daily']['close_price']) fall_price = float(hqdata['daily']['low_price']) if 1 == chance_item['op'] else float(hqdata['daily']['high_price']) rise_portion = factor * (exit_price - enter_price) / enter_price * 100 fall_portion = factor * (fall_price - enter_price) / enter_price * 100 return_info = {"sid": item['sid'], "code": item['code'], "time": item['time'], "op": chance_item['op'], "trend_length": trend_item['length'], "trend_portion": trend_item['vary_portion'], "enter": enter_price, "exit": exit_price, "fall": fall_price, "close_portion": rise_portion, "fall_portion": fall_portion, "vary_portion": hqdata['daily']['vary_portion']} # TODO: 输出 dyn数据 record_list = [] try: sql = "select * from t_stock_dyn where sid = {sid} and day < {day} order by day desc limit 1".format(sid=item['sid'], day=day) #print sql db_conn = SqlUtil.get_db(config_info['DB']) record_list = db_conn.query_sql(sql) except Exception as e: print e logging.getLogger("chance").error("err=get_stock_dyn sid=%d code=%s location=%d day=%d", item['sid'], item['code'], location, day) else: #print record_list if len(record_list) == 1: stock_dyn = record_list[0] for key in ['ma5_swing', 'ma20_swing', 'ma5_vary_portion', 'ma20_vary_portion', 'ma5_exchange_portion', 'ma20_exchange_portion', 'volume_ratio']: return_info[key] = stock_dyn[key] logging.getLogger("chance").info("%s", format_log("chance_item", item)) return return_info
def get_stock_data(db_config, day, sid=0): if sid == 0: sql = "select sid, day, open_price, high_price, low_price, close_price, volume, amount, \ vary_price, vary_portion from t_stock_data where day = {day} and status = 'Y'".format(day=day) else: if isinstance(sid, int): sql = "select sid, day, open_price, high_price, low_price, close_price, volume, amount, \ vary_price, vary_portion from t_stock_data where day = {day} and sid = {sid} and status = 'Y'".format(day=day, sid=sid) elif isinstance(sid, list): sql = "select sid, day, open_price, high_price, low_price, close_price, volume, amount, \ vary_price, vary_portion from t_stock_data where day = {day} and sid in ({sid_list}) and status = 'Y'".format(day=day, sid=",".join(sid)) try: db_conn = SqlUtil.get_db(db_config) record_list = db_conn.query_sql(sql) except Exception as e: print e return None data = dict() for stock_data in record_list: data[int(stock_data['sid'])] = stock_data return data if 0 == sid else data[sid]
def refresh_stock_histdata(redis_config, db_config, stock_list, today_data_list, day, location = 1, refresh = True): db_conn = SqlUtil.get_db(db_config) high_field_list = ["hist_high", "year_high", "month6_high", "month3_high"] low_field_list = ["hist_low", "year_low", "month6_low", "month3_low"] vary_stock_list = dict() for sid, stock_info in stock_list.items(): # 忽略指数 if sid not in today_data_list or int(stock_info['type']) == 2: continue #print stock_info stock_data = today_data_list[sid] #print stock_data high_index = 4 low_index = 4 close_price = float(stock_data['close_price']) out_capital = close_price * float(stock_info['out_capital']); capital_limit = 10 if 3 == location: out_capital = out_capital / 10000 capital_limit = 15 # capital < 10/15(us) if out_capital <= capital_limit: continue for index, field_name in enumerate(high_field_list): if close_price > float(stock_info[field_name]): high_index = index break for index, field_name in enumerate(low_field_list): if close_price < float(stock_info[field_name]): low_index = index break # 表明当天价格存在最高价或者最低价 #print sid, high_index, low_index if high_index < 4 or low_index < 4: vary_stock_list[sid] = {'high_index': high_index, 'low_index': low_index} sql = "update t_stock set " field_list = [] high_type = low_type = 0 # 起始日期定为之前3个交易日, 3个交易日内无同类型突破记录 range_start_day = get_past_openday(day, 3, location) if high_index < 4: high_type = high_index + 1 high_threshold_list = get_stock_price_threshold(db_config, sid, range_start_day, day, high_type, 0) if 0 == len(high_threshold_list): add_result = add_stock_price_threshold(db_config, sid, day, close_price, high_type, low_type) print format_log("add_high_price_threshold", {'sid': sid, 'day': day, 'close_price': close_price, 'high_type': high_type, 'result':add_result}) if add_result and high_type <= 2: # 年内新高/历史最高才加入股票池 pool_result = add_stock_pool(db_config, redis_config, sid, day, 2, {'wave':1}) print format_log("add_stock_pool", {'sid': sid, 'day': day, 'close_price': close_price, 'result':pool_result}) for field_name in high_field_list[high_index:]: stock_info[field_name] = close_price field_list.append(field_name + "=" + str(stock_info[field_name])) if low_index < 4: low_type = low_index + 1 low_threshold_list = get_stock_price_threshold(db_config, sid, range_start_day, day, 0, low_type) if 0 == len(low_threshold_list): add_result = add_stock_price_threshold(db_config, sid, day, close_price, high_type, low_type) print format_log("add_low_price_threshold", {'sid': sid, 'day': day, 'close_price': close_price, 'low_type': low_type, 'result':add_result}) # TODO: 把下跌突破也加入股票池 for field_name in low_field_list[low_index:]: stock_info[field_name] = close_price field_list.append(field_name + "=" + str(stock_info[field_name])) sql = sql + ", ".join(field_list) + " where id=" + str(stock_info['id']) print sql # 股票且设置刷新, 才插入价格突破记录 if refresh and int(stock_info['type']) == 1: try: db_conn.query_sql(sql, True) except Exception as e: continue log_info = {'sid': sid, 'code': stock_info['code'], 'name': stock_info['name'], 'day': day, 'close_price': stock_data['close_price'], 'high_price': stock_data['high_price'], 'low_price': stock_data['low_price'], 'high_index': high_index, 'low_index': low_index} print format_log("refresh_stock_info", log_info) #TODO: 统一删除变化的stock_info if refresh: conn = redis.StrictRedis(redis_config['host'], redis_config['port']) key_list = [ "stock:info-" + str(sid) for sid in vary_stock_list.keys() ] conn.delete(tuple(key_list)) return vary_stock_list
def __init__(self, config_info): self.config_info = config_info self.db_conn = SqlUtil.get_db(config_info['DB'])
def chance_return(config_info, location, day, item): hqdata = get_hqdata(config_info['DB'], config_info['REDIS'], day, item['sid']) print hqdata['daily'] chance_item = item['chance'] trend_item = item['trend_item'] # 卖出时用平仓价格减卖出价格, 需要乘-1 factor = 1 if 1 == chance_item['op'] else -1 # 严格一点, 应该用time之后的high_price/low_price计算收益和回撤, 买入点以较大价格进入, 卖出以较低价格卖出 enter_price = chance_item['price_range'][1] if 1 == chance_item[ 'op'] else chance_item['price_range'][0] exit_price = float(hqdata['daily']['close_price']) fall_price = float( hqdata['daily']['low_price']) if 1 == chance_item['op'] else float( hqdata['daily']['high_price']) rise_portion = factor * (exit_price - enter_price) / enter_price * 100 fall_portion = factor * (fall_price - enter_price) / enter_price * 100 return_info = { "sid": item['sid'], "code": item['code'], "time": item['time'], "op": chance_item['op'], "trend_length": trend_item['length'], "trend_portion": trend_item['vary_portion'], "enter": enter_price, "exit": exit_price, "fall": fall_price, "close_portion": rise_portion, "fall_portion": fall_portion, "vary_portion": hqdata['daily']['vary_portion'] } # TODO: 输出 dyn数据 record_list = [] try: sql = "select * from t_stock_dyn where sid = {sid} and day < {day} order by day desc limit 1".format( sid=item['sid'], day=day) #print sql db_conn = SqlUtil.get_db(config_info['DB']) record_list = db_conn.query_sql(sql) except Exception as e: print e logging.getLogger("chance").error( "err=get_stock_dyn sid=%d code=%s location=%d day=%d", item['sid'], item['code'], location, day) else: #print record_list if len(record_list) == 1: stock_dyn = record_list[0] for key in [ 'ma5_swing', 'ma20_swing', 'ma5_vary_portion', 'ma20_vary_portion', 'ma5_exchange_portion', 'ma20_exchange_portion', 'volume_ratio' ]: return_info[key] = stock_dyn[key] logging.getLogger("chance").info("%s", format_log("chance_item", item)) return return_info