Datasets:
File size: 9,202 Bytes
122eb34 0d94041 57fdd71 0d94041 57fdd71 0d94041 57fdd71 0d94041 122eb34 0d94041 57fdd71 0d94041 57fdd71 0d94041 57fdd71 0d94041 57fdd71 0d94041 122eb34 57fdd71 b0118b9 122eb34 b0118b9 57fdd71 0d94041 122eb34 57fdd71 122eb34 57fdd71 b0118b9 57fdd71 0d94041 122eb34 57fdd71 0d94041 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
```python
import krakenex
import pandas as pd
from datetime import datetime
import time
import os
from typing import Dict, List, Optional
import logging
from huggingface_hub import HfApi
# Set up logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler(f'kraken_collection_{datetime.now().strftime("%Y%m%d")}.log'),
logging.StreamHandler()
]
)
logger = logging.getLogger(__name__)
class KrakenHuggingFaceCollector:
def __init__(self, kraken_key_path: str, repo_id: str):
self.kraken_api = krakenex.API()
try:
self.kraken_api.load_key(kraken_key_path)
logger.info("Successfully loaded Kraken API key")
except Exception as e:
logger.error(f"Failed to load Kraken API key: {e}")
raise
try:
self.hf_api = HfApi()
self.repo_id = repo_id
logger.info("Successfully connected to Hugging Face")
except Exception as e:
logger.error(f"Failed to initialize Hugging Face API: {e}")
raise
self.pairs = [
"XXBTZUSD", # Bitcoin/USD
"XETHZUSD", # Ethereum/USD
"XXRPZUSD", # Ripple/USD
"ADAUSD", # Cardano/USD
"XDGUSD", # Dogecoin/USD
"SOLUSD", # Solana/USD
"DOTUSD", # Polkadot/USD
"MATICUSD", # Polygon/USD
"LTCUSD" # Litecoin/USD
]
self.running = True
self.data_points_collected = 0
self.collection_start_time = None
self.api_calls = 0
self.last_api_reset = datetime.now()
def check_api_rate(self) -> bool:
"""Monitor API call rate"""
current_time = datetime.now()
if (current_time - self.last_api_reset).total_seconds() >= 30:
self.api_calls = 0
self.last_api_reset = current_time
return self.api_calls < 15
def fetch_ticker_data(self, pair: str) -> Optional[Dict]:
"""Fetch ticker data with rate limiting"""
if not self.check_api_rate():
logger.warning("API rate limit approaching, waiting...")
time.sleep(2)
try:
self.api_calls += 1
response = self.kraken_api.query_public('Ticker', {'pair': pair})
if 'error' in response and response['error']:
logger.error(f"Kraken API error for {pair}: {response['error']}")
return None
data = response['result']
pair_data = list(data.values())[0]
return {
'timestamp': datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f'),
'pair': pair,
'price': float(pair_data['c'][0]),
'volume': float(pair_data['v'][0]),
'bid': float(pair_data['b'][0]),
'ask': float(pair_data['a'][0]),
'low': float(pair_data['l'][0]),
'high': float(pair_data['h'][0]),
'vwap': float(pair_data['p'][0]),
'trades': int(pair_data['t'][0])
}
except Exception as e:
logger.error(f"Error fetching data for {pair}: {e}")
return None
def upload_to_huggingface(self, df: pd.DataFrame, timestamp: str) -> None:
"""Upload DataFrame to Hugging Face as CSV"""
try:
# Create local directories
os.makedirs('data/continuous', exist_ok=True)
# Save locally first
local_path = f'data/continuous/kraken_trades_{timestamp}.csv'
df.to_csv(local_path, index=False)
# Upload to Hugging Face
self.hf_api.upload_file(
path_or_fileobj=local_path,
path_in_repo=f"data/continuous/kraken_trades_{timestamp}.csv",
repo_id=self.repo_id,
repo_type="dataset"
)
logger.info(f"Successfully uploaded batch to Hugging Face")
except Exception as e:
logger.error(f"Error uploading to Hugging Face: {e}")
logger.info(f"Data saved locally at: {local_path}")
def collect_continuous(self, interval_minutes: int = 3, batch_size: int = 30):
"""
Enhanced continuous data collection
Args:
interval_minutes: Minutes between each collection (default: 3)
batch_size: Number of snapshots per batch (default: 30)
"""
self.collection_start_time = datetime.now()
logger.info(f"Starting enhanced continuous collection at {self.collection_start_time}")
logger.info(f"Collecting {batch_size} snapshots every {interval_minutes} minutes")
logger.info(f"Total API calls per batch: ~{batch_size * len(self.pairs)}")
logger.info(f"Estimated daily data points: {(24 * 60 // interval_minutes) * batch_size * len(self.pairs)}")
logger.info("Press CTRL+C to stop collection")
while self.running:
try:
batch_start_time = datetime.now()
records = []
for i in range(batch_size):
if not self.running:
break
snapshot_start = datetime.now()
logger.info(f"Collecting snapshot {i+1}/{batch_size}")
for pair in self.pairs:
if self.check_api_rate():
record = self.fetch_ticker_data(pair)
if record:
records.append(record)
else:
time.sleep(1)
# Dynamic sleep calculation
elapsed = (datetime.now() - snapshot_start).total_seconds()
sleep_time = max(0.5, 1.5 - elapsed)
if i < batch_size - 1 and self.running:
time.sleep(sleep_time)
if records:
df = pd.DataFrame(records)
current_timestamp = datetime.now().strftime('%Y%m%d_%H%M')
self.upload_to_huggingface(df, current_timestamp)
self.data_points_collected += len(records)
collection_duration = (datetime.now() - self.collection_start_time)
logger.info("\nBatch Summary:")
logger.info(f"Records in batch: {len(records)}")
logger.info(f"Pairs collected: {len(df['pair'].unique())}")
logger.info(f"Total data points: {self.data_points_collected}")
logger.info(f"Collection duration: {collection_duration}")
logger.info(f"Data points per hour: {self.data_points_collected / collection_duration.total_seconds() * 3600:.2f}")
# Adaptive interval timing
batch_duration = (datetime.now() - batch_start_time).total_seconds()
sleep_time = max(0, interval_minutes * 60 - batch_duration)
if self.running and sleep_time > 0:
logger.info(f"Waiting {sleep_time:.2f} seconds until next batch...")
time.sleep(sleep_time)
except Exception as e:
logger.error(f"Error in continuous collection: {e}")
logger.info("Waiting 30 seconds before retry...")
time.sleep(30)
logger.info("Data collection stopped")
logger.info(f"Total data points collected: {self.data_points_collected}")
logger.info(f"Total collection time: {datetime.now() - self.collection_start_time}")
def main():
try:
collector = KrakenHuggingFaceCollector(
kraken_key_path="kraken.key",
repo_id="GotThatData/kraken-trading-data"
)
# Start collection with enhanced parameters
collector.collect_continuous(
interval_minutes=3, # Collect every 3 minutes
batch_size=30 # 30 snapshots per batch
)
except KeyboardInterrupt:
logger.info("Stopping collection (CTRL+C pressed)")
collector.running = False
except Exception as e:
logger.error(f"Fatal error: {e}")
raise
if __name__ == "__main__":
main()
```
To use this script:
1. Save it as `kraken_data_collector.py`
2. Make sure you have your `kraken.key` file with your API credentials
3. Install required packages if you haven't:
```bash
pip install krakenex pandas huggingface_hub
```
4. Run the script:
```bash
python kraken_data_collector.py
```
This will:
- Collect 30 snapshots every 3 minutes
- Save data locally and to Hugging Face
- Provide detailed logging
- Handle errors gracefully
- Respect API rate limits |