Skip to content

Commit

Permalink
mtgban/arbit: Add a Profitability index value
Browse files Browse the repository at this point in the history
Replace Price Ratio in the csv column since it's unused.
  • Loading branch information
kodawah committed May 21, 2024
1 parent c5e12c7 commit bbb8d95
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
29 changes: 29 additions & 0 deletions mtgban/arbit.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mtgban

import (
"math"
"strconv"
"strings"

Expand Down Expand Up @@ -72,6 +73,10 @@ type ArbitOpts struct {
// It returns a custom factor to be applied on the buylist price,
// and whether the entry shoul be skipped
CustomCardFilter func(co *mtgmatcher.CardObject) (float64, bool)

// Constant used to offset prices (the higher the value, the less impactful
// lower prices will be)
ProfitabilityConstant float64
}

type ArbitEntry struct {
Expand All @@ -98,13 +103,18 @@ type ArbitEntry struct {

// Amount of cards that can be applied
Quantity int

// The higher the number the better the arbit is. Using this formula
// Profitability Index (PI) = (Difference / (Sell Price + 10)) * log(1 + Spread) * sqrt(Units)
Profitability float64
}

func Arbit(opts *ArbitOpts, vendor Vendor, seller Seller) (result []ArbitEntry, err error) {
minDiff := 0.0
minSpread := 0.0
useTrades := false
rate := 1.0
profitabilityConstant := 10.0

minPrice := 0.0
minBuyPrice := 0.0
Expand Down Expand Up @@ -134,6 +144,9 @@ func Arbit(opts *ArbitOpts, vendor Vendor, seller Seller) (result []ArbitEntry,
if opts.Rate != 0 {
rate = opts.Rate
}
if opts.ProfitabilityConstant > 0 {
profitabilityConstant = opts.ProfitabilityConstant
}
useTrades = opts.UseTrades

minPrice = opts.MinPrice
Expand Down Expand Up @@ -306,6 +319,11 @@ func Arbit(opts *ArbitOpts, vendor Vendor, seller Seller) (result []ArbitEntry,
}
}

profitability := (difference / (price + profitabilityConstant)) * math.Log(1+spread)
if qty > 1 {
profitability *= math.Sqrt(float64(qty))
}

res := ArbitEntry{
CardId: cardId,
BuylistEntry: blEntry,
Expand All @@ -314,6 +332,7 @@ func Arbit(opts *ArbitOpts, vendor Vendor, seller Seller) (result []ArbitEntry,
AbsoluteDifference: difference * float64(qty),
Spread: spread,
Quantity: qty,
Profitability: profitability,
}
result = append(result, res)
}
Expand Down Expand Up @@ -417,6 +436,7 @@ func Mismatch(opts *ArbitOpts, reference Seller, probe Seller) (result []ArbitEn
maxSpread := 0.0
minPrice := 0.0
minQty := 0
profitabilityConstant := 10.0
filterFoil := false
filterOnlyFoil := false
filterRLOnly := false
Expand All @@ -434,6 +454,9 @@ func Mismatch(opts *ArbitOpts, reference Seller, probe Seller) (result []ArbitEn
if opts.MinSpread != 0 {
minSpread = opts.MinSpread
}
if opts.ProfitabilityConstant > 0 {
profitabilityConstant = opts.ProfitabilityConstant
}

minPrice = opts.MinPrice
maxSpread = opts.MaxSpread
Expand Down Expand Up @@ -559,13 +582,19 @@ func Mismatch(opts *ArbitOpts, reference Seller, probe Seller) (result []ArbitEn
}
}

profitability := (difference / (price + profitabilityConstant)) * math.Log(1+spread)
if qty > 1 {
profitability *= math.Sqrt(float64(qty))
}

res := ArbitEntry{
CardId: cardId,
InventoryEntry: invEntry,
ReferenceEntry: refEntry,
Difference: difference,
Spread: spread,
Quantity: qty,
Profitability: profitability,
}
result = append(result, res)
}
Expand Down
4 changes: 2 additions & 2 deletions mtgban/csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var (
// The canonical header that will be present in all buylist files
BuylistHeader = append(CardHeader, "Conditions", "Buy Price", "Trade Price", "Quantity", "Price Ratio", "URL", "Vendor")

ArbitHeader = append(CardHeader, "Conditions", "Available", "Sell Price", "Buy Price", "Trade Price", "Difference", "Spread", "Abs Difference", "Price Ratio")
ArbitHeader = append(CardHeader, "Conditions", "Available", "Sell Price", "Buy Price", "Trade Price", "Difference", "Spread", "Abs Difference", "Profitability")

MismatchHeader = append(CardHeader, "Conditions", "Price", "Reference", "Difference", "Spread")

Expand Down Expand Up @@ -463,7 +463,7 @@ func WriteArbitrageToCSV(arbitrage []ArbitEntry, w io.Writer) error {
fmt.Sprintf("%0.2f", entry.Difference),
fmt.Sprintf("%0.2f", entry.Spread),
fmt.Sprintf("%0.2f", entry.AbsoluteDifference),
fmt.Sprintf("%0.2f", bl.PriceRatio),
fmt.Sprintf("%0.2f", entry.Profitability),
)
if hasExtraSeller {
record = append(record, inv.SellerName)
Expand Down

0 comments on commit bbb8d95

Please sign in to comment.