Class: Iro::Stock
- Inherits:
-
Object
- Object
- Iro::Stock
- Includes:
- Mongoid::Document, Mongoid::Paranoia, Mongoid::Timestamps
- Defined in:
- app/models/iro/stock.rb
Overview
Constant Summary collapse
- STATUS_ACTIVE =
'active'- STATUS_INACTIVE =
'inactive'- STATUSES =
[ nil, 'active', 'inactive' ]
- LONG_ONLY =
'long-only'- LONG_OR_SHORT =
'long-or-short'- SHORT_ONLY =
'short-only'
Class Method Summary collapse
Instance Method Summary collapse
- #stdev(recompute: nil) ⇒ Object
- #symbol ⇒ Object
- #symbol=(a) ⇒ Object
- #to_s ⇒ Object
- #volatility(duration: 1.year, recompute: false) ⇒ Object
- #volatility_from_mo ⇒ Object
- #volatility_from_yr ⇒ Object
Class Method Details
.active ⇒ Object
16 17 18 |
# File 'app/models/iro/stock.rb', line 16 def self.active where( status: STATUS_ACTIVE ) end |
.f(ticker) ⇒ Object
my_find
48 49 50 |
# File 'app/models/iro/stock.rb', line 48 def self.f ticker self.find_by ticker: ticker end |
.list ⇒ Object
55 56 57 |
# File 'app/models/iro/stock.rb', line 55 def self.list [[nil,nil]] + all.map { |sss| [ sss.ticker, sss.id ] } end |
.tickers_list ⇒ Object
58 59 60 |
# File 'app/models/iro/stock.rb', line 58 def self.tickers_list [[nil,nil]] + all.map { |sss| [ sss.ticker, sss.ticker ] } end |
Instance Method Details
#stdev(recompute: nil) ⇒ Object
120 121 122 123 124 125 126 127 128 129 |
# File 'app/models/iro/stock.rb', line 120 def stdev recompute: nil if !self[:stdev] || recompute out = volatility_from_yr self[:stdev] = out save( validate: false ) return out else self[:stdev] end end |
#symbol ⇒ Object
24 |
# File 'app/models/iro/stock.rb', line 24 def symbol; ticker; end |
#symbol=(a) ⇒ Object
25 |
# File 'app/models/iro/stock.rb', line 25 def symbol= a; ticker = a; end |
#to_s ⇒ Object
52 53 54 |
# File 'app/models/iro/stock.rb', line 52 def to_s ticker end |
#volatility(duration: 1.year, recompute: false) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'app/models/iro/stock.rb', line 73 def volatility duration: 1.year, recompute: false if self[:volatility] if !recompute return self[:volatility] end end stock = self begin_on = Time.now - duration - 1.day points = Iro::Datapoint.where( kind: 'STOCK', symbol: stock.ticker, :date.gte => begin_on, ).order_by( date: :asc ) puts! [points.first.date, points.last.date], "from,to" points_p = [] points.each_with_index do |p, idx| next if idx == 0 prev = points[idx-1] out = p.value / prev.value - 1 points_p.push out end n = points_p.length avg = points_p.reduce(&:+) / n _sum_of_sq = [] points_p.map do |p| _sum_of_sq.push( ( p - avg )*( p - avg ) ) end sum_of_sq = _sum_of_sq.reduce( &:+ ) / n # n_periods = begin_on.to_date.business_days_until( Date.today ) out = Math.sqrt( sum_of_sq )*sqrt( n ) adjustment = 2.0 out = out * adjustment puts! out, 'volatility (adjusted)' self.update volatility: out return out end |
#volatility_from_mo ⇒ Object
114 115 116 |
# File 'app/models/iro/stock.rb', line 114 def volatility_from_mo volatility( duration: 1.month ) end |
#volatility_from_yr ⇒ Object
117 118 119 |
# File 'app/models/iro/stock.rb', line 117 def volatility_from_yr volatility( duration: 1.year ) end |