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
- .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.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/brm_finance/stock.rb', line 62 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.
52 53 54 |
# File 'lib/brm_finance/stock.rb', line 52 def @no_data_message end |
#response_code ⇒ Object
Returns the value of attribute response_code.
52 53 54 |
# File 'lib/brm_finance/stock.rb', line 52 def response_code @response_code end |
Class Method Details
.company_info(symbol) ⇒ Object
98 99 100 101 102 103 104 105 |
# File 'lib/brm_finance/stock.rb', line 98 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 |
.fields ⇒ Object
58 59 60 |
# File 'lib/brm_finance/stock.rb', line 58 def self.fields @@fields end |
.history(symbol, start_date = Date.today - 1.year, end_date = Date.today) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/brm_finance/stock.rb', line 135 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
107 108 109 110 111 112 113 114 |
# File 'lib/brm_finance/stock.rb', line 107 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
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/brm_finance/stock.rb', line 116 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
85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/brm_finance/stock.rb', line 85 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
81 82 83 |
# File 'lib/brm_finance/stock.rb', line 81 def failure? response_code==404 end |
#success? ⇒ Boolean
77 78 79 |
# File 'lib/brm_finance/stock.rb', line 77 def success? response_code==200 end |