Spaces:
Paused
Paused
Update api.py
Browse files
api.py
CHANGED
|
@@ -631,6 +631,7 @@ INSTRUMENT_CATALOG = [
|
|
| 631 |
|
| 632 |
# Symbol mappings from Frontend symbols to Yahoo Finance symbols
|
| 633 |
SYMBOL_MAP = {
|
|
|
|
| 634 |
"NIFTY50": "^NSEI",
|
| 635 |
"SENSEX": "^BSESN",
|
| 636 |
"RELIANCE": "RELIANCE.NS",
|
|
@@ -680,15 +681,16 @@ def fetch_live_data(symbol: str, exchange: str = "", interval: str = "5min",
|
|
| 680 |
}
|
| 681 |
yq_interval = intervals_map.get(interval, "5m")
|
| 682 |
|
| 683 |
-
# Select history period based on interval
|
| 684 |
if yq_interval == "1m":
|
| 685 |
period = "5d"
|
| 686 |
elif yq_interval in ["5m", "15m"]:
|
| 687 |
period = "1mo"
|
| 688 |
elif yq_interval == "1h":
|
| 689 |
-
|
|
|
|
| 690 |
else:
|
| 691 |
-
period = "
|
| 692 |
|
| 693 |
t = Ticker(yf_symbol)
|
| 694 |
df = t.history(period=period, interval=yq_interval)
|
|
@@ -765,6 +767,65 @@ def list_instruments():
|
|
| 765 |
}
|
| 766 |
|
| 767 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 768 |
@app.get("/price/{symbol:path}")
|
| 769 |
def get_price(symbol: str, exchange: str = Query(default="")):
|
| 770 |
"""Quick current-price lookup for a symbol."""
|
|
|
|
| 631 |
|
| 632 |
# Symbol mappings from Frontend symbols to Yahoo Finance symbols
|
| 633 |
SYMBOL_MAP = {
|
| 634 |
+
"NIFTY 50": "^NSEI",
|
| 635 |
"NIFTY50": "^NSEI",
|
| 636 |
"SENSEX": "^BSESN",
|
| 637 |
"RELIANCE": "RELIANCE.NS",
|
|
|
|
| 681 |
}
|
| 682 |
yq_interval = intervals_map.get(interval, "5m")
|
| 683 |
|
| 684 |
+
# Select history period based on interval to ensure we get 250+ valid bars
|
| 685 |
if yq_interval == "1m":
|
| 686 |
period = "5d"
|
| 687 |
elif yq_interval in ["5m", "15m"]:
|
| 688 |
period = "1mo"
|
| 689 |
elif yq_interval == "1h":
|
| 690 |
+
# For 4h we resample from 1h, so we need 4x the data length (approx 1 year of 1h data)
|
| 691 |
+
period = "1y" if interval == "4h" else "3mo"
|
| 692 |
else:
|
| 693 |
+
period = "2y" # 1day interval
|
| 694 |
|
| 695 |
t = Ticker(yf_symbol)
|
| 696 |
df = t.history(period=period, interval=yq_interval)
|
|
|
|
| 767 |
}
|
| 768 |
|
| 769 |
|
| 770 |
+
@app.get("/search")
|
| 771 |
+
def search_tickers(q: str = Query(...)):
|
| 772 |
+
"""Search Yahoo Finance for symbols/tickers matching a query."""
|
| 773 |
+
try:
|
| 774 |
+
from yahooquery import search
|
| 775 |
+
res = search(q)
|
| 776 |
+
quotes = res.get("quotes", [])
|
| 777 |
+
results = []
|
| 778 |
+
for quote in quotes:
|
| 779 |
+
symbol = quote.get("symbol")
|
| 780 |
+
if not symbol:
|
| 781 |
+
continue
|
| 782 |
+
name = quote.get("longname") or quote.get("shortname") or symbol
|
| 783 |
+
exchange = quote.get("exchDisp") or quote.get("exchange") or ""
|
| 784 |
+
quote_type = quote.get("typeDisp") or quote.get("quoteType") or "Equity"
|
| 785 |
+
|
| 786 |
+
# Map type to category
|
| 787 |
+
category = "Equity"
|
| 788 |
+
if quote_type.lower() in ["cryptocurrency", "crypto"]:
|
| 789 |
+
category = "Crypto"
|
| 790 |
+
elif quote_type.lower() in ["currency", "forex"]:
|
| 791 |
+
category = "Forex"
|
| 792 |
+
elif quote_type.lower() in ["commodity", "future"]:
|
| 793 |
+
category = "Commodities"
|
| 794 |
+
elif quote_type.lower() in ["index"]:
|
| 795 |
+
category = "Indices"
|
| 796 |
+
elif quote_type.lower() in ["etf"]:
|
| 797 |
+
category = "ETF"
|
| 798 |
+
else:
|
| 799 |
+
category = "Equity"
|
| 800 |
+
|
| 801 |
+
# Determine visual flag
|
| 802 |
+
flag = "๐"
|
| 803 |
+
if category == "Crypto":
|
| 804 |
+
flag = "โฟ"
|
| 805 |
+
elif category == "Forex":
|
| 806 |
+
flag = "๐ฑ"
|
| 807 |
+
elif category == "Commodities":
|
| 808 |
+
flag = "๐ฅ"
|
| 809 |
+
elif category == "Indices":
|
| 810 |
+
flag = "๐"
|
| 811 |
+
elif exchange == "NSE" or exchange == "BSE":
|
| 812 |
+
flag = "๐ฎ๐ณ"
|
| 813 |
+
elif exchange in ["NASDAQ", "NYSE", "AMEX"]:
|
| 814 |
+
flag = "๐บ๐ธ"
|
| 815 |
+
|
| 816 |
+
results.append({
|
| 817 |
+
"symbol": symbol,
|
| 818 |
+
"exchange": exchange,
|
| 819 |
+
"name": name,
|
| 820 |
+
"category": category,
|
| 821 |
+
"currency": quote.get("currency", "USD"),
|
| 822 |
+
"flag": flag
|
| 823 |
+
})
|
| 824 |
+
return {"results": results}
|
| 825 |
+
except Exception as e:
|
| 826 |
+
return {"error": str(e), "results": []}
|
| 827 |
+
|
| 828 |
+
|
| 829 |
@app.get("/price/{symbol:path}")
|
| 830 |
def get_price(symbol: str, exchange: str = Query(default="")):
|
| 831 |
"""Quick current-price lookup for a symbol."""
|