techan icon indicating copy to clipboard operation
techan copied to clipboard

Ability to remove (to re-calc) cached calculations

Open tomcruise81 opened this issue 5 years ago • 4 comments

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).

tomcruise81 avatar Mar 05 '21 14:03 tomcruise81

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.

codecov[bot] avatar Mar 05 '21 14:03 codecov[bot]

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

sdcoffey avatar Mar 06 '21 17:03 sdcoffey

@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 avatar Mar 08 '21 17:03 tomcruise81

...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)
			}
		}
	}
}

tomcruise81 avatar Mar 08 '21 17:03 tomcruise81