Skip to content

Commit

Permalink
Adds option expirations.
Browse files Browse the repository at this point in the history
  • Loading branch information
ackleymi committed Aug 31, 2018
1 parent 0043550 commit 76d8bea
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 9 deletions.
79 changes: 75 additions & 4 deletions cmd/options/options.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package options

import (
"fmt"
"os"
"time"

tw "github.com/olekukonko/tablewriter"
finance "github.com/piquette/finance-go"
"github.com/piquette/finance-go/datetime"
"github.com/piquette/finance-go/options"
"github.com/piquette/qtrn/utils"
"github.com/spf13/cobra"
Expand All @@ -26,14 +29,52 @@ var (
Example: "qtrn options AAPL",
RunE: execute,
}

// listExpirationsF set flag to list the available expiration dates for the supplied symbol.
listExpirationsF bool
// expirationF set flag to specify expiration date for the supplied symbol.
expirationF string
)

// execute implements the quote command
func init() {
Cmd.Flags().BoolVarP(&listExpirationsF, "list", "l", false, "list the available expiration dates for the supplied symbol. default is false.")
Cmd.Flags().StringVarP(&expirationF, "exp", "e", "", "set flag to specify expiration date for the supplied symbol. (formatted yyyy-mm-dd)")
}

// execute implements the options command
func execute(cmd *cobra.Command, args []string) error {
// check symbol.
symbols := args
if len(symbols) == 0 {
return fmt.Errorf("no symbols provided")
}

// fetch options.
p := &options.Params{
UnderlyingSymbol: symbols[0],
}
// add expiration.
if expirationF != "" {
dt, err := time.Parse("2006-01-02", expirationF)
if err != nil {
return fmt.Errorf("could not parse expiration- correct format is yyyy-mm-dd")
}
p.Expiration = datetime.New(&dt)
}
iter := options.GetStraddleP(p)

if listExpirationsF {
return writeE(iter)
}

symbol := args[0]
// write straddles.
return write(iter)
}

// write writes the straddle table.
func write(iter *options.StraddleIter) error {
// iterate.
straddles := []*finance.Straddle{}
iter := options.GetStraddle(symbol)
for iter.Next() {
straddles = append(straddles, iter.Straddle())
}
Expand All @@ -47,13 +88,43 @@ func execute(cmd *cobra.Command, args []string) error {
table.SetAlignment(tw.ALIGN_LEFT)
table.SetCenterSeparator("*")
table.SetColumnSeparator("|")
table.SetHeader([]string{"", "", "CALLS", "", "", utils.DateFS(iter.Meta().ExpirationDate), "", "", "PUTS", "", ""})
table.SetHeader([]string{"", "", "Calls", "", "", utils.DateFS(iter.Meta().ExpirationDate + 86400), "", "", "Puts", "", ""})
table.AppendBulk(build(straddles))
table.Render()

return nil
}

// writeE writes the expiration dates table.
func writeE(iter *options.StraddleIter) error {
// iterate.
meta := iter.Meta()
if meta == nil {
return fmt.Errorf("could not retrieve dates")
}

dates := [][]string{}
for _, stamp := range meta.AllExpirationDates {
// set the day to friday instead of EOD thursday..
// weird math here..
stamp = stamp + 86400
t := time.Unix(int64(stamp), 0)
dates = append(dates, []string{t.Format("2006-01-02")})
}

// Create table writer.
table := tw.NewWriter(os.Stdout)
table.SetAutoWrapText(false)
table.SetAlignment(tw.ALIGN_LEFT)
table.SetCenterSeparator("*")
table.SetColumnSeparator("|")
table.SetHeader([]string{"Exp. Dates"})
table.AppendBulk(dates)
table.Render()

return nil
}

// build builds table lines.
func build(ss []*finance.Straddle) (tbl [][]string) {
// Get fields.
Expand Down
9 changes: 4 additions & 5 deletions cmd/write/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ const (
usage = "write"
short = "Writes a csv of stock market data"
long = "Writes a csv of stock market data into the current directory."
quoteShort = "Writes a csv of a stock quote"
quoteLong = "Writes a csv of a stock quote and can accomodate multiple symbols as arguments"
historyShort = "Writes a csv of a historical data"
historyLong = "Writes a csv of a historical data, can only accept one symbol at a time"
quoteShort = "Writes a csv of stock quotes"
quoteLong = "Writes a csv of stock quotes and can accept multiple symbols as arguments"
historyShort = "Writes a csv of historical data"
historyLong = "Writes a csv of historical data, can only accept one symbol at a time"
)

var (
Expand Down Expand Up @@ -220,7 +220,6 @@ func formatC(iter *chart.Iter) (data [][]string, err error) {
utils.ToString(b.Volume),
iter.Meta().Symbol,
}
fmt.Println(data)
data = append(data, p)
}
return data, iter.Err()
Expand Down

0 comments on commit 76d8bea

Please sign in to comment.