forked from 51bitquant/howtrader
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
lin
committed
Nov 15, 2020
1 parent
8295edc
commit 12cd053
Showing
12 changed files
with
227 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
""" | ||
we use the ccxt to crawl data then save it to csv file. | ||
you need to install ccxt by running firstly: | ||
""" | ||
|
||
import pandas as pd | ||
import time | ||
from datetime import datetime | ||
import requests | ||
import pytz | ||
from howtrader.trader.database import database_manager | ||
|
||
pd.set_option('expand_frame_repr', False) # | ||
from howtrader.trader.object import BarData, Interval, Exchange | ||
|
||
BINANCE_SPOT_LIMIT = 1000 | ||
BINANCE_FUTURE_LIMIT = 1500 | ||
|
||
CHINA_TZ = pytz.timezone("Asia/Shanghai") | ||
from threading import Thread | ||
|
||
|
||
def generate_datetime(timestamp: float) -> datetime: | ||
""" | ||
:param timestamp: | ||
:return: | ||
""" | ||
dt = datetime.fromtimestamp(timestamp / 1000) | ||
dt = CHINA_TZ.localize(dt) | ||
return dt | ||
|
||
|
||
def get_binance_data(symbol: str, exchanges: str, start_time: str, end_time: str): | ||
""" | ||
爬取币安交易所的数据 | ||
:param symbol: BTCUSDT. | ||
:param exchanges: 现货、USDT合约, 或者币币合约. | ||
:param start_time: 格式如下:2020-1-1 或者2020-01-01 | ||
:param end_time: 格式如下:2020-1-1 或者2020-01-01 | ||
:return: | ||
""" | ||
|
||
api_url = '' | ||
save_symbol = symbol | ||
gate_way = 'BINANCES' | ||
|
||
if exchanges == 'spot': | ||
print("spot") | ||
limit = BINANCE_SPOT_LIMIT | ||
save_symbol = symbol.lower() | ||
gate_way = 'BINANCE' | ||
api_url = f'https://api.binance.com/api/v3/klines?symbol={symbol}&interval=1m&limit={limit}' | ||
|
||
elif exchanges == 'future': | ||
print('future') | ||
limit = BINANCE_FUTURE_LIMIT | ||
api_url = f'https://fapi.binance.com/fapi/v1/klines?symbol={symbol}&interval=1m&limit={limit}' | ||
|
||
elif exchanges == 'coin_future': | ||
print("coin_future") | ||
limit = BINANCE_FUTURE_LIMIT | ||
f'https://dapi.binance.com/dapi/v1/klines?symbol={symbol}&interval=1m&limit={limit}' | ||
pass | ||
|
||
else: | ||
raise Exception('交易所名称请输入以下其中一个:spot, future, coin_future') | ||
|
||
start_time = int(datetime.strptime(start_time, '%Y-%m-%d').timestamp() * 1000) | ||
end_time = int(datetime.strptime(end_time, '%Y-%m-%d').timestamp() * 1000) | ||
|
||
while True: | ||
try: | ||
print(start_time) | ||
url = f'{api_url}&startTime={start_time}' | ||
print(url) | ||
|
||
data = requests.get(url=url).json() | ||
|
||
""" | ||
[ | ||
[ | ||
1591258320000, // 开盘时间 | ||
"9640.7", // 开盘价 | ||
"9642.4", // 最高价 | ||
"9640.6", // 最低价 | ||
"9642.0", // 收盘价(当前K线未结束的即为最新价) | ||
"206", // 成交量 | ||
1591258379999, // 收盘时间 | ||
"2.13660389", // 成交额(标的数量) | ||
48, // 成交笔数 | ||
"119", // 主动买入成交量 | ||
"1.23424865", // 主动买入成交额(标的数量) | ||
"0" // 请忽略该参数 | ||
] | ||
""" | ||
|
||
buf = [] | ||
|
||
for l in data: | ||
bar = BarData( | ||
symbol=save_symbol, | ||
exchange=Exchange.BINANCE, | ||
datetime=generate_datetime(l[0]), | ||
interval=Interval.MINUTE, | ||
volume=float(l[5]), | ||
open_price=float(l[1]), | ||
high_price=float(l[2]), | ||
low_price=float(l[3]), | ||
close_price=float(l[4]), | ||
gateway_name=gate_way | ||
) | ||
buf.append(bar) | ||
|
||
database_manager.save_bar_data(buf) | ||
|
||
# 到结束时间就退出, 后者收盘价大于当前的时间. | ||
if (data[-1][0] > end_time) or data[-1][6] >= (int(time.time() * 1000) - 60 * 1000): | ||
break | ||
|
||
start_time = data[-1][0] | ||
|
||
except Exception as error: | ||
print(error) | ||
time.sleep(10) | ||
|
||
|
||
def download_spot(): | ||
""" | ||
下载现货数据的方法. | ||
:return: | ||
""" | ||
t1 = Thread(target=get_binance_data, args=('BTCUSDT', 'spot', "2018-1-1", "2019-1-1")) | ||
|
||
t2 = Thread(target=get_binance_data, args=('BTCUSDT', 'spot', "2019-1-1", "2020-1-1")) | ||
|
||
t3 = Thread(target=get_binance_data, args=('BTCUSDT', 'spot', "2020-1-1", "2020-11-16")) | ||
|
||
t1.start() | ||
t2.start() | ||
t3.start() | ||
|
||
t1.join() | ||
t2.join() | ||
t3.join() | ||
|
||
|
||
def download_future(): | ||
""" | ||
下载合约数据的方法。 | ||
:return: | ||
""" | ||
t1 = Thread(target=get_binance_data, args=('BTCUSDT', 'future', "2019-9-10", "2020-3-1")) | ||
t2 = Thread(target=get_binance_data, args=('BTCUSDT', 'future', "2019-3-1", "2020-11-16")) | ||
|
||
t1.start() | ||
t2.start() | ||
|
||
t1.join() | ||
t2.join() | ||
|
||
|
||
if __name__ == '__main__': | ||
# download_spot() # 下载现货的数据. | ||
|
||
download_future() # 下载合约的数据 | ||
|
||
|
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.