[英]How to get historical data with one minute interval?
我需要以一分钟的间隔获取历史交易数据。
我正在尝试使用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
这是因为在 CCXT exmo.has['fetchOHLCV'] == 'emulated'
中解释如下:
请参阅 EXMO API 中对trades
方法的描述,它不接受任何时间范围参数,因此fetch_ohlcv
参数没有效果,在 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
我认为问题出在fetch_ohlcv
在您的while
fetch_ohlcv
返回重复值这一事实。 一旦你有了df
,就试试吧
# Keep only needed rows
df = df[df.Timestamp <= end]
# Delete duplicate rows
df = df.drop_duplicates()
然后你可以绘制(针对索引),例如
df['Close'].plot()
或者,如果您将时间戳转换为更易读的格式(例如使用exchange.iso8601()
,
plt.plot(df['Date']._values, df['Close']._values)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.