Skip to content

Commit

Permalink
Add position market value estimator
Browse files Browse the repository at this point in the history
  • Loading branch information
ammario committed Feb 17, 2024
1 parent 444c5b6 commit 15ea7b8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
20 changes: 12 additions & 8 deletions market.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,20 +131,24 @@ func (m *Market) NoMidPrice() Cents {
return (m.NoBid + m.NoAsk) / 2
}

// EstimateReturn shows the estimated return for an open position.
func (m *Market) EstimateReturn(p *MarketPosition) Cents {
func (m *Market) MarketValue(p *MarketPosition) Cents {
if p == nil {
return 0
}

var posMarketValue Cents

if p.Position < 0 {
posMarketValue = Cents(p.Position) * m.NoMidPrice()
posMarketValue = -posMarketValue
} else {
posMarketValue = Cents(p.Position) * m.YesMidPrice()
return Cents(p.AbsPosition()) * m.NoMidPrice()
}
return Cents(p.AbsPosition()) * m.YesMidPrice()
}

// EstimateReturn shows the estimated return for an open position.
func (m *Market) EstimateReturn(p *MarketPosition) Cents {
if p == nil {
return 0
}

posMarketValue := m.MarketValue(p)
costBasis := p.MarketExposure
return p.RealizedPnl - p.FeesPaid + (posMarketValue - costBasis)
}
Expand Down
17 changes: 15 additions & 2 deletions portfolio.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package kalshi
import (
"context"
"fmt"
"math"
"strings"
"time"

Expand Down Expand Up @@ -378,6 +379,13 @@ func (p *MarketPosition) AvgPrice() Cents {
return p.MarketExposure / Cents(p.Position)
}

func (p *MarketPosition) AbsPosition() int {
if p.Position < 0 {
return -p.Position
}
return p.Position
}

func (p *MarketPosition) String() string {
if p == nil {
return "N/A"
Expand All @@ -389,9 +397,14 @@ func (p *MarketPosition) String() string {
avgCost = -avgCost
}
}

posSign := "+"
if p.Position < 0 {
posSign = "-"
}
return fmt.Sprintf(
"n: %d (+%d)\texp:\t%v\tcost: %v\treal: %v\tfee: %v",
p.Position, p.RestingOrdersCount, p.MarketExposure, avgCost, p.RealizedPnl,
"n: %s%d (𝚫 %d)\texp:\t%v\tcost: %v\treal: %v\tfee: %v",
posSign, int(math.Abs(float64(p.Position))), p.RestingOrdersCount, p.MarketExposure, avgCost, p.RealizedPnl,
p.FeesPaid,
)
}
Expand Down

0 comments on commit 15ea7b8

Please sign in to comment.