-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_charts.py
More file actions
81 lines (70 loc) · 2.69 KB
/
generate_charts.py
File metadata and controls
81 lines (70 loc) · 2.69 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
import os
import yaml
import pandas as pd
import yfinance as yf
from chart_utils import get_company_name, PatternDetector, plot_with_patterns_and_legend, plot_simple_chart, generate_html_file_list
# ----------------------------
# Load symbols and config
# ----------------------------
with open("symbols.yaml", "r") as f:
config = yaml.safe_load(f)
symbols = config["symbols"]
os.makedirs("charts", exist_ok=True)
# ----------------------------
# Main loop
# ----------------------------
for symbol in symbols:
print(f"Processing {symbol}...")
# Get company name
company_name = get_company_name(symbol)
print(f"Company: {company_name}")
df = yf.download(
symbol,
period="1y",
interval="1d",
auto_adjust=False,
progress=False,
)
df = df.dropna()
if df.empty:
print(f" No data for {symbol}, skipping.")
continue
# Fix MultiIndex columns
if isinstance(df.columns, pd.MultiIndex):
df.columns = df.columns.get_level_values(0)
# Build mplfinance-safe DataFrame (guaranteed 1-D floats)
clean_df = pd.DataFrame(
{
"Open": df["Open"].to_numpy().astype("float64").ravel(),
"High": df["High"].to_numpy().astype("float64").ravel(),
"Low": df["Low"].to_numpy().astype("float64").ravel(),
"Close": df["Close"].to_numpy().astype("float64").ravel(),
"Volume": df["Volume"].to_numpy().astype("float64").ravel(),
},
index=pd.to_datetime(df.index)
)
for enable_patterns in [True, False]:
print(f" Generating chart for enable_patterns={enable_patterns}...")
# Generate charts based on pattern setting
if enable_patterns:
# Detect all patterns
detector = PatternDetector(clean_df)
patterns = [
detector.detect_head_shoulders(),
detector.detect_double_top_bottom(),
detector.detect_triangle(),
detector.detect_flag_pennant(),
detector.detect_cup_handle(),
detector.detect_price_channels(),
detector.detect_undercut_rally(),
detector.detect_regime_start(),
detector.detect_threat_line()
]
# Plot with patterns and company name
plot_with_patterns_and_legend(clean_df, symbol, company_name, patterns)
else:
# Plot simple chart with company name
plot_simple_chart(clean_df, symbol, company_name)
print(f"{symbol}: Simple chart generated")
# Generate HTML index of all charts in the docs folder
generate_html_file_list("charts", "docs/allcharts.html")