sdcoffey / techan

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

Representing NA values #15

Closed zhengyangfeng00 closed 4 years ago

zhengyangfeng00 commented 5 years ago

Hi! I think it's useful to have a special NA or NaN value to represent the case where a value cannot be calculated. Other popular data analysis libraries such like numpy/pandas all have it.

One use case is that I want to calculate the difference between today's close price and yesterday's close price for each candle in the time series. Then I want to do dif := techan.NewDifferenceIndicator(today, NewRefIndicator(yday, 1)) but if I accidentally call dif.Calculate(0) it will segfault.

I think we can workaround this problem by other means but I feel the most natural way is to add a NA value. What's your thoughts on this?

sdcoffey commented 5 years ago

Hey @zhengyangfeng00, I like the idea! Definitely an omission from the library in it's current state. How would you feel about collaborating on this? If you send me a PR i'm happy to look at it, otherwise, i'll try to address this as soon as I have time

zhengyangfeng00 commented 5 years ago

I'm happy to work on a PR. I'm experimenting with a very simple implementation:

package na

import (
    "math"

    "github.com/sdcoffey/big"
)

var (
    // NA is a decimal object that represents a result that cannot be calculated.
    NA = big.NewDecimal(float64(math.MaxInt64))
)

func IsNA(d big.Decimal) bool {
    return d.EQ(NA)
}

and it worked fine with my use cases. I basically have all indicators be aware of this NaN value. Essentially if an indicator depends on another one, then in its Calculate it first checks if the indicator it depends on calculates to NaN and if so, it returns NaN as well.

The majority of work I image is to update all existing indicators to behave similar?

sdcoffey commented 4 years ago

@zhengyangfeng00 Since this is more of a feature of big, i've moved this ticket here: https://github.com/sdcoffey/big/issues/3