89 lines
3.5 KiB
Python
89 lines
3.5 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Example usage of the simplified data provider
|
|
"""
|
|
|
|
import time
|
|
import logging
|
|
from core.data_provider import DataProvider
|
|
|
|
# Set up logging
|
|
logging.basicConfig(level=logging.INFO)
|
|
logger = logging.getLogger(__name__)
|
|
|
|
def main():
|
|
"""Demonstrate the simplified data provider usage"""
|
|
|
|
# Initialize data provider (starts automatic maintenance)
|
|
logger.info("Initializing DataProvider...")
|
|
dp = DataProvider()
|
|
|
|
# Wait for initial data load (happens automatically in background)
|
|
logger.info("Waiting for initial data load...")
|
|
time.sleep(15) # Give it time to load data
|
|
|
|
# Example 1: Get cached historical data (no API calls)
|
|
logger.info("\n=== Example 1: Getting Historical Data ===")
|
|
eth_1m_data = dp.get_historical_data('ETH/USDT', '1m', limit=50)
|
|
if eth_1m_data is not None:
|
|
logger.info(f"ETH/USDT 1m data: {len(eth_1m_data)} candles")
|
|
logger.info(f"Latest candle: {eth_1m_data.iloc[-1]['close']}")
|
|
|
|
# Example 2: Get current prices
|
|
logger.info("\n=== Example 2: Current Prices ===")
|
|
eth_price = dp.get_current_price('ETH/USDT')
|
|
btc_price = dp.get_current_price('BTC/USDT')
|
|
logger.info(f"ETH current price: ${eth_price}")
|
|
logger.info(f"BTC current price: ${btc_price}")
|
|
|
|
# Example 3: Check cache status
|
|
logger.info("\n=== Example 3: Cache Status ===")
|
|
cache_summary = dp.get_cached_data_summary()
|
|
for symbol in cache_summary['cached_data']:
|
|
logger.info(f"\n{symbol}:")
|
|
for timeframe, info in cache_summary['cached_data'][symbol].items():
|
|
if 'candle_count' in info and info['candle_count'] > 0:
|
|
logger.info(f" {timeframe}: {info['candle_count']} candles, latest: ${info['latest_price']}")
|
|
else:
|
|
logger.info(f" {timeframe}: {info.get('status', 'no data')}")
|
|
|
|
# Example 4: Multiple timeframe data
|
|
logger.info("\n=== Example 4: Multiple Timeframes ===")
|
|
for tf in ['1s', '1m', '1h', '1d']:
|
|
data = dp.get_historical_data('ETH/USDT', tf, limit=5)
|
|
if data is not None and not data.empty:
|
|
logger.info(f"ETH {tf}: {len(data)} candles, range: ${data['close'].min():.2f} - ${data['close'].max():.2f}")
|
|
|
|
# Example 5: Health check
|
|
logger.info("\n=== Example 5: Health Check ===")
|
|
health = dp.health_check()
|
|
logger.info(f"Data maintenance active: {health['data_maintenance_active']}")
|
|
logger.info(f"Symbols: {health['symbols']}")
|
|
logger.info(f"Timeframes: {health['timeframes']}")
|
|
|
|
# Example 6: Wait and show automatic updates
|
|
logger.info("\n=== Example 6: Automatic Updates ===")
|
|
logger.info("Waiting 30 seconds to show automatic data updates...")
|
|
|
|
# Get initial timestamp
|
|
initial_data = dp.get_historical_data('ETH/USDT', '1s', limit=1)
|
|
initial_time = initial_data.index[-1] if initial_data is not None else None
|
|
|
|
time.sleep(30)
|
|
|
|
# Check if data was updated
|
|
updated_data = dp.get_historical_data('ETH/USDT', '1s', limit=1)
|
|
updated_time = updated_data.index[-1] if updated_data is not None else None
|
|
|
|
if initial_time and updated_time and updated_time > initial_time:
|
|
logger.info(f"✅ Data automatically updated! New timestamp: {updated_time}")
|
|
else:
|
|
logger.info("⏳ Data update in progress...")
|
|
|
|
# Clean shutdown
|
|
logger.info("\n=== Shutting Down ===")
|
|
dp.stop_automatic_data_maintenance()
|
|
logger.info("DataProvider stopped successfully")
|
|
|
|
if __name__ == "__main__":
|
|
main() |