update hedge fund db

This commit is contained in:
MuslemRahimi 2024-05-29 01:04:56 +02:00
parent 27a8c16ad5
commit 68080ac0d4
2 changed files with 22 additions and 12 deletions

View File

@ -116,9 +116,9 @@ class InstituteDatabase:
async def save_portfolio_data(self, session, cik):
try:
urls = [
f"https://financialmodelingprep.com/api/v4/institutional-ownership/industry/portfolio-holdings-summary?cik={cik}&date={quarter_date}&page=0&apikey={api_key}",
#f"https://financialmodelingprep.com/api/v4/institutional-ownership/industry/portfolio-holdings-summary?cik={cik}&date={quarter_date}&page=0&apikey={api_key}",
f"https://financialmodelingprep.com/api/v4/institutional-ownership/portfolio-holdings?cik={cik}&date={quarter_date}&page=0&apikey={api_key}",
f"https://financialmodelingprep.com/api/v4/institutional-ownership/portfolio-holdings-summary?cik={cik}&date={quarter_date}&page=0&apikey={api_key}"
f"https://financialmodelingprep.com/api/v4/institutional-ownership/portfolio-holdings-summary?cik={cik}&page=0&apikey={api_key}"
]
portfolio_data = {}
@ -129,9 +129,11 @@ class InstituteDatabase:
parsed_data = get_jsonparsed_data(data)
try:
'''
if isinstance(parsed_data, list) and "industry/portfolio-holdings-summary" in url:
# Handle list response, save as JSON object
portfolio_data['industry'] = json.dumps(parsed_data)
'''
if isinstance(parsed_data, list) and "https://financialmodelingprep.com/api/v4/institutional-ownership/portfolio-holdings?cik=" in url:
# Handle list response, save as JSON object
@ -153,7 +155,11 @@ class InstituteDatabase:
positive_performance_count = sum(1 for percentage in performance_percentages if percentage > 0)
win_rate = round(positive_performance_count / len(performance_percentages) * 100,2)
try:
win_rate = round(positive_performance_count / len(performance_percentages) * 100,2)
except:
win_rate = 0
data_dict = {
'winRate': win_rate,
'numberOfStocks': number_of_stocks,
@ -162,23 +168,25 @@ class InstituteDatabase:
portfolio_data.update(data_dict)
elif isinstance(parsed_data, list) and "https://financialmodelingprep.com/api/v4/institutional-ownership/portfolio-holdings-summary" in url:
if isinstance(parsed_data, list) and "https://financialmodelingprep.com/api/v4/institutional-ownership/portfolio-holdings-summary" in url:
# Handle list response, save as JSON object
portfolio_data['summary'] = json.dumps(parsed_data)
data_dict = {
#'numberOfStocks': parsed_data[0]['portfolioSize'],
'numberOfStocks': parsed_data[0]['portfolioSize'],
'marketValue': parsed_data[0]['marketValue'],
'averageHoldingPeriod': parsed_data[0]['averageHoldingPeriod'],
'turnover': parsed_data[0]['turnover'],
'performancePercentage': parsed_data[0]['performancePercentage'],
'performancePercentage3year': parsed_data[0]['performancePercentage3year'],
#'performancePercentage': parsed_data[0]['performancePercentage']
'performancePercentage5year': parsed_data[0]['performancePercentage5year'],
'performanceSinceInceptionPercentage': parsed_data[0]['performanceSinceInceptionPercentage']
}
portfolio_data.update(data_dict)
except:
pass
except Exception as e:
print(e)
# Check if columns already exist in the table
self.cursor.execute("PRAGMA table_info(institutes)")
@ -190,7 +198,8 @@ class InstituteDatabase:
symbols_not_in_list = not any(symbol in total_symbols for symbol in symbols_to_check)
if symbols_not_in_list or 'industry' not in portfolio_data or len(json.loads(portfolio_data['industry'])) == 0:
#if symbols_not_in_list or 'industry' not in portfolio_data or len(json.loads(portfolio_data['industry'])) == 0:
if symbols_not_in_list:
# If 'industry' is not a list, delete the row and return
#print(f"Deleting row for cik {cik} because 'industry' is not a list.")
self.cursor.execute("DELETE FROM institutes WHERE cik = ?", (cik,))
@ -256,7 +265,7 @@ class InstituteDatabase:
tasks.append(self.save_portfolio_data(session, cik))
i += 1
if i % 700 == 0:
if i % 1000 == 0:
await asyncio.gather(*tasks)
tasks = []
print('sleeping mode: ', i)

View File

@ -1410,7 +1410,7 @@ async def get_hedge_funds_data(data: GetCIKData):
cursor = con_inst.cursor()
# Execute a SQL query to select the top 10 best performing cik entries by winRate
cursor.execute("SELECT cik, name, numberOfStocks, performancePercentage3year, averageHoldingPeriod, turnover, marketValue, winRate, holdings FROM institutes WHERE cik = ?", (cik,))
cursor.execute("SELECT cik, name, numberOfStocks, performancePercentage3year, averageHoldingPeriod, turnover, marketValue, winRate, holdings, summary FROM institutes WHERE cik = ?", (cik,))
cik_data = cursor.fetchall()
res = [{
'cik': row[0],
@ -1422,9 +1422,10 @@ async def get_hedge_funds_data(data: GetCIKData):
'marketValue': row[6],
'winRate': row[7],
'holdings': ujson.loads(row[8]),
'summary': ujson.loads(row[9]),
} for row in cik_data]
res_json = ujson.dumps(res).encode('utf-8')
res_json = ujson.dumps(res[0]).encode('utf-8')
compressed_data = gzip.compress(res_json)
redis_client.set(cache_key, compressed_data)