implementations
This commit is contained in:
@ -7,7 +7,7 @@ from collections import deque
|
||||
|
||||
import ccxt.async_support as ccxt
|
||||
from dotenv import load_dotenv
|
||||
|
||||
import platform
|
||||
|
||||
class LiveDataManager:
|
||||
def __init__(self, symbol, exchange_name='mexc', window_size=120):
|
||||
@ -20,6 +20,7 @@ class LiveDataManager:
|
||||
self.last_candle_time = None
|
||||
self.exchange = self._initialize_exchange()
|
||||
self.lock = asyncio.Lock() # Lock to prevent race conditions
|
||||
self.is_windows = platform.system() == 'Windows'
|
||||
|
||||
def _initialize_exchange(self):
|
||||
exchange_class = getattr(ccxt, self.exchange_name)
|
||||
@ -41,15 +42,23 @@ class LiveDataManager:
|
||||
print(f"Fetching initial candles for {self.symbol}...")
|
||||
now = int(time.time() * 1000)
|
||||
since = now - self.window_size * 60 * 1000
|
||||
try:
|
||||
candles = await self.exchange.fetch_ohlcv(self.symbol, '1m', since=since, limit=self.window_size)
|
||||
for candle in candles:
|
||||
self.candles.append(self._format_candle(candle))
|
||||
if candles:
|
||||
self.last_candle_time = candles[-1][0]
|
||||
print(f"Fetched {len(candles)} initial candles.")
|
||||
except Exception as e:
|
||||
print(f"Error fetching initial candles: {e}")
|
||||
retries = 3
|
||||
for attempt in range(retries):
|
||||
try:
|
||||
candles = await self.exchange.fetch_ohlcv(self.symbol, '1m', since=since, limit=self.window_size)
|
||||
for candle in candles:
|
||||
self.candles.append(self._format_candle(candle))
|
||||
if candles:
|
||||
self.last_candle_time = candles[-1][0]
|
||||
print(f"Fetched {len(candles)} initial candles.")
|
||||
return # Exit the function if successful
|
||||
except Exception as e:
|
||||
print(f"Attempt {attempt + 1} failed: {e}")
|
||||
if self.is_windows and "aiodns needs a SelectorEventLoop" in str(e):
|
||||
print("aiodns issue detected on Windows. This is a known problem with aiodns and ccxt on Windows.")
|
||||
if attempt < retries - 1:
|
||||
await asyncio.sleep(5) # Wait before retrying
|
||||
print("Failed to fetch initial candles after multiple retries.")
|
||||
|
||||
def _format_candle(self, candle_data):
|
||||
return {
|
||||
@ -112,16 +121,23 @@ class LiveDataManager:
|
||||
async def fetch_and_process_ticks(self):
|
||||
async with self.lock:
|
||||
since = None if not self.ticks else self.ticks[-1]['timestamp']
|
||||
try:
|
||||
# Use fetch_trades (or appropriate method for your exchange) for live ticks.
|
||||
ticks = await self.exchange.fetch_trades(self.symbol, since=since)
|
||||
for tick in ticks:
|
||||
formatted_tick = self._format_tick(tick)
|
||||
if formatted_tick: # Add the check here
|
||||
self.ticks.append(formatted_tick)
|
||||
await self._update_candle(formatted_tick)
|
||||
except Exception as e:
|
||||
print(f"Error fetching ticks: {e}")
|
||||
retries = 3
|
||||
for attempt in range(retries):
|
||||
try:
|
||||
# Use fetch_trades (or appropriate method for your exchange) for live ticks.
|
||||
ticks = await self.exchange.fetch_trades(self.symbol, since=since)
|
||||
for tick in ticks:
|
||||
formatted_tick = self._format_tick(tick)
|
||||
if formatted_tick: # Add the check here
|
||||
self.ticks.append(formatted_tick)
|
||||
await self._update_candle(formatted_tick)
|
||||
break # Exit the retry loop if successful
|
||||
except Exception as e:
|
||||
print(f"Error fetching ticks (attempt {attempt + 1}): {e}")
|
||||
if self.is_windows and "aiodns needs a SelectorEventLoop" in str(e):
|
||||
print("aiodns issue detected on Windows. This is a known problem with aiodns and ccxt on Windows.")
|
||||
if attempt < retries - 1:
|
||||
await asyncio.sleep(5) # Wait before retrying
|
||||
|
||||
async def get_data(self):
|
||||
async with self.lock:
|
||||
|
Reference in New Issue
Block a user