finance

package module
Version: v0.0.0-...-0868c4d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 9, 2018 License: MIT Imports: 14 Imported by: 4

README

go-finance

GoDoc Build Status codecov.io Go Report Card License MIT

codecov.io

go-finance is a Go library for retrieving financial data for quantitative analysis.

Deprecation Warning!

This library will no longer be maintained due to several breaking yahoo finance api changes (surprise). The next gen iteration of this library that uses the newer apis (as well as a few other api integrations) will exist here - https://github.com/piquette/finance-go and is currently in early stages of development. Have an idea or want to get involved? @ me on twitter, @michael_ack. In the meantime, browse some memes - https://reddit.com/r/memes

---Deprecated---

To install go-finance, use the following command:

go get github.com/FlashBoys/go-finance

Features

Single security quotes
package main

import (
	"fmt"

	"github.com/FlashBoys/go-finance"
)

func main() {
	// 15-min delayed full quote for Apple.
	q, err := finance.GetQuote("AAPL")
	if err == nil {
		fmt.Println(q)
	}
}
Multiple securities quotes
package main

import (
	"fmt"

	"github.com/FlashBoys/go-finance"
)

func main() {
	// 15-min delayed full quotes for Apple, Twitter, and Facebook.
	symbols := []string{"AAPL", "TWTR", "FB"}
	quotes, err := finance.GetQuotes(symbols)
	if err == nil {
		fmt.Println(quotes)
	}
}
Currency pair quote
package main

import (
	"fmt"

	"github.com/FlashBoys/go-finance"
)

func main() {
	// Predefined pair constants
	// e.g
	//
	// USDJPY
	// EURUSD
	// NZDUSD
	//
	pairquote, err := finance.GetCurrencyPairQuote(finance.USDJPY)
	if err == nil {
		fmt.Println(pairquote)
	}
}
Quote history
package main

import (
	"fmt"

	"github.com/FlashBoys/go-finance"
)

func main() {
	// Set time frame to 1 month starting Jan. 1.
	start := finance.ParseDatetime("1/1/2017")
	end := finance.ParseDatetime("2/1/2017")

	// Request daily history for TWTR.
	// IntervalDaily OR IntervalWeekly OR IntervalMonthly are supported.
	bars, err := finance.GetHistory("TWTR", start, end, finance.Day)
	if err == nil {
		fmt.Println(bars)
	}
}
Dividend/Split event history
package main

import (
	"fmt"
	"time"

	"github.com/FlashBoys/go-finance"
)

func main() {
	// Set time range from Jan 2010 up to the current date.
	// This example will return a slice of either dividends or splits.
	start := finance.ParseDatetime("1/1/2010")
	end := finance.NewDatetime(time.Now())

	// Request event history for AAPL.
	events, err := finance.GetEventHistory("AAPL", start, end, finance.Dividends)
	if err == nil {
		fmt.Println(events)
	}
}
Symbols download
package main

import (
	"fmt"

	"github.com/FlashBoys/go-finance"
)

func main() {
	// Request all BATS symbols.
	symbols, err := finance.GetUSEquitySymbols()
	if err == nil {
		fmt.Println(symbols)
	}
}

Options chains
package main

import (
	"fmt"

	"github.com/FlashBoys/go-finance"
)

func main() {
	// Fetches the available expiration dates.
	c, err := finance.NewCycle("AAPL")
	if err != nil {
		panic(err)
	}

	// Some examples - see docs for full details.

	// Fetches the chain for the front month.
	calls, puts, err := c.GetFrontMonth()
	if err == nil {
		panic(err)
	}
	fmt.Println(calls)
	fmt.Println(puts)

	// Fetches the chain for the specified expiration date.
	calls, puts, err := c.GetChainForExpiration(chain.Expirations[1])
	if err == nil {
		panic(err)
	}
	fmt.Println(calls)
	fmt.Println(puts)

	// Fetches calls for the specified expiration date.
	calls, err := c.GetCallsForExpiration(chain.Expirations[1])
	if err == nil {
		panic(err)
	}
	fmt.Println(calls)
}

Intentions

