Trading API¶
Backtest execution semantics:
order*APIs only submit requests during the current callback; orders are filled uniformly at the next trading day's opening price.
order¶
Place a buy or sell order by share count.
| Parameter | Type | Required | Description |
|---|---|---|---|
security |
str |
Yes | Stock code, e.g. '601390' |
amount |
int |
Yes | Number of shares; positive = buy, negative = sell |
style |
— | No | Order type (reserved parameter) |
Returns the pending order ID (str), or None on failure. Buy orders are automatically rounded to the nearest multiple of 100.
order_value¶
Place a buy or sell order by monetary value.
| Parameter | Type | Required | Description |
|---|---|---|---|
security |
str |
Yes | Stock code |
value |
float |
Yes | Order value; positive = buy, negative = sell |
style |
— | No | Order type |
order_target¶
Adjust position to a target share count.
| Parameter | Type | Required | Description |
|---|---|---|---|
security |
str |
Yes | Stock code |
amount |
int |
Yes | Target position in shares; 0 = close position |
style |
— | No | Order type |
order_target_value¶
Adjust position to a target market value.
| Parameter | Type | Required | Description |
|---|---|---|---|
security |
str |
Yes | Stock code |
value |
float |
Yes | Target market value; 0 = close position |
style |
— | No | Order type |
order_lots¶
Place a buy or sell order by lot count (1 lot = 100 shares in A-share market).
| Parameter | Type | Required | Description |
|---|---|---|---|
security |
str |
Yes | Stock code |
lots |
int |
Yes | Number of lots; positive = buy, negative = sell |
style |
— | No | Order type |
order_lots('601390', 5) # Buy 5 lots (500 shares)
order_lots('601390', -2) # Sell 2 lots (200 shares)
order_pct¶
Place an order using a percentage of available cash.
| Parameter | Type | Required | Description |
|---|---|---|---|
security |
str |
Yes | Stock code |
pct |
float |
Yes | Cash ratio, e.g. 0.5 = 50% of available cash; -0.3 = sell 30% of current position |
style |
— | No | Order type |
order_pct('601390', 0.5) # Buy with 50% of available cash
order_pct('601390', -0.3) # Sell 30% of current position
Commission Fees¶
Configure via set_order_cost() (see Configuration API). Defaults: buy stamp duty 0%, sell stamp duty 0.05% (halved since Aug 2023), buy/sell commission 0.025%, minimum commission 5 yuan.