File size: 9,331 Bytes
aadd767
 
 
ca56bda
aadd767
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ca56bda
 
 
aadd767
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ca56bda
aadd767
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
import os, json, time, random
from collections import defaultdict
from datetime import date, datetime, timedelta
import gradio as gr

import pandas as pd
import finnhub
from openai import OpenAI

from io import StringIO
import requests


# ---------- 0  CONFIG ---------------------------------------------------------

OPENAI_MODEL  = os.getenv("OPENAI_MODEL", "gpt-4o-mini")
FINNHUB_KEY   = os.getenv("FINNHUB_API_KEY")
ALPHA_KEY = os.getenv("ALPHAVANTAGE_API_KEY")

if not FINNHUB_KEY:
    raise RuntimeError("FINNHUB_API_KEY not set")

client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
finnhub_client = finnhub.Client(api_key=FINNHUB_KEY)


SYSTEM_PROMPT = (
    "You are a seasoned stock-market analyst. "
    "Given recent company news and optional basic financials, "
    "return:\n"
    "[Positive Developments] – 2-4 bullets\n"
    "[Potential Concerns] – 2-4 bullets\n"
    "[Prediction & Analysis] – a one-week price outlook with rationale."
)


# ---------- 1  DATE / UTILITY HELPERS ----------------------------------------

def today() -> str:
    return date.today().strftime("%Y-%m-%d")

def n_weeks_before(date_string: str, n: int) -> str:
    return (datetime.strptime(date_string, "%Y-%m-%d") -
            timedelta(days=7 * n)).strftime("%Y-%m-%d")


# ---------- 2  DATA FETCHING --------------------------------------------------

def get_stock_data(symbol: str, steps: list[str]) -> pd.DataFrame:

    if not ALPHA_KEY:
        raise RuntimeError("ALPHAVANTAGE_API_KEY is Missing")

    # 免费端点:TIME_SERIES_DAILY :contentReference[oaicite:8]{index=8}
    url = (
        "https://www.alphavantage.co/query"
        "?function=TIME_SERIES_DAILY"
        f"&symbol={symbol}"
        f"&apikey={ALPHA_KEY}"
        "&datatype=csv"
        "&outputsize=full"
    )

    # 重试 3 次
    text = None
    for attempt in range(3):
        resp = requests.get(url, timeout=10)
        if not resp.ok:
            time.sleep(1)
            continue
        text = resp.text.strip()
        if text.startswith("{"):
            info = resp.json()
            msg = info.get("Note") or info.get("Error Message") or str(info)
            raise RuntimeError(f"Alpha Vantage Return Error:{msg}")
        break

    if not text:
        raise RuntimeError(f"Alpha Vantage Connection Error:{url}")

    df = pd.read_csv(StringIO(text))
    date_col = "timestamp" if "timestamp" in df.columns else df.columns[0]
    df[date_col] = pd.to_datetime(df[date_col])
    df = df.sort_values(date_col).set_index(date_col)

    data = {"Start Date": [], "End Date": [], "Start Price": [], "End Price": []}
    for i in range(len(steps) - 1):
        s_date = pd.to_datetime(steps[i])
        e_date = pd.to_datetime(steps[i+1])
        seg = df.loc[s_date:e_date]
        if seg.empty:
            raise RuntimeError(
                f"Alpha Vantage 无法获取 {symbol}{steps[i]}{steps[i+1]} 的数据"
            )
        data["Start Date"].append(seg.index[0])
        data["Start Price"].append(seg["close"].iloc[0])
        data["End Date"].append(seg.index[-1])
        data["End Price"].append(seg["close"].iloc[-1])
        # Limits:5 times/min
        time.sleep(12)

    return pd.DataFrame(data)

def current_basics(symbol: str, curday: str) -> dict:
    raw = finnhub_client.company_basic_financials(symbol, "all")
    if not raw["series"]:
        return {}
    merged = defaultdict(dict)
    for metric, vals in raw["series"]["quarterly"].items():
        for v in vals:
            merged[v["period"]][metric] = v["v"]

    latest = max((p for p in merged if p <= curday), default=None)
    if latest is None:
        return {}
    d = dict(merged[latest])
    d["period"] = latest
    return d

def attach_news(symbol: str, df: pd.DataFrame) -> pd.DataFrame:
    news_col = []
    for _, row in df.iterrows():
        start = row["Start Date"].strftime("%Y-%m-%d")
        end   = row["End Date"].strftime("%Y-%m-%d")
        time.sleep(1)                                        # Finnhub QPM guard
        weekly = finnhub_client.company_news(symbol, _from=start, to=end)
        weekly_fmt = [
            {
                "date"    : datetime.fromtimestamp(n["datetime"]).strftime("%Y%m%d%H%M%S"),
                "headline": n["headline"],
                "summary" : n["summary"],
            }
            for n in weekly
        ]
        weekly_fmt.sort(key=lambda x: x["date"])
        news_col.append(json.dumps(weekly_fmt))
    df["News"] = news_col
    return df


