-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
44 lines (39 loc) · 1.19 KB
/
Copy pathmain.py
File metadata and controls
44 lines (39 loc) · 1.19 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
from dotenv import load_dotenv
from alpha_vantage.timeseries import TimeSeries
import os
load_dotenv()
api_key = os.getenv("ALPHA_VANTAGE_KEY")
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from sklearn.linear_model import LinearRegression
ts = TimeSeries(key=api_key, output_format='pandas')
data, meta_data = ts.get_daily(symbol='AAPL', outputsize='compact')
data = data.rename(columns={
'1. open': 'Open',
'2. high': 'High',
'3. low': 'Low',
'4. close': 'Close',
'5. volume': 'Volume'
})
data = data.sort_index() # Ensure data is in time order (oldest first)
data = data[['Close']].dropna()
# Add time index for regression
data['TimeIndex'] = np.arange(len(data))
X = data[['TimeIndex']]
y = data['Close']
# Train Linear Regression model
model = LinearRegression()
model.fit(X, y)
data['Predicted_Close'] = model.predict(X)
# Plot results
plt.figure(figsize=(10, 5))
plt.plot(data['Close'], label='Actual Close Price')
plt.plot(data['Predicted_Close'], label='Predicted Trend', linestyle='--')
plt.title(f'{'AAPL'} Price Prediction using Linear Regression')
plt.xlabel('Days')
plt.ylabel('Price (USD)')
plt.legend()
plt.grid(True)
plt.tight_layout()
plt.show()