@weaxs/openclaw-stock-analysis 0.1.10 → 0.1.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1248,7 +1248,11 @@ def cmd_market_indices(args):
1248
1248
  ("sh000016", "上证50"),
1249
1249
  ]
1250
1250
  results = []
1251
- df = _akshare_retry(ak.stock_zh_index_spot_em)
1251
+ try:
1252
+ df = _akshare_retry(ak.stock_zh_index_spot_em)
1253
+ except Exception:
1254
+ # eastmoney refuses datacenter/proxy IPs (RemoteDisconnected) — sina is the fallback
1255
+ df = _akshare_retry(ak.stock_zh_index_spot_sina)
1252
1256
  for code, name in indices:
1253
1257
  clean_code = code[2:] if code[:2] in ("sh", "sz") else code
1254
1258
  row = df[df["代码"] == clean_code]
@@ -1497,76 +1501,80 @@ def cmd_chip_distribution(args):
1497
1501
  # --------------- market_stats ---------------
1498
1502
 
1499
1503
 
1504
+ def compute_market_stats(data: list) -> dict:
1505
+ """Compute breadth/limit/turnover statistics from a market snapshot list."""
1506
+ import numpy as np
1507
+
1508
+ changes = []
1509
+ turnovers = []
1510
+ limit_ups = []
1511
+ limit_downs = []
1512
+ up_count = down_count = flat_count = 0
1513
+
1514
+ for s in data:
1515
+ chg = s.get("change_pct")
1516
+ if chg is not None:
1517
+ changes.append(float(chg))
1518
+ if chg > 0:
1519
+ up_count += 1
1520
+ elif chg < 0:
1521
+ down_count += 1
1522
+ else:
1523
+ flat_count += 1
1524
+ t = s.get("turnover")
1525
+ if t is not None:
1526
+ turnovers.append(float(t))
1527
+
1528
+ price = s.get("price")
1529
+ prev = s.get("prev_close")
1530
+ sym = s.get("symbol", "")
1531
+ if price and prev and prev > 0:
1532
+ code_info = normalize_stock_code(sym)
1533
+ lp = code_info.get("limit_pct")
1534
+ if lp:
1535
+ limit_up = calc_limit_price(float(prev), lp, "up")
1536
+ limit_down = calc_limit_price(float(prev), lp, "down")
1537
+ if abs(float(price) - limit_up) < 0.01:
1538
+ limit_ups.append({"symbol": sym, "name": s.get("name"), "change_pct": chg})
1539
+ elif abs(float(price) - limit_down) < 0.01:
1540
+ limit_downs.append({"symbol": sym, "name": s.get("name"), "change_pct": chg})
1541
+
1542
+ changes_arr = np.array(changes) if changes else np.array([0])
1543
+ sorted_data = sorted(data, key=lambda x: x.get("change_pct") or 0, reverse=True)
1544
+
1545
+ return _clean_row(
1546
+ {
1547
+ "total_stocks": len(data),
1548
+ "up_count": up_count,
1549
+ "down_count": down_count,
1550
+ "flat_count": flat_count,
1551
+ "limit_up_count": len(limit_ups),
1552
+ "limit_down_count": len(limit_downs),
1553
+ "avg_change_pct": round(float(changes_arr.mean()), 2),
1554
+ "median_change_pct": round(float(np.median(changes_arr)), 2),
1555
+ "total_turnover": round(sum(turnovers), 0) if turnovers else None,
1556
+ "top5_gainers": [
1557
+ {"symbol": s.get("symbol"), "name": s.get("name"), "change_pct": s.get("change_pct")}
1558
+ for s in sorted_data[:5]
1559
+ ],
1560
+ "top5_losers": [
1561
+ {"symbol": s.get("symbol"), "name": s.get("name"), "change_pct": s.get("change_pct")}
1562
+ for s in sorted_data[-5:]
1563
+ ],
1564
+ "limit_up_samples": limit_ups[:5],
1565
+ "limit_down_samples": limit_downs[:5],
1566
+ }
1567
+ )
1568
+
1569
+
1500
1570
  def cmd_market_stats(args):
