import requests # 服务器地址 URL = "http://trader.biggerfish.tech:9527/yu" # URL = "http://localhost:9527/yu" # 策略名称常量 STRATEGY = "香港证券ETF网格" def buy(code: str, price: float, amount: int, strategy_name: str = STRATEGY, order_type: str = "market") -> dict: """买入股票 Args: code: 股票代码,例如 "601988.SH" price: 买入价格 amount: 买入数量 strategy_name: 策略名称 order_type: 订单类型,可选值为 "market" 或 "limit" """ data = { "code": code, "price": str(price), "amount": str(amount), "strategy_name": strategy_name, "order_type": order_type } response = requests.post(f"{URL}/buy", json=data) return response.json() def sell(code: str, price: float, amount: int, strategy_name: str = STRATEGY, order_type: str = "market") -> dict: """卖出股票 Args: code: 股票代码,例如 "601988.SH" price: 卖出价格 amount: 卖出数量 strategy_name: 策略名称 order_type: 订单类型,可选值为 "market" 或 "limit" """ data = { "code": code, "price": str(price), "amount": str(amount), "strategy_name": strategy_name, "order_type": order_type } response = requests.post(f"{URL}/sell", json=data) return response.json() def get_positions() -> dict: response = requests.get(f"{URL}/positions") return response.json() def clear_strategy(strategy_name: str = STRATEGY) -> dict: """清除策略持仓数据 Args: strategy_name: 策略名称 """ response = requests.delete(f"{URL}/clear/{strategy_name}") return response.json() def get_balance() -> dict: """获取账户余额(仅实盘模式) Returns: 字典形式的账户余额信息 """ response = requests.get(f"{URL}/balance") return response.json() def get_today_trades() -> dict: """获取今日成交记录(仅实盘模式) Returns: 字典形式的今日成交记录 """ response = requests.get(f"{URL}/todaytrades") return response.json() def get_today_orders() -> dict: """获取今日委托记录(仅实盘模式) Returns: 字典形式的今日委托记录 """ response = requests.get(f"{URL}/todayorders") return response.json() def cancel_order(order_id: str) -> dict: """取消订单 Args: order_id: 委托编号 Returns: 取消结果 """ response = requests.delete(f"{URL}/cancel/{order_id}") return response.json() def check_health() -> str: """健康检查 Returns: 健康状态 """ response = requests.get(f"{URL}/healthcheck") return response.json() if __name__ == "__main__": # 示例:健康检查 health = check_health() print("健康状态:", health) # 示例:查询默认策略的持仓信息 positions = get_positions() print("当前持仓:", positions) # 模拟输出: # {'data': [], 'success': True} # 示例:查询账户余额(仅实盘模式) balance = get_balance() print("账户余额:", balance) # 示例:买入中国银行1000股,价格3.45 result = buy("513090.SH", 3.45, 3000) print("买入结果:", result) # 模拟输出: #{'data': {'message': '模拟买入 - 代码: 601988.SH, 价格: 3.45, 数量: 3000', 'order_id': 'simulation'}, 'success': True} # 示例:卖出中国银行1000股,价格3.48 result = sell("513090.SH", 3.48, 1000) print("卖出结果:", result) # 模拟输出: # {'data': {'message': '模拟卖出 - 代码: 601988.SH, 价格: 3.48, 数量: 1000', 'order_id': 'simulation'}, 'success': True} # 示例:获取今日成交记录 trades = get_today_trades() print("今日成交:", trades) # 示例:获取今日委托记录 orders = get_today_orders() print("今日委托:", orders) # 示例:再次查询持仓变化 positions = get_positions() print("交易后持仓:", positions) # 模拟输出: # {'data': [{'601988.SH': {'closeable_amount': 2000, 'total_amount': 2000}}], 'success': True} # 示例:清除策略持仓数据 result = clear_strategy(STRATEGY) print("清除策略持仓结果:", result) # 模拟输出: # {'message': "成功清除策略 '追梦投资港股ETF' 的持仓数据", 'success': True} # 示例:清除后查询持仓 positions = get_positions() print("清除后持仓:", positions) # 模拟输出: # {'data': [], 'success': True}