返回列表 发布新帖

场景_如何用Python实时调用vba指标?(支持内置/原生Py,并行计算速度提升10-100倍)

1919 1
发表于 2024-4-10 18:28:30 | 显示全部楼层 阅读模式

问题

<span style="color:#FF0000;">大家常常在问,有些函数python没有,但vba有(先建一个vba公式使用这个函数,再用py去调这个公式的变量),或者py单线程计算速度慢,怎么把计算机多核资源用满提升速度,那就用 python 实时调用 vba 指标,在投研软件里面,调用一个接口就能解决:

# 内置Python
# 初始化参数变量
    stock_list   = ['000001.SZ']    # 股票代码列表
    formula_name = "MA"             # 指标名称
    period       = '1d'
    start_time   = '20230320'       # %Y%m%d 或 %Y%m%d%H%M%S
    end_time     = ''               # %Y%m%d 或 %Y%m%d%H%M%S
    extend_param = {'MA:N1':5,'MA:N2':20,'MA:N3':40,'MA:N4':60,"_group" : 'group1}
    count        = -1
    for s in stock_list:
        id = C.subscribe_formula(formula_name,s,period,start_time,end_time,count=count,extend_param=extend_param,callback = partial(callback, s))

# 原生Python
# 初始化参数变量
stock_list   = ['000001.SZ']    # 股票代码列表
formula_name = "MA"             # 指标名称 
period       = '1d' 
start_time   = '20230320'       # %Y%m%d 或 %Y%m%d%H%M%S
end_time     = ''               # %Y%m%d 或 %Y%m%d%H%M%S
extend_param = {'MA:N1':5,'MA:N2':20,'MA:N3':40,'MA:N4':60,}
count        = -1
for s in stock_list:
    id = xtdata.subscribe_formula(formula_name,s,period,start_time,end_time,count=count,extend_param=extend_param,callback = partial(callback, s))

像上方代码块所展示的一样,subscribe_formula 接口可以很好的帮我们完成这个任务。而这里的 参数变量取决于不同的需求,subscribe_formula函数默认使用电脑核心数个线程并行计算。 也支持在extend_param参数中手动指定分组, 格式为 extend_param = {"_group" : 'group1'},相同参数的订阅品种会在同一个线程中计算,节约CPU 与 内存的占用。

解答

内置 Python

image.png

# coding:gbk

'''
内置python,实时调用技术指标
'''
from functools import partial

def callback(s, data):
    '''
    回调函数,展示结果
    data格式示例:
    {'dbt': 0, 'timelist': [1712592000000, 1712678400000], 'outputs': {'ma1': [nan, nan], 'ma2': [nan, nan], 'ma3': [nan, nan], 'ma4': [nan, nan]}}
    '''
    # 转换时间戳
    timelist = data['timelist'][-5:]
    timelist_fmt = [timetag_to_datetime(t,'%Y%m%d') for t in timelist]
    for index_name in data['outputs']:
        for k, t, v in zip(timelist,timelist_fmt,data['outputs'][index_name][-5:]):
                print(s, k, t, index_name, v)

def init(C):
    return

def after_init(C):
    # 初始化参数变量
    stock_list   = ['000001.SZ']    # 股票代码列表
    formula_name = "MA"             # 指标名称
    period       = '1d'
    start_time   = '20230320'       # %Y%m%d 或 %Y%m%d%H%M%S
    end_time     = ''               # %Y%m%d 或 %Y%m%d%H%M%S
    extend_param = {'MA:N1':5,'MA:N2':20,'MA:N3':40,'MA:N4':60,}
    count        = -1
    for s in stock_list:
        id = C.subscribe_formula(formula_name,s,period,start_time,end_time,
                                  count=count,extend_param=extend_param,
                                  callback = partial(callback, s)
                              )
    #time.sleep(100)
    return

def handlebar(C):
    return

返回结果

000001.SZ 1712073600000 20240403 ma1 10.531999999999977
000001.SZ 1712505600000 20240408 ma1 10.519999999999976
000001.SZ 1712592000000 20240409 ma1 10.495999999999976
000001.SZ 1712678400000 20240410 ma1 10.423999999999975
000001.SZ 1712764800000 20240411 ma1 10.363999999999976
000001.SZ 1712073600000 20240403 ma2 10.502000000000011
000001.SZ 1712505600000 20240408 ma2 10.498000000000012
000001.SZ 1712592000000 20240409 ma2 10.502000000000011
000001.SZ 1712678400000 20240410 ma2 10.49000000000001
000001.SZ 1712764800000 20240411 ma2 10.45500000000001
000001.SZ 1712073600000 20240403 ma3 10.467999999999991
000001.SZ 1712505600000 20240408 ma3 10.47049999999999
000001.SZ 1712592000000 20240409 ma3 10.471499999999992
000001.SZ 1712678400000 20240410 ma3 10.461999999999993
000001.SZ 1712764800000 20240411 ma3 10.446499999999991
000001.SZ 1712073600000 20240403 ma4 9.952166666666667
000001.SZ 1712505600000 20240408 ma4 9.972666666666665
000001.SZ 1712592000000 20240409 ma4 9.994166666666665
000001.SZ 1712678400000 20240410 ma4 10.010999999999997
000001.SZ 1712764800000 20240411 ma4 10.029333333333332

原生 Python

image.png

import time
from xtquant import xtdata
from functools import partial

xtdata.connect(port=58622)

def callback(s, data):
    '''
    回调函数,展示结果
    data格式示例:
    {'dbt': 0, 'timelist': [1712592000000, 1712678400000], 'outputs': {'ma1': [nan, nan], 'ma2': [nan, nan], 'ma3': [nan, nan], 'ma4': [nan, nan]}}
    '''
    # 转换时间戳
    timelist = data['timelist'][-5:]
    timelist_fmt = [xtdata.timetag_to_datetime(t,'%Y%m%d') for t in timelist]
    for index_name in data['outputs']:
        for k, t, v in zip(timelist,timelist_fmt,data['outputs'][index_name][-5:]):
            print(s, k, t, index_name, v)

# 初始化参数变量
stock_list   = ['000001.SZ']    # 股票代码列表
formula_name = "MA"             # 指标名称 
period       = '1d' 
start_time   = '20230320'       # %Y%m%d 或 %Y%m%d%H%M%S
end_time     = ''               # %Y%m%d 或 %Y%m%d%H%M%S
extend_param = {'MA:N1':5,'MA:N2':20,'MA:N3':40,'MA:N4':60,"_group" : 'group1}
count        = -1
for s in stock_list:
    id = xtdata.subscribe_formula(formula_name,s,period,start_time,end_time,count=count,extend_param=extend_param,callback = partial(callback, s))
time.sleep(100)

返回结果

000001.SZ 1711987200000 20240402 ma1 10.545999999999971
000001.SZ 1712073600000 20240403 ma1 10.531999999999972
000001.SZ 1712505600000 20240408 ma1 10.519999999999971
000001.SZ 1712592000000 20240409 ma1 10.49599999999997
000001.SZ 1712678400000 20240410 ma1 10.42399999999997
000001.SZ 1711987200000 20240402 ma2 10.500999999999994
000001.SZ 1712073600000 20240403 ma2 10.501999999999994
000001.SZ 1712505600000 20240408 ma2 10.497999999999994
000001.SZ 1712592000000 20240409 ma2 10.501999999999994
000001.SZ 1712678400000 20240410 ma2 10.489999999999993
000001.SZ 1711987200000 20240402 ma3 10.4615
000001.SZ 1712073600000 20240403 ma3 10.468
000001.SZ 1712505600000 20240408 ma3 10.4705
000001.SZ 1712592000000 20240409 ma3 10.4715
000001.SZ 1712678400000 20240410 ma3 10.462
000001.SZ 1711987200000 20240402 ma4 9.931333333333336
000001.SZ 1712073600000 20240403 ma4 9.95216666666667
000001.SZ 1712505600000 20240408 ma4 9.972666666666669
000001.SZ 1712592000000 20240409 ma4 9.994166666666668
000001.SZ 1712678400000 20240410 ma4 10.011000000000001
000001.SZ 1711987200000 20240402 ma1 10.545999999999971
000001.SZ 1712073600000 20240403 ma1 10.531999999999972
000001.SZ 1712505600000 20240408 ma1 10.519999999999971
000001.SZ 1712592000000 20240409 ma1 10.49599999999997
000001.SZ 1712678400000 20240410 ma1 10.42399999999997
000001.SZ 1711987200000 20240402 ma2 10.500999999999994
000001.SZ 1712073600000 20240403 ma2 10.501999999999994
000001.SZ 1712505600000 20240408 ma2 10.497999999999994
000001.SZ 1712592000000 20240409 ma2 10.501999999999994
000001.SZ 1712678400000 20240410 ma2 10.489999999999993
000001.SZ 1711987200000 20240402 ma3 10.4615
000001.SZ 1712073600000 20240403 ma3 10.468
000001.SZ 1712505600000 20240408 ma3 10.4705
000001.SZ 1712592000000 20240409 ma3 10.4715
000001.SZ 1712678400000 20240410 ma3 10.462
000001.SZ 1711987200000 20240402 ma4 9.931333333333336
000001.SZ 1712073600000 20240403 ma4 9.95216666666667
000001.SZ 1712505600000 20240408 ma4 9.972666666666669
000001.SZ 1712592000000 20240409 ma4 9.994166666666668
000001.SZ 1712678400000 20240410 ma4 10.011000000000001

$\color{red}{tips:}$

原生Python调用 subscribe_formula 接口时,不能使用 token 连接。需要直接连接投研客户端,方法如下:

1. 在投研软件内,设置-模型设置里可以改投研监听的端口
2. from xtquant import xtdata
   xtdata.connect(port=58622)    # 在第一步中设置的端口号

不清楚的内容可添加下方助理微信咨询,有其他 QMT 小技巧想学习的吗?欢迎在下方留言,笔者将根据大家的留言持续更新哦!

欢迎和我一起加入迅投组建的 QMT 实战交流社群,交流群内有许多做量化交易的高手和大佬,具有良好的分享和互助氛围。且迅投官方会不定期为多次分享、乐于助人的群友申请送投研专业版的机会。

只需扫描下方的二维码,名额有限,限时加入。一起分享见解、交换信息、并共同进步,就像群友说的:“就算周末,晚上也有地方沟通交流!”

企业微信截图_17235220909173.png

评论1

greatpass
发表于 2024-9-7 16:37:43 | 显示全部楼层
不是投研版 是不是调用不了

回复

您需要登录后才可以回帖 登录 | 立即注册

主题

6

回帖

2

积分

0

客服专线

400-080-8112

用思考的速度交易,用真诚的态度合作,我们是认真的!
  • 关注公众号
  • 添加微信客服
Copyright © 2001-2024 迅投QMT社区 版权所有 All Rights Reserved. 蜀ICP备19002686号-2
关灯 快速发帖
扫一扫添加微信客服
QQ客服返回顶部
快速回复 返回顶部 返回列表