From f4c603a100bde8e1906184993749fbc6eb6d26a3 Mon Sep 17 00:00:00 2001 From: c9s Date: Sun, 18 Feb 2024 21:09:40 +0800 Subject: [PATCH] doc: add more v2 indicator details --- doc/development/indicator.md | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/doc/development/indicator.md b/doc/development/indicator.md index 44852eb81..08cb1430c 100644 --- a/doc/development/indicator.md +++ b/doc/development/indicator.md @@ -44,7 +44,7 @@ lastClosedPrice := closePrices.Last(0) secondClosedPrice := closePrices.Last(1) ``` -To create a EMA indicator instance, again, simply pass the closePrice indicator to the SMA stream constructor: +To create an EMA indicator instance, again, simply pass the closePrice indicator to the SMA stream constructor: ```go ema := indicatorv2.EMA(closePrices, 17) @@ -70,7 +70,33 @@ returns a float64 number as output. [input float64] -> [Calculate] -> [output float64] ``` -Since it is a float64 value indicator, we will use `*types.Float64Series` here to store our values: +Since it will be a float64 value indicator, we will use `*types.Float64Series` here to store our values, +`types.Float64Series` is a struct that contains a slice to store the float64 values, it is implemented as follows: + +```go +type Float64Series struct { + SeriesBase + Float64Updater + Slice floats.Slice +} +``` + +The `Slice` field is a []float64 slice, +which provides some helper methods that helps you do some calculation on the float64 slice. + +And Float64Updater provides a way to let indicators subscribe to the updated value: + +```go +u := Float64Updater{} +u.OnUpdate(func(v float64) { ... }) +u.OnUpdate(func(v float64) { ... }) + +// to emit the callbacks +u.EmitUpdate(10.0) +``` + +Now you have a basic concept about the Float64Series, +we can now start to implement our first indicator structure: ```go package indicatorv2 @@ -85,6 +111,8 @@ type EWMAStream struct { } ``` +Again, since it is a float64 value indicator, we use `*types.Float64Series` here to store our values. + And then, add the constructor of the indicator stream: ```go