diff --git a/cmd/options/options.go b/cmd/options/options.go index 182c132..2b68667 100644 --- a/cmd/options/options.go +++ b/cmd/options/options.go @@ -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" @@ -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()) } @@ -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. diff --git a/cmd/write/write.go b/cmd/write/write.go index 4a3f958..cfdd941 100644 --- a/cmd/write/write.go +++ b/cmd/write/write.go @@ -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 ( @@ -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()