Skip to Content
Python算法交易
book

Python算法交易

by Yves Hilpisch
November 2022
Intermediate to advanced
392 pages
10h 8m
Chinese
China Electric Power Press Ltd.
Content preview from Python算法交易
228
|
第
7
章
通常,对金融工具价格进行的蒙特卡洛模拟依赖于相同的时间间隔(例
如“一个交易日”)。在很多情况下,这与例如较长时间的收盘价一起
使用时,是一个“足够好”的近似值。在日内报价数据的场景中,数据
的随机到达是需要考虑的重要特征。实时数据服务器的
Python
脚本通过
暂停随机时间的运行实现随机到达。
7.2
连接报价数据客户端
报价数据服务器的代码已经很简洁了,其中
InstrumentPrice
模拟类占据了最长的
部分。第
7.7.2
节中所示的各个实时数据客户端的代码则更加简洁。只有几行代码,
用来实例化主要的
Context
对象,连接到发布套接字,并订阅
SYMBOL
通道,这恰好
是此处唯一可用的通道。在
while
循环中,接收并打印基于字符串的消息。所以脚
本就很短。
如下所示,脚本的初始部分几乎与报价数据服务器脚本一样。
import zmq
context = zmq.Context()
socket = context.socket(zmq.SUB)
socket.connect('tcp://0.0.0.0:5555')
socket.setsockopt_string(zmq.SUBSCRIBE, 'SYMBOL')
导入
ZeroMQ
库的
Python
包。
对于客户端来说,主要对象也是
zmq.Context
的实例。
这里有一点不同:套接字类型设置为
SUB
。
套接字连接到对应的
IP
地址和端口。
此行代码定义了套接字订阅的通道。虽然在这里只有一个通道,但是我们仍然
需要指明
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

人工智能技术与大数据

人工智能技术与大数据

Posts & Telecom Press, Anand Deshpande, Manish Kumar
图解大模型 : 生成式AI 原理与实战

图解大模型 : 生成式AI 原理与实战

Jay Alammar, Maarten Grootendorst

Publisher Resources

ISBN: 9787519869694