app/models/iro/stock.rb in iron_warbler-2.0.7.37 vs app/models/iro/stock.rb in iron_warbler-2.0.7.38

- old
+ new

@@ -1,8 +1,11 @@ include Math require 'business_time' +## +## https://www.macrotrends.net/stocks/charts/META/meta-platforms/stock-price-history +## class Iro::Stock include Mongoid::Document include Mongoid::Timestamps include Mongoid::Paranoia store_in collection: 'iro_stocks' @@ -16,10 +19,12 @@ field :status, default: STATUS_ACTIVE field :ticker validates :ticker, uniqueness: true, presence: true index({ ticker: -1 }, { unique: true }) + def symbol; ticker; end + def symbol= a; ticker = a; end field :last, type: :float field :options_price_increment, type: :float field :stdev, type: :float @@ -28,10 +33,12 @@ has_many :strategies, class_name: 'Iro::Strategy', inverse_of: :stock has_many :purses, class_name: 'Iro::Purse', inverse_of: :stock has_many :options, class_name: 'Iro::Option', inverse_of: :stock has_many :priceitems, inverse_of: :stock + default_scope { order_by({ ticker: :asc }) } + ## my_find def self.f ticker self.find_by ticker: ticker end @@ -53,11 +60,18 @@ duration = 1.year stock.volatility_from_yr =end - def volatility duration: + field :volatility, type: :float + 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 ) @@ -84,9 +98,10 @@ # 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 volatility( duration: 1.month )