Class: BrmFinance::Stock
- Inherits:
-
Object
- Object
- BrmFinance::Stock
- Defined in:
- lib/brm_finance/stock.rb
Constant Summary collapse
- @@fields =
["symbol", "Ask","AverageDailyVolume", "Bid", "AskRealtime", "BidRealtime", "BookValue", "Change_PercentChange","Change", "Commission", "ChangeRealtime", "AfterHoursChangeRealtime", "DividendShare","LastTradeDate","TradeDate", "EarningsShare", "ErrorIndicationreturnedforsymbolchangedinvalid", "EPSEstimateCurrentYear", "EPSEstimateNextYear", "EPSEstimateNextQuarter", "DaysLow","DaysHigh","YearLow", "YearHigh", "HoldingsGainPercent", "AnnualizedGain","HoldingsGain", "HoldingsGainPercentRealtime", "HoldingsGainRealtime", "MoreInfo", "OrderBookRealtime", "MarketCapitalization", "MarketCapRealtime", "EBITDA", "ChangeFromYearLow", "PercentChangeFromYearLow", "LastTradeRealtimeWithTime", "ChangePercentRealtime","ChangeFromYearHigh", "PercebtChangeFromYearHigh", "LastTradeWithTime", "LastTradePriceOnly", "HighLimit", "LowLimit", "DaysRange", "DaysRangeRealtime", "FiftydayMovingAverage", "TwoHundreddayMovingAverage", "ChangeFromTwoHundreddayMovingAverage", "PercentChangeFromTwoHundreddayMovingAverage", "ChangeFromFiftydayMovingAverage", "PercentChangeFromFiftydayMovingAverage", "Name", "Notes", "Open", "PreviousClose", "PricePaid", "ChangeinPercent", "PriceSales","PriceBook", "ExDividendDate", "PERatio", "DividendPayDate", "PERatioRealtime", "PEGRatio", "PriceEPSEstimateCurrentYear", "PriceEPSEstimateNextYear", "Symbol", "SharesOwned", "ShortRatio", "LastTradeTime", "TickerTrend", "OneyrTargetPrice", "Volume", "HoldingsValue", "HoldingsValueRealtime", "YearRange","DaysValueChange", "DaysValueChangeRealtime", "StockExchange","DividendYield","PercentChange", "ErrorIndicationreturnedforsymbolchangedinvalid"]
Instance Attribute Summary collapse
-
#no_data_message ⇒ Object
Returns the value of attribute no_data_message.
-
#response_code ⇒ Object
Returns the value of attribute response_code.
Class Method Summary collapse
- .company_info(symbol) ⇒ Object
- .dividends_splits(symbol, start_date = Date.today-1.year, end_date = Date.today) ⇒ Object
- .fields ⇒ Object
- .history(symbol, start_date = Date.today - 1.year, end_date = Date.today) ⇒ Object
- .key_stats(symbol) ⇒ Object
- .parse(json, symbol) ⇒ Object
- .quote(symbol, start_date = nil, end_date = nil) ⇒ Object
Instance Method Summary collapse
- #failure? ⇒ Boolean
-
#initialize(data) ⇒ Stock
constructor
A new instance of Stock.
- #success? ⇒ Boolean
Constructor Details
#initialize(data) ⇒ Stock
Returns a new instance of Stock.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/brm_finance/stock.rb', line 43 def initialize(data) if data['ErrorIndicationreturnedforsymbolchangedinvalid'] @no_data_message = data['ErrorIndicationreturnedforsymbolchangedinvalid'] @response_code = 404 elsif data['count'] && data['count'] == 0 @no_data_message = "Query returns no valid data" @response_code = 404 else @response_code = 200 data.map do |k,v| instance_variable_set("@#{k.underscore}", (v.nil? ? nil : v.to_fs)) end end end |
Instance Attribute Details
#no_data_message ⇒ Object
Returns the value of attribute no_data_message.
33 34 35 |
# File 'lib/brm_finance/stock.rb', line 33 def @no_data_message end |
#response_code ⇒ Object
Returns the value of attribute response_code.
33 34 35 |
# File 'lib/brm_finance/stock.rb', line 33 def response_code @response_code end |
Class Method Details
.company_info(symbol) ⇒ Object
99 100 101 102 103 104 105 106 |
# File 'lib/brm_finance/stock.rb', line 99 def self.company_info(symbol) url = 'http://query.yahooapis.com/v1/public/yql?q=' url += URI::encode("select * from yahoo.finance.stocks where symbol in (#{symbol.to_p})") url += "&env=http%3A%2F%2Fdatatables.org%2Falltables.env&format=json" response = RestClient.get(url) self.parse(response, symbol) end |
.dividends_splits(symbol, start_date = Date.today-1.year, end_date = Date.today) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/brm_finance/stock.rb', line 79 def self.dividends_splits(symbol, start_date=Date.today-1.year, end_date=Date.today) symbol = [symbol] unless symbol.is_a?(Array) start_date = start_date.strftime('%m-%d-%Y').split('-') start_date = "a=#{start_date[0]}&b=#{start_date[1]}&c=#{start_date[2]}" end_date = end_date.strftime('%m-%d-%Y').split('-') end_date = end_date.split('-') end_date = "d=#{end_date[0]}&e=#{end_date[1]}&f=#{end_date[2]}" results = [] symbol.each do |s| url = "http://ichart.finance.yahoo.com/x?s=#{s}&#{start_date}&#{end_date}&g=v&y=0&z=30000" response = RestClient.get(url) results.push({'Symbol' => s, 'Series' => CSV.parse(response)}) end results end |
.fields ⇒ Object
39 40 41 |
# File 'lib/brm_finance/stock.rb', line 39 def self.fields @@fields end |
.history(symbol, start_date = Date.today - 1.year, end_date = Date.today) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/brm_finance/stock.rb', line 136 def self.history(symbol, start_date=Date.today - 1.year, end_date=Date.today) symbol = symbol.is_a?(Array) ? symbol : [symbol] results = [] results = symbol.collect do |sym| series = [] (start_date.to_date..end_date.to_date+1.year).step(365) do |date| break if date > Date.today new_series = self.quote(sym, (date).strftime('%Y-%m-%d'), (date + 1.year - 1.day).strftime('%Y-%m-%d')) series += new_series if new_series.is_a?(Array) end {'Symbol' => sym, 'Series' => series.uniq} end results end |
.key_stats(symbol) ⇒ Object
108 109 110 111 112 113 114 115 |
# File 'lib/brm_finance/stock.rb', line 108 def self.key_stats(symbol) url = 'http://query.yahooapis.com/v1/public/yql?q=' url += URI::encode("SELECT * FROM yahoo.finance.keystats WHERE symbol in (#{symbol.to_p})") url += "&env=http%3A%2F%2Fdatatables.org%2Falltables.env&format=json" response = RestClient.get(url) self.parse(response, symbol) end |
.parse(json, symbol) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/brm_finance/stock.rb', line 117 def self.parse(json, symbol) results = [] json = JSON.parse(json) count = json["query"]["count"] return Stock.new(json["query"]) if count == 0 results = json["query"]["results"] #data = count == 1 ? [data] : data results.each do |k, v| results = results[k].is_a?(Array) ? results[k] : [results[k]] return results #d["symbol"] = symbol.to_p unless d["symbol"] #stock = Stock.new(d) #results += stock end #return results end |
.quote(symbol, start_date = nil, end_date = nil) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/brm_finance/stock.rb', line 66 def self.quote(symbol, start_date=nil, end_date=nil) url = 'http://query.yahooapis.com/v1/public/yql?q='; if start_date && end_date url = url+URI::encode("select * from yahoo.finance.historicaldata where symbol in (#{symbol.to_p}) and startDate = '#{start_date}' and endDate = '#{end_date}'") else url = url+URI::encode("select * from yahoo.finance.quotes where symbol in (#{symbol.to_p})") end url = url+"&env=http%3A%2F%2Fdatatables.org%2Falltables.env&format=json" response = RestClient.get(url) self.parse(response, symbol) end |
Instance Method Details
#failure? ⇒ Boolean
62 63 64 |
# File 'lib/brm_finance/stock.rb', line 62 def failure? response_code==404 end |
#success? ⇒ Boolean
58 59 60 |
# File 'lib/brm_finance/stock.rb', line 58 def success? response_code==200 end |