@Channelchan
2019-01-02T16:01:26.000000Z
字数 2660
阅读 66793
-----------------------------------By Patrick 黄瀚祺
可导入品种类型:Oanda、A股、商品期货、OKEX、binance
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作为可视化数据库管理
import pandas as pd
from datetime import datetime,timedelta
import requests
# OKEX V1
def getCandles(symbol, type, contract_type=None, size=None, since=None):
params = {'symbol':symbol,'type':type,'contract_type':contract_type,'size':size,'since':since}
if contract_type: # url for future
url = 'https://www.okex.me/api/v1/future_kline.do?'
else: # url for spot
url = 'https://www.okex.me/api/v1/kline.do?'
r = requests.get(url, params = params,timeout=10)
text = eval(r.text)
if contract_type:
df = pd.DataFrame(text, columns=["datetime", "open", "high", "low", "close", "volume","%s_volume"%symbol])
else:
df = pd.DataFrame(text, columns=["datetime", "open", "high", "low", "close", "volume"])
df["datetime"] = df["datetime"].map(lambda x: datetime.fromtimestamp(x / 1000))
# delta = datetime.timedelta(hours=8)
# df.rename(lambda s: datetime.datetime.strptime(s, "%Y-%m-%d %H:%M:%S") + delta) # Alter TimeZone
return df#.to_csv('a.csv')
getCandles('eos_usd','1min','quarter',size = 10)
# type available: 1min/3min/5min/15min/30min/1day/3day/1week/1hour/2hour/4hour/6hour/12hour
# size value up to 2000
# 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 |
symbol = 'eos-usdt'
data =getCandles(symbol,'1min','quarter',size = 10)
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)