前往:http://quantapi.10jqka.com.cn/?page=home 如果是windows,根据不同用户,可以选择不同的下载。
1、iFinDPy 模块
下载完成后,会出现一个DataInterface_free_Windows_20210812.7z的文件,解压到任意文件夹中。打开该文件夹进入Bin文件夹后,再打开Tool文件夹,双击运行SuperCommand.exe,并使用iFinD账号免费登录。 第一次登录账号,可能会弹出环境修复工具,请赋予环境修复工具管理员权限,如果您根据刚才的指导安装了python。请在修复界面选择python。进入下一步完成修复即可。 若需要在python中提取数据,您需要首先将同花顺iFinD数据接口的包导入到python当前的环境中。详细操作如下:
打开IDLE输入:from iFinDPy import *之后输入登录函数,登录您的账号:
THS_iFinDLogin(“账号”,”密码”)
二、相关代码 安装iFinDPy后,即可。具体可以参考:http://quantapi.10jqka.com.cn/?page=sample。
from iFinDPy import *
from datetime import datetime
import pandas as pd
import time as _time
import json
from threading import Thread,Lock,Semaphore
import requests
sem = Semaphore(5)
dllock = Lock()
def thslogindemo():
thsLogin = THS_iFinDLogin("数据接口_账号","数据接口_密码")
print(thsLogin)
if thsLogin != 0:
print('登录失败')
else:
print('登录成功')
def datepool_basicdata_demo():
data_hs300 = THS_DP('block', '2020-11-16;001005290', 'date:Y,thscode:Y,security_name:Y')
if data_hs300.errorcode != 0:
print('error:{}'.format(data_hs300.errmsg))
else:
seccode_hs300_list = data_hs300.data['THSCODE'].tolist()
data_result = THS_BD(seccode_hs300_list, 'ths_close_price_stock', '2020-11-16,100')
if data_result.errorcode != 0:
print('error:{}'.format(data_result.errmsg))
else:
data_df = data_result.data
print(data_df)
def datapool_realtime_demo():
today_str = datetime.today().strftime('%Y-%m-%d')
print('today:{}'.format(today_str))
data_sz50 = THS_DP('block', '{};001005260'.format(today_str), 'date:Y,thscode:Y,security_name:Y')
if data_sz50.errorcode != 0:
print('error:{}'.format(data_sz50.errmsg))
else:
seccode_sz50_list = data_sz50.data['THSCODE'].tolist()
data_result = THS_RQ(seccode_sz50_list,'latest')
if data_result.errorcode != 0:
print('error:{}'.format(data_result.errmsg))
else:
data_df = data_result.data
print(data_df)
data_df.to_csv('realtimedata_{}.csv'.format(today_str))
def iwencai_demo():
print('输出资金流向数据')
data_wencai_zjlx = THS_WC('主力资金流向', 'stock')
if data_wencai_zjlx.errorcode != 0:
print('error:{}'.format(data_wencai_zjlx.errmsg))
else:
print(data_wencai_zjlx.data)
print('输出股性评分数据')
data_wencai_xny = THS_WC('股性评分', 'stock')
if data_wencai_xny.errorcode != 0:
print('error:{}'.format(data_wencai_xny.errmsg))
else:
print(data_wencai_xny.data)
def dlwork(tick_data):
dllock.acquire()
with open('dlwork.txt', 'a') as f:
for stock_data in tick_data['tables']:
if 'time' in stock_data:
timestr = _time.strftime('%Y-%m-%d %H:%M:%S', _time.localtime(stock_data['time'][0]))
print(timestr)
f.write(timestr + str(stock_data) + '\n')
else:
pass
dllock.release()
def work(codestr,lock,indilist):
sem.acquire()
stockdata = THS_HF(codestr, ';'.join(indilist),'','2020-08-11 09:15:00', '2020-08-11 15:30:00','format:json')
if stockdata.errorcode != 0:
print('error:{}'.format(stockdata.errmsg))
sem.release()
else:
print(stockdata.data)
lock.acquire()
with open('test1.txt', 'a') as f:
f.write(str(stockdata.data) + '\n')
lock.release()
sem.release()
def multiThread_demo():
today_str = datetime.today().strftime('%Y-%m-%d')
print('today:{}'.format(today_str))
data_alla = THS_DP('block', '{};001005010'.format(today_str), 'date:Y,thscode:Y,security_name:Y')
if data_alla.errorcode != 0:
print('error:{}'.format(data_alla.errmsg))
else:
stock_list = data_alla.data['THSCODE'].tolist()
indi_list = ['close', 'high', 'low', 'volume']
lock = Lock()
btime = datetime.now()
l = []
for eachlist in [stock_list[i:i + int(len(stock_list) / 10)] for i in
range(0, len(stock_list), int(len(stock_list) / 10))]:
nowstr = ','.join(eachlist)
p = Thread(target=work, args=(nowstr, lock, indi_list))
l.append(p)
for p in l:
p.start()
for p in l:
p.join()
etime = datetime.now()
print(etime-btime)
pd.options.display.width = 320
pd.options.display.max_columns = None
def reportDownload():
df = THS_ReportQuery('300033.SZ','beginrDate:2021-08-01;endrDate:2021-08-31;reportType:901','reportDate:Y,thscode:Y,secName:Y,ctime:Y,reportTitle:Y,pdfURL:Y,seq:Y').data
print(df)
for i in range(len(df)):
pdfName = df.iloc[i,4]+str(df.iloc[i,6])+'.pdf'
pdfURL = df.iloc[i,5]
r = requests.get(pdfURL)
with open(pdfName,'wb+') as f:
f.write(r.content)
def main():
thslogindemo()
if __name__ == '__main__':
main()
|