-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
170 lines (132 loc) · 3.95 KB
/
Copy pathmain.py
File metadata and controls
170 lines (132 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
__version__ = "1.0"
__author__ = "Amit Kumar"
import asyncio
from typing import Optional
import uvicorn
from fastapi import FastAPI, Query, WebSocket, WebSocketDisconnect
from indicators import Indicator
from livefeed import LiveFeed
from utils import *
from orderclient import OrderClient, get_quote
from portfolio import Portfolio
from watchlist import Watchlist
# Add async to the function which are slower than others
app = FastAPI()
active_connections = set()
subscribed_flag = False
@app.on_event("startup")
def startup_event():
return
@app.on_event("shutdown")
def shutdown_event():
if subscribed_flag:
livefeed.unsubscribe()
return
@app.get("/")
def hello():
return {"data": ["hello", "world"]}
@app.websocket("/livefeed")
async def get_livefeed(websocket: WebSocket):
await websocket.accept()
active_connections.add(websocket)
try:
while True:
data = LiveFeed.df_notify
for connection in active_connections:
await connection.send_json(data)
await asyncio.sleep(30)
except WebSocketDisconnect:
active_connections.remove(websocket)
@app.get("/subscribe")
async def subscribe():
global subscribed_flag
if subscribed_flag:
return {"status": "success", "message": "Already Subscribed"}
global portfolio, indicator, livefeed
livefeed = LiveFeed()
response = await livefeed.subscribe()
if response["status"] == "success":
subscribed_flag = True
portfolio = Portfolio()
indicator = Indicator()
indicator.attachObserver(portfolio)
livefeed.attachObserver(indicator)
return response
@app.get("/unsubscribe")
def unsubscribe():
global subscribed_flag
if not subscribed_flag:
return {"status": "success", "message": "Subscribe First"}
global portfolio, indicator, livefeed
response = livefeed.unsubscribe()
subscribed_flag = False
try:
del livefeed
del indicator
del portfolio
except Exception as e:
response["Exception"] = e
return response
@app.get("/orders")
def get_orders():
response = OrderClient().get_order_report()
return response
@app.get("/trades")
def get_trades():
response = OrderClient().get_trade_report()
return response
@app.get("/funds")
def get_funds():
response = OrderClient().get_funds()
return response
@app.get("/margin")
def get_margin(
type: str = Query(
..., min_length=1, max_length=6, title="Transaction Type"
),
token: str = Query(
..., min_length=1, max_length=6, title="Instrument Token"
),
quantity: Optional[int] = Query(1, title="Quantity"),
price: Optional[float] = Query(0, title="Price"),
):
if type == "buy":
transactionType = TransactionType.buy
else:
transactionType = TransactionType.sell
response = OrderClient().get_required_margin(
transactionType, [OrderParams(token, quantity, price)]
)
return response
@app.get("/quote/{quote_type}")
def get_Quote(
quote_type: str,
token: str = Query(
..., min_length=1, max_length=6, title="Instrument Token"
),
):
quoteType = None
if quote_type == "ltp":
quoteType = QuoteType.ltp
elif quote_type == "depth":
quoteType = QuoteType.depth
else:
quoteType = QuoteType.ohlc
response = get_quote(instrumentToken=token, quote_type=quoteType)
return response
@app.get("/position/{position_type}")
def get_Open_position(position_type: str):
if position_type == "open":
positionType = PositionType.open
elif position_type == "stocks":
positionType = PositionType.stocks
else:
positionType = PositionType.today
response = OrderClient().get_position(positionType)
return response
@app.get("/fetchTokens")
def get_tokens():
response = Watchlist().fetch_tokens()
return response
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8080)