[关闭]
@Channelchan 2018-11-27T23:22:16.000000Z 字数 3278 阅读 65648

获取金融数据--Futures,A Share

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

1、RESTFul API

2、开源数据 JAQS,Tushare

3、¥¥¥

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

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、datetime、pandas)

  1. import pandas as pd
  2. from datetime import datetime,timedelta
  3. import requests

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

  1. # Futures,A Share From JAQS
  2. from jaqs.data import DataView,RemoteDataService
  3. def getCandles(symbol,granularity,start,end):
  4. data_config = {
  5. "remote.data.address": "tcp://data.quantos.org:8910",
  6. "remote.data.username": "18566262672",
  7. "remote.data.password": "eyJhbGciOiJIUzI1NiJ9.eyJjcmVhdGVfdGltZSI6IjE1MTI3MDI3NTAyMTIiLCJpc3MiOiJhdXRoMCIsImlkIjoiMTg1NjYyNjI2NzIifQ.O_-yR0zYagrLRvPbggnru1Rapk4kiyAzcwYt2a3vlpM"
  8. }
  9. ds = RemoteDataService()
  10. ds.init_from_config(data_config)
  11. if end:
  12. end_time = end
  13. else:
  14. end_time = datetime.now().strftime('%Y%m%d')
  15. tradeDays=ds.query_trade_dates(start,end_time)
  16. i=0
  17. for trade_date in tradeDays:
  18. minutebar,msg=ds.bar(symbol=symbol,start_time=190000,end_time=185959,trade_date=trade_date, freq=granularity,fields="")
  19. trade_datetime = []
  20. for j in range(0,len(minutebar)):
  21. stamp = datetime.strptime((str(minutebar['date'][j]) + ' ' + str(minutebar['time'][j]).zfill(6)),"%Y%m%d %H%M%S")
  22. trade_datetime.insert(j,stamp)
  23. minutebar['datetime']=trade_datetime
  24. minute=minutebar[['datetime','open','close','high','low','volume']]
  25. if i>0:
  26. result=result.append(minute)
  27. else:
  28. result= minute
  29. i+=1
  30. return result#.to_dict(orient = 'list')
  31. getCandles('300001.SZ','5M','20181023','20181102').tail(10)
  32. # 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
  1. 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

  1. symbol = 'rb.SHF'
  2. data =getCandles(symbol,'1M','20181101','20181106')
  3. import pymongo
  4. client = pymongo.MongoClient('localhost', 27017)
  5. collection = client['FinData'][symbol]
  6. collection.create_index([('datetime', pymongo.ASCENDING)], unique=True)
  7. for index, row in data.iterrows():
  8. bar = {}
  9. bar['open'] = row.open
  10. bar['close'] = row.close
  11. bar['high'] = row.high
  12. bar['low'] = row.low
  13. bar['volume'] = row.volume
  14. bar['symbol'] = symbol
  15. bar['datetime'] = row.datetime
  16. bar['date'] = bar['datetime'].date().strftime('%Y%m%d')
  17. bar['time'] = bar['datetime'].time().strftime('%H:%M:%S')
  18. flt = {'datetime': bar['datetime']}
  19. collection.update_one(flt, {'$set':bar}, upsert=True)
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注