-
-
Notifications
You must be signed in to change notification settings - Fork 293
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FEATURE: [indicator] add v2 MACD, SMA #1184
Merged
Merged
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9e6cb08
indicator: simplify source, calculate binding
c9s 47e869a
floats: add Truncate method support to floats slice
c9s ee8bbe3
indicator: add v2 sma
c9s 01ef6c2
indicator: add v2 MACD
c9s 0da0b10
indicator: fix SMA truncate call
c9s e320e5d
indicator: remove unused low value indicator
c9s 9c43c75
floats: fix floats.Slice truncate
c9s 23a49a8
indicator: fix klines stream emitter
c9s 3e94584
indicator: fix tests
c9s File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -191,14 +191,18 @@ func (s Slice) Last(i int) float64 { | |
return s[length-1-i] | ||
} | ||
|
||
func (s Slice) Truncate(size int) Slice { | ||
if len(s) < size { | ||
return s | ||
} | ||
|
||
return s[len(s)-size:] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if size is less than or equal to 0? |
||
} | ||
|
||
// Index fetches the element from the end of the slice | ||
// WARNING: it does not start from 0!!! | ||
func (s Slice) Index(i int) float64 { | ||
length := len(s) | ||
if i < 0 || length-1-i < 0 { | ||
return 0.0 | ||
} | ||
return s[length-1-i] | ||
return s.Last(i) | ||
} | ||
|
||
func (s Slice) Length() int { | ||
|
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
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,7 +1,6 @@ | ||
package indicator | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/c9s/bbgo/pkg/datatype/floats" | ||
|
@@ -82,21 +81,3 @@ func (inc *SMA) LoadK(allKLines []types.KLine) { | |
inc.PushK(k) | ||
} | ||
} | ||
|
||
func calculateSMA(kLines []types.KLine, window int, priceF KLineValueMapper) (float64, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unused |
||
length := len(kLines) | ||
if length == 0 || length < window { | ||
return 0.0, fmt.Errorf("insufficient elements for calculating SMA with window = %d", window) | ||
} | ||
if length != window { | ||
return 0.0, fmt.Errorf("too much klines passed in, requires only %d klines", window) | ||
} | ||
|
||
sum := 0.0 | ||
for _, k := range kLines { | ||
sum += priceF(k) | ||
} | ||
|
||
avg := sum / float64(window) | ||
return avg, nil | ||
} |
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
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,29 @@ | ||
package indicator | ||
|
||
type MACDStream struct { | ||
*SubtractStream | ||
|
||
shortWindow, longWindow, signalWindow int | ||
|
||
fastEWMA, slowEWMA, signal *EWMAStream | ||
histogram *SubtractStream | ||
} | ||
|
||
func MACD2(source Float64Source, shortWindow, longWindow, signalWindow int) *MACDStream { | ||
// bind and calculate these first | ||
fastEWMA := EWMA2(source, shortWindow) | ||
slowEWMA := EWMA2(source, longWindow) | ||
macd := Subtract(fastEWMA, slowEWMA) | ||
signal := EWMA2(macd, signalWindow) | ||
histogram := Subtract(macd, signal) | ||
return &MACDStream{ | ||
SubtractStream: macd, | ||
shortWindow: shortWindow, | ||
longWindow: longWindow, | ||
signalWindow: signalWindow, | ||
fastEWMA: fastEWMA, | ||
slowEWMA: slowEWMA, | ||
signal: signal, | ||
histogram: histogram, | ||
} | ||
} |
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,57 @@ | ||
package indicator | ||
|
||
import ( | ||
"encoding/json" | ||
"math" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/c9s/bbgo/pkg/fixedpoint" | ||
"github.com/c9s/bbgo/pkg/types" | ||
) | ||
|
||
/* | ||
python: | ||
|
||
import pandas as pd | ||
s = pd.Series([0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9]) | ||
slow = s.ewm(span=26, adjust=False).mean() | ||
fast = s.ewm(span=12, adjust=False).mean() | ||
print(fast - slow) | ||
*/ | ||
|
||
func Test_MACD2(t *testing.T) { | ||
var randomPrices = []byte(`[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]`) | ||
var input []fixedpoint.Value | ||
err := json.Unmarshal(randomPrices, &input) | ||
assert.NoError(t, err) | ||
|
||
tests := []struct { | ||
name string | ||
kLines []types.KLine | ||
want float64 | ||
}{ | ||
{ | ||
name: "random_case", | ||
kLines: buildKLines(input), | ||
want: 0.7967670223776384, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
prices := &PriceStream{} | ||
macd := MACD2(prices, 12, 26, 9) | ||
for _, k := range tt.kLines { | ||
prices.EmitUpdate(k.Close.Float64()) | ||
} | ||
|
||
got := macd.Last(0) | ||
diff := math.Trunc((got-tt.want)*100) / 100 | ||
if diff != 0 { | ||
t.Errorf("MACD2() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
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
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,29 @@ | ||
package indicator | ||
|
||
import "github.com/c9s/bbgo/pkg/types" | ||
|
||
type SMAStream struct { | ||
Float64Series | ||
window int | ||
rawValues *types.Queue | ||
} | ||
|
||
func SMA2(source Float64Source, window int) *SMAStream { | ||
s := &SMAStream{ | ||
Float64Series: NewFloat64Series(), | ||
window: window, | ||
rawValues: types.NewQueue(window), | ||
} | ||
s.Bind(source, s) | ||
return s | ||
} | ||
|
||
func (s *SMAStream) Calculate(v float64) float64 { | ||
s.rawValues.Update(v) | ||
sma := s.rawValues.Mean(s.window) | ||
return sma | ||
} | ||
|
||
func (s *SMAStream) Truncate() { | ||
s.slice.Truncate(MaxNumOfSMA) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could be <=.