Start project
This commit is contained in:
parent
bf096609e4
commit
c7a08aba96
4 changed files with 76 additions and 2 deletions
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
.env
|
||||||
|
__pycache__/
|
||||||
|
.venv
|
||||||
|
data/
|
||||||
13
README.md
13
README.md
|
|
@ -1,3 +1,12 @@
|
||||||
# stock-finance-analyzer
|
# Stock Finance Analyzer
|
||||||
|
|
||||||
A small Python finance project that fetches historical stock prices from Alpaca and calculates some indicators.
|
A small Python finance project that fetches historical stock prices from Alpaca and calculates:
|
||||||
|
|
||||||
|
- highest close price
|
||||||
|
- lowest close price
|
||||||
|
- average close price
|
||||||
|
- daily returns
|
||||||
|
- average return
|
||||||
|
- 3-day moving average
|
||||||
|
|
||||||
|
This project was built as part of my Python/pandas finance learning path.
|
||||||
3
requirements.txt
Normal file
3
requirements.txt
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
pandas
|
||||||
|
python-dotenv
|
||||||
|
alpaca-py
|
||||||
58
stock_analysis.py
Normal file
58
stock_analysis.py
Normal file
|
|
@ -0,0 +1,58 @@
|
||||||
|
from datetime import datetime
|
||||||
|
import os
|
||||||
|
|
||||||
|
import pandas as pd
|
||||||
|
from dotenv import load_dotenv
|
||||||
|
|
||||||
|
from alpaca.data.historical import StockHistoricalDataClient
|
||||||
|
from alpaca.data.requests import StockBarsRequest
|
||||||
|
from alpaca.data.timeframe import TimeFrame
|
||||||
|
|
||||||
|
|
||||||
|
load_dotenv()
|
||||||
|
|
||||||
|
API_KEY = os.getenv("ALPACA_API_KEY")
|
||||||
|
SECRET_KEY = os.getenv("ALPACA_SECRET_KEY")
|
||||||
|
|
||||||
|
|
||||||
|
def fetch_stock_data(symbol, start_date, end_date):
|
||||||
|
client = StockHistoricalDataClient(API_KEY, SECRET_KEY)
|
||||||
|
|
||||||
|
request = StockBarsRequest(
|
||||||
|
symbol_or_symbols=symbol,
|
||||||
|
timeframe=TimeFrame.Day,
|
||||||
|
start=datetime.fromisoformat(start_date),
|
||||||
|
end=datetime.fromisoformat(end_date),
|
||||||
|
)
|
||||||
|
|
||||||
|
bars = client.get_stock_bars(request)
|
||||||
|
df = bars.df.reset_index()
|
||||||
|
|
||||||
|
return df
|
||||||
|
|
||||||
|
|
||||||
|
def analyze_stock(df):
|
||||||
|
df["Return"] = df["close"].pct_change()
|
||||||
|
df["MA_3"] = df["close"].rolling(window=3).mean()
|
||||||
|
|
||||||
|
print("\nFirst rows:")
|
||||||
|
print(df.head())
|
||||||
|
|
||||||
|
print("\nStock analysis:")
|
||||||
|
print(f"Highest price: {df['close'].max():.2f}")
|
||||||
|
print(f"Lowest price: {df['close'].min():.2f}")
|
||||||
|
print(f"Average price: {df['close'].mean():.2f}")
|
||||||
|
print(f"Average return: {df['Return'].mean():.4f}")
|
||||||
|
|
||||||
|
print("\nClose, Return, MA_3:")
|
||||||
|
print(df[["timestamp", "close", "Return", "MA_3"]])
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
symbol = input("Enter stock symbol, for example AAPL: ").upper()
|
||||||
|
|
||||||
|
start_date = input("Start date YYYY-MM-DD: ")
|
||||||
|
end_date = input("End date YYYY-MM-DD: ")
|
||||||
|
|
||||||
|
df = fetch_stock_data(symbol, start_date, end_date)
|
||||||
|
analyze_stock(df)
|
||||||
Loading…
Add table
Reference in a new issue