Advertisement
Guest User

Untitled

a guest
Nov 27th, 2014
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.52 KB | None | 0 0
  1. def get_user_performances(user):
  2.             date_last_year = date_hoy - datetime.timedelta(days = 365)
  3.  
  4.             performances = []
  5.  
  6.             portfolios = Portfolio.objects.filter(user=user)
  7.             for portfolio in portfolios:
  8.                 try:
  9.                     nw_port = 0
  10.                     nw_port_week = 0
  11.                     nw_port_last_year = 0
  12.                     items = portfolio.portfolioitem_set.filter()
  13.                     for it in items:
  14.                         try:
  15.                             today = getClosestPrice(it.security.ticker, date_hoy)
  16.                             today = today.adj_close_price
  17.                             nw_port = nw_port + today * it.shares
  18.  
  19.                             last_friday = getClosestPrice(it.security.ticker, date_last_week)
  20.                             if last_friday:
  21.                                 try:
  22.                                     lf = last_friday.adj_close_price
  23.                                 except:
  24.                                     lf = last_friday.close_price
  25.                                     pass
  26.  
  27.                             nw_port_week = nw_port_week + ( lf * it.shares )
  28.  
  29.                             lst_year = getClosestPrice(it.security.ticker, date_last_year)
  30.                             if lst_year:
  31.                                 try:
  32.                                     p6m = lst_year.adj_close_price
  33.                                 except:
  34.                                     p6m = lst_year.close_price
  35.                                     pass
  36.  
  37.                             nw_port_last_year = nw_port_last_year + ( p6m * it.shares )
  38.  
  39.                         except Exception, e:
  40.                             print e
  41.                             pass
  42.  
  43.                     week_gain_port = str ( '%.2f' % ( ( ( float(nw_port) / float(nw_port_week) ) - 1 ) * 100 ) )
  44.                     year_gain_port = str ( '%.2f' % ( ( ( float(nw_port) / float(nw_port_last_year) ) - 1 ) * 100 ) )
  45.                     n_week_gain_port = ( ( ( float(nw_port) / float(nw_port_week) ) - 1 ) * 100 )
  46.                     n_year_gain_port = ( ( ( float(nw_port) / float(nw_port_last_year) ) - 1 ) * 100 )
  47.                     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})
  48.  
  49.                 except:
  50.                     pass
  51.  
  52.             s_performances = sorted(performances, key=lambda k: -k['n_7days'])
  53.             return s_performances
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement