Skip to content

Commit

Permalink
fix: query price range from volume profile trades on every updates. w…
Browse files Browse the repository at this point in the history
…ill make it slower on updates
  • Loading branch information
zenixls2 committed Jan 16, 2023
1 parent 746279d commit 0b71f2f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
27 changes: 19 additions & 8 deletions pkg/indicator/volumeprofile.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,8 @@ type VolumeProfile struct {
}

func (inc *VolumeProfile) Update(price, volume float64, timestamp types.Time) {
if inc.minPrice == 0 {
inc.minPrice = math.Inf(1)
}
if inc.maxPrice == 0 {
inc.maxPrice = math.Inf(-1)
}
inc.minPrice = math.Inf(1)
inc.maxPrice = math.Inf(-1)
if inc.profile == nil {
inc.profile = make(map[float64]float64)
}
Expand All @@ -47,13 +43,14 @@ func (inc *VolumeProfile) Update(price, volume float64, timestamp types.Time) {
for i = 0; i < len(inc.trades); i++ {
td := inc.trades[i]
if td.timestamp.After(filter) {
inc.trades = inc.trades[i : len(inc.trades)-1]
inc.trades = inc.trades[i:len(inc.trades)]
break
}
inc.profile[math.Round(td.price/inc.Delta)] -= td.volume
}

for k, _ := range inc.profile {
for i = 0; i < len(inc.trades); i++ {
k := math.Round(inc.trades[i].price / inc.Delta)
if k < inc.minPrice {
inc.minPrice = k
}
Expand All @@ -74,8 +71,14 @@ func (inc *VolumeProfile) PointOfControlAboveEqual(price float64, limit ...float
if len(limit) > 0 {
filter = limit[0]
}
if inc.Delta == 0 {
panic("Delta for volumeprofile shouldn't be zero")
}
start := math.Round(price / inc.Delta)
vol = math.Inf(-1)
if start > filter {
return 0, 0
}
for ; start <= filter; start += inc.Delta {
abs := math.Abs(inc.profile[start])
if vol < abs {
Expand All @@ -93,8 +96,16 @@ func (inc *VolumeProfile) PointOfControlBelowEqual(price float64, limit ...float
if len(limit) > 0 {
filter = limit[0]
}
if inc.Delta == 0 {
panic("Delta for volumeprofile shouldn't be zero")
}
start := math.Round(price / inc.Delta)
vol = math.Inf(-1)

if start < filter {
return 0, 0
}

for ; start >= filter; start -= inc.Delta {
abs := math.Abs(inc.profile[start])
if vol < abs {
Expand Down
29 changes: 29 additions & 0 deletions pkg/indicator/volumeprofile_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package indicator

import (
"testing"
"time"

"github.com/c9s/bbgo/pkg/types"
"github.com/stretchr/testify/assert"
)

func Test_DelVolumeProfile(t *testing.T) {

vp := VolumeProfile{IntervalWindow: types.IntervalWindow{Window: 1, Interval: types.Interval1s}, Delta: 1.0}
vp.Update(1., 100., types.Time(time.Now()))
r, v := vp.PointOfControlAboveEqual(1.)
assert.Equal(t, r, 1.)
assert.Equal(t, v, 100.)
vp.Update(2., 100., types.Time(time.Now().Add(time.Second*10)))
r, v = vp.PointOfControlAboveEqual(1.)
assert.Equal(t, r, 2.)
assert.Equal(t, v, 100.)
r, v = vp.PointOfControlBelowEqual(1.)
assert.Equal(t, r, 0.)
assert.Equal(t, v, 0.)
r, v = vp.PointOfControlBelowEqual(2.)
assert.Equal(t, r, 2.)
assert.Equal(t, v, 100.)

}

0 comments on commit 0b71f2f

Please sign in to comment.