bbgo_origin/pkg/bbgo/scale.go

442 lines
8.9 KiB
Go
Raw Permalink Normal View History

2021-02-27 17:53:00 +00:00
package bbgo
import (
"encoding/json"
2021-02-27 17:53:00 +00:00
"fmt"
"math"
"github.com/pkg/errors"
2021-02-27 17:53:00 +00:00
)
type Scale interface {
Solve() error
Formula() string
FormulaOf(x float64) string
Call(x float64) (y float64)
2023-06-14 07:16:32 +00:00
Sum(step float64) float64
}
func init() {
_ = Scale(&ExponentialScale{})
_ = Scale(&LogarithmicScale{})
_ = Scale(&LinearScale{})
_ = Scale(&QuadraticScale{})
}
2023-06-14 07:16:32 +00:00
// f(x) := ab^x
2021-02-27 17:53:00 +00:00
// y := ab^x
// shift xs[0] to 0 (x - h)
// a = y1
//
// y := ab^(x-h)
// y2/a = b^(x2-h)
// y2/y1 = b^(x2-h)
//
// also posted at https://play.golang.org/p/JlWlwZjoebE
type ExponentialScale struct {
2021-02-27 17:53:00 +00:00
Domain [2]float64 `json:"domain"`
Range [2]float64 `json:"range"`
a float64
b float64
h float64
s float64
2021-02-27 17:53:00 +00:00
}
func (s *ExponentialScale) Solve() error {
2022-01-26 18:32:26 +00:00
if s.Domain[0] > s.Domain[1] {
return errors.New("domain[0] can not greater than domain[1]")
}
2022-01-26 17:40:54 +00:00
if s.Range[0] == 0 {
return errors.New("for ExponentialScale, range can not start from 0")
}
2022-01-26 18:32:26 +00:00
s.h = s.Domain[0]
s.a = s.Range[0]
2021-02-27 17:53:00 +00:00
s.b = math.Pow(s.Range[1]/s.Range[0], 1/(s.Domain[1]-s.h))
s.s = s.Domain[1] - s.h
2021-02-27 17:53:00 +00:00
return nil
}
2023-06-14 07:16:32 +00:00
func (s *ExponentialScale) Sum(step float64) float64 {
sum := 0.0
for x := s.Domain[0]; x <= s.Domain[1]; x += step {
sum += s.Call(x)
}
return sum
}
func (s *ExponentialScale) String() string {
2021-02-27 17:53:00 +00:00
return s.Formula()
}
func (s *ExponentialScale) Formula() string {
2021-02-27 17:53:00 +00:00
return fmt.Sprintf("f(x) = %f * %f ^ (x - %f)", s.a, s.b, s.h)
}
func (s *ExponentialScale) FormulaOf(x float64) string {
2021-02-27 17:53:00 +00:00
return fmt.Sprintf("f(%f) = %f * %f ^ (%f - %f)", x, s.a, s.b, x, s.h)
}
func (s *ExponentialScale) Call(x float64) (y float64) {
2021-02-27 18:20:47 +00:00
if x < s.Domain[0] {
x = s.Domain[0]
} else if x > s.Domain[1] {
x = s.Domain[1]
}
y = s.a * math.Pow(s.Range[1]/s.Range[0], (x-s.h)/s.s)
2021-02-27 17:53:00 +00:00
return y
}
type LogarithmicScale struct {
2021-02-27 17:53:00 +00:00
Domain [2]float64 `json:"domain"`
Range [2]float64 `json:"range"`
h float64
s float64
a float64
}
func (s *LogarithmicScale) Call(x float64) (y float64) {
2021-02-27 18:20:47 +00:00
if x < s.Domain[0] {
x = s.Domain[0]
} else if x > s.Domain[1] {
x = s.Domain[1]
}
2021-02-27 17:53:00 +00:00
// y = a * log(x - h) + s
2021-02-27 18:06:33 +00:00
y = s.a*math.Log(x-s.h) + s.s
2021-02-27 17:53:00 +00:00
return y
}
2023-06-14 07:16:32 +00:00
func (s *LogarithmicScale) Sum(step float64) float64 {
sum := 0.0
for x := s.Domain[0]; x <= s.Domain[1]; x += step {
sum += s.Call(x)
}
return sum
}
func (s *LogarithmicScale) String() string {
2021-02-27 17:53:00 +00:00
return s.Formula()
}
func (s *LogarithmicScale) Formula() string {
2021-02-27 17:53:00 +00:00
return fmt.Sprintf("f(x) = %f * log(x - %f) + %f", s.a, s.h, s.s)
}
func (s *LogarithmicScale) FormulaOf(x float64) string {
2021-02-27 17:53:00 +00:00
return fmt.Sprintf("f(%f) = %f * log(%f - %f) + %f", x, s.a, x, s.h, s.s)
}
func (s *LogarithmicScale) Solve() error {
2021-02-27 17:53:00 +00:00
// f(x) = a * log2(x - h) + s
//
// log2(1) = 0
//
// h = x1 - 1
// s = y1
//
// y2 = a * log(x2 - h) + s
// y2 = a * log(x2 - h) + y1
// y2 - y1 = a * log(x2 - h)
// a = (y2 - y1) / log(x2 - h)
s.h = s.Domain[0] - 1
s.s = s.Range[0]
2021-02-27 18:06:33 +00:00
s.a = (s.Range[1] - s.Range[0]) / math.Log(s.Domain[1]-s.h)
return nil
}
2021-02-27 18:20:47 +00:00
type LinearScale struct {
Domain [2]float64 `json:"domain"`
Range [2]float64 `json:"range"`
2023-03-08 07:10:34 +00:00
// a is the ratio for Range to Domain
a float64
2021-02-27 18:20:47 +00:00
}
func (s *LinearScale) Solve() error {
xs := s.Domain
ys := s.Range
2023-03-08 07:10:34 +00:00
2021-02-27 18:20:47 +00:00
s.a = (ys[1] - ys[0]) / (xs[1] - xs[0])
2023-03-08 07:10:34 +00:00
2021-02-27 18:20:47 +00:00
return nil
}
func (s *LinearScale) Call(x float64) (y float64) {
2023-03-08 07:10:34 +00:00
if x <= s.Domain[0] {
return s.Range[0]
} else if x >= s.Domain[1] {
return s.Range[1]
2021-02-27 18:20:47 +00:00
}
2023-03-08 07:10:34 +00:00
y = s.Range[0] + (x-s.Domain[0])*s.a
2021-02-27 18:20:47 +00:00
return y
}
2023-06-14 07:16:32 +00:00
func (s *LinearScale) Sum(step float64) float64 {
sum := 0.0
for x := s.Domain[0]; x <= s.Domain[1]; x += step {
sum += s.Call(x)
}
return sum
}
2021-02-27 18:20:47 +00:00
func (s *LinearScale) String() string {
return s.Formula()
}
func (s *LinearScale) Formula() string {
2023-03-08 07:10:34 +00:00
return fmt.Sprintf("f(x) = %f + (x - %f) * %f", s.Range[0], s.Domain[0], s.a)
2021-02-27 18:20:47 +00:00
}
func (s *LinearScale) FormulaOf(x float64) string {
2023-03-08 11:31:47 +00:00
return fmt.Sprintf("f(%f) = %f + (%f - %f) * %f", x, s.Range[0], x, s.Domain[0], s.a)
2021-02-27 18:20:47 +00:00
}
// see also: http://www.vb-helper.com/howto_find_quadratic_curve.html
2021-02-27 18:06:33 +00:00
type QuadraticScale struct {
Domain [3]float64 `json:"domain"`
Range [3]float64 `json:"range"`
a, b, c float64
}
func (s *QuadraticScale) Solve() error {
xs := s.Domain
ys := s.Range
s.a = ((ys[1]-ys[0])*(xs[0]-xs[2]) + (ys[2]-ys[0])*(xs[1]-xs[0])) /
((xs[0]-xs[2])*(math.Pow(xs[1], 2)-math.Pow(xs[0], 2)) + (xs[1]-xs[0])*(math.Pow(xs[2], 2)-math.Pow(xs[0], 2)))
s.b = ((ys[1] - ys[0]) - s.a*(math.Pow(xs[1], 2)-math.Pow(xs[0], 2))) / (xs[1] - xs[0])
s.c = ys[1] - s.a*math.Pow(xs[1], 2) - s.b*xs[1]
2021-02-27 17:53:00 +00:00
return nil
}
2021-02-27 18:06:33 +00:00
func (s *QuadraticScale) Call(x float64) (y float64) {
2021-02-27 18:20:47 +00:00
if x < s.Domain[0] {
x = s.Domain[0]
} else if x > s.Domain[2] {
x = s.Domain[2]
}
2021-02-27 18:06:33 +00:00
// y = a * log(x - h) + s
y = s.a*math.Pow(x, 2) + s.b*x + s.c
2021-02-27 18:06:33 +00:00
return y
}
2023-06-14 07:16:32 +00:00
func (s *QuadraticScale) Sum(step float64) float64 {
sum := 0.0
for x := s.Domain[0]; x <= s.Domain[1]; x += step {
sum += s.Call(x)
}
return sum
}
2021-02-27 18:06:33 +00:00
func (s *QuadraticScale) String() string {
return s.Formula()
}
func (s *QuadraticScale) Formula() string {
return fmt.Sprintf("f(x) = %f * x ^ 2 + %f * x + %f", s.a, s.b, s.c)
}
func (s *QuadraticScale) FormulaOf(x float64) string {
return fmt.Sprintf("f(%f) = %f * %f ^ 2 + %f * %f + %f", x, s.a, x, s.b, x, s.c)
}
type SlideRule struct {
// Scale type could be one of "log", "exp", "linear", "quadratic"
// this is similar to the d3.scale
LinearScale *LinearScale `json:"linear"`
LogScale *LogarithmicScale `json:"log"`
ExpScale *ExponentialScale `json:"exp"`
QuadraticScale *QuadraticScale `json:"quadratic"`
}
2021-06-16 05:14:10 +00:00
func (rule *SlideRule) Range() ([2]float64, error) {
if rule.LogScale != nil {
return rule.LogScale.Range, nil
}
if rule.ExpScale != nil {
return rule.ExpScale.Range, nil
}
if rule.LinearScale != nil {
return rule.LinearScale.Range, nil
}
if rule.QuadraticScale != nil {
r := rule.QuadraticScale.Range
return [2]float64{r[0], r[len(r)-1]}, nil
}
return [2]float64{}, errors.New("no any scale domain is defined")
}
func (rule *SlideRule) Scale() (Scale, error) {
if rule.LogScale != nil {
return rule.LogScale, nil
}
if rule.ExpScale != nil {
return rule.ExpScale, nil
}
if rule.LinearScale != nil {
return rule.LinearScale, nil
}
if rule.QuadraticScale != nil {
return rule.QuadraticScale, nil
}
return nil, errors.New("no any scale is defined")
}
2021-05-09 18:52:41 +00:00
// LayerScale defines the scale DSL for maker layers, e.g.,
//
// quantityScale:
2023-06-14 07:16:32 +00:00
//
// byLayer:
// exp:
// domain: [1, 5]
// range: [0.01, 1.0]
2021-05-09 18:52:41 +00:00
//
// and
//
// quantityScale:
2023-06-14 07:16:32 +00:00
//
// byLayer:
// linear:
// domain: [1, 3]
// range: [0.01, 1.0]
2021-05-09 18:52:41 +00:00
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
}
2021-05-09 18:52:41 +00:00
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")
return
}
scale, err := s.LayerRule.Scale()
if err != nil {
return 0, err
}
if err := scale.Solve(); err != nil {
return 0, err
}
return scale.Call(float64(layer)), nil
}
// PriceVolumeScale defines the scale DSL for strategy, e.g.,
//
2021-05-09 18:52:41 +00:00
// quantityScale:
2023-06-14 07:16:32 +00:00
//
// byPrice:
// exp:
// domain: [10_000, 50_000]
// range: [0.01, 1.0]
//
// and
//
2021-05-09 18:52:41 +00:00
// quantityScale:
2023-06-14 07:16:32 +00:00
//
// byVolume:
// linear:
// domain: [10_000, 50_000]
// range: [0.01, 1.0]
type PriceVolumeScale struct {
2021-05-09 18:52:41 +00:00
ByPriceRule *SlideRule `json:"byPrice"`
ByVolumeRule *SlideRule `json:"byVolume"`
}
2021-05-09 18:52:41 +00:00
func (s *PriceVolumeScale) Scale(price float64, volume float64) (quantity float64, err error) {
if s.ByPriceRule != nil {
quantity, err = s.ScaleByPrice(price)
return
2021-05-09 18:52:41 +00:00
} else if s.ByVolumeRule != nil {
quantity, err = s.ScaleByVolume(volume)
} else {
err = errors.New("either price or volume scale is not defined")
}
return
}
// ScaleByPrice scale quantity by the given price
2021-05-09 18:52:41 +00:00
func (s *PriceVolumeScale) ScaleByPrice(price float64) (float64, error) {
if s.ByPriceRule == nil {
return 0, errors.New("byPrice scale is not defined")
}
2021-05-09 18:52:41 +00:00
scale, err := s.ByPriceRule.Scale()
if err != nil {
return 0, err
}
2021-03-15 18:14:10 +00:00
if err := scale.Solve(); err != nil {
return 0, err
}
return scale.Call(price), nil
}
// ScaleByVolume scale quantity by the given volume
2021-05-09 18:52:41 +00:00
func (s *PriceVolumeScale) ScaleByVolume(volume float64) (float64, error) {
if s.ByVolumeRule == nil {
return 0, errors.New("byVolume scale is not defined")
}
2021-05-09 18:52:41 +00:00
scale, err := s.ByVolumeRule.Scale()
if err != nil {
return 0, err
}
2021-03-15 18:14:10 +00:00
if err := scale.Solve(); err != nil {
return 0, err
}
return scale.Call(volume), nil
}
2022-01-26 17:40:54 +00:00
type PercentageScale struct {
ByPercentage *SlideRule `json:"byPercentage"`
}
func (s *PercentageScale) Scale(percentage float64) (float64, error) {
if s.ByPercentage == nil {
return 0.0, errors.New("percentage scale is not defined")
}
scale, err := s.ByPercentage.Scale()
if err != nil {
return 0.0, err
}
if err := scale.Solve(); err != nil {
return 0.0, err
}
return scale.Call(percentage), nil
}