2024-02-18 12:55:21 +00:00
|
|
|
How To Use Builtin Indicators and Add New Indicators (V2)
|
|
|
|
=========================================================
|
|
|
|
|
|
|
|
## Using Built-in Indicators
|
|
|
|
|
|
|
|
In bbgo session, we already have several built-in indicators defined.
|
2022-04-04 09:19:17 +00:00
|
|
|
|
|
|
|
We could refer to the live-data without the worriedness of handling market data subscription.
|
2024-02-18 12:55:21 +00:00
|
|
|
To use the builtin indicators, call `Indicators(symbol)` method to get the indicator set of a symbol,
|
2022-04-04 09:19:17 +00:00
|
|
|
|
|
|
|
```go
|
2024-02-18 12:55:21 +00:00
|
|
|
session.Indicators(symbol string) *IndicatorSet
|
2022-04-04 09:19:17 +00:00
|
|
|
```
|
|
|
|
|
2024-02-18 12:55:21 +00:00
|
|
|
IndicatorSet is a helper that helps you construct the indicator instance.
|
|
|
|
Each indicator is a stream that subscribes to
|
|
|
|
an upstream through the callback.
|
|
|
|
|
|
|
|
We will explain how the indicator works from scratch in the following section.
|
|
|
|
|
|
|
|
The following code will create a kLines stream that subscribes to specific klines from a websocket stream instance:
|
|
|
|
|
2022-04-04 09:19:17 +00:00
|
|
|
```go
|
2024-02-18 12:55:21 +00:00
|
|
|
kLines := indicatorv2.KLines(stream, "BTCUSDT", types.Interval1m)
|
2022-04-04 09:19:17 +00:00
|
|
|
```
|
|
|
|
|
2024-02-18 12:55:21 +00:00
|
|
|
The kLines stream is a special indicator stream that subscribes to the kLine event from the websocket stream. It
|
|
|
|
registers a callback on `OnKLineClosed`, and when there is a kLine event triggered, it pushes the kLines into its
|
|
|
|
series, and then it triggers all the subscribers that subscribe to it.
|
2022-04-04 09:19:17 +00:00
|
|
|
|
2024-02-18 12:55:21 +00:00
|
|
|
To get the closed prices from the kLines stream, simply pass the kLines stream instance to a ClosedPrice indicator:
|
2022-04-04 09:19:17 +00:00
|
|
|
|
|
|
|
```go
|
2024-02-18 12:55:21 +00:00
|
|
|
closePrices := indicatorv2.ClosePrices(kLines)
|
|
|
|
```
|
2022-04-04 09:19:17 +00:00
|
|
|
|
2024-02-18 12:55:21 +00:00
|
|
|
When the kLine indicator pushes a new kline, the ClosePrices stream receives a kLine object then gets the closed price
|
|
|
|
from the kLine object.
|
2022-04-04 09:19:17 +00:00
|
|
|
|
2024-02-18 12:55:21 +00:00
|
|
|
to get the latest value of an indicator (closePrices), use Last(n) method, where n starts from 0:
|
2022-04-04 09:19:17 +00:00
|
|
|
|
2024-02-18 12:55:21 +00:00
|
|
|
```go
|
|
|
|
lastClosedPrice := closePrices.Last(0)
|
|
|
|
secondClosedPrice := closePrices.Last(1)
|
|
|
|
```
|
2022-04-04 09:19:17 +00:00
|
|
|
|
2024-02-18 12:55:21 +00:00
|
|
|
To create a EMA indicator instance, again, simply pass the closePrice indicator to the SMA stream constructor:
|
|
|
|
|
|
|
|
```go
|
|
|
|
ema := indicatorv2.EMA(closePrices, 17)
|
2022-04-04 09:19:17 +00:00
|
|
|
```
|
|
|
|
|
2024-02-18 12:55:21 +00:00
|
|
|
If you want to listen to the EMA value events, just add a callback on the indicator instance:
|
2022-04-04 09:19:17 +00:00
|
|
|
|
|
|
|
```go
|
2024-02-18 12:55:21 +00:00
|
|
|
ema.OnUpdate(func(v float64) { .... })
|
|
|
|
```
|
|
|
|
|
|
|
|
## Adding New Indicator
|
|
|
|
|
|
|
|
Adding a new indicator is pretty straightforward. Simply create a new struct and insert the necessary parameters as
|
|
|
|
struct fields.
|
|
|
|
|
|
|
|
The indicator algorithm will be implemented in the `Calculate(v float64) float64` method.
|
|
|
|
|
|
|
|
You can think of it as a simple input-output model: it takes a float64 number as input, calculates the value, and
|
|
|
|
returns a float64 number as output.
|
2022-04-04 09:19:17 +00:00
|
|
|
|
|
|
|
```
|
2024-02-18 12:55:21 +00:00
|
|
|
[input float64] -> [Calculate] -> [output float64]
|
|
|
|
```
|
|
|
|
|
|
|
|
Since it is a float64 value indicator, we will use `*types.Float64Series` here to store our values:
|
|
|
|
|
2022-04-04 09:19:17 +00:00
|
|
|
```go
|
2024-02-18 12:55:21 +00:00
|
|
|
package indicatorv2
|
2022-07-14 01:34:48 +00:00
|
|
|
|
2024-02-18 12:55:21 +00:00
|
|
|
type EWMAStream struct {
|
|
|
|
// embedded struct to inherit Float64Series methods
|
|
|
|
*types.Float64Series
|
2022-07-14 01:34:48 +00:00
|
|
|
|
2024-02-18 12:55:21 +00:00
|
|
|
// parameters we need
|
|
|
|
window int
|
|
|
|
multiplier float64
|
2022-07-14 01:34:48 +00:00
|
|
|
}
|
2024-02-18 12:55:21 +00:00
|
|
|
```
|
2022-07-14 01:34:48 +00:00
|
|
|
|
2024-02-18 12:55:21 +00:00
|
|
|
And then, add the constructor of the indicator stream:
|
|
|
|
|
|
|
|
```go
|
|
|
|
// the "source" here is your value source
|
|
|
|
func EWMA(source types.Float64Source, window int) *EWMAStream {
|
|
|
|
s := &EWMAStream{
|
|
|
|
Float64Series: types.NewFloat64Series(),
|
|
|
|
window: window,
|
|
|
|
multiplier: 2.0 / float64(1+window),
|
|
|
|
}
|
|
|
|
s.Bind(source, s)
|
|
|
|
return s
|
2022-04-04 09:19:17 +00:00
|
|
|
}
|
2024-02-18 12:55:21 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
Where the source refers to your upstream value, such as closedPrices, openedPrices, or any type of float64 series. For
|
|
|
|
example, Volume could also serve as the source.
|
|
|
|
|
|
|
|
The Bind method invokes the `Calculate()` method to obtain the updated value from a callback of the upstream source.
|
|
|
|
Subsequently, it calls EmitUpdate to activate the callbacks of its subscribers,
|
|
|
|
thereby passing the updated value to all of them.
|
2022-04-04 09:19:17 +00:00
|
|
|
|
2024-02-18 12:55:21 +00:00
|
|
|
Next, write your algorithm within the Calculate method:
|
|
|
|
|
|
|
|
```go
|
|
|
|
func (s *EWMAStream) Calculate(v float64) float64 {
|
|
|
|
// if you need the last number to calculate the next value
|
|
|
|
// call s.Slice.Last(0)
|
|
|
|
//
|
|
|
|
last := s.Slice.Last(0)
|
|
|
|
if last == 0.0 {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
m := s.multiplier
|
|
|
|
return (1.0-m)*last + m*v
|
2022-04-04 09:19:17 +00:00
|
|
|
}
|
2024-02-18 12:55:21 +00:00
|
|
|
```
|
2022-04-04 09:19:17 +00:00
|
|
|
|
2024-02-18 12:55:21 +00:00
|
|
|
Sometimes you might need to store the intermediate values inside your indicator, you can add the extra field with type Float64Series like this:
|
|
|
|
|
|
|
|
```go
|
|
|
|
type EWMAStream struct {
|
|
|
|
// embedded struct to inherit Float64Series methods
|
|
|
|
*types.Float64Series
|
|
|
|
|
|
|
|
A *types.Float64Series
|
|
|
|
B *types.Float64Series
|
|
|
|
|
|
|
|
// parameters we need
|
|
|
|
window int
|
|
|
|
multiplier float64
|
2022-04-04 09:19:17 +00:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2024-02-18 12:55:21 +00:00
|
|
|
In your `Calculate()` method, you can push the values into these float64 series, for example:
|
2022-04-04 09:19:17 +00:00
|
|
|
|
2024-02-18 12:55:21 +00:00
|
|
|
```go
|
|
|
|
func (s *EWMAStream) Calculate(v float64) float64 {
|
|
|
|
// if you need the last number to calculate the next value
|
|
|
|
// call s.Slice.Last(0)
|
|
|
|
last := s.Slice.Last(0)
|
|
|
|
if last == 0.0 {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
// If you need to trigger callbacks, use PushAndEmit
|
|
|
|
s.A.Push(last / 2)
|
|
|
|
s.B.Push(last / 3)
|
|
|
|
|
|
|
|
m := s.multiplier
|
|
|
|
return (1.0-m)*last + m*v
|
|
|
|
}
|
|
|
|
```
|
2022-05-09 01:55:32 +00:00
|
|
|
|
|
|
|
|