sdcoffey / techan

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

Ability to remove (to re-calc) cached calculations #33

Open tomcruise81 opened 3 years ago

tomcruise81 commented 3 years ago

Not sure if this would be something that you're interested in, but I've found benefit from using the MACD Histogram with minute candles, where the last candle is updated based on sub-minute streamed values - i.e. the last candle is an approximation, but still provides value. It requires the ability to re-calculate (i.e. not use the cached value).

codecov[bot] commented 3 years ago

Codecov Report

Merging #33 (61a09a5) into main (e87ec2e) will increase coverage by 0.02%. The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main      #33      +/-   ##
==========================================
+ Coverage   99.29%   99.31%   +0.02%     
==========================================
  Files          35       35              
  Lines         566      587      +21     
==========================================
+ Hits          562      583      +21     
  Misses          2        2              
  Partials        2        2              
Impacted Files Coverage Δ
indicator_basic.go 100.00% <ø> (ø)
indicator_cci.go 100.00% <ø> (ø)
indicator_constant.go 100.00% <ø> (ø)
indicator_fixed.go 100.00% <ø> (ø)
indicator_aroon.go 93.54% <100.00%> (+0.21%) :arrow_up:
indicator_bollinger_band.go 100.00% <100.00%> (ø)
indicator_derivative.go 100.00% <100.00%> (ø)
indicator_difference.go 100.00% <100.00%> (ø)
indicator_maximum_drawdown.go 100.00% <100.00%> (ø)
indicator_maximum_value.go 100.00% <100.00%> (ø)
... and 8 more

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update e87ec2e...61a09a5. Read the comment docs.

sdcoffey commented 3 years ago

Hey @tomcruise81! Love the idea. Check out #30 which I just merged. I think if we export the new cachedIndicator type, and you add this method there, and then change the return types of all cached indicators to the new type, we can accomplish this without changing the base type or breaking the API. Lmk what you think

tomcruise81 commented 3 years ago

@sdcoffey - I think we'd still need to augment the other types due to the need for recursively removing the cached calculations from inner indicators. So even if I do something like:

type CachedIndicator interface {
    Indicator
    cache() resultCache
    setCache(cache resultCache)
    windowSize() int
}

func RemoveCachedEntry(indicator Indicator, index int) {
    cachedIndicator, ok := indicator.(CachedIndicator)
    if ok {
        cacheResult(cachedIndicator, index, nil)
    }
    for _, subIndicator := range indicator.subIndicators() {
        RemoveCachedEntry(subIndicator, index)
    }
}

we'd need to change to something like indicator.subIndicators() for the recursive part...

tomcruise81 commented 3 years ago

...although I may be able to create another interface:


type NestedIndicator interface {
    Indicator
    nestedIndicators() []Indicator
}

func RemoveCachedEntry(indicator Indicator, index int) {
    cachedIndicator, ok := indicator.(CachedIndicator)
    if ok {
        cacheResult(cachedIndicator, index, nil)
    }

    // Recursion
    nestedIndicator, ok := indicator.(NestedIndicator)
    if ok {
        for _, nestedIndicator := range nestedIndicator.nestedIndicators() {
            cachedNestedIndicator, ok := nestedIndicator.(CachedIndicator)
            if ok {
                RemoveCachedEntry(cachedNestedIndicator, index)
            }
        }
    }
}