Ptrade在一个循环事件里 能否不断获取股票实时价格?

假设在一个事件循环里:
如:
run_daily(context, get_price, '09:44')
定义的get_price 函数,
然后get_price函数里面有一个死循环,不断地获取价格。
 
因为ptrade的行情切片 是每3秒更新的一次的,如果行情没更新,那么当前的价格也是过去最近的一个3s的价格。
 
现在问题是,在一个固定的时间里面,不断地读取价格函数,能获取到最新的价格吗 ?
 
 
我们用代码实践一下:
 
import time
def initialize(context):
# 初始化策略

run_daily(context, get_price, '09:44')

def handle_data(context, data):
pass


def get_price(context):
for i in range(10):
target_list =['113585.SS','123057.SZ']
bond_gear_price_target = get_gear_price(target_list)

for code in target_list:
price = bond_gear_price_target[code]['offer_grp'][1][0]
log.info('code: {} price {} '.format(code,price))

time.sleep(1)

输出的结果:
 
2022-09-28 10:31:00 - INFO - code: 113585.SS price 169.147 
2022-09-28 10:31:00 - INFO - code: 128053.SZ price 144.785
2022-09-28 10:31:01 - INFO - code: 113585.SS price 169.147
2022-09-28 10:31:01 - INFO - code: 128053.SZ price 144.785
2022-09-28 10:31:02 - INFO - code: 113585.SS price 169.156
2022-09-28 10:31:02 - INFO - code: 128053.SZ price 144.785
2022-09-28 10:31:03 - INFO - code: 113585.SS price 169.156
2022-09-28 10:31:03 - INFO - code: 128053.SZ price 144.8
2022-09-28 10:31:04 - INFO - code: 113585.SS price 169.199
2022-09-28 10:31:04 - INFO - code: 128053.SZ price 144.8
2022-09-28 10:31:05 - INFO - code: 113585.SS price 169.199
2022-09-28 10:31:05 - INFO - code: 128053.SZ price 144.8
2022-09-28 10:31:06 - INFO - code: 113585.SS price 169.199
2022-09-28 10:31:06 - INFO - code: 128053.SZ price 144.785
2022-09-28 10:31:07 - INFO - code: 113585.SS price 169.068
2022-09-28 10:31:07 - INFO - code: 128053.SZ price 144.785
2022-09-28 10:31:08 - INFO - code: 113585.SS price 169.068
2022-09-28 10:31:08 - INFO - code: 128053.SZ price 144.785
2022-09-28 10:31:09 - INFO - code: 113585.SS price 169.068
2022-09-28 10:31:09 - INFO - code: 128053.SZ price 144.746
2022-09-28 10:31:10 - INFO - code: 113585.SS price 169.066
2022-09-28 10:31:10 - INFO - code: 128053.SZ price 144.746
2022-09-28 10:31:11 - INFO - code: 113585.SS price 169.066
2022-09-28 10:31:11 - INFO - code: 128053.SZ price 144.746
2022-09-28 10:31:12 - INFO - code: 113585.SS price 169.066
2022-09-28 10:31:12 - INFO - code: 128053.SZ price 144.785
2022-09-28 10:31:13 - INFO - code: 113585.SS price 169.068
2022-09-28 10:31:13 - INFO - code: 128053.SZ price 144.785
2022-09-28 10:31:14 - INFO - code: 113585.SS price 169.068
2022-09-28 10:31:14 - INFO - code: 128053.SZ price 144.785
2022-09-28 10:31:15 - INFO - code: 113585.SS price 169.068
2022-09-28 10:31:15 - INFO - code: 128053.SZ price 144.692
2022-09-28 10:31:16 - INFO - code: 113585.SS price 169.068
2022-09-28 10:31:16 - INFO - code: 128053.SZ price 144.692
2022-09-28 10:31:17 - INFO - code: 113585.SS price 169.068
2022-09-28 10:31:17 - INFO - code: 128053.SZ price 144.692
2022-09-28 10:31:18 - INFO - code: 113585.SS price 169.068
2022-09-28 10:31:18 - INFO - code: 128053.SZ price 144.691
2022-09-28 10:31:19 - INFO - code: 113585.SS price 169.062
2022-09-28 10:31:19 - INFO - code: 128053.SZ price 144.691
2022-09-28 10:31:20 - INFO - code: 113585.SS price 169.062


为了更为直观,过滤掉另外一只可转债
只保留一只
2022-09-28 10:31:00 - INFO - code: 113585.SS price 169.147 

2022-09-28 10:31:01 - INFO - code: 113585.SS price 169.147

2022-09-28 10:31:02 - INFO - code: 113585.SS price 169.156

2022-09-28 10:31:03 - INFO - code: 113585.SS price 169.156

2022-09-28 10:31:04 - INFO - code: 113585.SS price 169.199

2022-09-28 10:31:05 - INFO - code: 113585.SS price 169.199

2022-09-28 10:31:06 - INFO - code: 113585.SS price 169.199

2022-09-28 10:31:07 - INFO - code: 113585.SS price 169.068

2022-09-28 10:31:08 - INFO - code: 113585.SS price 169.068

2022-09-28 10:31:09 - INFO - code: 113585.SS price 169.068

2022-09-28 10:31:10 - INFO - code: 113585.SS price 169.066

2022-09-28 10:31:11 - INFO - code: 113585.SS price 169.066
可以看到价格也是基本没个3s更新一次。
 
 
 
更多ptrade实盘代码,请常见个人星球。
【可转债追涨杀跌日内T+0,双低 低价 低溢价 规模多因子轮动】

20220928002.jpg

 

20220928001.jpg

 

 
 

0 个评论

要回复文章请先登录注册