You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
73 lines
1.9 KiB
73 lines
1.9 KiB
import requests
|
|
from time import sleep
|
|
|
|
def quick_test_alpha(username, password, alpha_expression, region="USA"):
|
|
"""
|
|
快速测试alpha表达式
|
|
|
|
Parameters:
|
|
- username: 用户名
|
|
- password: 密码
|
|
- alpha_expression: 因子表达式
|
|
- region: 区域 (USA, EUROPE, ASIA, CHINA等)
|
|
"""
|
|
|
|
# 登录
|
|
s = requests.Session()
|
|
s.auth = (username, password)
|
|
s.post('https://api.worldquantbrain.com/authentication')
|
|
|
|
# 设置
|
|
settings = {
|
|
'instrumentType': 'EQUITY',
|
|
'region': region,
|
|
'universe': 'TOP3000',
|
|
'delay': 1,
|
|
'decay': 8,
|
|
'neutralization': 'SUBINDUSTRY',
|
|
'truncation': 0.08,
|
|
'pasteurization': 'ON',
|
|
'testPeriod': 'P2Y',
|
|
'language': 'FASTEXPR',
|
|
}
|
|
|
|
# 提交测试
|
|
simulation_data = {
|
|
'type': 'REGULAR',
|
|
'settings': settings,
|
|
'regular': alpha_expression
|
|
}
|
|
|
|
response = s.post('https://api.worldquantbrain.com/simulations', json=simulation_data)
|
|
progress_url = response.headers['Location']
|
|
|
|
# 等待结果
|
|
while True:
|
|
progress = s.get(progress_url)
|
|
if progress.headers.get("Retry-After", 0) == 0:
|
|
break
|
|
sleep(float(progress.headers["Retry-After"]))
|
|
|
|
result = progress.json()
|
|
|
|
if result["status"] == "COMPLETE":
|
|
alpha_info = result["is"]
|
|
return {
|
|
"alpha_id": result["alpha"],
|
|
"sharpe": alpha_info["sharpe"],
|
|
"fitness": alpha_info["fitness"],
|
|
"turnover": alpha_info["turnover"],
|
|
"margin": alpha_info["margin"]
|
|
}
|
|
else:
|
|
return {"error": "模拟失败", "status": result["status"]}
|
|
|
|
# 使用示例
|
|
if __name__ == "__main__":
|
|
result = quick_test_alpha(
|
|
"jack0210_@hotmail.com",
|
|
"!QAZ2wsx+0913",
|
|
"rank(ts_delta(close, 5))", # 你的alpha表达式
|
|
"USA"
|
|
)
|
|
print(result) |