More highlights , Welcome to the official account : Quantity technology house , You can also add Technology Personal micro signal :sljsz01, Communicate with me .

The importance of real-time stock data

For the four tradable assets : Stocks 、 futures 、 option 、 For digital money , futures 、 option 、 Digital currency , Can be provided from the exchange api Receive real-time market data , The quantitative trading interface of stocks is not open to ordinary people , This leads us to want to get real-time data of stocks , Very difficult . And at the same time , Real time stock data , It is also extremely important floor trading data .

For manual traders , On the one hand, real-time data can assist in staring at the disk , On the other hand , You can use the program to simply develop the price reminder , To a certain price , Make entry and exit transactions .

For quantitative traders , Real time market is even more important . After we receive real-time quotes , It is not only necessary to calculate the strategy signal with real-time data , And when the strategy signal needs to place an order for a stock , We also need to know the latest price of the stock 、 Pankou data , Thus according to the price + Handicap , Choose the right price to order . Besides , After real-time data landing , It can also serve our policy backtesting .

The most popular explanation of reptiles is

Reptiles , It is equivalent to imitating the action of web page query , For example, we enter in the browser www.baidu.com, The browser returns to Baidu's home page , In fact, this is a request + The process of returning . We're asking for an address , What is returned is data ( Although what we see is Baidu home page , In fact, there are some columns of data behind it , But the subsequent visual display became a web page ).

For requesting stock data , It's the same thing . For example, we request the data of a stock ( With 600000 Take Shanghai Pudong Development Bank as an example ), Enter... In the browser address :http://hq.sinajs.cn/list=sh600000, The stock code can be modified at will , The browser returns the following data set and displays :

Of course, query multiple stocks at the same time , It can also be done , We also enter the address in the browser , for example :http://hq.sinajs.cn/list=sh600000,sz000001,sz000002, At the same time, I inquired about Shanghai Pudong Development Bank 、 Ping An Bank 、 vanke A The data of .

that , about Python Come on , How do we use Python To imitate the action of Web query ? There must be a Python Three party Library :request library .

requests It's using Apache2 licensed Permit HTTP library .Request Support HTTP Connection hold and connection pool , Support use cookie Keep talking , Support file upload , Support automatic response content encoding , Support international URL and POST Automatic data coding . so to speak ,requests stay python The built-in module is highly encapsulated , Thus making python When making a network request , Become human , Use Requests It's easy to do whatever the browser can do . meanwhile ,requests Persistent connections are automatically made keep-alive.

requests The above advantages of the library , Plus the convenience of its use , Make it Python The preferred tool for crawlers . We go through requests, Let's repeat the query process of the above web page , The steps are also very simple , First, query a single stock , Return as follows

Query multiple stocks again , Return as follows , You can see , use requests The result returned by the query , It's exactly the same as what we see on our website , This also shows that requests It perfectly simulates the request action of the browser .

Python Reptiles Sina Stock actual combat

First step ,Sina API + Requests Library calls core functions

By calling Sina Stock API, Query the stock price in real time . We use multi stock query , Use requests Request interface .

Core function logic :code Pass in the stock code , call requests Query the library , And parse the query results , Get the latest price of the stock we want to query 、 The day's rise and fall 、 Yesterday's closing price and other key fields

The second step , adopt threading Multi thread simultaneous query results 、 adopt Queue Implement thread pool

Let's briefly introduce threading and Queue.threading The module contains rich functions about thread operation , Include : Common thread functions , Thread object , Lock object , Recursive lock object , Event object , Conditional variable object , Semaphore object , Timer Objects , Fence object .threading.Thread: Thread object , Important method ,start(): Start thread activity . It will make run() Method is called in a separate control thread , It should be noted that the same thread object start() Method can only be called once , If called more than once , Will report RuntimeError error .run(): This method represents thread activity .

Python Of Queue Synchronous... Is provided in the module 、 Thread safe queue class , Include FIFO( First in, first out ) queue Queue,LIFO( After the first out ) queue LifoQueue, And priority queues PriorityQueue. These queues all implement lock primitives , Can be used directly in multithreading . You can use queues to synchronize threads .Queue.put(item) Written to the queue ;Queue.get([block[, timeout]]) Get the queue

The third step , Modular implementation :Worker class & Stock class

