sdcoffey / techan

Technical Analysis Library for Golang
https://godoc.org/github.com/sdcoffey/techan
MIT License
840 stars 143 forks source link

MACD Signal line (EMA) seems to be broken/not working... #39

Open amlwwalker opened 3 years ago

amlwwalker commented 3 years ago

Based on a series of data, to plot the MACD and signal lines, I am doing:

    macD := analytics.MACD(closePriceIndicator, 12, 26)
    macDSignal := analytics.MACDSignalLine(macD, 9)

Where the above two functions are defined as:

func MACD(indicator techan.Indicator, shortWindow, longWindow int) techan.Indicator {
    return techan.NewMACDIndicator(indicator, shortWindow, longWindow)
}
func MACDSignalLine(indicator techan.Indicator, window int) techan.Indicator {
    return techan.NewEMAIndicator(indicator, window)
}

However when I plot this, I get two very different lines

image

To check I wasn't going completely mad, I changed the signal line to use an SMA instead, so my MACDSignalLine function becomes

func MACDSignalLine(indicator techan.Indicator, window int) techan.Indicator {
    return techan.NewSimpleMovingAverage(indicator, window) // Create an exponential moving average with a window of 10
}

which plots like

image

Which suggests that the EMA isn't working correctly for some reason here. Any suggestions as to where Im going wrong would be great, thanks!

GeorgeGkinis commented 3 years ago

@amlwwalker Did you plot this in Excell or something else?

amlwwalker commented 3 years ago

@GeorgeGkinis https://github.com/360EntSecGroup-Skylar/excelize

amlwwalker commented 3 years ago

@sdcoffey - was trying to add a bug label to this, as for the v1 milestone be good to know if this is a EMA bug here?

sdcoffey commented 3 years ago

@amlwwalker I'll check this out! Will you let me know what version of the library you're using? Also if you can drop in your test data that would be super helpful as I debug. Thanks!

amlwwalker commented 3 years ago

hey, apologies @sdcoffey I actually now think it was an issue with scaling in Excel - i re plotted the graph in react-vis and it seemed to be good. I wonder if excel was getting caught by not quite adjusting the y scale correctly or something.

I am however having divide by 0 crashes uses the trend_line:

techan.NewTrendlineIndicator(closeIndicator, 14)

Im using AAPL stock data (last ~thousand points) that I downloaded the CSV for from yahoo. I get:

panic: division of zero by zero or infinity by infinity [recovered]
    panic: interface conversion: interface {} is big.ErrNaN, not string
math/big.(*Float).Quo(0xc000ca3890, 0xc000ca3680, 0xc000ca3860, 0xc000ca3860)
    /usr/local/go/src/math/big/float.go:1651 +0x11f
github.com/sdcoffey/big.Decimal.Div.func1(0xc000486fd0)
    /Users/alex.walker/go/pkg/mod/github.com/sdcoffey/big@v0.7.0/decimal.go:133 +0x79
github.com/sdcoffey/big.nanGuard(0xc000486fe0, 0xc000486fd0, 0x2, 0x2, 0xc000ca3860)
    /Users/alex.walker/go/pkg/mod/github.com/sdcoffey/big@v0.7.0/decimal.go:359 +0x66
github.com/sdcoffey/big.Decimal.Div(0xc000ca3680, 0xc000ca3860, 0xc000ca3860)
    /Users/alex.walker/go/pkg/mod/github.com/sdcoffey/big@v0.7.0/decimal.go:132 +0x8b
github.com/sdcoffey/techan.trendLineIndicator.Calculate(0x4953160, 0xc000bdd180, 0xe, 0x0, 0xc0000a2300)
    /Users/alex.walker/go/pkg/mod/github.com/sdcoffey/techan@v0.12.0/indicator_trend.go:32 +0x265

My guess is for a few days there isn't a value, but I didn't think that would cause a run time error. I was wondering if it needs something on line 32 of indicator_trend.go:

    if cd.LTE(big.NewDecimal(0)) {
        return big.NewDecimal(0)
    }

versions:

    github.com/sdcoffey/big v0.7.0
    github.com/sdcoffey/techan v0.12.0

While here, I was wondering if there was any seasonality/ARIMA/Prophet esq work, in the works, of if not do you know of any good library and I might try and implement it myself....?

sdcoffey commented 3 years ago

Hey @amlwwalker, good to know. I'll check out the divide by zero error.

I definitely do not have any forecasting plans in the works, I think it might be out of scope for this project. I don't know of any library in go that does forecasting/seasonality, although you might look in awesome-go.