add missing file
Browse files
zacks.py
ADDED
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
import urllib.request
|
3 |
+
import json
|
4 |
+
import time
|
5 |
+
import random
|
6 |
+
from concurrent.futures import ThreadPoolExecutor, as_completed
|
7 |
+
import logging
|
8 |
+
|
9 |
+
logging.basicConfig(level=logging.INFO)
|
10 |
+
logger = logging.getLogger(__name__)
|
11 |
+
|
12 |
+
def get_zacks_rank(symbol, max_retries=3):
|
13 |
+
"""Get Zacks rank for a symbol."""
|
14 |
+
for _ in range(max_retries):
|
15 |
+
try:
|
16 |
+
url = f'https://quote-feed.zacks.com/index?t={symbol}'
|
17 |
+
with urllib.request.urlopen(url, timeout=10) as response:
|
18 |
+
return json.loads(response.read().decode())[symbol]["zacks_rank"]
|
19 |
+
except Exception as e:
|
20 |
+
logger.error(f"Error fetching Zacks Rank for {symbol}: {e}")
|
21 |
+
break
|
22 |
+
return None
|
23 |
+
|
24 |
+
def find_rank_1_stocks(n=10, max_workers=60):
|
25 |
+
"""Find n stocks with Zacks rank 1 from SP500."""
|
26 |
+
try:
|
27 |
+
# Get and shuffle SP500 symbols
|
28 |
+
sp500_symbols = pd.read_html('https://en.wikipedia.org/wiki/List_of_S%26P_500_companies')[0]['Symbol'].tolist()
|
29 |
+
|
30 |
+
# Get SP400 (Mid Cap) symbols
|
31 |
+
sp400_symbols = pd.read_html('https://en.wikipedia.org/wiki/List_of_S%26P_400_companies')[0]['Symbol'].tolist()
|
32 |
+
|
33 |
+
# Combine and shuffle all symbols
|
34 |
+
|
35 |
+
symbols = sp500_symbols + sp400_symbols
|
36 |
+
# Get and shuffle SP500 symbols
|
37 |
+
#symbols = pd.read_html('https://en.wikipedia.org/wiki/List_of_S%26P_500_companies')[0]['Symbol'].tolist()
|
38 |
+
random.shuffle(symbols)
|
39 |
+
|
40 |
+
rank_1_stocks = []
|
41 |
+
processed = 0
|
42 |
+
|
43 |
+
# Process all symbols in parallel
|
44 |
+
with ThreadPoolExecutor(max_workers=max_workers) as executor:
|
45 |
+
futures = {executor.submit(get_zacks_rank, symbol): symbol for symbol in symbols}
|
46 |
+
|
47 |
+
for future in as_completed(futures):
|
48 |
+
if len(rank_1_stocks) >= n:
|
49 |
+
break
|
50 |
+
|
51 |
+
symbol = futures[future]
|
52 |
+
try:
|
53 |
+
rank = future.result()
|
54 |
+
processed += 1
|
55 |
+
|
56 |
+
if rank == '1':
|
57 |
+
rank_1_stocks.append({'symbol': symbol, 'zacks_rank': rank})
|
58 |
+
|
59 |
+
except Exception as e:
|
60 |
+
logger.error(f"Error processing {symbol}: {e}")
|
61 |
+
|
62 |
+
return rank_1_stocks[:n]
|
63 |
+
|
64 |
+
except Exception as e:
|
65 |
+
logger.error(f"Error fetching SP500 symbols: {e}")
|
66 |
+
return []
|
67 |
+
|
68 |
+
if __name__ == "__main__":
|
69 |
+
stocks = find_rank_1_stocks()
|
70 |
+
|
71 |
+
if stocks:
|
72 |
+
print("\nFound Zacks rank 1 stocks:")
|
73 |
+
for stock in stocks:
|
74 |
+
print(f"Symbol: {stock['symbol']}")
|
75 |
+
print(f"\nTotal found: {len(stocks)}")
|
76 |
+
else:
|
77 |
+
print("No rank 1 stocks found.")
|
78 |
+
|
79 |
+
|