feat: add clear strategy positions
This commit is contained in:
parent
93dfcf165f
commit
5bb9740182
@ -51,6 +51,15 @@ def get_positions(strategy_name: str) -> dict:
|
||||
response = requests.get(f"{URL}/positions", params=params)
|
||||
return response.json()
|
||||
|
||||
def clear_strategy(strategy_name: str) -> dict:
|
||||
"""清除策略持仓数据
|
||||
|
||||
Args:
|
||||
strategy_name: 策略名称
|
||||
"""
|
||||
response = requests.delete(f"{URL}/clear/{strategy_name}")
|
||||
return response.json()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# 示例:查询默认策略的持仓信息
|
||||
@ -79,3 +88,15 @@ if __name__ == "__main__":
|
||||
# 模拟输出:
|
||||
# {'data': [{'601988.SH': {'closeable_amount': 2000, 'total_amount': 2000}}], 'success': True}
|
||||
|
||||
# 示例:清除策略持仓数据
|
||||
result = clear_strategy(STRATEGY)
|
||||
print("清除策略持仓结果:", result)
|
||||
# 模拟输出:
|
||||
# {'success': True, 'message': "成功清除策略 '追梦投资港股ETF' 的持仓数据"}
|
||||
|
||||
# 示例:清除后查询持仓
|
||||
positions = get_positions(STRATEGY)
|
||||
print("清除后持仓:", positions)
|
||||
# 模拟输出:
|
||||
# {'data': [], 'success': True}
|
||||
|
@ -704,6 +704,44 @@ def get_today_entrust():
|
||||
abort(500, description="Internal server error")
|
||||
|
||||
|
||||
@app.route("/yu/clear/<strategy_name>", methods=["DELETE"])
|
||||
def clear_strategy(strategy_name):
|
||||
"""清除指定策略的持仓管理数据"""
|
||||
logger.info(f"接收到清除策略持仓请求: {strategy_name}")
|
||||
try:
|
||||
if not strategy_name:
|
||||
raise ValueError("缺少策略名称参数")
|
||||
|
||||
# 检查策略是否存在
|
||||
if strategy_name in strategy_positions:
|
||||
# 从策略持仓字典中删除该策略
|
||||
del strategy_positions[strategy_name]
|
||||
# 清除该策略的交易记录
|
||||
if strategy_name in strategy_trades:
|
||||
del strategy_trades[strategy_name]
|
||||
|
||||
# 清除与该策略相关的未完成委托
|
||||
for order_id, order_info in list(pending_orders.items()):
|
||||
if order_info.get('strategy_name') == strategy_name:
|
||||
del pending_orders[order_id]
|
||||
|
||||
# 保存更新后的策略数据
|
||||
save_strategy_data()
|
||||
|
||||
logger.info(f"成功清除策略持仓数据: {strategy_name}")
|
||||
return jsonify({"success": True, "message": f"成功清除策略 '{strategy_name}' 的持仓数据"}), 200
|
||||
else:
|
||||
logger.info(f"策略不存在或没有持仓数据: {strategy_name}")
|
||||
return jsonify({"success": True, "message": f"策略 '{strategy_name}' 不存在或没有持仓数据"}), 200
|
||||
|
||||
except ValueError as e:
|
||||
logger.error(f"无效的请求参数: {str(e)}")
|
||||
abort(400, description=str(e))
|
||||
except Exception as e:
|
||||
logger.error(f"清除策略持仓时出错: {str(e)}")
|
||||
abort(500, description="服务器内部错误")
|
||||
|
||||
|
||||
# 超时处理函数
|
||||
def execute_with_timeout(func, timeout, *args, **kwargs):
|
||||
with concurrent.futures.ThreadPoolExecutor(max_workers=1) as executor:
|
||||
|
Loading…
x
Reference in New Issue
Block a user