Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions myenv/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import sys
sys.path.append('/path/to/yfinance') # I had trouble importing yfinance at first.
import yfinance as yf # Library where I can fetch stocks data.
import requests # To interact with web services like IFTTT.
import time # Provide functions related to time and delays.

# The event name I set up in my IFTTT applet.
ifttt_event_name = 'stock_price_drop'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Const can be written in all caps :)


# My IFTTT webhook key.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment not necessary, your variable name is clear enough.

ifttt_webhook_key = 'hThx0WApHyDPrZzRnUlxAv0QMkIUfB55haBlJWd5kIC'

# The ticker symbols of the stocks I am monitoring, I can add or remove as needed.
stocks = ['TSLA', 'AAPL', 'MSFT', 'GOOGL', 'NKE']

# This is a dictionary that stores the previous prices of the stocks.
previous_prices = {}
Copy link

@virginiacodes virginiacodes Oct 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add key and value of 0 to your dictionary so it gets updated in your function. {'TSLA': 0, etc...


'''This function fetches the current stock price. I used the parameter stock, so I can get individual stock prices.
I have to index with:.iloc, if not an alert appears, warning that method of indexing may change in a future Pandas version. It suggests using the iloc method to ensure it is compatible with future versions. '''
def get_stock_price(stock):
stock_data = yf.Ticker(stock)
price = stock_data.history(period='1d')['Close'].iloc[0] # I had to add .iloc here (explained above).

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can save a line of code by directly returning line 23 instead of creating a variable

return price

# Similar to above, this time I want price for the last 7 days, I used the mean function to get the average.
def calculate_7_day_average(stock):
stock_data = yf.Ticker(stock)
historical_data = stock_data.history(period='7d')
return historical_data['Close'].mean()

# Check if prices fall by £0.25 or more and notify me using the IFTTT applet, round prices to 2 decimal places.
def send_price_drop_notification(stock, current_price):
notification = {'value1': f'{stock} stock price fell by at least £0.25. Current price: £{current_price:.2f}'}
url = f'https://maker.ifttt.com/trigger/{ifttt_event_name}/with/key/{ifttt_webhook_key}'
response = requests.post(url, json=notification)
print(f'Notification sent: {notification}')
print('£' * 50)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why this line is here. Was it a test?


# Similar to above but will notify me when prices are below the seven day average.
def send_average_drop_notification(stock, current_price, seven_day_average):
notification = {'value2': f'{stock} below 7 day average. 7 day average: {seven_day_average:.2f} Current price: £{current_price:.2f}'}
url = f'https://maker.ifttt.com/trigger/{ifttt_event_name}/with/key/{ifttt_webhook_key}'
response = requests.post(url, json=notification)
print(f'Notification sent: {notification}')
print('*' * 50)

'''By creating a while loop I can run my code every 60 seconds as long as the conditions inside this loop are true. '''
while True:
for stock in stocks:
# Stored my fetch price function in a variable.
current_price = get_stock_price(stock)
print(f'{stock}: {current_price:.2f}')

# Stored my 7 day average function in a variable.
seven_day_average = calculate_7_day_average(stock)
print(f'7-day Average for {stock}: {seven_day_average:.2f}')

# Check if both conditions are met, then notify me.
if stock in previous_prices and current_price <= previous_prices[stock] - 0.25:
send_price_drop_notification(stock, current_price)

# Update the previous price in the dictionary to the current price.
previous_prices[stock] = current_price

# Check if the current price is less than 7 day average and notify me.
if current_price < seven_day_average:
send_average_drop_notification(stock, current_price, seven_day_average)

print('-' * 50)
time.sleep(60)
24 changes: 24 additions & 0 deletions myenv/unit_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import unittest

# Import the function you want to test
from main import calculate_7_day_average

class TestCalculate7DayAverage(unittest.TestCase):
def test_calculate_7_day_average(self):
# Test case 1: Test case with seven figures
test_stock_data_1 = [100.0, 110.0, 95.0, 105.0, 112.0, 108.0, 98.0]
expected_average_1 = 104.57 # Rounded to two decimal places

result_1 = calculate_7_day_average(test_stock_data_1)
self.assertAlmostEqual(result_1, expected_average_1, places=2)

# Test case 2: Test case with eight figures
test_stock_data_2 = [50.0, 60.0, 55.0, 70.0, 65.0, 62.0, 58.0, 50.0]
expected_average_2 = 60.00 # Rounded to two decimal places

result_2 = calculate_7_day_average(test_stock_data_2)
self.assertAlmostEqual(result_2, expected_average_2, places=2)

if __name__ == '__main__':
unittest.main()

24 changes: 24 additions & 0 deletions unit_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import unittest

# Import the function you want to test
from main import calculate_7_day_average

class TestCalculate7DayAverage(unittest.TestCase):
def test_calculate_7_day_average(self):
# Test case 1: Test case with seven figures
test_stock_data_1 = [100.0, 110.0, 95.0, 105.0, 112.0, 108.0, 98.0]
expected_average_1 = 104.57 # Rounded to two decimal places

result_1 = calculate_7_day_average(test_stock_data_1)
self.assertAlmostEqual(result_1, expected_average_1, places=2)

# Test case 2: Test case with eight figures
test_stock_data_2 = [50.0, 60.0, 55.0, 70.0, 65.0, 62.0, 58.0, 50.0]
expected_average_2 = 60.00 # Rounded to two decimal places

result_2 = calculate_7_day_average(test_stock_data_2)
self.assertAlmostEqual(result_2, expected_average_2, places=2)

if __name__ == '__main__':
unittest.main()