barebones pnl calculation
This commit is contained in:
35
main.go
35
main.go
@@ -52,15 +52,42 @@ func run() error {
|
|||||||
return ErrSellWithoutBuy
|
return ErrSellWithoutBuy
|
||||||
}
|
}
|
||||||
|
|
||||||
first := lst.Front()
|
unmatchedQty := new(big.Float).Copy(record.Quantity())
|
||||||
if first == nil {
|
zero := new(big.Float)
|
||||||
return ErrSellWithoutBuy
|
|
||||||
|
for unmatchedQty.Cmp(zero) > 0 {
|
||||||
|
front := lst.Front()
|
||||||
|
if front == nil {
|
||||||
|
return ErrSellWithoutBuy
|
||||||
|
}
|
||||||
|
|
||||||
|
next, ok := front.Value.(Record)
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("unexpected record type: %T", front)
|
||||||
|
}
|
||||||
|
|
||||||
|
var matchedQty *big.Float
|
||||||
|
if next.Quantity().Cmp(unmatchedQty) > 0 {
|
||||||
|
matchedQty = unmatchedQty
|
||||||
|
next.Quantity().Sub(next.Quantity(), unmatchedQty)
|
||||||
|
} else {
|
||||||
|
matchedQty = next.Quantity()
|
||||||
|
lst.Remove(front)
|
||||||
|
}
|
||||||
|
|
||||||
|
unmatchedQty.Sub(unmatchedQty, matchedQty)
|
||||||
|
|
||||||
|
sellValue := new(big.Float).Mul(matchedQty, record.Price())
|
||||||
|
buyValue := new(big.Float).Mul(matchedQty, next.Price())
|
||||||
|
realisedPnL := new(big.Float).Sub(sellValue, buyValue)
|
||||||
|
slog.Info("Realised PnL",
|
||||||
|
slog.Any("Symbol", record.Symbol()),
|
||||||
|
slog.Any("PnL", realisedPnL))
|
||||||
}
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("unknown direction: %s", record.Direction())
|
return fmt.Errorf("unknown direction: %s", record.Direction())
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
slog.Info("Finish processing statement", slog.Any("assets_count", len(assets)))
|
slog.Info("Finish processing statement", slog.Any("assets_count", len(assets)))
|
||||||
|
|||||||
Reference in New Issue
Block a user