Really-amin commited on
Commit
e55b2da
·
verified ·
1 Parent(s): af92233

fix: indicators comprehensive empty prices and stoch RSI guard

Browse files
Files changed (1) hide show
  1. backend/routers/indicators_api.py +11 -4
backend/routers/indicators_api.py CHANGED
@@ -331,8 +331,12 @@ def calculate_stoch_rsi(prices: List[float], rsi_period: int = 14, stoch_period:
331
  k_values = []
332
  for i in range(3):
333
  idx = -3 + i
334
- r_high = max(rsi_values[idx-stoch_period+1:idx+1]) if idx+1 <= 0 else rsi_high
335
- r_low = min(rsi_values[idx-stoch_period+1:idx+1]) if idx+1 <= 0 else rsi_low
 
 
 
 
336
  curr = rsi_values[idx]
337
  if r_high != r_low:
338
  k_values.append(((curr - r_low) / (r_high - r_low)) * 100)
@@ -1400,6 +1404,8 @@ async def get_comprehensive_analysis(
1400
  ):
1401
  """Get comprehensive analysis with all indicators"""
1402
  try:
 
 
1403
  # Try to import coingecko client
1404
  try:
1405
  from backend.services.coingecko_client import coingecko_client
@@ -1412,12 +1418,13 @@ async def get_comprehensive_analysis(
1412
  ohlcv = None
1413
  if client_available:
1414
  try:
1415
- ohlcv = await coingecko_client.get_ohlcv(symbol, days=365)
1416
  except Exception as fetch_err:
1417
  logger.error(f"Failed to fetch OHLCV data: {fetch_err}")
1418
  ohlcv = None
1419
 
1420
- if not ohlcv or "prices" not in ohlcv:
 
1421
  try:
1422
  from backend.services.binance_client import binance_client
1423
  candles = await binance_client.get_ohlcv(symbol, timeframe=timeframe, limit=200)
 
331
  k_values = []
332
  for i in range(3):
333
  idx = -3 + i
334
+ window = rsi_values[idx - stoch_period + 1:idx + 1] if idx + 1 <= 0 else recent_rsi
335
+ if not window:
336
+ k_values.append(50)
337
+ continue
338
+ r_high = max(window)
339
+ r_low = min(window)
340
  curr = rsi_values[idx]
341
  if r_high != r_low:
342
  k_values.append(((curr - r_low) / (r_high - r_low)) * 100)
 
1404
  ):
1405
  """Get comprehensive analysis with all indicators"""
1406
  try:
1407
+ base_symbol = symbol.upper().replace("USDT", "").replace("USD", "").strip() or symbol.upper()
1408
+
1409
  # Try to import coingecko client
1410
  try:
1411
  from backend.services.coingecko_client import coingecko_client
 
1418
  ohlcv = None
1419
  if client_available:
1420
  try:
1421
+ ohlcv = await coingecko_client.get_ohlcv(base_symbol, days=365)
1422
  except Exception as fetch_err:
1423
  logger.error(f"Failed to fetch OHLCV data: {fetch_err}")
1424
  ohlcv = None
1425
 
1426
+ prices_data = ohlcv.get("prices") if isinstance(ohlcv, dict) else None
1427
+ if not prices_data:
1428
  try:
1429
  from backend.services.binance_client import binance_client
1430
  candles = await binance_client.get_ohlcv(symbol, timeframe=timeframe, limit=200)