add more rules

This commit is contained in:
MuslemRahimi 2024-10-26 16:58:24 +02:00
parent 0163ecdeef
commit 9de702534f
3 changed files with 43 additions and 8 deletions

View File

@ -542,6 +542,13 @@ data = {
"sharesYoY": { "sharesYoY": {
"text": "The change in the number of shares outstanding, comparing the most recent quarter to the same quarter a year ago.", "text": "The change in the number of shares outstanding, comparing the most recent quarter to the same quarter a year ago.",
}, },
"floatShares": {
"text": "Float is the amount of shares that are considered available for trading. It subtracts closely held shares by insiders and restricted stock from the total number of shares outstanding."
},
"interestCoverage": {
"text": "The interest coverage ratio is a measure of the ability of a company to pay its interest expenses. It is calculated by dividing the company's Earnings Before Interest and Taxes (EBIT) by its interest expenses.",
"equation": "Interest Coverage Ratio = EBIT / Interest Expense"
}
} }

View File

@ -1,21 +1,42 @@
from datetime import datetime, timedelta from datetime import datetime
import orjson import orjson
import time
import sqlite3 import sqlite3
import asyncio import asyncio
import aiohttp
import random
from tqdm import tqdm from tqdm import tqdm
from dotenv import load_dotenv
import os
# Load stock screener data
with open(f"json/stock-screener/data.json", 'rb') as file:
stock_screener_data = orjson.loads(file.read())
stock_screener_data_dict = {item['symbol']: item for item in stock_screener_data} stock_screener_data_dict = {item['symbol']: item for item in stock_screener_data}
async def save_json(symbol, data):
"""Save JSON data to a file."""
with open(f"json/statistics/{symbol}.json", 'wb') as file:
file.write(orjson.dumps(data))
async def get_data(symbol):
"""Extract specified columns data for a given symbol."""
columns = ['sharesOutStanding', 'sharesQoQ', 'sharesYoY','institutionalOwnership','floatShares',
'priceEarningsRatio','forwardPE','priceToSalesRatio','forwardPS','priceToBookRatio','priceToFreeCashFlowsRatio',
'sharesShort','shortOutStandingPercent','shortFloatPercent','shortRatio',
'enterpriseValue','evEarnings','evSales','evEBITDA','evEBIT','evFCF',
'currentRatio','quickRatio','debtRatio','debtEquityRatio',]
if symbol in stock_screener_data_dict:
result = {}
for column in columns:
result[column] = stock_screener_data_dict[symbol].get(column, None)
return result
return {}
async def run(): async def run():
"""Main function to run the data extraction process."""
# Connect to SQLite database
con = sqlite3.connect('stocks.db') con = sqlite3.connect('stocks.db')
cursor = con.cursor() cursor = con.cursor()
cursor.execute("PRAGMA journal_mode = wal") cursor.execute("PRAGMA journal_mode = wal")
@ -23,7 +44,11 @@ async def run():
total_symbols = [row[0] for row in cursor.fetchall()] total_symbols = [row[0] for row in cursor.fetchall()]
con.close() con.close()
# Process symbols with progress bar
for symbol in tqdm(total_symbols, desc="Extracting dividend data"):
data = await get_data(symbol)
if data: # Only save if we have data
await save_json(symbol, data)
if __name__ == "__main__": if __name__ == "__main__":
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()

View File

@ -93,6 +93,7 @@ def filter_data_quarterly(data):
def calculate_share_changes(symbol, item, con): def calculate_share_changes(symbol, item, con):
item['sharesQoQ'] = None item['sharesQoQ'] = None
item['sharesYoY'] = None item['sharesYoY'] = None
item['floatShares'] = None
try: try:
# Execute query and load data # Execute query and load data
df = pd.read_sql_query(query_shares, con, params=(symbol,)) df = pd.read_sql_query(query_shares, con, params=(symbol,))
@ -109,7 +110,8 @@ def calculate_share_changes(symbol, item, con):
] ]
shareholder_statistics = sorted(shareholder_statistics, key=lambda x: datetime.strptime(x['date'], '%Y-%m-%d'), reverse=False) shareholder_statistics = sorted(shareholder_statistics, key=lambda x: datetime.strptime(x['date'], '%Y-%m-%d'), reverse=False)
#Add latest float shares for statistics page
item['floatShares'] = shareholder_statistics[-1]['floatShares']
historical_shares = filter_data_quarterly(shareholder_statistics) historical_shares = filter_data_quarterly(shareholder_statistics)
latest_data = historical_shares[-1]['outstandingShares'] latest_data = historical_shares[-1]['outstandingShares']
@ -121,6 +123,7 @@ def calculate_share_changes(symbol, item, con):
except: except:
item['sharesQoQ'] = None item['sharesQoQ'] = None
item['sharesYoY'] = None item['sharesYoY'] = None
item['floatShares'] = None
# Replace NaN values with None in the resulting JSON object # Replace NaN values with None in the resulting JSON object