fix: rename getSource to sourceGetter

This commit is contained in:
zenix 2022-09-06 19:15:13 +09:00
parent 36a5579660
commit 66c8a3bb0d
2 changed files with 12 additions and 12 deletions

View File

@ -13,7 +13,7 @@ type SourceFunc func(*types.KLine) fixedpoint.Value
type selectorInternal struct { type selectorInternal struct {
Source string Source string
getSource SourceFunc sourceGetter SourceFunc
} }
func (s *selectorInternal) UnmarshalJSON(d []byte) error { func (s *selectorInternal) UnmarshalJSON(d []byte) error {
@ -39,26 +39,26 @@ type SourceSelector struct {
func (s *selectorInternal) init() { func (s *selectorInternal) init() {
switch strings.ToLower(s.Source) { switch strings.ToLower(s.Source) {
case "close": case "close":
s.getSource = func(kline *types.KLine) fixedpoint.Value { return kline.Close } s.sourceGetter = func(kline *types.KLine) fixedpoint.Value { return kline.Close }
case "high": case "high":
s.getSource = func(kline *types.KLine) fixedpoint.Value { return kline.High } s.sourceGetter = func(kline *types.KLine) fixedpoint.Value { return kline.High }
case "low": case "low":
s.getSource = func(kline *types.KLine) fixedpoint.Value { return kline.Low } s.sourceGetter = func(kline *types.KLine) fixedpoint.Value { return kline.Low }
case "hl2": case "hl2":
s.getSource = func(kline *types.KLine) fixedpoint.Value { return kline.High.Add(kline.Low).Div(fixedpoint.Two) } s.sourceGetter = func(kline *types.KLine) fixedpoint.Value { return kline.High.Add(kline.Low).Div(fixedpoint.Two) }
case "hlc3": case "hlc3":
s.getSource = func(kline *types.KLine) fixedpoint.Value { s.sourceGetter = func(kline *types.KLine) fixedpoint.Value {
return kline.High.Add(kline.Low).Add(kline.Close).Div(fixedpoint.Three) return kline.High.Add(kline.Low).Add(kline.Close).Div(fixedpoint.Three)
} }
case "ohlc4": case "ohlc4":
s.getSource = func(kline *types.KLine) fixedpoint.Value { s.sourceGetter = func(kline *types.KLine) fixedpoint.Value {
return kline.High.Add(kline.Low).Add(kline.Close).Add(kline.Open).Div(fixedpoint.Four) return kline.High.Add(kline.Low).Add(kline.Close).Add(kline.Open).Div(fixedpoint.Four)
} }
case "open": case "open":
s.getSource = func(kline *types.KLine) fixedpoint.Value { return kline.Open } s.sourceGetter = func(kline *types.KLine) fixedpoint.Value { return kline.Open }
default: default:
log.Infof("source not set: %s, use hl2 by default", s.Source) log.Infof("source not set: %s, use hl2 by default", s.Source)
s.getSource = func(kline *types.KLine) fixedpoint.Value { return kline.High.Add(kline.Low).Div(fixedpoint.Two) } s.sourceGetter = func(kline *types.KLine) fixedpoint.Value { return kline.High.Add(kline.Low).Div(fixedpoint.Two) }
} }
} }
@ -76,5 +76,5 @@ func (s *SourceSelector) GetSource(kline *types.KLine) fixedpoint.Value {
s.Source.Source = "close" s.Source.Source = "close"
s.Source.init() s.Source.init()
} }
return s.Source.getSource(kline) return s.Source.sourceGetter(kline)
} }

View File

@ -17,7 +17,7 @@ func TestSource(t *testing.T) {
s := Strategy{} s := Strategy{}
assert.NoError(t, json.Unmarshal([]byte(input), &s)) assert.NoError(t, json.Unmarshal([]byte(input), &s))
assert.Equal(t, s.Source.Source, "high") assert.Equal(t, s.Source.Source, "high")
assert.NotNil(t, s.Source.getSource) assert.NotNil(t, s.Source.sourceGetter)
e, err := json.Marshal(&s) e, err := json.Marshal(&s)
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, input, string(e)) assert.Equal(t, input, string(e))