class Iro::Stock
def self.active
def self.active where( status: STATUS_ACTIVE ) end
def self.f ticker
def self.f ticker self.find_by ticker: ticker end
def self.list
def self.list [[nil,nil]] + all.map { |sss| [ sss.ticker, sss.id ] } end
def self.tickers_list
def self.tickers_list [[nil,nil]] + all.map { |sss| [ sss.ticker, sss.ticker ] } end
def stdev recompute: nil
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
def symbol; ticker; end
def symbol; ticker; end
def symbol= a; ticker = a; end
def symbol= a; ticker = a; end
def to_s
def to_s ticker end
def volatility duration: 1.year, recompute: false
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
def volatility_from_mo
def volatility_from_mo volatility( duration: 1.month ) end
def volatility_from_yr
def volatility_from_yr volatility( duration: 1.year ) end