Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,5 @@ internal/adapters/market/finnhub_test.go
PLAN.md
.DS_Store
/dist
docs/demo
tick2
24 changes: 24 additions & 0 deletions db/query/query.sql
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,27 @@ SELECT *
FROM portfolio_snapshot_positions
WHERE snapshot_id = ?
ORDER BY symbol ASC;


-- name: CreateTarget :one
INSERT INTO portfolio_targets (
portfolio_id,
symbol,
type,
target_price,
quote_currency
) VALUES (?, ?, ?, ?, ?)
RETURNING id;

-- name: ListTargetsByPortfolio :many
SELECT
p.name,
t.symbol,
t.target_price,
t.type,
t.quote_currency
FROM portfolio_targets AS t
JOIN portfolios AS p ON t.portfolio_id = p.id
WHERE t.portfolio_id = ?
AND t.deleted_at IS NULL
ORDER BY t.symbol ASC;
32 changes: 29 additions & 3 deletions db/schema/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ CREATE TABLE IF NOT EXISTS instruments (
exchange TEXT,
quote_currency TEXT NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
deleted_at TIMESTAMP
);

CREATE TABLE IF NOT EXISTS portfolios (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL UNIQUE,
base_currency TEXT NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
deleted_at TIMESTAMP
);

CREATE TABLE IF NOT EXISTS positions (
Expand All @@ -26,6 +28,7 @@ CREATE TABLE IF NOT EXISTS positions (
currency TEXT NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
deleted_at TIMESTAMP,
FOREIGN KEY (instrument_id) REFERENCES instruments(id),
FOREIGN KEY (portfolio_id) REFERENCES portfolios(id),
UNIQUE (portfolio_id, instrument_id)
Expand Down Expand Up @@ -75,6 +78,29 @@ CREATE TABLE portfolio_snapshot_positions (
fx_rate REAL NOT NULL,
market_value_base REAL NOT NULL,
weight REAL NOT NULL,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (snapshot_id) REFERENCES portfolio_snapshots(id) ON DELETE CASCADE
);

CREATE TABLE portfolio_targets (
id INTEGER PRIMARY KEY AUTOINCREMENT,
portfolio_id INTEGER NOT NULL REFERENCES portfolios(id) ON DELETE CASCADE,
symbol TEXT NOT NULL,
type TEXT NOT NULL,
target_price REAL NOT NULL,
quote_currency TEXT NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
deleted_at TIMESTAMP,

CONSTRAINT portfolio_targets_type_check
CHECK (type IN ('take-profit', 'stop-loss'))
);

CREATE UNIQUE INDEX idx_portfolio_targets_unique_active
ON portfolio_targets (portfolio_id, symbol, type)
WHERE deleted_at IS NULL;

CREATE INDEX idx_portfolio_targets_portfolio_id
ON portfolio_targets (portfolio_id);

4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ go 1.25.1

require (
github.com/joho/godotenv v1.5.1
github.com/mattn/go-isatty v0.0.20
github.com/pressly/goose/v3 v3.27.0
github.com/spf13/cobra v1.10.2
go.uber.org/mock v0.6.0
golang.org/x/text v0.36.0
modernc.org/sqlite v1.48.1
)

Expand All @@ -15,7 +17,6 @@ require (
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mfridman/interpolate v0.0.2 // indirect
github.com/ncruces/go-strftime v1.0.0 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
Expand All @@ -24,7 +25,6 @@ require (
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/sync v0.20.0 // indirect
golang.org/x/sys v0.42.0 // indirect
golang.org/x/text v0.36.0
modernc.org/libc v1.70.0 // indirect
modernc.org/mathutil v1.7.1 // indirect
modernc.org/memory v1.11.0 // indirect
Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
github.com/chzyer/logex v1.2.1 h1:XHDu3E6q+gdHgsdTPH6ImJMIp436vR6MPtH8gP05QzM=
github.com/chzyer/logex v1.2.1/go.mod h1:JLbx6lG2kDbNRFnfkgvh4eRJRPX1QCoOIWomwysCBrQ=
github.com/chzyer/readline v1.5.1 h1:upd/6fQk4src78LMRzh5vItIt361/o4uq553V8B5sGI=
github.com/chzyer/readline v1.5.1/go.mod h1:Eh+b79XXUwfKfcPLepksvw2tcLE/Ct21YObkaSkeBlk=
github.com/chzyer/test v1.0.0 h1:p3BQDXSxOhOG0P9z6/hGnII4LGiEPOYBhs8asl/fC04=
github.com/chzyer/test v1.0.0/go.mod h1:2JlltgoNkt4TW/z9V/IzDdFaMTM2JPIi26O1pF38GC8=
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand Down Expand Up @@ -43,10 +45,8 @@ go.uber.org/mock v0.6.0/go.mod h1:KiVJ4BqZJaMj4svdfmHM0AUx4NJYO8ZNpPnZn1Z+BBU=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
golang.org/x/mod v0.33.0 h1:tHFzIWbBifEmbwtGz65eaWyGiGZatSrT9prnU8DbVL8=
golang.org/x/mod v0.33.0/go.mod h1:swjeQEj+6r7fODbD2cqrnje9PnziFuw4bmLbBZFrQ5w=
golang.org/x/sync v0.19.0 h1:vV+1eWNmZ5geRlYjzm2adRgW2/mcpevXNg50YZtPCE4=
golang.org/x/sync v0.19.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
golang.org/x/mod v0.34.0 h1:xIHgNUUnW6sYkcM5Jleh05DvLOtwc6RitGHbDk4akRI=
golang.org/x/mod v0.34.0/go.mod h1:ykgH52iCZe79kzLLMhyCUzhMci+nQj+0XkbXpNYtVjY=
golang.org/x/sync v0.20.0 h1:e0PTpb7pjO8GAtTs2dQ6jYa5BWYlMuX047Dco/pItO4=
golang.org/x/sync v0.20.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0=
golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand All @@ -55,8 +55,8 @@ golang.org/x/sys v0.42.0 h1:omrd2nAlyT5ESRdCLYdm3+fMfNFE/+Rf4bDIQImRJeo=
golang.org/x/sys v0.42.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
golang.org/x/text v0.36.0 h1:JfKh3XmcRPqZPKevfXVpI1wXPTqbkE5f7JA92a55Yxg=
golang.org/x/text v0.36.0/go.mod h1:NIdBknypM8iqVmPiuco0Dh6P5Jcdk8lJL0CUebqK164=
golang.org/x/tools v0.42.0 h1:uNgphsn75Tdz5Ji2q36v/nsFSfR/9BRFvqhGBaJGd5k=
golang.org/x/tools v0.42.0/go.mod h1:Ma6lCIwGZvHK6XtgbswSoWroEkhugApmsXyrUmBhfr0=
golang.org/x/tools v0.43.0 h1:12BdW9CeB3Z+J/I/wj34VMl8X+fEXBxVR90JeMX5E7s=
golang.org/x/tools v0.43.0/go.mod h1:uHkMso649BX2cZK6+RpuIPXS3ho2hZo4FVwfoy1vIk0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (p *CoinGeckoProvider) GetQuote(ctx context.Context, ticker string) (domain
change := price - previous

return domain.Quote{
Ticker: strings.ToUpper(strings.TrimSpace(ticker)),
Symbol: strings.ToUpper(strings.TrimSpace(ticker)),
Price: price,
PriceCurrency: "USD",
PreviousClose: previous,
Expand Down
2 changes: 1 addition & 1 deletion internal/adapters/market/crypto_price_provider_static.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (p *StaticCryptoPriceProvider) GetQuote(_ context.Context, ticker string) (
}

return domain.Quote{
Ticker: ticker,
Symbol: ticker,
Price: v.Price,
PriceCurrency: v.Currency,
PreviousClose: v.PreviousClose,
Expand Down
4 changes: 2 additions & 2 deletions internal/adapters/market/equity_price_provider_cached.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (p *CachedPriceProvider) GetQuote(ctx context.Context, ticker string) (doma

func toDomainQuote(c repository.CachedPriceQuote) domain.Quote {
return domain.Quote{
Ticker: c.PriceQuote.Ticker,
Symbol: c.PriceQuote.Ticker,
Price: c.PriceQuote.Price,
PriceCurrency: c.PriceQuote.PriceCurrency,
PreviousClose: c.PriceQuote.PreviousClose,
Expand All @@ -77,7 +77,7 @@ func toDomainQuote(c repository.CachedPriceQuote) domain.Quote {

func toRepositoryQuote(q domain.Quote) repository.PriceQuote {
return repository.PriceQuote{
Ticker: q.Ticker,
Ticker: q.Symbol,
Price: q.Price,
PriceCurrency: q.PriceCurrency,
PreviousClose: q.PreviousClose,
Expand Down
2 changes: 1 addition & 1 deletion internal/adapters/market/equity_price_provider_finnhub.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (p *FinnhubPriceProvider) GetQuote(ctx context.Context, ticker string) (dom
}

return domain.Quote{
Ticker: ticker,
Symbol: ticker,
Price: data.C,
PriceCurrency: "USD",
PreviousClose: data.PC,
Expand Down
2 changes: 1 addition & 1 deletion internal/adapters/market/equity_price_provider_static.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (p *StaticPriceProvider) GetQuote(_ context.Context, ticker string) (domain
}

return domain.Quote{
Ticker: ticker,
Symbol: ticker,
Price: v.Price,
PriceCurrency: v.Currency,
PreviousClose: v.PreviousClose,
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"errors"
"testing"

"github.com/squeakycheese75/tick/internal/analysis/mocks"
"github.com/squeakycheese75/tick/internal/domain"
"github.com/squeakycheese75/tick/internal/domain/analysis/mocks"
"go.uber.org/mock/gomock"
)

Expand Down
22 changes: 15 additions & 7 deletions internal/app/runtime.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package app

import (
"github.com/squeakycheese75/tick/internal/analysis"
"github.com/squeakycheese75/tick/internal/db"
"github.com/squeakycheese75/tick/internal/domain/analysis"
"github.com/squeakycheese75/tick/internal/instruments"
"github.com/squeakycheese75/tick/internal/report"
"github.com/squeakycheese75/tick/internal/repository"
Expand All @@ -19,6 +19,8 @@ type Runtime struct {
ImportPortfolio *usecase.ImportPortfolioUseCase
GetTickerNews *usecase.GetTickerNewsUseCase
GetMorningBrief *usecase.GetMorningBriefUsecase
SetTarget *usecase.SetTargetUseCase
ListTargets *usecase.ListTargetsUseCase
}

func BuildRuntime(dbPath string) (*Runtime, error) {
Expand All @@ -40,12 +42,13 @@ func BuildRuntime(dbPath string) (*Runtime, error) {
positionRepo := repository.NewPositionRepository(database)
instrumentRepo := repository.NewInstrumentRepository(database)
snapshotRepo := repository.NewSnapshotRepository(database)
targetRespository := repository.NewTargetRepository(database)

// Caching
priceCacher := repository.NewPriceCacheRepository(database)
fxCacher := repository.NewFXCacheRepository(database)

// Providers
// Adapters/Providers
equityPriceProvider, err := BuildEquityPriceProvider(cfg, priceCacher)
if err != nil {
return nil, err
Expand Down Expand Up @@ -76,18 +79,20 @@ func BuildRuntime(dbPath string) (*Runtime, error) {

portfolioAnalyser := analysis.NewPortfolioAnalyzer(pricingSvc)
riskAnalyser := analysis.NewRiskAnalyzer()
analysisSvc := service.NewPortfolioAnalysisSvc(portfolioRepo, positionRepo, portfolioAnalyser)
riskSvc := service.NewPortfolioRiskSvc(portfolioRepo, positionRepo, riskAnalyser)

portfolioSvc := service.NewPortfolioService(portfolioRepo, positionRepo, portfolioAnalyser, riskAnalyser)
portfolioInsights := service.NewPortfolioInsights()
portfolioInsights := service.NewInsightsSvc()
newsSvc := service.NewNewsService(newsProvider)
snapshotSvc := service.NewSnapshotService(snapshotRepo)
targetSvc := service.NewTargetSvc(portfolioRepo, targetRespository)

instrumentResolver, err := instruments.NewStaticResolver()
if err != nil {
return nil, err
}

reportingBuilder := report.NewReportBuilder(portfolioSvc, pricingSvc, newsSvc, portfolioInsights, snapshotSvc)
reportingBuilder := report.NewReportBuilder(analysisSvc, riskSvc, pricingSvc, newsSvc, portfolioInsights, snapshotSvc, targetSvc)

var summarizer usecase.DailyReportSummarizer = service.NoopSummarizer{}

Expand All @@ -97,16 +102,19 @@ func BuildRuntime(dbPath string) (*Runtime, error) {

return &Runtime{
GetPortfolioSummary: usecase.NewGetPortfolioSummaryUseCase(
portfolioSvc,
analysisSvc,
),
CreatePortfolio: usecase.NewCreatePortfolioUseCase(portfolioRepo),
AddPosition: usecase.NewAddPositionToPortfolioUseCase(positionRepo, portfolioRepo, instrumentRepo, instrumentResolver),
GetPortfolioRisk: usecase.NewGetPortfolioRiskUseCase(
portfolioSvc,
analysisSvc,
riskSvc,
),
GetDailyReport: usecase.NewGetDailyReportUseCase(reportingBuilder, summarizer, snapshotRepo),
ImportPortfolio: usecase.NewImportPortfolioUseCase(positionRepo, portfolioRepo, instrumentRepo),
GetTickerNews: usecase.NewGetTickerNewsUseCase(newsSvc),
GetMorningBrief: usecase.NewGetMorningBriefUsecase(reportingBuilder),
SetTarget: usecase.NewSetTargetUseCase(portfolioRepo, targetRespository),
ListTargets: usecase.NewListTargetsUseCase(portfolioRepo, targetRespository),
}, nil
}
1 change: 1 addition & 0 deletions internal/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func NewRootCmd(runtimeBuilder RuntimeBuilder) *cobra.Command {
rootCmd.AddCommand(newNewsCmd(runtimeBuilder))
rootCmd.AddCommand(newConfigCmd())
rootCmd.AddCommand(newShellCmd(rootCmd))
rootCmd.AddCommand(newTargetCmd(runtimeBuilder))

return rootCmd
}
Loading
Loading