exchange.IO("extend", ...)
Inject parameters into requests sent by an exchange object (such as the exchange's self-trade prevention mode or recvWindow) without modifying calls to order placement or other functions. Each rule uses regular expressions to match the request's target string: for REST requests, the HTTP method followed by the endpoint path (for example, POST/api/v3/order); for ctx.submit, ws:order for WebSocket order placement and ws:cancel for order cancellation.
exchange.IO(k)
exchange.IO(k, rules)Examples
-
Add a self-trade prevention mode to both REST and WebSocket order placement for Binance Spot, then query the rules' hit counts.
javascriptfunction main() { var ok = exchange.IO("extend", [ { match: ["^POST/api/v3/order$", "^ws:order$"], params: {selfTradePreventionMode: "EXPIRE_TAKER"} } ]) if (!ok) { Log("bad rules:", GetLastError()) return } var t = exchange.GetTicker("BTC_USDT") if (t) { ctx.order(exchange, "BTC_USDT", "buy", _N(t.Buy * 0.98, 2), 0.001) } Log(exchange.IO("extend")) // [{match, params, force, hits}] }pythondef main(): ok = exchange.IO("extend", [ { "match": ["^POST/api/v3/order$", "^ws:order$"], "params": {"selfTradePreventionMode": "EXPIRE_TAKER"} } ]) if not ok: Log("bad rules:", GetLastError()) return t = exchange.GetTicker("BTC_USDT") if t: ctx.order(exchange, "BTC_USDT", "buy", _N(t["Buy"] * 0.98, 2), 0.001) Log(exchange.IO("extend")) # [{match, params, force, hits}]rustfn main() { // Define the rules as a JSON string let rules = r#"[{ "match": ["^POST/api/v3/order$", "^ws:order$"], "params": {"selfTradePreventionMode": "EXPIRE_TAKER"} }]"#; if exchange.IO(("extend", rules)).is_err() { Log!("bad rules:", GetLastError()); return; } if let Ok(t) = exchange.GetTicker("BTC_USDT") { let _ = ctx::order(exchange, "BTC_USDT", "buy", _N(t.Buy * 0.98, 2), 0.001, "{}"); } Log!(exchange.IO("extend")); } -
Add
recvWindowto all signed GET requests, forcibly overwrite a parameter in order placement requests, and finally clear the strategy-configured rules (built-in rules remain unaffected).javascriptfunction main() { exchange.IO("extend", [ {match: "^GET/api/v3/", params: {recvWindow: 10000}}, {match: "^POST/api/v3/order$", params: {newOrderRespType: "RESULT"}, force: true} ]) Log(exchange.GetAccount()) exchange.IO("extend", "") // Clear }pythondef main(): exchange.IO("extend", [ {"match": "^GET/api/v3/", "params": {"recvWindow": 10000}}, {"match": "^POST/api/v3/order$", "params": {"newOrderRespType": "RESULT"}, "force": True} ]) Log(exchange.GetAccount()) exchange.IO("extend", "") # Clearrustfn main() { let rules = r#"[ {"match":"^GET/api/v3/","params":{"recvWindow":10000}}, {"match":"^POST/api/v3/order$","params":{"newOrderRespType":"RESULT"},"force":true} ]"#; let _ = exchange.IO(("extend", rules)); Log!(exchange.GetAccount()); let _ = exchange.IO(("extend", "")); // Clear }
Returns
| Type | Description |
bool / array / null value | Returns |
Arguments
| Name | Type | Required | Description |
k | string | Yes |
|
rules | array / object / string | No | An array of rules, each in the form |
See Also
Remarks
Rule fields:
| Field | Type | Description |
|---|---|---|
| match | string / array | A regular expression, or an array of regular expressions (matching any one is sufficient), used to match the request's target string; required |
| params | object | Parameters to inject, in the form {name: value}; required; non-string values are injected as JSON text |
| force | bool | When true, overwrites existing parameters with the same names in the request; defaults to false, filling in a parameter only when it is missing or empty |
The legacy fields query and body remain supported and serve the same purpose as params, but inject only into the request's query parameters (query) or request body (body); a rule can specify only one of params, query, or body. The legacy object format {"<regex>": {name: value}} also remains supported and is equivalent to one query rule per regular expression, sorted lexicographically by regular expression; use the array format when you need to control the order.
Target strings:
| Request | Target string | Example |
|---|---|---|
| REST request | Uppercase HTTP method followed by the endpoint path (excluding the domain and query parameters) | POST/api/v3/order, GET/fapi/v2/account |
ctx.submit order placement | ws:order (matched separately for each order in a batch request) | ws:order |
ctx.submit order cancellation | ws:cancel | ws:cancel |
Use anchored regular expressions, such as ^POST/api/v3/order$ and ^ws:order$. Unanchored regular expressions (such as order) match both REST and WebSocket requests. REST path rules do not apply to WebSocket order placement, and ws: rules do not apply to REST requests; to inject the same parameter into both types of requests, specify two regular expressions in match.
The exchange object determines where parameters are injected: requests that construct query parameters receive injected parameters in the query parameters, and requests that construct a JSON request body receive them in the request body (when both are present, parameters are injected into the request body). For WebSocket order placement, parameters are injected into the order's parameters. Parameter injection occurs before signing, and the injected parameters are included in the signature calculation. Parameter values support templates: {{uuid}} (a newly generated 32-character uppercase hexadecimal string for each request), {{uuid:N}} (the first N characters), and {{ts}} (a timestamp in milliseconds).
Rules are applied sequentially in array order, and a request can match multiple rules; when multiple rules set the same parameter, earlier rules fill it in first, and subsequent rules with force enabled overwrite the value.
Rules have two layers: the exchange object's built-in rules are applied first, followed by the rules configured by the strategy. exchange.IO("extend", rules) replaces only the strategy-configured rule layer without clearing built-in rules, and queries return only the strategy-configured rules. All built-in rules use default injection and do not overwrite parameters with the same names already present in strategy requests; to overwrite values filled in by built-in rules, use rules with force enabled or use extend!.
The client order ID (localId) for orders placed through ctx.submit is also generated through ws:order rules: when a rule injects the exchange's client order ID field, the injected value becomes localId and must meet the exchange's requirements for client order IDs. Configuration is generally unnecessary. Client order IDs for Hyperliquid, Bitfinex, and Lighter are generated directly by the Docker and cannot be modified by ws:order rules.
Rules are configured separately for each exchange object and are valid only for the current run. Rules are validated when set: invalid JSON, regular expressions that fail to compile, missing match or params, a rule specifying two or more of params, query, and body, or the presence of a headers field all trigger errors, leaving the existing rules unchanged. Use hits in the query results to check whether a rule has matched: a hit count that remains at 0 usually indicates an incorrect regular expression.