Spaces:
Sleeping
Sleeping
| from fastapi.responses import HTMLResponse | |
| from fastapi.templating import Jinja2Templates | |
| from fastapi import FastAPI, Request, HTTPException, Query, Body | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from utils import StockDataFetcher | |
| from Model import * | |
| import json | |
| fetcher = StockDataFetcher() | |
| app = FastAPI(swagger_ui_parameters={"syntaxHighlight.theme": "obsidian"}) | |
| origins = ["*"] | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=origins, | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| templates = Jinja2Templates(directory="templates") | |
| # HTML Page | |
| async def read_root(request: Request): | |
| return templates.TemplateResponse("hello.html", {"request": request}) | |
| # Latest Price Fetch | |
| async def get_data(ticker: str = Query(..., description="Enter your stock name.", example='zomato')): | |
| try: | |
| response = fetcher.fetch_latest_price(ticker) | |
| if response: | |
| return {'ltp' :response} | |
| else: | |
| raise HTTPException(status_code=404, detail="ltp price not found") | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| # Search Stock Ticker | |
| async def stock_search(ticker: str = Query(..., description="Enter your stock name.", example='zomato')): | |
| try: | |
| fetcher = StockDataFetcher() | |
| response = fetcher.search_entity(ticker) | |
| if response: | |
| return response | |
| else: | |
| raise HTTPException(status_code=404, detail="Stock not found") | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| # Fetch Historical Data | |
| async def get_stocks_data(ticker: str = Query(..., description="Enter your stock name.", example='zomato'), intervals: int = Query(..., description="Select your interval 1m, 5m, 15m", example=5), days: int = Query(..., description="Enter you numbers of day", example=10)): | |
| try: | |
| response = fetcher.fetch_stock_data(ticker, intervals, days).to_dict(orient="records") | |
| if response: | |
| return {"data" : response} | |
| else: | |
| raise HTTPException(status_code=404, detail="historical data not found.") | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| # Fetch Stock Chain | |
| async def get_chain(ticker: str = Query(..., description="Enter your index name from [nifty, banknifty]", example='banknifty')): | |
| try: | |
| response, exp, index_ltp = fetcher.fetch_option_chain(ticker) | |
| response = response.to_dict(orient="records") | |
| if response: | |
| return {"ltp": index_ltp, "expiry": exp, "data" : response} | |
| else: | |
| raise HTTPException(status_code=404, detail="Unable to fetch chain.") | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| # Stocks News | |
| async def get_stocks_data(ticker: str = Query(..., description="Enter your stock name.", example='zomato'), page: int = Query(..., description="Enter number of pages.", example=3), size: int = Query(..., description="Enter number of news you want to extract", example=4)): | |
| try: | |
| response = fetcher.fetch_stock_news(ticker, page=page, size=size).to_dict(orient="records") | |
| if response: | |
| return {"data" : response} | |
| else: | |
| raise HTTPException(status_code=404, detail="Unable to fetch news data.") | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| # Get signal data | |
| async def get_stock_signal(index: str = Query(..., description="Enter your index name from [nifty, banknifty]", example='nifty')): | |
| try: | |
| response = fetcher.realtime_signal(index) | |
| if response: | |
| return {"data" : response} | |
| else: | |
| raise HTTPException(status_code=404, detail="Unable to fetch signals") | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| # Fetch all stocks info | |
| async def get_stocks_data(): | |
| try: | |
| response = fetcher.fetch_all_stock().to_json(orient="records") | |
| if response: | |
| return {"data" : response} | |
| else: | |
| raise HTTPException(status_code=404, detail="Unable to fetch all stocks") | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) |