Create your own mock stock portfolio, complete with a P/L statement in Python.The goal of this final project is to be able to pretend to buy and sell stock with real stock data; while recording your transaction histories and presenting that information to a user in the form of a P & L statement.Rubric is as follows20% – Able to get real time stock data – Allows you to mock buy/sell stock using tickers and share amount20% – Saves your P & L data between running your program (For beginners, I suggest using pandas.DataFrame.to_csv or sqlite3) (google it)20% – Creates a decent P & L with the columns DATE, TICKER, PRICE, SHARES, BALANCE 20% – Program runs smoothly (doesn’t crash, output is aesthetically pleasant to look at)10% – Comments within code, code is well structured and easy to look at10% – Methods used in Script are contained within a Class
import pandas as pd import datetime #Open persistance data try:
pl = pd.read_csv(‘my_pl.csv’,index_col=False) except Exception as e:
print(str(e)) pl = pd.DataFrame({‘BALANCE’:[1000000.0], ‘DATE’:[datetime.datetime.now()], ‘TICKER’:[‘GOOG’], ‘PRICE’:[55.0], ‘SHARES’:[100.0], ‘COST’:[5500.0]}) #Should already be sorted, but lets make sure pl = pl.sort_values(by=’DATE’) #User input function while True:
try:
TICKER = input(‘Please enter a ticker’) PRICE = int(input(‘Remind me, what price was this?’)) COST = float(input(‘Please enter how much to buy in USD’)) break except:
pass SHARES = COST / PRICE OLD_BALANCE = float(pl.tail(1)[‘BALANCE’]) NEW_BALANCE = OLD_BALANCE – COST new_row = pd.DataFrame({‘BALANCE’:[NEW_BALANCE], ‘DATE’:[datetime.datetime.now()], ‘TICKER’:[TICKER], ‘PRICE’:[PRICE], ‘SHARES’:[SHARES], ‘COST’:[COST]}) pl = pd.concat([pl,new_row]) pl.to_csv(‘my_pl.csv’,index=False)