# ---------- 3  PROMPT CONSTRUCTION -------------------------------------------

def sample_news(news: list[str], k: int = 5) -> list[str]:
    if len(news) <= k: return news
    return [news[i] for i in sorted(random.sample(range(len(news)), k))]


def make_prompt(symbol: str, df: pd.DataFrame, curday: str, use_basics=False) -> str:
    # Company profile
    prof = finnhub_client.company_profile2(symbol=symbol)
    company_blurb = (
        f"[Company Introduction]:\n{prof['name']} operates in the "
        f"{prof['finnhubIndustry']} sector ({prof['country']}). "
        f"Founded {prof['ipo']}, market cap {prof['marketCapitalization']:.1f} "
        f"{prof['currency']}; ticker {symbol} on {prof['exchange']}.\n"
    )

    # Past weeks block
    past_block = ""
    for _, row in df.iterrows():
        term = "increased" if row["End Price"] > row["Start Price"] else "decreased"
        head = (f"From {row['Start Date']:%Y-%m-%d} to {row['End Date']:%Y-%m-%d}, "
                f"{symbol}'s stock price {term} from "
                f"{row['Start Price']:.2f} to {row['End Price']:.2f}.")
        news_items = json.loads(row["News"])
        summaries  = [
            f"[Headline] {n['headline']}\n[Summary] {n['summary']}\n"
            for n in news_items
            if not n["summary"].startswith("Looking for stock market analysis")
        ]
        past_block += "\n" + head + "\n" + "".join(sample_news(summaries, 5))

    # Optional basic financials
    if use_basics:
        basics = current_basics(symbol, curday)
        if basics:
            basics_txt = "\n".join(f"{k}: {v}" for k, v in basics.items() if k != "period")
            basics_block = (f"\n[Basic Financials] (reported {basics['period']}):\n{basics_txt}\n")
        else:
            basics_block = "\n[Basic Financials]: not available\n"
    else:
        basics_block = "\n[Basic Financials]: not requested\n"

    horizon = f"{curday} to {n_weeks_before(curday, -1)}"
    final_user_msg = (
        company_blurb
        + past_block
        + basics_block
        + f"\nBased on all information before {curday}, analyse positive "
          "developments and potential concerns for {symbol}, then predict its "
          f"price movement for next week ({horizon})."
    )
    return final_user_msg


# ---------- 4  LLM CALL -------------------------------------------------------

def chat_completion(prompt: str,
                    model: str = OPENAI_MODEL,
                    temperature: float = 0.3,
                    stream: bool = False) -> str:

    response = client.chat.completions.create(
        model=model,
        temperature=temperature,
        stream=stream,
        messages=[
            {"role": "system", "content": SYSTEM_PROMPT},
            {"role": "user",   "content": prompt}
        ],
    )

    if stream:
        collected = []
        for chunk in response:
            delta = chunk.choices[0].delta.content or ""
            print(delta, end="", flush=True)
            collected.append(delta)
        print()
        return "".join(collected)

    # without stream
    return response.choices[0].message.content


# ---------- 5  MAIN ENTRY (CLI test) -----------------------------------------

def predict(symbol: str = "AAPL",
            curday: str = today(),
            n_weeks: int = 3,
            use_basics: bool = False,
            stream: bool = False) -> tuple[str, str]:
    steps = [n_weeks_before(curday, n) for n in range(n_weeks + 1)][::-1]
    df    = get_stock_data(symbol, steps)
    df    = attach_news(symbol, df)

    prompt_info = make_prompt(symbol, df, curday, use_basics)
    answer      = chat_completion(prompt_info, stream=stream)

    return prompt_info, answer



# ---------- 6  SETUP HF -----------------------------------------


def hf_predict(symbol, n_weeks, use_basics):
    # 1. get curday
    curday = date.today().strftime("%Y-%m-%d")
    # 2. call predict
    prompt, answer = predict(
        symbol=symbol.upper(),
        curday=curday,
        n_weeks=int(n_weeks),
        use_basics=bool(use_basics),
        stream=False
    )
    return prompt, answer

with gr.Blocks() as demo:
    gr.Markdown("FinRobot_Forecaster")
    with gr.Row():
        symbol = gr.Textbox(label="Ticker(eg. AAPL)", value="AAPL")
        n_weeks = gr.Slider(1, 6, value=3, step=1, label="Trace Back Weeks")
        use_basics = gr.Checkbox(label="Add Basic Financials", value=False)
    output_prompt = gr.Textbox(label="Model Prompt", lines=8)
    output_answer = gr.Textbox(label="Model Output", lines=12)
    btn = gr.Button("Run Forecaster")
    btn.click(fn=hf_predict,
              inputs=[symbol, n_weeks, use_basics],
              outputs=[output_prompt, output_answer])

if __name__ == "__main__":
    demo.launch()