import http
|
import json
|
import logging
|
import socketserver
|
import time
|
from http.server import BaseHTTPRequestHandler
|
import dask
|
|
from code_attribute.gpcode_manager import BlackListCodeManager
|
from l2.l2_transaction_data_manager import HuaXinBuyOrderManager
|
from log_module.log import logger_system, logger_debug, logger_kpl_limit_up, logger_request_api
|
from third_data.kpl_limit_up_data_manager import LatestLimitUpBlockManager, CodeLimitUpSequenceManager
|
from trade.buy_strategy import OpenLimitUpGoodBlocksBuyStrategy
|
from utils import global_util, tool, data_export_util
|
from code_attribute import gpcode_manager
|
from log_module import log, log_analyse, log_export, async_log_util
|
from l2 import code_price_manager, l2_data_util, transaction_progress
|
from cancel_strategy.s_l_h_cancel_strategy import HourCancelBigNumComputer, LCancelRateManager
|
from output.limit_up_data_filter import IgnoreCodeManager
|
from third_data import kpl_util, kpl_data_manager, kpl_api, block_info
|
from third_data.code_plate_key_manager import RealTimeKplMarketData, KPLPlateForbiddenManager, CodePlateKeyBuyManager
|
from third_data.history_k_data_util import HistoryKDatasUtils
|
from third_data.kpl_data_manager import KPLDataManager, KPLLimitUpDataRecordManager, \
|
KPLCodeLimitUpReasonManager
|
from third_data.kpl_util import KPLDataType, KPLPlatManager
|
import urllib.parse as urlparse
|
from urllib.parse import parse_qs
|
from output import code_info_output, limit_up_data_filter, output_util, kp_client_msg_manager
|
|
from trade import bidding_money_manager, trade_manager, l2_trade_util, trade_record_log_util, trade_constant, \
|
trade_data_manager
|
import concurrent.futures
|
|
# 禁用http.server的日志输出
|
logger = logging.getLogger("http.server")
|
logger.setLevel(logging.CRITICAL)
|
|
|
class DataServer(BaseHTTPRequestHandler):
|
ocr_temp_data = {}
|
__kplDataManager = KPLDataManager()
|
__IgnoreCodeManager = IgnoreCodeManager()
|
__KPLPlatManager = KPLPlatManager()
|
__KPLCodeLimitUpReasonManager = KPLCodeLimitUpReasonManager()
|
__KPLPlateForbiddenManager = KPLPlateForbiddenManager()
|
# 历史板块
|
__history_plates_dict = {}
|
# 板块
|
__blocks_dict = {}
|
# 精选,行业数据缓存
|
__jingxuan_cache_dict = {}
|
__industry_cache_dict = {}
|
__latest_limit_up_codes_set = set()
|
__data_process_thread_pool = concurrent.futures.ThreadPoolExecutor(max_workers=10)
|
# 代码的涨幅
|
__code_limit_rate_dict = {}
|
|
# 禁用日志输出
|
def log_message(self, format, *args):
|
pass
|
|
def __get_limit_up_list(self):
|
# 统计目前为止的代码涨停数量(分涨停原因)
|
total_datas = KPLLimitUpDataRecordManager.total_datas
|
if not total_datas:
|
KPLLimitUpDataRecordManager.load_total_datas()
|
total_datas = KPLLimitUpDataRecordManager.total_datas
|
# 通过涨停时间排序
|
total_datas = list(total_datas)
|
current_reason_codes_dict = KPLLimitUpDataRecordManager.get_current_reason_codes_dict()
|
|
# 统计涨停原因
|
limit_up_reason_dict = {}
|
for d in total_datas:
|
if d[2] not in limit_up_reason_dict:
|
limit_up_reason_dict[d[2]] = []
|
limit_up_reason_dict[d[2]].append(d)
|
for k in limit_up_reason_dict:
|
limit_up_reason_dict[k].sort(key=lambda x: int(x[5]))
|
# 统计想买单数量
|
want_codes = gpcode_manager.WantBuyCodesManager().list_code_cache()
|
limit_up_reason_want_count_dict = {}
|
for d in total_datas:
|
if d[2] not in limit_up_reason_want_count_dict:
|
limit_up_reason_want_count_dict[d[2]] = 0
|
if d[3] in want_codes:
|
limit_up_reason_want_count_dict[d[2]] += 1
|
# (板块名称,涨停代码数量,想买单数量,涨停时间)
|
limit_up_reason_statistic_info = [
|
(k, len(limit_up_reason_dict[k]), limit_up_reason_want_count_dict.get(k), limit_up_reason_dict[k][0][5]) for
|
k in
|
limit_up_reason_dict]
|
limit_up_reason_statistic_info.sort(
|
key=lambda x: len(current_reason_codes_dict[x[0]]) if x[0] in current_reason_codes_dict else 0)
|
limit_up_reason_statistic_info.reverse()
|
|
codes_set = set([d[3] for d in total_datas])
|
# 判断是龙几,判断是否涨停,判断是否炸板,加载分数
|
rank_dict = limit_up_data_filter.get_limit_up_time_rank_dict(total_datas)
|
limit_up_dict, limit_up_codes, open_limit_up_codes = limit_up_data_filter.get_limit_up_info(codes_set)
|
score_dict = {}
|
fresult = []
|
ignore_codes = self.__IgnoreCodeManager.list_ignore_codes("1")
|
|
total_datas.sort(key=lambda x: int(x[5]))
|
total_datas.reverse()
|
|
# 获取涨停原因变化记录
|
reason_changes = log_export.load_kpl_reason_changes()
|
reason_changes.reverse()
|
reason_changes_dict = {}
|
for r in reason_changes:
|
if r[0] not in reason_changes_dict:
|
reason_changes_dict[r[0]] = r[1]
|
# 统计最近下单动作反馈
|
order_reasons_dict = log_analyse.get_cant_order_reasons_dict()
|
kpl_can_buy_reasons_dict = log_analyse.get_kpl_can_buy_reasons_dict()
|
|
for d in total_datas:
|
code = d[3]
|
# (代码, 名称, 涨停状态(0 - 无状态 1-涨停 2-炸板), 龙几, 首板, 分值, 涨停时间, 原因, 相同原因代码数量, 自由流通, 涨停原因是否变化,涨停原因的流入净额,下单简介)
|
limit_up_state = 0
|
if code in limit_up_dict:
|
if limit_up_dict[code][0]:
|
limit_up_state = 1
|
elif limit_up_dict[code][1]:
|
limit_up_state = 2
|
score = ""
|
if code in score_dict:
|
score = score_dict[code]
|
if code in ignore_codes:
|
continue
|
|
# 涨停原因的净流入金额
|
reason = d[2]
|
reason_money = ''
|
if reason in self.__jingxuan_cache_dict:
|
reason_money = output_util.money_desc(self.__jingxuan_cache_dict[reason][3])
|
elif reason in self.__industry_cache_dict:
|
reason_money = output_util.money_desc(self.__industry_cache_dict[reason][3])
|
|
# 匹配下单反馈
|
order_desc = ''
|
order_reason = order_reasons_dict.get(code)
|
kpl_can_buy_reason = kpl_can_buy_reasons_dict.get(code)
|
if order_reason and kpl_can_buy_reason:
|
if int(order_reason[0].replace(":", "").replace(".", "")) > int(
|
kpl_can_buy_reason[0].replace(":", "").replace(".", "")):
|
order_desc = f"不:{order_reason[1]}"
|
else:
|
order_desc = f"买:{kpl_can_buy_reason[1]}"
|
elif order_reason:
|
order_desc = f"不:{order_reason[1]}"
|
elif kpl_can_buy_reason:
|
order_desc = f"买:{kpl_can_buy_reason[1]}"
|
|
fresult.append((code, d[4], limit_up_state, f"龙{rank_dict.get(code)}", d[12], score,
|
output_util.time_format(int(d[5])), d[2], d[10], output_util.money_desc(d[13]),
|
reason_changes_dict.get(code), reason_money, order_desc))
|
response_data = json.dumps({"code": 0, "data": {"limit_up_count": len(limit_up_codes),
|
"open_limit_up_count": len(open_limit_up_codes),
|
"limit_up_reason_statistic": limit_up_reason_statistic_info,
|
"limit_up_codes": fresult}})
|
return response_data
|
|
def __get_plate_info(self, ps_dict):
|
|
@dask.delayed
|
def kpl_getStockIDPlate(code_):
|
temp_data = kpl_api.getStockIDPlate(code_)
|
return temp_data
|
|
@dask.delayed
|
def kpl_getSonPlate(plate_code_):
|
if not plate_code:
|
return None
|
temp_data = kpl_api.getSonPlate(plate_code_)
|
return temp_data
|
|
@dask.delayed
|
def kpl_getCodesByPlate(plate_code_):
|
if not plate_code:
|
return None
|
temp_data = kpl_api.getCodesByPlate(plate_code_)
|
return temp_data
|
|
@dask.delayed
|
def request_data(f1_, f2_):
|
temp_data = f1_, f2_
|
return temp_data
|
|
# 获取板块的代码
|
fresult = {}
|
code = ps_dict["code"]
|
code_info = KPLLimitUpDataRecordManager.list_by_code(code, tool.get_now_date_str())[0]
|
hot_block_name = code_info[2]
|
plate_code = self.__KPLPlatManager.get_plat(hot_block_name)
|
f1 = kpl_getStockIDPlate(code)
|
# f2 = kpl_getSonPlate(plate_code)
|
f3 = kpl_getCodesByPlate(plate_code)
|
dask_result = request_data(f1, f3)
|
plate_info, codes_by_plate_info = dask_result.compute()
|
|
if plate_info:
|
plate_info.sort(key=lambda x: x[2])
|
plate_info.reverse()
|
fresult["plate"] = plate_info
|
|
# 获取代码的历史涨停数据,(涨停原因,日期,板块)
|
fresult["code_records"] = KPLLimitUpDataRecordManager.get_latest_infos(code, 4, False)[:2]
|
# 获取今日数据
|
fresult["today"] = (code_info[2], code_info[1], code_info[6])
|
fresult["industry"] = global_util.code_industry_map.get(code)
|
if plate_code:
|
# 获取强度
|
# datas = son_plate_info
|
# # (代码,名称,强度)
|
# temp = kpl_util.parseSonPlat(datas)
|
# temp.sort(key=lambda x: x[2])
|
# temp.reverse()
|
# fresult["plat_strength"] = temp
|
|
# 获取涨停原因下面的列表
|
datas = codes_by_plate_info
|
# (代码,名称,现价,涨幅,自由流通,几板,龙几,主力净额,300w净额,机构增仓)
|
temps = kpl_util.parsePlateCodes(datas)
|
# --数据准备开始--
|
codes_set = set([d[0] for d in temps])
|
limit_up_dict, limit_up_codes, open_limit_up_codes = limit_up_data_filter.get_limit_up_info(codes_set)
|
score_dict = {}
|
want_codes = gpcode_manager.WantBuyCodesManager().list_code_cache()
|
black_codes = BlackListCodeManager().list_codes()
|
total_datas = KPLLimitUpDataRecordManager.total_datas
|
code_info_dict = {}
|
for val in total_datas:
|
code_info_dict[val[3]] = val
|
|
# --数据准备结束--
|
|
ignore_codes = self.__IgnoreCodeManager.list_ignore_codes("2")
|
# 最终结果:(代码,名称,涨停状态(0-无状态 1-涨停 2-炸板),龙几,首板,分值,涨停时间,原因,相同原因代码数量,自由流通,涨停原因是否变化,涨幅,现价,黑名单,想买单,主力净值,300w,)
|
codes_info_list = []
|
for t in temps:
|
code = t[0]
|
limit_up_state = 0
|
if code in limit_up_dict:
|
if limit_up_dict[code][0]:
|
limit_up_state = 1
|
elif limit_up_dict[code][1]:
|
limit_up_state = 2
|
score = ""
|
if code in score_dict:
|
score = score_dict[code]
|
|
limit_up_time = ''
|
if code in code_info_dict:
|
limit_up_time = output_util.time_format(code_info_dict[code][5])
|
final_code_info = {"code_info": (
|
t[0], t[1], limit_up_state, t[6], t[5], score, limit_up_time,
|
code_info[2], code_info[10], output_util.money_desc(t[4]), 0, t[3], t[2],
|
"黑名单" if code in black_codes else "", "想买单" if code in want_codes else "",
|
output_util.money_desc(t[7]), output_util.money_desc(t[8]), output_util.money_desc(t[9]))}
|
if code in code_info_dict:
|
final_code_info["today"] = (
|
code_info_dict[code][2], code_info_dict[code][1], code_info_dict[code][6])
|
# 加载历史
|
if code in self.__history_plates_dict:
|
final_code_info["code_records"] = self.__history_plates_dict[code][1]
|
# 加载板块
|
if code in self.__blocks_dict:
|
final_code_info["plate"] = self.__blocks_dict[code][1]
|
|
# 获取二级行业
|
final_code_info["industry"] = global_util.code_industry_map.get(code)
|
|
if code not in ignore_codes:
|
codes_info_list.append(final_code_info)
|
fresult["code_list_info"] = codes_info_list
|
response_data = json.dumps({"code": 0, "data": fresult})
|
return response_data
|
|
def do_GET(self):
|
path = self.path
|
url = urlparse.urlparse(path)
|
async_log_util.info(logger_request_api, f"开始请求{tool.get_thread_id()}-{url}")
|
response_data = ""
|
if url.path == "/get_kpl_data":
|
best_feng_kou = self.__kplDataManager.get_data(kpl_util.KPLDataType.BEST_FENG_KOU)
|
if not best_feng_kou:
|
best_feng_kou = []
|
best_feng_kou = best_feng_kou[:22]
|
feng_kou = self.__kplDataManager.get_data(kpl_util.KPLDataType.FENG_KOU)
|
if not feng_kou:
|
feng_kou = []
|
feng_kou = feng_kou[:22]
|
industry_rank = self.__kplDataManager.get_data(kpl_util.KPLDataType.INDUSTRY_RANK)
|
if not industry_rank:
|
industry_rank = []
|
industry_rank = industry_rank[:22]
|
feng_xiang = self.__kplDataManager.get_data(kpl_util.KPLDataType.FENG_XIANG)
|
if not feng_xiang:
|
feng_xiang = []
|
feng_xiang = feng_xiang[:22]
|
response_data = json.dumps({"code": 0, "data": {"best_feng_kou": best_feng_kou, "feng_kou": feng_kou,
|
"industry_rank": industry_rank, "feng_xiang": feng_xiang}})
|
elif url.path == "/get_score_info":
|
start_time = time.time()
|
ps_dict = dict([(k, v[0]) for k, v in parse_qs(url.query).items()])
|
code = ps_dict['code']
|
name = ps_dict.get('name')
|
date = ps_dict.get('date')
|
try:
|
data = code_info_output.get_output_params(code, self.__jingxuan_cache_dict, self.__industry_cache_dict)
|
if data["code_name"].find("None") > -1 and name:
|
data["code_name"] = f"{name} {code}"
|
|
self.__history_plates_dict[code] = (time.time(), data["kpl_code_info"]["code_records"])
|
if "plate" in data["kpl_code_info"]:
|
self.__blocks_dict[code] = (time.time(), data["kpl_code_info"]["plate"])
|
|
response_data = json.dumps({"code": 0, "data": data})
|
print("get_score_info 耗时:", time.time() - start_time)
|
except Exception as e:
|
logger_debug.exception(e)
|
logging.exception(e)
|
|
elif url.path == "/get_trade_records":
|
# 获取挂撤信息
|
ps_dict = dict([(k, v[0]) for k, v in parse_qs(url.query).items()])
|
code = ps_dict['code']
|
date = ps_dict.get('date')
|
local_today_datas = log_export.load_l2_from_log(date)
|
total_datas = local_today_datas.get(code)
|
trade_info = code_info_output.load_trade_record(code, total_datas, date)
|
response_data = json.dumps({"code": 0, "data": {"open_limit_up": trade_info[0], "records": trade_info[2]}})
|
|
elif url.path == "/get_l2_cant_buy_reasons":
|
# 获取L2没买的原因
|
ps_dict = dict([(k, v[0]) for k, v in parse_qs(url.query).items()])
|
code = ps_dict['code']
|
fdatas = log_export.get_l2_cant_buy_reasons(code)
|
response_data = json.dumps({"code": 0, "data": fdatas})
|
|
elif url.path == "/get_kpl_block_info":
|
start_time = time.time()
|
ps_dict = dict([(k, v[0]) for k, v in parse_qs(url.query).items()])
|
code = ps_dict['code']
|
try:
|
data = code_info_output.get_kpl_block_info(code)
|
response_data = json.dumps({"code": 0, "data": data})
|
print("get_kpl_block_info 耗时:", time.time() - start_time)
|
except Exception as e:
|
logger_debug.exception(e)
|
logging.exception(e)
|
|
elif url.path == "/get_l2_datas":
|
try:
|
# 获取L2的数据
|
ps_dict = dict([(k, v[0]) for k, v in parse_qs(url.query).items()])
|
code = ps_dict['code']
|
date = ps_dict.get('date')
|
time_str = ps_dict.get('time')
|
total_datas = l2_data_util.local_today_datas.get(code)
|
if not total_datas:
|
total_datas = []
|
if date or time_str:
|
total_datas = None
|
else:
|
date = tool.get_now_date_str()
|
delegate_datas = data_export_util.get_l2_datas(code, total_datas, date=date)
|
transaction_datas = data_export_util.get_l2_transaction_datas(code, date=date)
|
code_name = gpcode_manager.get_code_name(code)
|
response_data = json.dumps({"code": 0, "data": {"code": code, "code_name": code_name,
|
"data": {"delegates": delegate_datas,
|
"transactions": transaction_datas}}})
|
except Exception as e:
|
logger_debug.exception(e)
|
elif url.path == "/get_trade_progress":
|
# 获取交易进度
|
ps_dict = dict([(k, v[0]) for k, v in parse_qs(url.query).items()])
|
code = ps_dict['code']
|
trade_progress, is_default = transaction_progress.TradeBuyQueue().get_traded_index(code)
|
# 获取正在成交, 计算成交进度
|
dealing_info = HuaXinBuyOrderManager.get_dealing_order_info(code)
|
dealing_active_info = HuaXinBuyOrderManager.get_dealing_active_order_info(code)
|
percent = 100
|
if dealing_info:
|
total_datas = l2_data_util.local_today_datas.get(code)
|
if str(total_datas[trade_progress]['val']["orderNo"]) == str(dealing_info[0]):
|
num = total_datas[trade_progress]['val']['num']
|
if dealing_active_info and dealing_info[0] == dealing_active_info[0]:
|
if tool.is_sh_code(code):
|
num += dealing_active_info[1] // 100
|
percent = int(dealing_info[1] / num)
|
response_data = json.dumps(
|
{"code": 0, "data": {"trade_progress": trade_progress, "is_default": is_default, "percent": percent}})
|
elif url.path == "/get_l_cancel_datas":
|
# 最新的l撤数据
|
ps_dict = dict([(k, v[0]) for k, v in parse_qs(url.query).items()])
|
code = ps_dict['code']
|
date = ps_dict.get('date')
|
if not date:
|
date = tool.get_now_date_str()
|
buy_single_index = ps_dict.get('buy_single_index')
|
if buy_single_index is not None:
|
buy_single_index = int(buy_single_index)
|
records = code_info_output.load_trade_record_cancel_watch_indexes(code, date=date)
|
# 获取最新的L上与L下
|
records.reverse()
|
up_indexes = []
|
down_indexes = []
|
for r in records:
|
if buy_single_index and buy_single_index != r[1]:
|
continue
|
if r[0] == trade_record_log_util.CancelWatchIndexesInfo.CANCEL_TYPE_L_UP:
|
up_indexes = r[2]
|
break
|
for r in records:
|
if buy_single_index and buy_single_index != r[1]:
|
continue
|
if r[0] == trade_record_log_util.CancelWatchIndexesInfo.CANCEL_TYPE_L_DOWN:
|
down_indexes = r[2]
|
break
|
|
response_data = json.dumps(
|
{"code": 0, "data": {"up": up_indexes, "down": down_indexes}})
|
elif url.path == "/get_h_cancel_datas":
|
# 最新的H撤数据
|
ps_dict = dict([(k, v[0]) for k, v in parse_qs(url.query).items()])
|
code = ps_dict['code']
|
buy_single_index = ps_dict.get('buy_single_index')
|
records = code_info_output.load_trade_record_cancel_watch_indexes(code,
|
trade_record_log_util.CancelWatchIndexesInfo.CANCEL_TYPE_H)
|
# 获取最新的L上与L下
|
records.reverse()
|
indexes = []
|
for r in records:
|
if buy_single_index and buy_single_index != r[1]:
|
continue
|
indexes = r[2]
|
break
|
response_data = json.dumps(
|
{"code": 0, "data": indexes})
|
elif url.path == "/kpl/get_limit_up_list":
|
response_data = self.__get_limit_up_list()
|
|
elif url.path == "/kpl/get_plate_info":
|
ps_dict = dict([(k, v[0]) for k, v in parse_qs(url.query).items()])
|
response_data = self.__get_plate_info(ps_dict)
|
|
elif url.path == "/kpl/get_market_data":
|
# 获取板块信息
|
ps_dict = dict([(k, v[0]) for k, v in parse_qs(url.query).items()])
|
type_ = int(ps_dict['type'])
|
result = []
|
if type_ == 0:
|
# 行业,主力净额倒序
|
result = kpl_api.getMarketIndustryRealRankingInfo(True)
|
result = kpl_util.parseMarketIndustry(result)
|
elif type_ == 1:
|
# 行业,主力净额顺序
|
result = kpl_api.getMarketIndustryRealRankingInfo(False)
|
result = kpl_util.parseMarketIndustry(result)
|
elif type_ == 2:
|
# 精选,主力净额倒序
|
result = kpl_api.getMarketJingXuanRealRankingInfo(True)
|
result = kpl_util.parseMarketJingXuan(result)
|
elif type_ == 3:
|
# 精选,主力净额顺序
|
result = kpl_api.getMarketJingXuanRealRankingInfo(False)
|
result = kpl_util.parseMarketJingXuan(result)
|
forbidden_plates = self.__KPLPlateForbiddenManager.list_all()
|
fresult = []
|
for d in result:
|
if type_ == 2 or type_ == 3:
|
self.__jingxuan_cache_dict[d[1]] = d
|
elif type_ == 0 or type_ == 1:
|
self.__industry_cache_dict[d[1]] = d
|
d = list(d)
|
d.append(1 if d[1] in forbidden_plates else 0)
|
fresult.append(d)
|
response_data = json.dumps({"code": 0, "data": fresult})
|
elif url.path == "/kpl/add_ignore_code":
|
ps_dict = dict([(k, v[0]) for k, v in parse_qs(url.query).items()])
|
code = ps_dict['code']
|
type_ = ps_dict['type']
|
self.__IgnoreCodeManager.ignore_code(type_, code)
|
response_data = json.dumps({"code": 0})
|
elif url.path == "/kpl/forbidden_plate":
|
ps_dict = dict([(k, v[0]) for k, v in parse_qs(url.query).items()])
|
plate = ps_dict["plate"]
|
# 加入禁止
|
self.__KPLPlateForbiddenManager.save_plate(plate)
|
response_data = json.dumps({"code": 0})
|
elif url.path == "/kpl/get_plate_codes":
|
# 获取涨停原因下面的代码
|
ps_dict = dict([(k, v[0]) for k, v in parse_qs(url.query).items()])
|
plate = kpl_util.filter_block(ps_dict["plate"])
|
# 获取板块下的代码
|
# 统计目前为止的代码涨停数量(分涨停原因)
|
now_limit_up_codes_info = self.__kplDataManager.get_data(KPLDataType.LIMIT_UP)
|
now_limit_up_codes = set([d[0] for d in now_limit_up_codes_info])
|
# 获取历史涨停
|
record_limit_up_datas = KPLLimitUpDataRecordManager.total_datas
|
if not record_limit_up_datas:
|
KPLLimitUpDataRecordManager.load_total_datas()
|
record_limit_up_datas = KPLLimitUpDataRecordManager.total_datas
|
|
codes_info = []
|
for d in record_limit_up_datas:
|
if kpl_util.filter_block(d[2]) != plate:
|
continue
|
# 代码,名称,涨停时间,是否炸板,是否想买,是否已经下过单,涨停时间,自由流通市值,是否在黑名单里面
|
codes_info.append(
|
[d[3], d[4], tool.to_time_str(int(d[5])), 1 if d[3] not in now_limit_up_codes else 0, 0, 0, d[12],
|
output_util.money_desc(d[13]), 1, 1 if l2_trade_util.is_in_forbidden_trade_codes(d[3]) else 0])
|
|
for d in record_limit_up_datas:
|
if kpl_util.filter_block(d[2]) == plate:
|
continue
|
if plate not in [kpl_util.filter_block(k) for k in d[6].split("、")]:
|
continue
|
# 代码,名称,涨停时间,是否炸板,是否想买,是否已经下过单,涨停时间,自由流通市值,是否在黑名单里面
|
codes_info.append(
|
[d[3], d[4], tool.to_time_str(int(d[5])), 1 if d[3] not in now_limit_up_codes else 0, 0, 0, d[12],
|
output_util.money_desc(d[13]), 0, 1 if l2_trade_util.is_in_forbidden_trade_codes(d[3]) else 0])
|
|
codes_info.sort(key=lambda x: x[2])
|
# 查询是否为想买单
|
want_codes = gpcode_manager.WantBuyCodesManager().list_code_cache()
|
for code_info in codes_info:
|
code_info[4] = 1 if code_info[0] in want_codes else 0
|
# 获取代码状态
|
if trade_manager.CodesTradeStateManager().get_trade_state_cache(
|
code_info[0]) != trade_constant.TRADE_STATE_NOT_TRADE:
|
code_info[5] = 1
|
|
response_data = json.dumps({"code": 0, "data": codes_info})
|
elif url.path == "/kpl/get_open_limit_up_count_rank":
|
# 获取炸板次数排行
|
ps_dict = dict([(k, v[0]) for k, v in parse_qs(url.query).items()])
|
code = ps_dict.get("code")
|
results = log_export.load_kpl_open_limit_up()
|
statistic = {}
|
for result in results:
|
for c in result[1]:
|
if not tool.is_can_buy_code(c):
|
continue
|
if code and code != c:
|
continue
|
if c not in statistic:
|
statistic[c] = 0
|
statistic[c] += 1
|
# 倒序排
|
statistic_list = [(k, statistic[k]) for k in statistic]
|
statistic_list.sort(key=lambda x: x[1], reverse=True)
|
fresults = []
|
limit_up_records = KPLLimitUpDataRecordManager.list_all_cache(tool.get_now_date_str())
|
limit_up_count_dict = {}
|
if limit_up_records:
|
for d in limit_up_records:
|
limit_up_count_dict[d[3]] = d[12]
|
|
for x in statistic_list:
|
fresults.append((x[0], gpcode_manager.get_code_name(x[0]), x[1], limit_up_count_dict.get(x[0])))
|
|
fresults = fresults[:30]
|
response_data = json.dumps({"code": 0, "data": fresults})
|
elif url.path == "/get_h_cancel_data":
|
ps_dict = dict([(k, v[0]) for k, v in parse_qs(url.query).items()])
|
code = ps_dict["code"]
|
if code:
|
total_datas = l2_data_util.local_today_datas.get(code)
|
if total_datas is None:
|
l2_data_util.load_l2_data(code)
|
total_datas = l2_data_util.local_today_datas.get(code)
|
|
trade_state = trade_manager.CodesTradeStateManager().get_trade_state_cache(code)
|
if trade_state == trade_constant.TRADE_STATE_BUY_PLACE_ORDER or trade_state == trade_constant.TRADE_STATE_BUY_DELEGATED or trade_state == trade_constant.TRADE_STATE_BUY_SUCCESS:
|
hcancel_datas_dict, cancel_indexes_set = HourCancelBigNumComputer().get_watch_index_dict(code)
|
# 根据日志读取实时的计算数据
|
h_cancel_latest_compute_info = log_export.get_h_cancel_compute_info(code)
|
if hcancel_datas_dict:
|
temp_list = [(k, hcancel_datas_dict[k][0]) for k in hcancel_datas_dict]
|
canceled_indexs = set([int(k.split("-")[0]) for k in cancel_indexes_set])
|
temp_list.sort(key=lambda x: x[0])
|
fdata = {
|
"computed_info": list(
|
h_cancel_latest_compute_info) if h_cancel_latest_compute_info else None,
|
"datas": []}
|
for i in range(0, len(temp_list)):
|
temp = temp_list[i]
|
val = total_datas[temp[0]]["val"]
|
canceled = temp[0] in canceled_indexs
|
fdata["datas"].append(
|
(val["time"], val["num"],
|
code_info_output.money_desc(val["num"] * float(val["price"]) * 100),
|
(1 if canceled else 0)))
|
response_data = json.dumps({"code": 0, "data": fdata})
|
else:
|
response_data = json.dumps({"code": 1, "msg": "无H撤数据"})
|
else:
|
response_data = json.dumps({"code": 1, "msg": "无H撤数据"})
|
else:
|
response_data = json.dumps({"code": 1, "msg": "请上传code"})
|
|
elif url.path == "/get_last_trade_day_reasons":
|
|
# 计算平均涨幅
|
def get_limit_rate_list(codes):
|
if not codes:
|
return []
|
need_request_codes = set()
|
if tool.trade_time_sub(tool.get_now_time_str(), "09:30:00") < 0:
|
need_request_codes |= set(codes)
|
else:
|
now_time = time.time()
|
for c in codes:
|
if c not in self.__code_limit_rate_dict:
|
need_request_codes.add(c)
|
elif now_time - self.__code_limit_rate_dict[c][1] > 60:
|
need_request_codes.add(c)
|
if need_request_codes:
|
_limit_rate_list = HistoryKDatasUtils.get_codes_limit_rate(list(need_request_codes))
|
for d in _limit_rate_list:
|
self.__code_limit_rate_dict[d[0]] = (d[1], time.time())
|
return [(c_, self.__code_limit_rate_dict[c_][0]) for c_ in codes]
|
|
try:
|
raise Exception("接口暂停使用")
|
# 获取上个交易日的相同涨停原因的代码信息
|
ps_dict = dict([(k, v[0]) for k, v in parse_qs(url.query).items()])
|
code = ps_dict["code"]
|
# 获取昨日涨停数据
|
day = HistoryKDatasUtils.get_previous_trading_date_cache(tool.get_now_date_str())
|
|
limit_up_records = kpl_data_manager.KPLLimitUpDataRecordManager.list_all_cache(day)
|
reasons = []
|
for d in limit_up_records:
|
if d[3] == code:
|
reasons.append(d)
|
# 获取代码的原因
|
if reasons:
|
reasons = list(reasons)
|
reasons.sort(key=lambda x: x[9])
|
reason = reasons[-1][2]
|
# 获取涨停数据
|
datas = self.__kplDataManager.get_from_file_cache(kpl_util.KPLDataType.LIMIT_UP, day)
|
# (代码,名称,首次涨停时间,最近涨停时间,几板,涨停原因,板块,实际流通,主力净额,涨停原因代码,涨停原因代码数量)
|
yesterday_result_list = []
|
percent_rate = 0
|
if datas:
|
yesterday_codes = set()
|
for d in datas:
|
if d[5] == reason:
|
yesterday_codes.add(d[0])
|
# 获取涨幅
|
limit_rate_list = get_limit_rate_list(yesterday_codes)
|
limit_rate_dict = {}
|
if limit_rate_list:
|
total_rate = 0
|
for d in limit_rate_list:
|
limit_rate_dict[d[0]] = d[1]
|
total_rate += d[1]
|
percent_rate = round(total_rate / len(limit_rate_list), 2)
|
|
for d in datas:
|
if d[5] == reason:
|
yesterday_codes.add(d[0])
|
if d[0] != code:
|
# (代码,名称, 涨幅)
|
yesterday_result_list.append((d[0], d[1], limit_rate_dict.get(d[0])))
|
|
current_limit_up_list = kpl_data_manager.KPLLimitUpDataRecordManager.latest_origin_datas
|
current_result_list = []
|
if current_limit_up_list:
|
for c in current_limit_up_list:
|
if c[5] == reason and c[0] != code:
|
current_result_list.append((c[0], c[1]))
|
response_data = json.dumps({"code": 0, "data": {"reason": reason, "reason_rate": percent_rate,
|
"data": {"yesterday": yesterday_result_list,
|
"current": current_result_list}}})
|
else:
|
response_data = json.dumps({"code": 1, "msg": "昨日未涨停"})
|
except Exception as e:
|
logger_debug.exception(e)
|
raise e
|
|
elif url.path == "/pull_kp_client_msg":
|
# 拉取客户端消息
|
ps_dict = dict([(k, v[0]) for k, v in parse_qs(url.query).items()])
|
client = ps_dict["client"]
|
msg = kp_client_msg_manager.read_msg(client)
|
if msg:
|
response_data = json.dumps({"code": 0, "data": msg})
|
else:
|
response_data = json.dumps({"code": 1, "msg": "暂无消息"})
|
elif url.path == "/list_kp_client_msg":
|
msg_list = kp_client_msg_manager.list_msg_from_local()
|
msg_list.reverse()
|
msg_list = [f"{msg.split('|')[0]}{msg.split('|')[-1].split('-')[1].strip()}" for msg in msg_list]
|
response_data = json.dumps({"code": 0, "data": msg_list})
|
elif url.path == "/statistic_latest_limit_up_block":
|
try:
|
# 统计最近的涨停板块
|
datas = LatestLimitUpBlockManager().statistics_limit_up_block_infos()
|
response_data = json.dumps({"code": 0, "data": datas})
|
except Exception as e:
|
logger_debug.exception(e)
|
|
elif url.path == "/get_account_commission_detail":
|
# 获取手续费详情
|
try:
|
fdata = {"delegates": {}}
|
# 获取本月的手续费
|
end_date = tool.get_now_date_str("%Y%m%d")
|
start_date = f"{end_date[:6]}01"
|
delegates_month = trade_data_manager.AccountMoneyManager().get_delegated_count_info(start_date,
|
end_date)
|
# 股票,上证可转债 , 深证可转债
|
|
deals_month = trade_data_manager.AccountMoneyManager().get_deal_count_info(start_date, end_date)
|
cost_month = sum([round(0.1 * x[1], 2) for x in delegates_month])
|
make_month = 0
|
make_month += 5 * deals_month[0][1] + 1 * deals_month[1][1] + 0 * deals_month[2][1]
|
fdata["month_commission"] = round(make_month - cost_month, 2)
|
# 计算当日手续费详情
|
delegates = trade_data_manager.AccountMoneyManager().get_delegated_count_info()
|
delegates = [{"count": x[1], "price": 0.1, "money": round(0.1 * x[1], 2)} for x in delegates]
|
fdata["delegates"]["buy"] = delegates[0]
|
fdata["delegates"]["buy_cancel"] = delegates[1]
|
fdata["delegates"]["sell_cancel"] = delegates[2]
|
fdata["delegates"]["sell"] = delegates[3]
|
deals = trade_data_manager.AccountMoneyManager().get_deal_count_info()
|
fdata["deals"] = {}
|
fdata["deals"]["stock"]={"count": deals[0][1], "price": 5, "money": round(5 * deals[0][1], 2)}
|
fdata["deals"]["sh_cb"]={"count": deals[1][1], "price": 1, "money": round(1 * deals[1][1], 2)}
|
fdata["deals"]["sz_cb"]={"count": deals[2][1], "price": 0, "money": round(0 * deals[2][1], 2)}
|
fdata["commission"] = trade_data_manager.AccountMoneyManager().get_commission_cache()
|
response_data = json.dumps({"code": 0, "data": fdata})
|
except Exception as e:
|
logger_debug.exception(e)
|
|
async_log_util.info(logger_request_api, f"结束请求{tool.get_thread_id()}-{url}")
|
self.send_response(200)
|
# 发给请求客户端的响应数据
|
self.send_header('Content-type', 'application/json')
|
self.end_headers()
|
self.wfile.write(response_data.encode())
|
|
def do_POST(self):
|
path = self.path
|
url = urlparse.urlparse(path)
|
if url.path == "/upload_kpl_data":
|
# 接受开盘啦数据
|
params = self.__parse_request()
|
result_str = self.__process_kpl_data(params)
|
self.__send_response(result_str)
|
|
def __process_kpl_data(self, data_origin):
|
def do_limit_up(result_list_):
|
try:
|
if result_list_:
|
# 保存涨停时间
|
codes_set = set()
|
limit_up_reasons = {}
|
limit_up_time_dict = {}
|
for d in result_list_:
|
code = d[0]
|
limit_up_reasons[code] = d[5]
|
codes_set.add(code)
|
limit_up_time = time.strftime("%H:%M:%S", time.localtime(d[2]))
|
if tool.is_can_buy_code(code):
|
code_price_manager.Buy1PriceManager().set_limit_up_time(code, limit_up_time)
|
limit_up_time_dict[code] = limit_up_time
|
add_codes = codes_set - self.__latest_limit_up_codes_set
|
self.__latest_limit_up_codes_set = codes_set
|
|
if limit_up_reasons:
|
# 统计涨停原因的票的个数
|
limit_up_reason_code_dict = {}
|
for code in limit_up_reasons:
|
b = limit_up_reasons[code]
|
if b not in limit_up_reason_code_dict:
|
limit_up_reason_code_dict[b] = set()
|
limit_up_reason_code_dict[b].add(code)
|
# 涨停时间code
|
LCancelRateManager.set_block_limit_up_count(limit_up_reason_code_dict, limit_up_time_dict)
|
|
if add_codes:
|
for code in add_codes:
|
# 根据涨停原因判断是否可以买
|
if tool.is_can_buy_code(code):
|
try:
|
# 判断是否下单
|
trade_state = trade_manager.CodesTradeStateManager().get_trade_state(code)
|
if trade_state == trade_constant.TRADE_STATE_BUY_PLACE_ORDER or trade_state == trade_constant.TRADE_STATE_BUY_DELEGATED:
|
# 委托中的订单,判断是否需要撤单
|
if not gpcode_manager.WantBuyCodesManager().is_in_cache(code):
|
yesterday_codes = kpl_data_manager.get_yesterday_limit_up_codes()
|
current_limit_up_datas, limit_up_record_datas, yesterday_current_limit_up_codes, before_blocks_dict = kpl_data_manager.KPLLimitUpDataRecordManager.latest_origin_datas, kpl_data_manager.KPLLimitUpDataRecordManager.total_datas, yesterday_codes, block_info.get_before_blocks_dict()
|
if not current_limit_up_datas:
|
current_limit_up_datas = []
|
if not limit_up_record_datas:
|
limit_up_record_datas = []
|
# 买绝对老大
|
# 中途不能撤单
|
# if CodePlateKeyBuyManager.is_need_cancel(code, limit_up_reasons.get(code),
|
# current_limit_up_datas,
|
# limit_up_record_datas,
|
# yesterday_current_limit_up_codes,
|
# before_blocks_dict):
|
# l2_data_manager_new.L2TradeDataProcessor.cancel_buy(code,
|
# f"涨停原因({limit_up_reasons.get(code)})不是老大撤单",
|
# "板块撤")
|
except Exception as e:
|
logger_debug.exception(e)
|
kpl_data_manager.KPLLimitUpDataRecordManager.save_record(tool.get_now_date_str(), result_list_)
|
try:
|
LatestLimitUpBlockManager().set_current_limit_up_data(tool.get_now_date_str(), result_list_)
|
except:
|
pass
|
try:
|
CodeLimitUpSequenceManager().set_current_limit_up_datas(result_list_)
|
except:
|
pass
|
try:
|
OpenLimitUpGoodBlocksBuyStrategy.set_current_limit_up_data(result_list_)
|
CodePlateKeyBuyManager.compute_open_limit_up_code_dict_for_radical_buy(result_list_)
|
except:
|
pass
|
|
self.__kplDataManager.save_data(type_, result_list_)
|
except Exception as e:
|
logger_debug.exception(e)
|
|
# 将"概念"二字替换掉
|
data = data_origin
|
type_ = data["type"]
|
print("开盘啦type:", type_)
|
if type_ == KPLDataType.BIDDING.value:
|
result_list = kpl_util.parseDaBanData(data["data"], kpl_util.DABAN_TYPE_BIDDING)
|
# 竞价取前20
|
if result_list:
|
result_list.sort(key=lambda x: x[2])
|
result_list.reverse()
|
result_list = result_list[:20]
|
bs = []
|
for d in result_list:
|
bs.append((d[0], f"{d[2] // 10000}万"))
|
bidding_money_manager.set_bidding_money(bs[:10])
|
|
self.__kplDataManager.save_data(type_, result_list)
|
elif type_ == KPLDataType.LIMIT_UP.value:
|
result_list = kpl_util.parseLimitUpData(data["data"])
|
self.__data_process_thread_pool.submit(lambda: do_limit_up(result_list))
|
# 记录涨停日志
|
logger_kpl_limit_up.info(result_list)
|
elif type_ == KPLDataType.OPEN_LIMIT_UP.value:
|
result_list = kpl_util.parseDaBanData(data["data"], kpl_util.DABAN_TYPE_OPEN_LIMIT_UP)
|
if result_list:
|
self.__kplDataManager.save_data(type_, result_list)
|
elif type_ == KPLDataType.LIMIT_DOWN.value:
|
result_list = kpl_util.parseDaBanData(data["data"], kpl_util.DABAN_TYPE_LIMIT_DOWN)
|
if result_list:
|
self.__kplDataManager.save_data(type_, result_list)
|
elif type_ == KPLDataType.EVER_LIMIT_DOWN.value:
|
result_list = kpl_util.parseDaBanData(data["data"], kpl_util.DABAN_TYPE_EVER_LIMIT_DOWN)
|
if result_list:
|
self.__kplDataManager.save_data(type_, result_list)
|
elif type_ == KPLDataType.FENG_KOU.value:
|
fdata = data["data"]
|
result_list = kpl_util.parseFengKou(fdata)
|
result_list.sort(key=lambda x: x[3])
|
result_list.reverse()
|
self.__kplDataManager.save_data(type_, result_list)
|
elif type_ == KPLDataType.BEST_FENG_KOU.value:
|
result_list = kpl_util.parseBestFengKou(data["data"])
|
if result_list:
|
self.__kplDataManager.save_data(type_, result_list)
|
# 保存最强风口
|
elif type_ == KPLDataType.FENG_XIANG.value:
|
result_list = kpl_util.parseFengXiang(data["data"])
|
# 保存风向数据
|
if result_list:
|
self.__kplDataManager.save_data(type_, result_list)
|
elif type_ == KPLDataType.INDUSTRY_RANK.value:
|
result_list = kpl_util.parseIndustryRank(data["data"])
|
# 保存行业数据
|
if result_list:
|
self.__kplDataManager.save_data(type_, result_list)
|
RealTimeKplMarketData.set_top_5_industry(result_list)
|
elif type_ == KPLDataType.JINGXUAN_RANK.value:
|
result_list = kpl_util.parseMarketJingXuan(data["data"])
|
# 保存精选数据
|
if result_list:
|
self.__kplDataManager.save_data(type_, result_list)
|
RealTimeKplMarketData.set_top_5_reasons(result_list)
|
return json.dumps({"code": 0})
|
|
def __send_response(self, data):
|
# 发给请求客户端的响应数据
|
self.send_response(200)
|
self.send_header('Content-type', 'application/json')
|
self.end_headers()
|
self.wfile.write(data.encode())
|
|
def __parse_request(self):
|
params = {}
|
datas = self.rfile.read(int(self.headers['content-length']))
|
_str = str(datas, encoding="gbk")
|
# print(_str)
|
params = json.loads(_str)
|
return params
|
|
|
class ThreadedHTTPServer(socketserver.ThreadingMixIn, http.server.HTTPServer):
|
pass
|
|
|
def run(addr, port):
|
# 运行看盘消息采集
|
# kp_client_msg_manager.run_capture()
|
kpl_data_manager.PullTask.run_pull_task()
|
|
handler = DataServer
|
# httpd = socketserver.TCPServer((addr, port), handler)
|
try:
|
httpd = ThreadedHTTPServer((addr, port), handler)
|
print("HTTP server is at: http://%s:%d/" % (addr, port))
|
httpd.serve_forever()
|
except Exception as e:
|
logger_system.exception(e)
|
logger_system.error(f"端口服务器:{port} 启动失败")
|
|
|
if __name__ == "__main__":
|
code = "603359"
|
records = code_info_output.load_trade_record_cancel_watch_indexes(code,
|
trade_record_log_util.CancelWatchIndexesInfo.CANCEL_TYPE_H)
|
print(records)
|
|
# data = code_info_output.get_output_params(code, self.__jingxuan_cache_dict, self.__industry_cache_dict,
|
# trade_record_date=date)
|