Skip to content

Commit

Permalink
Refactors datetime struct into its own package.
Browse files Browse the repository at this point in the history
  • Loading branch information
ackleymi committed Jul 8, 2018
1 parent 39aaa44 commit 8940312
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 42 deletions.
35 changes: 17 additions & 18 deletions chart/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"

finance "github.com/piquette/finance-go"
"github.com/piquette/finance-go/datetime"
form "github.com/piquette/finance-go/form"
"github.com/shopspring/decimal"
)
Expand All @@ -23,19 +24,17 @@ type Params struct {
finance.Params `form:"-"`

// Accessible fields.
Symbol string `form:"-"`
Start *Datetime `form:"-"`
End *Datetime `form:"-"`
Interval Interval `form:"-"`
Symbol string `form:"-"`
Start *datetime.Datetime `form:"-"`
End *datetime.Datetime `form:"-"`
Interval datetime.Interval `form:"-"`

IncludeExt bool `form:"includePrePost"`

// Internal request fields.
interval string `form:"interval"`
start int `form:"period1"`
end int `form:"period2"`
region string `form:"region"`
domain string `form:"corsDomain"`
}

// Iter is a structure containing results
Expand Down Expand Up @@ -67,25 +66,25 @@ func Get(params *Params) *Iter {
// Get returns a historical chart.
func (c Client) Get(params *Params) *Iter {

if params.Context == nil {
ctx := context.TODO()
params.Context = &ctx
}

// Construct request from params input.
// TODO: validate symbol..
if params == nil || len(params.Symbol) == 0 {
return &Iter{finance.GetErrIter(finance.CreateArgumentError())}
}

// Start and End times.
if params.Context == nil {
ctx := context.TODO()
params.Context = &ctx
}

// Start and End times
params.start = -1
params.end = -1
if params.Start != nil {
params.start = params.Start.ToUnix()
params.start = params.Start.Unix()
}
if params.End != nil {
params.end = params.End.ToUnix()
params.end = params.End.Unix()
}
if params.start > params.end {
return &Iter{finance.GetErrIter(finance.CreateChartTimeError())}
Expand All @@ -96,12 +95,12 @@ func (c Client) Get(params *Params) *Iter {
params.interval = string(params.Interval)
}

// Set meta data.
params.domain = "com.finance.yahoo"
params.region = "US"

// Build request.
body := &form.Values{}
form.AppendTo(body, params)
// Set request meta data.
body.Set("region", "US")
body.Set("corsDomain", "com.finance.yahoo")

return &Iter{finance.GetChartIter(body, func(b *form.Values) (m interface{}, bars []interface{}, err error) {

Expand Down
30 changes: 14 additions & 16 deletions chart/time.go → datetime/datetime.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package chart
package datetime

import (
"time"
Expand Down Expand Up @@ -58,18 +58,24 @@ type Datetime struct {
t *time.Time
}

// NewDatetime creates a new instance of Datetime.
func NewDatetime(t time.Time) *Datetime {
// New creates a new instance of Datetime from a go time struct.
func New(t *time.Time) *Datetime {
year, month, day := t.Date()
return &Datetime{
Month: int(month),
Day: day,
Year: year,
t: &t,
t: t,
}
}

// Time returns a time object from a datetime.
// FromUnix returns a new instance of Datetime from a unix timestamp.
func FromUnix(timestamp int) *Datetime {
t := time.Unix(int64(timestamp), 0)
return New(&t)
}

// Time returns a go time struct from a datetime.
func (d *Datetime) Time() *time.Time {
if d.t != nil {
return d.t
Expand All @@ -78,22 +84,14 @@ func (d *Datetime) Time() *time.Time {
return d.Time()
}

// ToUnix converts a Datetime struct to
// a valid unix timestamp.
func (d *Datetime) ToUnix() int {
// Unix returns a valid unix timestamp from Datetime fields.
func (d *Datetime) Unix() int {
if d.t != nil {
return int(d.t.Unix())
}

d.calculateTime()
return d.ToUnix()
}

// NewDatetimeU converts a valid unix timestamp
// to a datetime object.
func NewDatetimeU(timestamp int) *Datetime {
t := time.Unix(int64(timestamp), 0)
return NewDatetime(t)
return d.Unix()
}

func (d *Datetime) calculateTime() {
Expand Down
5 changes: 3 additions & 2 deletions examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/piquette/finance-go/chart"
"github.com/piquette/finance-go/datetime"
"github.com/piquette/finance-go/quote"
)

Expand All @@ -24,13 +25,13 @@ func main() {
// --------------------
params := &chart.Params{
Symbol: "TWTR",
Interval: chart.OneHour,
Interval: datetime.OneHour,
}
iter := chart.Get(params)

for iter.Next() {
b := iter.Bar()
fmt.Println(chart.NewDatetimeU(b.Timestamp))
fmt.Println(datetime.FromUnix(b.Timestamp))

}
if iter.Err() != nil {
Expand Down
3 changes: 1 addition & 2 deletions iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ type ChartQuery func(*form.Values) (interface{}, []interface{}, error)
// for iterating over the elements
// returned from paginated list API calls.
// Successive calls to the Next method
// will step through each item in the list,
// fetching pages of items as needed.
// will step through each item in the list.
// Iterators are not thread-safe, so they should not be consumed
// across multiple goroutines.
type Iter struct {
Expand Down
51 changes: 47 additions & 4 deletions options/client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package options

import finance "github.com/piquette/finance-go"
import (
"context"

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

// Client is used to invoke options APIs.
type Client struct {
Expand All @@ -11,6 +15,13 @@ func getC() Client {
return Client{finance.GetBackend(finance.YFinBackend)}
}

type format int

const (
optionchain format = iota
optionstraddle
)

// Params carries a context and chart information.
type Params struct {
// Context access.
Expand All @@ -21,13 +32,45 @@ type Params struct {
//Start *Datetime `form:"-"`
}

// Iter is a structure containing results
// StraddleIter is a structure containing results
// and related metadata for a
// yfin options request.
type Iter struct {
// yfin option straddles request.
type StraddleIter struct {
*finance.Iter
}

// GetStraddle returns options straddles.
// and requires a underlier symbol as an argument.
func GetStraddle(underlier string) *StraddleIter {
return GetStraddleP(&Params{Underlier: underlier})
}

// GetStraddleP returns options straddles.
// and requires a params struct as an argument.
func GetStraddleP(params *Params) *StraddleIter {
return getC().GetStraddleP(params)
}

// GetStraddleP returns options straddles.
// and requires a params struct as an argument.
func (c Client) GetStraddleP(params *Params) *StraddleIter {

// Construct request from params input.
// TODO: validate symbol..
if params == nil || len(params.Underlier) == 0 {
return &StraddleIter{finance.GetErrIter(finance.CreateArgumentError())}
}

if params.Context == nil {
ctx := context.TODO()
params.Context = &ctx
}

// ---

return nil
}

// response is a yfin option response.
type response struct {
Inner struct {
Expand Down

0 comments on commit 8940312

Please sign in to comment.