mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-21 22:43:52 +00:00
bbgo: solve the scale when unmarshalling the json
This commit is contained in:
parent
d123e89a1b
commit
263c0883d1
|
@ -1,6 +1,7 @@
|
|||
package bbgo
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math"
|
||||
|
||||
|
@ -318,6 +319,18 @@ type LayerScale struct {
|
|||
LayerRule *SlideRule `json:"byLayer"`
|
||||
}
|
||||
|
||||
func (s *LayerScale) UnmarshalJSON(data []byte) error {
|
||||
type T LayerScale
|
||||
var p T
|
||||
err := json.Unmarshal(data, &p)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*s = LayerScale(p)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *LayerScale) Scale(layer int) (quantity float64, err error) {
|
||||
if s.LayerRule == nil {
|
||||
err = errors.New("either price or volume scale is not defined")
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package bbgo
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
@ -8,6 +9,24 @@ import (
|
|||
|
||||
const delta = 1e-9
|
||||
|
||||
func TestLayerScale_UnmarshalJSON(t *testing.T) {
|
||||
var s LayerScale
|
||||
err := json.Unmarshal([]byte(`{
|
||||
"byLayer": {
|
||||
"linear": {
|
||||
"domain": [ 1, 3 ],
|
||||
"range": [ 10000.0, 30000.0 ]
|
||||
}
|
||||
}
|
||||
}`), &s)
|
||||
assert.NoError(t, err)
|
||||
|
||||
if assert.NotNil(t, s.LayerRule) {
|
||||
assert.NotNil(t, s.LayerRule.LinearScale.Range)
|
||||
assert.NotNil(t, s.LayerRule.LinearScale.Domain)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExponentialScale(t *testing.T) {
|
||||
// graph see: https://www.desmos.com/calculator/ip0ijbcbbf
|
||||
scale := ExponentialScale{
|
||||
|
|
Loading…
Reference in New Issue
Block a user