The primary technical tenants of this project are:

  • Make financial data easy and fun to work with in Go.
  • Abstract the burden of non-sexy model serialization away from the end-user.
  • Provide a mature framework where the end-user needs only be concerned with analysis instead of data sourcing.

There are several applications for this library. It's intentions are to be conducive to the following activities:

  • Quantitative financial analysis in Go.
  • Academic study/comparison in a clean, easy language.
  • Algorithmic/Statistical-based strategy implementation.

API Changes

Yahoo decided to deprecate the ichart API for historical data. A few things to note:

  • Dividends and Splits got separated into their own calls, use finance.Dividends or finance.Splits.
  • A cookie and a crumb are now needed in the new historical API. This requires 2 calls, slowing down the response time/quality.
  • Continuation of the historical data funcs were made possible by the solution proposed by pandas contributors here, so thanks for the help!
    • That PR is also reporting a degradation of data quality in the responses, so watch out for that.

You can use the new health checking command to determine if all the endpoints are responding appropriately. Run go run main.go in the cmd/health directory and report any failures!

Contributing

If you find this repo helpful, please give it a star! If you wish to discuss changes to it, please open an issue. This project is not as mature as it could be, and financial projects in Go are in drastic need of some basic helpful dependencies.

Similar Projects

Documentation

Overview

Package finance implements a growing set of financial data retrieval functions. It aims to provide a clean, flexible, and easy way to pull in basic financial markets quotes and information for analytical purposes.

Features

- Single security quotes

- Multiple security quotes

- Historical quotes in custom time frames and intervals

- Dividend and Split history

- FX pair quotes for all major pairs

- Option chains for multiple future expiration dates

- Updating symbols list of traded US Equities

Index

Constants

