風控閘門¶
trading engine 在委託送到券商之前套用的事前風控:每個 bot 的資金配額、每根 K 棒的下單次數上限、重複委託的時間窗,以及虧損停損開關。每一項預設都是關閉的。
cjtrade.pkgs.risk.pre_trade
¶
Pre-trade risk controls.
Separate from a broker's own checks, and not a replacement for them. A broker answers "can this account do this at all" -- buying power, available-to-sell, price bands. This answers "should this strategy do this now" -- its capital allocation, its order rate, whether trading has been halted. No real broker will enforce your per-strategy budget.
Every control is off by default (None), so a caller opts into exactly what it wants and the gate never silently changes behaviour it was not asked about.
Usage::
from cjtrade.pkgs.risk import RiskGate, RiskLimits
gate = RiskGate(RiskLimits(max_notional_per_bot=500_000,
max_orders_per_bar=5,
max_loss_pct=0.10),
initial_equity=1_000_000)
gate.start_bar(bar_index)
if gate.observe_equity(equity): # kill switch tripped
... # cancel resting orders, stop trading
reason = gate.check("Bot_C", "2317", "BUY", 20, 240.0)
if reason:
... # refuse, do not reach the broker
gate.record_submitted("Bot_C", "2317", "BUY", 20, 240.0, order_id=oid)
...
gate.record_outcome(oid, filled=False) # cancelled: capital released
RiskLimits(max_notional_per_bot=None, max_orders_per_bar=None, max_loss_pct=None, duplicate_window_bars=0)
dataclass
¶
Which controls are on. None disables that one.
Attributes:
| Name | Type | Description |
|---|---|---|
max_notional_per_bot |
(float, optional)
|
Capital a single strategy may have deployed at once: resting buys plus the notional it has actually bought. This is what keeps N strategies on one account from each sizing against the full equity. |
max_orders_per_bar |
(int, optional)
|
Runaway protection. A strategy that re-fires every bar is a bug, and without this the broker absorbs it as a reject storm. |
max_loss_pct |
(float, optional)
|
Kill switch. Equity below |
duplicate_window_bars |
int
|
An identical (bot, symbol, action, quantity, price) within this many bars is refused. 0 disables it. |
RiskGate(limits, initial_equity)
¶
The one place an order can be refused before it reaches the broker.
Not thread-safe: drive it from the single task that owns execution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
limits
|
RiskLimits
|
Which controls are on. |
required |
initial_equity
|
float
|
Equity the |
required |
Source code in src/cjtrade/pkgs/risk/pre_trade.py
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | |
halted
property
¶
True once trading has been halted.
halt_reason
property
¶
Why trading was halted, or None.
exposures
property
¶
Every bot that has had an order accepted, with its exposure.
Read-only: asking does not register a bot, so a status endpoint or a log line can look without writing, on an object documented above as not thread-safe.
exposure(bot_id)
¶
Capital the bot has committed: resting buys plus held cost basis.
Source code in src/cjtrade/pkgs/risk/pre_trade.py
114 115 116 117 118 119 120 121 122 123 | |
start_bar(bar_index)
¶
Reset per-bar counters. Call once per bar before any check().
Source code in src/cjtrade/pkgs/risk/pre_trade.py
135 136 137 138 139 140 141 | |
observe_equity(equity)
¶
Kill switch. Returns the halt reason the first time it trips.
Returns None once already halted, so a caller can use a truthy return as "halt right now" without re-running its shutdown every bar.
Source code in src/cjtrade/pkgs/risk/pre_trade.py
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | |
halt(reason)
¶
Trip the kill switch by hand.
Source code in src/cjtrade/pkgs/risk/pre_trade.py
161 162 163 164 | |
check(bot_id, symbol, action, quantity, price)
¶
Rejection reason, or None to let the order through.
Checked in order of how cheap the refusal is: a halted engine refuses everything, then the rate limit, then the duplicate, then the budget.
Source code in src/cjtrade/pkgs/risk/pre_trade.py
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | |
record_submitted(bot_id, symbol, action, quantity, price, order_id=None)
¶
An order the broker accepted. Call only after check() allowed it.
With an order_id the order counts against the bot's allocation
until record_outcome() resolves it. A resting buy commits the capital
from submission, not from the fill, which is too late to refuse the
next order. Without one, only the rate and duplicate checks see it.
Source code in src/cjtrade/pkgs/risk/pre_trade.py
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 | |
record_outcome(order_id, filled)
¶
An order reached a terminal state, filled in full or not at all.
An order the gate is not holding is ignored: one the broker rejected on placement was never recorded, and an order resolved twice (a cancel racing a fill) must only count once.
Source code in src/cjtrade/pkgs/risk/pre_trade.py
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 | |