[关闭]
@Channelchan 2018-11-30T13:07:57.000000Z 字数 2322 阅读 61948

获取金融数据--BINANCE

可导入品种类型: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、json)

  1. import pandas as pd
  2. from datetime import datetime,timedelta
  3. import requests
  4. import json
  1. # Binance
  2. def getCandles(symbol, interval, limit=0, startTime=None, endTime=None):
  3. url = 'https://www.binance.co/api/v1/klines?'
  4. params = {
  5. 'symbol': symbol,
  6. 'interval': interval
  7. }
  8. if limit:
  9. params['limit'] = limit
  10. if startTime:
  11. params['startTime'] = int(startTime.timestamp()*1000)
  12. if endTime:
  13. params['endTime'] = int(endTime.timestamp()*1000)
  14. r = requests.get(url, headers={
  15. 'Content-Type': 'application/x-www-form-urlencoded',
  16. 'Accept': 'application/json'
  17. }, params = params,timeout=10)
  18. text = json.loads(r.content)
  19. df = pd.DataFrame(text, columns=[
  20. "opentime", "open", "high", "low", "close", "volume","closetime","Quote","trades","buybase","buyquote","ignore"])
  21. df["datetime"] = df["opentime"].map(lambda x: datetime.fromtimestamp(x / 1000).strftime("%Y%m%d %H:%M:%S"))
  22. return df
  23. # interval available: 1m, 3m, 5m, 15m, 30m, 1h, 2h, 4h, 6h, 8h, 12h, 1d, 3d, 1w, 1M
  1. symbol = 'EOSUSDT'
  2. EXCHANGE = "binance"
  3. def vt_symbol(symbol):
  4. return "%s:%s" % (symbol, EXCHANGE)
  5. def write(collection, data):
  6. for index, row in data.iterrows():
  7. bar = {}
  8. bar['open'] = float(row.open)
  9. bar['close'] = float(row.close)
  10. bar['high'] = float(row.high)
  11. bar['low'] = float(row.low)
  12. bar['volume'] = float(row.volume)
  13. bar['symbol'] = symbol
  14. bar['exchange'] = EXCHANGE
  15. bar['vtSymbol'] = vt_symbol(symbol)
  16. bar['openInterest'] = 0
  17. bar['datetime'] = datetime.strptime(row.datetime, "%Y%m%d %H:%M:%S")
  18. bar['date'] = bar['datetime'].date().strftime('%Y%m%d')
  19. bar['time'] = bar['datetime'].time().strftime('%H:%M:%S')
  20. flt = {'datetime': bar['datetime']}
  21. collection.update_one(flt, {'$set':bar}, upsert=True)
  22. from datetime import timedelta
  23. def iter_dates(start, end):
  24. start = start.replace(second=0, microsecond=0)
  25. while start < end:
  26. _next = start + timedelta(hours=12)
  27. yield start, _next - timedelta(seconds=1)
  28. start = _next
  1. import pymongo
  2. client = pymongo.MongoClient('localhost', 27017)
  3. collection = client['VnTrader_1Min_Db'][vt_symbol(symbol)]
  4. collection.create_index([('datetime', pymongo.ASCENDING)], unique=True)
  5. for s, e in iter_dates(datetime(2018, 10, 1), datetime(2018, 11, 29)):
  6. data = getCandles(symbol,'1m' , 1000,startTime=s, endTime=e)
  7. write(collection, data)
  8. print(symbol, s, e, data.shape)
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注