-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·313 lines (264 loc) · 9.95 KB
/
main.py
File metadata and controls
executable file
·313 lines (264 loc) · 9.95 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
#!/usr/bin/env python3
"""
Tech Losers Bot - Main Entry Point
Scans for US Technology stocks that dropped >= 5% (close-to-close)
with market cap >= $1B, and sends email report via Gmail.
Usage:
python main.py [--dry-run] [--no-email] [--verify]
Options:
--dry-run Run without sending email, print results only
--no-email Skip email sending (same as --dry-run)
--verify Enable Twelve Data verification (requires API key)
--status Show configuration and cache status
"""
import sys
import argparse
import socket
import time
from datetime import datetime
from typing import List
from src.config import (
validate_config,
print_config_status,
FMP_API_KEY,
TWELVE_DATA_API_KEY,
DATA_DIR,
get_now_sgt,
)
from src.provider_router import ProviderRouter, FilteredStock
from src.gmail_sender import GmailSender
from src.cache import ProfileCache
def print_local_setup_help():
print("\nLocal setup (copy/paste your keys):")
print(" 1) Create .env from template:")
print(" cp .env.example .env")
print(" 2) Open .env and paste values after '=':")
print(" nano .env")
print(" # or on macOS")
print(" open -e .env")
print(" 3) Re-run:")
print(" python3 main.py --status")
print("Docs: README.md and LOCAL_SETUP.md")
def wait_for_network(check_interval: int = 300):
"""
Wait for network connectivity, checking every 5 minutes until next day.
Args:
check_interval: Time between checks in seconds (default 300 = 5 min)
"""
print("\n[Network] Checking internet connectivity...")
start_time = get_now_sgt()
start_date = start_time.date()
attempt = 0
while True:
attempt += 1
current_time = get_now_sgt()
current_date = current_time.date()
# Stop checking if we've moved to the next day
if current_date > start_date:
print(f"[Network] Moved to next day - proceeding without connection")
return False
try:
# Try to reach Google DNS (reliable and fast)
socket.create_connection(("8.8.8.8", 53), timeout=3)
elapsed = (current_time - start_time).total_seconds()
print(f"[Network] ✓ Connected (attempt {attempt}, after {int(elapsed)}s)")
return True
except (socket.timeout, socket.error):
elapsed = (current_time - start_time).total_seconds()
print(f"[Network] No connection yet... ({int(elapsed)}s elapsed, attempt {attempt})")
print(f"[Network] Waiting {check_interval}s before next check...")
time.sleep(check_interval)
def parse_args():
"""Parse command line arguments."""
parser = argparse.ArgumentParser(
description="Scan for US Technology stocks with large daily losses"
)
parser.add_argument(
'--dry-run', '--no-email',
action='store_true',
dest='dry_run',
help='Run without sending email'
)
parser.add_argument(
'--verify',
action='store_true',
help='Enable verification with Twelve Data'
)
parser.add_argument(
'--status',
action='store_true',
help='Show configuration and cache status'
)
parser.add_argument(
'--clear-cache',
action='store_true',
help='Clear expired cache entries'
)
parser.add_argument(
'--no-wait',
action='store_true',
help='Skip network wait check'
)
parser.add_argument(
'--sample-size',
type=int,
default=0,
help='Limit scan universe size (useful for faster dry runs)'
)
parser.add_argument(
'--scan-source',
choices=['td', 'fmp', 'fmp-quotes', 'hybrid'],
default='td',
help='Scan source: td (Twelve Data), fmp (FMP biggest losers feed), fmp-quotes (single-symbol FMP quotes), or hybrid (FMP+TD split)'
)
return parser.parse_args()
def print_results(stocks: List[FilteredStock]):
"""Print formatted results to console."""
if not stocks:
print("\n" + "=" * 60)
print("No stocks met all criteria today")
print("=" * 60)
return
print("\n" + "=" * 80)
print("RESULTS: Technology Losers <= -5% with Market Cap >= $1B")
print("=" * 80)
print(f"\n{'Rank':<6}{'Symbol':<10}{'Company':<35}{'% Change':<12}{'Market Cap':>18}")
print("-" * 80)
for i, stock in enumerate(stocks, 1):
print(
f"{i:<6}"
f"{stock.symbol:<10}"
f"{stock.company_name[:33]:<35}"
f"{stock.change_percentage:>+8.2f}% "
f"${stock.market_cap:>15,.0f}"
)
print("-" * 80)
print(f"Total: {len(stocks)} stocks")
print()
def show_status():
"""Show configuration and cache status."""
print_config_status()
# Cache stats
cache = ProfileCache()
stats = cache.stats()
print("\nCache Status:")
print(f" Total entries: {stats['total_entries']}")
print(f" Valid entries: {stats['valid_entries']}")
print(f" Expired entries: {stats['expired_entries']}")
print(f" Cache duration: {stats['cache_days']} days")
def mark_day_completed(run_timestamp: datetime, stocks_count: int, email_sent: bool):
"""Write a completion marker file for the run date."""
date_str = run_timestamp.strftime("%Y-%m-%d")
marker_path = DATA_DIR / f"run_completed_{date_str}.txt"
status = "sent" if email_sent else "not_sent"
contents = [
f"date={date_str}",
f"timestamp={run_timestamp.strftime('%Y-%m-%d %H:%M:%S')}",
f"stocks={stocks_count}",
f"email={status}",
]
marker_path.write_text("\n".join(contents) + "\n")
print(f"[Run] Marked completed: {marker_path}")
def main():
"""Main entry point."""
args = parse_args()
run_timestamp = get_now_sgt()
# Show status if requested
if args.status:
show_status()
return 0
# Clear cache if requested
if args.clear_cache:
cache = ProfileCache()
cache.clear_expired()
print("Expired cache entries cleared")
return 0
# Skip runs on Sunday and Monday
if run_timestamp.weekday() in (6, 0):
print("[Schedule] Skipping run on Sunday and Monday.")
return 0
# Wait for network connectivity before proceeding (checks every 5 min until next day)
if not args.no_wait:
wait_for_network(check_interval=300)
else:
print("[Network] Skipping network wait (--no-wait)")
# Validate configuration
errors = validate_config()
if errors and not args.dry_run:
print("Configuration errors:")
for error in errors:
print(f" - {error}")
print_local_setup_help()
print("\nTip: use --dry-run to skip email sending (SMTP), but FMP_API_KEY is still required to scan.")
return 1
print(f"\n{'='*60}")
print(f"Tech Losers Bot - {run_timestamp.strftime('%Y-%m-%d %H:%M:%S')}")
print(f"{'='*60}")
# Check API keys
if not FMP_API_KEY:
print("\n[Error] FMP_API_KEY is required!")
print("Get your free API key at: https://financialmodelingprep.com/developer/docs/")
print_local_setup_help()
return 1
# Initialize provider router
enable_verification = args.verify and bool(TWELVE_DATA_API_KEY)
if args.verify and not TWELVE_DATA_API_KEY:
print("\n[Warning] --verify requested but TWELVE_DATA_API_KEY not set")
print("Continuing without verification...")
sample_size = args.sample_size if args.sample_size and args.sample_size > 0 else None
router = ProviderRouter(
enable_verification=enable_verification,
max_verification_checks=20,
sample_size=sample_size,
scan_source=args.scan_source
)
# Run the scan
try:
stocks = router.get_tech_losers()
except Exception as e:
print(f"\n[Error] Scan failed: {e}")
import traceback
traceback.print_exc()
return 1
# Gather statistics from ProviderRouter (real values, no placeholders)
stats = router.stats
stats['total_scanned'] = int(stats.get('total_scanned', 0) or 0)
stats['total_losers'] = int(stats.get('total_losers', 0) or 0)
# Step 1 already returns symbols at/under the % threshold, so this equals losers count.
stats['after_percent_filter'] = stats['total_losers']
stats['after_sector_mcap_filter'] = len(stocks)
# Print results
print_results(stocks)
# Print stats
print("\nRun Statistics:")
print(f" Total scanned: {stats.get('total_scanned', 0)}")
print(f" Candidates from losers feed: {stats.get('total_losers', 0)}")
print(f" After sector + mcap filter: {stats.get('after_sector_mcap_filter', len(stocks))}")
print(f" FMP API requests: {stats.get('fmp_requests', 'N/A')}")
if 'twelve_data_credits' in stats:
td_credits = stats['twelve_data_credits']
print(f" Twelve Data credits used today: {td_credits.get('daily_used', 0)}")
print(f" Twelve Data credits remaining: {td_credits.get('daily_remaining', 0)}")
print(f" Verification used: {stats.get('verification_used', False)}")
# Send email (unless dry run). Send even with zero final matches so daily summary is visible.
email_sent = False
if not args.dry_run:
print("\n[Email] Preparing to send report...")
gmail = GmailSender()
stock_dicts = [s.to_dict() for s in stocks]
email_sent = gmail.send_report(
stocks=stock_dicts,
stats=stats,
run_timestamp=run_timestamp
)
if email_sent:
print("[Email] Report sent successfully!")
else:
print("[Email] Failed to send report")
return 1
elif args.dry_run:
print("\n[Dry Run] Email not sent (--dry-run mode)")
mark_day_completed(run_timestamp, len(stocks), email_sent)
return 0
if __name__ == "__main__":
sys.exit(main())