Skip to content

Commit

Permalink
example
Browse files Browse the repository at this point in the history
  • Loading branch information
51bitquant committed Jun 30, 2022
1 parent 9414006 commit 10bd489
Showing 1 changed file with 72 additions and 40 deletions.
112 changes: 72 additions & 40 deletions examples/download_data_demo2.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
"""
use binance api to get the kline datas.
author: 51bitquant
use the binance api to crawl data then save into the sqlite database.
discord: 51bitquant#8078
"""

import pandas as pd
import time
from datetime import datetime
import requests
import pytz
from howtrader.trader.database import BaseDatabase, get_database
from howtrader.trader.database import get_database, BaseDatabase

pd.set_option('expand_frame_repr', False) #
from howtrader.trader.object import BarData, Interval, Exchange
Expand All @@ -20,11 +18,8 @@

CHINA_TZ = pytz.timezone("Asia/Shanghai")
from threading import Thread


database: BaseDatabase = get_database()


def generate_datetime(timestamp: float) -> datetime:
"""
:param timestamp:
Expand All @@ -35,7 +30,7 @@ def generate_datetime(timestamp: float) -> datetime:
return dt


def get_binance_data(symbol: str, exchange: str, start_time: str, end_time: str, gate_way: str="BINANCE_USDT"):
def get_binance_data(symbol: str, exchange: str, start_time: str, end_time: str):
"""
crawl binance exchange data
:param symbol: BTCUSDT.
Expand All @@ -48,23 +43,24 @@ def get_binance_data(symbol: str, exchange: str, start_time: str, end_time: str,

api_url = ''
save_symbol = symbol


gateway = "BINANCE_USDT"
if exchange == 'spot':
print("spot")
limit = BINANCE_SPOT_LIMIT
save_symbol = symbol.lower()
gate_way = 'BINANCE'
gateway = 'BINANCE_SPOT'
api_url = f'https://api.binance.com/api/v3/klines?symbol={symbol}&interval=1m&limit={limit}'

elif exchange == 'usdt_future':
print('usdt_future')
limit = BINANCE_FUTURE_LIMIT
gateway = "BINANCE_USDT"
api_url = f'https://fapi.binance.com/fapi/v1/klines?symbol={symbol}&interval=1m&limit={limit}'

elif exchange == 'inverse_future':
print("inverse_future")
limit = BINANCE_FUTURE_LIMIT
gateway = "BINANCE_INVERSE"
f'https://dapi.binance.com/dapi/v1/klines?symbol={symbol}&interval=1m&limit={limit}'

else:
Expand All @@ -83,18 +79,18 @@ def get_binance_data(symbol: str, exchange: str, start_time: str, end_time: str,
"""
[
[
1591258320000, // 开盘时间
"9640.7", // 开盘价
"9642.4", // 最高价
"9640.6", // 最低价
"9642.0", // 收盘价(当前K线未结束的即为最新价)
"206", // 成交量
1591258379999, // 收盘时间
"2.13660389", // 成交额(标的数量)
48, // 成交笔数
"119", // 主动买入成交量
"1.23424865", // 主动买入成交额(标的数量)
"0" // 请忽略该参数
1591258320000, // open time
"9640.7", // open price
"9642.4", // highest price
"9640.6", // lowest price
"9642.0", // close price(latest price if the kline is not close)
"206", // volume
1591258379999, // close time
"2.13660389", // turnover
48, // trade count
"119", // buy volume
"1.23424865", // buy turnover
"0" // ignore
]
"""
Expand All @@ -113,13 +109,13 @@ def get_binance_data(symbol: str, exchange: str, start_time: str, end_time: str,
high_price=float(row[2]),
low_price=float(row[3]),
close_price=float(row[4]),
gateway_name=gate_way
gateway_name=gateway
)
buf.append(bar)

database.save_bar_data(buf)

# 到结束时间就退出, 后者收盘价大于当前的时间.
# exit the loop, if close time is greater than the current time
if (datas[-1][0] > end_time) or datas[-1][6] >= (int(time.time() * 1000) - 60 * 1000):
break

Expand All @@ -132,53 +128,89 @@ def get_binance_data(symbol: str, exchange: str, start_time: str, end_time: str,

def download_spot(symbol):
"""
下载现货数据的方法.
download binance spot data, config your start date and end date(format: year-month-day)
:return:
"""
t1 = Thread(target=get_binance_data, args=(symbol, 'spot', "2018-1-1", "2019-1-1"))
t1 = Thread(target=get_binance_data, args=(symbol, 'spot', "2018-1-1", "2018-6-1"))
t2 = Thread(target=get_binance_data, args=(symbol, 'spot', "2018-6-1", "2018-12-1"))

t3 = Thread(target=get_binance_data, args=(symbol, 'spot', "2018-12-1", "2019-6-1"))
t4 = Thread(target=get_binance_data, args=(symbol, 'spot', "2019-6-1", "2019-12-1"))

t2 = Thread(target=get_binance_data, args=(symbol, 'spot', "2019-1-1", "2020-1-1"))
t5 = Thread(target=get_binance_data, args=(symbol, 'spot', "2019-12-1", "2020-6-1"))
t6 = Thread(target=get_binance_data, args=(symbol, 'spot', "2020-6-1", "2020-12-1"))

t3 = Thread(target=get_binance_data, args=(symbol, 'spot', "2020-1-1", "2020-11-16"))
t7 = Thread(target=get_binance_data, args=(symbol, 'spot', "2020-12-1", "2021-6-1"))
t8 = Thread(target=get_binance_data, args=(symbol, 'spot', "2021-6-1", "2021-12-1"))
t9 = Thread(target=get_binance_data, args=(symbol, 'spot', "2021-12-1", "2022-6-28"))

t1.start()
t2.start()
t3.start()
t4.start()
t5.start()
t6.start()
t7.start()
t8.start()
t9.start()

t1.join()
t2.join()
t3.join()
t4.join()
t5.join()
t6.join()
t7.join()
t8.join()
t9.join()


def download_usdt_future(symbol):
def download_future(symbol):
"""
下载合约数据的方法。
download binance future data, config your start date and end date(format: year-month-day)
:return:
"""
t1 = Thread(target=get_binance_data, args=(symbol, 'usdt_future', "2019-9-10", "2020-3-1"))
t2 = Thread(target=get_binance_data, args=(symbol, 'usdt_future', "2019-3-1", "2020-11-16"))

t1 = Thread(target=get_binance_data, args=(symbol, 'usdt_future', "2020-1-1", "2020-6-1"))
t2 = Thread(target=get_binance_data, args=(symbol, 'usdt_future', "2020-6-1", "2020-12-1"))
t3 = Thread(target=get_binance_data, args=(symbol, 'usdt_future', "2020-12-1", "2021-6-1"))
t4 = Thread(target=get_binance_data, args=(symbol, 'usdt_future', "2021-6-1", "2021-12-1"))
t5 = Thread(target=get_binance_data, args=(symbol, 'usdt_future', "2021-12-1", "2022-6-28"))

t1.start()
t2.start()
t3.start()
t4.start()
t5.start()

t1.join()
t2.join()
t3.join()
t4.join()
t5.join()



if __name__ == '__main__':

# 如果你有代理你就设置,如果没有你就设置为 None 或者空的字符串 "",
# 但是你要确保你的电脑网络能访问币安交易所,你可以通过 ping api.binance.com 看看过能否ping得通
proxy_host = "127.0.0.1" # 如果没有就设置为"", 如果有就设置为你的代理主机如:127.0.0.1
proxy_port = 1087 # 设置你的代理端口号如: 1087, 没有你修改为0,但是要保证你能访问api.binance.com这个主机。
"""
read the code before run it. the software crawl the binance data then save into the sqlite database.
you may need to change the start date and end date.
"""

# proxy_host , if you can directly connect to the binance exchange, then set it to None or empty string "",如果没有你就设置为 None 或者空的字符串 "",
# you can use the command ping api.binance.com to check whether your network work well: 你可以在终端运行 ping api.binance.com 查看你的网络是否正常。
proxy_host = "127.0.0.1" # set it to your proxy_host 如果没有就设置为"", 如果有就设置为你的代理主机如:127.0.0.1
proxy_port = 1087 # set it to your proxy_port 设置你的代理端口号如: 1087, 没有你修改为0,但是要保证你能访问api.binance.com这个主机。

proxies = None
if proxy_host and proxy_port:
proxy = f'http://{proxy_host}:{proxy_port}'
proxies = {'http': proxy, 'https': proxy}

symbol = "BTCUSDT"
download_future(symbol="BTCUSDT") # crawl binance usdt_future data. 下载合约的数据

download_spot(symbol="BTCUSDT") # crawl binance spot data.

# download_spot(symbol) # 下载现货的数据.

download_usdt_future(symbol) # 下载合约的数据

0 comments on commit 10bd489

Please sign in to comment.