-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
359 lines (320 loc) · 12.5 KB
/
app.py
File metadata and controls
359 lines (320 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# Work in progress. Comparative analysis @ Line 248 is currently hard coded. I will add proper functionality soon.
# Import necessary libraries and modules
from flask import Flask, render_template, request, jsonify
import requests
import pandas as pd
import sqlite3
import json
from datetime import datetime, timedelta
# Initialize Flask application
app = Flask(__name__)
# Create a cache dictionary to store company data
# Using a dictionary allows us to store both the data and when it was last updated
company_cache = {
'data': None,
'last_updated': None
}
def get_company_data():
"""
Fetch and cache company lookup data from SEC.
Implements caching to avoid hitting SEC's API too frequently.
Returns:
list: List of dictionaries containing company information
"""
global company_cache
# Check if we have valid cached data (less than 24 hours old)
if (company_cache['data'] is not None and
company_cache['last_updated'] is not None and
datetime.now() - company_cache['last_updated'] < timedelta(hours=24)):
return company_cache['data']
try:
# Fetch fresh data from SEC
url = 'https://www.sec.gov/files/company_tickers.json'
headers = {
'User-Agent': 'Sample Company Name AdminContact@company.com'
}
response = requests.get(url, headers=headers)
data = response.json()
# Convert the data structure for easier lookup
companies = []
for entry in data.values():
companies.append({
'cik_str': str(entry['cik_str']).zfill(10), # Ensure CIK is 10 digits
'ticker': entry['ticker'],
'title': entry['title']
})
# Update cache with new data
company_cache['data'] = companies
company_cache['last_updated'] = datetime.now()
return companies
except Exception as e:
print(f"Error fetching company data: {e}")
# Return cached data if available, empty list if not
return [] if company_cache['data'] is None else company_cache['data']
def build_sec_url(cik, accession):
"""
Builds a direct link to the SEC filing
Args:
cik (str): Company CIK number
accession (str): Filing accession number
Returns:
str: URL to the SEC filing
"""
# Remove dashes from accession number as per SEC URL format
clean_accession = accession.replace('-', '')
# Build the URL using SEC's format
return f"https://www.sec.gov/Archives/edgar/data/{cik}/{clean_accession}/{accession}.txt"
def fetch_sec_data(cik='320193'):
"""
Fetch company filing data from SEC EDGAR
Args:
cik (str): Company's CIK number (Central Index Key)
Returns:
dict: JSON response from SEC
"""
# Check if CIK is empty or None
if not cik:
print("No CIK provided, using default (Apple Inc.)")
cik = '320193' # Default to Apple Inc.
url = f'https://data.sec.gov/submissions/CIK{cik.zfill(10)}.json'
headers = {
'User-Agent': 'Sample Company Name AdminContact@company.com'
}
try:
response = requests.get(url, headers=headers)
# Check if the response was successful
if response.status_code == 200:
data = response.json()
print("\nAvailable SEC data fields:", data.keys())
print("\nAvailable filing fields:", data['filings']['recent'].keys())
return data
else:
print(f"Error: Received status code {response.status_code} from SEC API")
return None
except Exception as e:
print(f"Error fetching data: {e}")
return None
def process_sec_data(data):
"""
Convert SEC data into a pandas DataFrame with enhanced information
Args:
data (dict): JSON data from SEC
Returns:
pandas.DataFrame: Processed SEC filing data
"""
if not data or 'filings' not in data:
print("No filings data found")
return pd.DataFrame()
try:
# Create the base DataFrame with comprehensive filing information
filings = pd.DataFrame({
'Form Type': data['filings']['recent']['form'],
'Filing Date': data['filings']['recent']['filingDate'],
'Report Date': data['filings']['recent']['reportDate'],
'Document': data['filings']['recent']['primaryDocument'],
'Description': data['filings']['recent']['primaryDocDescription'],
'Size': data['filings']['recent']['size'],
'Is XBRL': data['filings']['recent']['isXBRL'],
'Accession': data['filings']['recent']['accessionNumber']
})
# Add direct links to SEC documents
cik = str(data['cik']).zfill(10)
filings['SEC URL'] = filings['Accession'].apply(
lambda x: build_sec_url(cik, x)
)
return filings.head(10) # Return the 10 most recent filings
except Exception as e:
print(f"Error processing SEC data: {e}")
return pd.DataFrame()
def store_filings(df):
"""
Store the processed filings in SQLite database
Args:
df (pandas.DataFrame): Processed SEC filing data
"""
conn = sqlite3.connect('sec_data.db')
df.to_sql('filings', conn, if_exists='replace', index=False)
conn.close()
def get_stored_filings():
"""
Retrieve stored filings from the database
Returns:
pandas.DataFrame: Stored filing data
"""
conn = sqlite3.connect('sec_data.db')
df = pd.read_sql('SELECT * FROM filings', conn)
conn.close()
return df
@app.route('/search_companies')
def search_companies():
"""
Enhanced API endpoint for company search with smart ranking
Returns:
JSON: Ranked list of matching companies
"""
query = request.args.get('query', '').lower()
companies = get_company_data()
if not query:
return jsonify([])
# Score and rank matches using a sophisticated ranking algorithm
scored_matches = []
for company in companies:
score = 0
title_lower = company['title'].lower()
ticker_lower = company['ticker'].lower()
# Exact matches get highest score
if query == ticker_lower:
score += 100
elif query == title_lower:
score += 90
# Starts-with matches get next highest score
elif ticker_lower.startswith(query):
score += 80
elif title_lower.startswith(query):
score += 70
# Contains matches get lower score
elif query in ticker_lower:
score += 60
elif query in title_lower:
score += 50
# If we have any kind of match, add to results
if score > 0:
scored_matches.append({
**company,
'score': score,
'matchType': 'ticker' if query in ticker_lower else 'name'
})
# Sort by score and take top 10 results
sorted_matches = sorted(scored_matches, key=lambda x: x['score'], reverse=True)[:10]
return jsonify(sorted_matches)
@app.route('/')
def index():
# Get the search parameters
cik = request.args.get('cik', '320193')
company_name = request.args.get('company_name', '')
# Fetch and process data
raw_data = fetch_sec_data(cik)
if raw_data:
processed_data = process_sec_data(raw_data)
store_filings(processed_data)
# Get stored filings - notice we use 'filings' not 'filing'
filings = get_stored_filings()
filings_list = filings.to_dict('records')
# Render template with all necessary data
return render_template('index.html',
filings=filings_list, # This is the correct variable name
company_name=company_name)
# Comparative analysis of financials in detailed view``
@app.route('/filing_details/<accession>')
def get_filing_details(accession):
print(f"Received request for filing details: {accession}")
try:
# Enhanced sample data with comparative analysis
details = {
'financials': {
'Revenue': {
'current': 123456789,
'previous_year': 110234567,
'previous_quarter': 119876543,
'yoy_change': 12.0,
'qoq_change': 3.0
},
'Net Income': {
'current': 45678901,
'previous_year': 41234567,
'previous_quarter': 44567890,
'yoy_change': 10.8,
'qoq_change': 2.5
},
'Operating Cash Flow': {
'current': 34567890,
'previous_year': 31234567,
'previous_quarter': 33456789,
'yoy_change': 10.7,
'qoq_change': 3.3
}
},
'industry_benchmarks': {
'Profitability': {
'Operating Margin': {
'company': 22.3,
'industry_avg': 18.7,
'industry_high': 28.4,
'industry_low': 12.1,
'percentile': 75, # Company is better than 75% of peers
'trend': 'improving' # Can be 'improving', 'stable', or 'declining'
},
'Net Profit Margin': {
'company': 15.7,
'industry_avg': 12.4,
'industry_high': 20.1,
'industry_low': 8.2,
'percentile': 80,
'trend': 'stable'
}
},
'Efficiency': {
'Asset Turnover': {
'company': 0.85,
'industry_avg': 0.72,
'industry_high': 1.1,
'industry_low': 0.5,
'percentile': 70,
'trend': 'improving'
},
'Revenue per Employee': {
'company': 450000,
'industry_avg': 380000,
'industry_high': 520000,
'industry_low': 290000,
'percentile': 85,
'trend': 'stable'
}
},
'Growth': {
'Revenue Growth': {
'company': 12.0,
'industry_avg': 8.5,
'industry_high': 15.2,
'industry_low': 3.1,
'percentile': 78,
'trend': 'improving'
},
'Market Share Growth': {
'company': 2.3,
'industry_avg': 0.5,
'industry_high': 3.1,
'industry_low': -1.2,
'percentile': 85,
'trend': 'improving'
}
}
},
'key_ratios': {
'current': {
'PE Ratio': 25.4,
'Price to Book': 12.3,
'Debt to Equity': 0.45,
'Current Ratio': 2.1,
'Quick Ratio': 1.8
},
'historical': {
'1y_avg': {
'PE Ratio': 24.8,
'Price to Book': 11.9
},
'5y_avg': {
'PE Ratio': 23.5,
'Price to Book': 10.8
}
}
}
}
print("Returning enhanced details:", details)
return jsonify(details)
except Exception as e:
print(f"Error fetching filing details: {e}")
return jsonify({'error': 'Failed to fetch filing details'}), 500
# Run the application in debug mode if executed directly
if __name__ == '__main__':
app.run(debug=True)