Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files
Model.py
ADDED
@@ -0,0 +1,144 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from pydantic import BaseModel, ConfigDict, Field
|
2 |
+
from typing import Any, List
|
3 |
+
|
4 |
+
|
5 |
+
## Search Model
|
6 |
+
class Stock(BaseModel):
|
7 |
+
ID: str
|
8 |
+
title: str
|
9 |
+
NSE_Symbol: str
|
10 |
+
contract_id: str
|
11 |
+
|
12 |
+
search_response_example = {
|
13 |
+
"ID": "zomato-ltd",
|
14 |
+
"title": "Zomato Ltd.",
|
15 |
+
"NSE_Symbol": "ZOMATO",
|
16 |
+
"contract_id": "GSTK543320"
|
17 |
+
}
|
18 |
+
|
19 |
+
|
20 |
+
## LTP Model
|
21 |
+
class LTP(BaseModel):
|
22 |
+
ltp: float
|
23 |
+
|
24 |
+
ltp_response_example = {
|
25 |
+
"ltp" : 209.50
|
26 |
+
}
|
27 |
+
|
28 |
+
|
29 |
+
## Historical Data
|
30 |
+
class HISTORICAL(BaseModel):
|
31 |
+
data: List[dict]
|
32 |
+
|
33 |
+
historical_response_example = {
|
34 |
+
"data": [
|
35 |
+
{
|
36 |
+
"Date": "2024-06-24T09:15:00+05:30",
|
37 |
+
"Open": 193.9,
|
38 |
+
"High": 194.84,
|
39 |
+
"Low": 192.02,
|
40 |
+
"Close": 194.15,
|
41 |
+
"Volume": 2767062
|
42 |
+
},
|
43 |
+
{
|
44 |
+
"Date": "2024-06-24T09:30:00+05:30",
|
45 |
+
"Open": 194.14,
|
46 |
+
"High": 194.2,
|
47 |
+
"Low": 192.8,
|
48 |
+
"Close": 193.97,
|
49 |
+
"Volume": 4066133
|
50 |
+
}
|
51 |
+
]
|
52 |
+
}
|
53 |
+
|
54 |
+
|
55 |
+
## Historical Data
|
56 |
+
class CHAIN(BaseModel):
|
57 |
+
expiry: str
|
58 |
+
data: List[dict]
|
59 |
+
|
60 |
+
chain_response_example = {
|
61 |
+
"expiry": "BANKNIFTY24703",
|
62 |
+
"data": [
|
63 |
+
{
|
64 |
+
"Symbol_CE": "BANKNIFTY2470351600CE",
|
65 |
+
"OI_CALL": 3347,
|
66 |
+
"CALL": 676.8,
|
67 |
+
"strikePrice": 51600,
|
68 |
+
"PUT": 43.3,
|
69 |
+
"OI_PUT": 61530,
|
70 |
+
"Symbol_PE": "BANKNIFTY2470351600PE"
|
71 |
+
},
|
72 |
+
{
|
73 |
+
"Symbol_CE": "BANKNIFTY2470351700CE",
|
74 |
+
"OI_CALL": 4870,
|
75 |
+
"CALL": 585.55,
|
76 |
+
"strikePrice": 51700,
|
77 |
+
"PUT": 56.45,
|
78 |
+
"OI_PUT": 66516,
|
79 |
+
"Symbol_PE": "BANKNIFTY2470351700PE"
|
80 |
+
},
|
81 |
+
]
|
82 |
+
}
|
83 |
+
|
84 |
+
|
85 |
+
## News Data
|
86 |
+
class NEWS(BaseModel):
|
87 |
+
data: List[dict]
|
88 |
+
|
89 |
+
news_response_example = {
|
90 |
+
"data": [
|
91 |
+
{
|
92 |
+
"title": "Karnataka's new gig worker draft bill: Why Morgan Stanley is 'overweight' on Zomato",
|
93 |
+
"summary": "NSE Morgan Stanley has maintained an overweight rating on Zomato with a target price of ₹235 per share.",
|
94 |
+
"url": "https://www.cnbctv18.com/market/karnataka-new-draft-gig-worker-bill-zomato-shares-price-stock-morgan-stanley-target-overweight-19436697.htm",
|
95 |
+
"pubDate": "2024-07-02T09:38:15",
|
96 |
+
"source": "CNBC TV18",
|
97 |
+
"companyName": "Zomato",
|
98 |
+
"symbol": "ZOMATO",
|
99 |
+
"blogUrl": "https://www.cnbctv18.com/market/karnataka-new-draft-gig-worker-bill-zomato-shares-price-stock-morgan-stanley-target-overweight-19436697.htm",
|
100 |
+
"topics": "Regulatory and Legal"
|
101 |
+
},
|
102 |
+
{
|
103 |
+
"title": "Zomato Shares Touch An All-Time High After ESOP Plan Gets Shareholders’ Nod",
|
104 |
+
"summary": "Shares of Zomato reached an all-time high of INR 209.75 apiece during Tuesday’s intraday trading session The price surge in the stock came after Zomato said it has secured approval from its shareholders to grant",
|
105 |
+
"url": "https://inc42.com/buzz/zomato-shares-touch-an-all-time-high-after-esop-plan-gets-shareholders-nod/",
|
106 |
+
"pubDate": "2024-07-02T07:36:51",
|
107 |
+
"source": "Inc42",
|
108 |
+
"companyName": "Zomato",
|
109 |
+
"symbol": "ZOMATO",
|
110 |
+
"blogUrl": "https://inc42.com/buzz/zomato-shares-touch-an-all-time-high-after-esop-plan-gets-shareholders-nod/",
|
111 |
+
"topics": "Financial Results"
|
112 |
+
}
|
113 |
+
]
|
114 |
+
}
|
115 |
+
|
116 |
+
|
117 |
+
## Signal Model
|
118 |
+
class SIGNAL(BaseModel):
|
119 |
+
data: dict
|
120 |
+
|
121 |
+
signal_response_example = {
|
122 |
+
"data": {
|
123 |
+
"Date": "2024-07-02T15:15:00+05:30",
|
124 |
+
"Open": 52164.5,
|
125 |
+
"High": 52200.7,
|
126 |
+
"Low": 52131.2,
|
127 |
+
"Close": 52148.1,
|
128 |
+
"RSI": 42.25281555502232,
|
129 |
+
"Prev_RSI": 43.331830665536984,
|
130 |
+
"Signal": 0,
|
131 |
+
"Option": 52200,
|
132 |
+
"direction": "",
|
133 |
+
"symbol": "BANKNIFTY2470352200"
|
134 |
+
}
|
135 |
+
}
|
136 |
+
|
137 |
+
## get all stocks
|
138 |
+
class ALL(BaseModel):
|
139 |
+
data: str
|
140 |
+
|
141 |
+
|
142 |
+
all_response_example = {
|
143 |
+
"data": '''[{\"isin\":\"INE002A01018\",\"growwContractId\":\"GSTK500325\"'''
|
144 |
+
}
|
main.py
CHANGED
@@ -1,13 +1,14 @@
|
|
1 |
from fastapi.responses import HTMLResponse
|
2 |
from fastapi.templating import Jinja2Templates
|
3 |
-
from fastapi import FastAPI, Request, HTTPException
|
4 |
from fastapi.middleware.cors import CORSMiddleware
|
5 |
from utils import StockDataFetcher
|
6 |
-
|
7 |
-
|
8 |
|
9 |
fetcher = StockDataFetcher()
|
10 |
|
|
|
11 |
origins = ["*"]
|
12 |
|
13 |
app.add_middleware(
|
@@ -20,65 +21,100 @@ app.add_middleware(
|
|
20 |
|
21 |
templates = Jinja2Templates(directory="templates")
|
22 |
|
|
|
23 |
@app.get("/", response_class=HTMLResponse)
|
24 |
async def read_root(request: Request):
|
25 |
return templates.TemplateResponse("hello.html", {"request": request})
|
26 |
|
27 |
|
28 |
-
|
29 |
-
|
|
|
30 |
try:
|
31 |
response = fetcher.fetch_latest_price(ticker)
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
35 |
|
36 |
-
@app.get('/historical')
|
37 |
-
async def get_stocks_data(ticker: str, intervals: int, days: int):
|
38 |
-
try:
|
39 |
-
response = fetcher.fetch_stock_data(ticker, 15, 10).to_dict(orient="records")
|
40 |
-
return {"data" : response}
|
41 |
-
except:
|
42 |
-
return {"Timeout" : "Error"}
|
43 |
|
44 |
-
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
try:
|
47 |
response, exp = fetcher.fetch_option_chain(ticker)
|
48 |
response = response.to_dict(orient="records")
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
|
|
|
|
53 |
|
54 |
-
@app.get('/search')
|
55 |
-
async def stock_search(ticker: str):
|
56 |
-
try:
|
57 |
-
response = fetcher.search_entity(ticker)
|
58 |
-
return {"data" : response}
|
59 |
-
except:
|
60 |
-
return {"Timeout" : "Error"}
|
61 |
-
|
62 |
-
# @app.get('/news')
|
63 |
-
# async def get_stocks_data(ticker: str, page: int, size: int):
|
64 |
-
# try:
|
65 |
-
# response = fetcher.fetch_stock_news(ticker, page=page, size=size).to_dict(orient="records")
|
66 |
-
# return {"data" : response}
|
67 |
-
# except:
|
68 |
-
# return {"Timeout" : "Error"}
|
69 |
-
|
70 |
-
# @app.get('/all')
|
71 |
-
# async def get_stocks_data():
|
72 |
-
# try:
|
73 |
-
# response = fetcher.fetch_all_stock().to_dict(orient="records")
|
74 |
-
# return {"data" : response}
|
75 |
-
# except:
|
76 |
-
# return {"Timeout" : "Error"}
|
77 |
|
78 |
-
|
79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
try:
|
81 |
response = fetcher.realtime_signal(index).tail(1).to_dict(orient="records")[0]
|
82 |
-
|
83 |
-
|
84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
from fastapi.responses import HTMLResponse
|
2 |
from fastapi.templating import Jinja2Templates
|
3 |
+
from fastapi import FastAPI, Request, HTTPException, Query, Body
|
4 |
from fastapi.middleware.cors import CORSMiddleware
|
5 |
from utils import StockDataFetcher
|
6 |
+
from Model import *
|
7 |
+
import json
|
8 |
|
9 |
fetcher = StockDataFetcher()
|
10 |
|
11 |
+
app = FastAPI(swagger_ui_parameters={"syntaxHighlight.theme": "obsidian"})
|
12 |
origins = ["*"]
|
13 |
|
14 |
app.add_middleware(
|
|
|
21 |
|
22 |
templates = Jinja2Templates(directory="templates")
|
23 |
|
24 |
+
# HTML Page
|
25 |
@app.get("/", response_class=HTMLResponse)
|
26 |
async def read_root(request: Request):
|
27 |
return templates.TemplateResponse("hello.html", {"request": request})
|
28 |
|
29 |
|
30 |
+
# Latest Price Fetch
|
31 |
+
@app.get('/ltp', response_model=LTP, responses={200: {"description": "Successful response", "content": {"application/json": {"example": ltp_response_example}}}})
|
32 |
+
async def get_data(ticker: str = Query(..., description="Enter your stock name.", example='zomato')):
|
33 |
try:
|
34 |
response = fetcher.fetch_latest_price(ticker)
|
35 |
+
if response:
|
36 |
+
return {'ltp' :response}
|
37 |
+
else:
|
38 |
+
raise HTTPException(status_code=404, detail="ltp price not found")
|
39 |
+
except Exception as e:
|
40 |
+
raise HTTPException(status_code=500, detail=str(e))
|
41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
+
# Search Stock Ticker
|
44 |
+
@app.get('/search', response_model=Stock, responses={200: {"description": "Successful response", "content": {"application/json": {"example": search_response_example}}}})
|
45 |
+
async def stock_search(ticker: str = Query(..., description="Enter your stock name.", example='zomato')):
|
46 |
+
try:
|
47 |
+
fetcher = StockDataFetcher()
|
48 |
+
response = fetcher.search_entity(ticker)
|
49 |
+
if response:
|
50 |
+
return response
|
51 |
+
else:
|
52 |
+
raise HTTPException(status_code=404, detail="Stock not found")
|
53 |
+
except Exception as e:
|
54 |
+
raise HTTPException(status_code=500, detail=str(e))
|
55 |
+
|
56 |
+
|
57 |
+
# Fetch Historical Data
|
58 |
+
@app.get('/historical', response_model=HISTORICAL, responses={200: {"description": "Successful response", "content": {"application/json": {"example": historical_response_example}}}})
|
59 |
+
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)):
|
60 |
+
try:
|
61 |
+
response = fetcher.fetch_stock_data(ticker, intervals, days).to_dict(orient="records")
|
62 |
+
if response:
|
63 |
+
return {"data" : response}
|
64 |
+
else:
|
65 |
+
raise HTTPException(status_code=404, detail="historical data not found.")
|
66 |
+
except Exception as e:
|
67 |
+
raise HTTPException(status_code=500, detail=str(e))
|
68 |
+
|
69 |
+
|
70 |
+
# Fetch Stock Chain
|
71 |
+
@app.get('/chain', response_model=CHAIN, responses={200: {"description": "Successful response", "content": {"application/json": {"example": chain_response_example}}}})
|
72 |
+
async def get_chain(ticker: str = Query(..., description="Enter your index name from [nifty, nifty-bank]", example='nifty-bank')):
|
73 |
try:
|
74 |
response, exp = fetcher.fetch_option_chain(ticker)
|
75 |
response = response.to_dict(orient="records")
|
76 |
+
if response:
|
77 |
+
return {"expiry": exp, "data" : response}
|
78 |
+
else:
|
79 |
+
raise HTTPException(status_code=404, detail="Unable to fetch chain.")
|
80 |
+
except Exception as e:
|
81 |
+
raise HTTPException(status_code=500, detail=str(e))
|
82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
|
84 |
+
# Stocks News
|
85 |
+
@app.get('/news', response_model=NEWS, responses={200: {"description": "Successful response", "content": {"application/json": {"example": news_response_example}}}})
|
86 |
+
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)):
|
87 |
+
try:
|
88 |
+
response = fetcher.fetch_stock_news(ticker, page=page, size=size).to_dict(orient="records")
|
89 |
+
if response:
|
90 |
+
return {"data" : response}
|
91 |
+
else:
|
92 |
+
raise HTTPException(status_code=404, detail="Unable to fetch news data.")
|
93 |
+
except Exception as e:
|
94 |
+
raise HTTPException(status_code=500, detail=str(e))
|
95 |
+
|
96 |
+
|
97 |
+
# Get signal data
|
98 |
+
@app.get('/signal', response_model=SIGNAL, responses={200: {"description": "Successful response", "content": {"application/json": {"example": signal_response_example}}}})
|
99 |
+
async def get_stock_signal(index: str = Query(..., description="Enter your index name from [nifty, nifty-bank]", example='nifty')):
|
100 |
try:
|
101 |
response = fetcher.realtime_signal(index).tail(1).to_dict(orient="records")[0]
|
102 |
+
if response:
|
103 |
+
return {"data" : response}
|
104 |
+
else:
|
105 |
+
raise HTTPException(status_code=404, detail="Unable to fetch signals")
|
106 |
+
except Exception as e:
|
107 |
+
raise HTTPException(status_code=500, detail=str(e))
|
108 |
+
|
109 |
+
|
110 |
+
# Fetch all stocks info
|
111 |
+
@app.get('/all', response_model=ALL, responses={200: {"description": "Successful response", "content": {"application/json": {"example": all_response_example}}}})
|
112 |
+
async def get_stocks_data():
|
113 |
+
try:
|
114 |
+
response = fetcher.fetch_all_stock().to_json(orient="records")
|
115 |
+
if response:
|
116 |
+
return {"data" : response}
|
117 |
+
else:
|
118 |
+
raise HTTPException(status_code=404, detail="Unable to fetch all stocks")
|
119 |
+
except Exception as e:
|
120 |
+
raise HTTPException(status_code=500, detail=str(e))
|
run.sh
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
uvicorn main:app --host 0.0.0.0 --port 8000 --reload
|