Class: Rebalance::Target

Inherits:
Object
  • Object
show all
Defined in:
lib/rebalance/target.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Target

Returns a new instance of Target.



5
6
7
8
# File 'lib/rebalance/target.rb', line 5

def initialize(&block)
  self.asset_classes = {}
  instance_eval &block
end

Instance Attribute Details

#asset_classesObject

Returns the value of attribute asset_classes.



3
4
5
# File 'lib/rebalance/target.rb', line 3

def asset_classes
  @asset_classes
end

Instance Method Details

#asset_class(percentage, asset_class) ⇒ Object



10
11
12
# File 'lib/rebalance/target.rb', line 10

def asset_class(percentage, asset_class)
  self.asset_classes[asset_class] = percentage
end

#asset_class_percentages_across_all_accounts(*accounts) ⇒ Object

get each account’s asset class percentage breakdown in relation to all the accounts



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/rebalance/target.rb', line 67

def asset_class_percentages_across_all_accounts(*accounts)
  total_value_of_all_accounts = total_value_of_all_accounts(*accounts)
   = {}

  accounts.each do ||
    .funds.each do |symbol, fund|
      asset_class_total = 0
      .find_by_asset_class(fund.asset_class).each do |asset_class_fund|
        asset_class_total += asset_class_fund.value
      end
      [.name] = {} if [.name].nil?
      [.name][fund.asset_class] = ((asset_class_total / total_value_of_all_accounts) * 100).round(4)
    end
  end
  
end

#calculate_current_asset_class_percentages(*accounts) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/rebalance/target.rb', line 46

def calculate_current_asset_class_percentages(*accounts)
  current_percentages = {}
  current_values = calculate_current_asset_class_values(*accounts)
  total_value = total_value_of_all_accounts(*accounts)

  current_values.each do |asset_class, asset_class_value|
    current_percentages[asset_class] = ((asset_class_value / total_value)*100).round(4)
  end
  current_percentages
end

#calculate_current_asset_class_values(*accounts) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/rebalance/target.rb', line 35

def calculate_current_asset_class_values(*accounts)
  current_values = {}
  accounts.each do ||
    .funds.each do |symbol, fund|
      current_values[fund.asset_class] = 0 if current_values[fund.asset_class].nil?
      current_values[fund.asset_class] += fund.value
    end
  end
  current_values
end

#calculate_target_asset_class_percentages(*accounts) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/rebalance/target.rb', line 24

def calculate_target_asset_class_percentages(*accounts)
  target_percentages = {}
  target_values = calculate_target_asset_class_values(*accounts)
  total_value = total_value_of_all_accounts(*accounts)

  target_values.each do |asset_class, asset_class_value|
    target_percentages[asset_class] = ((asset_class_value / total_value)*100).round(4)
  end
  target_percentages
end

#calculate_target_asset_class_values(*accounts) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/rebalance/target.rb', line 14

def calculate_target_asset_class_values(*accounts)
  total_value = total_value_of_all_accounts(*accounts)

  target_values = {}
  asset_classes.each do |asset_class, percentage|
    target_values[asset_class] = (total_value * (percentage.to_f/100)).round(2)
  end
  target_values
end

#total_value_of_all_accounts(*accounts) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/rebalance/target.rb', line 57

def total_value_of_all_accounts(*accounts)
  value = 0
  accounts.each do ||
    value += .total_value
  end
  value
end