获取金融数据 Futures,A Share - ChannelCMT/OFO GitHub Wiki

获取金融数据

-----------------------------------By Patrick 黄瀚祺

1、RESTFul API

2、开源数据 JAQS,Tushare

3、¥¥¥

可导入品种类型:Oanda、A股、商品期货、OKEX

Install

vnpy_fxdayu: https://github.com/xingetouzi/vnpy_fxdayu

Mongodb: https://www.mongodb.com/download-center#community

安装说明: https://github.com/xingetouzi/vnpy_fxdayu/wiki/Windows环境安装

建议安装robomongo作为可视化数据库管理

导入模块(request、json、pandas)

import pandas as pd
from datetime import datetime,timedelta
import requests

注册并获取API :https://www.quantos.org/

# Futures,A Share From JAQS
from jaqs.data import DataView,RemoteDataService
def getCandles(symbol,granularity,start,end):
    data_config = {
                "remote.data.address": "tcp://data.quantos.org:8910",
                "remote.data.username": "18566262672",
                "remote.data.password": "eyJhbGciOiJIUzI1NiJ9.eyJjcmVhdGVfdGltZSI6IjE1MTI3MDI3NTAyMTIiLCJpc3MiOiJhdXRoMCIsImlkIjoiMTg1NjYyNjI2NzIifQ.O_-yR0zYagrLRvPbggnru1Rapk4kiyAzcwYt2a3vlpM"
                }
    ds = RemoteDataService()
    ds.init_from_config(data_config)
    if end:
        end_time = end
    else:
        end_time = datetime.now().strftime('%Y%m%d')

    tradeDays=ds.query_trade_dates(start,end_time)
    i=0
    for trade_date in tradeDays:
        minutebar,msg=ds.bar(symbol=symbol,start_time=190000,end_time=185959,trade_date=trade_date, freq=granularity,fields="")
        trade_datetime = []
        for j in range(0,len(minutebar)):
            stamp = datetime.strptime((str(minutebar['date'][j]) + ' ' + str(minutebar['time'][j]).zfill(6)),"%Y%m%d %H%M%S")
            trade_datetime.insert(j,stamp)

        minutebar['datetime']=trade_datetime
        minute=minutebar[['datetime','open','close','high','low','volume']]
        if i>0:
            result=result.append(minute)
        else:
            result= minute
            i+=1
    return result#.to_dict(orient = 'list')
getCandles('300001.SZ','5M','20181023','20181102').tail(10)
# granularity available: 1M, 5M, 15M
Begin: DataApi login 18566262672@tcp://data.quantos.org:8910
    login success 
datetime open close high low volume
38 2018-11-02 14:15:00 13.82 13.84 13.84 13.80 45900.0
39 2018-11-02 14:20:00 13.84 13.83 13.84 13.82 31300.0
40 2018-11-02 14:25:00 13.83 13.84 13.84 13.82 39900.0
41 2018-11-02 14:30:00 13.84 13.90 13.90 13.84 139539.0
42 2018-11-02 14:35:00 13.90 13.85 13.91 13.85 92700.0
43 2018-11-02 14:40:00 13.87 13.84 13.87 13.83 32400.0
44 2018-11-02 14:45:00 13.84 13.84 13.87 13.84 35400.0
45 2018-11-02 14:50:00 13.84 13.79 13.84 13.78 151900.0
46 2018-11-02 14:55:00 13.79 13.87 13.88 13.78 258700.0
47 2018-11-02 15:00:00 13.88 13.95 13.95 13.87 406460.0
getCandles('rb.SHF','1M','20181101','20181106').tail(5)
Begin: DataApi login 18566262672@tcp://data.quantos.org:8910
    Already login as 18566262672, skip init_from_config
datetime open close high low volume
550 2018-11-05 14:56:00 4061.0 4062.0 4063.0 4059.0 12864.0
551 2018-11-05 14:57:00 4062.0 4066.0 4068.0 4061.0 40838.0
552 2018-11-05 14:58:00 4066.0 4068.0 4069.0 4066.0 21708.0
553 2018-11-05 14:59:00 4069.0 4073.0 4073.0 4066.0 48024.0
554 2018-11-05 15:00:00 4073.0 4072.0 4073.0 4069.0 46496.0

导入数据到mongodb

symbol = 'rb.SHF'
data =getCandles(symbol,'1M','20181101','20181106')

import pymongo
client = pymongo.MongoClient('localhost', 27017)
collection = client['FinData'][symbol]
collection.create_index([('datetime', pymongo.ASCENDING)], unique=True)
for index, row in data.iterrows():
    bar = {}
    bar['open'] = row.open
    bar['close'] = row.close
    bar['high'] = row.high
    bar['low'] = row.low
    bar['volume'] = row.volume
    bar['symbol'] = symbol
    bar['datetime'] = row.datetime
    bar['date'] = bar['datetime'].date().strftime('%Y%m%d')
    bar['time'] = bar['datetime'].time().strftime('%H:%M:%S')
    flt = {'datetime': bar['datetime']}
    collection.update_one(flt, {'$set':bar}, upsert=True)
⚠️ **GitHub.com Fallback** ⚠️