Worker class , Main functions :

  1. Incoming thread instance object , call init、start Method , heavy load run function

  2. work_queue Store the queue to query , Take out one by one (FIFO), Save the query results to result_queue queue , When the queue is full , Retrieve all query results in the result queue , And print

Stock class , Main functions :

  1. Initialize the producer 、 Consumer queue , The producer queue is empty , The maximum number of consumer queues is the number of stocks queried

  2. According to the preset thread data , Initializes the thread pool , binding Worker class

  3. For every stock you query , Join the producer queue

  4. Define the function for the crawler to get data

Last , We call Worker Classes and Stock class , You can get the results you want .

Operation result display

The program can not only obtain individual stock data , You can also get index data , We subscribe to the Shanghai index by default 、 Shenzhen Composite Index 、 vanke A、 Pudong Development Bank ,4 Data streams , The following is a display of the default run results , We printed the latest price 、 applies 、 Yesterday's closing price and other key data . We follow certain time intervals , Get data and print .

Program extension : At the main program entry , You can modify the number of stocks queried , We support simultaneous query of multiple stocks ; Query interval 、 Modification of the number of threads queried .

Of course , We can also further expand the crawler program , For example, calculate the quantization strategy signal , The landing data is used as the back test data , wait .

If you want to get this Python Reptiles Sina Stock Program , Welcome to scan code to add quantity technology to wechat , Get full code .


Previous dry goods sharing recommended reading

Digital currency stabilizes currency to grid market making strategy

Digital currency capital cost strategy

Share a yearly 15% The above risk-free arbitrage opportunities

Grid trading system development

Analyze and forecast the stock price through deep learning stock price cross section data

Omega System Trading and Development Club Internal sharing strategy Easylanguage Source code

A complete machine learning solution for real data sets ( Next )

A complete machine learning solution for real data sets ( On )

How to use trading pioneers (TB) Developing a digital currency strategy

Stock index futures high frequency data machine learning prediction

How to use TradingView(TV) Back testing digital currency trading strategy

How to invest in stock funds ? When to buy ? Can I help you? ?

【 Quantity technology house | Quantitative investment strategy series share 】 Stock index futures trading strategy based on moving average index

AMA Index original author Perry Kaufman 100+ Set of transaction strategy source sharing

【 Quantity technology house | Options series share 】 Options strategy “ The lonely nine sword ”

【 Quantity technology house | Financial data series sharing 】 The spread sequence calculation of arbitrage strategy , I'm afraid it's not as simple as you think

【 Quantity technology house | Quantitative investment strategy series share 】 Follow the mature trading strategy of futures

How to get free historical data of digital currency

【 Quantity technology house | Quantitative investment strategy series share 】 Multi period resonant trading strategy

【 Quantity technology house | Financial data analysis series sharing 】 Why Zhongzheng 500(IC) Is the most suitable index for long-term long

Commodity spot data is not easy to get ? The seasonality of goods is hard to track ? One click solution to the trouble free Python Crawler sharing

【 Quantity technology house | Financial data analysis series sharing 】 How to copy bottom commodity futures correctly 、 Commodities

【 Quantity technology house | Quantitative investment strategy series share 】 Stock index futures IF Minute volatility statistics strategy

【 Quantity technology house | Python Crawler series share 】 Real time monitoring of major stock market announcements Python Reptiles

