83 lines
1.6 KiB
Go
83 lines
1.6 KiB
Go
package model
|
|
|
|
import (
|
|
"errors"
|
|
"time"
|
|
)
|
|
|
|
type Position struct {
|
|
Company Company
|
|
Currency Currency
|
|
Weight float64
|
|
CostBasis float64
|
|
Shares int
|
|
}
|
|
|
|
type TradeProduct int
|
|
|
|
const (
|
|
StockTrade TradeProduct = iota // 0
|
|
OptionCallTrade // 1
|
|
OptionPutTrade // 2
|
|
CurrencyTrade // 3
|
|
BondTrade
|
|
)
|
|
|
|
type TradeType bool
|
|
|
|
const (
|
|
Buy TradeType = true
|
|
Sell TradeType = false
|
|
)
|
|
|
|
type Trade struct {
|
|
Ticker Company
|
|
Shares int
|
|
Product TradeProduct
|
|
Type TradeType
|
|
Price float64
|
|
Currency string
|
|
Date time.Time
|
|
}
|
|
|
|
type AddTradeRequest struct {
|
|
TickerId int `json:"ticker_id"`
|
|
Shares int `json:"shares"`
|
|
Product int `json:"product"`
|
|
Type bool `json:"type"`
|
|
Price float64 `json:"price"`
|
|
Currency string `json:"currency"`
|
|
Date time.Time `json:"date"`
|
|
}
|
|
|
|
func (r *AddTradeRequest) Validate() error {
|
|
if r.TickerId <= 0 {
|
|
return errors.New("ticker id must be a positive integer")
|
|
}
|
|
if r.Shares <= 0 {
|
|
return errors.New("shares must be a positive integer")
|
|
}
|
|
if r.Product < 0 || r.Product > 3 {
|
|
return errors.New("product must be between 0 and 3")
|
|
}
|
|
if r.Price <= 0 {
|
|
return errors.New("price must be a positive number")
|
|
}
|
|
if r.Currency == "" {
|
|
return errors.New("currency is required")
|
|
}
|
|
if r.Date.IsZero() {
|
|
return errors.New("date is required")
|
|
}
|
|
if r.Date.After(time.Now()) {
|
|
return errors.New("date cannot be in the future")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// for now trades and none stock position will not be supported.
|
|
type Portifolio struct {
|
|
Positions []Position
|
|
Trades []Trade
|
|
}
|