refactor: 字段与接口路径改用中性演示命名,并扩充下单用例矩阵
This commit is contained in:
+26
-26
@@ -49,11 +49,11 @@ CURRENCY_ID = 1 # 结算币种(演示固定为 1)
|
||||
class MatchEngine:
|
||||
|
||||
_ROUTES = {
|
||||
"/api/futures/addOrder": "add_order",
|
||||
"/api/futures/cancelOrder": "cancel_order",
|
||||
"/api/future/user/transferIn": "transfer_in",
|
||||
"/api/future/mark/setMarkPrice": "set_mark_price",
|
||||
"/api/future/risk/check": "risk_check",
|
||||
"/api/v1/orders": "add_order",
|
||||
"/api/v1/orders/cancel": "cancel_order",
|
||||
"/api/v1/account/transfer": "transfer_in",
|
||||
"/api/v1/market/mark-price": "set_mark_price",
|
||||
"/api/v1/risk/check": "risk_check",
|
||||
}
|
||||
|
||||
def __init__(self):
|
||||
@@ -108,8 +108,8 @@ class MatchEngine:
|
||||
margin_type = self._to_int(body, "marginType")
|
||||
if margin_type not in (1, 2):
|
||||
raise ParamError("marginType")
|
||||
if body.get("durationQualifiers", "GTC") not in ("GTC", "IOC", "FOK", "GTX"):
|
||||
raise ParamError("durationQualifiers")
|
||||
if body.get("timeInForce", "GTC") not in ("GTC", "IOC", "FOK", "GTX"):
|
||||
raise ParamError("timeInForce")
|
||||
margin_rate = self._to_decimal(body, "marginRate")
|
||||
if not Decimal("0") < margin_rate <= Decimal("1"):
|
||||
raise ParamError("marginRate")
|
||||
@@ -145,7 +145,7 @@ class MatchEngine:
|
||||
account[frozen_field] += frozen
|
||||
self._add_flow(user_id, FLOW_FREEZE, frozen, order_no)
|
||||
self.cache.set(
|
||||
"frozen_margin:userId:%s:orderNo:%s" % (user_id, order_no), frozen)
|
||||
"order_freeze:userId:%s:orderNo:%s" % (user_id, order_no), frozen)
|
||||
self._sync_account_cache(user_id)
|
||||
return 0, "OK", {"orderNo": order_no}
|
||||
|
||||
@@ -214,7 +214,7 @@ class MatchEngine:
|
||||
account[frozen_field] -= frozen
|
||||
order["status"] = ORDER_STATUS_CANCELED
|
||||
self._add_flow(user_id, FLOW_UNFREEZE, frozen, order_no)
|
||||
self.cache.delete("frozen_margin:userId:%s:orderNo:%s" % (user_id, order_no))
|
||||
self.cache.delete("order_freeze:userId:%s:orderNo:%s" % (user_id, order_no))
|
||||
self._sync_account_cache(user_id)
|
||||
return 0, "OK", {"contractId": contract_id, "orderNo": order_no}
|
||||
|
||||
@@ -278,14 +278,14 @@ class MatchEngine:
|
||||
# ------------------------------------------------------------------
|
||||
def init_user(self, user_id, balance):
|
||||
"""初始化用户合约资金账户并划入初始资金。"""
|
||||
self.db.insert("account_future", {
|
||||
self.db.insert("futures_account", {
|
||||
"user_id": user_id,
|
||||
"currency_id": CURRENCY_ID,
|
||||
"total_money": Decimal(str(balance)),
|
||||
"cross_position_margin": Decimal("0"),
|
||||
"cross_frozen_position_margin": Decimal("0"),
|
||||
"isolated_position_margin": Decimal("0"),
|
||||
"isolated_frozen_position_margin": Decimal("0"),
|
||||
"cross_margin": Decimal("0"),
|
||||
"cross_frozen_margin": Decimal("0"),
|
||||
"isolated_margin": Decimal("0"),
|
||||
"isolated_frozen_margin": Decimal("0"),
|
||||
})
|
||||
self._add_flow(user_id, FLOW_TRANSFER_IN, Decimal(str(balance)), None)
|
||||
self._sync_account_cache(user_id)
|
||||
@@ -312,7 +312,7 @@ class MatchEngine:
|
||||
account["total_money"] += balance
|
||||
|
||||
pos["status"] = POSITION_STATUS_LIQUIDATED
|
||||
self.db.insert("force_close_record", {
|
||||
self.db.insert("liquidation_record", {
|
||||
"user_id": user_id,
|
||||
"contract_id": contract_id,
|
||||
"position_no": pos["position_no"],
|
||||
@@ -320,7 +320,7 @@ class MatchEngine:
|
||||
"bankrupt_price": bankrupt,
|
||||
"margin_balance": balance,
|
||||
})
|
||||
self.db.insert("risk_fund_flow", {
|
||||
self.db.insert("risk_fund_ledger", {
|
||||
"user_id": user_id,
|
||||
"contract_id": contract_id,
|
||||
"position_no": pos["position_no"],
|
||||
@@ -337,25 +337,25 @@ class MatchEngine:
|
||||
})
|
||||
|
||||
def _get_account(self, user_id):
|
||||
rows = self.db.select("account_future", user_id=user_id)
|
||||
rows = self.db.select("futures_account", user_id=user_id)
|
||||
return rows[0] if rows else None
|
||||
|
||||
@staticmethod
|
||||
def _margin_field(margin_type):
|
||||
"""仓位保证金字段:1-全仓 2-逐仓。"""
|
||||
return "cross_position_margin" if margin_type == 1 else "isolated_position_margin"
|
||||
return "cross_margin" if margin_type == 1 else "isolated_margin"
|
||||
|
||||
@staticmethod
|
||||
def _frozen_field(margin_type):
|
||||
"""冻结保证金字段:1-全仓 2-逐仓。"""
|
||||
if margin_type == 1:
|
||||
return "cross_frozen_position_margin"
|
||||
return "isolated_frozen_position_margin"
|
||||
return "cross_frozen_margin"
|
||||
return "isolated_frozen_margin"
|
||||
|
||||
def _add_flow(self, user_id, flow_type, quantity, order_no):
|
||||
"""写一条用户资金流水。"""
|
||||
self._flow_seq += 1
|
||||
self.db.insert("user_currency_flow", {
|
||||
self.db.insert("account_flow", {
|
||||
"id": self._flow_seq,
|
||||
"user_id": user_id,
|
||||
"currency_id": CURRENCY_ID,
|
||||
@@ -368,15 +368,15 @@ class MatchEngine:
|
||||
"""把资金表行镜像写入缓存(camelCase,模拟缓存与库字段命名不一致)。"""
|
||||
account = self._get_account(user_id)
|
||||
self.cache.set(
|
||||
"account_future:userId:%s:currencyId:%s" % (user_id, CURRENCY_ID),
|
||||
"futures_account:userId:%s:currencyId:%s" % (user_id, CURRENCY_ID),
|
||||
json.dumps({
|
||||
"userId": str(user_id),
|
||||
"currencyId": str(CURRENCY_ID),
|
||||
"totalMoney": str(account["total_money"]),
|
||||
"crossPositionMargin": str(account["cross_position_margin"]),
|
||||
"crossFrozenPositionMargin": str(account["cross_frozen_position_margin"]),
|
||||
"isolatedPositionMargin": str(account["isolated_position_margin"]),
|
||||
"isolatedFrozenPositionMargin": str(account["isolated_frozen_position_margin"]),
|
||||
"crossMargin": str(account["cross_margin"]),
|
||||
"crossFrozenMargin": str(account["cross_frozen_margin"]),
|
||||
"isolatedMargin": str(account["isolated_margin"]),
|
||||
"isolatedFrozenMargin": str(account["isolated_frozen_margin"]),
|
||||
}))
|
||||
|
||||
def _sync_position_cache(self, user_id, contract_id):
|
||||
|
||||
Reference in New Issue
Block a user