Viewing File: /home/ubuntu/codegamaai-test/broker_bot/archive/neo.py
import json
from fuzzywuzzy import process
import pandas as pd
import yahooquery as yq
# Load the stock data
with open('transformed_stock.json', 'r') as file:
stocks_data = json.load(file)
stock_names = [stock['name'] for stock in stocks_data]
def find_closest_stock(query):
closest_match, score = process.extractOne(query, stock_names)
stock_entry = next((stock for stock in stocks_data if stock['name'] == closest_match), None)
if score > 80: # Adjust the threshold score as needed
return {
"symbol": stock_entry['symbol'],
"name": stock_entry['name'],
"industry": stock_entry['industry'],
"score": score
}
else:
return "No close match found."
def get_symbol(query):
try:
data = yq.search(query)
except ValueError: # Will catch JSONDecodeError
print(query)
else:
quotes = data['quotes']
if len(quotes) == 0:
return 'No Symbol Found'
symbol = quotes[0]['symbol']
longname = quotes[0]['longname']
industry = quotes[0]['industry']
exchange = quotes[0]['exchDisp']
response = {
'symbol': symbol,
'longname': longname,
'industry': industry,
'exchange': exchange
}
# for quote in quotes:
# if quote['exchange'] == preferred_exchange:
# symbol = quote['symbol']
# break
return response
query = "nvidia"
result = find_closest_stock(query)
print(result)
Back to Directory
File Manager