Skip to content

Commit

Permalink
calculate volume and vwap by using swap histroy data
Browse files Browse the repository at this point in the history
  • Loading branch information
notJoon committed May 21, 2024
1 parent 20e5cc0 commit d1e07ff
Showing 1 changed file with 63 additions and 2 deletions.
65 changes: 63 additions & 2 deletions main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,17 @@ func main() {
priceMap["gno.land/r/demo/foo"] = 0.0

priceHistory := calculatePriceHistory(transactions, priceMap)
volumeHistory := calculateVolumeHistory(transactions)

for i := 0; i < len(priceHistory); i++ {
entry := priceHistory[i]
volumeEntry := volumeHistory[i]

for _, entry := range priceHistory {
fmt.Printf("Time: %s\n", entry.time.Format(layout))
for token, price := range entry.prices {
fmt.Printf("%s: $%.4f\n", token, price)
volume := volumeEntry.volumes[token]
vwap := calculateVWAP(token, transactions, entry.time)
fmt.Printf("%s: $%.4f, Volume: %d, VWAP: $%.4f\n", token, price, volume, vwap)
}
fmt.Println("-----------")
}
Expand Down Expand Up @@ -103,6 +109,48 @@ func updatePrices(tx Transaction, prices map[string]float64) {
}
}

func calculateVolumeHistory(transactions []Transaction) []VolumeEntry {
var volumeHistory []VolumeEntry
currentVolumes := make(map[string]int)

start := transactions[0].time

for current := start; current.Before(transactions[len(transactions)-1].time); current = current.Add(10 * time.Minute) {
for _, tx := range transactions {
if tx.time.Before(current.Add(10 * time.Minute)) {
currentVolumes[tx.token0Path] += tx.amount0
currentVolumes[tx.token1Path] += -tx.amount1
}
}
volumeHistory = append(volumeHistory, VolumeEntry{time: current, volumes: copyIntMap(currentVolumes)})
}

return volumeHistory
}

func calculateVWAP(token string, transactions []Transaction, currentTime time.Time) float64 {
var totalValue float64
var totalVolume int

for _, tx := range transactions {
if tx.time.Before(currentTime.Add(10 * time.Minute)) {
if tx.token0Path == token {
totalValue += float64(tx.amount0) * float64(-tx.amount1) / float64(tx.amount0)
totalVolume += tx.amount0
} else if tx.token1Path == token {
totalValue += float64(-tx.amount1)
totalVolume += -tx.amount1
}
}
}

if totalVolume == 0 {
return 0
}

return totalValue / float64(totalVolume)
}

func copyMap(original map[string]float64) map[string]float64 {
newMap := make(map[string]float64)
for key, value := range original {
Expand All @@ -111,7 +159,20 @@ func copyMap(original map[string]float64) map[string]float64 {
return newMap
}

func copyIntMap(original map[string]int) map[string]int {
newMap := make(map[string]int)
for key, value := range original {
newMap[key] = value
}
return newMap
}

type PriceEntry struct {
time time.Time
prices map[string]float64
}

type VolumeEntry struct {
time time.Time
volumes map[string]int
}

0 comments on commit d1e07ff

Please sign in to comment.