Skip to content

Commit

Permalink
vwap prototype
Browse files Browse the repository at this point in the history
Use the volumeWeightedSum variable to calculate the sum of the product of each trade's price and volume.
Store the price of the last trade with a volume greater than zero in the lastActivePrice variable.
After traversing all trades, if the total volume (totalVolume) is greater than zero, calculate and return the VWAP by dividing volumeWeightedSum by totalVolume.
  • Loading branch information
notJoon committed May 3, 2024
1 parent cebd4ec commit 3b2663c
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: "1.22.2"
go-version: "1.22.x"

- name: Run tests
run: go test ./...
Expand Down
7 changes: 7 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
module github.com/gnoswap-labs/vwap

go 1.22.2

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.9.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
24 changes: 24 additions & 0 deletions vwap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package vwap

type Trade struct {
Price float64
Volume int
}

func VWAP(trades []Trade) float64 {
var (
totalVolume int
totalValue float64
)

for _, trade := range trades {
totalVolume += trade.Volume
totalValue += trade.Price * float64(trade.Volume)
}

if totalVolume == 0 {
return 0
}

return totalValue / float64(totalVolume)
}
61 changes: 61 additions & 0 deletions vwap_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package vwap

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestVWAP(t *testing.T) {
tests := []struct {
name string
trades []Trade
expectedVWAP float64
}{
{
name: "Normal case",
trades: []Trade{
{Price: 100.0, Volume: 100},
{Price: 101.0, Volume: 200},
{Price: 102.0, Volume: 150},
{Price: 103.0, Volume: 50},
},
expectedVWAP: 101.3,
},
{
name: "Empty trades",
trades: []Trade{},
expectedVWAP: 0.0,
},
{
name: "Single trade",
trades: []Trade{
{Price: 100.0, Volume: 100},
},
expectedVWAP: 100.0,
},
{
name: "Zero volume trades",
trades: []Trade{
{Price: 100.0, Volume: 0},
{Price: 101.0, Volume: 0},
},
expectedVWAP: 0.0,
},
{
name: "Large volume",
trades: []Trade{
{Price: 100.0, Volume: 1000000},
{Price: 101.0, Volume: 2000000},
},
expectedVWAP: 100.666667,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
vwap := VWAP(tt.trades)
assert.InDelta(t, tt.expectedVWAP, vwap, 0.000001, "Expected VWAP")
})
}
}

0 comments on commit 3b2663c

Please sign in to comment.