算法模块示例

twap_table - 时间平均加权算法

#!/usr/bin/env python
#  -*- coding: utf-8 -*-
__author__ = 'yanqiong'

from tqsdk import TqApi, TargetPosScheduler
from tqsdk.algorithm import twap_table

"""
在 300s 时间内调整净持仓到 -100,每次下单手数在 5~10 手之间
"""

api = TqApi(auth="信易账号,用户密码")
quote = api.get_quote("CZCE.MA109")

# 设置twap任务参数
time_table = twap_table(api, "CZCE.MA109", -100, 600, 1, 5)  # 目标持仓 -100 手,600s 内完成
print(time_table.to_string())

target_pos_sch = TargetPosScheduler(api, "CZCE.MA109", time_table)
# 启动循环
while not target_pos_sch.is_finished():
    api.wait_update()
api.close()

vwap_table - 交易量平均加权算法

#!/usr/bin/env python
#  -*- coding: utf-8 -*-
__author__ = 'yanqiong'

from tqsdk import TqApi, TargetPosScheduler
from tqsdk.algorithm import vwap_table

"""
在 600s 时间内,按照历史成交量比例调整目标净持仓到 -100
"""

api = TqApi(auth="信易账号,用户密码")
quote = api.get_quote("CZCE.MA109")

# 设置twap任务参数
time_table = vwap_table(api, "CZCE.MA109", -100, 600)  # 目标持仓 -100 手,600s 内完成
print(time_table.to_string())

target_pos_sch = TargetPosScheduler(api, "CZCE.MA109", time_table)
# 启动循环
while not target_pos_sch.is_finished():
    api.wait_update()
api.close()