从 binance 获取历史数据
Get historical data from binance
我正在尝试提取 [curr_time - 2 年 curr_time] 之间的历史数据。时间间隔为1天。所以,我预计大约有 700 件商品,但我只收到了 3 件商品。
我该如何解决这个问题?
我的代码
from binance.client import Client
# Binance test_key https://testnet.binance.vision/key/generate
API_KEY = "---"
API_SECRET = "---"
DAYS_IN_YEAR = 365
DB_NAME = "charts"
def GetHistoricalData(
timedelta_days=DAYS_IN_YEAR * 2,
ticker="BTCUSDT",
kline_interval=Client.KLINE_INTERVAL_1HOUR
):
start_time = time.time()
untilThisDate = datetime.datetime.now()
sinceThisDate = untilThisDate - datetime.timedelta(days=timedelta_days)
print("ZZZZZZZZZ_ ", str(sinceThisDate), str(untilThisDate)) # 2019-11-06 00:23:43.620016 2021-11-05 00:23:43.620016
client = Client(API_KEY, API_SECRET)
client.API_URL = 'https://testnet.binance.vision/api'
candle = client.get_historical_klines(ticker, kline_interval, str(sinceThisDate), str(untilThisDate))
print("CANDLE_", len(candle)) # 3
我试过这个请求:
candle = client.get_historical_klines(ticker, kline_interval, "01 January, 2019", "04 November 2021")
但又只收到了3件
dateTime ...
2021-11-02 00:00:00 61722.80000000 150535.61000000 ... 448.99018200 1635897599999
2021-11-03 00:00:00 63208.69000000 100000.00000000 ... 451.03367500 1635983999999
2021-11-04 00:00:00 62894.04000000 70000.00000000 ... 401.86212800 1636070399999
试试下面的代码。我得到一堆数据,但没有格式化:
import datetime
from binance.client import Client
import time
# Binance test_key https://testnet.binance.vision/key/generate
API_KEY = "---"
API_SECRET = "---"
DAYS_IN_YEAR = 365
DB_NAME = "charts"
def GetHistoricalData(
timedelta_days=DAYS_IN_YEAR * 2,
ticker="BTCUSDT",
kline_interval=Client.KLINE_INTERVAL_1HOUR
):
start_time = time.time()
untilThisDate = datetime.datetime.now()
sinceThisDate = untilThisDate - datetime.timedelta(days=timedelta_days)
print("ZZZZZZZZZ_ ", str(sinceThisDate),
str(untilThisDate)) # 2019-11-06 00:23:43.620016 2021-11-05 00:23:43.620016
client = Client(API_KEY, API_SECRET)
client.API_URL = 'https://testnet.binance.vision/api'
candle = client.get_historical_klines(ticker, kline_interval, str(sinceThisDate), str(untilThisDate))
print(candle)
GetHistoricalData()
嗯....
如果您尝试使用 API 调用请求此数据,它将为您提供:
In [1]: import requests
...: len(requests.get('https://testnet.binance.vision/api/v3/klines?symbol=BTCUSDT&interval=1h&limit=1000').json())
Out[1]: 65
但是如果您尝试使用 binance 的生产环境 运行 它(顺便说一句 klines/candles 是一个 public 数据并且不需要 apiKey):
In [2]: import requests
...: len(requests.get('https://api.binance.com/api/v3/klines?symbol=BTCUSDT&interval=1h&limit=1000').json())
Out[2]: 1000
因此,要修复您的示例,您需要替换 BASE_URL
client.API_URL = 'https://api.binance.com/api'
它给了我:
ZZZZZZZZZ_ 2019-11-06 01:15:15.122873 2021-11-05 01:15:15.122873
CANDLE_ 17483
我正在尝试提取 [curr_time - 2 年 curr_time] 之间的历史数据。时间间隔为1天。所以,我预计大约有 700 件商品,但我只收到了 3 件商品。
我该如何解决这个问题?
我的代码
from binance.client import Client
# Binance test_key https://testnet.binance.vision/key/generate
API_KEY = "---"
API_SECRET = "---"
DAYS_IN_YEAR = 365
DB_NAME = "charts"
def GetHistoricalData(
timedelta_days=DAYS_IN_YEAR * 2,
ticker="BTCUSDT",
kline_interval=Client.KLINE_INTERVAL_1HOUR
):
start_time = time.time()
untilThisDate = datetime.datetime.now()
sinceThisDate = untilThisDate - datetime.timedelta(days=timedelta_days)
print("ZZZZZZZZZ_ ", str(sinceThisDate), str(untilThisDate)) # 2019-11-06 00:23:43.620016 2021-11-05 00:23:43.620016
client = Client(API_KEY, API_SECRET)
client.API_URL = 'https://testnet.binance.vision/api'
candle = client.get_historical_klines(ticker, kline_interval, str(sinceThisDate), str(untilThisDate))
print("CANDLE_", len(candle)) # 3
我试过这个请求:
candle = client.get_historical_klines(ticker, kline_interval, "01 January, 2019", "04 November 2021")
但又只收到了3件
dateTime ...
2021-11-02 00:00:00 61722.80000000 150535.61000000 ... 448.99018200 1635897599999
2021-11-03 00:00:00 63208.69000000 100000.00000000 ... 451.03367500 1635983999999
2021-11-04 00:00:00 62894.04000000 70000.00000000 ... 401.86212800 1636070399999
试试下面的代码。我得到一堆数据,但没有格式化:
import datetime
from binance.client import Client
import time
# Binance test_key https://testnet.binance.vision/key/generate
API_KEY = "---"
API_SECRET = "---"
DAYS_IN_YEAR = 365
DB_NAME = "charts"
def GetHistoricalData(
timedelta_days=DAYS_IN_YEAR * 2,
ticker="BTCUSDT",
kline_interval=Client.KLINE_INTERVAL_1HOUR
):
start_time = time.time()
untilThisDate = datetime.datetime.now()
sinceThisDate = untilThisDate - datetime.timedelta(days=timedelta_days)
print("ZZZZZZZZZ_ ", str(sinceThisDate),
str(untilThisDate)) # 2019-11-06 00:23:43.620016 2021-11-05 00:23:43.620016
client = Client(API_KEY, API_SECRET)
client.API_URL = 'https://testnet.binance.vision/api'
candle = client.get_historical_klines(ticker, kline_interval, str(sinceThisDate), str(untilThisDate))
print(candle)
GetHistoricalData()
嗯....
如果您尝试使用 API 调用请求此数据,它将为您提供:
In [1]: import requests
...: len(requests.get('https://testnet.binance.vision/api/v3/klines?symbol=BTCUSDT&interval=1h&limit=1000').json())
Out[1]: 65
但是如果您尝试使用 binance 的生产环境 运行 它(顺便说一句 klines/candles 是一个 public 数据并且不需要 apiKey):
In [2]: import requests
...: len(requests.get('https://api.binance.com/api/v3/klines?symbol=BTCUSDT&interval=1h&limit=1000').json())
Out[2]: 1000
因此,要修复您的示例,您需要替换 BASE_URL
client.API_URL = 'https://api.binance.com/api'
它给了我:
ZZZZZZZZZ_ 2019-11-06 01:15:15.122873 2021-11-05 01:15:15.122873
CANDLE_ 17483