From 2603c67e10effc149911ced40adece6026593e4b Mon Sep 17 00:00:00 2001 From: Will Charczuk Date: Mon, 18 Jul 2016 12:57:10 -0700 Subject: [PATCH] adding lastvalueprovider interface and relevant methods. --- continuous_series.go | 5 +++++ ema_series.go | 4 ++-- sma_series.go | 4 ++-- time_series.go | 9 ++++++++- value_provider.go | 10 ++++++++++ 5 files changed, 27 insertions(+), 5 deletions(-) diff --git a/continuous_series.go b/continuous_series.go index 57c145b..fe0581d 100644 --- a/continuous_series.go +++ b/continuous_series.go @@ -31,6 +31,11 @@ func (cs ContinuousSeries) GetValue(index int) (float64, float64) { return cs.XValues[index], cs.YValues[index] } +// GetLastValue gets the last value. +func (cs ContinuousSeries) GetLastValue() (float64, float64) { + return cs.XValues[len(cs.XValues)-1], cs.YValues[len(cs.YValues)-1] +} + // GetValueFormatters returns value formatter defaults for the series. func (cs ContinuousSeries) GetValueFormatters() (x, y ValueFormatter) { x = FloatValueFormatter diff --git a/ema_series.go b/ema_series.go index d0c58f8..b435751 100644 --- a/ema_series.go +++ b/ema_series.go @@ -52,7 +52,7 @@ func (ema EMASeries) GetSigma() float64 { } // GetValue gets a value at a given index. -func (ema EMASeries) GetValue(index int) (x float64, y float64) { +func (ema EMASeries) GetValue(index int) (x, y float64) { if ema.InnerSeries == nil { return } @@ -64,7 +64,7 @@ func (ema EMASeries) GetValue(index int) (x float64, y float64) { // GetLastValue computes the last moving average value but walking back window size samples, // and recomputing the last moving average chunk. -func (ema EMASeries) GetLastValue() (x float64, y float64) { +func (ema EMASeries) GetLastValue() (x, y float64) { if ema.InnerSeries == nil { return } diff --git a/sma_series.go b/sma_series.go index 89f5344..ca43e2b 100644 --- a/sma_series.go +++ b/sma_series.go @@ -36,7 +36,7 @@ func (sma SMASeries) Len() int { } // GetValue gets a value at a given index. -func (sma SMASeries) GetValue(index int) (x float64, y float64) { +func (sma SMASeries) GetValue(index int) (x, y float64) { if sma.InnerSeries == nil { return } @@ -48,7 +48,7 @@ func (sma SMASeries) GetValue(index int) (x float64, y float64) { // GetLastValue computes the last moving average value but walking back window size samples, // and recomputing the last moving average chunk. -func (sma SMASeries) GetLastValue() (x float64, y float64) { +func (sma SMASeries) GetLastValue() (x, y float64) { if sma.InnerSeries == nil { return } diff --git a/time_series.go b/time_series.go index becf213..37ba6b9 100644 --- a/time_series.go +++ b/time_series.go @@ -29,12 +29,19 @@ func (ts TimeSeries) Len() int { } // GetValue gets a value at a given index. -func (ts TimeSeries) GetValue(index int) (x float64, y float64) { +func (ts TimeSeries) GetValue(index int) (x, y float64) { x = TimeToFloat64(ts.XValues[index]) y = ts.YValues[index] return } +// GetLastValue gets the last value. +func (ts TimeSeries) GetLastValue() (x, y float64) { + x = TimeToFloat64(ts.XValues[len(ts.XValues)-1]) + y = ts.YValues[len(ts.YValues)-1] + return +} + // GetValueFormatters returns value formatter defaults for the series. func (ts TimeSeries) GetValueFormatters() (x, y ValueFormatter) { x = TimeValueFormatter diff --git a/value_provider.go b/value_provider.go index 1ff8e82..6fceeb0 100644 --- a/value_provider.go +++ b/value_provider.go @@ -11,3 +11,13 @@ type BoundedValueProvider interface { Len() int GetBoundedValue(index int) (x, y1, y2 float64) } + +// LastValueProvider is a special type of value provider that can return it's (potentially computed) last value. +type LastValueProvider interface { + GetLastValue() (x, y float64) +} + +// BoundedLastValueProvider is a special type of value provider that can return it's (potentially computed) bounded last value. +type BoundedLastValueProvider interface { + GetBoundedLastValue(index int) (x, y1, y2 float64) +}