Skip to content

Commit

Permalink
add forward pe
Browse files Browse the repository at this point in the history
  • Loading branch information
MuslemRahimi committed Jul 17, 2024
1 parent 94bc166 commit 3857597
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
4 changes: 3 additions & 1 deletion app/cron_dark_pool_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ def run():
for entry in filtered_data
]

print(res)

if len(res) > 0:
save_json(res)

Expand All @@ -99,7 +101,7 @@ def run():
future = executor.submit(run)
try:
# Wait for the result with a timeout of 300 seconds (5 minutes)
future.result(timeout=300)
future.result(timeout=10)
except TimeoutError:
print("The operation timed out.")
except Exception as e:
Expand Down
28 changes: 16 additions & 12 deletions app/cron_share_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
import time


async def save_as_json(symbol, data):
async def save_as_json(symbol, forward_pe_dict, short_dict):
with open(f"json/share-statistics/{symbol}.json", 'w') as file:
ujson.dump(data, file)
ujson.dump(short_dict, file)
with open(f"json/forward-pe/{symbol}.json", 'w') as file:
ujson.dump(forward_pe_dict, file)


query_template = f"""
Expand All @@ -33,14 +35,15 @@ def filter_data_quarterly(data):

return filtered_data

def get_short_data(ticker, outstanding_shares, float_shares):
def get_yahoo_data(ticker, outstanding_shares, float_shares):
try:
data_dict = yf.Ticker(ticker).info
forward_pe = round(data_dict['forwardPE'],2)
short_outstanding_percent = round((data_dict['sharesShort']/outstanding_shares)*100,2)
short_float_percent = round((data_dict['sharesShort']/float_shares)*100,2)
return {'sharesShort': data_dict['sharesShort'], 'shortRatio': data_dict['shortRatio'], 'sharesShortPriorMonth': data_dict['sharesShortPriorMonth'], 'shortOutStandingPercent': short_outstanding_percent, 'shortFloatPercent': short_float_percent}
return {'forwardPE': forward_pe}, {'sharesShort': data_dict['sharesShort'], 'shortRatio': data_dict['shortRatio'], 'sharesShortPriorMonth': data_dict['sharesShortPriorMonth'], 'shortOutStandingPercent': short_outstanding_percent, 'shortFloatPercent': short_float_percent}
except:
return {'sharesShort': 0, 'shortRatio': 0, 'sharesShortPriorMonth': 0, 'shortOutStandingPercent': 0, 'shortFloatPercent': 0}
return {'forwardPE': 0}, {'sharesShort': 0, 'shortRatio': 0, 'sharesShortPriorMonth': 0, 'shortOutStandingPercent': 0, 'shortFloatPercent': 0}


async def get_data(ticker, con):
Expand All @@ -66,13 +69,14 @@ async def get_data(ticker, con):
# Filter out only quarter-end dates
historical_shares = filter_data_quarterly(shareholder_statistics)

short_data = get_short_data(ticker, latest_outstanding_shares, latest_float_shares)
res = {**short_data, 'latestOutstandingShares': latest_outstanding_shares, 'latestFloatShares': latest_float_shares,'historicalShares': historical_shares}
forward_pe_data, short_data = get_yahoo_data(ticker, latest_outstanding_shares, latest_float_shares)
short_data = {**short_data, 'latestOutstandingShares': latest_outstanding_shares, 'latestFloatShares': latest_float_shares,'historicalShares': historical_shares}
except Exception as e:
print(e)
res = {}
short_data = {}
forward_pe_data = {}

return res
return forward_pe_data, short_data


async def run():
Expand All @@ -87,9 +91,9 @@ async def run():
counter = 0

for ticker in tqdm(stock_symbols):
data_dict = await get_data(ticker, con)
if data_dict.keys():
await save_as_json(ticker, data_dict)
forward_pe_dict, short_dict = await get_data(ticker, con)
if forward_pe_dict.keys() and short_dict.keys():
await save_as_json(ticker, forward_pe_dict, short_dict)

counter += 1
if counter % 100 == 0:
Expand Down
8 changes: 8 additions & 0 deletions app/restart_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,14 @@ async def get_stock_screener(con,symbols):
except:
item['fundamentalAnalysis'] = {"accuracy": None}

try:
with open(f"json/forward-pe/{symbol}.json", 'r') as file:
res = ujson.load(file)
if res['forwardPE'] != 0:
item['forwardPE'] = res['forwardPE']
except:
item['forwardPE'] = None


return stock_screener_data

Expand Down

0 comments on commit 3857597

Please sign in to comment.