Files
workspace/projects/market-watch/sector_fix.py

53 lines
1.5 KiB
Python

#!/usr/bin/env python3
"""Fix and update sector information for all portfolio positions"""
import sys
sys.path.append('/home/wdjones/.openclaw/workspace/projects/market-watch')
import game_engine
import yfinance as yf
import json
def update_portfolio_sectors(game_id, username):
"""Update sector information for all positions in a portfolio"""
pf_path = game_engine._portfolio_path(game_id, username)
pf = game_engine._load_json(pf_path)
if not pf or not pf["positions"]:
print("No positions to update")
return
print("Updating sector information for all positions...")
updated = 0
for ticker, pos in pf["positions"].items():
try:
print(f"Updating {ticker}...")
stock = yf.Ticker(ticker)
info = stock.info
sector = info.get("sector", "Unknown")
if sector and sector != "Unknown":
pos["sector"] = sector
updated += 1
print(f" {ticker}: {sector}")
else:
print(f" {ticker}: No sector found")
except Exception as e:
print(f" {ticker}: Error - {e}")
# Save updated portfolio
game_engine._save_json(pf_path, pf)
print(f"\nUpdated sectors for {updated} positions")
def main():
gid = game_engine.get_default_game_id()
if not gid:
print("No game found")
return
update_portfolio_sectors(gid, "case")
if __name__ == "__main__":
main()