Get stock data in real time , free !——Python Reptiles Sina Stock More articles about actual combat

  1. 【 Quantity technology house | Python Crawler series share 】 Real time monitoring of major stock market announcements Python Reptiles

    Real time monitoring of major stock market announcements Python Reptilian tips We have limited energy , How to monitor information more efficiently ? A lot of times, especially when trading , We need to find a way to monitor some information , For example, the announcement of the stock market . If the existing software can't meet our requirements , then ...

  2. Android Real time access to the latest data in network development

    In the actual development, we need to obtain the latest data in real time , For example, road traffic . Real time weather information, etc , At this time, you need to control the update of the view through a thread . Example : We first create a web page to display the current time of the system , And then in Android Every... In the program 5 second ...

  3. In my new book , Trying to tell with stock cases Python Reptile big data visualization and other knowledge

    My new book ,< Based on the stock big data analysis Python Introduction of actual combat >, It is expected that 2019 Published in Tsinghua Press at the end of the year . If you are interested in big data analysis , I want to learn Python, This book is a good choice . In terms of knowledge system , ...

  4. Python Stock analysis series —— Series introduction and access to stock data .p1

    This series is reprinted from youtuber sentdex Content of the tutorial video of the blogger https://www.youtube.com/watch?v=19yyasfGLhk&index=4&list=PLQ ...

  5. Python Reptiles - Stock data Scrapy Reptiles

    2017-08-06 19:52:21 The goal is : Obtain the name and trading information output of all stocks of Shanghai Stock Exchange and Shenzhen Stock Exchange : Save to file technology roadmap :scrapy Get a list of stocks : Dongfang fortune.com :http://quote.eastmone ...

  6. R Get stock data

    R Several of them Pkg Both provide online download methods of stock data , If you have to find one of the best , that quantmod fully deserve ! For example , For example, download the Shanghai stock market data , The code can be : library(quantmod)SSE & ...

  7. PyQt Learning essays :Model/View The method of real-time obtaining change data in view data item editing change in

    about Model/View After editing the data in the view, how can I get the change position and change of the edited data in real time? I consulted some materials , Finally, I basically understand that I have to rewrite Model Of setData Method to intercept .setData The method is to use various role data in the view ...

  8. One adoption python Open source library for stock data , Quite complete , And some quantitative investment strategy library

    tushare: http://tushare.waditu.com/index.html Why Python? Just follow javascript stay web The field has the same unshakable position ,Python It's also being quantified in finance ...

  9. Anaconda Installation and use akshare Get stock data

    Introduce Anaconda It's open source. Python Package manager . both Python A collection of gift bags from various libraries , In particular, data analysis and scientific computing libraries are pre installed , It is also a tool that can create virtual machine environment . Why do I install The reason I installed it is not science ...

  10. js Get... In real time input data

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

Random recommendation

  1. BZOJ4551——[Tjoi2016&amp;Heoi2016] Trees

    1. The question : Given a rooted tree ( Root is 1), There are the following Two kinds of operations :1. Mark operation : Mark a node ( At the very beginning , Only nodes 1 Marked , Other nodes are unmarked , And for someone node , You can mark multiple times .)2. Query operation : Ask someone ...

  2. understand RHEL Installation on oracle Configuration parameters for

    No matter what version is installed oracle, Before installation , They all need to be configured /etc/pam.d/login   /etc/profile   /etc/security/limits.conf These three files Then these three documents ...

  3. There is nothing in the world RedBSD,SuseBSD perhaps ArchBSD,Turb...

    There is nothing in the world RedBSD,SuseBSD perhaps ArchBSD,TurboBSD Things like that .

  4. rfc all download

    http://www.rfc-editor.org/download.html about RTSP http://en.wikipedia.org/wiki/Real_Time_Streaming_ ...

  5. go Realize the linked list of sorting

    go Realize the linked list of sorting Posted on 2013-08-14 23:25 mac Learning notes of   read (224) Comment on (0)  edit   Collection The data structure of the linked list is a linear array , Advantage is It can be easily inserted and inserted at any position ...

  6. Azure Messaging-ServiceBus Messaging Message queue technology series 8- Service bus quota

    In the last blog, we introduced Azure ServiceBus Messaging Message transaction mechanism : Azure Messaging-ServiceBus Messaging Message queue technology series 7- Message transaction (2017 ...

  7. Spring Security introduction (1-3-2)Spring Security - http Elements - intercept-url To configure

    http The login page can be configured under the element , You can also configure url Intercept . 1. Direct configuration interception url And corresponding access rights <security:http use-expressions="false" ...

  8. How to understand java It's an object-oriented language ?( from Hollis Face to face java)

    To understand this problem ? There's a lot to think about 1. What is object-oriented Break down the problem step by step , Each step is abstracted accordingly , Forming objects . Through calls between different objects , Combination to solve problems , in other words , In object-oriented programming , Attribute , Behavior, etc ...

  9. nginx unit PHP

    2018-12-26 14:20:33 Wednesday review : nginx unit php The relationship between : nginx -> Forward the request to 8300 port -> unit forward 8300 Requests received -& ...

  10. java utilize Tesseract Identification ID number

    install Tesseract http://blog.csdn.net/hiredme/article/details/50894814 http://blog.csdn.net/yoara/article ...