@@ -214,14 +214,16 @@ def get_stock_quote(self, ticker):
214
214
f"Stock Search Request failed with status code { search_response .status_code } : { search_response .text } "
215
215
)
216
216
search_response = search_response .json ()
217
- return search_response
217
+ securities = search_response ["data" ]["searchSearch" ]["searchSecurities" ]
218
+ if len (securities ) == 0 :
219
+ raise Exception (f"No stock found with ticker { ticker } . Please check the app to see if it is valid." )
220
+ stock_quote = next ((x for x in securities if x ["security" ]["ticker" ].lower () == ticker .lower ()), None )
221
+ return stock_quote
218
222
219
223
@check_login
220
224
def get_stock_price (self , ticker ):
221
225
quote = self .get_stock_quote (ticker )
222
- if len (quote ["data" ]["searchSearch" ]["searchSecurities" ]) == 0 :
223
- return None
224
- return quote ["data" ]["searchSearch" ]["searchSecurities" ][0 ]["security" ]["currentStockPrice" ]
226
+ return None if quote is None else quote ["security" ]["currentStockPrice" ]
225
227
226
228
@check_login
227
229
def get_stock_holdings (self , account_id ):
@@ -251,6 +253,11 @@ def is_market_open(self):
251
253
response = response .json ()
252
254
return response ["data" ]["securityMarketInfo" ]["isOpen" ]
253
255
256
+ @check_login
257
+ def get_stock_isin (self , ticker ):
258
+ quote = self .get_stock_quote (ticker )
259
+ return None if quote is None else quote ["isin" ]
260
+
254
261
@check_login
255
262
def place_order (
256
263
self , account_id , ticker , quantity , side , price = "market" , dry_run = False
@@ -261,27 +268,24 @@ def place_order(
261
268
if not self .is_market_open ():
262
269
raise Exception ("Market is closed. Cannot place order." )
263
270
# Search for stock "isin"
264
- query = self .endpoints .stock_search_query (ticker )
265
- headers = self .endpoints .build_headers (self .Bearer )
266
- search_response = self .session .post (
267
- self .endpoints .graphql , headers = headers , data = query
268
- )
269
- if search_response .status_code != 200 :
270
- raise Exception (
271
- f"Stock Search Request failed with status code { search_response .status_code } : { search_response .text } "
272
- )
273
- search_response = search_response .json ()
271
+ isin = self .get_stock_isin (ticker )
272
+ if isin is None :
273
+ raise Exception (f"Failed to find ISIN for stock with ticker { ticker } " )
274
274
if dry_run :
275
- return search_response
276
- if len (search_response ["data" ]["searchSearch" ]["searchSecurities" ]) == 0 :
277
- raise Exception (
278
- f"No stock found with ticker { ticker } . Please check the app to see if it is valid."
279
- )
280
- isin = search_response ["data" ]["searchSearch" ]["searchSecurities" ][0 ]["isin" ]
275
+ return {
276
+ "account_id" : account_id ,
277
+ "ticker" : ticker ,
278
+ "quantity" : quantity ,
279
+ "isin" : isin ,
280
+ "side" : side ,
281
+ "price" : price ,
282
+ "dry_run_success" : True ,
283
+ }
281
284
# Place order
282
285
query = self .endpoints .stock_order_query (
283
286
account_id , ticker , quantity , isin , side , price
284
287
)
288
+ headers = self .endpoints .build_headers (self .Bearer )
285
289
order_response = self .session .post (
286
290
self .endpoints .graphql , headers = headers , data = query
287
291
)
0 commit comments