Hướng dẫn python quants certificate program - chương trình chứng chỉ python quants

This is the latest post in a series of updates bringing the reader behind the scenes as I progress through Yves Hilpisch’s excellent Python Quants Certificate Program. Check out my publisher page for prior posts and follow me for future updates.

The Python Quants Certificate Program with a primary focus on the Python for Algorithmic Trading, Artificial Intelligence for Finance and Financial Data Science modules covered many interesting topics in week 7. We took a close look at various data in/out operations available within the Python ecosystem including the HDF5 file format and its integration within Pandas, and I will personally be using the pd.HDFStore() option extensively moving forward given that in the past I’ve tended to rely too much on repeatedly pulling data from online sources.

We also took a deeper look at various methods to improve out-of-sample price prediction accuracy using Dense Neural Networks, including dropout, regularization, bagging, and optimization, and I plan on discussing each of these topics in detail in future posts when the Certificate Program enters the final project phase. My final project will be completed over the final 4 weeks of the 4-month version of the program, and I plan on building a neural network-based cryptocurrency forecasting model. During this phase there will be plenty of time to discuss neural networks in detail.

One topic which really stood out to me during week 7 from the Python for Algorithmic Trading module was the simulation of live streaming market data. We also built an associated trading simulation and a real-time charting notebook using Plotly in JupyterLab, and in what follows, the reader can follow along as we piece together all the necessary components. The simulation of streaming data is an important first step in building out online trading architecture, and in subsequent weeks the PyAlgo Trading module covers in detail the process of connecting to FX and crypto brokers which I will likely cover as part of this series.

With any online/streaming data feed, the data source is referred to as the publisher (PUB), and to receive data from a publisher, like an exchange, we need to connect as subscribers (SUB). The following examples make use of the popular zeromq socket library, and we begin by setting up a zmq.PUB script which will serve as our simulated Bitcoin price source. The socket creation is fairly straight forward, though it is important to note the tcp address in line 8 is for a Windows machine. For mac/linux, replace line 8 (any any other instances in subsequent scripts) with socket.bind(‘tcp://0.0.0.0:5555’):

The BTC simulation in our above script is a simple random walk using a zero mean with a $25 standard deviation. The data is sampled from a gaussian distribution using the random library and a starting price of 37,250 as this was the prevailing BTC level on 24 Feb, 2022 when I built this variation of the PyAlgo AAPL stock simulation. One of the coolest features of this script is found in line 16 where the time between ticks is randomized using a uniform distribution multiplied by 2 which simulates the random arrival time seen in real-world tick data. It is really cool to see this script in action as it looks very much like real, live data, and as you are about to see, this is all we need to build and test pretty much any type of online trading model.

While the PyAlgo class uses docker running ubuntu with a multi-window screen shell to run the following setup, I will offer instructions on running everything on one’s local machine for simplicity’s sake. In order to implement the above script, save it to your local machine with a name such as “TickServer.py”, open your shell and cd to the directory where you saved the script and type:

python .\TickServer.py

Voila! You should now see the simulated Bitcoin tick prices arriving at randomized intervals:

Pretty cool, right? Next we will build our subscriber script which “subscribes” to our TickServer, and when doing so we can also build out the trading logic for a simple Moving Average trading system as one example of the interesting applications we might have for simulated live data.

You’ll notice that in line 8 below we’ve changed our zmq connection to the SUB type, and in line 11 we SUBSCRIBE to our BTCUSD zmq.PUB from our first script. In the while loop, we split the incoming string message into symbol and price data and append this information to our “ticks” dataframe. Next we create a new dataframe “data” which is resampled into 5 second bars using our “ticks” dataframe and serves as the basis for our moving average trading model. We create short and long-term moving averages and then print position state information to the console based on the relationship of the 2 moving averages (using the 2nd to last bar [-2] since the last bar might not have actually printed yet). One could use such a system in live trading by replacing lines 39 and 44 with API calls, though that isn’t recommended using this particular system which is for demonstration purposes and is highly sensitive/aggressive given the short MA windows:

Cá nhân tôi đã gặp vấn đề trên máy cục bộ của mình khi cố gắng chạy các phần trên thông qua vỏ miniconda của tôi, nhưng tôi đã có thể chạy kịch bản bằng thiết bị đầu cuối Jupyterlab. Lưu tập lệnh thứ 2 ở trên dưới dạng Hồi Tick_Trader.py ở cùng một vị trí như là Tick Ticktrader.py và ra mắt thông qua:

python .\tick_trader.py

Bây giờ bạn sẽ thấy dữ liệu đánh dấu BTC mô phỏng cùng với thông tin trạng thái thời gian, giá cả và vị trí:

Sau một thời gian, bạn sẽ thấy một số giao dịch dài (1) và ngắn (-1) dựa trên sự phát triển giá mô phỏng:

Cuối cùng nhưng chắc chắn không kém phần quan trọng, chúng tôi chuyển sang âm mưu trực tiếp của dữ liệu phát trực tuyến của chúng tôi bằng cách sử dụng Plotly và Jupyterlab. Quá trình này có liên quan nhiều hơn khi so sánh với in tĩnh bằng pylab, mặc dù nỗ lực này rất đáng để xem xét khả năng xem các biểu đồ tương tác cập nhật theo thời gian thực, như đã thấy trong phần mềm môi giới.

Mã sau đây cần được chạy trong sổ ghi chép Jupyter vì các tính năng tương tác của Plotly, yêu cầu định dạng này. Sao chép và dán phần sau vào sổ ghi chép Jupyter và Run:

Mã trên sẽ vẽ cho 100 Ticks gần đây nhất từ ​​máy chủ dữ liệu SIM Tick của chúng tôi. Tại đây, những gì 1.000 BTC ve trông giống như (thay đổi 100 100 trong dòng 25 ở trên thành 1000 1000):

Tôi trông rất hợp lý!

Chúng tôi thậm chí có thể vẽ các chỉ số kỹ thuật như 2 mức trung bình di chuyển từ mô hình đồ chơi của chúng tôi:

năng suất nào:

Tôi đã âm mưu với Python trong vài năm, và thiết lập ở trên là trải nghiệm đầu tiên của tôi với âm mưu trực tiếp thông qua Plotly và Jupyterlab, và tất cả những gì tôi có thể nói là tôi rất ấn tượng!

Có nhiều cách khác nhau chúng ta có thể mở rộng về các chủ đề được đề cập trong bài viết này. Bạn có thể điều chỉnh các tham số mô phỏng giá để mô hình hóa các tài sản khác nhau hoặc thậm chí thực hiện các mô hình mô phỏng giá phức tạp hơn như sơ đồ Euler cho mô hình Black-Scholes. Bạn cũng có thể xây dựng các hệ thống giao dịch khác nhau (nghĩa là đảo ngược trung bình hoặc dựa trên động lượng) để kiểm tra trên dữ liệu mô phỏng và vẽ mọi thứ khi nó phát triển trong thời gian thực! Đây sẽ là một bài tập hoàn hảo để thử nghiệm một hệ thống giao dịch mới để đảm bảo chức năng phù hợp trước khi ra mắt sản xuất tại các thị trường thực và trong những tuần để theo dõi tôi sẽ chia sẻ chi tiết về điều này, vì vậy, như mọi khi, hãy theo dõi!