add more example
This commit is contained in:
parent
aec79fb718
commit
6cb24fb618
@ -41,13 +41,27 @@ def sell(code: str, price: float, amount: int, strategy_name: str) -> dict:
|
|||||||
response = requests.post(f"{URL}/sell", json=data)
|
response = requests.post(f"{URL}/sell", json=data)
|
||||||
return response.json()
|
return response.json()
|
||||||
|
|
||||||
def get_positions(strategy_name: str) -> dict:
|
def get_positions(strategy_name: str = "") -> dict:
|
||||||
"""获取持仓信息
|
"""获取持仓信息
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
strategy_name: 策略名称
|
strategy_name: 策略名称,默认为空字符串,表示获取所有持仓
|
||||||
"""
|
"""
|
||||||
params = {"strategy_name": strategy_name}
|
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 = "") -> dict:
|
||||||
|
"""获取目标持仓信息(仅实盘模式有效)
|
||||||
|
|
||||||
|
Args:
|
||||||
|
strategy_name: 策略名称,默认为空字符串,表示获取所有策略的目标持仓
|
||||||
|
"""
|
||||||
|
params = {"target": "true"}
|
||||||
|
if strategy_name:
|
||||||
|
params["strategy_name"] = strategy_name
|
||||||
response = requests.get(f"{URL}/positions", params=params)
|
response = requests.get(f"{URL}/positions", params=params)
|
||||||
return response.json()
|
return response.json()
|
||||||
|
|
||||||
@ -60,14 +74,93 @@ def clear_strategy(strategy_name: str) -> dict:
|
|||||||
response = requests.delete(f"{URL}/clear/{strategy_name}")
|
response = requests.delete(f"{URL}/clear/{strategy_name}")
|
||||||
return response.json()
|
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_entrust() -> dict:
|
||||||
|
"""获取今日委托记录(仅实盘模式)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
字典形式的今日委托记录
|
||||||
|
"""
|
||||||
|
response = requests.get(f"{URL}/todayentrust")
|
||||||
|
return response.json()
|
||||||
|
|
||||||
|
def cancel_order(entrust_no: str) -> dict:
|
||||||
|
"""取消订单
|
||||||
|
|
||||||
|
Args:
|
||||||
|
entrust_no: 委托编号
|
||||||
|
|
||||||
|
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 = "") -> dict:
|
||||||
|
"""获取策略目标持仓(仅实盘模式)
|
||||||
|
|
||||||
|
Args:
|
||||||
|
strategy_name: 策略名称,默认为空字符串,表示获取所有策略的目标持仓
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
字典形式的策略目标持仓信息
|
||||||
|
"""
|
||||||
|
params = {}
|
||||||
|
if strategy_name:
|
||||||
|
params["strategy_name"] = strategy_name
|
||||||
|
response = requests.get(f"{URL}/strategy_targets", params=params)
|
||||||
|
return response.json()
|
||||||
|
|
||||||
|
def check_health() -> str:
|
||||||
|
"""健康检查
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
健康状态
|
||||||
|
"""
|
||||||
|
response = requests.get(f"{URL}/healthcheck")
|
||||||
|
return response.text
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
# 示例:健康检查
|
||||||
|
health = check_health()
|
||||||
|
print("健康状态:", health)
|
||||||
|
|
||||||
# 示例:查询默认策略的持仓信息
|
# 示例:查询默认策略的持仓信息
|
||||||
positions = get_positions(STRATEGY)
|
positions = get_positions(STRATEGY)
|
||||||
print("当前持仓:", positions)
|
print("当前持仓:", positions)
|
||||||
# 模拟输出:
|
# 模拟输出:
|
||||||
# {'data': [], 'success': True}
|
# {'data': [], 'success': True}
|
||||||
|
|
||||||
|
# 示例:查询账户余额(仅实盘模式)
|
||||||
|
balance = get_balance()
|
||||||
|
print("账户余额:", balance)
|
||||||
|
|
||||||
# 示例:买入中国银行1000股,价格3.45
|
# 示例:买入中国银行1000股,价格3.45
|
||||||
result = buy("601988.SH", 3.45, 3000, STRATEGY)
|
result = buy("601988.SH", 3.45, 3000, STRATEGY)
|
||||||
@ -75,6 +168,9 @@ if __name__ == "__main__":
|
|||||||
# 模拟输出:
|
# 模拟输出:
|
||||||
#{'data': {'message': '模拟买入 - 代码: 601988.SH, 价格: 3.45, 数量: 3000', 'order_id': 'simulation'}, 'success': True}
|
#{'data': {'message': '模拟买入 - 代码: 601988.SH, 价格: 3.45, 数量: 3000', 'order_id': 'simulation'}, 'success': True}
|
||||||
|
|
||||||
|
# 示例:获取订单状态
|
||||||
|
order_status = get_order_status()
|
||||||
|
print("订单状态:", order_status)
|
||||||
|
|
||||||
# 示例:卖出中国银行1000股,价格3.48
|
# 示例:卖出中国银行1000股,价格3.48
|
||||||
result = sell("601988.SH", 3.48, 1000, STRATEGY)
|
result = sell("601988.SH", 3.48, 1000, STRATEGY)
|
||||||
@ -82,12 +178,32 @@ if __name__ == "__main__":
|
|||||||
# 模拟输出:
|
# 模拟输出:
|
||||||
# {'data': {'message': '模拟卖出 - 代码: 601988.SH, 价格: 3.48, 数量: 1000', 'order_id': 'simulation'}, 'success': True}
|
# {'data': {'message': '模拟卖出 - 代码: 601988.SH, 价格: 3.48, 数量: 1000', 'order_id': 'simulation'}, 'success': True}
|
||||||
|
|
||||||
|
# 示例:获取今日成交记录
|
||||||
|
trades = get_today_trades()
|
||||||
|
print("今日成交:", trades)
|
||||||
|
|
||||||
|
# 示例:获取今日委托记录
|
||||||
|
entrusts = get_today_entrust()
|
||||||
|
print("今日委托:", entrusts)
|
||||||
|
|
||||||
# 示例:再次查询持仓变化
|
# 示例:再次查询持仓变化
|
||||||
positions = get_positions(STRATEGY)
|
positions = get_positions(STRATEGY)
|
||||||
print("交易后持仓:", positions)
|
print("交易后持仓:", positions)
|
||||||
# 模拟输出:
|
# 模拟输出:
|
||||||
# {'data': [{'601988.SH': {'closeable_amount': 2000, 'total_amount': 2000}}], 'success': True}
|
# {'data': [{'601988.SH': {'closeable_amount': 2000, 'total_amount': 2000}}], 'success': True}
|
||||||
|
|
||||||
|
# 示例:获取目标持仓
|
||||||
|
target_positions = get_target_positions(STRATEGY)
|
||||||
|
print("目标持仓:", target_positions)
|
||||||
|
|
||||||
|
# 示例:获取策略目标持仓
|
||||||
|
strategy_targets = get_strategy_targets(STRATEGY)
|
||||||
|
print("策略目标持仓:", strategy_targets)
|
||||||
|
|
||||||
|
# 示例:取消订单(需要真实的委托编号)
|
||||||
|
# cancel_result = cancel_order("123456")
|
||||||
|
# print("取消订单结果:", cancel_result)
|
||||||
|
|
||||||
# 示例:清除策略持仓数据
|
# 示例:清除策略持仓数据
|
||||||
result = clear_strategy(STRATEGY)
|
result = clear_strategy(STRATEGY)
|
||||||
print("清除策略持仓结果:", result)
|
print("清除策略持仓结果:", result)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user