bbgo_origin/pkg/indicator/psar.go

125 lines
3.1 KiB
Go
Raw Permalink Normal View History

2022-12-16 07:39:49 +00:00
package indicator
import (
2022-12-19 09:11:08 +00:00
"math"
2022-12-16 07:39:49 +00:00
"time"
2022-12-19 09:11:08 +00:00
"github.com/c9s/bbgo/pkg/datatype/floats"
2022-12-16 07:39:49 +00:00
"github.com/c9s/bbgo/pkg/types"
)
2022-12-19 09:11:08 +00:00
// Parabolic SAR(Stop and Reverse) / SAR
2022-12-16 07:39:49 +00:00
// Refer: https://www.investopedia.com/terms/p/parabolicindicator.asp
// The parabolic SAR indicator, developed by J. Wells Wilder, is used by traders to determine
// trend direction and potential reversals in price. The indicator uses a trailing stop and
// reverse method called "SAR," or stop and reverse, to identify suitable exit and entry points.
// Traders also refer to the indicator as to the parabolic stop and reverse, parabolic SAR, or PSAR.
//
// The parabolic SAR indicator appears on a chart as a series of dots, either above or below an asset's
// price, depending on the direction the price is moving. A dot is placed below the price when it is
// trending upward, and above the price when it is trending downward.
//go:generate callbackgen -type PSAR
type PSAR struct {
types.SeriesBase
types.IntervalWindow
2022-12-19 09:11:08 +00:00
High *types.Queue
Low *types.Queue
Values floats.Slice // Stop and Reverse
AF float64 // Acceleration Factor
EP float64
Falling bool
2022-12-16 07:39:49 +00:00
EndTime time.Time
UpdateCallbacks []func(value float64)
}
func (inc *PSAR) Last() float64 {
2022-12-19 09:11:08 +00:00
if len(inc.Values) == 0 {
2022-12-16 07:39:49 +00:00
return 0
}
2022-12-19 09:11:08 +00:00
return inc.Values.Last()
2022-12-16 07:39:49 +00:00
}
func (inc *PSAR) Length() int {
2022-12-19 09:11:08 +00:00
return len(inc.Values)
}
func (inc *PSAR) falling() bool {
up := inc.High.Last() - inc.High.Index(1)
dn := inc.Low.Index(1) - inc.Low.Last()
return (dn > up) && (dn > 0)
2022-12-16 07:39:49 +00:00
}
2022-12-19 09:11:08 +00:00
func (inc *PSAR) Update(high, low float64) {
if inc.High == nil {
2022-12-16 07:39:49 +00:00
inc.SeriesBase.Series = inc
2022-12-19 09:11:08 +00:00
inc.High = types.NewQueue(inc.Window)
inc.Low = types.NewQueue(inc.Window)
inc.Values = floats.Slice{}
2022-12-16 07:39:49 +00:00
inc.AF = 0.02
2022-12-19 09:11:08 +00:00
inc.High.Update(high)
inc.Low.Update(low)
return
2022-12-16 07:39:49 +00:00
}
2022-12-19 09:11:08 +00:00
isFirst := inc.High.Length() < inc.Window
inc.High.Update(high)
inc.Low.Update(low)
if !isFirst {
ppsar := inc.Values.Last()
if inc.Falling { // falling formula
psar := ppsar - inc.AF*(ppsar-inc.EP)
h := inc.High.Shift(1).Highest(2)
inc.Values.Push(math.Max(psar, h))
if low < inc.EP {
inc.EP = low
if inc.AF <= 0.18 {
inc.AF += 0.02
2022-12-16 07:39:49 +00:00
}
}
2022-12-19 09:11:08 +00:00
if high > psar { // reverse
2022-12-16 07:39:49 +00:00
inc.AF = 0.02
2022-12-19 09:11:08 +00:00
inc.Values[len(inc.Values)-1] = inc.EP
inc.EP = high
inc.Falling = false
2022-12-16 07:39:49 +00:00
}
2022-12-19 09:11:08 +00:00
} else { // rising formula
psar := ppsar + inc.AF*(inc.EP-ppsar)
l := inc.Low.Shift(1).Lowest(2)
inc.Values.Push(math.Min(psar, l))
if high > inc.EP {
inc.EP = high
if inc.AF <= 0.18 {
inc.AF += 0.02
2022-12-16 07:39:49 +00:00
}
}
2022-12-19 09:11:08 +00:00
if low < psar { // reverse
2022-12-16 07:39:49 +00:00
inc.AF = 0.02
2022-12-19 09:11:08 +00:00
inc.Values[len(inc.Values)-1] = inc.EP
inc.EP = low
inc.Falling = true
2022-12-16 07:39:49 +00:00
}
2022-12-19 09:11:08 +00:00
}
} else {
inc.Falling = inc.falling()
if inc.Falling {
inc.Values.Push(inc.High.Index(1))
inc.EP = inc.Low.Index(1)
} else {
inc.Values.Push(inc.Low.Index(1))
inc.EP = inc.High.Index(1)
2022-12-16 07:39:49 +00:00
}
}
}
var _ types.SeriesExtend = &PSAR{}
func (inc *PSAR) PushK(k types.KLine) {
inc.Update(k.High.Float64(), k.Low.Float64())
2022-12-16 07:39:49 +00:00
}
func (inc *PSAR) BindK(target KLineClosedEmitter, symbol string, interval types.Interval) {
target.OnKLineClosed(types.KLineWith(symbol, interval, inc.PushK))
2022-12-16 07:39:49 +00:00
}