Skip to content

Commit 4948720

Browse files
committed
Stock Market Exchange
1 parent 2dbf735 commit 4948720

File tree

10 files changed

+284
-6
lines changed

10 files changed

+284
-6
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ node_modules/
55
__pycache__/
66
.env/
77
# Ignore Python virtual environments
8-
Stock Market Exchange/.env/
8+
Stock_Market_Exchange/.env/
9+
Stock_Market_Exchange/Easy/.env

README.md

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,55 @@
1-
# pythonproject
1+
# Python Codes
2+
3+
This repository contains various Python projects and exercises, organized by topic and difficulty. Each subdirectory represents a different project or challenge, ranging from beginner to advanced levels.
4+
5+
## Repository Structure
6+
7+
- **Stock_Market_Exchange/**
8+
Contains projects related to stock market data analysis, news fetching, and SMS notifications using APIs such as Alpha Vantage, NewsAPI, and Twilio.
9+
- **Easy/**:
10+
Beginner-level implementation for fetching stock prices and related news articles.
11+
- **Intermediate/**:
12+
Intermediate-level project with more advanced logic for stock and news integration.
13+
- **Hard/**:
14+
Advanced project structure for comprehensive stock market and news analysis.
15+
- Other directories may contain additional Python projects or scripts.
16+
17+
## Getting Started
18+
19+
1. **Clone the repository:**
20+
```sh
21+
git clone <repository-url>
22+
cd "Python Codes"
23+
```
24+
2. **Set up virtual environment (optional but recommended):**
25+
```sh
26+
python -m venv venv
27+
source venv/bin/activate # On Windows: venv\Scripts\activate
28+
```
29+
3. **Install dependencies:**
30+
Each project may have its own requirements. Check for a `requirements.txt` file in the respective directory and install with:
31+
```sh
32+
pip install -r requirements.txt
33+
```
34+
4. **Set up environment variables:**
35+
Some projects require API keys. Create a `.env` file in the project directory and add your API credentials as shown in the sample `.env` files.
36+
37+
## Requirements
38+
39+
- Python 3.x
40+
- `requests` library
41+
- `python-dotenv` for environment variable management
42+
- API keys for [Alpha Vantage](https://www.alphavantage.co/), [NewsAPI](https://newsapi.org/), and [Twilio](https://www.twilio.com/) (for relevant projects)
43+
44+
## Usage
45+
46+
Navigate to the desired project directory and run the main script. For example:
47+
```sh
48+
cd Stock_Market_Exchange/Easy
49+
python main.py
50+
```
51+
Follow the instructions in each project's README or comments for specific usage details.
52+
53+
## License
54+
55+
This repository is provided for educational purposes. Please check individual project directories for specific license information if applicable.

Stock_Market_Exchange/.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
api = "0CDA2IATNBQB8Q5Z"
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Stock Market Exchange - Easy
2+
3+
This project demonstrates how to fetch stock prices and related news articles using public APIs.
4+
5+
## Features
6+
7+
- Fetches daily stock prices for a given symbol using Alpha Vantage.
8+
- Calculates percentage change between consecutive days.
9+
- If the change exceeds 5%, fetches the latest news articles about the company using NewsAPI.
10+
11+
## Requirements
12+
13+
- Python 3.x
14+
- `requests`
15+
- `python-dotenv`
16+
- Alpha Vantage API key
17+
- NewsAPI key
18+
19+
## Setup
20+
21+
1. Install dependencies:
22+
```sh
23+
pip install requests python-dotenv
24+
```
25+
2. Create a `.env` file with your API keys:
26+
```
27+
API_KEY=your_alpha_vantage_key
28+
news_api_key=your_newsapi_key
29+
```
30+
3. Run the script:
31+
```sh
32+
python main.py
33+
```
34+
35+
## License
36+
37+
For educational purposes only.

Stock_Market_Exchange/Easy/main.py

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,73 @@
11
import requests
2+
import os
3+
from dotenv import load_dotenv
4+
5+
"load .env folder"
6+
load_dotenv()
27

38
STOCK_NAME = "TSLA"
49
COMPANY_NAME = "Tesla Inc"
10+
API_KEY=os.getenv("API_KEY")
511

6-
STOCK_ENDPOINT = "https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=COMPANY_NAME&interval=5min&apikey=0CDA2IATNBQB8Q5Z"
12+
# You can now use api_key in your application
13+
STOCK_ENDPOINT = "https://www.alphavantage.co/query"
714
NEWS_ENDPOINT = "https://newsapi.org/v2/everything"
815

916
## STEP 1: Use https://www.alphavantage.co/documentation/#daily
1017
# When stock price increase/decreases by 5% between yesterday and the day before yesterday then print("Get News").
1118

1219
#TODO 1. - Get yesterday's closing stock price. Hint: You can perform list comprehensions on Python dictionaries. e.g. [new_value for (key, value) in dictionary.items()]
13-
response = requests.get(STOCK_ENDPOINT)
20+
parameter={
21+
"function":"TIME_SERIES_DAILY",
22+
"symbol":"IBM",
23+
"apikey":API_KEY
24+
}
25+
response = requests.get(STOCK_ENDPOINT,params=parameter)
1426
data = response.json()
15-
print(data)
27+
Daily_Time_series=data["Time Series (Daily)"]
28+
specific_data=[value for (key,value) in Daily_Time_series.items()]
29+
yesterday_data=specific_data[0]["4. close"]
30+
print(yesterday_data)
31+
32+
33+
# print(yesterday)
34+
# print(data)
1635
#TODO 2. - Get the day before yesterday's closing stock price
36+
a_day_before_yesterday=specific_data[1]["4. close"]
37+
print(a_day_before_yesterday)
1738

1839
#TODO 3. - Find the positive difference between 1 and 2. e.g. 40 - 20 = -20, but the positive difference is 20. Hint: https://www.w3schools.com/python/ref_func_abs.asp
1940

41+
difference=abs(float(yesterday_data)-float(a_day_before_yesterday))
42+
print(difference)
43+
2044
#TODO 4. - Work out the percentage difference in price between closing price yesterday and closing price the day before yesterday.
2145

46+
difference_percentage = (difference/float(yesterday_data))*100
47+
print("yesterday_percentage :",difference_percentage)
48+
2249
#TODO 5. - If TODO4 percentage is greater than 5 then print("Get News").
50+
if difference_percentage> 5:
51+
print("getnews")
2352

2453
## STEP 2: https://newsapi.org/
2554
# Instead of printing ("Get News"), actually get the first 3 news pieces for the COMPANY_NAME.
2655

2756
#TODO 6. - Instead of printing ("Get News"), use the News API to get articles related to the COMPANY_NAME.
2857

29-
#TODO 7. - Use Python slice operator to create a list that contains the first 3 articles. Hint: https://stackoverflow.com/questions/509211/understanding-slice-notation
58+
news_api_key=os.getenv("news_api_key")
59+
news_parameters={
60+
"q":"IBM",
61+
"from":"2025-06-01",
62+
"sortedBy":"publishedAt",
63+
"apikey":news_api_key
64+
}
65+
news_data=requests.get(NEWS_ENDPOINT,params=news_parameters)
66+
print(news_data.json())
3067

68+
#TODO 7. - Use Python slice operator to create a list that contains the first 3 articles. Hint: https://stackoverflow.com/questions/509211/understanding-slice-notation
69+
first_3=news_data[:2]
70+
print(first_3)
3171

3272
## STEP 3: Use twilio.com/docs/sms/quickstart/python
3373
#to send a separate message with each article's title and description to your phone number.
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Stock Market Exchange - Hard
2+
3+
This advanced project combines stock price monitoring, news aggregation, and SMS notifications.
4+
5+
## Features
6+
7+
- Monitors stock price changes using Alpha Vantage.
8+
- Fetches and formats the top 3 news articles about the company using NewsAPI.
9+
- Sends SMS alerts for significant stock price changes using Twilio.
10+
11+
## Requirements
12+
13+
- Python 3.x
14+
- `requests`
15+
- Alpha Vantage API key
16+
- NewsAPI key
17+
- Twilio account and credentials
18+
19+
## Setup
20+
21+
1. Install dependencies:
22+
```sh
23+
pip install requests
24+
```
25+
2. Add your API keys and Twilio credentials to a `.env` file or directly in the script.
26+
3. Run the script:
27+
```sh
28+
python main.py
29+
```
30+
31+
## License
32+
33+
For educational purposes only.
34+
35+
## Detailed Description
36+
37+
This project is designed to help users keep track of stock market changes and relevant news about specific companies. It uses the Alpha Vantage API to monitor stock price changes, the NewsAPI to fetch the latest news articles, and Twilio to send SMS notifications to users.
38+
39+
### Alpha Vantage
40+
41+
Alpha Vantage provides real-time and historical data on stocks, ETFs, mutual funds, and cryptocurrencies. In this project, we use Alpha Vantage to get the latest stock price and monitor any significant changes.
42+
43+
### NewsAPI
44+
45+
NewsAPI is a simple HTTP REST API for searching and retrieving live articles from all over the web. We use it to fetch the top news articles related to the company whose stock price we are monitoring.
46+
47+
### Twilio
48+
49+
Twilio is a cloud communications platform that allows you to send and receive SMS messages through its web service APIs. We use Twilio to send SMS alerts to users when there is a significant change in the stock price.
50+
51+
## How It Works
52+
53+
1. The user specifies the stock ticker symbol and the threshold for significant price changes.
54+
2. The script fetches the latest stock price from Alpha Vantage.
55+
3. If the price change is significant, the script fetches the top 3 news articles about the company from NewsAPI.
56+
4. The script sends an SMS alert to the user with the stock price change and the top news articles.
57+
58+
## Customization
59+
60+
Users can customize the following parameters in the script:
61+
62+
- `STOCK_TICKER`: The stock ticker symbol for the company you want to monitor.
63+
- `PRICE_CHANGE_THRESHOLD`: The threshold for significant price changes (in percentage).
64+
- `SMS_TO`: The phone number to send SMS alerts to.
65+
- `SMS_FROM`: The Twilio phone number to send SMS alerts from.
66+
67+
## Troubleshooting
68+
69+
- Ensure that you have installed all the required dependencies.
70+
- Check that your API keys and Twilio credentials are correct.
71+
- If you encounter any issues, refer to the documentation for Alpha Vantage, NewsAPI, and Twilio for troubleshooting tips.
72+
73+
## Acknowledgments
74+
75+
- [Alpha Vantage](https://www.alphavantage.co/) for the stock market data.
76+
- [NewsAPI](https://newsapi.org/) for the news articles.
77+
- [Twilio](https://www.twilio.com/) for the SMS notifications.
78+
- [Python](https://www.python.org/) for the programming language.
79+
- [Requests](https://docs.python-requests.org/en/master/) for the HTTP library.
Binary file not shown.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Stock Market Exchange - Intermediate
2+
3+
This project builds on the Easy version by integrating more advanced logic for stock and news analysis.
4+
5+
## Features
6+
7+
- Detects significant stock price changes (5% threshold).
8+
- Fetches the top 3 news articles about the company when a significant change is detected.
9+
- Prepares formatted messages for each news article.
10+
11+
## Requirements
12+
13+
- Python 3.x
14+
- `requests`
15+
- Alpha Vantage API key
16+
- NewsAPI key
17+
18+
## Setup
19+
20+
1. Install dependencies:
21+
```sh
22+
pip install requests
23+
```
24+
2. Add your API keys to the script or a `.env` file as needed.
25+
3. Run the script:
26+
```sh
27+
python main.py
28+
```
29+
30+
## License
31+
32+
For educational purposes only.

Stock_Market_Exchange/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Stock Market Exchange Projects
2+
3+
This directory contains Python projects focused on stock market data analysis, news aggregation, and SMS notifications using various APIs.
4+
5+
## Subdirectories
6+
7+
- **Easy/**:
8+
Beginner-level project for fetching stock prices and related news articles.
9+
- **Intermediate/**:
10+
Intermediate-level project with enhanced logic for integrating stock and news APIs.
11+
- **Hard/**:
12+
Advanced project for comprehensive stock market and news analysis, including SMS notifications.
13+
14+
## Features
15+
16+
- Fetch real-time stock prices using the Alpha Vantage API.
17+
- Retrieve latest news articles related to a company using NewsAPI.
18+
- Send SMS alerts for significant stock price changes using Twilio (in advanced projects).
19+
20+
## Requirements
21+
22+
- Python 3.x
23+
- `requests`
24+
- `python-dotenv`
25+
- API keys for Alpha Vantage, NewsAPI, and Twilio (for relevant features)
26+
27+
## Usage
28+
29+
Navigate to the desired subdirectory and follow the instructions in its `README.md` or main script.
30+
31+
## License
32+
33+
For educational purposes only.

git submodule status.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
git submodule status

0 commit comments

Comments
 (0)