tqsdk.TqMultiAccount - 多账户

class tqsdk.TqMultiAccount(accounts: List[TqAccount | TqKq | TqZq | TqKqStock | TqSim | TqSimStock | TqCtp | TqRohon | TqJees | TqYida] | None = None)

天勤多账户 - TqMultiAccount

天勤多账户模块提供了单 api 同时操作不同账户及其组合的功能支持,目前已支持实盘账户、模拟账户和快期模拟账户的任意组合。 使用天勤多账户进行跨市场或跨账户交易时,可以在不引入多进程和多线程的前提下, 比较方便的传递账户信息进行策略编写, 同时, 也更方便对不同账户的交易数据进行统计分析。

注意

  • 多账户模式下, 对于 get_position,account,insert_order,set_target_volume 等函数必须指定 account 参数

  • 多账户模式下, 实盘账户的数量受限于快期账户支持实盘账户数, 详见:更多的实盘交易账户数

创建 TqMultiAccount 实例

Args:

accounts (List[Union[TqAccount, TqKq, TqKqStock, TqSim, TqSimStock, TqZq, TqCtp, TqRohon, TqJees, TqYida]]): [可选] 多账户列表, 若未指定任何账户, 则为 [TqSim()]

Example1:

from tqsdk import TqApi, TqAccount, TqMultiAccount

account1 = TqAccount("H海通期货", "123456", "123456")
account2 = TqAccount("H宏源期货", "654321", "123456")
api = TqApi(TqMultiAccount([account1, account2]), auth=TqAuth("快期账户", "账户密码"))
# 分别获取账户资金信息
order1 = api.insert_order(symbol="DCE.m2101", direction="BUY", offset="OPEN", volume=3, account=account1)
order2 = api.insert_order(symbol="SHFE.au2012C308", direction="BUY", offset="OPEN", volume=3, limit_price=78.0, account=account2)
while order1.status != "FINISHED" or order2.status != "FINISHED":
     api.wait_update()
# 分别获取账户资金信息
account_info1 = account1.get_account()
account_info2 = account2.get_account()
api.close()

Example2:

# 多账户模式下使用 TargetPosTask
from tqsdk import TqApi, TqAccount, TqMultiAccount, TqAuth, TargetPosTask

account1 = TqAccount("H海通期货", "123456", "123456")
account2 = TqAccount("H宏源期货", "654321", "123456")
api = TqApi(TqMultiAccount([account1, account2]), auth=TqAuth("快期账户", "账户密码"))
symbol1 = "DCE.m2105"
symbol2 = "DCE.i2101"
position1 = account1.get_position(symbol1)
position2 = account2.get_position(symbol2)
# 多账户模式下, 调仓工具需要指定账户实例
target_pos1 = TargetPosTask(api, symbol1, account=account1)
target_pos2 = TargetPosTask(api, symbol2, account=account2)
target_pos1.set_target_volume(30)
target_pos2.set_target_volume(80)
while position1.volume_long != 30 or position2.volume_long != 80:
    api.wait_update()

api.close()