gpt4 book ai didi

python - 如何获取一分钟间隔的历史数据?

转载 作者:行者123 更新时间:2023-12-05 06:28:27 25 4
gpt4 key购买 nike

我需要获取一分钟间隔的历史交易数据。
我正在尝试使用 ccxt 获取它。但我得到了几个循环值。
我做错了什么?

import ccxt
import pandas as pd
import numpy as np
import time

np.set_printoptions(threshold=np.inf)

hitbtc = ccxt.hitbtc({'verbose': True})
bitmex = ccxt.bitmex()
huobi = ccxt.huobipro()
exchange = ccxt.exmo({
'apiKey': 'K-...',
'secret': 'S-...',
})

symbol = 'BTC/USD'
tf = '1m'
from_timestamp = exchange.parse8601('2019-01-10 00:00:00')
end = exchange.parse8601('2019-01-10 03:00:00')

# set timeframe in msecs
tf_multi = 60 * 1000
hold = 30

# make list to hold data
data = []

candle_no = (int(end) - int(from_timestamp)) / tf_multi + 1
print('downloading...')
while from_timestamp < end:
try:
ohlcvs = exchange.fetch_ohlcv(symbol, tf, from_timestamp)
from_timestamp += len(ohlcvs) * tf_multi
print(from_timestamp)
data += ohlcvs
print(str(len(data)) + ' of ' + str(int(candle_no)) + ' candles loaded...')
except (ccxt.ExchangeError, ccxt.AuthenticationError, ccxt.ExchangeNotAvailable, ccxt.RequestTimeout) as error:
print('Got an error', type(error).__name__, error.args, ', retrying in', hold, 'seconds...')
time.sleep(hold)

header = ['t', 'o', 'h', 'l', 'c', 'v']
df = pd.DataFrame(data, columns=header)
open('btcusd.txt', 'w')
np.savetxt('btcusd.txt', df.o, fmt='%.8f')

// https://pastebin.com/xy1Ddb5z - btcusd.txt

enter image description here

最佳答案

这是因为在 CCXT exmo.has['fetchOHLCV'] == 'emulated' 中解释如下:

请参阅 EXMO API 中 trades 方法的描述,它不接受任何时间范围参数,因此 fetch_ohlcv 的 since 参数无效,并且在 EXMO 的情况下被忽略。

import ccxt
import pandas as pd
import numpy as np
import time
import sys # ←---------------- ADDED

np.set_printoptions(threshold=np.inf)

hitbtc = ccxt.hitbtc({'verbose': True})
bitmex = ccxt.bitmex()
huobi = ccxt.huobipro()
exchange = ccxt.exmo({
'apiKey': 'K-...',
'secret': 'S-...',
})

symbol = 'BTC/USD'
tf = '1m'
from_timestamp = exchange.parse8601('2019-01-10 00:00:00')
end = exchange.parse8601('2019-01-10 03:00:00')

# set timeframe in msecs
tf_multi = 60 * 1000
hold = 30

# make list to hold data
data = []

# -----------------------------------------------------------------------------
# ADDED:
if exchange.has['fetchOHLCV'] == 'emulated':
print(exchange.id, " cannot fetch old historical OHLCVs, because it has['fetchOHLCV'] =", exchange.has['fetchOHLCV'])
sys.exit ()
# -----------------------------------------------------------------------------

candle_no = (int(end) - int(from_timestamp)) / tf_multi + 1
print('downloading...')
while from_timestamp < end:
try:
ohlcvs = exchange.fetch_ohlcv(symbol, tf, from_timestamp)
# --------------------------------------------------------------------
# ADDED:
# check if returned ohlcvs are actually
# within the from_timestamp > ohlcvs > end range
if (ohlcvs[0][0] > end) or (ohlcvs[-1][0] > end):
print(exchange.id, "got a candle out of range! has['fetchOHLCV'] =", exchange.has['fetchOHLCV'])
break
# ---------------------------------------------------------------------
from_timestamp += len(ohlcvs) * tf_multi
print(from_timestamp)
data += ohlcvs
print(str(len(data)) + ' of ' + str(int(candle_no)) + ' candles loaded...')
except (ccxt.ExchangeError, ccxt.AuthenticationError, ccxt.ExchangeNotAvailable, ccxt.RequestTimeout) as error:
print('Got an error', type(error).__name__, error.args, ', retrying in', hold, 'seconds...')
time.sleep(hold)

header = ['t', 'o', 'h', 'l', 'c', 'v']
df = pd.DataFrame(data, columns=header)
open('btcusd.txt', 'w')
np.savetxt('btcusd.txt', df.o, fmt='%.8f')

// https://pastebin.com/xy1Ddb5z - btcusd.txt

关于python - 如何获取一分钟间隔的历史数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54309223/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com