-
-
Notifications
You must be signed in to change notification settings - Fork 283
/
utBotAlert.go
154 lines (120 loc) · 4.21 KB
/
utBotAlert.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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(0) * inc.KeyValue
// xATRTrailingStop
if inc.xATRTrailingStop.Length() == 0 {
// For first run
inc.xATRTrailingStop.Update(0)
} else if closePrice > inc.xATRTrailingStop.Last(1) && inc.previousClosePrice > inc.xATRTrailingStop.Last(1) {
inc.xATRTrailingStop.Update(math.Max(inc.xATRTrailingStop.Last(1), closePrice-nLoss))
} else if closePrice < inc.xATRTrailingStop.Last(1) && inc.previousClosePrice < inc.xATRTrailingStop.Last(1) {
inc.xATRTrailingStop.Update(math.Min(inc.xATRTrailingStop.Last(1), closePrice+nLoss))
} else if closePrice > inc.xATRTrailingStop.Last(1) {
inc.xATRTrailingStop.Update(closePrice - nLoss)
} else {
inc.xATRTrailingStop.Update(closePrice + nLoss)
}
// pos
if inc.previousClosePrice < inc.xATRTrailingStop.Last(1) && closePrice > inc.xATRTrailingStop.Last(1) {
inc.pos = types.DirectionUp
} else if inc.previousClosePrice > inc.xATRTrailingStop.Last(1) && closePrice < inc.xATRTrailingStop.Last(1) {
inc.pos = types.DirectionDown
} else {
inc.pos = inc.previousPos
}
above := closePrice > inc.xATRTrailingStop.Last(0) && inc.previousClosePrice < inc.xATRTrailingStop.Last(1)
below := closePrice < inc.xATRTrailingStop.Last(0) && inc.previousClosePrice > inc.xATRTrailingStop.Last(1)
buy := closePrice > inc.xATRTrailingStop.Last(0) && above // buy
sell := closePrice < inc.xATRTrailingStop.Last(0) && 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)
}
}