11package options
22
33import (
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
3245func 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.
58129func build (ss []* finance.Straddle ) (tbl [][]string ) {
59130 // Get fields.
0 commit comments