Managing Target Position Tasks and Cancelling Pending Orders
Summary
This documentation explains how to manage a TargetPosTask instance in a futures trading application, focusing on cancelling a task and checking when it has finished. It states that an account may have only one such instance per contract at a time, and that its constructor settings cannot be changed. The cancellation method stops future target-volume updates and withdraws that task’s outstanding unfilled orders; the completion check indicates when the task will no longer submit or cancel orders.
The example uses a passive-priced task during the session, cancels it before the close, and waits for cancellation processing to finish. It then creates an active-priced task and sets the target volume to zero to close the position. This illustrates a practical order-management sequence, not a trading signal or profitability method. The example depends on the API’s update loop to process cancellations, and the document offers no execution guarantees or performance evidence.
Key ideas
- Only one TargetPosTask instance can exist per account and contract at a time, with fixed constructor parameters.
- Cancelling a task also requests withdrawal of its outstanding unfilled orders.
- After cancellation, target-volume changes on that instance no longer take effect.
- The completion check lets a strategy wait until task cancellation has finished before creating another instance.
- The example switches from passive order handling to an active task targeting a flat position.
Tags
Cited by
- Strategies 美股行业ETF三年输家—赢家逆向组合
- Hypotheses 美股行业ETF三年输家—赢家逆向组合
Full text
# ... 策略代码 ...
.. _targetpostask2:
TargetPosTask 高级功能
====================================================
本篇文档假设您已经了解 :py:class:`~tqsdk.TargetPosTask` 的用法,文档参考 :ref:`targetpostask`。
本篇文档主要介绍 :py:class:`~tqsdk.TargetPosTask` 的高级用法。如何使用\
:py:meth:`~tqsdk.TargetPosTask.cancel` 和 :py:meth:`~tqsdk.TargetPosTask.is_finished` 方法。
应用情景说明
----------------------------------------------------
任何时刻,每个账户下一个合约只能有一个 :py:class:`~tqsdk.TargetPosTask` 实例,并且其构造参数不能修改。
但是在某些情况下,用户会希望可以管理 :py:class:`~tqsdk.TargetPosTask` 实例。
比如说,用户使用 :py:class:`~tqsdk.TargetPosTask` 的 **PASSIVE** 模式进行下单,希望在收盘前取消所有挂单(包含 :py:class:`~tqsdk.TargetPosTask` 实例的未成委托单),并平仓。
如何实现这样的功能
----------------------------------------------------
:py:class:`~tqsdk.TargetPosTask` 类提供了 :py:meth:`~tqsdk.TargetPosTask.cancel` 和 :py:meth:`~tqsdk.TargetPosTask.is_finished` 方法。
+ :py:meth:`~tqsdk.TargetPosTask.cancel` 方法会取消当前 :py:class:`~tqsdk.TargetPosTask` 实例,并撤销该实例已经发出但还未成交的委托单。取消后,此实例的 set_target_volume 函数不会再生效。
+ :py:meth:`~tqsdk.TargetPosTask.is_finished` 方法可以获取当前 :py:class:`~tqsdk.TargetPosTask` 实例是否已经结束。已经结束实例的 set_target_volume 函数不会再接受参数,此实例不会再下单或者撤单。
下面是一个例子::
from datetime import datetime, time
from tqsdk import TqApi, TqAuth, TargetPosTask
api = TqApi(auth=TqAuth("快期账户", "账户密码"))
quote = api.get_quote("SHFE.rb2701")
target_pos_passive = TargetPosTask(api, "SHFE.rb2701", price="PASSIVE")
while datetime.strptime(quote.datetime, "%Y-%m-%d %H:%M:%S.%f").time() < time(14, 50):
api.wait_update()
# ... 策略代码 ...
# 取消 TargetPosTask 实例
target_pos_passive.cancel()
while not target_pos_passive.is_finished(): # 此循环等待 target_pos_passive 处理 cancel 结束
api.wait_update() # 调用wait_update(),会对已经发出但还是未成交的委托单撤单
# 创建新的 TargetPosTask 实例
target_pos_active = TargetPosTask(api, "SHFE.rb2701", price="ACTIVE")
target_pos_active.set_target_volume(0) # 平所有仓位
while True:
api.wait_update()
# ... 策略代码 ...
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.