CodeAlpha Python Programming Internship | Task 2
A stock tracker that calculates total investment value, current value, and profit/loss based on manually defined stock prices.
| File | Purpose |
|---|---|
main.py |
Main script โ stock tracker with menu |
portfolio_<timestamp>.csv |
Auto-generated output file with portfolio summary |
No external libraries needed. Uses only built-in Python modules.
python main.py
==================================================
Stock Portfolio Tracker
==================================================
1. Add stock
2. View portfolio (with profit/loss)
3. Remove/sell stock
4. Save portfolio to CSV
5. Exit
- Add stock โ enter symbol, quantity, and your buy price (or press Enter to use the current market price). Adding more shares of an existing stock automatically calculates a weighted average buy price.
- View portfolio โ shows quantity, buy price, current price, invested amount, current value, and profit/loss (in $ and %) for each stock, plus an overall total.
- Remove/sell stock โ sell part or all of your shares of a stock.
- Save portfolio to CSV โ exports the full breakdown including profit/loss to a
.csvfile. - Exit โ quit the program.
| Symbol | Price ($) |
|---|---|
| AAPL | 180 |
| TSLA | 250 |
| GOOGL | 140 |
| MSFT | 380 |
| AMZN | 185 |
| META | 490 |
| NFLX | 620 |
| NVDA | 875 |
| AMD | 165 |
| INTC | 35 |
================================================================================
PORTFOLIO SUMMARY
================================================================================
Symbol Qty Buy $ Cur $ Invested Value P/L P/L %
--------------------------------------------------------------------------------
AAPL 5 170.00 180.00 850.00 900.00 + 50.00 + 5.88%
TSLA 5 250.00 250.00 1,250.00 1,250.00 + 0.00 + 0.00%
--------------------------------------------------------------------------------
TOTAL 2,100.00 2,150.00 + 50.00 + 2.38%
================================================================================
[+] Overall: You're up $50.00 (2.38%)
dictionaryโ hardcoded current stock pricesinput/outputโ user enters stock symbol, quantity, and buy pricebasic arithmeticโ invested amount, current value, profit/loss calculationsfile handlingโ saves portfolio summary (with P/L) to.csvfile
- Profit/Loss tracking โ compares your buy price against current market price to show gains/losses per stock and overall, in both dollar amount and percentage.
- Weighted average buy price โ adding more shares of a stock you already own recalculates your average cost basis automatically.
- Sell/remove option โ sell part or all of a holding directly from the menu.
- Detailed CSV export โ saved file includes buy price, current price, invested amount, value, and P/L columns.