Skip to content

Commit 76d8bea

Browse files
committed
Adds option expirations.
1 parent 0043550 commit 76d8bea

File tree

2 files changed

+79
-9
lines changed

2 files changed

+79
-9
lines changed

cmd/options/options.go

Lines changed: 75 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package options
22

33
import (
4+
"fmt"
45
"os"
6+
"time"
57

68
tw "github.com/olekukonko/tablewriter"
79
finance "github.com/piquette/finance-go"
10+
"github.com/piquette/finance-go/datetime"
811
"github.com/piquette/finance-go/options"
912
"github.com/piquette/qtrn/utils"
1013
"github.com/spf13/cobra"
@@ -26,14 +29,52 @@ var (
2629
Example: "qtrn options AAPL",
2730
RunE: execute,
2831
}
32+
33+
// listExpirationsF set flag to list the available expiration dates for the supplied symbol.
34+
listExpirationsF bool
35+
// expirationF set flag to specify expiration date for the supplied symbol.
36+
expirationF string
2937
)
3038

31-
// execute implements the quote command
39+
func init() {
40+
Cmd.Flags().BoolVarP(&listExpirationsF, "list", "l", false, "list the available expiration dates for the supplied symbol. default is false.")
41+
Cmd.Flags().StringVarP(&expirationF, "exp", "e", "", "set flag to specify expiration date for the supplied symbol. (formatted yyyy-mm-dd)")
42+
}
43+
44+
// execute implements the options command
3245
func execute(cmd *cobra.Command, args []string) error {
46+
// check symbol.
47+
symbols := args
48+
if len(symbols) == 0 {
49+
return fmt.Errorf("no symbols provided")
50+
}
51+
52+
// fetch options.
53+
p := &options.Params{
54+
UnderlyingSymbol: symbols[0],
55+
}
56+
// add expiration.
57+
if expirationF != "" {
58+
dt, err := time.Parse("2006-01-02", expirationF)
59+
if err != nil {
60+
return fmt.Errorf("could not parse expiration- correct format is yyyy-mm-dd")
61+
}
62+
p.Expiration = datetime.New(&dt)
63+
}
64+
iter := options.GetStraddleP(p)
65+
66+
if listExpirationsF {
67+
return writeE(iter)
68+
}
3369

34-
symbol := args[0]
70+
// write straddles.
71+
return write(iter)
72+
}
73+
74+
// write writes the straddle table.
75+
func write(iter *options.StraddleIter) error {
76+
// iterate.
3577
straddles := []*finance.Straddle{}
36-
iter := options.GetStraddle(symbol)
3778
for iter.Next() {
3879
straddles = append(straddles, iter.Straddle())
3980
}
@@ -47,13 +88,43 @@ func execute(cmd *cobra.Command, args []string) error {
4788
table.SetAlignment(tw.ALIGN_LEFT)
4889
table.SetCenterSeparator("*")
4990
table.SetColumnSeparator("|")
50-
table.SetHeader([]string{"", "", "CALLS", "", "", utils.DateFS(iter.Meta().ExpirationDate), "", "", "PUTS", "", ""})
91+
table.SetHeader([]string{"", "", "Calls", "", "", utils.DateFS(iter.Meta().ExpirationDate + 86400), "", "", "Puts", "", ""})
5192
table.AppendBulk(build(straddles))
5293
table.Render()
5394

5495
return nil
5596
}
5697

98+
// writeE writes the expiration dates table.
99+
func writeE(iter *options.StraddleIter) error {
100+
// iterate.
101+
meta := iter.Meta()
102+
if meta == nil {
103+
return fmt.Errorf("could not retrieve dates")
104+
}
105+
106+
dates := [][]string{}
107+
for _, stamp := range meta.AllExpirationDates {
108+
// set the day to friday instead of EOD thursday..
109+
// weird math here..
110+
stamp = stamp + 86400
111+
t := time.Unix(int64(stamp), 0)
112+
dates = append(dates, []string{t.Format("2006-01-02")})
113+
}
114+
115+
// Create table writer.
116+
table := tw.NewWriter(os.Stdout)
117+
table.SetAutoWrapText(false)
118+
table.SetAlignment(tw.ALIGN_LEFT)
119+
table.SetCenterSeparator("*")
120+
table.SetColumnSeparator("|")
121+
table.SetHeader([]string{"Exp. Dates"})
122+
table.AppendBulk(dates)
123+
table.Render()
124+
125+
return nil
126+
}
127+
57128
// build builds table lines.
58129
func build(ss []*finance.Straddle) (tbl [][]string) {
59130
// Get fields.

cmd/write/write.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ const (
3232
usage = "write"
3333
short = "Writes a csv of stock market data"
3434
long = "Writes a csv of stock market data into the current directory."
35-
quoteShort = "Writes a csv of a stock quote"
36-
quoteLong = "Writes a csv of a stock quote and can accomodate multiple symbols as arguments"
37-
historyShort = "Writes a csv of a historical data"
38-
historyLong = "Writes a csv of a historical data, can only accept one symbol at a time"
35+
quoteShort = "Writes a csv of stock quotes"
36+
quoteLong = "Writes a csv of stock quotes and can accept multiple symbols as arguments"
37+
historyShort = "Writes a csv of historical data"
38+
historyLong = "Writes a csv of historical data, can only accept one symbol at a time"
3939
)
4040

4141
var (
@@ -220,7 +220,6 @@ func formatC(iter *chart.Iter) (data [][]string, err error) {
220220
utils.ToString(b.Volume),
221221
iter.Meta().Symbol,
222222
}
223-
fmt.Println(data)
224223
data = append(data, p)
225224
}
226225
return data, iter.Err()

0 commit comments

Comments
 (0)