-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
5 changed files
with
102 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
}) | ||
} | ||
} |