1501
1571
  try:
1502
- import numpy as np
1503
-
1504
1572
  data = snapshot_a()
1505
1573
  if isinstance(data, dict) and "error" in data:
1506
1574
  return data
1507
1575
  if not data:
1508
1576
  return {"error": "No market data"}
1509
-
1510
- changes = []
1511
- turnovers = []
1512
- limit_ups = []
1513
- limit_downs = []
1514
- up_count = down_count = flat_count = 0
1515
-
1516
- for s in data:
1517
- chg = s.get("change_pct")
1518
- if chg is not None:
1519
- changes.append(float(chg))
1520
- if chg > 0:
1521
- up_count += 1
1522
- elif chg < 0:
1523
- down_count += 1
1524
- else:
1525
- flat_count += 1
1526
- t = s.get("turnover")
1527
- if t is not None:
1528
- turnovers.append(float(t))
1529
-
1530
- price = s.get("price")
1531
- prev = s.get("prev_close")
1532
- sym = s.get("symbol", "")
1533
- if price and prev and prev > 0:
1534
- code_info = normalize_stock_code(sym)
1535
- lp = code_info.get("limit_pct")
1536
- if lp:
1537
- limit_up = calc_limit_price(float(prev), lp, "up")
1538
- limit_down = calc_limit_price(float(prev), lp, "down")
1539
- if abs(float(price) - limit_up) < 0.01:
1540
- limit_ups.append({"symbol": sym, "name": s.get("name"), "change_pct": chg})
1541
- elif abs(float(price) - limit_down) < 0.01:
1542
- limit_downs.append({"symbol": sym, "name": s.get("name"), "change_pct": chg})
1543
-
1544
- changes_arr = np.array(changes) if changes else np.array([0])
1545
- sorted_data = sorted(data, key=lambda x: x.get("change_pct") or 0, reverse=True)
1546
-
1547
- return _clean_row(
1548
- {
1549
- "total_stocks": len(data),
1550
- "up_count": up_count,
1551
- "down_count": down_count,
1552
- "flat_count": flat_count,
1553
- "limit_up_count": len(limit_ups),
1554
- "limit_down_count": len(limit_downs),
1555
- "avg_change_pct": round(float(changes_arr.mean()), 2),
1556
- "median_change_pct": round(float(np.median(changes_arr)), 2),
1557
- "total_turnover": round(sum(turnovers), 0) if turnovers else None,
1558
- "top5_gainers": [
1559
- {"symbol": s.get("symbol"), "name": s.get("name"), "change_pct": s.get("change_pct")}
1560
- for s in sorted_data[:5]
1561
- ],
1562
- "top5_losers": [
1563
- {"symbol": s.get("symbol"), "name": s.get("name"), "change_pct": s.get("change_pct")}
1564
- for s in sorted_data[-5:]
1565
- ],
1566
- "limit_up_samples": limit_ups[:5],
1567
- "limit_down_samples": limit_downs[:5],
1568
- }
1569
- )
1577
+ return compute_market_stats(data)
1570
1578
  except Exception as e:
1571
1579
  return {"error": str(e)}
1572
1580
 
@@ -1708,7 +1716,10 @@ def main():
1708
1716
  "market_stats": cmd_market_stats,
1709
1717
  "fundamental_context": cmd_fundamental_context,
1710
1718
  }
1711
- result = dispatch[args.command](args)
1719
+ # Provider libraries (baostock etc.) print prose to stdout on failure,
1720
+ # which would corrupt the JSON-only contract — divert that noise to stderr.
1721
+ with contextlib.redirect_stdout(sys.stderr):
1722
+ result = dispatch[args.command](args)
1712
1723
  output(result)
1713
1724
 
1714
1725