View Source
const (
	// Day interval.
	Day = "1d"
	// Week interval.
	Week = "1wk"
	// Month interval.
	Month = "1mo"
	// Dividends event type.
	Dividends = "div"
	// Splits event type.
	Splits = "split"
)
View Source
const (
	// USDGBP pair.
	USDGBP = "USDGBP=X"
	// USDEUR pair.
	USDEUR = "USDEUR=X"
	// USDAUD pair.
	USDAUD = "USDAUD=X"
	// USDCHF pair.
	USDCHF = "USDCHF=X"
	// USDJPY pair.
	USDJPY = "USDJPY=X"
	// USDCAD pair.
	USDCAD = "USDCAD=X"
	// USDSGD pair.
	USDSGD = "USDSGD=X"
	// USDNZD pair.
	USDNZD = "USDNZD=X"
	// USDHKD pair.
	USDHKD = "USDHKD=X"
	// GBPUSD pair.
	GBPUSD = "GBPUSD=X"
	// GBPEUR pair.
	GBPEUR = "GBPEUR=X"
	// GBPAUD pair.
	GBPAUD = "GBPAUD=X"
	// GBPCHF pair.
	GBPCHF = "GBPCHF=X"
	// GBPJPY pair.
	GBPJPY = "GBPJPY=X"
	// GBPCAD pair.
	GBPCAD = "GBPCAD=X"
	// GBPSGD pair.
	GBPSGD = "GBPSGD=X"
	// GBPNZD pair.
	GBPNZD = "GBPNZD=X"
	// GBPHKD pair.
	GBPHKD = "GBPHKD=X"
	// EURUSD pair.
	EURUSD = "EURUSD=X"
	// EURGBP pair.
	EURGBP = "EURGBP=X"
	// EURAUD pair.
	EURAUD = "EURAUD=X"
	// EURCHF pair.
	EURCHF = "EURCHF=X"
	// EURJPY pair.
	EURJPY = "EURJPY=X"
	// EURCAD pair.
	EURCAD = "EURCAD=X"
	// EURSGD pair.
	EURSGD = "EURSGD=X"
	// EURNZD pair.
	EURNZD = "EURNZD=X"
	// EURHKD pair.
	EURHKD = "EURHKD=X"
	// AUDUSD pair.
	AUDUSD = "AUDUSD=X"
	// AUDGBP pair.
	AUDGBP = "AUDGBP=X"
	// AUDEUR pair.
	AUDEUR = "AUDEUR=X"
	// AUDCHF pair.
	AUDCHF = "AUDCHF=X"
	// AUDJPY pair.
	AUDJPY = "AUDJPY=X"
	// AUDCAD pair.
	AUDCAD = "AUDCAD=X"
	// AUDSGD pair.
	AUDSGD = "AUDSGD=X"
	// AUDNZD pair.
	AUDNZD = "AUDNZD=X"
	// AUDHKD pair.
	AUDHKD = "AUDHKD=X"
	// CHFGBP pair.
	CHFGBP = "CHFGBP=X"
	// CHFEUR pair.
	CHFEUR = "CHFEUR=X"
	// CHFAUD pair.
	CHFAUD = "CHFAUD=X"
	// CHFJPY pair.
	CHFJPY = "CHFJPY=X"
	// CHFCAD pair.
	CHFCAD = "CHFCAD=X"
	// CHFSGD pair.
	CHFSGD = "CHFSGD=X"
	// CHFNZD pair.
	CHFNZD = "CHFNZD=X"
	// CHFHKD pair.
	CHFHKD = "CHFHKD=X"
	// JPYUSD pair.
	JPYUSD = "JPYUSD=X"
	// JPYGBP pair.
	JPYGBP = "JPYGBP=X"
	// JPYEUR pair.
	JPYEUR = "JPYEUR=X"
	// JPYAUD pair.
	JPYAUD = "JPYAUD=X"
	// JPYCHF pair.
	JPYCHF = "JPYCHF=X"
	// JPYCAD pair.
	JPYCAD = "JPYCAD=X"
	// JPYSGD pair.
	JPYSGD = "JPYSGD=X"
	// JPYNZD pair.
	JPYNZD = "JPYNZD=X"
	// JPYHKD pair.
	JPYHKD = "JPYHKD=X"
	// CADUSD pair.
	CADUSD = "CADUSD=X"
	// CADGBP pair.
	CADGBP = "CADGBP=X"
	// CADEUR pair.
	CADEUR = "CADEUR=X"
	// CADAUD pair.
	CADAUD = "CADAUD=X"
	// CADCHF pair.
	CADCHF = "CADCHF=X"
	// CADJPY pair.
	CADJPY = "CADJPY=X"
	// CADSGD pair.
	CADSGD = "CADSGD=X"
	// CADNZD pair.
	CADNZD = "CADNZD=X"
	// CADHKD pair.
	CADHKD = "CADHKD=X"
	// SGDUSD pair.
	SGDUSD = "SGDUSD=X"
	// SGDGBP pair.
	SGDGBP = "SGDGBP=X"
	// SGDEUR pair.
	SGDEUR = "SGDEUR=X"
	// SGDAUD pair.
	SGDAUD = "SGDAUD=X"
	// SGDCHF pair.
	SGDCHF = "SGDCHF=X"
	// SGDJPY pair.
	SGDJPY = "SGDJPY=X"
	// SGDCAD pair.
	SGDCAD = "SGDCAD=X"
	// SGDNZD pair.
	SGDNZD = "SGDNZD=X"
	// SGDHKD pair.
	SGDHKD = "SGDHKD=X"
	// NZDUSD pair.
	NZDUSD = "NZDUSD=X"
	// NZDGBP pair.
	NZDGBP = "NZDGBP=X"
	// NZDEUR pair.
	NZDEUR = "NZDEUR=X"
	// NZDAUD pair.
	NZDAUD = "NZDAUD=X"
	// NZDCHF pair.
	NZDCHF = "NZDCHF=X"
	// NZDJPY pair.
	NZDJPY = "NZDJPY=X"
	// NZDCAD pair.
	NZDCAD = "NZDCAD=X"
	// NZDSGD pair.
	NZDSGD = "NZDSGD=X"
	// NZDHKD pair.
	NZDHKD = "NZDHKD=X"
	// HKDUSD pair.
	HKDUSD = "HKDUSD=X"
	// HKDGBP pair.
	HKDGBP = "HKDGBP=X"
	// HKDEUR pair.
	HKDEUR = "HKDEUR=X"
	// HKDAUD pair.
	HKDAUD = "HKDAUD=X"
	// HKDCHF pair.
	HKDCHF = "HKDCHF=X"
	// HKDJPY pair.
	HKDJPY = "HKDJPY=X"
	// HKDCAD pair.
	HKDCAD = "HKDCAD=X"
	// HKDSGD pair.
	HKDSGD = "HKDSGD=X"
	// HKDNZD pair.
	HKDNZD = "HKDNZD=X"
)

