程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python quantification - how to get real-time stock information

編輯:Python

How to get real-time stock information

There are many interfaces for stock information , Before, Sina was commonly used , But at the beginning of the year , Sina's interface is suddenly unavailable , It has caused a lot of trouble to everyone , For this reason, there are many tutorials on the Internet to teach you how to obtain data from Sina , I can't follow the tutorial for a long time , Simply change to 126( That is, Netease ), I think the speed is pretty good .

First, let's look at the interface address :http://api.money.126.net/data/feed/1000001,money.api

Among them 1000001 It's the stock code , Different from Sina , His first representative is the exchange , Back 6 Bit is the stock code

  • 0: Shanghai
  • 1: The shenzhen stock exchange
  • 2: Beijing stock exchange

First, view the data structure through the browser :

_ntes_quote_callback({
"1000001": {
"code": "1000001",
"percent": 0.002113,
"high": 14.25,
"askvol3": 1026758,
"askvol2": 810700,
"askvol5": 290493,
"askvol4": 461100,
"price": 14.23,
"open": 14.2,
"bid5": 14.18,
"bid4": 14.19,
"bid3": 14.2,
"bid2": 14.21,
"bid1": 14.22,
"low": 14.11,
"updown": 0.03,
"type": "SZ",
"bidvol1": 323600,
"status": 0,
"bidvol3": 244200,
"bidvol2": 673474,
"symbol": "000001",
"update": "2022/06/25 17:59:57",
"bidvol5": 343500,
"bidvol4": 145200,
"volume": 86604061,
"askvol1": 817268,
"ask5": 14.27,
"ask4": 14.26,
"ask1": 14.23,
"name": " Ping An Bank ",
"ask3": 14.25,
"ask2": 14.24,
"arrow": "↑",
"time": "2022/06/24 16:00:58",
"yestclose": 14.2,
"turnover": 1227798687.09
}
});

It can be seen that _ntes_quote_callback() What is in is the standard json data , We just need to use regular expressions to get out .
Let's first define a data structure :

class NetTick:
def __init__(self, dict={}):
self.name = dict.get('name') # The name of the stock
self.yestclose = dict.get('yestclose') # Yesterday's closing price
self.bidvol5 = dict.get('bidvol5') # buy 5 Number
self.bidvol4 = dict.get('bidvol4') # buy 4 Number
self.bidvol3 = dict.get('bidvol3') # buy 3 Number
self.bidvol2 = dict.get('bidvol2') # buy 2 Number
self.bidvol1 = dict.get('bidvol1') # buy 1 Number
self.bid5 = dict.get('bid5') # buy 5 Price
self.bid4 = dict.get('bid4') # buy 4 Price
self.bid3 = dict.get('bid3') # buy 3 Price
self.bid2 = dict.get('bid2') # buy 2 Price
self.bid1 = dict.get('bid1') # buy 1 Price
self.askvol5 = dict.get('askvol5') # sell 5 Number
self.askvol4 = dict.get('askvol4') # sell 4 Number
self.askvol3 = dict.get('askvol3') # sell 3 Number
self.askvol2 = dict.get('askvol2') # sell 2 Number
self.askvol1 = dict.get('askvol1') # sell 1 Number
self.ask5 = dict.get('ask5') # sell 5 Price
self.ask4 = dict.get('ask4') # sell 4 Price
self.ask3 = dict.get('ask3') # sell 3 Price
self.ask2 = dict.get('ask2') # sell 2 Price
self.ask1 = dict.get('ask1') # sell 1 Price
self.symbol = dict.get('symbol') # Stock code first place 1: The shenzhen stock exchange 0: Shanghai 2 Beijing stock exchange
self.volume = dict.get('volume') # volume
self.price = dict.get('price') # Current price
self.open = dict.get('open') # Opening price
self.low = dict.get('low') # The lowest price
self.high = dict.get('high') # Highest price
self.code = dict.get('code') # Remove the stock code marked as
self.turnover = dict.get('turnover') # turnover
self.percent = dict.get('percent') # applies
self.updown = dict.get('updown') # Up and down

Through research , We found that 126 The interface supports multiple stock queries , Then we can define two methods , One by one , Check more than one , The specific implementation is as follows :

import requests
import re
from models.nettick import NetTick
from utils.packages import *
class NetEaseData:
@staticmethod
def get_realtime_data(symbol):
"""
Netease's real-time data interface
:param symbol: Stock code
:return: Tick
"""
code = NetEaseData.convert_market(symbol)
try:
response = requests.get("http://api.money.126.net/data/feed/{},money.api".format(code)).text
re_find = NetEaseData.__re_find(response)
if re_find is not None:
find_stock = re_find.get(code)
if find_stock is not None:
return NetTick(find_stock)
except Exception as e:
logger.error(' Error requesting Netease interface , error message :{}'.format(e))
return None
@staticmethod
def convert_market(other_market_code=str):
"""
Convert the general stock code sz sh bj start + Stock code
"""
if other_market_code[0:2].lower() == 'sh':
return '0' + other_market_code[2:]
elif other_market_code[0:2].lower() == 'sz':
return '1' + other_market_code[2:]
else:
return '2' + other_market_code[2:]
@staticmethod
def get_realtime_datas(symbols=[]):
"""
Netease's real-time data interface
:param symbols: Stock code list
:return: Ticks list
"""
codes = [NetEaseData.convert_market(code) for code in symbols]
result = []
try:
response = requests.get("http://api.money.126.net/data/feed/{},money.api".format(','.join(codes))).text
re_find = NetEaseData.__re_find(response)
if re_find is not None:
for code in re_find:
item = re_find[code]
result.append(NetTick(item))
except Exception as e:
logger.error(' Error requesting Netease interface , error message :{}'.format(e))
return result
@staticmethod
def __re_find(response):
find = re.findall(r"_ntes_quote_callback\((.*)\);", response)
if len(find) >= 1:
return to_obj(find[-1])
return None
if __name__ == '__main__':
ticks = NetEaseData.get_realtime_datas(['sh588000', 'sz000001', 'bj831010'])
[print(tick.symbol, tick.name, tick.price) for tick in ticks]
tick = NetEaseData.get_realtime_data('sz127045')
print(tick.symbol, tick.name, tick.price)

It's very easy to use

  • NetEaseData.get_realtime_data: Acquire individual shares
  • NetEaseData.get_realtime_datas : Get multiple stock data

My stock code here is compatible with the original Sina model , You can make your own changes .

Currently, we are upgrading our own quantification platform , Some of the previous code will also be published , If you like it, please make a recommendation , thank you


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved