real_trader/src/example_trade.py
2025-04-29 17:07:39 +08:00

53 lines
1.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import requests
# 服务器地址
URL = "http://trader.biggerfish.tech:9527/yu"
def buy(code: str, price: float, amount: int) -> dict:
"""买入股票
Args:
code: 股票代码,例如 "601988.SH"
price: 买入价格
amount: 买入数量
Returns:
dict: 交易结果
"""
data = {
"code": code,
"price": str(price),
"amount": str(amount)
}
response = requests.post(f"{URL}/buy", json=data)
return response.json()
def sell(code: str, price: float, amount: int) -> dict:
"""卖出股票
Args:
code: 股票代码,例如 "601988.SH"
price: 卖出价格
amount: 卖出数量
Returns:
dict: 交易结果
"""
data = {
"code": code,
"price": str(price),
"amount": str(amount)
}
response = requests.post(f"{URL}/sell", json=data)
return response.json()
if __name__ == "__main__":
# 示例买入中国银行1000股价格3.45
result = buy("601988.SH", 3.45, 1000)
print("买入结果:", result)
# 示例卖出中国银行1000股价格3.48
result = sell("601988.SH", 3.48, 1000)
print("卖出结果:", result)