-
Notifications
You must be signed in to change notification settings - Fork 0
/
events.go
313 lines (262 loc) · 6.23 KB
/
events.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package backtest
import (
"time"
"github.com/shopspring/decimal"
)
// EventHandler declares the basic event interface
type EventHandler interface {
IsEvent() bool
Timer
Symboler
}
// Timer declares the timer interface
type Timer interface {
GetTime() time.Time
}
// Symboler declares the symboler interface
type Symboler interface {
GetSymbol() string
}
// Event is the implementation of the basic event interface.
type Event struct {
Time time.Time
Symbol string
}
// IsEvent declares an event interface implementation.
func (e Event) IsEvent() bool {
return true
}
// GetTime returns the Time of an event
func (e Event) GetTime() time.Time {
return e.Time
}
// GetSymbol returns the symbol string of the event
func (e Event) GetSymbol() string {
return e.Symbol
}
// DataEventHandler declares a data event interface
type DataEventHandler interface {
EventHandler
IsDataEvent() bool
LatestPrice() float64
}
// DataEvent is the basic implementation of a data event handler.
type DataEvent struct {
Metrics map[string]float64
}
// IsDataEvent declares a data event
func (d DataEvent) IsDataEvent() bool {
return true
}
// BarEvent declares a bar event interface.
type BarEvent interface {
DataEventHandler
IsBar() bool
}
type BarData struct {
Time int `json:"time"`
Open float64 `json:"open"`
Close float64 `json:"close"`
Low float64 `json:"low"`
High float64 `json:"high"`
Volume float64 `json:"volume"`
}
// Bar declares an event for an OHLCV bar (Open, High, Low, Close, Volume).
type Bar struct {
Event
// BarEvent
BarData // Should this be treated as pointer ?? *
// Open float64
// High float64
// Low float64
// Close float64
// Volume int64
}
// IsBar declares a Bar event
func (b Bar) IsBar() bool {
return true
}
// func (b Bar) IsEvent() bool {
// return true
// }
func (b Bar) IsDataEvent() bool {
return true
}
// LatestPrice returns the close proce of the bar event.
func (b Bar) LatestPrice() float64 {
return b.Close
}
// TickEvent declares a tick event interface.
type TickEvent interface {
DataEventHandler
IsTick() bool
}
// Tick declares an tick event
type Tick struct {
Event
DataEvent
Bid float64
Ask float64
}
// IsTick declares a tick event
func (t Tick) IsTick() bool {
return true
}
// LatestPrice returns the middle of Bid and Ask.
func (t Tick) LatestPrice() float64 {
bid := decimal.NewFromFloat(t.Bid)
ask := decimal.NewFromFloat(t.Ask)
diff := decimal.New(2, 0)
latest, _ := bid.Add(ask).Div(diff).Round(DP).Float64()
return latest
}
// SignalEvent declares the signal event interface.
type SignalEvent interface {
EventHandler
Directioner
IsSignal() bool
}
// Signal declares a basic signal event
type Signal struct {
Event
Direction string // long or short
}
// IsSignal implements the Signal interface.
func (s Signal) IsSignal() bool {
return true
}
// SetDirection sets the Directions field of a Signal
func (s *Signal) SetDirection(st string) {
s.Direction = st
}
// GetDirection returns the Direction of a Signal
func (s Signal) GetDirection() string {
return s.Direction
}
// OrderEvent declares the order event interface.
type OrderEvent interface {
EventHandler
Directioner
Quantifier
IsOrder() bool
}
// Directioner defines a direction interface
type Directioner interface {
SetDirection(string)
GetDirection() string
}
// Quantifier defines a qty interface
type Quantifier interface {
SetQty(float64)
GetQty() float64
}
// Order declares a basic order event
type Order struct {
Event
Direction string // buy or sell
Qty float64 // quantity of the order
OrderType string // market or limit
Limit float64 // limit for the order
}
// IsOrder declares an order event.
func (o Order) IsOrder() bool {
return true
}
// SetDirection sets the Directions field of an Order
func (o *Order) SetDirection(s string) {
o.Direction = s
}
// GetDirection returns the Direction of an Order
func (o Order) GetDirection() string {
return o.Direction
}
// SetQty sets the Qty field of an Order
func (o *Order) SetQty(i float64) {
o.Qty = i
}
// GetQty returns the Qty field of an Order
func (o Order) GetQty() float64 {
return o.Qty
}
// FillEvent declares the fill event interface.
type FillEvent interface {
EventHandler
Directioner
Quantifier
IsFill() bool
GetPrice() float64
GetCommission() float64
GetExchangeFee() float64
GetCost() float64
Value() float64
NetValue() float64
}
// Fill declares a basic fill event
type Fill struct {
Event
Exchange string // exchange symbol
Direction string // BOT for buy or SLD for sell
Qty float64
Price float64
Commission float64
ExchangeFee float64
Cost float64 // the total cost of the filled order incl commission and fees
}
// IsFill declares a fill event.
func (f Fill) IsFill() bool {
return true
}
// SetDirection sets the Directions field of a Fill
func (f *Fill) SetDirection(s string) {
f.Direction = s
}
// GetDirection returns the direction of a Fill
func (f Fill) GetDirection() string {
return f.Direction
}
// SetQty sets the Qty field of a Fill
func (f *Fill) SetQty(i float64) {
f.Qty = i
}
// GetQty returns the qty field of a fill
func (f Fill) GetQty() float64 {
return f.Qty
}
// GetPrice returns the Price field of a fill
func (f Fill) GetPrice() float64 {
return f.Price
}
// GetCommission returns the Commission field of a fill.
func (f Fill) GetCommission() float64 {
return f.Commission
}
// GetExchangeFee returns the ExchangeFee Field of a fill
func (f Fill) GetExchangeFee() float64 {
return f.ExchangeFee
}
// GetCost returns the Cost field of a Fill
func (f Fill) GetCost() float64 {
return f.Cost
}
// Value returns the value without cost.
func (f Fill) Value() float64 {
qty := decimal.NewFromFloat(f.Qty)
price := decimal.NewFromFloat(f.Price)
value, _ := qty.Mul(price).Round(DP).Float64()
return value
}
// NetValue returns the net value including cost.
func (f Fill) NetValue() float64 {
qty := decimal.NewFromFloat(f.Qty)
price := decimal.NewFromFloat(f.Price)
cost := decimal.NewFromFloat(f.Cost)
if f.Direction == "BOT" {
// qty * price + cost
netValue, _ := qty.Mul(price).Add(cost).Round(DP).Float64()
return netValue
}
// SLD
//qty * price - cost
netValue, _ := qty.Mul(price).Sub(cost).Round(DP).Float64()
return netValue
}