mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-10 09:11:55 +00:00
Merge pull request #1050 from frin1/new_indicators
Feature: New indicators
This commit is contained in:
commit
effd4df72e
54
pkg/datatype/bools/slice.go
Normal file
54
pkg/datatype/bools/slice.go
Normal file
|
@ -0,0 +1,54 @@
|
|||
package bools
|
||||
|
||||
type BoolSlice []bool
|
||||
|
||||
func New(a ...bool) BoolSlice {
|
||||
return BoolSlice(a)
|
||||
}
|
||||
|
||||
func (s *BoolSlice) Push(v bool) {
|
||||
*s = append(*s, v)
|
||||
}
|
||||
|
||||
func (s *BoolSlice) Update(v bool) {
|
||||
*s = append(*s, v)
|
||||
}
|
||||
|
||||
func (s *BoolSlice) Pop(i int64) (v bool) {
|
||||
v = (*s)[i]
|
||||
*s = append((*s)[:i], (*s)[i+1:]...)
|
||||
return v
|
||||
}
|
||||
|
||||
func (s BoolSlice) Tail(size int) BoolSlice {
|
||||
length := len(s)
|
||||
if length <= size {
|
||||
win := make(BoolSlice, length)
|
||||
copy(win, s)
|
||||
return win
|
||||
}
|
||||
|
||||
win := make(BoolSlice, size)
|
||||
copy(win, s[length-size:])
|
||||
return win
|
||||
}
|
||||
|
||||
func (s *BoolSlice) Length() int {
|
||||
return len(*s)
|
||||
}
|
||||
|
||||
func (s *BoolSlice) Index(i int) bool {
|
||||
length := len(*s)
|
||||
if length-i < 0 || i < 0 {
|
||||
return false
|
||||
}
|
||||
return (*s)[length-i-1]
|
||||
}
|
||||
|
||||
func (s *BoolSlice) Last() bool {
|
||||
length := len(*s)
|
||||
if length > 0 {
|
||||
return (*s)[length-1]
|
||||
}
|
||||
return false
|
||||
}
|
|
@ -20,7 +20,9 @@ type Supertrend struct {
|
|||
|
||||
AverageTrueRange *ATR
|
||||
|
||||
trendPrices floats.Slice
|
||||
trendPrices floats.Slice // Value of the trend line (buy or sell)
|
||||
supportLine floats.Slice // The support line in an uptrend (green)
|
||||
resistanceLine floats.Slice // The resistance line in a downtrend (red)
|
||||
|
||||
closePrice float64
|
||||
previousClosePrice float64
|
||||
|
@ -119,6 +121,10 @@ func (inc *Supertrend) Update(highPrice, lowPrice, closePrice float64) {
|
|||
inc.trendPrices.Push(inc.uptrendPrice)
|
||||
}
|
||||
|
||||
// Save the trend lines
|
||||
inc.supportLine.Push(inc.uptrendPrice)
|
||||
inc.resistanceLine.Push(inc.downtrendPrice)
|
||||
|
||||
logst.Debugf("Update supertrend result: closePrice: %v, uptrendPrice: %v, downtrendPrice: %v, trend: %v,"+
|
||||
" tradeSignal: %v, AverageTrueRange.Last(): %v", inc.closePrice, inc.uptrendPrice, inc.downtrendPrice,
|
||||
inc.trend, inc.tradeSignal, inc.AverageTrueRange.Last())
|
||||
|
@ -128,6 +134,21 @@ func (inc *Supertrend) GetSignal() types.Direction {
|
|||
return inc.tradeSignal
|
||||
}
|
||||
|
||||
// GetDirection return the current trend
|
||||
func (inc *Supertrend) Direction() types.Direction {
|
||||
return inc.trend
|
||||
}
|
||||
|
||||
// LastSupertrendSupport return the current supertrend support
|
||||
func (inc *Supertrend) LastSupertrendSupport() float64 {
|
||||
return inc.supportLine.Last()
|
||||
}
|
||||
|
||||
// LastSupertrendResistance return the current supertrend resistance
|
||||
func (inc *Supertrend) LastSupertrendResistance() float64 {
|
||||
return inc.resistanceLine.Last()
|
||||
}
|
||||
|
||||
var _ types.SeriesExtend = &Supertrend{}
|
||||
|
||||
func (inc *Supertrend) PushK(k types.KLine) {
|
||||
|
|
217
pkg/indicator/supertrendPivot.go
Normal file
217
pkg/indicator/supertrendPivot.go
Normal file
|
@ -0,0 +1,217 @@
|
|||
package indicator
|
||||
|
||||
import (
|
||||
"math"
|
||||
"time"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/c9s/bbgo/pkg/datatype/floats"
|
||||
"github.com/c9s/bbgo/pkg/types"
|
||||
)
|
||||
|
||||
// based on "Pivot Point Supertrend by LonesomeTheBlue" from tradingview
|
||||
|
||||
var logpst = logrus.WithField("indicator", "pivotSupertrend")
|
||||
|
||||
//go:generate callbackgen -type PivotSupertrend
|
||||
type PivotSupertrend struct {
|
||||
types.SeriesBase
|
||||
types.IntervalWindow
|
||||
ATRMultiplier float64 `json:"atrMultiplier"`
|
||||
PivotWindow int `json:"pivotWindow"`
|
||||
|
||||
AverageTrueRange *ATR // Value must be set when initialized in strategy
|
||||
|
||||
PivotLow *PivotLow // Value must be set when initialized in strategy
|
||||
PivotHigh *PivotHigh // Value must be set when initialized in strategy
|
||||
|
||||
trendPrices floats.Slice // Tsl: value of the trend line (buy or sell)
|
||||
supportLine floats.Slice // The support line in an uptrend (green)
|
||||
resistanceLine floats.Slice // The resistance line in a downtrend (red)
|
||||
|
||||
closePrice float64
|
||||
previousClosePrice float64
|
||||
uptrendPrice float64
|
||||
previousUptrendPrice float64
|
||||
downtrendPrice float64
|
||||
previousDowntrendPrice float64
|
||||
|
||||
lastPp float64
|
||||
src float64 // center
|
||||
previousPivotHigh float64 // temp variable to save the last value
|
||||
previousPivotLow float64 // temp variable to save the last value
|
||||
|
||||
trend types.Direction
|
||||
previousTrend types.Direction
|
||||
tradeSignal types.Direction
|
||||
|
||||
EndTime time.Time
|
||||
UpdateCallbacks []func(value float64)
|
||||
}
|
||||
|
||||
func (inc *PivotSupertrend) Last() float64 {
|
||||
return inc.trendPrices.Last()
|
||||
}
|
||||
|
||||
func (inc *PivotSupertrend) Index(i int) float64 {
|
||||
length := inc.Length()
|
||||
if length == 0 || length-i-1 < 0 {
|
||||
return 0
|
||||
}
|
||||
return inc.trendPrices[length-i-1]
|
||||
}
|
||||
|
||||
func (inc *PivotSupertrend) Length() int {
|
||||
return len(inc.trendPrices)
|
||||
}
|
||||
|
||||
func (inc *PivotSupertrend) Update(highPrice, lowPrice, closePrice float64) {
|
||||
if inc.Window <= 0 {
|
||||
panic("window must be greater than 0")
|
||||
}
|
||||
|
||||
if inc.AverageTrueRange == nil {
|
||||
inc.SeriesBase.Series = inc
|
||||
}
|
||||
|
||||
// Start with DirectionUp
|
||||
if inc.trend != types.DirectionUp && inc.trend != types.DirectionDown {
|
||||
inc.trend = types.DirectionUp
|
||||
}
|
||||
|
||||
inc.previousPivotLow = inc.PivotLow.Last()
|
||||
inc.previousPivotHigh = inc.PivotHigh.Last()
|
||||
|
||||
// Update High / Low pivots
|
||||
inc.PivotLow.Update(lowPrice)
|
||||
inc.PivotHigh.Update(highPrice)
|
||||
|
||||
// Update ATR
|
||||
inc.AverageTrueRange.Update(highPrice, lowPrice, closePrice)
|
||||
|
||||
// Update last prices
|
||||
inc.previousUptrendPrice = inc.uptrendPrice
|
||||
inc.previousDowntrendPrice = inc.downtrendPrice
|
||||
inc.previousClosePrice = inc.closePrice
|
||||
inc.previousTrend = inc.trend
|
||||
|
||||
inc.closePrice = closePrice
|
||||
|
||||
// Initialize lastPp as soon as pivots are made
|
||||
if inc.lastPp == 0 || math.IsNaN(inc.lastPp) {
|
||||
if inc.PivotHigh.Length() > 0 {
|
||||
inc.lastPp = inc.PivotHigh.Last()
|
||||
} else if inc.PivotLow.Length() > 0 {
|
||||
inc.lastPp = inc.PivotLow.Last()
|
||||
} else {
|
||||
inc.lastPp = math.NaN()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Set lastPp to the latest pivotPoint (only changed when new pivot is found)
|
||||
if inc.PivotHigh.Last() != inc.previousPivotHigh {
|
||||
inc.lastPp = inc.PivotHigh.Last()
|
||||
} else if inc.PivotLow.Last() != inc.previousPivotLow {
|
||||
inc.lastPp = inc.PivotLow.Last()
|
||||
}
|
||||
|
||||
// calculate the Center line using pivot points
|
||||
if inc.src == 0 || math.IsNaN(inc.src) {
|
||||
inc.src = inc.lastPp
|
||||
} else {
|
||||
//weighted calculation
|
||||
inc.src = (inc.src*2 + inc.lastPp) / 3
|
||||
}
|
||||
|
||||
// Update uptrend
|
||||
inc.uptrendPrice = inc.src - inc.AverageTrueRange.Last()*inc.ATRMultiplier
|
||||
if inc.previousClosePrice > inc.previousUptrendPrice {
|
||||
inc.uptrendPrice = math.Max(inc.uptrendPrice, inc.previousUptrendPrice)
|
||||
}
|
||||
|
||||
// Update downtrend
|
||||
inc.downtrendPrice = inc.src + inc.AverageTrueRange.Last()*inc.ATRMultiplier
|
||||
if inc.previousClosePrice < inc.previousDowntrendPrice {
|
||||
inc.downtrendPrice = math.Min(inc.downtrendPrice, inc.previousDowntrendPrice)
|
||||
}
|
||||
|
||||
// Update trend
|
||||
if inc.previousTrend == types.DirectionUp && inc.closePrice < inc.previousUptrendPrice {
|
||||
inc.trend = types.DirectionDown
|
||||
} else if inc.previousTrend == types.DirectionDown && inc.closePrice > inc.previousDowntrendPrice {
|
||||
inc.trend = types.DirectionUp
|
||||
} else {
|
||||
inc.trend = inc.previousTrend
|
||||
}
|
||||
|
||||
// Update signal
|
||||
if inc.AverageTrueRange.Last() <= 0 {
|
||||
inc.tradeSignal = types.DirectionNone
|
||||
} else if inc.trend == types.DirectionUp && inc.previousTrend == types.DirectionDown {
|
||||
inc.tradeSignal = types.DirectionUp
|
||||
} else if inc.trend == types.DirectionDown && inc.previousTrend == types.DirectionUp {
|
||||
inc.tradeSignal = types.DirectionDown
|
||||
} else {
|
||||
inc.tradeSignal = types.DirectionNone
|
||||
}
|
||||
|
||||
// Update trend price
|
||||
if inc.trend == types.DirectionDown {
|
||||
inc.trendPrices.Push(inc.downtrendPrice)
|
||||
} else {
|
||||
inc.trendPrices.Push(inc.uptrendPrice)
|
||||
}
|
||||
|
||||
// Save the trend lines
|
||||
inc.supportLine.Push(inc.uptrendPrice)
|
||||
inc.resistanceLine.Push(inc.downtrendPrice)
|
||||
|
||||
logpst.Debugf("Update pivot point supertrend result: closePrice: %v, uptrendPrice: %v, downtrendPrice: %v, trend: %v,"+
|
||||
" tradeSignal: %v, AverageTrueRange.Last(): %v", inc.closePrice, inc.uptrendPrice, inc.downtrendPrice,
|
||||
inc.trend, inc.tradeSignal, inc.AverageTrueRange.Last())
|
||||
}
|
||||
|
||||
// GetSignal returns signal (Down, None or Up)
|
||||
func (inc *PivotSupertrend) GetSignal() types.Direction {
|
||||
return inc.tradeSignal
|
||||
}
|
||||
|
||||
// GetDirection return the current trend
|
||||
func (inc *PivotSupertrend) Direction() types.Direction {
|
||||
return inc.trend
|
||||
}
|
||||
|
||||
// LastSupertrendSupport return the current supertrend support value
|
||||
func (inc *PivotSupertrend) LastSupertrendSupport() float64 {
|
||||
return inc.supportLine.Last()
|
||||
}
|
||||
|
||||
// LastSupertrendResistance return the current supertrend resistance value
|
||||
func (inc *PivotSupertrend) LastSupertrendResistance() float64 {
|
||||
return inc.resistanceLine.Last()
|
||||
}
|
||||
|
||||
var _ types.SeriesExtend = &PivotSupertrend{}
|
||||
|
||||
func (inc *PivotSupertrend) PushK(k types.KLine) {
|
||||
if inc.EndTime != zeroTime && !k.EndTime.After(inc.EndTime) {
|
||||
return
|
||||
}
|
||||
|
||||
inc.Update(k.GetHigh().Float64(), k.GetLow().Float64(), k.GetClose().Float64())
|
||||
inc.EndTime = k.EndTime.Time()
|
||||
inc.EmitUpdate(inc.Last())
|
||||
}
|
||||
|
||||
func (inc *PivotSupertrend) BindK(target KLineClosedEmitter, symbol string, interval types.Interval) {
|
||||
target.OnKLineClosed(types.KLineWith(symbol, interval, inc.PushK))
|
||||
}
|
||||
|
||||
func (inc *PivotSupertrend) LoadK(allKLines []types.KLine) {
|
||||
inc.SeriesBase.Series = inc
|
||||
for _, k := range allKLines {
|
||||
inc.PushK(k)
|
||||
}
|
||||
}
|
15
pkg/indicator/supertrendPivot_callbacks.go
Normal file
15
pkg/indicator/supertrendPivot_callbacks.go
Normal file
|
@ -0,0 +1,15 @@
|
|||
// Code generated by "callbackgen -type Supertrend"; DO NOT EDIT.
|
||||
|
||||
package indicator
|
||||
|
||||
import ()
|
||||
|
||||
func (inc *PivotSupertrend) OnUpdate(cb func(value float64)) {
|
||||
inc.UpdateCallbacks = append(inc.UpdateCallbacks, cb)
|
||||
}
|
||||
|
||||
func (inc *PivotSupertrend) EmitUpdate(value float64) {
|
||||
for _, cb := range inc.UpdateCallbacks {
|
||||
cb(value)
|
||||
}
|
||||
}
|
154
pkg/indicator/utBotAlert.go
Normal file
154
pkg/indicator/utBotAlert.go
Normal file
|
@ -0,0 +1,154 @@
|
|||
package indicator
|
||||
|
||||
import (
|
||||
"math"
|
||||
"time"
|
||||
|
||||
"github.com/c9s/bbgo/pkg/datatype/bools"
|
||||
"github.com/c9s/bbgo/pkg/datatype/floats"
|
||||
"github.com/c9s/bbgo/pkg/types"
|
||||
)
|
||||
|
||||
// based on "UT Bot Alerts by QuantNomad" from tradingview
|
||||
|
||||
//go:generate callbackgen -type UtBotAlert
|
||||
type UtBotAlert struct {
|
||||
types.IntervalWindow
|
||||
KeyValue float64 `json:"keyValue"` // Should be ATRMultiplier
|
||||
|
||||
Values []types.Direction
|
||||
buyValue bools.BoolSlice
|
||||
sellValue bools.BoolSlice
|
||||
|
||||
AverageTrueRange *ATR // Value must be set when initialized in strategy
|
||||
|
||||
xATRTrailingStop floats.Slice
|
||||
pos types.Direction // NB: This is currently not in use (kept in case of expanding as it is in the tradingview version)
|
||||
previousPos types.Direction // NB: This is currently not in use (kept in case of expanding as it is in the tradingview version)
|
||||
|
||||
previousClosePrice float64
|
||||
|
||||
EndTime time.Time
|
||||
UpdateCallbacks []func(value types.Direction)
|
||||
}
|
||||
|
||||
func NewUtBotAlert(iw types.IntervalWindow, keyValue float64) *UtBotAlert {
|
||||
return &UtBotAlert{
|
||||
IntervalWindow: iw,
|
||||
KeyValue: keyValue,
|
||||
AverageTrueRange: &ATR{
|
||||
IntervalWindow: iw,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (inc *UtBotAlert) Last() types.Direction {
|
||||
length := len(inc.Values)
|
||||
if length > 0 {
|
||||
return inc.Values[length-1]
|
||||
}
|
||||
return types.DirectionNone
|
||||
}
|
||||
|
||||
func (inc *UtBotAlert) Index(i int) types.Direction {
|
||||
length := inc.Length()
|
||||
if length == 0 || length-i-1 < 0 {
|
||||
return 0
|
||||
}
|
||||
return inc.Values[length-i-1]
|
||||
}
|
||||
|
||||
func (inc *UtBotAlert) Length() int {
|
||||
return len(inc.Values)
|
||||
}
|
||||
|
||||
func (inc *UtBotAlert) Update(highPrice, lowPrice, closePrice float64) {
|
||||
if inc.Window <= 0 {
|
||||
panic("window must be greater than 0")
|
||||
}
|
||||
|
||||
// Update ATR
|
||||
inc.AverageTrueRange.Update(highPrice, lowPrice, closePrice)
|
||||
|
||||
nLoss := inc.AverageTrueRange.Last() * inc.KeyValue
|
||||
|
||||
// xATRTrailingStop
|
||||
if inc.xATRTrailingStop.Length() == 0 {
|
||||
// For first run
|
||||
inc.xATRTrailingStop.Update(0)
|
||||
|
||||
} else if closePrice > inc.xATRTrailingStop.Index(1) && inc.previousClosePrice > inc.xATRTrailingStop.Index(1) {
|
||||
inc.xATRTrailingStop.Update(math.Max(inc.xATRTrailingStop.Index(1), closePrice-nLoss))
|
||||
|
||||
} else if closePrice < inc.xATRTrailingStop.Index(1) && inc.previousClosePrice < inc.xATRTrailingStop.Index(1) {
|
||||
inc.xATRTrailingStop.Update(math.Min(inc.xATRTrailingStop.Index(1), closePrice+nLoss))
|
||||
|
||||
} else if closePrice > inc.xATRTrailingStop.Index(1) {
|
||||
inc.xATRTrailingStop.Update(closePrice - nLoss)
|
||||
|
||||
} else {
|
||||
inc.xATRTrailingStop.Update(closePrice + nLoss)
|
||||
}
|
||||
|
||||
// pos
|
||||
if inc.previousClosePrice < inc.xATRTrailingStop.Index(1) && closePrice > inc.xATRTrailingStop.Index(1) {
|
||||
inc.pos = types.DirectionUp
|
||||
} else if inc.previousClosePrice > inc.xATRTrailingStop.Index(1) && closePrice < inc.xATRTrailingStop.Index(1) {
|
||||
inc.pos = types.DirectionDown
|
||||
} else {
|
||||
inc.pos = inc.previousPos
|
||||
}
|
||||
|
||||
above := closePrice > inc.xATRTrailingStop.Last() && inc.previousClosePrice < inc.xATRTrailingStop.Index(1)
|
||||
below := closePrice < inc.xATRTrailingStop.Last() && inc.previousClosePrice > inc.xATRTrailingStop.Index(1)
|
||||
|
||||
buy := closePrice > inc.xATRTrailingStop.Last() && above // buy
|
||||
sell := closePrice < inc.xATRTrailingStop.Last() && below // sell
|
||||
|
||||
inc.buyValue.Push(buy)
|
||||
inc.sellValue.Push(sell)
|
||||
|
||||
if buy {
|
||||
inc.Values = append(inc.Values, types.DirectionUp)
|
||||
} else if sell {
|
||||
inc.Values = append(inc.Values, types.DirectionDown)
|
||||
} else {
|
||||
inc.Values = append(inc.Values, types.DirectionNone)
|
||||
}
|
||||
|
||||
// Update last prices
|
||||
inc.previousClosePrice = closePrice
|
||||
inc.previousPos = inc.pos
|
||||
|
||||
}
|
||||
|
||||
// GetSignal returns signal (down, none or up)
|
||||
func (inc *UtBotAlert) GetSignal() types.Direction {
|
||||
length := len(inc.Values)
|
||||
if length > 0 {
|
||||
return inc.Values[length-1]
|
||||
}
|
||||
return types.DirectionNone
|
||||
}
|
||||
|
||||
func (inc *UtBotAlert) PushK(k types.KLine) {
|
||||
if inc.EndTime != zeroTime && !k.EndTime.After(inc.EndTime) {
|
||||
return
|
||||
}
|
||||
|
||||
inc.Update(k.GetHigh().Float64(), k.GetLow().Float64(), k.GetClose().Float64())
|
||||
inc.EndTime = k.EndTime.Time()
|
||||
inc.EmitUpdate(inc.Last())
|
||||
|
||||
}
|
||||
|
||||
func (inc *UtBotAlert) BindK(target KLineClosedEmitter, symbol string, interval types.Interval) {
|
||||
target.OnKLineClosed(types.KLineWith(symbol, interval, inc.PushK))
|
||||
}
|
||||
|
||||
// LoadK calculates the initial values
|
||||
func (inc *UtBotAlert) LoadK(allKLines []types.KLine) {
|
||||
for _, k := range allKLines {
|
||||
inc.PushK(k)
|
||||
}
|
||||
}
|
17
pkg/indicator/utBotAlert_callbacks.go
Normal file
17
pkg/indicator/utBotAlert_callbacks.go
Normal file
|
@ -0,0 +1,17 @@
|
|||
// Code generated by "callbackgen -type Supertrend"; DO NOT EDIT.
|
||||
|
||||
package indicator
|
||||
|
||||
import (
|
||||
"github.com/c9s/bbgo/pkg/types"
|
||||
)
|
||||
|
||||
func (inc *UtBotAlert) OnUpdate(cb func(value types.Direction)) {
|
||||
inc.UpdateCallbacks = append(inc.UpdateCallbacks, cb)
|
||||
}
|
||||
|
||||
func (inc *UtBotAlert) EmitUpdate(value types.Direction) {
|
||||
for _, cb := range inc.UpdateCallbacks {
|
||||
cb(value)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user