Skip to content

Instantly share code, notes, and snippets.

@Lvl4Sword
Last active February 28, 2018 23:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lvl4Sword/f3a792b2633c8bf9f94ae6b63fbe86a5 to your computer and use it in GitHub Desktop.
Save Lvl4Sword/f3a792b2633c8bf9f94ae6b63fbe86a5 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import requests
import sys
import time
try:
print('CTRL + D or CTRL + C will close the program at any time.')
stock_names_input = input('Stock names separated by \',\' (AAPL, GOOG, TSLA): ')
stock_prices_input = input('Stock prices you bought in at separated by \',\' (100.00, 23.31, 493.84): ')
stock_amount_input = input('Amount of stocks you bought at this price separated by \',\': (192, 83, 91): ')
stock_name_list = stock_names_input.split(',')
stock_name_list = [w.strip() for w in stock_name_list]
stock_name_list = [x.upper() for x in stock_name_list]
stock_prices_list = stock_prices_input.split(',')
stock_prices_list = [float(y.strip()) for y in stock_prices_list]
stock_amount_list = stock_amount_input.split(',')
stock_amount_list = [int(z.strip()) for z in stock_amount_list]
name_length = len(stock_name_list)
price_length = len(stock_prices_list)
amount_length = len(stock_amount_list)
if name_length == price_length == amount_length:
stock_dictionary = {}
for each in range(0, name_length):
stock_dictionary[each] = {}
stock_dictionary[each]['name'] = stock_name_list[each]
stock_dictionary[each]['price'] = stock_prices_list[each]
stock_dictionary[each]['amount'] = stock_amount_list[each]
for each in stock_dictionary:
if stock_dictionary[each]['name'] == '':
raise IndexError
while True:
stock_request = requests.get('https://finance.google.com/finance?q=NASDAQ:{0}'.format(stock_dictionary[each]['name']))
if 'produced no matches.' in stock_request.text:
if each + 1 != len(stock_dictionary):
print('{0}: doesn\'t exist, trying next..'.format(stock_dictionary[each]['name']))
else:
print('{0}: doesn\'t exist'.format(stock_dictionary[each]['name']))
else:
current_stock_price = stock_request.text.split('<span class="pr">')[1].split('">')[1].split('</span>')[0].replace(',', '')
current_stock_price = float(current_stock_price)
total_put_in = stock_dictionary[each]['price'] * stock_dictionary[each]['amount']
stock_value = current_stock_price * stock_dictionary[each]['amount']
your_total = stock_value - total_put_in
print('{0}: YOUR TOTAL: {1:.2f}'.format(stock_dictionary[each]['name'], total_put_in))
print('{0}: CURRENT PRICE @ 1 STOCK: {1:,.2f}'.format(stock_dictionary[each]['name'], current_stock_price))
print('{0}: CURRENT TOTAL @ {1:.2f} STOCKS: {2:,.2f}'.format(stock_dictionary[each]['name'], stock_dictionary[each]['price'], stock_value))
if your_total < 0:
print('{0}: LOST: {1:,.2f}'.format(stock_dictionary[each]['name'], your_total))
else:
print('{0}: GAINED: {1:,.2f}'.format(stock_dictionary[each]['name'], your_total))
print('Sleeping for 5 minutes..')
time.sleep(300)
else:
print('You have {0} stocks, {1} stock prices, and {2} for'.format(name_length, price_length, amount_length))
print('the total amount of different stocks you\'ve bought.')
print('These all must be equal.\n')
print('Please re-run the program and fix this.')
sys.exit(3)
except IndexError:
print('That stock doesn\'t exist, try again.')
except ValueError:
print('Invalid input\n')
except(EOFError, KeyboardInterrupt):
print('\nGoodbye!')
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment