Skip to content

Commit

Permalink
Changed to a bool parse from original print
Browse files Browse the repository at this point in the history
  • Loading branch information
marcusmoller committed Oct 22, 2024
1 parent 051c2f0 commit 1548801
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 23 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,25 @@ $ ticker --config=./.ticker.yaml print
* Ensure there is at least one lot in the configuration file in order to generate output
* A specific config file can be specified with the `--config` flag

#### Printing Holdings Summary

Use the `--summary` flag for printing the holding summary, showing day change, total change, value and cost of portfolio.

```sh
$ ticker --config=./.ticker.yaml print --summary
Day change: -192.30 (-1.05%)
Total change: -13296.86 (-42.06%)
Value: 18315.27
Cost: 31612.13
```

The output is easily parseable using `awk`:

```sh
$ ticker --config=./.ticker.yaml print --summary | awk '/Value/{print $2}'
18315.27
```

## Notes

* **Real-time quotes** - Quotes are pulled from Yahoo finance which may provide delayed stock quotes depending on the exchange. The major US exchanges (NYSE, NASDAQ) have real-time quotes however other exchanges may not. Consult the [help article](https://help.yahoo.com/kb/SLN2310.html) on exchange delays to determine which exchanges you can expect delays for or use the `--show-tags` flag to include timeliness of data alongside quotes in `ticker`.
Expand Down
9 changes: 1 addition & 8 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,6 @@ var (
Args: cli.Validate(&config, &options, &err),
Run: print.Run(&dep, &ctx, &optionsPrint),
}
printSummaryCmd = &cobra.Command{
Use: "print-summary",
Short: "Prints holdings summary",
PreRun: initContext,
Args: cli.Validate(&config, &options, &err),
Run: print.RunSummary(&dep, &ctx, &optionsPrint),
}
)

// Execute starts the CLI or prints an error is there is one
Expand All @@ -68,7 +61,7 @@ func init() { //nolint: gochecknoinits
rootCmd.Flags().StringVar(&options.Proxy, "proxy", "", "proxy URL for requests (default is none)")
rootCmd.Flags().StringVar(&options.Sort, "sort", "", "sort quotes on the UI. Set \"alpha\" to sort by ticker name. Set \"value\" to sort by position value. Keep empty to sort according to change percent")
rootCmd.AddCommand(printCmd)
rootCmd.AddCommand(printSummaryCmd)
printCmd.Flags().BoolVar(&optionsPrint.Summary, "summary", false, "prints holdings summary (day change, total change, value, cost)")
printCmd.Flags().StringVar(&optionsPrint.Format, "format", "", "output format for printing holdings. Set \"csv\" to print as a CSV or \"json\" for JSON. Defaults to JSON.")
printCmd.Flags().StringVar(&configPath, "config", "", "config file (default is $HOME/.ticker.yaml)")
}
Expand Down
25 changes: 10 additions & 15 deletions internal/print/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
// Options to configure print behavior
type Options struct {
Format string

Check failure on line 19 in internal/print/print.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofmt`-ed with `-s` (gofmt)
Summary bool
}

type jsonRow struct {
Expand Down Expand Up @@ -89,29 +90,23 @@ func Run(dep *c.Dependencies, ctx *c.Context, options *Options) func(*cobra.Comm
return func(_ *cobra.Command, _ []string) {

assetGroupQuote := quote.GetAssetGroupQuote(*dep)(ctx.Groups[0])
assets, _ := asset.GetAssets(*ctx, assetGroupQuote)
assets, holdingSummary := asset.GetAssets(*ctx, assetGroupQuote)

if options.Format == "csv" {
fmt.Println(convertAssetsToCSV(assets))

return
}
if options.Summary {
fmt.Printf("Day change:\t%.2f (%.2f%%)\n", holdingSummary.DayChange.Amount, holdingSummary.DayChange.Percent)
fmt.Printf("Total change:\t%.2f (%.2f%%)\n", holdingSummary.TotalChange.Amount, holdingSummary.TotalChange.Percent)
fmt.Printf("Value:\t\t%.2f\n", holdingSummary.Value)
fmt.Printf("Cost:\t\t%.2f\n", holdingSummary.Cost)

return
}

fmt.Println(convertAssetsToJSON(assets))
}
}

// RunSummary prints holdings summary to the terminal
func RunSummary(dep *c.Dependencies, ctx *c.Context, options *Options) func(*cobra.Command, []string) {
return func(_ *cobra.Command, _ []string) {

assetGroupQuote := quote.GetAssetGroupQuote(*dep)(ctx.Groups[0])
_, holdingSummary := asset.GetAssets(*ctx, assetGroupQuote)

fmt.Printf("Day change:\t%.2f (%.2f%%)\n", holdingSummary.DayChange.Amount, holdingSummary.DayChange.Percent)
fmt.Printf("Total change:\t%.2f (%.2f%%)\n", holdingSummary.TotalChange.Amount, holdingSummary.TotalChange.Percent)
fmt.Printf("Value:\t%.2f\n", holdingSummary.Value)
fmt.Printf("Cost:\t%.2f\n", holdingSummary.Cost)

}
}

0 comments on commit 1548801

Please sign in to comment.