Variables

View Source
var (
	// OptionsURL option chains
	OptionsURL = "http://www.google.com/finance/option_chain?"
	// HistoryURL quote history
	HistoryURL = "https://query1.finance.yahoo.com/v7/finance/download/"
	// SymbolsURL symbols list
	SymbolsURL = "http://www.batstrading.com/market_data/symbol_listing/csv/"
	// QuoteURL stock quotes
	QuoteURL = "http://download.finance.yahoo.com/d/quotes.csv"
)

Functions

func GetUSEquitySymbols

func GetUSEquitySymbols() (symbols []string, err error)

GetUSEquitySymbols fetches the symbols available through BATS, ~8k symbols.

Types

type Bar

type Bar struct {
	Date     Datetime
	Open     decimal.Decimal
	High     decimal.Decimal
	Low      decimal.Decimal
	Close    decimal.Decimal
	AdjClose decimal.Decimal
	Volume   int
	Symbol   string `yfin:"-"`
}

Bar represents a single bar(candle) in time-series of quotes.

func GetHistory

func GetHistory(symbol string, start Datetime, end Datetime, interval Interval) (b []Bar, err error)

GetHistory fetches a single symbol's quote history from Yahoo Finance.

type Contract

type Contract struct {
	ID            string
	Security      string
	Strike        decimal.Decimal
	Price         decimal.Decimal
	Change        decimal.Decimal
	ChangePercent decimal.Decimal
	Bid           decimal.Decimal
	Ask           decimal.Decimal
	Volume        int
	OpenInterest  int
}

Contract represents an instance of an option contract.

type Datetime

type Datetime struct {
	Month  int `json:"m,string"`
	Day    int `json:"d,string"`
	Year   int `json:"y,string"`
	Hour   int `json:",omitempty"`
	Minute int `json:",omitempty"`
	Second int `json:",omitempty"`
	// contains filtered or unexported fields
}

Datetime is a simple time construct.

func NewDatetime

func NewDatetime(t time.Time) Datetime

NewDatetime creates a new instance of Datetime.

func ParseDatetime

func ParseDatetime(s string) Datetime

ParseDatetime creates a new instance of Datetime from a string.

type Event

type Event struct {
	Date   Datetime
	Val    Value
	Type   EventType `yfin:"-"`
	Symbol string    `yfin:"-"`
}

Event contains one historical event (either a split or a dividend).

func GetEventHistory

func GetEventHistory(symbol string, start Datetime, end Datetime, eventType EventType) (e []Event, err error)

GetEventHistory fetches a single symbol's dividend and split history from Yahoo Finance.

type EventType

type EventType string

EventType is the type of history event, either divs or splits.

type FXPairQuote

type FXPairQuote struct {
	Symbol           string          `yfin:"s"`
	PairName         string          `yfin:"n"`
	LastTradeTime    Datetime        `yfin:"t1"`
	LastTradeDate    Datetime        `yfin:"d1"`
	LastRate         decimal.Decimal `yfin:"l1"`
	ChangeNominal    decimal.Decimal `yfin:"c1"`
	ChangePercent    decimal.Decimal `yfin:"p2"`
	DayLow           decimal.Decimal `yfin:"g"`
	DayHigh          decimal.Decimal `yfin:"h"`
	FiftyTwoWeekLow  decimal.Decimal `yfin:"j"`
	FiftyTwoWeekHigh decimal.Decimal `yfin:"k"`
}

FXPairQuote represents the quote of a currency pair.

func GetCurrencyPairQuote

func GetCurrencyPairQuote(symbol string) (fq FXPairQuote, err error)

GetCurrencyPairQuote fetches a single currency pair's quote from Yahoo Finance.

type Interval

type Interval string

Interval is the duration of the bars returned from the query.

type OptionsCycle

type OptionsCycle struct {
	Symbol          string
	UnderlyingPrice decimal.Decimal
	Expirations     []Datetime
}

