34 lines
1.9 KiB
Python
34 lines
1.9 KiB
Python
from typing import Any
|
|
|
|
from .models import Assets, Position
|
|
|
|
|
|
class AccountMixin:
|
|
account_type: str
|
|
|
|
def _positions(self, path: str) -> tuple[list[str], list[Position]]:
|
|
raw = self._post(path, {"account": self.account_type}) or {}
|
|
return list(raw), [Position.from_dict(value, code) for code, value in raw.items()]
|
|
|
|
def positions(self): return self._positions("/api/v2/positions")
|
|
def holding(self): return self._positions("/api/holding")
|
|
|
|
def assets(self) -> Assets:
|
|
data = self._post("/api/v2/assets", {"account": self.account_type})
|
|
return Assets(float(data.get("total", 0)), float(data.get("available", 0)))
|
|
|
|
def total_money(self) -> float: return float(self._post("/api/money/total", {"account": self.account_type}).get("total_money", 0))
|
|
def available_money(self) -> float: return float(self._post("/api/money/available", {"account": self.account_type}).get("available_money", 0))
|
|
def buy(self, stock: str, price: float, volume: int, pr_type: int = 0): return self._order("/api/order/buy", stock, price, volume, pr_type)
|
|
def sell(self, stock: str, price: float, volume: int, pr_type: int = 0): return self._order("/api/order/sell", stock, price, volume, pr_type)
|
|
|
|
def _order(self, path, stock, price, volume, pr_type):
|
|
body = {"stock": stock, "price": price, "volume": volume}
|
|
if pr_type: body["prType"] = pr_type
|
|
return self._post(path, body)
|
|
|
|
def order_status_list(self): return self._post("/api/order/status", {"account": self.account_type}).get("orders", [])
|
|
def cancel_all(self): return self._post("/api/order/cancel_all", {"account": self.account_type})
|
|
def cancel_by_rule(self, stock: str, volume: int): return self._post("/api/order/cancel_order", {"stock": stock, "volume": volume, "account": self.account_type})
|
|
def deals(self): return self._post("/api/order/deal", {"account": self.account_type}).get("deals", [])
|