[关闭]
@Channelchan 2019-03-27T20:57:14.000000Z 字数 2715 阅读 65969

获取金融数据--OANDA

-----------------------------------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说明: http://developer.oanda.com/rest-live/rates/#retrieveInstrumentHistory

count为返回的蜡烛数,如果没有指定,数量将默认为500,最大可接受值数是5000。

candleFormat 为可选的蜡烛参数表示,“midpoint” - 基于中点的参数。“bidask” - 基于买/卖的参数,如果未指定candleFormat参数,candleFormat的默认值为“bidask”。

granularity为可选每个参数表示的时间范围。
秒可选为S5、S10、S15、S30、M1;

分钟可选为M2、M3、M4、M5、M10、M15、M30、H1;

小时可选为H2、H3、H4、H6、H8、H12、D.

其中D为1day,W为1week,M为1month.

url中自行修改start的时间,其中T15为15点,表示收集当天15点以后的数据。
获取数据后,再将数据转换为dataframe的格式。

若收集一定区间的数据,在末端加上&end=

  1. import json
  2. # OANDA
  3. def getCandles(instrument,granularity,candleFormat=None,count=None,start=None):
  4. params = {'instrument':instrument,'granularity':granularity,'candleFormat':candleFormat,'count':count,'start':start}
  5. url = "https://api-fxtrade.oanda.com/v1/candles"
  6. r = requests.get(url, headers={"contentType": "application/x-www-form-urlencoded"}, params=params,timeout=10)
  7. text = json.loads(r.content)
  8. data = pd.DataFrame(text["candles"])
  9. data["datetime"] = data["time"].apply(pd.to_datetime)
  10. data.rename(columns={'closeMid':'close', 'highMid':'high', 'lowMid':'low', 'openMid':'open'}, inplace = True)
  11. return data
  12. getCandles('XAU_USD','M1','midpoint',start = '2018-10-29').tail()
  13. # granularity available:S5/ S10/ S15/ S30/ M1/ M2/ M3/ M4/ M5/ M10/ M15/ M30/ H1/ H2/ H3/ H4/ H6/ H8/ H12/ D/ W/ M
close complete high low open time volume datetime
495 1230.4085 True 1230.500 1230.387 1230.4280 2018-10-29T08:36:00.000000Z 9 2018-10-29 08:36:00
496 1230.2030 True 1230.459 1230.203 1230.3680 2018-10-29T08:37:00.000000Z 8 2018-10-29 08:37:00
497 1230.0775 True 1230.162 1230.078 1230.1620 2018-10-29T08:38:00.000000Z 3 2018-10-29 08:38:00
498 1230.0235 True 1230.204 1229.993 1230.0365 2018-10-29T08:39:00.000000Z 18 2018-10-29 08:39:00
499 1229.9695 True 1230.113 1229.812 1229.9830 2018-10-29T08:40:00.000000Z 50 2018-10-29 08:40:00

导入数据到mongodb

  1. symbol = 'XAU_USD'
  2. data =getCandles(symbol,'M1','midpoint',start = '2018-10-29')
  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)
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注