Using Target Position and Order Sizing for Iceberg Execution
Summary
This example demonstrates an iceberg-style execution workflow for a futures contract. The trader chooses a symbol, a total volume, minimum and maximum order sizes, and a buy or sell direction. A target-position task manages orders toward the desired net position, while the price mode can use active execution, passive placement, or a user-specified price rule.
The script reads the starting net position, adds or subtracts the requested volume to set a target, then monitors position updates and reports progress until the target is reached. It closes the API connection when execution ends or an exception occurs. This is implementation guidance rather than an execution study: it provides no evidence about fill quality, market impact, timing, or performance. The example also does not describe safeguards for partial fills, changing positions from other orders, or interrupted execution.
Key ideas
- The workflow sets a target net position based on the starting position and requested trade direction.
- Minimum and maximum order sizes constrain order chunks managed by the target-position task.
- The price mode can be active, passive, or supplied by a pricing function.
- Progress is tracked by comparing the live net position with the starting position.
- The example offers no fill-quality analysis or performance results.
Tags
Full text
# iceberg_algorithm.py
```py
#!/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.