-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_loader.py
More file actions
54 lines (40 loc) · 1.65 KB
/
Copy pathdata_loader.py
File metadata and controls
54 lines (40 loc) · 1.65 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
import yfinance as yf
import pandas as pd
import numpy as np
from ta.momentum import RSIIndicator
from ta.trend import MACD
from ta.volatility import BollingerBands
def download_stock_data(tickers, start_date, end_date):
data = yf.download(tickers, start=start_date, end=end_date, group_by='ticker')
return data
def calculate_technical_indicators(df):
rsi = RSIIndicator(df['Close'], window=14).rsi()
macd = MACD(df['Close'])
bb = BollingerBands(df['Close'], window=20)
df['RSI'] = rsi
df['MACD'] = macd.macd()
df['BB_high'] = bb.bollinger_hband()
df['BB_low'] = bb.bollinger_lband()
return df
def prepare_data(tickers, start_date, end_date):
raw_data = download_stock_data(tickers, start_date, end_date)
processed_dfs = []
for ticker in tickers:
if len(tickers) > 1:
stock_data = raw_data[ticker].copy()
else:
stock_data = raw_data.copy()
stock_data = calculate_technical_indicators(stock_data)
stock_data = stock_data[['Close', 'RSI', 'MACD', 'BB_high', 'BB_low']].copy()
stock_data.columns = [f'{ticker}_{col}' for col in stock_data.columns]
processed_dfs.append(stock_data)
combined_df = pd.concat(processed_dfs, axis=1)
combined_df = combined_df.dropna()
for col in combined_df.columns:
combined_df[col] = (combined_df[col] - combined_df[col].mean()) / combined_df[col].std()
return combined_df
def split_data(df, train_ratio=0.8):
split_idx = int(len(df) * train_ratio)
train_df = df.iloc[:split_idx]
test_df = df.iloc[split_idx:]
return train_df, test_df