|
|
|
from fastapi import FastAPI, HTTPException, Depends
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from typing import Dict, List
|
|
from services.data_fetcher import IndianMarketDataFetcher
|
|
from services.analyzer import IndianFinancialAnalyzer
|
|
from services.swarm_service import create_indian_market_swarm
|
|
from models.market_data import StockDataResponse, IndexData, SectorPerformance, EconomicIndicators
|
|
from models.analysis import StockAnalysisResponse, SwarmAnalysisResponse, InvestmentRecommendationResponse
|
|
from config import NIFTY50_COMPANIES
|
|
|
|
app = FastAPI(title="Indian Market Analysis API", version="1.0.0")
|
|
|
|
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=["*"],
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
|
|
def get_data_fetcher():
|
|
return IndianMarketDataFetcher()
|
|
|
|
def get_analyzer():
|
|
return IndianFinancialAnalyzer()
|
|
|
|
|
|
|
|
@app.get("/")
|
|
async def root():
|
|
return {"message": "Indian Market Analysis API"}
|
|
|
|
|
|
|
|
@app.get("/api/market/indices", response_model=Dict[str, IndexData])
|
|
async def get_market_indices(fetcher: IndianMarketDataFetcher = Depends(get_data_fetcher)):
|
|
"""Get live market indices data."""
|
|
return fetcher.get_market_indices()
|
|
|
|
@app.get("/api/market/sectors", response_model=Dict[str, SectorPerformance])
|
|
async def get_sector_performance(fetcher: IndianMarketDataFetcher = Depends(get_data_fetcher)):
|
|
"""Get sector performance data."""
|
|
return fetcher.get_sector_performance()
|
|
|
|
@app.get("/api/market/economic", response_model=EconomicIndicators)
|
|
async def get_economic_indicators(analyzer: IndianFinancialAnalyzer = Depends(get_analyzer)):
|
|
"""Get key economic indicators."""
|
|
return EconomicIndicators(
|
|
repo_rate=analyzer.rbi_repo_rate,
|
|
inflation_rate=analyzer.indian_inflation_rate
|
|
)
|
|
|
|
|
|
|
|
@app.get("/api/stocks/list", response_model=Dict[str, str])
|
|
async def get_nifty50_list(fetcher: IndianMarketDataFetcher = Depends(get_data_fetcher)):
|
|
"""Get the list of Nifty 50 companies."""
|
|
return fetcher.get_nifty50_companies()
|
|
|
|
@app.get("/api/stocks/{symbol}", response_model=StockDataResponse)
|
|
async def get_stock_data(symbol: str, period: str = "1y", fetcher: IndianMarketDataFetcher = Depends(get_data_fetcher)):
|
|
"""Get detailed stock data for a given symbol."""
|
|
stock_data = fetcher.get_stock_data(symbol, period)
|
|
if not stock_data:
|
|
raise HTTPException(status_code=404, detail=f"Data for stock symbol {symbol} could not be fetched.")
|
|
return stock_data
|
|
|
|
|
|
|
|
@app.post("/api/analysis/stock/{symbol}", response_model=StockAnalysisResponse)
|
|
async def analyze_stock(symbol: str, period: str = "1y",
|
|
fetcher: IndianMarketDataFetcher = Depends(get_data_fetcher),
|
|
analyzer: IndianFinancialAnalyzer = Depends(get_analyzer)):
|
|
"""Perform basic financial analysis on a stock."""
|
|
stock_data = fetcher.get_stock_data(symbol, period)
|
|
if not stock_data:
|
|
raise HTTPException(status_code=404, detail=f"Data for stock symbol {symbol} could not be fetched.")
|
|
|
|
company_name = NIFTY50_COMPANIES.get(symbol, symbol)
|
|
return analyzer.analyze_indian_stock(stock_data, company_name)
|
|
|
|
@app.post("/api/analysis/swarm/{symbol}", response_model=SwarmAnalysisResponse)
|
|
async def run_swarm_analysis(symbol: str, period: str = "1y",
|
|
fetcher: IndianMarketDataFetcher = Depends(get_data_fetcher),
|
|
analyzer: IndianFinancialAnalyzer = Depends(get_analyzer)):
|
|
"""Run AI swarm analysis on a stock."""
|
|
stock_data = fetcher.get_stock_data(symbol, period)
|
|
if not stock_data:
|
|
return SwarmAnalysisResponse(status="error", error=f"Data for stock symbol {symbol} could not be fetched.")
|
|
|
|
company_name = NIFTY50_COMPANIES.get(symbol, symbol)
|
|
|
|
|
|
basic_analysis_response = analyzer.analyze_indian_stock(stock_data, company_name)
|
|
basic_analysis_text = basic_analysis_response.basic_analysis
|
|
|
|
|
|
market_data_summary = f"""
|
|
Stock: {company_name} ({symbol})
|
|
Current Price: ₹{stock_data.history.get('Close', [0])[-1] if stock_data.history.get('Close') else 'N/A'}
|
|
Market Data Analysis: {basic_analysis_text}
|
|
"""
|
|
|
|
|
|
return create_indian_market_swarm(market_data_summary, company_name)
|
|
|
|
@app.get("/api/analysis/recommendation", response_model=InvestmentRecommendationResponse)
|
|
async def get_investment_recommendation(analyzer: IndianFinancialAnalyzer = Depends(get_analyzer)):
|
|
"""Get general investment recommendations."""
|
|
return analyzer.generate_investment_recommendation()
|
|
|
|
|
|
|
|
@app.get("/api/health")
|
|
async def health_check():
|
|
"""Health check endpoint."""
|
|
return {"status": "ok"}
|
|
|