update
This commit is contained in:
+37
-93
@@ -5,8 +5,6 @@ import time
|
||||
from httpx import BasicAuth
|
||||
from typing import Dict, Any, Optional, Tuple
|
||||
|
||||
from .models import AlphaMetrics, TrainMetrics, TestMetrics, AlphaInfo
|
||||
|
||||
|
||||
class WorldQuantBrainSimulate:
|
||||
def __init__(self, credentials_file='account.txt'):
|
||||
@@ -37,6 +35,7 @@ class WorldQuantBrainSimulate:
|
||||
|
||||
if response.status_code == 201:
|
||||
print("登录成功!")
|
||||
print(f"账户信息: {response.json()}")
|
||||
return True
|
||||
else:
|
||||
print(f"登录失败: {response.json()}")
|
||||
@@ -83,7 +82,12 @@ class WorldQuantBrainSimulate:
|
||||
|
||||
if retry_after_sec == 0:
|
||||
break
|
||||
print(sim_progress_resp.json())
|
||||
if sim_progress_resp.json():
|
||||
result = sim_progress_resp.json()
|
||||
progress = result['progress']
|
||||
if progress:
|
||||
print(f"模拟进度: {float(progress)*100}%")
|
||||
|
||||
print(f"等待 {retry_after_sec} 秒...")
|
||||
time.sleep(retry_after_sec)
|
||||
|
||||
@@ -103,7 +107,7 @@ class WorldQuantBrainSimulate:
|
||||
return {"status": "success", "alpha_id": alpha_id, "metrics": metrics}
|
||||
|
||||
"""获取Alpha因子的详细指标"""
|
||||
def get_alpha_metrics(self, alpha_id: str) -> AlphaMetrics:
|
||||
def get_alpha_metrics(self, alpha_id: str) -> Dict[str, Any]:
|
||||
if self.client is None:
|
||||
raise Exception("请先登录")
|
||||
|
||||
@@ -114,99 +118,39 @@ class WorldQuantBrainSimulate:
|
||||
|
||||
if alpha_resp.status_code in [200, 201]:
|
||||
alpha_data = alpha_resp.json()
|
||||
return self._parse_alpha_metrics(alpha_data)
|
||||
|
||||
# 以后可能需要获取其他参数
|
||||
if alpha_data.get('metrics'):
|
||||
alpha_data = alpha_data.get('metrics')
|
||||
|
||||
return alpha_data or {}
|
||||
else:
|
||||
return AlphaMetrics(
|
||||
train_metrics=TrainMetrics(),
|
||||
is_metrics=TestMetrics(),
|
||||
test_metrics=TestMetrics(),
|
||||
alpha_info=AlphaInfo()
|
||||
)
|
||||
print(f"获取Alpha指标失败: {alpha_resp.status_code}")
|
||||
# 返回一个空的字典结构
|
||||
return {
|
||||
"train": {},
|
||||
"is": {},
|
||||
"test": {},
|
||||
"grade": None,
|
||||
"stage": None,
|
||||
"status": None,
|
||||
"dateCreated": None,
|
||||
"id": alpha_id
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
print(f"获取指标时出错: {str(e)}")
|
||||
return AlphaMetrics(
|
||||
train_metrics=TrainMetrics(),
|
||||
is_metrics=TestMetrics(),
|
||||
test_metrics=TestMetrics(),
|
||||
alpha_info=AlphaInfo()
|
||||
)
|
||||
|
||||
"""解析Alpha数据,提取关键指标"""
|
||||
def _parse_alpha_metrics(self, alpha_data: Dict[str, Any]) -> AlphaMetrics:
|
||||
# 解析训练集数据
|
||||
train_metrics = TrainMetrics()
|
||||
if 'train' in alpha_data and alpha_data['train']:
|
||||
train_data = alpha_data['train']
|
||||
train_metrics = TrainMetrics(
|
||||
sharpe_ratio=train_data.get('sharpe'),
|
||||
annual_return=train_data.get('returns'),
|
||||
max_drawdown=train_data.get('drawdown'),
|
||||
turnover=train_data.get('turnover'),
|
||||
fitness=train_data.get('fitness'),
|
||||
pnl=train_data.get('pnl'),
|
||||
book_size=train_data.get('bookSize'),
|
||||
long_count=train_data.get('longCount'),
|
||||
short_count=train_data.get('shortCount'),
|
||||
margin=train_data.get('margin'),
|
||||
)
|
||||
|
||||
# 解析样本内测试数据
|
||||
is_metrics = TestMetrics()
|
||||
if 'is' in alpha_data and alpha_data['is']:
|
||||
is_data = alpha_data['is']
|
||||
is_metrics = TestMetrics(
|
||||
sharpe_ratio=is_data.get('sharpe'),
|
||||
annual_return=is_data.get('returns'),
|
||||
max_drawdown=is_data.get('drawdown'),
|
||||
turnover=is_data.get('turnover'),
|
||||
fitness=is_data.get('fitness'),
|
||||
pnl=is_data.get('pnl'),
|
||||
)
|
||||
|
||||
# 解析样本外测试数据
|
||||
test_metrics = TestMetrics()
|
||||
if 'test' in alpha_data and alpha_data['test']:
|
||||
test_data = alpha_data['test']
|
||||
test_metrics = TestMetrics(
|
||||
sharpe_ratio=test_data.get('sharpe'),
|
||||
annual_return=test_data.get('returns'),
|
||||
max_drawdown=test_data.get('drawdown'),
|
||||
turnover=test_data.get('turnover'),
|
||||
fitness=test_data.get('fitness'),
|
||||
pnl=test_data.get('pnl'),
|
||||
)
|
||||
|
||||
# 解析Alpha基本信息
|
||||
alpha_info = AlphaInfo(
|
||||
grade=alpha_data.get('grade'),
|
||||
stage=alpha_data.get('stage'),
|
||||
status=alpha_data.get('status'),
|
||||
date_created=alpha_data.get('dateCreated'),
|
||||
)
|
||||
|
||||
# 解析检查结果
|
||||
if 'is' in alpha_data and 'checks' in alpha_data['is']:
|
||||
checks = alpha_data['is']['checks']
|
||||
check_results = {}
|
||||
for check in checks:
|
||||
check_name = check.get('name', '')
|
||||
result = check.get('result', '')
|
||||
value = check.get('value', None)
|
||||
check_results[check_name.lower()] = {
|
||||
'result': result,
|
||||
'value': value,
|
||||
'limit': check.get('limit', None)
|
||||
}
|
||||
alpha_info.checks = check_results
|
||||
|
||||
return AlphaMetrics(
|
||||
train_metrics=train_metrics,
|
||||
is_metrics=is_metrics,
|
||||
test_metrics=test_metrics,
|
||||
alpha_info=alpha_info,
|
||||
alpha_id=alpha_data.get('id')
|
||||
)
|
||||
# 返回一个空的字典结构
|
||||
return {
|
||||
"train": {},
|
||||
"is": {},
|
||||
"test": {},
|
||||
"grade": None,
|
||||
"stage": None,
|
||||
"status": None,
|
||||
"dateCreated": None,
|
||||
"id": alpha_id
|
||||
}
|
||||
|
||||
def close(self):
|
||||
"""关闭连接"""
|
||||
|
||||
+2
-5
@@ -56,10 +56,7 @@ class SimulationResult:
|
||||
time_consuming: float
|
||||
formatted_time: str
|
||||
alpha_id: str
|
||||
status: str # success, error, failed
|
||||
status: str
|
||||
description: str
|
||||
simulation_timestamp: str
|
||||
train_metrics: Optional[TrainMetrics] = None
|
||||
is_metrics: Optional[TestMetrics] = None
|
||||
test_metrics: Optional[TestMetrics] = None
|
||||
alpha_info: Optional[AlphaInfo] = None
|
||||
metrics: Optional[Dict[str, Any]] = None
|
||||
Reference in New Issue
Block a user