from jqdatasdk import *
import pymongo
import json
import time
import os
import random
import sys
# 初始化数据库
mongo_client = pymongo.MongoClient('mongodb://localhost:27017/')
auth('', '') # ID是申请时所填写的手机号;Password为聚宽官网登录密码
# 查询当日剩余可调用数据条数 2857902405
count = get_query_count()
print(count)
def ShareTODB(num):
mongo_db = mongo_client['JoinQuantDB']
mongo_collection = mongo_db[str(num)]
mydict = {'age': num, 'name': '李四6', 'sex': False}
mongo_collection.insert_one(mydict)
def ChechProcRecord(num):
file = open('D:/shao.txt', 'r')
# 内容content
content = file.read() # 读取文件所有内容
# print(content)
result = content.find(str(num))
return result
# for line in file.readline():
# if str(num) in line:
# print("找到记录"+str(num))
# return True # print("找到记录")
# else:
# print("未找到记录")
# return False
# 对数据进行记录 Record the data
def RecordProgram(num):
ffw = open('D:/shao.txt', 'a+', errors='ignore') # 对数据进行记录
ffw.writelines(str(num))
ffw.writelines("\n")
ffw.close()
def DownloadJoinQuantShareholders(codes):
# 下标 subscript 记录
num = 0
for i in codes:
# #检查程序记录信息 Check procedure record 股东人数 number of shareholders
if ChechProcRecord(num) >= 0:
num = num + 1
print('continue' + str(num))
continue
else:
print(num, end=' ')
print(i)
time.sleep(1)
q = query(finance.STK_HOLDER_NUM).filter(finance.STK_HOLDER_NUM.code == i)
df = finance.run_query(q)
print(df)
JKdata = df.astype({'end_date': 'str'})
DataTranslation = df2bson(JKdata)
DataToDB('JoinQuantDB', 'NUMshareHolders'+str(i), DataTranslation) # 将股票列表存入mongodb里面
ShareTODB(num)
RecordProgram(num) # 记录程序
num = num + 1
def FindDBCodeName(DBname, collectionName):
codes = []
names = []
mongo_db = mongo_client[DBname]
mongo_collection = mongo_db[collectionName]
allDate = mongo_collection.find()
for eve in allDate:
codes.append(eve['sec_code'])
names.append(eve['display_name'])
return codes, names
def DataToDB(DBname, collectionName, data):
mongo_db = mongo_client[DBname]
mongo_collection = mongo_db[collectionName]
result = mongo_collection.insert_many(data)
def df2bson(df):
"""DataFrame类型转化为Bson类型"""
data = json.loads(df.T.to_json()).values()
return data
def main():
# 查询所有股票代码 名称
allcodes, allnames = FindDBCodeName('JoinQuantDB', 'all_securities')
print(allcodes)
print(allnames)
# 下载 股东 人数 number of shareholders 如果包含子字符串,返回第一次出现该子字符串的开始索引值,否则返回-1。
DownloadJoinQuantShareholders(allcodes)
# q = query(finance.STK_HOLDER_NUM).filter(finance.STK_HOLDER_NUM.code == '000006.XSHE')
# df = finance.run_query(q)
# print(df)
# df.to_csv("D:/FDnumsc446.csv", encoding='gbk', index=True)
if __name__ == '__main__':
main()
[66 量化交易]下载股东人数
最新推荐文章于 2024-08-23 16:53:34 发布