def seed_db(self): name = 'Some Account' category = 'My 401k' owner = 'Jane Dow' retirement = 1 act = Account(name=name, category=category, owner=owner, retirement=retirement) db.session.add(act) inv = Investment(name='SPDR S&P 500 ETF', symbol='SPY', shares=100, price=2020.38, account=act) db.session.add(inv) inv = Investment(name='iShares Russell 2000', symbol='IWM', shares=50, price=25.05, account=act) db.session.add(inv) history = AccountHistory() history.account = act history.snapshot_date = dateutil.parser.parse("2015-12-01") history.value = 200000 db.session.add(history) history = AccountHistory() history.account = act history.snapshot_date = dateutil.parser.parse("2015-12-05") history.value = 203290.5 db.session.add(history) act = Account(name='My Brokerage', category='Brokerage', owner='Bob Dow', retirement=0) db.session.add(act) inv = Investment(name='VANGUARD TOTAL STOCK MARKET ETF', symbol='VTI', shares=152.63, price=2020.38, account=act) db.session.add(inv) history = AccountHistory() history.account = act history.snapshot_date = dateutil.parser.parse("2015-12-01") history.value = 300000 db.session.add(history) history = AccountHistory() history.account = act history.snapshot_date = dateutil.parser.parse("2015-12-05") history.value = 308370.59 db.session.add(history) db.session.commit()
def update_account_prices(accounts, db): for account in accounts: account_total_val = 0 for investment in account.investments: price = get_current_price(investment.symbol) account_total_val += price * investment.shares investment.price = round(price, 2) db.session.add(investment) db.session.commit() history = AccountHistory.query.filter( and_(AccountHistory.account_id == account.id, AccountHistory.snapshot_date == date.today())).all() if not history: history = AccountHistory() history.account = account history.snapshot_date = date.today() else: history = history[0] history.value = account_total_val db.session.add(history) db.session.commit()