akashraut commited on
Commit
4e1243c
·
verified ·
1 Parent(s): 68a6c1f

Update prices_server.py

Browse files
Files changed (1) hide show
  1. prices_server.py +25 -41
prices_server.py CHANGED
@@ -1,51 +1,35 @@
 
1
  import yfinance as yf
2
- import numpy as np
3
- import pandas as pd
4
- from datetime import timedelta
5
- from mcp.server.fastmcp import FastMCP
6
 
7
- mcp = FastMCP("metal_price_server")
8
 
9
- GOLD_SYMBOL = "GC=F"
10
- SILVER_SYMBOL = "SI=F"
 
 
 
11
 
 
 
12
 
13
- def fetch_data(symbol="XAU"):
14
- ticker_symbol = GOLD_SYMBOL if symbol == "XAU" else SILVER_SYMBOL
15
- ticker = yf.Ticker(ticker_symbol)
16
- hist = ticker.history(period="15d")
17
- if hist.empty:
18
- raise ValueError(f"No data from yfinance for {ticker_symbol}")
19
- return hist.reset_index(), round(hist["Close"].iloc[-1], 2)
20
 
 
 
 
21
 
22
- @mcp.tool()
23
- def get_report(symbol: str = "XAU") -> str:
24
- """
25
- Generates live price + 3-day forecast for Gold (XAU) or Silver (XAG).
26
- """
27
- df, live_price = fetch_data(symbol)
28
- df["day"] = np.arange(len(df))
29
- coef = np.polyfit(df["day"], df["Close"], 1)
30
- model = np.poly1d(coef)
31
-
32
- future_days = np.arange(len(df), len(df) + 3)
33
- preds = model(future_days)
34
-
35
- last_date = pd.to_datetime(df["Date"].iloc[-1])
36
- future_dates = [(last_date + timedelta(days=i + 1)).strftime("%Y-%m-%d") for i in range(3)]
37
- forecast = list(zip(future_dates, np.round(preds, 2)))
38
-
39
- name = "Gold" if symbol == "XAU" else "Silver"
40
- report = f"📊 **{name} Price Report**\n\n"
41
- report += f"**Live Price:** ${live_price} per ounce\n\n"
42
- report += "**3-Day Forecast:**\n"
43
- for date, price in forecast:
44
- report += f"- {date}: ${price}\n"
45
-
46
- return report
47
 
 
 
 
 
48
 
49
  if __name__ == "__main__":
50
- print("✅ prices_server.py running with yfinance + MCP.")
51
- mcp.run()
 
1
+ # prices_server.py
2
  import yfinance as yf
3
+ from datetime import datetime, timedelta
4
+ from fastmcp import FastMCP, tool
 
 
5
 
6
+ app = FastMCP("price_server")
7
 
8
+ @tool
9
+ def get_gold_price():
10
+ """Get live and 7-day predicted gold price using Yahoo Finance"""
11
+ ticker = yf.Ticker("GC=F") # Gold futures
12
+ hist = ticker.history(period="30d", interval="1d")
13
 
14
+ # Live price
15
+ live_price = hist["Close"].iloc[-1]
16
 
17
+ # Simple 7-day forecast (trend-based)
18
+ hist["Returns"] = hist["Close"].pct_change()
19
+ avg_return = hist["Returns"].mean()
20
+ predicted_prices = []
21
+ current_price = live_price
 
 
22
 
23
+ for i in range(7):
24
+ current_price *= (1 + avg_return)
25
+ predicted_prices.append(round(current_price, 2))
26
 
27
+ dates = [(datetime.now() + timedelta(days=i + 1)).strftime("%Y-%m-%d") for i in range(7)]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
+ return {
30
+ "live_price": round(live_price, 2),
31
+ "predicted": dict(zip(dates, predicted_prices))
32
+ }
33
 
34
  if __name__ == "__main__":
35
+ app.run()