@Channelchan
2018-11-30T05:07:57.000000Z
字数 2322
阅读 62168
可导入品种类型: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 pdfrom datetime import datetime,timedeltaimport requestsimport json
# Binancedef getCandles(symbol, interval, limit=0, startTime=None, endTime=None):url = 'https://www.binance.co/api/v1/klines?'params = {'symbol': symbol,'interval': interval}if limit:params['limit'] = limitif startTime:params['startTime'] = int(startTime.timestamp()*1000)if endTime:params['endTime'] = int(endTime.timestamp()*1000)r = requests.get(url, headers={'Content-Type': 'application/x-www-form-urlencoded','Accept': 'application/json'}, params = params,timeout=10)text = json.loads(r.content)df = pd.DataFrame(text, columns=["opentime", "open", "high", "low", "close", "volume","closetime","Quote","trades","buybase","buyquote","ignore"])df["datetime"] = df["opentime"].map(lambda x: datetime.fromtimestamp(x / 1000).strftime("%Y%m%d %H:%M:%S"))return df# interval available: 1m, 3m, 5m, 15m, 30m, 1h, 2h, 4h, 6h, 8h, 12h, 1d, 3d, 1w, 1M
symbol = 'EOSUSDT'EXCHANGE = "binance"def vt_symbol(symbol):return "%s:%s" % (symbol, EXCHANGE)def write(collection, data):for index, row in data.iterrows():bar = {}bar['open'] = float(row.open)bar['close'] = float(row.close)bar['high'] = float(row.high)bar['low'] = float(row.low)bar['volume'] = float(row.volume)bar['symbol'] = symbolbar['exchange'] = EXCHANGEbar['vtSymbol'] = vt_symbol(symbol)bar['openInterest'] = 0bar['datetime'] = datetime.strptime(row.datetime, "%Y%m%d %H:%M:%S")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)from datetime import timedeltadef iter_dates(start, end):start = start.replace(second=0, microsecond=0)while start < end:_next = start + timedelta(hours=12)yield start, _next - timedelta(seconds=1)start = _next
import pymongoclient = pymongo.MongoClient('localhost', 27017)collection = client['VnTrader_1Min_Db'][vt_symbol(symbol)]collection.create_index([('datetime', pymongo.ASCENDING)], unique=True)for s, e in iter_dates(datetime(2018, 10, 1), datetime(2018, 11, 29)):data = getCandles(symbol,'1m' , 1000,startTime=s, endTime=e)write(collection, data)print(symbol, s, e, data.shape)
