[关闭]
@Channelchan 2019-01-02T16:01:26.000000Z 字数 2660 阅读 66793

获取金融数据--OKEX

-----------------------------------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.okex.com/docs/zh/

  1. # OKEX V1
  2. def getCandles(symbol, type, contract_type=None, size=None, since=None):
  3. params = {'symbol':symbol,'type':type,'contract_type':contract_type,'size':size,'since':since}
  4. if contract_type: # url for future
  5. url = 'https://www.okex.me/api/v1/future_kline.do?'
  6. else: # url for spot
  7. url = 'https://www.okex.me/api/v1/kline.do?'
  8. r = requests.get(url, params = params,timeout=10)
  9. text = eval(r.text)
  10. if contract_type:
  11. df = pd.DataFrame(text, columns=["datetime", "open", "high", "low", "close", "volume","%s_volume"%symbol])
  12. else:
  13. df = pd.DataFrame(text, columns=["datetime", "open", "high", "low", "close", "volume"])
  14. df["datetime"] = df["datetime"].map(lambda x: datetime.fromtimestamp(x / 1000))
  15. # delta = datetime.timedelta(hours=8)
  16. # df.rename(lambda s: datetime.datetime.strptime(s, "%Y-%m-%d %H:%M:%S") + delta) # Alter TimeZone
  17. return df#.to_csv('a.csv')
  18. getCandles('eos_usd','1min','quarter',size = 10)
  19. # type available: 1min/3min/5min/15min/30min/1day/3day/1week/1hour/2hour/4hour/6hour/12hour
  20. # size value up to 2000
  21. # since value using timestamp, eg. since = 1417536000000
datetime open high low close volume eos_usd_volume
0 2019-01-02 14:38:00 2.498 2.499 2.495 2.496 11301 45251.083247
1 2019-01-02 14:39:00 2.496 2.496 2.495 2.495 891 3570.800254
2 2019-01-02 14:40:00 2.495 2.495 2.483 2.485 20037 80508.371791
3 2019-01-02 14:41:00 2.484 2.490 2.480 2.490 48903 196775.630327
4 2019-01-02 14:42:00 2.490 2.493 2.489 2.490 11726 47085.711645
5 2019-01-02 14:43:00 2.489 2.490 2.483 2.488 21377 85950.163653
6 2019-01-02 14:44:00 2.488 2.489 2.484 2.484 18490 74367.765421
7 2019-01-02 14:45:00 2.485 2.491 2.485 2.490 17794 71500.862923
8 2019-01-02 14:46:00 2.490 2.492 2.487 2.491 11230 45106.396109
9 2019-01-02 14:47:00 2.490 2.490 2.490 2.490 378 1518.072289

导入数据到mongodb

  1. symbol = 'eos-usdt'
  2. data =getCandles(symbol,'1min','quarter',size = 10)
  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)
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注