Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def get_user_performances(user):
- date_last_year = date_hoy - datetime.timedelta(days = 365)
- performances = []
- portfolios = Portfolio.objects.filter(user=user)
- for portfolio in portfolios:
- try:
- nw_port = 0
- nw_port_week = 0
- nw_port_last_year = 0
- items = portfolio.portfolioitem_set.filter()
- for it in items:
- try:
- today = getClosestPrice(it.security.ticker, date_hoy)
- today = today.adj_close_price
- nw_port = nw_port + today * it.shares
- last_friday = getClosestPrice(it.security.ticker, date_last_week)
- if last_friday:
- try:
- lf = last_friday.adj_close_price
- except:
- lf = last_friday.close_price
- pass
- nw_port_week = nw_port_week + ( lf * it.shares )
- lst_year = getClosestPrice(it.security.ticker, date_last_year)
- if lst_year:
- try:
- p6m = lst_year.adj_close_price
- except:
- p6m = lst_year.close_price
- pass
- nw_port_last_year = nw_port_last_year + ( p6m * it.shares )
- except Exception, e:
- print e
- pass
- week_gain_port = str ( '%.2f' % ( ( ( float(nw_port) / float(nw_port_week) ) - 1 ) * 100 ) )
- year_gain_port = str ( '%.2f' % ( ( ( float(nw_port) / float(nw_port_last_year) ) - 1 ) * 100 ) )
- n_week_gain_port = ( ( ( float(nw_port) / float(nw_port_week) ) - 1 ) * 100 )
- n_year_gain_port = ( ( ( float(nw_port) / float(nw_port_last_year) ) - 1 ) * 100 )
- performances.append({ 'name': portfolio.name, '7days': week_gain_port, 'year': year_gain_port, 'n_7days': n_week_gain_port, 'n_year':n_year_gain_port})
- except:
- pass
- s_performances = sorted(performances, key=lambda k: -k['n_7days'])
- return s_performances
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement