Skip to content

Commit

Permalink
Merge pull request #30 from actfong/add_earnings
Browse files Browse the repository at this point in the history
Add earnings
  • Loading branch information
timpalpant authored Jan 19, 2019
2 parents 0b8a5fd + a8b5d23 commit aef4b1f
Show file tree
Hide file tree
Showing 5 changed files with 290 additions and 0 deletions.
26 changes: 26 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,32 @@ func (c *Client) GetDividends(symbol string) ([]*Dividends, error) {
return result, nil
}

// GetEarnings gets earnings from the four most recent reported quarters.
func (c *Client) GetEarnings(symbol string) (*EarningsReport, error) {
var result *EarningsReport
err := c.getJSON("/stock/"+symbol+"/earnings", nil, &result)
if err != nil {
return nil, err
}
return result, nil
}

// GetFinancials Pulls income statement, balance sheet
// and cash flow data from the four most recent reported periods.
// The default period is "quarter", unless "annual" is provided
func (c *Client) GetFinancials(symbol string, period_optional ...string) (*FinancialsReport, error) {
var result *FinancialsReport
period := "quarter"
if len(period_optional) > 0 && period_optional[0] == "annual" {
period = "annual"
}
err := c.getJSON("/stock/"+symbol+"/financials"+"?period="+period, nil, &result)
if err != nil {
return nil, err
}
return result, nil
}

// GetChart retuns chart data for a symbol covering a date range.
// Range can be: 5y 2y 1y ytd 6m 3d 1m 1d
// Please note the 1d range returns different data than other formats.
Expand Down
57 changes: 57 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,63 @@ func TestGetDividends(t *testing.T) {
}
}

func TestGetEarnings(t *testing.T) {
// this file contains data from here:
// https://api.iextrading.com/1.0/stock/aapl/earnings
body, err := readTestData("earnings.json")
if err != nil {
t.Fatal(err)
}

httpc := mockHTTPClient{body: body, code: 200}
c := NewClient(&httpc)

symbol := "AAPL"

result, err := c.GetEarnings(symbol)
if err != nil {
t.Fatal(err)
}

if result.Symbol != symbol {
t.Fatalf("Returned unexpected symbol %s should be %s", result.Symbol, symbol)
}

earnings := result.Earnings
expected := 4
if len(earnings) != expected {
t.Fatalf("Returned unexpected count %d should be %d", len(earnings), expected)
}
}

