Skip to content

Commit 7d9f1d9

Browse files
committed
fix isin retrieval
1 parent 7e085eb commit 7d9f1d9

File tree

3 files changed

+27
-22
lines changed

3 files changed

+27
-22
lines changed

fennel_invest_api/endpoints.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,8 @@ def stock_search_query(self, symbol, count=5):
139139
searchSecurities(query: $query, count: $count) {
140140
isin
141141
security {
142-
currentStockPrice
142+
currentStockPrice
143+
ticker
143144
}
144145
}
145146
}

fennel_invest_api/fennel.py

+24-20
Original file line numberDiff line numberDiff line change
@@ -214,14 +214,16 @@ def get_stock_quote(self, ticker):
214214
f"Stock Search Request failed with status code {search_response.status_code}: {search_response.text}"
215215
)
216216
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
218222

219223
@check_login
220224
def get_stock_price(self, ticker):
221225
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"]
225227

226228
@check_login
227229
def get_stock_holdings(self, account_id):
@@ -251,6 +253,11 @@ def is_market_open(self):
251253
response = response.json()
252254
return response["data"]["securityMarketInfo"]["isOpen"]
253255

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+
254261
@check_login
255262
def place_order(
256263
self, account_id, ticker, quantity, side, price="market", dry_run=False
@@ -261,27 +268,24 @@ def place_order(
261268
if not self.is_market_open():
262269
raise Exception("Market is closed. Cannot place order.")
263270
# 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}")
274274
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+
}
281284
# Place order
282285
query = self.endpoints.stock_order_query(
283286
account_id, ticker, quantity, isin, side, price
284287
)
288+
headers = self.endpoints.build_headers(self.Bearer)
285289
order_response = self.session.post(
286290
self.endpoints.graphql, headers=headers, data=query
287291
)

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
setup(
44
name="fennel_invest_api",
5-
version="1.0.8",
5+
version="1.0.9",
66
description="Unofficial Fennel.com Invest API written in Python Requests",
77
long_description=open("README.md").read(),
88
long_description_content_type="text/markdown",

0 commit comments

Comments
 (0)