From abde9e5bfbaa8fc9df040633f0790744a3b2f021 Mon Sep 17 00:00:00 2001 From: Jesse Kuang Date: Fri, 23 Nov 2018 17:43:04 +0800 Subject: [PATCH 1/9] fix TestHeikinashiCandles, return index from 1 --- talib_test.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/talib_test.go b/talib_test.go index a496dea..986c250 100644 --- a/talib_test.go +++ b/talib_test.go @@ -674,24 +674,32 @@ func TestHeikinashiCandles(t *testing.T) { resultHigh, resultOpen, resultClose, resultLow := HeikinashiCandles(testHigh, testOpen, testClose, testLow) for i, expected := range expectedHighs { + i++ + if i == len(expectedHighs) { continue } if resultHigh[i] != expected { t.Errorf("Highs error: Expected %f at cell %d got %f ", expected, i, resultHigh[i]) } } for i, expected := range expectedOpens { + i++ + if i == len(expectedOpens) { continue } if resultOpen[i] != expected { t.Errorf("Opens error: Expected %f at cell %d got %f ", expected, i, resultOpen[i]) } } for i, expected := range expectedCloses { + i++ + if i == len(expectedCloses) { continue } if resultClose[i] != expected { t.Errorf("Closes error: Expected %f at cell %d got %f ", expected, i, resultClose[i]) } } for i, expected := range expectedLows { + i++ + if i == len(expectedLows) { continue } if resultLow[i] != expected { t.Errorf("Lows error: Expected %f at cell %d got %f ", expected, i, resultLow[i]) } @@ -735,7 +743,7 @@ func TestCrossunder(t *testing.T) { series1 = []float64{1, 2, 3, 4, 8, 6, 7} series2 = []float64{1, 4, 5, 9, 5, 3, 7} - if Crossunder(series1, series2) == true { + if Crossover(series1, series2) == true { t.Error("Crossunder: Not expected and found") } From ed1653a100eb42fab0d3859d9766bd882479a4c5 Mon Sep 17 00:00:00 2001 From: Jesse Kuang Date: Fri, 23 Nov 2018 22:08:58 +0800 Subject: [PATCH 2/9] fix TestCrossover --- talib_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/talib_test.go b/talib_test.go index 986c250..089e678 100644 --- a/talib_test.go +++ b/talib_test.go @@ -53,7 +53,7 @@ var ( testNothingCrossed1 = []float64{1, 2, 3, 4, 8, 6, 7} testNothingCrossed2 = []float64{1, 4, 5, 9, 5, 3, 7} - testCrossover1 = []float64{1, 3, 2, 4, 8, 6, 7} + testCrossover1 = []float64{1, 3, 2, 4, 8, 3, 8} testCrossover2 = []float64{1, 5, 1, 4, 5, 6, 7} ) From 9b94426e4329fbd88a0882dbbbf18dbdd2b74c67 Mon Sep 17 00:00:00 2001 From: Jesse Kuang Date: Sun, 25 Nov 2018 11:55:34 +0800 Subject: [PATCH 3/9] optimize Macd --- talib.go | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/talib.go b/talib.go index 5159890..30ff627 100644 --- a/talib.go +++ b/talib.go @@ -2010,13 +2010,23 @@ func Macd(inReal []float64, inFastPeriod int, inSlowPeriod int, inSignalPeriod i fastEMABuffer[i] = fastEMABuffer[i] - slowEMABuffer[i] } - outMACD := make([]float64, len(inReal)) - for i := lookbackTotal - 1; i < len(fastEMABuffer); i++ { - outMACD[i] = fastEMABuffer[i] + /* + for i := lookbackTotal - 1; i < len(fastEMABuffer); i++ { + outMACD[i] = fastEMABuffer[i] + } + */ + outMACD := fastEMABuffer + for i := 0; i < lookbackTotal-1; i++ { + outMACD[i] = 0 } + outMACDSignal := ema(outMACD, inSignalPeriod, (2.0 / float64(inSignalPeriod+1))) - outMACDHist := make([]float64, len(inReal)) + //outMACDHist := make([]float64, len(inReal)) + outMACDHist := slowEMABuffer + for i := 0; i < lookbackTotal; i++ { + outMACDHist[i] = 0 + } for i := lookbackTotal; i < len(outMACDHist); i++ { outMACDHist[i] = outMACD[i] - outMACDSignal[i] } From 08872d164d617fe12610c6711b9069358954cf39 Mon Sep 17 00:00:00 2001 From: Jesse Kuang Date: Mon, 26 Nov 2018 01:55:29 +0800 Subject: [PATCH 4/9] add compare cgo --- talib_test.go | 40 ++++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/talib_test.go b/talib_test.go index 089e678..c882dac 100644 --- a/talib_test.go +++ b/talib_test.go @@ -18,9 +18,10 @@ import ( "strings" "testing" - "github.com/stretchr/testify/require" + "github.com/kjx98/cgo-talib" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func ok(t *testing.T, err error) { @@ -160,7 +161,9 @@ func TestRsi(t *testing.T) { func TestAdd(t *testing.T) { result := Add(testHigh, testLow) - compare(t, result, "result = talib.ADD(testHigh,testLow)") + cgo_result := talib.Add(testHigh, testLow) + equals(t, result, cgo_result) + //compare(t, result, "result = talib.ADD(testHigh,testLow)") } func TestDiv(t *testing.T) { @@ -402,12 +405,16 @@ func TestWillR(t *testing.T) { func TestAdx(t *testing.T) { result := Adx(testHigh, testLow, testClose, 14) - compare(t, result, "result = talib.ADX(testHigh,testLow,testClose,14)") + cgo_result := talib.Adx(testHigh, testLow, testClose, 14) + equals(t, result, cgo_result) + //compare(t, result, "result = talib.ADX(testHigh,testLow,testClose,14)") } func TestAdxR(t *testing.T) { result := AdxR(testHigh, testLow, testClose, 5) - compare(t, result, "result = talib.ADXR(testHigh,testLow,testClose,5)") + cgo_result := talib.Adxr(testHigh, testLow, testClose, 5) + equals(t, result, cgo_result) + //compare(t, result, "result = talib.ADXR(testHigh,testLow,testClose,5)") } func TestCci(t *testing.T) { @@ -658,12 +665,17 @@ func TestMacdFix(t *testing.T) { func TestAd(t *testing.T) { result := Ad(testHigh, testLow, testClose, testVolume) - compare(t, result, "result = talib.AD(testHigh,testLow,testClose,testVolume)") + //compare(t, result, "result = talib.AD(testHigh,testLow,testClose,testVolume)") + cgo_result := talib.Ad(testHigh, testLow, testClose, testVolume) + equals(t, result, cgo_result) } func TestAdOsc(t *testing.T) { result := AdOsc(testHigh, testLow, testClose, testVolume, 3, 10) - compare(t, result, "result = talib.ADOSC(testHigh,testLow,testClose,testVolume,3,10)") + cgo_result := talib.AdOsc(testHigh, testLow, testClose, testVolume, 3, 10) + //compare(t, result, "result = talib.ADOSC(testHigh,testLow,testClose,testVolume,3,10)") + //compare_cgo(t, result, cgo_result) + equals(t, result, cgo_result) } func TestHeikinashiCandles(t *testing.T) { @@ -675,7 +687,9 @@ func TestHeikinashiCandles(t *testing.T) { resultHigh, resultOpen, resultClose, resultLow := HeikinashiCandles(testHigh, testOpen, testClose, testLow) for i, expected := range expectedHighs { i++ - if i == len(expectedHighs) { continue } + if i == len(expectedHighs) { + continue + } if resultHigh[i] != expected { t.Errorf("Highs error: Expected %f at cell %d got %f ", expected, i, resultHigh[i]) } @@ -683,7 +697,9 @@ func TestHeikinashiCandles(t *testing.T) { for i, expected := range expectedOpens { i++ - if i == len(expectedOpens) { continue } + if i == len(expectedOpens) { + continue + } if resultOpen[i] != expected { t.Errorf("Opens error: Expected %f at cell %d got %f ", expected, i, resultOpen[i]) } @@ -691,7 +707,9 @@ func TestHeikinashiCandles(t *testing.T) { for i, expected := range expectedCloses { i++ - if i == len(expectedCloses) { continue } + if i == len(expectedCloses) { + continue + } if resultClose[i] != expected { t.Errorf("Closes error: Expected %f at cell %d got %f ", expected, i, resultClose[i]) } @@ -699,7 +717,9 @@ func TestHeikinashiCandles(t *testing.T) { for i, expected := range expectedLows { i++ - if i == len(expectedLows) { continue } + if i == len(expectedLows) { + continue + } if resultLow[i] != expected { t.Errorf("Lows error: Expected %f at cell %d got %f ", expected, i, resultLow[i]) } From 5b3a6182f10f88486758ed2188f35bdcdb3540f9 Mon Sep 17 00:00:00 2001 From: Jesse Kuang Date: Mon, 26 Nov 2018 23:49:45 +0800 Subject: [PATCH 5/9] add talib CGO bind test --- talibCgo_test.go | 662 +++++++++++++++++++++++++++++++++++++++++++++++ talib_test.go | 23 +- 2 files changed, 667 insertions(+), 18 deletions(-) create mode 100644 talibCgo_test.go diff --git a/talibCgo_test.go b/talibCgo_test.go new file mode 100644 index 0000000..3da41ba --- /dev/null +++ b/talibCgo_test.go @@ -0,0 +1,662 @@ +/* +Copyright 2016 Mark Chenoweth +Copyright 2018 Alessandro Sanino +Licensed under terms of MIT license (see LICENSE) +*/ + +package talib + +import ( + "testing" + "github.com/kjx98/cgo-talib" +) + +// Test all the functions + +func TestCgoSma(t *testing.T) { + result := Sma(testClose, 20) + cgo_result := talib.Sma(testClose, 20) + equals(t, result, cgo_result) +} + +func TestCgoEma(t *testing.T) { + result := Ema(testClose, 5) + cgo_result := talib.Ema(testClose, 5) + equals(t, result, cgo_result) + result = Ema(testClose, 20) + cgo_result = talib.Ema(testClose, 20) + equals(t, result, cgo_result) + result = Ema(testClose, 50) + cgo_result = talib.Ema(testClose, 50) + equals(t, result, cgo_result) + result = Ema(testClose, 100) + cgo_result = talib.Ema(testClose, 100) + equals(t, result, cgo_result) +} + +func TestCgoRsi(t *testing.T) { + result := Rsi(testClose, 10) + cgo_result := talib.Rsi(testClose, 10) + equals(t, result, cgo_result) +} + +func TestCgoAdd(t *testing.T) { + result := Add(testHigh, testLow) + cgo_result := talib.Add(testHigh, testLow) + equals(t, result, cgo_result) +} + +func TestCgoDiv(t *testing.T) { + result := Div(testHigh, testLow) + cgo_result := talib.Div(testHigh, testLow) + equals(t, result, cgo_result) +} + +func TestCgoMax(t *testing.T) { + result := Max(testClose, 10) + cgo_result := talib.Max(testClose, 10) + equals(t, result, cgo_result) +} + +func TestCgoMaxIndex(t *testing.T) { + result := MaxIndex(testClose, 10) + cgo_result := talib.MaxIndex(testClose, 10) + equals(t, result, cgo_result) +} + +func TestCgoMin(t *testing.T) { + result := Min(testClose, 10) + cgo_result := talib.Min(testClose, 10) + equals(t, result, cgo_result) +} + +func TestCgoMinIndex(t *testing.T) { + result := MinIndex(testClose, 10) + cgo_result := talib.MinIndex(testClose, 10) + equals(t, result, cgo_result) +} + +func TestCgoMult(t *testing.T) { + result := Mult(testHigh, testLow) + cgo_result := talib.Mult(testHigh, testLow) + equals(t, result, cgo_result) +} + +func TestCgoSub(t *testing.T) { + result := Sub(testHigh, testLow) + cgo_result := talib.Sub(testHigh, testLow) + equals(t, result, cgo_result) +} + +func TestCgoRocp(t *testing.T) { + result := Rocp(testClose, 10) + cgo_result := talib.Rocp(testClose, 10) + equals(t, result, cgo_result) +} + +func TestCgoObv(t *testing.T) { + result := Obv(testClose, testVolume) + cgo_result := talib.Obv(testClose, testVolume) + equals(t, result, cgo_result) +} + +func TestCgoAtr(t *testing.T) { + result := Atr(testHigh, testLow, testClose, 14) + cgo_result := talib.Atr(testHigh, testLow, testClose, 14) + equals(t, result, cgo_result) +} + +func TestCgoNatr(t *testing.T) { + result := Natr(testHigh, testLow, testClose, 14) + cgo_result := talib.Natr(testHigh, testLow, testClose, 14) + equals(t, result, cgo_result) +} + +/* +func TestCgoTRange(t *testing.T) { + result := TRange(testHigh, testLow, testClose) + cgo_result := talib.Trange(testHigh, testLow, testClose) + equals(t, result, cgo_result) +} +*/ + +func TestCgoAvgPrice(t *testing.T) { + result := AvgPrice(testOpen, testHigh, testLow, testClose) + cgo_result := talib.AvgPrice(testOpen, testHigh, testLow, testClose) + equals(t, result, cgo_result) +} + +func TestCgoMedPrice(t *testing.T) { + result := MedPrice(testHigh, testLow) + cgo_result := talib.MedPrice(testHigh, testLow) + equals(t, result, cgo_result) +} + +func TestCgoTypPrice(t *testing.T) { + result := TypPrice(testHigh, testLow, testClose) + cgo_result := talib.TypPrice(testHigh, testLow, testClose) + equals(t, result, cgo_result) +} + +func TestCgoWclPrice(t *testing.T) { + result := WclPrice(testHigh, testLow, testClose) + cgo_result := talib.WclPrice(testHigh, testLow, testClose) + equals(t, result, cgo_result) +} + +/* +func TestCgoAcos(t *testing.T) { + result := Acos(testRand) + cgo_result := talib.Acos(testRand) + equals(t, result, cgo_result) +} + +func TestCgoAsin(t *testing.T) { + result := Asin(testRand) + cgo_result := talib.Asin(testRand) + equals(t, result, cgo_result) +} + +func TestCgoAtan(t *testing.T) { + result := Atan(testRand) + cgo_result := talib.Atan(testRand) + equals(t, result, cgo_result) +} +*/ + +func TestCgoCeil(t *testing.T) { + result := Ceil(testClose) + cgo_result := talib.Ceil(testClose) + equals(t, result, cgo_result) +} + +/* +func TestCgoCos(t *testing.T) { + result := Cos(testRand) + cgo_result := talib.Cos(testRand) + equals(t, result, cgo_result) +} + +func TestCgoCosh(t *testing.T) { + result := Cosh(testRand) + cgo_result := talib.Cosh(testRand) + equals(t, result, cgo_result) +} + +func TestCgoExp(t *testing.T) { + result := Exp(testRand) + cgo_result := talib.Exp(testRand) + equals(t, result, cgo_result) +} +*/ + +func TestCgoFloor(t *testing.T) { + result := Floor(testClose) + cgo_result := talib.Floor(testClose) + equals(t, result, cgo_result) +} + +/* +func TestCgoLn(t *testing.T) { + result := Ln(testClose) + cgo_result := talib.Ln(testClose) + equals(t, result, cgo_result) +} + +func TestCgoLog10(t *testing.T) { + result := Log10(testClose) + cgo_result := talib.Log10(testClose) + equals(t, result, cgo_result) +} + +func TestCgoSin(t *testing.T) { + result := Sin(testRand) + cgo_result := talib.Sin(testRand) + equals(t, result, cgo_result) +} + +func TestCgoSinh(t *testing.T) { + result := Sinh(testRand) + cgo_result := talib.Sinh(testRand) + equals(t, result, cgo_result) +} +*/ + +func TestCgoSqrt(t *testing.T) { + result := Sqrt(testClose) + cgo_result := talib.Sqrt(testClose) + equals(t, result, cgo_result) +} + +/* +func TestCgoTan(t *testing.T) { + result := Tan(testRand) + cgo_result := talib.Tan(testRand) + equals(t, result, cgo_result) +} + +func TestCgoTanh(t *testing.T) { + result := Tanh(testRand) + cgo_result := talib.Tanh(testRand) + equals(t, result, cgo_result) +} + +func TestCgoSum(t *testing.T) { + result := Sum(testClose, 10) + cgo_result := talib.Sum(testClose, 10) + equals(t, result, cgo_result) +} +*/ + +func TestCgoVar(t *testing.T) { + result := Var(testClose, 10) + cgo_result := talib.Var(testClose, 10, 1.0) + equals(t, result, cgo_result) +} + +/* +func TestCgoTsf(t *testing.T) { + result := Tsf(testClose, 10) + cgo_result := talib.Tsf(testClose, 10) + equals(t, result, cgo_result) +} +*/ + +func TestCgoStdDev(t *testing.T) { + result := StdDev(testRand, 10, 1.0) + cgo_result := talib.StdDev(testRand, 10, 1.0) + equals(t, result, cgo_result) +} + +/* +func TestCgoLinearRegSlope(t *testing.T) { + result := LinearRegSlope(testClose, 10) + cgo_result := talib.LinearRegSlope(testClose, 10) + equals(t, result, cgo_result) +} + +func TestCgoLinearRegIntercept(t *testing.T) { + result := LinearRegIntercept(testClose, 10) + cgo_result := talib.LinearRegIntercept(testClose, 10) + equals(t, result, cgo_result) +} + +func TestCgoLinearRegAngle(t *testing.T) { + result := LinearRegAngle(testClose, 10) + cgo_result := talib.LinearRegAngle(testClose, 10) + equals(t, result, cgo_result) +} + +func TestCgoLinearReg(t *testing.T) { + result := LinearReg(testClose, 10) + cgo_result := talib.LinearReg(testClose, 10) + equals(t, result, cgo_result) +} +*/ + +func TestCgoCorrel(t *testing.T) { + result := Correl(testHigh, testLow, 10) + cgo_result := talib.Correl(testHigh, testLow, 10) + equals(t, result, cgo_result) +} + +func TestCgoBeta(t *testing.T) { + result := Beta(testHigh, testLow, 5) + cgo_result := talib.Beta(testHigh, testLow, 5) + equals(t, result, cgo_result) +} + +func TestCgoHtDcPeriod(t *testing.T) { + result := HtDcPeriod(testClose) + cgo_result := talib.HtDcPeriod(testClose) + equals(t, result, cgo_result) +} + +func TestCgoHtPhasor(t *testing.T) { + result1, result2 := HtPhasor(testClose) + cgo_result1, cgo_result2 := talib.HtPhasor(testClose) + equals(t, result1, cgo_result1) + equals(t, result2, cgo_result2) +} + +func TestCgoHtSine(t *testing.T) { + result1, result2 := HtSine(testClose) + cgo_result1, cgo_result2 := talib.HtSine(testClose) + equals(t, result1, cgo_result1) + equals(t, result2, cgo_result2) +} + +func TestCgoHtTrendline(t *testing.T) { + result := HtTrendline(testClose) + cgo_result := talib.HtTrendLine(testClose) + equals(t, result, cgo_result) +} + +func TestCgoHtTrendMode(t *testing.T) { + result := HtTrendMode(testClose) + cgo_result := talib.HtTrendMode(testClose) + equals(t, result, cgo_result) +} + +func TestCgoWillR(t *testing.T) { + result := WillR(testHigh, testLow, testClose, 9) + cgo_result := talib.Willr(testHigh, testLow, testClose, 9) + equals(t, result, cgo_result) +} + +func TestCgoAdx(t *testing.T) { + result := Adx(testHigh, testLow, testClose, 14) + cgo_result := talib.Adx(testHigh, testLow, testClose, 14) + equals(t, result, cgo_result) +} + +func TestCgoAdxR(t *testing.T) { + result := AdxR(testHigh, testLow, testClose, 5) + cgo_result := talib.Adxr(testHigh, testLow, testClose, 5) + equals(t, result, cgo_result) +} + +func TestCgoCci(t *testing.T) { + result := Cci(testHigh, testLow, testClose, 14) + cgo_result := talib.Cci(testHigh, testLow, testClose, 14) + equals(t, result, cgo_result) +} + +func TestCgoRoc(t *testing.T) { + result := Roc(testClose, 10) + cgo_result := talib.Roc(testClose, 10) + equals(t, result, cgo_result) +} + +func TestCgoRocr(t *testing.T) { + result := Rocr(testClose, 10) + cgo_result := talib.Rocr(testClose, 10) + equals(t, result, cgo_result) +} + +func TestCgoRocr100(t *testing.T) { + result := Rocr100(testClose, 10) + cgo_result := talib.Rocr100(testClose, 10) + equals(t, result, cgo_result) +} + +func TestCgoMom(t *testing.T) { + result := Mom(testClose, 10) + cgo_result := talib.Mom(testClose, 10) + equals(t, result, cgo_result) +} + +func TestCgoBBands(t *testing.T) { + upper, middle, lower := BBands(testClose, 5, 2.0, 2.0, SMA) + cgo_upper, cgo_middle, cgo_lower := talib.BBands(testClose, 5, 2.0, 2.0, int32(SMA)) + equals(t, upper, cgo_upper) + equals(t, middle, cgo_middle) + equals(t, lower, cgo_lower) +} + +func TestCgoDema(t *testing.T) { + result := Dema(testClose, 10) + cgo_result := talib.Dema(testClose, 10) + equals(t, result, cgo_result) +} + +func TestCgoTema(t *testing.T) { + result := Tema(testClose, 10) + cgo_result := talib.Tema(testClose, 10) + equals(t, result, cgo_result) +} + +func TestCgoWma(t *testing.T) { + result := Wma(testClose, 10) + cgo_result := talib.Wma(testClose, 10) + equals(t, result, cgo_result) +} + +func TestCgoMa(t *testing.T) { + result := Ma(testClose, 10, DEMA) + cgo_result := talib.Ma(testClose, 10, int32(DEMA)) + equals(t, result, cgo_result) +} + +func TestCgoTrima(t *testing.T) { + result := Trima(testClose, 10) + cgo_result := talib.TriMa(testClose, 10) + equals(t, result, cgo_result) + result = Trima(testClose, 11) + cgo_result = talib.TriMa(testClose, 11) + equals(t, result, cgo_result) +} + +func TestCgoMidPoint(t *testing.T) { + result := MidPoint(testClose, 10) + cgo_result := talib.MidPoint(testClose, 10) + equals(t, result, cgo_result) +} + +func TestCgoMidPrice(t *testing.T) { + result := MidPrice(testHigh, testLow, 10) + cgo_result := talib.MidPrice(testHigh, testLow, 10) + equals(t, result, cgo_result) +} + +func TestCgoT3(t *testing.T) { + result := T3(testClose, 5, 0.7) + cgo_result := talib.T3(testClose, 5, 0.7) + equals(t, result, cgo_result) +} + +func TestCgoKama(t *testing.T) { + result := Kama(testClose, 10) + cgo_result := talib.Kama(testClose, 10) + equals(t, result, cgo_result) +} + +func TestCgoMaVp(t *testing.T) { + periods := make([]float64, len(testClose)) + for i := range testClose { + periods[i] = 5.0 + } + result := MaVp(testClose, periods, 2, 10, SMA) + cgo_result := talib.Mavp(testClose, periods, 2, 10, int32(SMA)) + equals(t, result, cgo_result) +} + +func TestCgoMinusDM(t *testing.T) { + result := MinusDM(testHigh, testLow, 14) + cgo_result := talib.MinusDm(testHigh, testLow, 14) + equals(t, result, cgo_result) +} + +func TestCgoPlusDM(t *testing.T) { + result := PlusDM(testHigh, testLow, 14) + cgo_result := talib.PlusDm(testHigh, testLow, 14) + equals(t, result, cgo_result) +} + +func TestCgoSar(t *testing.T) { + result := Sar(testHigh, testLow, 0.0, 0.0) + cgo_result := talib.Sar(testHigh, testLow, 0.0, 0.0) + equals(t, result, cgo_result) +} + +func TestCgoSarExt(t *testing.T) { + result := SarExt(testHigh, testLow, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0) + cgo_result := talib.SarExt(testHigh, testLow, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0) + equals(t, result, cgo_result) +} + +func TestCgoMama(t *testing.T) { + mama, fama := Mama(testClose, 0.5, 0.05) + cgo_mama, cgo_fama := talib.Mama(testClose, 0.5, 0.05) + equals(t, mama, cgo_mama) + equals(t, fama, cgo_fama) +} + +func TestCgoMinMax(t *testing.T) { + min, max := MinMax(testClose, 10) + cgo_min, cgo_max := talib.MinMax(testClose, 10) + equals(t, min, cgo_min) + equals(t, max, cgo_max) +} + +func TestCgoMinMaxIndex(t *testing.T) { + minidx, maxidx := MinMaxIndex(testClose, 10) + cgo_minidx, cgo_maxidx := talib.MinMaxIndex(testClose, 10) + equals(t, minidx, cgo_minidx) + equals(t, maxidx, cgo_maxidx) +} + +func TestCgoApo(t *testing.T) { + result := Apo(testClose, 12, 26, SMA) + cgo_result := talib.Apo(testClose, 12, 26, int32(SMA)) + equals(t, result, cgo_result) + result = Apo(testClose, 26, 12, SMA) + cgo_result = talib.Apo(testClose, 26, 12, int32(SMA)) + equals(t, result, cgo_result) +} + +func TestCgoPpo(t *testing.T) { + result := Ppo(testClose, 12, 26, SMA) + cgo_result := talib.Ppo(testClose, 12, 26, int32(SMA)) + equals(t, result, cgo_result) + result = Ppo(testClose, 26, 12, SMA) + cgo_result = talib.Ppo(testClose, 26, 12, int32(SMA)) + equals(t, result, cgo_result) +} + +func TestCgoAroon(t *testing.T) { + dn, up := Aroon(testHigh, testLow, 14) + cgo_dn, cgo_up := talib.AroOn(testHigh, testLow, 14) + equals(t, dn, cgo_dn) + equals(t, up, cgo_up) +} + +func TestCgoAroonOsc(t *testing.T) { + result := AroonOsc(testHigh, testLow, 14) + cgo_result := talib.AroOnOsc(testHigh, testLow, 14) + equals(t, result, cgo_result) +} + +func TestCgoBop(t *testing.T) { + result := Bop(testOpen, testHigh, testLow, testClose) + cgo_result := talib.Bop(testOpen, testHigh, testLow, testClose) + equals(t, result, cgo_result) +} + +func TestCgoCmo(t *testing.T) { + result := Cmo(testClose, 14) + cgo_result := talib.Cmo(testClose, 14) + equals(t, result, cgo_result) +} + +func TestCgoDx(t *testing.T) { + result := Dx(testHigh, testLow, testClose, 14) + cgo_result := talib.Dx(testHigh, testLow, testClose, 14) + equals(t, result, cgo_result) +} + +func TestCgoMinusDI(t *testing.T) { + result := MinusDI(testHigh, testLow, testClose, 14) + cgo_result := talib.MinusDi(testHigh, testLow, testClose, 14) + equals(t, result, cgo_result) +} + +func TestCgoPlusDI(t *testing.T) { + result := PlusDI(testHigh, testLow, testClose, 14) + cgo_result := talib.PlusDi(testHigh, testLow, testClose, 14) + equals(t, result, cgo_result) +} + +func TestCgoMfi(t *testing.T) { + result := Mfi(testHigh, testLow, testClose, testVolume, 14) + cgo_result := talib.Mfi(testHigh, testLow, testClose, testVolume, 14) + equals(t, result, cgo_result) +} + +func TestCgoUltOsc(t *testing.T) { + result := UltOsc(testHigh, testLow, testClose, 7, 14, 28) + cgo_result := talib.UltOsc(testHigh, testLow, testClose, 7, 14, 28) + equals(t, result, cgo_result) +} + +func TestCgoStoch(t *testing.T) { + slowk, slowd := Stoch(testHigh, testLow, testClose, 5, 3, SMA, 3, SMA) + cgo_slowk, cgo_slowd := talib.Stoch(testHigh, testLow, testClose, 5, 3, int32(SMA), 3, int32(SMA)) + equals(t, slowk, cgo_slowk) + equals(t, slowd, cgo_slowd) +} + +func TestCgoStoch2(t *testing.T) { + slowk, slowd := Stoch(testHigh, testLow, testClose, 12, 3, SMA, 3, SMA) + cgo_slowk, cgo_slowd := talib.Stoch(testHigh, testLow, testClose, 12, 3, int32(SMA), 3, int32(SMA)) + equals(t, slowk, cgo_slowk) + equals(t, slowd, cgo_slowd) +} + +func TestCgoStoch3(t *testing.T) { + slowk, slowd := Stoch(testHigh, testLow, testClose, 12, 3, SMA, 15, SMA) + cgo_slowk, cgo_slowd := talib.Stoch(testHigh, testLow, testClose, 12, 3, int32(SMA), 15, int32(SMA)) + equals(t, slowk, cgo_slowk) + equals(t, slowd, cgo_slowd) +} + +func TestCgoStochF(t *testing.T) { + fastk, fastd := StochF(testHigh, testLow, testClose, 5, 3, SMA) + cgo_fastk, cgo_fastd := talib.Stochf(testHigh, testLow, testClose, 5, 3, int32(SMA)) + equals(t, fastk, cgo_fastk) + equals(t, fastd, cgo_fastd) +} + +func TestCgoStochRsi(t *testing.T) { + fastk, fastd := StochRsi(testClose, 14, 5, 2, SMA) + cgo_fastk, cgo_fastd := talib.StochRsi(testClose, 14, 5, 2, int32(SMA)) + equals(t, fastk, cgo_fastk) + equals(t, fastd, cgo_fastd) +} + +func TestCgoMacdExt(t *testing.T) { + macd, macdsignal, macdhist := MacdExt(testClose, 12, SMA, 26, SMA, 9, SMA) + cgo_macd, cgo_macdsignal, cgo_macdhist := talib.MacdExt(testClose, 12, int32(SMA), 26, int32(SMA), 9, int32(SMA)) + equals(t, macd, cgo_macd) + equals(t, macdsignal, cgo_macdsignal) + equals(t, macdhist, cgo_macdhist) +} + +func TestCgoTrix(t *testing.T) { + result := Trix(testClose, 5) + cgo_result := talib.Trix(testClose, 5) + equals(t, result, cgo_result) + result = Trix(testClose, 30) + cgo_result = talib.Trix(testClose, 30) + equals(t, result, cgo_result) +} + +func TestCgoMacd(t *testing.T) { + macd, macdsignal, macdhist := Macd(testClose, 12, 26, 9) + cgo_macd, cgo_macdsignal, cgo_macdhist := talib.Macd(testClose, 12, 26, 9) + //unstable := 100 + equals(t, macd, cgo_macd) + equals(t, macdsignal, cgo_macdsignal) + equals(t, macdhist, cgo_macdhist) +} + +func TestCgoMacdFix(t *testing.T) { + macd, macdsignal, macdhist := MacdFix(testClose, 9) + cgo_macd, cgo_macdsignal, cgo_macdhist := talib.MacdFix(testClose, 9) + //unstable := 100 + equals(t, macd, cgo_macd) + equals(t, macdsignal, cgo_macdsignal) + equals(t, macdhist, cgo_macdhist) +} + +func TestCgoAd(t *testing.T) { + result := Ad(testHigh, testLow, testClose, testVolume) + cgo_result := talib.Ad(testHigh, testLow, testClose, testVolume) + equals(t, result, cgo_result) +} + +func TestCgoAdOsc(t *testing.T) { + result := AdOsc(testHigh, testLow, testClose, testVolume, 3, 10) + cgo_result := talib.AdOsc(testHigh, testLow, testClose, testVolume, 3, 10) + equals(t, result, cgo_result) +} diff --git a/talib_test.go b/talib_test.go index c882dac..b02eb91 100644 --- a/talib_test.go +++ b/talib_test.go @@ -18,8 +18,6 @@ import ( "strings" "testing" - "github.com/kjx98/cgo-talib" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -161,9 +159,7 @@ func TestRsi(t *testing.T) { func TestAdd(t *testing.T) { result := Add(testHigh, testLow) - cgo_result := talib.Add(testHigh, testLow) - equals(t, result, cgo_result) - //compare(t, result, "result = talib.ADD(testHigh,testLow)") + compare(t, result, "result = talib.ADD(testHigh,testLow)") } func TestDiv(t *testing.T) { @@ -405,16 +401,12 @@ func TestWillR(t *testing.T) { func TestAdx(t *testing.T) { result := Adx(testHigh, testLow, testClose, 14) - cgo_result := talib.Adx(testHigh, testLow, testClose, 14) - equals(t, result, cgo_result) - //compare(t, result, "result = talib.ADX(testHigh,testLow,testClose,14)") + compare(t, result, "result = talib.ADX(testHigh,testLow,testClose,14)") } func TestAdxR(t *testing.T) { result := AdxR(testHigh, testLow, testClose, 5) - cgo_result := talib.Adxr(testHigh, testLow, testClose, 5) - equals(t, result, cgo_result) - //compare(t, result, "result = talib.ADXR(testHigh,testLow,testClose,5)") + compare(t, result, "result = talib.ADXR(testHigh,testLow,testClose,5)") } func TestCci(t *testing.T) { @@ -665,17 +657,12 @@ func TestMacdFix(t *testing.T) { func TestAd(t *testing.T) { result := Ad(testHigh, testLow, testClose, testVolume) - //compare(t, result, "result = talib.AD(testHigh,testLow,testClose,testVolume)") - cgo_result := talib.Ad(testHigh, testLow, testClose, testVolume) - equals(t, result, cgo_result) + compare(t, result, "result = talib.AD(testHigh,testLow,testClose,testVolume)") } func TestAdOsc(t *testing.T) { result := AdOsc(testHigh, testLow, testClose, testVolume, 3, 10) - cgo_result := talib.AdOsc(testHigh, testLow, testClose, testVolume, 3, 10) - //compare(t, result, "result = talib.ADOSC(testHigh,testLow,testClose,testVolume,3,10)") - //compare_cgo(t, result, cgo_result) - equals(t, result, cgo_result) + compare(t, result, "result = talib.ADOSC(testHigh,testLow,testClose,testVolume,3,10)") } func TestHeikinashiCandles(t *testing.T) { From ea13d9edede803bcbc30fec413a87a8b31292aef Mon Sep 17 00:00:00 2001 From: Jesse Kuang Date: Tue, 27 Nov 2018 17:08:04 +0800 Subject: [PATCH 6/9] cgo compare move to cgo-talib --- talibCgo_test.go | 662 ----------------------------------------------- 1 file changed, 662 deletions(-) delete mode 100644 talibCgo_test.go diff --git a/talibCgo_test.go b/talibCgo_test.go deleted file mode 100644 index 3da41ba..0000000 --- a/talibCgo_test.go +++ /dev/null @@ -1,662 +0,0 @@ -/* -Copyright 2016 Mark Chenoweth -Copyright 2018 Alessandro Sanino -Licensed under terms of MIT license (see LICENSE) -*/ - -package talib - -import ( - "testing" - "github.com/kjx98/cgo-talib" -) - -// Test all the functions - -func TestCgoSma(t *testing.T) { - result := Sma(testClose, 20) - cgo_result := talib.Sma(testClose, 20) - equals(t, result, cgo_result) -} - -func TestCgoEma(t *testing.T) { - result := Ema(testClose, 5) - cgo_result := talib.Ema(testClose, 5) - equals(t, result, cgo_result) - result = Ema(testClose, 20) - cgo_result = talib.Ema(testClose, 20) - equals(t, result, cgo_result) - result = Ema(testClose, 50) - cgo_result = talib.Ema(testClose, 50) - equals(t, result, cgo_result) - result = Ema(testClose, 100) - cgo_result = talib.Ema(testClose, 100) - equals(t, result, cgo_result) -} - -func TestCgoRsi(t *testing.T) { - result := Rsi(testClose, 10) - cgo_result := talib.Rsi(testClose, 10) - equals(t, result, cgo_result) -} - -func TestCgoAdd(t *testing.T) { - result := Add(testHigh, testLow) - cgo_result := talib.Add(testHigh, testLow) - equals(t, result, cgo_result) -} - -func TestCgoDiv(t *testing.T) { - result := Div(testHigh, testLow) - cgo_result := talib.Div(testHigh, testLow) - equals(t, result, cgo_result) -} - -func TestCgoMax(t *testing.T) { - result := Max(testClose, 10) - cgo_result := talib.Max(testClose, 10) - equals(t, result, cgo_result) -} - -func TestCgoMaxIndex(t *testing.T) { - result := MaxIndex(testClose, 10) - cgo_result := talib.MaxIndex(testClose, 10) - equals(t, result, cgo_result) -} - -func TestCgoMin(t *testing.T) { - result := Min(testClose, 10) - cgo_result := talib.Min(testClose, 10) - equals(t, result, cgo_result) -} - -func TestCgoMinIndex(t *testing.T) { - result := MinIndex(testClose, 10) - cgo_result := talib.MinIndex(testClose, 10) - equals(t, result, cgo_result) -} - -func TestCgoMult(t *testing.T) { - result := Mult(testHigh, testLow) - cgo_result := talib.Mult(testHigh, testLow) - equals(t, result, cgo_result) -} - -func TestCgoSub(t *testing.T) { - result := Sub(testHigh, testLow) - cgo_result := talib.Sub(testHigh, testLow) - equals(t, result, cgo_result) -} - -func TestCgoRocp(t *testing.T) { - result := Rocp(testClose, 10) - cgo_result := talib.Rocp(testClose, 10) - equals(t, result, cgo_result) -} - -func TestCgoObv(t *testing.T) { - result := Obv(testClose, testVolume) - cgo_result := talib.Obv(testClose, testVolume) - equals(t, result, cgo_result) -} - -func TestCgoAtr(t *testing.T) { - result := Atr(testHigh, testLow, testClose, 14) - cgo_result := talib.Atr(testHigh, testLow, testClose, 14) - equals(t, result, cgo_result) -} - -func TestCgoNatr(t *testing.T) { - result := Natr(testHigh, testLow, testClose, 14) - cgo_result := talib.Natr(testHigh, testLow, testClose, 14) - equals(t, result, cgo_result) -} - -/* -func TestCgoTRange(t *testing.T) { - result := TRange(testHigh, testLow, testClose) - cgo_result := talib.Trange(testHigh, testLow, testClose) - equals(t, result, cgo_result) -} -*/ - -func TestCgoAvgPrice(t *testing.T) { - result := AvgPrice(testOpen, testHigh, testLow, testClose) - cgo_result := talib.AvgPrice(testOpen, testHigh, testLow, testClose) - equals(t, result, cgo_result) -} - -func TestCgoMedPrice(t *testing.T) { - result := MedPrice(testHigh, testLow) - cgo_result := talib.MedPrice(testHigh, testLow) - equals(t, result, cgo_result) -} - -func TestCgoTypPrice(t *testing.T) { - result := TypPrice(testHigh, testLow, testClose) - cgo_result := talib.TypPrice(testHigh, testLow, testClose) - equals(t, result, cgo_result) -} - -func TestCgoWclPrice(t *testing.T) { - result := WclPrice(testHigh, testLow, testClose) - cgo_result := talib.WclPrice(testHigh, testLow, testClose) - equals(t, result, cgo_result) -} - -/* -func TestCgoAcos(t *testing.T) { - result := Acos(testRand) - cgo_result := talib.Acos(testRand) - equals(t, result, cgo_result) -} - -func TestCgoAsin(t *testing.T) { - result := Asin(testRand) - cgo_result := talib.Asin(testRand) - equals(t, result, cgo_result) -} - -func TestCgoAtan(t *testing.T) { - result := Atan(testRand) - cgo_result := talib.Atan(testRand) - equals(t, result, cgo_result) -} -*/ - -func TestCgoCeil(t *testing.T) { - result := Ceil(testClose) - cgo_result := talib.Ceil(testClose) - equals(t, result, cgo_result) -} - -/* -func TestCgoCos(t *testing.T) { - result := Cos(testRand) - cgo_result := talib.Cos(testRand) - equals(t, result, cgo_result) -} - -func TestCgoCosh(t *testing.T) { - result := Cosh(testRand) - cgo_result := talib.Cosh(testRand) - equals(t, result, cgo_result) -} - -func TestCgoExp(t *testing.T) { - result := Exp(testRand) - cgo_result := talib.Exp(testRand) - equals(t, result, cgo_result) -} -*/ - -func TestCgoFloor(t *testing.T) { - result := Floor(testClose) - cgo_result := talib.Floor(testClose) - equals(t, result, cgo_result) -} - -/* -func TestCgoLn(t *testing.T) { - result := Ln(testClose) - cgo_result := talib.Ln(testClose) - equals(t, result, cgo_result) -} - -func TestCgoLog10(t *testing.T) { - result := Log10(testClose) - cgo_result := talib.Log10(testClose) - equals(t, result, cgo_result) -} - -func TestCgoSin(t *testing.T) { - result := Sin(testRand) - cgo_result := talib.Sin(testRand) - equals(t, result, cgo_result) -} - -func TestCgoSinh(t *testing.T) { - result := Sinh(testRand) - cgo_result := talib.Sinh(testRand) - equals(t, result, cgo_result) -} -*/ - -func TestCgoSqrt(t *testing.T) { - result := Sqrt(testClose) - cgo_result := talib.Sqrt(testClose) - equals(t, result, cgo_result) -} - -/* -func TestCgoTan(t *testing.T) { - result := Tan(testRand) - cgo_result := talib.Tan(testRand) - equals(t, result, cgo_result) -} - -func TestCgoTanh(t *testing.T) { - result := Tanh(testRand) - cgo_result := talib.Tanh(testRand) - equals(t, result, cgo_result) -} - -func TestCgoSum(t *testing.T) { - result := Sum(testClose, 10) - cgo_result := talib.Sum(testClose, 10) - equals(t, result, cgo_result) -} -*/ - -func TestCgoVar(t *testing.T) { - result := Var(testClose, 10) - cgo_result := talib.Var(testClose, 10, 1.0) - equals(t, result, cgo_result) -} - -/* -func TestCgoTsf(t *testing.T) { - result := Tsf(testClose, 10) - cgo_result := talib.Tsf(testClose, 10) - equals(t, result, cgo_result) -} -*/ - -func TestCgoStdDev(t *testing.T) { - result := StdDev(testRand, 10, 1.0) - cgo_result := talib.StdDev(testRand, 10, 1.0) - equals(t, result, cgo_result) -} - -/* -func TestCgoLinearRegSlope(t *testing.T) { - result := LinearRegSlope(testClose, 10) - cgo_result := talib.LinearRegSlope(testClose, 10) - equals(t, result, cgo_result) -} - -func TestCgoLinearRegIntercept(t *testing.T) { - result := LinearRegIntercept(testClose, 10) - cgo_result := talib.LinearRegIntercept(testClose, 10) - equals(t, result, cgo_result) -} - -func TestCgoLinearRegAngle(t *testing.T) { - result := LinearRegAngle(testClose, 10) - cgo_result := talib.LinearRegAngle(testClose, 10) - equals(t, result, cgo_result) -} - -func TestCgoLinearReg(t *testing.T) { - result := LinearReg(testClose, 10) - cgo_result := talib.LinearReg(testClose, 10) - equals(t, result, cgo_result) -} -*/ - -func TestCgoCorrel(t *testing.T) { - result := Correl(testHigh, testLow, 10) - cgo_result := talib.Correl(testHigh, testLow, 10) - equals(t, result, cgo_result) -} - -func TestCgoBeta(t *testing.T) { - result := Beta(testHigh, testLow, 5) - cgo_result := talib.Beta(testHigh, testLow, 5) - equals(t, result, cgo_result) -} - -func TestCgoHtDcPeriod(t *testing.T) { - result := HtDcPeriod(testClose) - cgo_result := talib.HtDcPeriod(testClose) - equals(t, result, cgo_result) -} - -func TestCgoHtPhasor(t *testing.T) { - result1, result2 := HtPhasor(testClose) - cgo_result1, cgo_result2 := talib.HtPhasor(testClose) - equals(t, result1, cgo_result1) - equals(t, result2, cgo_result2) -} - -func TestCgoHtSine(t *testing.T) { - result1, result2 := HtSine(testClose) - cgo_result1, cgo_result2 := talib.HtSine(testClose) - equals(t, result1, cgo_result1) - equals(t, result2, cgo_result2) -} - -func TestCgoHtTrendline(t *testing.T) { - result := HtTrendline(testClose) - cgo_result := talib.HtTrendLine(testClose) - equals(t, result, cgo_result) -} - -func TestCgoHtTrendMode(t *testing.T) { - result := HtTrendMode(testClose) - cgo_result := talib.HtTrendMode(testClose) - equals(t, result, cgo_result) -} - -func TestCgoWillR(t *testing.T) { - result := WillR(testHigh, testLow, testClose, 9) - cgo_result := talib.Willr(testHigh, testLow, testClose, 9) - equals(t, result, cgo_result) -} - -func TestCgoAdx(t *testing.T) { - result := Adx(testHigh, testLow, testClose, 14) - cgo_result := talib.Adx(testHigh, testLow, testClose, 14) - equals(t, result, cgo_result) -} - -func TestCgoAdxR(t *testing.T) { - result := AdxR(testHigh, testLow, testClose, 5) - cgo_result := talib.Adxr(testHigh, testLow, testClose, 5) - equals(t, result, cgo_result) -} - -func TestCgoCci(t *testing.T) { - result := Cci(testHigh, testLow, testClose, 14) - cgo_result := talib.Cci(testHigh, testLow, testClose, 14) - equals(t, result, cgo_result) -} - -func TestCgoRoc(t *testing.T) { - result := Roc(testClose, 10) - cgo_result := talib.Roc(testClose, 10) - equals(t, result, cgo_result) -} - -func TestCgoRocr(t *testing.T) { - result := Rocr(testClose, 10) - cgo_result := talib.Rocr(testClose, 10) - equals(t, result, cgo_result) -} - -func TestCgoRocr100(t *testing.T) { - result := Rocr100(testClose, 10) - cgo_result := talib.Rocr100(testClose, 10) - equals(t, result, cgo_result) -} - -func TestCgoMom(t *testing.T) { - result := Mom(testClose, 10) - cgo_result := talib.Mom(testClose, 10) - equals(t, result, cgo_result) -} - -func TestCgoBBands(t *testing.T) { - upper, middle, lower := BBands(testClose, 5, 2.0, 2.0, SMA) - cgo_upper, cgo_middle, cgo_lower := talib.BBands(testClose, 5, 2.0, 2.0, int32(SMA)) - equals(t, upper, cgo_upper) - equals(t, middle, cgo_middle) - equals(t, lower, cgo_lower) -} - -func TestCgoDema(t *testing.T) { - result := Dema(testClose, 10) - cgo_result := talib.Dema(testClose, 10) - equals(t, result, cgo_result) -} - -func TestCgoTema(t *testing.T) { - result := Tema(testClose, 10) - cgo_result := talib.Tema(testClose, 10) - equals(t, result, cgo_result) -} - -func TestCgoWma(t *testing.T) { - result := Wma(testClose, 10) - cgo_result := talib.Wma(testClose, 10) - equals(t, result, cgo_result) -} - -func TestCgoMa(t *testing.T) { - result := Ma(testClose, 10, DEMA) - cgo_result := talib.Ma(testClose, 10, int32(DEMA)) - equals(t, result, cgo_result) -} - -func TestCgoTrima(t *testing.T) { - result := Trima(testClose, 10) - cgo_result := talib.TriMa(testClose, 10) - equals(t, result, cgo_result) - result = Trima(testClose, 11) - cgo_result = talib.TriMa(testClose, 11) - equals(t, result, cgo_result) -} - -func TestCgoMidPoint(t *testing.T) { - result := MidPoint(testClose, 10) - cgo_result := talib.MidPoint(testClose, 10) - equals(t, result, cgo_result) -} - -func TestCgoMidPrice(t *testing.T) { - result := MidPrice(testHigh, testLow, 10) - cgo_result := talib.MidPrice(testHigh, testLow, 10) - equals(t, result, cgo_result) -} - -func TestCgoT3(t *testing.T) { - result := T3(testClose, 5, 0.7) - cgo_result := talib.T3(testClose, 5, 0.7) - equals(t, result, cgo_result) -} - -func TestCgoKama(t *testing.T) { - result := Kama(testClose, 10) - cgo_result := talib.Kama(testClose, 10) - equals(t, result, cgo_result) -} - -func TestCgoMaVp(t *testing.T) { - periods := make([]float64, len(testClose)) - for i := range testClose { - periods[i] = 5.0 - } - result := MaVp(testClose, periods, 2, 10, SMA) - cgo_result := talib.Mavp(testClose, periods, 2, 10, int32(SMA)) - equals(t, result, cgo_result) -} - -func TestCgoMinusDM(t *testing.T) { - result := MinusDM(testHigh, testLow, 14) - cgo_result := talib.MinusDm(testHigh, testLow, 14) - equals(t, result, cgo_result) -} - -func TestCgoPlusDM(t *testing.T) { - result := PlusDM(testHigh, testLow, 14) - cgo_result := talib.PlusDm(testHigh, testLow, 14) - equals(t, result, cgo_result) -} - -func TestCgoSar(t *testing.T) { - result := Sar(testHigh, testLow, 0.0, 0.0) - cgo_result := talib.Sar(testHigh, testLow, 0.0, 0.0) - equals(t, result, cgo_result) -} - -func TestCgoSarExt(t *testing.T) { - result := SarExt(testHigh, testLow, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0) - cgo_result := talib.SarExt(testHigh, testLow, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0) - equals(t, result, cgo_result) -} - -func TestCgoMama(t *testing.T) { - mama, fama := Mama(testClose, 0.5, 0.05) - cgo_mama, cgo_fama := talib.Mama(testClose, 0.5, 0.05) - equals(t, mama, cgo_mama) - equals(t, fama, cgo_fama) -} - -func TestCgoMinMax(t *testing.T) { - min, max := MinMax(testClose, 10) - cgo_min, cgo_max := talib.MinMax(testClose, 10) - equals(t, min, cgo_min) - equals(t, max, cgo_max) -} - -func TestCgoMinMaxIndex(t *testing.T) { - minidx, maxidx := MinMaxIndex(testClose, 10) - cgo_minidx, cgo_maxidx := talib.MinMaxIndex(testClose, 10) - equals(t, minidx, cgo_minidx) - equals(t, maxidx, cgo_maxidx) -} - -func TestCgoApo(t *testing.T) { - result := Apo(testClose, 12, 26, SMA) - cgo_result := talib.Apo(testClose, 12, 26, int32(SMA)) - equals(t, result, cgo_result) - result = Apo(testClose, 26, 12, SMA) - cgo_result = talib.Apo(testClose, 26, 12, int32(SMA)) - equals(t, result, cgo_result) -} - -func TestCgoPpo(t *testing.T) { - result := Ppo(testClose, 12, 26, SMA) - cgo_result := talib.Ppo(testClose, 12, 26, int32(SMA)) - equals(t, result, cgo_result) - result = Ppo(testClose, 26, 12, SMA) - cgo_result = talib.Ppo(testClose, 26, 12, int32(SMA)) - equals(t, result, cgo_result) -} - -func TestCgoAroon(t *testing.T) { - dn, up := Aroon(testHigh, testLow, 14) - cgo_dn, cgo_up := talib.AroOn(testHigh, testLow, 14) - equals(t, dn, cgo_dn) - equals(t, up, cgo_up) -} - -func TestCgoAroonOsc(t *testing.T) { - result := AroonOsc(testHigh, testLow, 14) - cgo_result := talib.AroOnOsc(testHigh, testLow, 14) - equals(t, result, cgo_result) -} - -func TestCgoBop(t *testing.T) { - result := Bop(testOpen, testHigh, testLow, testClose) - cgo_result := talib.Bop(testOpen, testHigh, testLow, testClose) - equals(t, result, cgo_result) -} - -func TestCgoCmo(t *testing.T) { - result := Cmo(testClose, 14) - cgo_result := talib.Cmo(testClose, 14) - equals(t, result, cgo_result) -} - -func TestCgoDx(t *testing.T) { - result := Dx(testHigh, testLow, testClose, 14) - cgo_result := talib.Dx(testHigh, testLow, testClose, 14) - equals(t, result, cgo_result) -} - -func TestCgoMinusDI(t *testing.T) { - result := MinusDI(testHigh, testLow, testClose, 14) - cgo_result := talib.MinusDi(testHigh, testLow, testClose, 14) - equals(t, result, cgo_result) -} - -func TestCgoPlusDI(t *testing.T) { - result := PlusDI(testHigh, testLow, testClose, 14) - cgo_result := talib.PlusDi(testHigh, testLow, testClose, 14) - equals(t, result, cgo_result) -} - -func TestCgoMfi(t *testing.T) { - result := Mfi(testHigh, testLow, testClose, testVolume, 14) - cgo_result := talib.Mfi(testHigh, testLow, testClose, testVolume, 14) - equals(t, result, cgo_result) -} - -func TestCgoUltOsc(t *testing.T) { - result := UltOsc(testHigh, testLow, testClose, 7, 14, 28) - cgo_result := talib.UltOsc(testHigh, testLow, testClose, 7, 14, 28) - equals(t, result, cgo_result) -} - -func TestCgoStoch(t *testing.T) { - slowk, slowd := Stoch(testHigh, testLow, testClose, 5, 3, SMA, 3, SMA) - cgo_slowk, cgo_slowd := talib.Stoch(testHigh, testLow, testClose, 5, 3, int32(SMA), 3, int32(SMA)) - equals(t, slowk, cgo_slowk) - equals(t, slowd, cgo_slowd) -} - -func TestCgoStoch2(t *testing.T) { - slowk, slowd := Stoch(testHigh, testLow, testClose, 12, 3, SMA, 3, SMA) - cgo_slowk, cgo_slowd := talib.Stoch(testHigh, testLow, testClose, 12, 3, int32(SMA), 3, int32(SMA)) - equals(t, slowk, cgo_slowk) - equals(t, slowd, cgo_slowd) -} - -func TestCgoStoch3(t *testing.T) { - slowk, slowd := Stoch(testHigh, testLow, testClose, 12, 3, SMA, 15, SMA) - cgo_slowk, cgo_slowd := talib.Stoch(testHigh, testLow, testClose, 12, 3, int32(SMA), 15, int32(SMA)) - equals(t, slowk, cgo_slowk) - equals(t, slowd, cgo_slowd) -} - -func TestCgoStochF(t *testing.T) { - fastk, fastd := StochF(testHigh, testLow, testClose, 5, 3, SMA) - cgo_fastk, cgo_fastd := talib.Stochf(testHigh, testLow, testClose, 5, 3, int32(SMA)) - equals(t, fastk, cgo_fastk) - equals(t, fastd, cgo_fastd) -} - -func TestCgoStochRsi(t *testing.T) { - fastk, fastd := StochRsi(testClose, 14, 5, 2, SMA) - cgo_fastk, cgo_fastd := talib.StochRsi(testClose, 14, 5, 2, int32(SMA)) - equals(t, fastk, cgo_fastk) - equals(t, fastd, cgo_fastd) -} - -func TestCgoMacdExt(t *testing.T) { - macd, macdsignal, macdhist := MacdExt(testClose, 12, SMA, 26, SMA, 9, SMA) - cgo_macd, cgo_macdsignal, cgo_macdhist := talib.MacdExt(testClose, 12, int32(SMA), 26, int32(SMA), 9, int32(SMA)) - equals(t, macd, cgo_macd) - equals(t, macdsignal, cgo_macdsignal) - equals(t, macdhist, cgo_macdhist) -} - -func TestCgoTrix(t *testing.T) { - result := Trix(testClose, 5) - cgo_result := talib.Trix(testClose, 5) - equals(t, result, cgo_result) - result = Trix(testClose, 30) - cgo_result = talib.Trix(testClose, 30) - equals(t, result, cgo_result) -} - -func TestCgoMacd(t *testing.T) { - macd, macdsignal, macdhist := Macd(testClose, 12, 26, 9) - cgo_macd, cgo_macdsignal, cgo_macdhist := talib.Macd(testClose, 12, 26, 9) - //unstable := 100 - equals(t, macd, cgo_macd) - equals(t, macdsignal, cgo_macdsignal) - equals(t, macdhist, cgo_macdhist) -} - -func TestCgoMacdFix(t *testing.T) { - macd, macdsignal, macdhist := MacdFix(testClose, 9) - cgo_macd, cgo_macdsignal, cgo_macdhist := talib.MacdFix(testClose, 9) - //unstable := 100 - equals(t, macd, cgo_macd) - equals(t, macdsignal, cgo_macdsignal) - equals(t, macdhist, cgo_macdhist) -} - -func TestCgoAd(t *testing.T) { - result := Ad(testHigh, testLow, testClose, testVolume) - cgo_result := talib.Ad(testHigh, testLow, testClose, testVolume) - equals(t, result, cgo_result) -} - -func TestCgoAdOsc(t *testing.T) { - result := AdOsc(testHigh, testLow, testClose, testVolume, 3, 10) - cgo_result := talib.AdOsc(testHigh, testLow, testClose, testVolume, 3, 10) - equals(t, result, cgo_result) -} From 4a62c9bb3aaa063322e402fb7db119d9d84e7bd6 Mon Sep 17 00:00:00 2001 From: Jesse Kuang Date: Tue, 27 Nov 2018 23:20:45 +0800 Subject: [PATCH 7/9] add series --- series.go | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 series.go diff --git a/series.go b/series.go new file mode 100644 index 0000000..8319ec6 --- /dev/null +++ b/series.go @@ -0,0 +1,54 @@ +package talib + +import ( + "time" +) + +type Series interface { + Len() int + Data(i int) float64 + VData() []float64 +} + +type TimeSeries interface { + Len() int + Data(time.Time) float64 + Index(time.Time) int +} + +type TaSeries interface { + Len() int + High() []float64 + Open() []float64 + Close() []float64 + Low() []float64 + Volume() []float64 +} + +type SimpleSeries struct { + Highs []float64 + Opens []float64 + Closes []float64 + Lows []float64 + Volumes []float64 +} + +func (s SimpleSeries) Len() int { + return len(s.Highs) +} + +func (s SimpleSeries) High() []float64 { + return s.Highs +} + +func (s SimpleSeries) Open() []float64 { + return s.Opens +} + +func (s SimpleSeries) Close() []float64 { + return s.Closes +} + +func (s SimpleSeries) Low() []float64 { + return s.Lows +} From 6e5c7ff2229066330b51394c525884ca16f76e40 Mon Sep 17 00:00:00 2001 From: Jesse Kuang Date: Wed, 28 Nov 2018 17:06:37 +0800 Subject: [PATCH 8/9] mod Series, add TaSeries --- series.go | 106 ++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 79 insertions(+), 27 deletions(-) diff --git a/series.go b/series.go index 8319ec6..4b5f8f6 100644 --- a/series.go +++ b/series.go @@ -4,51 +4,103 @@ import ( "time" ) +// Series data with int index +// Len return length of Series +// DataAt return Data at index i +// if i >= 0, data at index +// else data at index reverse order, -1 for last data type Series interface { - Len() int - Data(i int) float64 - VData() []float64 + Len() int + DataAt(i int) float64 } +// TimeSeries time as index type TimeSeries interface { - Len() int - Data(time.Time) float64 - Index(time.Time) int + Len() int + DataAt(time.Time) float64 + Index(time.Time) int } +// TaSeries TA OHLCV type TaSeries interface { Len() int - High() []float64 - Open() []float64 - Close() []float64 - Low() []float64 - Volume() []float64 + Time(i int) time.Time + Open(i int) float64 + High(i int) float64 + Low(i int) float64 + Close(i int) float64 + Volume(i int) float64 } -type SimpleSeries struct { - Highs []float64 - Opens []float64 - Closes []float64 - Lows []float64 - Volumes []float64 +func NewSlice(s Series) (res []float64) { + if s.Len() <= 0 { + return + } + ll := s.Len() + res = make([]float64, ll) + for i := 0; i < ll; i++ { + res[i] = s.DataAt(i) + } + return } -func (s SimpleSeries) Len() int { - return len(s.Highs) +func Opens(s TaSeries) (res []float64) { + if s.Len() <= 0 { + return + } + ll := s.Len() + res = make([]float64, ll) + for i := 0; i < ll; i++ { + res[i] = s.Open(i) + } + return } -func (s SimpleSeries) High() []float64 { - return s.Highs + +func Highs(s TaSeries) (res []float64) { + if s.Len() <= 0 { + return + } + ll := s.Len() + res = make([]float64, ll) + for i := 0; i < ll; i++ { + res[i] = s.High(i) + } + return } -func (s SimpleSeries) Open() []float64 { - return s.Opens +func Lows(s TaSeries) (res []float64) { + if s.Len() <= 0 { + return + } + ll := s.Len() + res = make([]float64, ll) + for i := 0; i < ll; i++ { + res[i] = s.Low(i) + } + return } -func (s SimpleSeries) Close() []float64 { - return s.Closes +func Closes(s TaSeries) (res []float64) { + if s.Len() <= 0 { + return + } + ll := s.Len() + res = make([]float64, ll) + for i := 0; i < ll; i++ { + res[i] = s.Close(i) + } + return } -func (s SimpleSeries) Low() []float64 { - return s.Lows +func Volumes(s TaSeries) (res []float64) { + if s.Len() <= 0 { + return + } + ll := s.Len() + res = make([]float64, ll) + for i := 0; i < ll; i++ { + res[i] = s.Volume(i) + } + return } From ca5d5c5caa30fa453fd9939a174c5050ebcf91cb Mon Sep 17 00:00:00 2001 From: Jesse Kuang Date: Thu, 20 Dec 2018 15:48:12 +0800 Subject: [PATCH 9/9] remove series --- series.go | 106 ------------------------------------------------------ 1 file changed, 106 deletions(-) delete mode 100644 series.go diff --git a/series.go b/series.go deleted file mode 100644 index 4b5f8f6..0000000 --- a/series.go +++ /dev/null @@ -1,106 +0,0 @@ -package talib - -import ( - "time" -) - -// Series data with int index -// Len return length of Series -// DataAt return Data at index i -// if i >= 0, data at index -// else data at index reverse order, -1 for last data -type Series interface { - Len() int - DataAt(i int) float64 -} - -// TimeSeries time as index -type TimeSeries interface { - Len() int - DataAt(time.Time) float64 - Index(time.Time) int -} - -// TaSeries TA OHLCV -type TaSeries interface { - Len() int - Time(i int) time.Time - Open(i int) float64 - High(i int) float64 - Low(i int) float64 - Close(i int) float64 - Volume(i int) float64 -} - -func NewSlice(s Series) (res []float64) { - if s.Len() <= 0 { - return - } - ll := s.Len() - res = make([]float64, ll) - for i := 0; i < ll; i++ { - res[i] = s.DataAt(i) - } - return -} - -func Opens(s TaSeries) (res []float64) { - if s.Len() <= 0 { - return - } - ll := s.Len() - res = make([]float64, ll) - for i := 0; i < ll; i++ { - res[i] = s.Open(i) - } - return -} - - -func Highs(s TaSeries) (res []float64) { - if s.Len() <= 0 { - return - } - ll := s.Len() - res = make([]float64, ll) - for i := 0; i < ll; i++ { - res[i] = s.High(i) - } - return -} - -func Lows(s TaSeries) (res []float64) { - if s.Len() <= 0 { - return - } - ll := s.Len() - res = make([]float64, ll) - for i := 0; i < ll; i++ { - res[i] = s.Low(i) - } - return -} - -func Closes(s TaSeries) (res []float64) { - if s.Len() <= 0 { - return - } - ll := s.Len() - res = make([]float64, ll) - for i := 0; i < ll; i++ { - res[i] = s.Close(i) - } - return -} - -func Volumes(s TaSeries) (res []float64) { - if s.Len() <= 0 { - return - } - ll := s.Len() - res = make([]float64, ll) - for i := 0; i < ll; i++ { - res[i] = s.Volume(i) - } - return -}