VeighNa量化社区
你的开源社区量化交易平台
Member
avatar
加入于:
帖子: 1
声望: 3
    量化分析离不开数据,米筐数据试用期过了要收费,但Tushare基本免费。量化分析数据源用Tushare的话,第一步就是下载数据存入数据库。Tuhare网站有代码例子,我又补充、修改、完善,下面是完整的Tushare股票数据存入MongoDB代码。

    1、代码是我修改、完善的,原代码来自Tushare公众号:挖地兔;
    2、insert_mongo():用 pymongo包把数据入库;
    3、save_bar_data():用 mongoengine包把数据入库.

    填入你的token码,代码立马可以运行。

import pandas as pd
import tushare as ts
from pymongo import MongoClient

# 调用tushare的pro_api借口
try:
    pro = ts.pro_api()
    pro
except Exception:
    token = '你的 token 码'
    ts.set_token(token)
    pro = ts.pro_api()


client = MongoClient(host='localhost',
                     port=27017)


def readfrom_mongo(exchange=None):
    db = client.demos
    collection = db.stock_basic
    rows = collection.find({})
    df = pd.DataFrame([basic for basic in rows])
    return df


def insert_mongo(df):
    db = client['demos']
    collection = db['stock_basic']
    collection.insert_many(df.to_dict('records'))


def save_bar_data(df, collection_name: str = None):
    connect(
        db="vnpytest",
        host= "localhost",
        port=27017,
        username="",
        password="",
        authentication_source="")

    class DbBarData(Document):
        ts_code: str = StringField()
        trade_date: datetime = DateTimeField()
        open: float = FloatField()
        high: float = FloatField()
        low: float = FloatField()
        close: float = FloatField()
        pre_close: float = FloatField()
        change: float = FloatField()
        pct_chg: float = FloatField()
        vol: float = FloatField()
        amount: float = FloatField()

    datetime_format = '%Y%m%d'
    df['trade_date'] = pd.to_datetime(df['trade_date'],format=datetime_format)
    datas = df.to_dict('records')

    with switch_collection(DbBarData, collection_name):
        for d in datas:
            DbBarData.objects(trade_date=d['trade_date']).update_one(upsert=True, **d)

def get_basic():
    pro = ts.pro_api()
    # df = pro.daily(trade_date='20200123')
    df = pro.daily(ts_code='000001.SZ', start_date='20180701', end_date='20180718')

    # insert_mongo(df)
    save_bar_data(df, 'stocksdocument')


if __name__ == '__main__':
    get_basic()
    # df = readfrom_mongo()
    # print(df)
Member
avatar
加入于:
帖子: 311
声望: 17

感谢分享!

© 2015-2022 上海韦纳软件科技有限公司
备案服务号:沪ICP备18006526号-3

沪公网安备 31011502017034号

【用户协议】
【隐私政策】
【免责条款】