Created
February 23, 2024 20:47
-
-
Save mikecabana/8b3888e2eb513b92fd2e3a4d939f044b to your computer and use it in GitHub Desktop.
Terminal stock ticker - generated by Chat GPT 3.5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import platform | |
import yfinance as yf | |
import time | |
import sys | |
import warnings | |
def clear_terminal(): | |
# Clear terminal screen based on the operating system | |
if platform.system() == 'Windows': | |
os.system('cls') | |
else: | |
os.system('clear') | |
def get_stock_price(ticker): | |
try: | |
stock = yf.Ticker(ticker) | |
price = stock.history(period='1d')['Close'][0] | |
return round(price,2) | |
except Exception as e: | |
return f"Error fetching stock price for {ticker}: {str(e)}" | |
def main(): | |
# Disable specific types of warnings | |
warnings.simplefilter(action="ignore", category=FutureWarning) | |
warnings.simplefilter(action="ignore", category=KeyboardInterrupt) | |
clear_terminal() | |
ticker = input("Enter stock ticker symbol: ") | |
clear_terminal() | |
print(ticker.upper()) | |
previous_price = None | |
while True: | |
try: | |
price = get_stock_price(ticker) | |
if previous_price is not None: | |
if price < previous_price: | |
sys.stdout.write(f"\r\033[91m{price}") | |
elif price > previous_price: | |
sys.stdout.write(f"\r\033[92m{price}") | |
else: | |
sys.stdout.write(f"\r{price}") | |
else: | |
sys.stdout.write(f"\r{price}") | |
sys.stdout.flush() | |
time.sleep(1) # Sleep for 1 second | |
previous_price = price | |
except KeyboardInterrupt: | |
sys.stdout.write(f"\033[0m\nExiting...") | |
break | |
if __name__ == "__main__": | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment