|
| 1 | +// Copyright 2026 Peter Edge |
| 2 | +// |
| 3 | +// All rights reserved. |
| 4 | + |
| 5 | +// Package holdingvalue implements the "holding value" command. |
| 6 | +package holdingvalue |
| 7 | + |
| 8 | +import ( |
| 9 | + "context" |
| 10 | + "fmt" |
| 11 | + "math" |
| 12 | + "os" |
| 13 | + |
| 14 | + "buf.build/go/app/appcmd" |
| 15 | + "buf.build/go/app/appext" |
| 16 | + "github.com/bufdev/ibctl/cmd/ibctl/internal/ibctlcmd" |
| 17 | + "github.com/bufdev/ibctl/internal/ibctl/ibctlconfig" |
| 18 | + "github.com/bufdev/ibctl/internal/ibctl/ibctlfxrates" |
| 19 | + "github.com/bufdev/ibctl/internal/ibctl/ibctlholdings" |
| 20 | + "github.com/bufdev/ibctl/internal/ibctl/ibctlmerge" |
| 21 | + "github.com/bufdev/ibctl/internal/ibctl/ibctlpath" |
| 22 | + "github.com/bufdev/ibctl/internal/pkg/cliio" |
| 23 | + "github.com/bufdev/ibctl/internal/pkg/mathpb" |
| 24 | + "github.com/spf13/pflag" |
| 25 | +) |
| 26 | + |
| 27 | +// downloadFlagName is the flag name for downloading fresh data before displaying. |
| 28 | +const downloadFlagName = "download" |
| 29 | + |
| 30 | +// NewCommand returns a new holding value command. |
| 31 | +func NewCommand(name string, builder appext.SubCommandBuilder) *appcmd.Command { |
| 32 | + flags := newFlags() |
| 33 | + return &appcmd.Command{ |
| 34 | + Use: name, |
| 35 | + Short: "Display portfolio value with estimated tax impact", |
| 36 | + Args: appcmd.NoArgs, |
| 37 | + Run: builder.NewRunFunc( |
| 38 | + func(ctx context.Context, container appext.Container) error { |
| 39 | + return run(ctx, container, flags) |
| 40 | + }, |
| 41 | + ), |
| 42 | + BindFlags: flags.Bind, |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +type flags struct { |
| 47 | + // Dir is the base directory containing ibctl.yaml and data subdirectories. |
| 48 | + Dir string |
| 49 | + // Download fetches fresh data before displaying. |
| 50 | + Download bool |
| 51 | +} |
| 52 | + |
| 53 | +func newFlags() *flags { |
| 54 | + return &flags{} |
| 55 | +} |
| 56 | + |
| 57 | +// Bind registers the flag definitions with the given flag set. |
| 58 | +func (f *flags) Bind(flagSet *pflag.FlagSet) { |
| 59 | + flagSet.StringVar(&f.Dir, ibctlcmd.DirFlagName, ".", "The ibctl directory containing ibctl.yaml") |
| 60 | + flagSet.BoolVar(&f.Download, downloadFlagName, false, "Download fresh data before displaying") |
| 61 | +} |
| 62 | + |
| 63 | +func run(ctx context.Context, container appext.Container, flags *flags) error { |
| 64 | + // Read and validate the configuration file from the base directory. |
| 65 | + config, err := ibctlconfig.ReadConfig(flags.Dir) |
| 66 | + if err != nil { |
| 67 | + return err |
| 68 | + } |
| 69 | + // Download fresh data if --download is set. |
| 70 | + if flags.Download { |
| 71 | + downloader, err := ibctlcmd.NewDownloader(container, flags.Dir) |
| 72 | + if err != nil { |
| 73 | + return err |
| 74 | + } |
| 75 | + if err := downloader.Download(ctx); err != nil { |
| 76 | + return err |
| 77 | + } |
| 78 | + } |
| 79 | + // Merge trade data from all sources. |
| 80 | + mergedData, err := ibctlmerge.Merge( |
| 81 | + ibctlpath.DataAccountsDirPath(config.DirPath), |
| 82 | + ibctlpath.CacheAccountsDirPath(config.DirPath), |
| 83 | + ibctlpath.ActivityStatementsDirPath(config.DirPath), |
| 84 | + ibctlpath.SeedDirPath(config.DirPath), |
| 85 | + config.AccountAliases, |
| 86 | + ) |
| 87 | + if err != nil { |
| 88 | + return err |
| 89 | + } |
| 90 | + // Load FX rates for USD conversion. |
| 91 | + fxStore := ibctlfxrates.NewStore(ibctlpath.CacheFXDirPath(config.DirPath)) |
| 92 | + // Compute holdings via FIFO from all trade data. |
| 93 | + result, err := ibctlholdings.GetHoldingsOverview(mergedData.Trades, mergedData.Positions, mergedData.CashPositions, config, fxStore) |
| 94 | + if err != nil { |
| 95 | + return err |
| 96 | + } |
| 97 | + // Sum up portfolio value, STCG, and LTCG from all holdings. |
| 98 | + var totalValueMicros, totalSTCGMicros, totalLTCGMicros int64 |
| 99 | + for _, h := range result.Holdings { |
| 100 | + totalValueMicros += mathpb.ParseMicros(h.MarketValueUSD) |
| 101 | + totalSTCGMicros += mathpb.ParseMicros(h.STCGUSD) |
| 102 | + totalLTCGMicros += mathpb.ParseMicros(h.LTCGUSD) |
| 103 | + } |
| 104 | + // Compute tax amounts. Gains are taxed; losses reduce taxes (can be negative). |
| 105 | + stcgTaxMicros := int64(math.Round(float64(totalSTCGMicros) * config.TaxRateSTCG)) |
| 106 | + ltcgTaxMicros := int64(math.Round(float64(totalLTCGMicros) * config.TaxRateLTCG)) |
| 107 | + totalTaxMicros := stcgTaxMicros + ltcgTaxMicros |
| 108 | + // After-tax value = portfolio value - total taxes. |
| 109 | + afterTaxMicros := totalValueMicros - totalTaxMicros |
| 110 | + // Print the summary. |
| 111 | + writer := os.Stdout |
| 112 | + fmt.Fprintf(writer, "Portfolio Value: %s\n", cliio.FormatUSDMicros(totalValueMicros)) |
| 113 | + fmt.Fprintf(writer, "\n") |
| 114 | + fmt.Fprintf(writer, "STCG: %s\n", cliio.FormatUSDMicros(totalSTCGMicros)) |
| 115 | + fmt.Fprintf(writer, "STCG Tax (%.1f%%): %s\n", config.TaxRateSTCG*100, cliio.FormatUSDMicros(stcgTaxMicros)) |
| 116 | + fmt.Fprintf(writer, "LTCG: %s\n", cliio.FormatUSDMicros(totalLTCGMicros)) |
| 117 | + fmt.Fprintf(writer, "LTCG Tax (%.1f%%): %s\n", config.TaxRateLTCG*100, cliio.FormatUSDMicros(ltcgTaxMicros)) |
| 118 | + fmt.Fprintf(writer, "Total Tax: %s\n", cliio.FormatUSDMicros(totalTaxMicros)) |
| 119 | + fmt.Fprintf(writer, "\n") |
| 120 | + fmt.Fprintf(writer, "After-Tax Value: %s\n", cliio.FormatUSDMicros(afterTaxMicros)) |
| 121 | + return nil |
| 122 | +} |
0 commit comments