Skip to content

Instantly share code, notes, and snippets.

@clintmjohnson
Last active October 2, 2017 19:52
Show Gist options
  • Save clintmjohnson/4cb45299b97b58f9e8fe4c2a39a670a7 to your computer and use it in GitHub Desktop.
Save clintmjohnson/4cb45299b97b58f9e8fe4c2a39a670a7 to your computer and use it in GitHub Desktop.
This Python program Tracks Sample Stock buying and selling, using Yahoo Finance live Data
def stocktrack(stock,buysell,qty):
import sqlite3
from yahoo_finance import Share
conn = sqlite3.connect('Stocks.db')
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS stocks
(date text, trans text, symbol text, qty real, price real, total real)''')
yahoo = Share(stock)
pricenow = float(yahoo.get_price())
if buysell in ('Buy','BUY','b','B'):
buysell = 'BUY'
elif buysell == None:
buysell = 'BUY'
elif buysell in ('Sell','SELL','S','s'):
buysell = 'SELL'
c.execute("INSERT INTO stocks (date,trans,symbol,qty,price,total) values (date('now'),?,?,?,?,?)",\
(buysell,stock,qty,pricenow,(int(qty)*int(pricenow))))
conn.commit()
conn.close()
stocktrack(input('Stock to Buy: '),input('Buy or Sell? :'),input('Qty :'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment