2020-10-18 03:33:13 +00:00
|
|
|
package accounting
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math"
|
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
2022-02-02 11:37:18 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/fixedpoint"
|
2022-02-15 05:55:19 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
2020-10-18 03:33:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Stock types.Trade
|
|
|
|
|
|
|
|
func (stock *Stock) String() string {
|
2022-02-08 04:41:24 +00:00
|
|
|
return stock.Price.String() + " (" + stock.Quantity.String() + ")"
|
2020-10-18 03:33:13 +00:00
|
|
|
}
|
|
|
|
|
2022-02-02 11:37:18 +00:00
|
|
|
func (stock *Stock) Consume(quantity fixedpoint.Value) fixedpoint.Value {
|
|
|
|
q := fixedpoint.Min(stock.Quantity, quantity)
|
2022-02-08 04:41:24 +00:00
|
|
|
stock.Quantity = stock.Quantity.Sub(q)
|
2020-10-18 03:33:13 +00:00
|
|
|
return q
|
|
|
|
}
|
|
|
|
|
|
|
|
type StockSlice []Stock
|
|
|
|
|
2022-02-02 11:37:18 +00:00
|
|
|
func (slice StockSlice) QuantityBelowPrice(price fixedpoint.Value) (quantity fixedpoint.Value) {
|
2020-10-18 03:33:13 +00:00
|
|
|
for _, stock := range slice {
|
2022-02-02 11:37:18 +00:00
|
|
|
if stock.Price.Compare(price) < 0 {
|
|
|
|
quantity = quantity.Add(stock.Quantity)
|
2020-10-18 03:33:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-08 04:41:24 +00:00
|
|
|
return quantity
|
2020-10-18 03:33:13 +00:00
|
|
|
}
|
|
|
|
|
2022-02-02 11:37:18 +00:00
|
|
|
func (slice StockSlice) Quantity() (total fixedpoint.Value) {
|
2020-10-18 03:33:13 +00:00
|
|
|
for _, stock := range slice {
|
2022-02-02 11:37:18 +00:00
|
|
|
total = total.Add(stock.Quantity)
|
2020-10-18 03:33:13 +00:00
|
|
|
}
|
|
|
|
|
2022-02-08 04:41:24 +00:00
|
|
|
return total
|
2020-10-18 03:33:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type StockDistribution struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
|
|
|
|
Symbol string
|
|
|
|
TradingFeeCurrency string
|
|
|
|
Stocks StockSlice
|
|
|
|
PendingSells StockSlice
|
|
|
|
}
|
|
|
|
|
|
|
|
type DistributionStats struct {
|
2022-02-15 05:55:19 +00:00
|
|
|
PriceLevels []string `json:"priceLevels"`
|
|
|
|
TotalQuantity fixedpoint.Value `json:"totalQuantity"`
|
|
|
|
Quantities map[string]fixedpoint.Value `json:"quantities"`
|
|
|
|
Stocks map[string]StockSlice `json:"stocks"`
|
2020-10-18 03:33:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *StockDistribution) DistributionStats(level int) *DistributionStats {
|
|
|
|
var d = DistributionStats{
|
2022-02-15 05:55:19 +00:00
|
|
|
Quantities: map[string]fixedpoint.Value{},
|
2020-10-18 03:33:13 +00:00
|
|
|
Stocks: map[string]StockSlice{},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, stock := range m.Stocks {
|
2022-02-02 11:37:18 +00:00
|
|
|
n := math.Ceil(math.Log10(stock.Price.Float64()))
|
2020-10-18 03:33:13 +00:00
|
|
|
digits := int(n - math.Max(float64(level), 1.0))
|
2022-02-08 04:41:24 +00:00
|
|
|
key := stock.Price.Round(-digits, fixedpoint.Down).FormatString(2)
|
2020-10-18 03:33:13 +00:00
|
|
|
|
2022-02-02 11:37:18 +00:00
|
|
|
d.TotalQuantity = d.TotalQuantity.Add(stock.Quantity)
|
2020-10-18 03:33:13 +00:00
|
|
|
d.Stocks[key] = append(d.Stocks[key], stock)
|
2022-02-02 11:37:18 +00:00
|
|
|
d.Quantities[key] = d.Quantities[key].Add(stock.Quantity)
|
2020-10-18 03:33:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var priceLevels []float64
|
|
|
|
for priceString := range d.Stocks {
|
|
|
|
price, _ := strconv.ParseFloat(priceString, 32)
|
|
|
|
priceLevels = append(priceLevels, price)
|
|
|
|
}
|
|
|
|
sort.Float64s(priceLevels)
|
|
|
|
|
|
|
|
for _, price := range priceLevels {
|
|
|
|
d.PriceLevels = append(d.PriceLevels, strconv.FormatFloat(price, 'f', 2, 64))
|
|
|
|
}
|
|
|
|
|
|
|
|
return &d
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *StockDistribution) stock(stock Stock) error {
|
|
|
|
m.mu.Lock()
|
|
|
|
m.Stocks = append(m.Stocks, stock)
|
|
|
|
m.mu.Unlock()
|
|
|
|
return m.flushPendingSells()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *StockDistribution) squash() {
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
|
|
|
|
var squashed StockSlice
|
|
|
|
for _, stock := range m.Stocks {
|
2022-02-02 11:37:18 +00:00
|
|
|
if !stock.Quantity.IsZero() {
|
2020-10-18 03:33:13 +00:00
|
|
|
squashed = append(squashed, stock)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m.Stocks = squashed
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *StockDistribution) flushPendingSells() error {
|
|
|
|
if len(m.Stocks) == 0 || len(m.PendingSells) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
pendingSells := m.PendingSells
|
|
|
|
m.PendingSells = nil
|
|
|
|
|
|
|
|
for _, sell := range pendingSells {
|
|
|
|
if err := m.consume(sell); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *StockDistribution) consume(sell Stock) error {
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
|
|
|
|
if len(m.Stocks) == 0 {
|
|
|
|
m.PendingSells = append(m.PendingSells, sell)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
idx := len(m.Stocks) - 1
|
|
|
|
for ; idx >= 0; idx-- {
|
|
|
|
stock := m.Stocks[idx]
|
|
|
|
|
|
|
|
// find any stock price is lower than the sell trade
|
2022-02-02 11:37:18 +00:00
|
|
|
if stock.Price.Compare(sell.Price) >= 0 {
|
2020-10-18 03:33:13 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-02-02 11:37:18 +00:00
|
|
|
if stock.Quantity.IsZero() {
|
2020-10-18 03:33:13 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
delta := stock.Consume(sell.Quantity)
|
|
|
|
sell.Consume(delta)
|
|
|
|
m.Stocks[idx] = stock
|
|
|
|
|
2022-02-02 11:37:18 +00:00
|
|
|
if sell.Quantity.IsZero() {
|
2020-10-18 03:33:13 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
idx = len(m.Stocks) - 1
|
|
|
|
for ; idx >= 0; idx-- {
|
|
|
|
stock := m.Stocks[idx]
|
|
|
|
|
2022-02-02 11:37:18 +00:00
|
|
|
if stock.Quantity.IsZero() {
|
2020-10-18 03:33:13 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
delta := stock.Consume(sell.Quantity)
|
|
|
|
sell.Consume(delta)
|
|
|
|
m.Stocks[idx] = stock
|
|
|
|
|
2022-02-02 11:37:18 +00:00
|
|
|
if sell.Quantity.IsZero() {
|
2020-10-18 03:33:13 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-02 11:37:18 +00:00
|
|
|
if sell.Quantity.Sign() > 0 {
|
2020-10-18 03:33:13 +00:00
|
|
|
m.PendingSells = append(m.PendingSells, sell)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *StockDistribution) AddTrades(trades []types.Trade) (checkpoints []int, err error) {
|
|
|
|
feeSymbol := strings.HasPrefix(m.Symbol, m.TradingFeeCurrency)
|
|
|
|
for idx, trade := range trades {
|
|
|
|
// for other market trades
|
|
|
|
// convert trading fee trades to sell trade
|
|
|
|
if trade.Symbol != m.Symbol {
|
|
|
|
if feeSymbol && trade.FeeCurrency == m.TradingFeeCurrency {
|
|
|
|
trade.Symbol = m.Symbol
|
|
|
|
trade.IsBuyer = false
|
|
|
|
trade.Quantity = trade.Fee
|
2022-02-02 11:37:18 +00:00
|
|
|
trade.Fee = fixedpoint.Zero
|
2020-10-18 03:33:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if trade.Symbol != m.Symbol {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if trade.IsBuyer {
|
|
|
|
if idx > 0 && len(m.Stocks) == 0 {
|
|
|
|
checkpoints = append(checkpoints, idx)
|
|
|
|
}
|
|
|
|
|
|
|
|
stock := toStock(trade)
|
|
|
|
if err := m.stock(stock); err != nil {
|
|
|
|
return checkpoints, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
stock := toStock(trade)
|
|
|
|
if err := m.consume(stock); err != nil {
|
|
|
|
return checkpoints, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = m.flushPendingSells()
|
|
|
|
|
|
|
|
m.squash()
|
|
|
|
|
|
|
|
return checkpoints, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func toStock(trade types.Trade) Stock {
|
|
|
|
if strings.HasPrefix(trade.Symbol, trade.FeeCurrency) {
|
|
|
|
if trade.IsBuyer {
|
2022-02-02 11:37:18 +00:00
|
|
|
trade.Quantity = trade.Quantity.Sub(trade.Fee)
|
2020-10-18 03:33:13 +00:00
|
|
|
} else {
|
2022-02-02 11:37:18 +00:00
|
|
|
trade.Quantity = trade.Quantity.Add(trade.Fee)
|
2020-10-18 03:33:13 +00:00
|
|
|
}
|
2022-02-02 11:37:18 +00:00
|
|
|
trade.Fee = fixedpoint.Zero
|
2020-10-18 03:33:13 +00:00
|
|
|
}
|
|
|
|
return Stock(trade)
|
|
|
|
}
|