OptionsCycle contains the list of expirations for a symbol.

func NewCycle

func NewCycle(symbol string) (oc OptionsCycle, err error)

NewCycle creates a new OptionsCycle instance.

func (*OptionsCycle) GetCallsForExpiration

func (c *OptionsCycle) GetCallsForExpiration(e Datetime) (calls []Contract, err error)

GetCallsForExpiration fetches calls for the given expiration date.

func (*OptionsCycle) GetChainForExpiration

func (c *OptionsCycle) GetChainForExpiration(e Datetime) (calls []Contract, puts []Contract, err error)

GetChainForExpiration fetches the option chain for the given expiration datetime.

func (*OptionsCycle) GetFrontMonth

func (c *OptionsCycle) GetFrontMonth() (calls []Contract, puts []Contract, err error)

GetFrontMonth fetches the option chain for the front month.

func (*OptionsCycle) GetPutsForExpiration

func (c *OptionsCycle) GetPutsForExpiration(e Datetime) (puts []Contract, err error)

GetPutsForExpiration fetches puts for the given expiration date.

type Quote

type Quote struct {
	Symbol             string          `yfin:"s"`
	Name               string          `yfin:"n"`
	LastTradeTime      Datetime        `yfin:"t1"`
	LastTradeDate      Datetime        `yfin:"d1"`
	LastTradePrice     decimal.Decimal `yfin:"l1"`
	LastTradeSize      int             `yfin:"k3"`
	Ask                decimal.Decimal `yfin:"a"`
	AskSize            int             `yfin:"a5"`
	Bid                decimal.Decimal `yfin:"b"`
	BidSize            int             `yfin:"b6"`
	Volume             int             `yfin:"v"`
	ChangeNominal      decimal.Decimal `yfin:"c1"`
	ChangePercent      decimal.Decimal `yfin:"p2"`
	Open               decimal.Decimal `yfin:"o"`
	PreviousClose      decimal.Decimal `yfin:"p"`
	Exchange           string          `yfin:"x"`
	DayLow             decimal.Decimal `yfin:"g"`
	DayHigh            decimal.Decimal `yfin:"h"`
	FiftyTwoWeekLow    decimal.Decimal `yfin:"j"`
	FiftyTwoWeekHigh   decimal.Decimal `yfin:"k"`
	Currency           string          `yfin:"c4"`
	MarketCap          string          `yfin:"j1"`
	FiftyDayMA         decimal.Decimal `yfin:"m3"`
	TwoHundredDayMA    decimal.Decimal `yfin:"m4"`
	AvgDailyVolume     int             `yfin:"a2"`
	FiftyTwoWeekTarget decimal.Decimal `yfin:"t8"`
	ShortRatio         decimal.Decimal `yfin:"s7"`
	BookValue          decimal.Decimal `yfin:"b4"`
	EBITDA             string          `yfin:"j4"`
	PriceSales         decimal.Decimal `yfin:"p5"`
	PriceBook          decimal.Decimal `yfin:"p6"`
	PERatio            decimal.Decimal `yfin:"r"`
	PEGRatio           decimal.Decimal `yfin:"r5"`
	DivYield           decimal.Decimal `yfin:"y"`
	DivPerShare        decimal.Decimal `yfin:"d"`
	DivExDate          Datetime        `yfin:"q"`
	DivPayDate         Datetime        `yfin:"r1"`
	EPS                decimal.Decimal `yfin:"e"`
	EPSEstCurrentYear  decimal.Decimal `yfin:"e7"`
	EPSEstNextYear     decimal.Decimal `yfin:"e8"`
	EPSEstNextQuarter  decimal.Decimal `yfin:"e9"`
}

Quote is the object that is returned for a quote inquiry.

func GetQuote

func GetQuote(symbol string) (q Quote, err error)

GetQuote fetches a single symbol's quote from Yahoo Finance.

func GetQuotes

func GetQuotes(symbols []string) (q []Quote, err error)

GetQuotes fetches multiple symbol's quotes from Yahoo Finance.

type Value

type Value struct {
	Dividend decimal.Decimal
	Ratio    string
}

Value is an event object that contains either a div amt or a split ratio.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto