update hedge fund db
This commit is contained in:
parent
27a8c16ad5
commit
68080ac0d4
@ -116,9 +116,9 @@ class InstituteDatabase:
|
|||||||
async def save_portfolio_data(self, session, cik):
|
async def save_portfolio_data(self, session, cik):
|
||||||
try:
|
try:
|
||||||
urls = [
|
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?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 = {}
|
portfolio_data = {}
|
||||||
@ -129,9 +129,11 @@ class InstituteDatabase:
|
|||||||
parsed_data = get_jsonparsed_data(data)
|
parsed_data = get_jsonparsed_data(data)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
'''
|
||||||
if isinstance(parsed_data, list) and "industry/portfolio-holdings-summary" in url:
|
if isinstance(parsed_data, list) and "industry/portfolio-holdings-summary" in url:
|
||||||
# Handle list response, save as JSON object
|
# Handle list response, save as JSON object
|
||||||
portfolio_data['industry'] = json.dumps(parsed_data)
|
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:
|
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
|
# 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)
|
positive_performance_count = sum(1 for percentage in performance_percentages if percentage > 0)
|
||||||
|
try:
|
||||||
win_rate = round(positive_performance_count / len(performance_percentages) * 100,2)
|
win_rate = round(positive_performance_count / len(performance_percentages) * 100,2)
|
||||||
|
except:
|
||||||
|
win_rate = 0
|
||||||
|
|
||||||
data_dict = {
|
data_dict = {
|
||||||
'winRate': win_rate,
|
'winRate': win_rate,
|
||||||
'numberOfStocks': number_of_stocks,
|
'numberOfStocks': number_of_stocks,
|
||||||
@ -162,23 +168,25 @@ class InstituteDatabase:
|
|||||||
|
|
||||||
portfolio_data.update(data_dict)
|
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
|
# Handle list response, save as JSON object
|
||||||
portfolio_data['summary'] = json.dumps(parsed_data)
|
portfolio_data['summary'] = json.dumps(parsed_data)
|
||||||
data_dict = {
|
data_dict = {
|
||||||
#'numberOfStocks': parsed_data[0]['portfolioSize'],
|
'numberOfStocks': parsed_data[0]['portfolioSize'],
|
||||||
'marketValue': parsed_data[0]['marketValue'],
|
'marketValue': parsed_data[0]['marketValue'],
|
||||||
'averageHoldingPeriod': parsed_data[0]['averageHoldingPeriod'],
|
'averageHoldingPeriod': parsed_data[0]['averageHoldingPeriod'],
|
||||||
'turnover': parsed_data[0]['turnover'],
|
'turnover': parsed_data[0]['turnover'],
|
||||||
|
'performancePercentage': parsed_data[0]['performancePercentage'],
|
||||||
'performancePercentage3year': parsed_data[0]['performancePercentage3year'],
|
'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)
|
portfolio_data.update(data_dict)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
except:
|
except Exception as e:
|
||||||
pass
|
print(e)
|
||||||
|
|
||||||
# Check if columns already exist in the table
|
# Check if columns already exist in the table
|
||||||
self.cursor.execute("PRAGMA table_info(institutes)")
|
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)
|
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
|
# If 'industry' is not a list, delete the row and return
|
||||||
#print(f"Deleting row for cik {cik} because 'industry' is not a list.")
|
#print(f"Deleting row for cik {cik} because 'industry' is not a list.")
|
||||||
self.cursor.execute("DELETE FROM institutes WHERE cik = ?", (cik,))
|
self.cursor.execute("DELETE FROM institutes WHERE cik = ?", (cik,))
|
||||||
@ -256,7 +265,7 @@ class InstituteDatabase:
|
|||||||
tasks.append(self.save_portfolio_data(session, cik))
|
tasks.append(self.save_portfolio_data(session, cik))
|
||||||
|
|
||||||
i += 1
|
i += 1
|
||||||
if i % 700 == 0:
|
if i % 1000 == 0:
|
||||||
await asyncio.gather(*tasks)
|
await asyncio.gather(*tasks)
|
||||||
tasks = []
|
tasks = []
|
||||||
print('sleeping mode: ', i)
|
print('sleeping mode: ', i)
|
||||||
|
|||||||
@ -1410,7 +1410,7 @@ async def get_hedge_funds_data(data: GetCIKData):
|
|||||||
cursor = con_inst.cursor()
|
cursor = con_inst.cursor()
|
||||||
|
|
||||||
# Execute a SQL query to select the top 10 best performing cik entries by winRate
|
# 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()
|
cik_data = cursor.fetchall()
|
||||||
res = [{
|
res = [{
|
||||||
'cik': row[0],
|
'cik': row[0],
|
||||||
@ -1422,9 +1422,10 @@ async def get_hedge_funds_data(data: GetCIKData):
|
|||||||
'marketValue': row[6],
|
'marketValue': row[6],
|
||||||
'winRate': row[7],
|
'winRate': row[7],
|
||||||
'holdings': ujson.loads(row[8]),
|
'holdings': ujson.loads(row[8]),
|
||||||
|
'summary': ujson.loads(row[9]),
|
||||||
} for row in cik_data]
|
} 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)
|
compressed_data = gzip.compress(res_json)
|
||||||
|
|
||||||
redis_client.set(cache_key, compressed_data)
|
redis_client.set(cache_key, compressed_data)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user