Разобраться со скриптом на питоне
Но при работе он отдаёт No data returned, No data available.
Код скрипта:
import requests
import pandas as pd
from datetime import datetime, timedelta
import time
# Function to fetch historical data from OKX API
def fetch_candlesticks(inst_id, bar='1D', start_time=None, end_time=None, limit=100):
url = f"https://www.okx.com/api/v5/market/history-candles"
params = {
'instId': inst_id,
'bar': bar,
'limit': limit,
}
if start_time:
params['after'] = start_time # Параметр для получения данных после этой временной метки
if end_time:
params['before'] = end_time # Параметр для получения данных до этой временной метки
print(f"Requesting data for {inst_id} | Bar: {bar} | After: {start_time} | Before: {end_time}")
response = requests.get(url, params=params)
# Check for rate limit errors (Too many requests)
if response.status_code == 429:
print("Rate limit exceeded. Waiting for 2 seconds before retrying...")
time.sleep(2) # Wait before retrying
return fetch_candlesticks(inst_id, bar, start_time, end_time, limit)
if response.status_code == 200:
data = response.json().get("data", [])
print(f"Received {len(data)} rows of data.")
return data
else:
print(f"Error fetching data for {inst_id} with timeframe {bar}: {response.status_code}")
return []
# Function to get all historical data for a specific timeframe and period
def fetch_full_history(inst_id, bar='1D', years=5):
end_time = int(datetime.now().timestamp() * 1000) # Текущий момент в миллисекундах
start_time = int((datetime.now() - timedelta(days=years * 365)).timestamp() * 1000) # 5 лет назад
all_data = []
while end_time > start_time:
data = fetch_candlesticks(inst_id, bar=bar, end_time=end_time)
if not data:
print(f"No data returned for {inst_id}. Stopping further requests.")
break
all_data.extend(data)
end_time = int(data[-1][0]) # Обновление end_time до временной метки последней свечи
print(f"Updated end_time to {end_time} (millis). Continuing to fetch earlier data.")
return all_data
# Function to save data to Excel
def save_to_excel(inst_id, data_dict, file_name='candlesticks_data.xlsx'):
with pd.ExcelWriter(file_name) as writer:
for timeframe, data in data_dict.items():
if not data:
print(f"No data available for {inst_id} with timeframe {timeframe}. Skipping.")
continue # Этот continue должен быть на том же уровне отступа
# Debug: Print the first row to check structure
print(f"Sample data for {inst_id} [{timeframe}]: {data[0]}")
# Fix for 9 columns: drop duplicate Quote Volume
columns = ['Timestamp', 'Open', 'High', 'Low', 'Close', 'Volume', 'Quote Volume', 'Ignored', 'Number of Trades']
try:
df = pd.DataFrame(data, columns=columns)
# Drop the 'Ignored' column
df.drop(columns=['Ignored'], inplace=True)
except Exception as e:
print(f"Error creating DataFrame for {inst_id} [{timeframe}]: {e}")
continue # Этот continue должен быть на том же уровне отступа
# Convert Timestamp to human-readable format
if 'Timestamp' in df.columns:
df['Timestamp'] = pd.to_numeric(df['Timestamp']) # Преобразование в число
df['Timestamp'] = pd.to_datetime(df['Timestamp'], unit='ms')
df['Date'] = df['Timestamp'].dt.date
df['Time'] = df['Timestamp'].dt.strftime('%H:%M:%S')
df = df[['Date', 'Time', 'Open', 'High', 'Low', 'Close', 'Volume', 'Quote Volume', 'Number of Trades']]
# Save data to the respective sheet
try:
df.to_excel(writer, sheet_name=f'{timeframe}', index=False)
except Exception as e:
print(f"Error writing to Excel for {inst_id} [{timeframe}]: {e}")
continue # Этот continue также должен быть на том же уровне отступа
print(f"Data saved to {file_name}")
# List of trading pairs (instrument IDs) with pairs that include USDT
inst_ids = [
'BTC-USDT', 'ETH-USDT', 'SOL-USDT', 'TON-USDT', 'XRP-USDT', # Use USDT pairs
'1INCH-USDT', 'ADA-USDT', 'ATOM-USDT', 'DOT-USDT', 'EGLD-USDT', # Update to available USDT pairs
'NEAR-USDT', 'TRX-USDT', 'ISP-USDT', 'KSM-USDT', 'ALGO-USDT'
]
# List of timeframes (5m, 15m, 1H, 1D)
timeframes = ['5m', '15m', '1H', '1D'] # Таймфреймы, которые вы указали
# Fetch data and save to Excel for each trading pair
for inst_id in inst_ids:
data_dict = {}
for timeframe in timeframes:
print(f"Fetching data for {inst_id} with timeframe {timeframe}...")
data = fetch_full_history(inst_id, bar=timeframe, years=5)
if data: # Проверяем, что данные не пустые
data_dict[timeframe] = data
if not data_dict:
print(f"No data available for {inst_id}. Skipping.")
continue # Этот continue должен быть на том же уровне отступа
file_name = f"{inst_id}_candlesticks.xlsx"
save_to_excel(inst_id, data_dict, file_name=file_name)