omniverse1 commited on
Commit
ac24887
·
verified ·
1 Parent(s): 539f8db

Update data_processor.py

Browse files
Files changed (1) hide show
  1. data_processor.py +34 -36
data_processor.py CHANGED
@@ -7,15 +7,15 @@ class DataProcessor:
7
  def __init__(self):
8
  self.fundamentals_cache = {}
9
 
10
- def get_asset_data(self, ticker="GC=F", interval="1d", period="max"):
11
- """Fetch asset data from Yahoo Finance"""
12
  try:
13
- # Map internal intervals to yfinance format
14
  interval_map = {
15
  "5m": "5m",
16
  "15m": "15m",
17
  "30m": "30m",
18
  "1h": "60m",
 
19
  "1d": "1d",
20
  "1wk": "1wk",
21
  "1mo": "1mo",
@@ -24,9 +24,8 @@ class DataProcessor:
24
 
25
  yf_interval = interval_map.get(interval, "1d")
26
 
27
- # Determine appropriate period based on interval
28
- if interval in ["5m", "15m", "30m", "1h"]:
29
- period = "60d" # Intraday data limited to 60 days
30
  elif interval in ["1d"]:
31
  period = "1y"
32
  elif interval in ["1wk"]:
@@ -38,15 +37,14 @@ class DataProcessor:
38
  df = ticker_obj.history(interval=yf_interval, period=period)
39
 
40
  if df.empty:
41
- raise ValueError("No data retrieved from Yahoo Finance")
42
 
43
- # Ensure proper column names
44
  df.columns = [col.capitalize() for col in df.columns]
45
 
46
  return df
47
 
48
  except Exception as e:
49
- print(f"Error fetching data for {ticker} with interval {interval}: {e}")
50
  return pd.DataFrame()
51
 
52
  def calculate_indicators(self, df):
@@ -54,19 +52,23 @@ class DataProcessor:
54
  if df.empty:
55
  return df
56
 
57
- # Simple Moving Averages
 
58
  df['SMA_20'] = df['Close'].rolling(window=20).mean()
59
- df['SMA_50'] = df['Close'].rolling(window=50).mean()
60
 
61
  # Exponential Moving Averages
62
  df['EMA_12'] = df['Close'].ewm(span=12, adjust=False).mean()
63
  df['EMA_26'] = df['Close'].ewm(span=26, adjust=False).mean()
64
 
65
- # MACD
66
  df['MACD'] = df['EMA_12'] - df['EMA_26']
67
  df['MACD_signal'] = df['MACD'].ewm(span=9, adjust=False).mean()
68
  df['MACD_histogram'] = df['MACD'] - df['MACD_signal']
69
 
 
 
 
 
70
  # RSI
71
  delta = df['Close'].diff()
72
  gain = (delta.where(delta > 0, 0)).rolling(window=14).mean()
@@ -89,43 +91,41 @@ class DataProcessor:
89
  df['ATR'] = true_range.rolling(window=14).mean()
90
 
91
  # Volume indicators
92
- if 'Volume' in df.columns:
93
- df['Volume_SMA'] = df['Volume'].rolling(window=20).mean()
94
- df['Volume_ratio'] = df['Volume'] / df['Volume_SMA']
 
 
 
 
 
 
 
 
95
 
96
  return df
97
 
98
  def get_fundamental_data(self, ticker="GC=F"):
99
- """Get fundamental market data"""
100
  try:
101
- ticker_obj = yf.Ticker(ticker)
102
- info = ticker_obj.info
103
-
104
- # Asset-specific fundamentals
105
  if ticker == "BTC-USD":
106
- market_cap = info.get('marketCap', 0)
107
  fundamentals = {
108
- "Strength Index": round(np.random.uniform(30, 80), 1),
109
- "Market Cap": f"${market_cap:,.0f}" if market_cap else "N/A",
110
- "24h Volume": f"${np.random.uniform(20, 80):.1f}B",
111
- "Volatility": f"{np.random.uniform(40, 120):.1f}%",
112
- "Network Hash Rate": f"{np.random.uniform(300, 600):.0f} EH/s",
113
- "Active Addresses": f"{np.random.uniform(500000, 1000000):,.0f}",
114
  "Market Sentiment": np.random.choice(["Bullish", "Neutral", "Bearish"]),
115
- "Institutional Adoption": np.random.choice(["High", "Medium", "Low"]),
116
- "Mining Difficulty Trend": np.random.choice(["Increasing", "Stable", "Decreasing"])
117
  }
118
- else: # Gold
119
  fundamentals = {
120
- "Strength Index": round(np.random.uniform(30, 80), 1),
121
- "Dollar Index": round(np.random.uniform(90, 110), 1),
122
  "Real Interest Rate": f"{np.random.uniform(-2, 5):.2f}%",
123
  "Gold Volatility": f"{np.random.uniform(10, 40):.1f}%",
124
  "Commercial Hedgers (Net)": f"{np.random.uniform(-50000, 50000):,.0f}",
125
  "Managed Money (Net)": f"{np.random.uniform(-100000, 100000):,.0f}",
126
  "Market Sentiment": np.random.choice(["Bullish", "Neutral", "Bearish"]),
127
- "Central Bank Demand": np.random.choice(["High", "Medium", "Low"]),
128
- "Jewelry Demand Trend": np.random.choice(["Increasing", "Stable", "Decreasing"])
129
  }
130
 
131
  return fundamentals
@@ -139,11 +139,9 @@ class DataProcessor:
139
  if df.empty or len(df) < lookback:
140
  return None
141
 
142
- # Use close prices and normalize
143
  prices = df['Close'].iloc[-lookback:].values
144
  prices = prices.astype(np.float32)
145
 
146
- # Normalize to help model performance
147
  mean = np.mean(prices)
148
  std = np.std(prices)
149
  normalized = (prices - mean) / (std + 1e-8)
 
7
  def __init__(self):
8
  self.fundamentals_cache = {}
9
 
10
+ def get_market_data(self, ticker="GC=F", interval="1d"):
11
+ """Fetch market data from Yahoo Finance for a given ticker"""
12
  try:
 
13
  interval_map = {
14
  "5m": "5m",
15
  "15m": "15m",
16
  "30m": "30m",
17
  "1h": "60m",
18
+ "4h": "240m",
19
  "1d": "1d",
20
  "1wk": "1wk",
21
  "1mo": "1mo",
 
24
 
25
  yf_interval = interval_map.get(interval, "1d")
26
 
27
+ if interval in ["5m", "15m", "30m", "1h", "4h"]:
28
+ period = "60d"
 
29
  elif interval in ["1d"]:
30
  period = "1y"
31
  elif interval in ["1wk"]:
 
37
  df = ticker_obj.history(interval=yf_interval, period=period)
38
 
39
  if df.empty:
40
+ raise ValueError(f"No data retrieved from Yahoo Finance for {ticker}")
41
 
 
42
  df.columns = [col.capitalize() for col in df.columns]
43
 
44
  return df
45
 
46
  except Exception as e:
47
+ print(f"Error fetching data for {ticker}: {e}")
48
  return pd.DataFrame()
49
 
50
  def calculate_indicators(self, df):
 
52
  if df.empty:
53
  return df
54
 
55
+ # Simple Moving Averages (5, 20 as requested)
56
+ df['SMA_5'] = df['Close'].rolling(window=5).mean()
57
  df['SMA_20'] = df['Close'].rolling(window=20).mean()
 
58
 
59
  # Exponential Moving Averages
60
  df['EMA_12'] = df['Close'].ewm(span=12, adjust=False).mean()
61
  df['EMA_26'] = df['Close'].ewm(span=26, adjust=False).mean()
62
 
63
+ # MACD (12, 26, 9)
64
  df['MACD'] = df['EMA_12'] - df['EMA_26']
65
  df['MACD_signal'] = df['MACD'].ewm(span=9, adjust=False).mean()
66
  df['MACD_histogram'] = df['MACD'] - df['MACD_signal']
67
 
68
+ # Split histogram into positive and negative for plotting
69
+ df['MACD_bar_positive'] = df['MACD_histogram'].where(df['MACD_histogram'] > 0, 0)
70
+ df['MACD_bar_negative'] = df['MACD_histogram'].where(df['MACD_histogram'] < 0, 0)
71
+
72
  # RSI
73
  delta = df['Close'].diff()
74
  gain = (delta.where(delta > 0, 0)).rolling(window=14).mean()
 
91
  df['ATR'] = true_range.rolling(window=14).mean()
92
 
93
  # Volume indicators
94
+ df['Volume_SMA'] = df['Volume'].rolling(window=20).mean()
95
+ df['Volume_ratio'] = df['Volume'] / df['Volume_SMA']
96
+
97
+ # Stochastic Oscillator (14, 3)
98
+ low_14 = df['Low'].rolling(window=14).min()
99
+ high_14 = df['High'].rolling(window=14).max()
100
+ df['%K'] = 100 * (df['Close'] - low_14) / (high_14 - low_14)
101
+ df['%D'] = df['%K'].rolling(window=3).mean()
102
+ df['%SD'] = df['%D'].rolling(window=3).mean()
103
+ df['UL'] = 70 # Upper limit
104
+ df['DL'] = 30 # Lower limit
105
 
106
  return df
107
 
108
  def get_fundamental_data(self, ticker="GC=F"):
109
+ """Get fundamental gold market data (now generalized/mocked)"""
110
  try:
 
 
 
 
111
  if ticker == "BTC-USD":
 
112
  fundamentals = {
113
+ "Crypto Volatility Index": round(np.random.uniform(50, 150), 1),
114
+ "Dominance Index": f"{np.random.uniform(40, 60):.2f}%",
115
+ "Fear & Greed Index": np.random.choice(["Extreme Fear", "Fear", "Neutral", "Greed", "Extreme Greed"]),
116
+ "Hash Rate Trend": np.random.choice(["Increasing", "Stable", "Decreasing"]),
117
+ "Institutional Flow (Net)": f"{np.random.uniform(-100, 100):,.0f}M USD",
 
118
  "Market Sentiment": np.random.choice(["Bullish", "Neutral", "Bearish"]),
 
 
119
  }
120
+ else:
121
  fundamentals = {
122
+ "Gold Strength Index": round(np.random.uniform(30, 80), 1),
123
+ "Dollar Index (DXY)": round(np.random.uniform(90, 110), 1),
124
  "Real Interest Rate": f"{np.random.uniform(-2, 5):.2f}%",
125
  "Gold Volatility": f"{np.random.uniform(10, 40):.1f}%",
126
  "Commercial Hedgers (Net)": f"{np.random.uniform(-50000, 50000):,.0f}",
127
  "Managed Money (Net)": f"{np.random.uniform(-100000, 100000):,.0f}",
128
  "Market Sentiment": np.random.choice(["Bullish", "Neutral", "Bearish"]),
 
 
129
  }
130
 
131
  return fundamentals
 
139
  if df.empty or len(df) < lookback:
140
  return None
141
 
 
142
  prices = df['Close'].iloc[-lookback:].values
143
  prices = prices.astype(np.float32)
144
 
 
145
  mean = np.mean(prices)
146
  std = np.std(prices)
147
  normalized = (prices - mean) / (std + 1e-8)