-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathyf_tool.py
61 lines (46 loc) · 1.97 KB
/
yf_tool.py
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
from datetime import datetime, timedelta
import yfinance as yf
def calculate_performance(symbol, days_ago):
ticker = yf.Ticker(symbol)
end_date = datetime.now()
start_date = end_date - timedelta(days=days_ago)
start_date = start_date.strftime('%Y-%m-%d')
end_date = end_date.strftime('%Y-%m-%d')
historical_data = ticker.history(start=start_date, end=end_date)
old_price = historical_data['Close'].iloc[0]
new_price = historical_data['Close'].iloc[-1]
percent_change = ((new_price - old_price) / old_price) * 100
return round(percent_change, 2)
def get_best_performing(stocks, days_ago):
best_stock = None
best_performance = None
for stock in stocks:
try:
performance = calculate_performance(stock, days_ago)
if best_performance is None or performance > best_performance:
best_stock = stock
best_performance = performance
except Exception as e:
print(f"Could not calculate performance for {stock}: {e}")
return best_stock, best_performance
def get_stock_price(symbol):
ticker = yf.Ticker(symbol)
todays_data = ticker.history(period='1d')
return round(todays_data['Close'][0], 2)
def get_price_change_percent(symbol, days_ago):
ticker = yf.Ticker(symbol)
# Get today's date
end_date = datetime.now()
# Get the date N days ago
start_date = end_date - timedelta(days=days_ago)
# Convert dates to string format that yfinance can accept
start_date = start_date.strftime('%Y-%m-%d')
end_date = end_date.strftime('%Y-%m-%d')
# Get the historical data
historical_data = ticker.history(start=start_date, end=end_date)
# Get the closing price N days ago and today's closing price
old_price = historical_data['Close'].iloc[0]
new_price = historical_data['Close'].iloc[-1]
# Calculate the percentage change
percent_change = ((new_price - old_price) / old_price) * 100
return round(percent_change, 2)