# -*- coding: utf-8 -*-
import akshare as ak
from pymongo import UpdateOne
# from database import DB_CONN
from datetime import datetime
from pymongo import MongoClient
index_codes = ['sh000001', 'sh000300', 'sz399001', 'sz399005', 'sz399006']
data = ak.stock_zh_index_daily(symbol="sh000001")
stock_zh_index_daily_df = data.astype(str) # 将数据转换为字符串
print(stock_zh_index_daily_df)
'''
date open high low close volume
0 1990-12-19 96.050 99.980 95.790 99.980 126000
1 1990-12-20 104.300 104.390 99.980 104.390 19700
2 1990-12-21 109.070 109.130 103.730 109.130 2800
3 1990-12-24 113.570 114.550 109.130 114.550 3200
'''
# {'ts_code': '000001.SZ', 'trade_date': '20210105', 'open': 18.4, 'high': 18.48, 'low': 17.8, 'close': 18.17,
# 'pre_close': 18.6, 'change': -0.43, 'pct_chg': -2.3118, 'vol': 1821352.1, 'amount': 3284606.913}
DB_CONN = MongoClient('mongodb://127.0.0.1:27017')['quant_01']
# 创建daily数据集
daily = DB_CONN['daily']
# 创建daily_hfq数据集
daily_hfq = DB_CONN['daily_hfq']
def save_data(code, df_daily, collection, extra_fields=None):
update_requests = [] # 数据更新的请求列表
# 将DataFrame中的行情数据,生成更新数据的请求
print('+++++++++df_daily.index+++++++++++')
print(df_daily.index)
for df_index in df_daily.index:
# 将DataFrame中的一行数据转dict
doc = dict(df_daily.loc[df_index])
print('-------doc---------')
print(doc)
# 设置股票代码
doc['code'] = code
# 如果指定了其他字段,则更新dict
if extra_fields is not None:
doc.update(extra_fields)
# $set操作符替换掉指定字段的值 首先找到文档 集合 数据库
update_requests.append(
UpdateOne(
{'code': 'sh000001', 'date': doc['date'], 'index': doc['index']},
{'$set': doc},
upsert=True)
)
print('update_requests :')
print(len(update_requests))
# 如果写入的请求列表不为空,则保存都数据库中
if len(update_requests) > 0:
# 批量写入到数据库中,批量写入可以降低网络IO,提高速度
update_result = collection.bulk_write(update_requests, ordered=False)
print('保存日线数据,代码: %s, 插入:%4d条, 更新:%4d条' %
(code, update_result.upserted_count, update_result.modified_count),
flush=True)
save_data('sh000001', stock_zh_index_daily_df, daily, {'index': True})
[045]量化交易]python akshare 上证指数写入Mongodb
最新推荐文章于 2024-07-12 18:25:28 发布