Skip to content

Instantly share code, notes, and snippets.

@jaredonline
Created September 21, 2011 21:03
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 jaredonline/1233309 to your computer and use it in GitHub Desktop.
Save jaredonline/1233309 to your computer and use it in GitHub Desktop.
def percent_asset_split
plan.best_investment(risk_index).map do |symbol, percentage|
::Position.new(:symbol => symbol.to_s, :amount => percentage.to_f, :account => self.account).asset_allocation
end.sum.compress.to_percent
end
@Umofomia
Copy link

Due to the issue with empty arrays I pointed out yesterday, you should use AllocationHash::sum rather than #sum:

position_allocations = plan.best_investment(risk_index).map do |symbol, percentage|
                         ::Position.new(:symbol => symbol.to_s, :amount => percentage.to_f, :account => self.account).asset_allocation
                       end
AllocationHash.sum(position_allocations).compress.to_percent

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment