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
-
#get_historic_data(date_from = nil, date_to = nil) ⇒ Object
From: stockdata_org.
-
#min ⇒ Object
for charting:.
- #stdev(recompute: nil) ⇒ Object
-
#step ⇒ Object
for histograms.
- #step_must_fit_range ⇒ Object
- #symbol ⇒ Object
- #symbol=(a) ⇒ Object
- #to_s ⇒ Object
- #volatility(duration: 1.year, recompute: false) ⇒ Object
- #volatility_from_mo ⇒ Object
- #volatility_from_yr ⇒ Object
- #volatility_old(duration: 1.year, recompute: false) ⇒ 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
71 72 73 |
# File 'app/models/iro/stock.rb', line 71 def self.f ticker self.find_by ticker: ticker end |
.list ⇒ Object
78 79 80 |
# File 'app/models/iro/stock.rb', line 78 def self.list [[nil,nil]] + all.map { |sss| [ sss.ticker, sss.id ] } end |
.sync ⇒ Object
221 222 223 224 225 226 227 228 |
# File 'app/models/iro/stock.rb', line 221 def self.sync tickers = Iro::Stock.all.map { |s| s.ticker }.join(',') outs = Tda::Stock.get_quotes tickers outs.map do |out| Iro::Stock.where( ticker: out[:symbol] ).update_all( last: out[:last] ) end puts "+++ Synced stocks." end |
.tickers_list ⇒ Object
81 82 83 |
# File 'app/models/iro/stock.rb', line 81 def self.tickers_list [[nil,nil]] + all.map { |sss| [ sss.ticker, sss.ticker ] } end |
Instance Method Details
#get_historic_data(date_from = nil, date_to = nil) ⇒ Object
From: stockdata_org
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'app/models/iro/stock.rb', line 195 def get_historic_data date_from=nil, date_to=nil date_from ||= Time.now - 1.year - 1.week date_to ||= date_from + 180.days date_from = date_from.strftime('%Y-%m-%d') date_to = date_to.strftime('%Y-%m-%d') puts! [ticker, date_from, date_to], "ticker,date_from,date_to" outs = HTTParty.get("https://api.stockdata.org/v1/data/eod?symbols=#{ticker}&date_from=#{date_from}&date_to=#{date_to}&api_token=#{STOCKDATA_ORG_KEY}") outs['data'].each do |datum| existing = ::Iro::Datapoint.find_by({ symbol: ticker, date: datum['date'].to_date.strftime('%Y-%m-%d') }) rescue nil if existing print('.') else ::Iro::Datapoint.create!({ symbol: ticker, kind: ::Iro::Datapoint::KIND_STOCK, date: datum['date'].to_date.strftime('%Y-%m-%d'), open: datum['open'], high: datum['high'], low: datum['low'], value: datum['close'], volume: datum['volume'], }) print('^') end end end |
#min ⇒ Object
for charting:
36 |
# File 'app/models/iro/stock.rb', line 36 field :min, type: :integer |
#stdev(recompute: nil) ⇒ Object
172 173 174 175 176 177 178 179 180 181 |
# File 'app/models/iro/stock.rb', line 172 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 |
#step ⇒ Object
for histograms. I liked 50 bars, that's $1-3 bucket size.
38 |
# File 'app/models/iro/stock.rb', line 38 field :step, type: :integer, default: 1.0 |
#step_must_fit_range ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 |
# File 'app/models/iro/stock.rb', line 41 def step_must_fit_range return if min.blank? || max.blank? || step.blank? range = max. - min.to_i step_val = step.to_i if !((max - min) % step).zero? errors.add(:step, "must evenly divide (max-min)/step") 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
75 76 77 |
# File 'app/models/iro/stock.rb', line 75 def to_s ticker end |
#volatility(duration: 1.year, recompute: false) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'app/models/iro/stock.rb', line 99 def volatility duration: 1.year, recompute: false if self[:volatility] if !recompute return self[:volatility] end end stock = self begin_on = Time.now - duration points = ::Iro::Datapoint.where( kind: 'STOCK', symbol: stock.ticker, :date.gte => begin_on, ).order_by( date: :asc ) returns = [] points.each_cons(2) do |prev, curr| returns << Math.log(curr.value / prev.value) end # puts! returns, 'returns' mean = returns.sum / returns.size variance = returns.sum { |r| (r - mean) ** 2 } / (returns.size - 1) daily_vol = Math.sqrt(variance) monthly_vol = daily_vol * Math.sqrt(21) annual_vol = daily_vol * Math.sqrt(252) self.update(volatility_annual: annual_vol, volatility_monthly: monthly_vol, volatility_daily: daily_vol) annual_vol end |
#volatility_from_mo ⇒ Object
166 167 168 |
# File 'app/models/iro/stock.rb', line 166 def volatility_from_mo volatility( duration: 1.month ) end |
#volatility_from_yr ⇒ Object
169 170 171 |
# File 'app/models/iro/stock.rb', line 169 def volatility_from_yr volatility( duration: 1.year ) end |
#volatility_old(duration: 1.year, recompute: false) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'app/models/iro/stock.rb', line 127 def volatility_old 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 ) puts! out, 'volatility (adjusted)' self.update( volatility: out, volatility_annual: out ) return out end |