func TestGetFinancials(t *testing.T) {
// this file contains data from here:
// https://api.iextrading.com/1.0/stock/aapl/financials
body, err := readTestData("financials.json")
if err != nil {
t.Fatal(err)
}

httpc := mockHTTPClient{body: body, code: 200}
c := NewClient(&httpc)

symbol := "AAPL"

result, err := c.GetFinancials(symbol)
if err != nil {
t.Fatal(err)
}

if result.Symbol != symbol {
t.Fatalf("Returned unexpected symbol %s should be %s", result.Symbol, symbol)
}

financials := result.Financials
expected := 4
if len(financials) != expected {
t.Fatalf("Returned unexpected count %d should be %d", len(financials), expected)
}
}
func TestMarkets(t *testing.T) {
c := setupTestClient()
markets, err := c.GetMarkets()
Expand Down
49 changes: 49 additions & 0 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,55 @@ type Dividends struct {
Indicated float64 // refers to the indicated rate of the dividend
}

type EarningsReport struct {
Symbol string
Earnings []*Earning
}

type Earning struct {
ActualEPS float64 // Actual earnings per share for the period
ConsensusEPS float64 // Consensus EPS estimate trend for the period
EstimatedEPS float64 // Earnings per share estimate for the period
AnnounceTime string // Time of earnings announcement. BTO (Before open), DMT (During trading), AMC (After close)
NumberOfEstimates float64 // Number of estimates for the period
EPSSurpriseDollar float64 // Dollar amount of EPS surprise for the period
EPSReportDate string // Expected earnings report date YYYY-MM-DD
FiscalPeriod string // The fiscal quarter the earnings data applies to Q# YYYY
FiscalEndDate string // Date representing the company fiscal quarter end YYYY-MM-DD
YearAgo float64 // Represents the EPS of the quarter a year ago
YearAgoChangePercent float64 // Represents the percent difference between the quarter a year ago actualEPS and current period actualEPS.
EstimatedChangePercent float64 // Represents the percent difference between the quarter a year ago actualEPS and current period estimatedEPS.
SymbolId float64 // Represents the IEX id for the stock
}

type FinancialsReport struct {
Symbol string
Financials []*Financial
}

type Financial struct {
ReportDate string
GrossProfit int64
CostOfRevenue int64
OperatingRevenue int64
TotalRevenue int64
OperatingIncome int64
NetIncome int64
ResearchAndDevelopment int64
OperatingExpense int64
CurrentAssets int64
TotalAssets int64
TotalLiabilities int64
CurrentCash int64
CurrentDebt int64
TotalCash int64
TotalDebt int64
ShareholderEquity int64
CashChange int64
CashFlow int64
OperatingGainsLosses string
}

type Chart struct {
// Only available on 1d charts
Minute string
Expand Down
65 changes: 65 additions & 0 deletions testdata/responses/earnings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
{
"symbol": "AAPL",
"earnings": [
{
"actualEPS": 2.91,
"consensusEPS": 2.79,
"estimatedEPS": 2.79,
"announceTime": "AMC",
"numberOfEstimates": 12,
"EPSSurpriseDollar": 0.12,
"EPSReportDate": "2018-11-01",
"fiscalPeriod": "Q4 2018",
"fiscalEndDate": "2018-09-30",
"yearAgo": 2.07,
"yearAgoChangePercent": 0.40579710144927555,
"estimatedChangePercent": 0.34782608695652184,
"symbolId": 11
},
{
"actualEPS": 2.34,
"consensusEPS": 2.17,
"estimatedEPS": 2.17,
"announceTime": "AMC",
"numberOfEstimates": 10,
"EPSSurpriseDollar": 0.17,
"EPSReportDate": "2018-07-31",
"fiscalPeriod": "Q3 2018",
"fiscalEndDate": "2018-06-30",
"yearAgo": 1.67,
"yearAgoChangePercent": 0.40119760479041916,
"estimatedChangePercent": 0.29940119760479045,
"symbolId": 11
},
{
"actualEPS": 2.73,
"consensusEPS": 2.69,
"estimatedEPS": 2.69,
"announceTime": "AMC",
"numberOfEstimates": 10,
"EPSSurpriseDollar": 0.04,
"EPSReportDate": "2018-05-01",
"fiscalPeriod": "Q2 2018",
"fiscalEndDate": "2018-03-31",
"yearAgo": 2.1,
"yearAgoChangePercent": 0.29999999999999993,
"estimatedChangePercent": 0.2809523809523809,
"symbolId": 11
},
{
"actualEPS": 3.89,
"consensusEPS": 3.82,
"estimatedEPS": 3.82,
"announceTime": "AMC",
"numberOfEstimates": 9,
"EPSSurpriseDollar": 0.07,
"EPSReportDate": "2018-02-01",
"fiscalPeriod": "Q1 2018",
"fiscalEndDate": "2017-12-31",
"yearAgo": 3.36,
"yearAgoChangePercent": 0.15773809523809532,
"estimatedChangePercent": 0.1369047619047619,
"symbolId": 11
}
]
}
93 changes: 93 additions & 0 deletions testdata/responses/financials.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
{
"symbol": "AAPL",
"financials": [
{
"reportDate": "2018-09-30",
"grossProfit": 24084000000,
"costOfRevenue": 38816000000,
"operatingRevenue": 62900000000,
"totalRevenue": 62900000000,
"operatingIncome": 16118000000,
"netIncome": 14125000000,
"researchAndDevelopment": 3750000000,
"operatingExpense": 7966000000,
"currentAssets": 131339000000,
"totalAssets": 365725000000,
"totalLiabilities": 258578000000,
"currentCash": 25913000000,
"currentDebt": 20748000000,
"totalCash": 66301000000,
"totalDebt": 114483000000,
"shareholderEquity": 107147000000,
"cashChange": -6058000000,
"cashFlow": 19523000000,
"operatingGainsLosses": null
},
{
"reportDate": "2018-06-30",
"grossProfit": 20421000000,
"costOfRevenue": 32844000000,
"operatingRevenue": 53265000000,
"totalRevenue": 53265000000,
"operatingIncome": 12612000000,
"netIncome": 11519000000,
"researchAndDevelopment": 3701000000,
"operatingExpense": 7809000000,
"currentAssets": 115761000000,
"totalAssets": 349197000000,
"totalLiabilities": 234248000000,
"currentCash": 31971000000,
"currentDebt": 17472000000,
"totalCash": 70970000000,
"totalDebt": 114600000000,
"shareholderEquity": 114949000000,
"cashChange": -13088000000,
"cashFlow": 14488000000,
"operatingGainsLosses": null
},
{
"reportDate": "2018-03-31",
"grossProfit": 23422000000,
"costOfRevenue": 37715000000,
"operatingRevenue": 61137000000,
"totalRevenue": 61137000000,
"operatingIncome": 15894000000,
"netIncome": 13822000000,
"researchAndDevelopment": 3378000000,
"operatingExpense": 7528000000,
"currentAssets": 130053000000,
"totalAssets": 367502000000,
"totalLiabilities": 240624000000,
"currentCash": 45059000000,
"currentDebt": 20478000000,
"totalCash": 87940000000,
"totalDebt": 121840000000,
"shareholderEquity": 126878000000,
"cashChange": 17568000000,
"cashFlow": 15130000000,
"operatingGainsLosses": null
},
{
"reportDate": "2017-12-31",
"grossProfit": 33912000000,
"costOfRevenue": 54381000000,
"operatingRevenue": 88293000000,
"totalRevenue": 88293000000,
"operatingIncome": 26274000000,
"netIncome": 20065000000,
"researchAndDevelopment": 3407000000,
"operatingExpense": 7638000000,
"currentAssets": 143810000000,
"totalAssets": 406794000000,
"totalLiabilities": 266595000000,
"currentCash": 27491000000,
"currentDebt": 18478000000,
"totalCash": 77153000000,
"totalDebt": 122400000000,
"shareholderEquity": 140199000000,
"cashChange": 7202000000,
"cashFlow": 28293000000,
"operatingGainsLosses": null
}
]
}

0 comments on commit aef4b1f

Please sign in to comment.