Iceberg Order Execution with Target Position and Volume Limits
Summary
This Python example demonstrates an iceberg-style execution workflow for a futures contract using a trading API’s target-position task. The user specifies a target trade amount, a minimum and maximum order size, a buy or sell direction, and an execution style such as active or passive pricing. The script reads the initial net position, adjusts the target position by the requested amount, then monitors net position changes until the target is reached.
Progress is printed as fills change the net position, and the API connection is closed when execution ends or an exception occurs. The example identifies a silver futures contract and provides fixed sample parameters, but does not explain how the API chooses individual slice sizes, measures market impact, or handles partial fills and timeouts. It includes no execution benchmarks or tests, so it illustrates order orchestration rather than evidence that the approach reduces information leakage or improves fills. Credentials and account configuration also need to be supplied for actual use.
Key ideas
- The script expresses an execution objective as a target net position based on the existing position and requested trade size.
- Minimum and maximum volume settings constrain the order task’s individual order sizes.
- The user can select buy or sell direction and active, passive, or custom pricing behavior.
- The loop tracks position progress and stops after the target net position is reached.
- No execution quality evidence or detailed slice-sizing method is provided.
Tags
Full text
# iceberg_algorithm
# iceberg_algorithm
## Source (Apache-2.0)
```python
#!/usr/bin/env python
# coding=utf-8
__author__ = "Chaos"
from tqsdk import TqApi, TqAuth, TqKq, TargetPosTask
# === 用户参数 ===
SYMBOL = "SHFE.ag2506" # 交易合约
TOTAL_VOLUME = 100 # 目标总手数
MIN_VOLUME = 1 # 每笔最小委托手数
MAX_VOLUME = 10 # 每笔最大委托手数
DIRECTION = "BUY" # "BUY"为买入,"SELL"为卖出
ORDER_TYPE = "ACTIVE" # 对价 "ACTIVE" / 挂价 "PASSIVE" / 指定价 lambda direction: 价格
# === 初始化 ===
api = TqApi(account=TqKq(), auth=TqAuth("快期账户", "快期密码"))
quote = api.get_quote(SYMBOL)
# 创建目标持仓任务
target_pos = TargetPosTask(
api, SYMBOL,
price=ORDER_TYPE,
min_volume=MIN_VOLUME,
max_volume=MAX_VOLUME
)
# 获取下单方式描述
order_type_str = (f"指定价 {ORDER_TYPE(DIRECTION)}" if callable(ORDER_TYPE)
else str(ORDER_TYPE))
print(f"冰山算法启动,合约: {SYMBOL},目标: {TOTAL_VOLUME}手,"
f"每批: {MIN_VOLUME}-{MAX_VOLUME}手,方向: {DIRECTION},下单方式: {order_type_str}")
try:
# 获取初始持仓并设置目标
pos = api.get_position(SYMBOL)
start_net_pos = pos.pos_long - pos.pos_short
target_volume = start_net_pos + (TOTAL_VOLUME if DIRECTION == "BUY" else -TOTAL_VOLUME)
target_pos.set_target_volume(target_volume)
last_progress = 0 # 记录上次进度
while True:
api.wait_update()
pos = api.get_position(SYMBOL)
net_pos = pos.pos_long - pos.pos_short
progress = abs(net_pos - start_net_pos)
# 当进度发生变化时打印
if progress != last_progress:
print(f"当前进度: {progress}/{TOTAL_VOLUME}")
last_progress = progress
# 检查是否完成
if (DIRECTION == "BUY" and net_pos >= target_volume) or \
(DIRECTION == "SELL" and net_pos <= target_volume):
print(f"冰山算法完成")
break
except Exception as e:
print(f"算法执行异常: {e}")
finally:
api.close()
```Shown in full with attribution under the source's licence. Licence: Apache-2.0
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.