-
-
Notifications
You must be signed in to change notification settings - Fork 301
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
89 additions
and
7 deletions.
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
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) | ||
} | ||
}) | ||
} | ||
} |