Merge remote-tracking branch 'origin/master'
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
"""
|
||||
BitMart 30分钟K线数据抓取脚本
|
||||
从 BitMart API 获取30分钟K线数据并存储到数据库
|
||||
BitMart 15分钟K线数据抓取脚本
|
||||
从 BitMart API 获取15分钟K线数据并存储到数据库
|
||||
"""
|
||||
|
||||
import time
|
||||
@@ -33,7 +33,7 @@ class BitMartDataCollector:
|
||||
|
||||
response = self.contractAPI.get_kline(
|
||||
contract_symbol=self.contract_symbol,
|
||||
step=15, # 30分钟
|
||||
step=15, # 15分钟
|
||||
start_time=start_time,
|
||||
end_time=end_time
|
||||
)[0]
|
||||
@@ -88,23 +88,37 @@ class BitMartDataCollector:
|
||||
|
||||
return saved_count
|
||||
|
||||
def collect_historical_data(self, days=30):
|
||||
def collect_historical_data(self, start_date=None, days=None):
|
||||
"""
|
||||
抓取历史数据
|
||||
:param days: 抓取最近多少天的数据
|
||||
抓取历史数据(从指定日期到现在)
|
||||
:param start_date: 起始日期字符串,格式 'YYYY-MM-DD',如 '2025-01-01'
|
||||
:param days: 如果不指定 start_date,则抓取最近多少天的数据
|
||||
"""
|
||||
logger.info(f"开始抓取 BitMart {self.contract_symbol} 最近 {days} 天的30分钟K线数据")
|
||||
import datetime
|
||||
|
||||
end_time = int(time.time())
|
||||
start_time = end_time - 3600 * 24 * days
|
||||
now = int(time.time())
|
||||
|
||||
# 分批获取,每次获取7天的数据
|
||||
batch_days = 7
|
||||
if start_date:
|
||||
# 解析起始日期
|
||||
start_dt = datetime.datetime.strptime(start_date, '%Y-%m-%d')
|
||||
target_start_time = int(start_dt.timestamp())
|
||||
logger.info(f"开始抓取 BitMart {self.contract_symbol} 从 {start_date} 到现在的15分钟K线数据")
|
||||
elif days:
|
||||
target_start_time = now - 3600 * 24 * days
|
||||
logger.info(f"开始抓取 BitMart {self.contract_symbol} 最近 {days} 天的15分钟K线数据")
|
||||
else:
|
||||
target_start_time = now - 3600 * 24 * 30 # 默认30天
|
||||
logger.info(f"开始抓取 BitMart {self.contract_symbol} 最近 30 天的15分钟K线数据")
|
||||
|
||||
# 分批获取,每次获取5天的数据(15分钟K线数据量较大)
|
||||
batch_days = 5
|
||||
total_saved = 0
|
||||
fail_count = 0
|
||||
max_fail = 3 # 连续失败超过3次则停止
|
||||
|
||||
current_start = start_time
|
||||
while current_start < end_time:
|
||||
current_end = min(current_start + 3600 * 24 * batch_days, end_time)
|
||||
current_end = now
|
||||
while current_end > target_start_time:
|
||||
current_start = max(current_end - 3600 * 24 * batch_days, target_start_time)
|
||||
|
||||
logger.info(f"抓取时间段: {time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(current_start))} "
|
||||
f"到 {time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(current_end))}")
|
||||
@@ -114,10 +128,15 @@ class BitMartDataCollector:
|
||||
saved = self.save_klines(klines)
|
||||
total_saved += saved
|
||||
logger.info(f"本批次保存 {saved} 条数据,累计 {total_saved} 条")
|
||||
fail_count = 0 # 重置失败计数
|
||||
else:
|
||||
logger.warning("本批次未获取到数据")
|
||||
fail_count += 1
|
||||
logger.warning(f"本批次未获取到数据 (连续失败 {fail_count} 次)")
|
||||
if fail_count >= max_fail:
|
||||
logger.error(f"连续 {max_fail} 次获取数据失败,可能已达到 API 历史数据限制,停止抓取")
|
||||
break
|
||||
|
||||
current_start = current_end
|
||||
current_end = current_start
|
||||
time.sleep(1) # 避免请求过快
|
||||
|
||||
logger.success(f"数据抓取完成,共保存 {total_saved} 条K线数据")
|
||||
@@ -126,7 +145,7 @@ class BitMartDataCollector:
|
||||
"""
|
||||
实时抓取最新数据(用于定时任务)
|
||||
"""
|
||||
logger.info("开始抓取 BitMart 最新30分钟K线数据")
|
||||
logger.info("开始抓取 BitMart 最新15分钟K线数据")
|
||||
|
||||
# 获取最近1小时的数据(确保能获取到最新的K线)
|
||||
end_time = int(time.time())
|
||||
@@ -143,8 +162,8 @@ class BitMartDataCollector:
|
||||
if __name__ == '__main__':
|
||||
collector = BitMartDataCollector()
|
||||
|
||||
# 抓取最近30天的历史数据
|
||||
collector.collect_historical_data(days=500)
|
||||
# 抓取从 2025-01-01 到现在的15分钟K线历史数据
|
||||
collector.collect_historical_data(start_date='2025-01-01')
|
||||
|
||||
# 如果需要实时抓取,可以取消下面的注释
|
||||
# collector.collect_realtime_data()
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user