From f29f22e3cbafa9cd43fd3e5719d2f7e12929768d Mon Sep 17 00:00:00 2001 From: zhiyong Date: Sun, 11 May 2025 22:25:45 +0800 Subject: [PATCH] update example --- examples/example_trade.py | 80 +++++---------------------------------- 1 file changed, 10 insertions(+), 70 deletions(-) diff --git a/examples/example_trade.py b/examples/example_trade.py index 8bcd928..6664b9d 100644 --- a/examples/example_trade.py +++ b/examples/example_trade.py @@ -3,7 +3,7 @@ import requests # 服务器地址 URL = "http://trader.biggerfish.tech:9527/yu" # 策略名称常量 -STRATEGY = "港股ETF网格" +STRATEGY = "香港证券ETF网格" def buy(code: str, price: float, amount: int, strategy_name: str = STRATEGY) -> dict: """买入股票 @@ -41,28 +41,8 @@ def sell(code: str, price: float, amount: int, strategy_name: str = STRATEGY) -> response = requests.post(f"{URL}/sell", json=data) return response.json() -def get_positions(strategy_name: str = STRATEGY) -> dict: - """获取持仓信息 - - Args: - strategy_name: 策略名称,默认为STRATEGY常量 - """ - params = {} - if strategy_name: - params["strategy_name"] = strategy_name - response = requests.get(f"{URL}/positions", params=params) - return response.json() - -def get_target_positions(strategy_name: str = STRATEGY) -> dict: - """获取目标持仓信息(仅实盘模式有效) - - Args: - strategy_name: 策略名称,默认为STRATEGY常量 - """ - params = {"target": "true"} - if strategy_name: - params["strategy_name"] = strategy_name - response = requests.get(f"{URL}/positions", params=params) +def get_positions() -> dict: + response = requests.get(f"{URL}/positions") return response.json() def clear_strategy(strategy_name: str = STRATEGY) -> dict: @@ -98,43 +78,19 @@ def get_today_orders() -> dict: Returns: 字典形式的今日委托记录 """ - response = requests.get(f"{URL}/todayentrust") + response = requests.get(f"{URL}/todayorders") return response.json() -def cancel_order(entrust_no: str) -> dict: +def cancel_order(order_id: str) -> dict: """取消订单 Args: - entrust_no: 委托编号 + order_id: 委托编号 Returns: 取消结果 """ - response = requests.delete(f"{URL}/cancel/{entrust_no}") - return response.json() - -def get_order_status() -> dict: - """获取订单状态 - - Returns: - 字典形式的订单状态信息 - """ - response = requests.get(f"{URL}/order_status") - return response.json() - -def get_strategy_targets(strategy_name: str = STRATEGY) -> dict: - """获取策略目标持仓(仅实盘模式) - - Args: - strategy_name: 策略名称,默认为STRATEGY常量 - - Returns: - 字典形式的策略目标持仓信息 - """ - params = {} - if strategy_name: - params["strategy_name"] = strategy_name - response = requests.get(f"{URL}/strategy_targets", params=params) + response = requests.delete(f"{URL}/cancel/{order_id}") return response.json() def check_health() -> str: @@ -168,10 +124,6 @@ if __name__ == "__main__": # 模拟输出: #{'data': {'message': '模拟买入 - 代码: 601988.SH, 价格: 3.45, 数量: 3000', 'order_id': 'simulation'}, 'success': True} - # 示例:获取订单状态 - order_status = get_order_status() - print("订单状态:", order_status) - # 示例:卖出中国银行1000股,价格3.48 result = sell("601988.SH", 3.48, 1000) print("卖出结果:", result) @@ -183,8 +135,8 @@ if __name__ == "__main__": print("今日成交:", trades) # 示例:获取今日委托记录 - entrusts = get_today_orders() - print("今日委托:", entrusts) + orders = get_today_orders() + print("今日委托:", orders) # 示例:再次查询持仓变化 positions = get_positions() @@ -192,20 +144,8 @@ if __name__ == "__main__": # 模拟输出: # {'data': [{'601988.SH': {'closeable_amount': 2000, 'total_amount': 2000}}], 'success': True} - # 示例:获取目标持仓 - target_positions = get_target_positions() - print("目标持仓:", target_positions) - - # 示例:获取策略目标持仓 - strategy_targets = get_strategy_targets() - print("策略目标持仓:", strategy_targets) - - # 示例:取消订单(需要真实的委托编号) - # cancel_result = cancel_order("123456") - # print("取消订单结果:", cancel_result) - # 示例:清除策略持仓数据 - result = clear_strategy() + result = clear_strategy("测试策略名") print("清除策略持仓结果:", result) # 模拟输出: # {'message': "成功清除策略 '追梦投资港股ETF' 的持仓数据", 'success': True}