Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
spec/config.yml
*.lock
Copy link
Copy Markdown

@federbenjamin federbenjamin Aug 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] Why?

edit: in description, my bad

*.gem
.idea
2 changes: 1 addition & 1 deletion AlphavantageRB.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

Gem::Specification.new do |s|
s.name = "alphavantagerb"
s.version = "1.4.0"
s.version = "1.5.0"
s.authors = ["Stefano Martin"]
s.email = ["stefano.martin87@gmail.com"]
s.homepage = "https://github.com/StefanoMartin/AlphaVantageRB"
Expand Down
48 changes: 48 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
GEM
remote: https://rubygems.org/
specs:
awesome_print (1.8.0)
byebug (11.1.3)
coderay (1.1.3)
diff-lcs (1.4.4)
httparty (0.18.1)
mime-types (~> 3.0)
multi_xml (>= 0.5.2)
humanize (2.4.3)
method_source (1.0.0)
mime-types (3.3.1)
mime-types-data (~> 3.2015)
mime-types-data (3.2020.0512)
multi_xml (0.6.0)
pry (0.13.1)
coderay (~> 1.1)
method_source (~> 1.0)
pry-byebug (3.9.0)
byebug (~> 11.0)
pry (~> 0.13.0)
rspec (3.9.0)
rspec-core (~> 3.9.0)
rspec-expectations (~> 3.9.0)
rspec-mocks (~> 3.9.0)
rspec-core (3.9.2)
rspec-support (~> 3.9.3)
rspec-expectations (3.9.2)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.9.0)
rspec-mocks (3.9.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.9.0)
rspec-support (3.9.3)

PLATFORMS
ruby

DEPENDENCIES
awesome_print (>= 1.7)
httparty (>= 0.17.0)
humanize (>= 1.7.0)
pry-byebug
rspec (>= 3.5)

BUNDLED WITH
2.1.4
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ AlphavantateRB has the following classes:
* [Alphavantage::Client](#Client): to manage the credentials to contact Alpha
Vantage
* [Alphavantage::Stock](#Stock): to create a stock class
* [Alphavantage::Fundamental_Data](#Fundamental_Data): to retrieve financial data of a stock
* [Alphavantage::Timeseries](#Timeseries): to retrieve historical data of a stock
* [Alphavantage::Indicator](#Indicator): to use some technical indicator of a stock
* [Alphavantage::Crypto](#Crypto): to create a crypto currency class
Expand Down Expand Up @@ -143,6 +144,36 @@ stock_quote.change
stock_quote.change_percent
```

<a name="Fundamental_Data"></a>
## Alphavantage::Fundamental_Data
This class returns financial information about an individual stock

To create a new Fundamental_Data class you can use a client object, a stock object, or you can create it directly.
These creation commands are equivalent:

``` ruby
fundamental_data = client.fundamental_data symbol: "MSFT"
fundamental_data = stock.fundamental_data
fundamental_data = Alphavantage::Fundamental_Data.new symbol: "MSFT", key: "YOURKEY"
```

Note that the initialization owns different entries:

* symbol: it is a string that denote the stock you want to retrieve. This value cannot be setup if you are initializing a fundamental_data object from a stock
* key: authentication key.
* verbose: used to see the request to Alpha Vantage (default false). This value cannot be setup if you are initializing a fundamental_data object from a stock
* datatype: it can be "json" or "csv" (default "json")
* file: path where a csv file should be saved (default "nil")

Specific information from Fundamental_Data can be retrieved using the following methods:

``` ruby
fundamental_data.overview
fundamental_data.balance_sheets
fundamental_data.income_statements
fundamental_data.cash_flow
```

<a name="Timeseries"></a>
## Alphavantage::Timeseries

Expand Down
6 changes: 6 additions & 0 deletions config/endpoints.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Endpoints
OVERVIEW = "OVERVIEW"
BALANCE_SHEET = "BALANCE_SHEET"
INCOME_STATEMENT = "INCOME_STATEMENT"
CASH_FLOW = "CASH_FLOW"
end
4 changes: 4 additions & 0 deletions lib/Client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ def crypto(symbol:, market:, datatype: "json")
Alphavantage::Crypto.new symbol: symbol, key: self, datatype: datatype, market: market
end

def fundamental_data(symbol:, datatype: "json")
Alphavantage::Fundamental_Data.new symbol: symbol, key: self, datatype: datatype, verbose: verbose
end

def sector
Alphavantage::Sector.new key: self
end
Expand Down
61 changes: 61 additions & 0 deletions lib/Fundamental_Data.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
module Alphavantage
class Fundamental_Data
include HelperFunctions

def initialize(symbol:, datatype: "json", key:, verbose: false)
check_argument([true, false], verbose, "verbose")
@client = return_client(key, verbose)
Comment thread
tylerhaugen-stanley marked this conversation as resolved.
@symbol = symbol
@datatype = datatype
end

def overview(file: nil, datatype: @datatype)
check_argument(["json", "csv"], datatype, "datatype")
make_request(file: file, datatype: datatype, endpoint: Endpoints::OVERVIEW)
end

def income_statements(file: nil, datatype: @datatype, period: :both)
check_argument([:both, :quarterly, :annually], period, "period")
payload = make_request(file: file, datatype: datatype, endpoint: Endpoints::INCOME_STATEMENT)

extract_period_data(payload, period)
end

def balance_sheets(file: nil, datatype: @datatype, period: :both)
check_argument([:both, :quarterly, :annually], period, "period")
payload = make_request(file: file, datatype: datatype, endpoint: Endpoints::BALANCE_SHEET)

extract_period_data(payload, period)
end

def cash_flow_statements(file: nil, datatype: @datatype, period: :both)
check_argument([:both, :quarterly, :annually], period, "period")
payload = make_request(file: file, datatype: datatype, endpoint: Endpoints::CASH_FLOW)

extract_period_data(payload, period)
end

private

def make_request(file: nil, datatype: @datatype, endpoint:)
check_datatype(datatype, file)
url = "function=#{endpoint}&symbol=#{@symbol}"
return @client.download(url, file) if datatype == "csv"
@client.request(url)
end

def extract_period_data(payload, period)
return quarterly(payload) if period == :quarterly
return annually(payload) if period == :annually
payload # Return both periods in one hash
end

def quarterly(payload)
payload['quarterlyReports']
end

def annually(payload)
payload['annualReports']
end
end
end
4 changes: 4 additions & 0 deletions lib/Stock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ def datatype=(datatype)
@datatype = datatype
end

def fundamental_data datatype: @datatype, file: nil
Alphavantage::Fundamental_Data.new symbol: @symbol, key: @client, datatype: datatype
end

def quote file: nil, datatype: @datatype
check_datatype(datatype, file)
url = "function=GLOBAL_QUOTE&symbol=#{symbol}"
Expand Down
2 changes: 2 additions & 0 deletions lib/alphavantagerb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require "humanize"
require "open-uri"
require "ostruct"
require_relative "../config/endpoints"
require_relative "helper_function"
require_relative "Timeseries"
require_relative "Indicator"
Expand All @@ -13,3 +14,4 @@
require_relative "Crypto_Timeseries"
require_relative "Sector"
require_relative "Client"
require_relative "Fundamental_Data"
5 changes: 5 additions & 0 deletions spec/lib/1.0.0/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,10 @@
sector = @client.sector
expect(sector.class).to eq Alphavantage::Sector
end

it "can create a new fundamental_data object from a client" do
fd = @client.fundamental_data symbol: "MSFT"
expect(fd.class).to eq Alphavantage::Fundamental_Data
end
end
end
Loading