一、tick数据获取的三种方式
-
1、通过类似期货CTP接口的模式获取
-
2、通过券商获取tick数据
-
3、爬虫方式获取tick数据
二、pycharm实现数据获取
1 – 调用接口获取并添加refer
如果
import requests
报错
-
使用requests获取对应的数据
import requests
headers = {'Referer': "https://finance.sina.com.cn"}
page = requests.get("https://hq.sinajs.cn/list=sh600519", headers=headers)
stock_info = page.text
print(stock_info)
2 – 分割解析价格与时间
import time
import requests
from time import sleep
def get_tick():
headers = {'Referer': "https://finance.sina.com.cn"}
page = requests.get("https://hq.sinajs.cn/list=sh600519", headers=headers)
stock_info = page.text
mt_info = stock_info.replace("\"", "").split("=")[1].split(",")
# 收盘价
last = float(mt_info[1])
trade_datetime = mt_info[30] + ' ' + mt_info[31]
tick = (time.strftime("%H:%M:%S", time.localtime()), last, trade_datetime)
return tick
while True:
last_tick = get_tick()
print(last_tick)
sleep(3)
3 – 增加时间判断
-
from dateutil import parser
:在Terminal执行
pip install python-dateutil
import requests
from time import sleep
from datetime import time
from dateutil import parser
def get_tick():
headers = {'Referer': "https://finance.sina.com.cn"}
page = requests.get("https://hq.sinajs.cn/list=sh600519", headers=headers)
stock_info = page.text
mt_info = stock_info.replace("\"", "").split("=")[1].split(",")
# 收盘价
last = float(mt_info[1])
trade_datetime = mt_info[30] + ' ' + mt_info[31]
tick = (last, trade_datetime)
return tick
trade_time = time(9, 30) # 交易时间是9:30-15:00
while time(9) < trade_time < time(15):
last_tick = get_tick()
print(last_tick)
trade_time = parser.parse(last_tick[1]).time()
sleep(3)
print("job done")
版权声明:本文为qq23001186原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。