Skip to content

Instantly share code, notes, and snippets.

@jrmoran
Created February 19, 2011 18:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jrmoran/835262 to your computer and use it in GitHub Desktop.
Save jrmoran/835262 to your computer and use it in GitHub Desktop.
def build_portfolio
# preparing portfolio data for charts
stocks, eps, pm, industries, price_performance = [], [], [], {}, []
Portfolio.first.portfolio_companies.each do |pc|
# create arrays from data
stocks << pc.company.symbol if pc.company.symbol
eps << pc.company.diluted_eps if pc.company.diluted_eps
pm << (pc.company.profit_margin*100).round(1) if pc.company.profit_margin
if pc.company.industry
# if the industry is already in there, increase its value by one
industries.include?(pc.company.industry) ?
industries[pc.company.industry]+=1 : industries[pc.company.industry]=1
end
price_performance << {
:name=>pc.company.symbol,
:data=> pc.company.returns
}
end
# parse to percentage the number of times an industry is in the portfolio
companies_total = stocks.count
industries.keys.each { |k| industries[k]=industries[k]*100/companies_total }
render :json => {:message =>"OK",
:stocks =>stocks,
:eps => eps,
:pm => pm,
:diversification => industries.to_a, #array of [key, value]
:price_performance=>price_performance
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment