-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.go
161 lines (135 loc) · 3.97 KB
/
data.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
package backtest
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"sort"
"github.com/ivtpz/utils"
)
// DataHandler is the combined data interface
type DataHandler interface {
DataLoader
DataStreamer
Reseter
}
// DataLoader is the interface loading the data into the data stream
type DataLoader interface {
Load(string, string, string, string) error
}
// DataStreamer is the interface returning the data streams
type DataStreamer interface {
Next() (DataEventHandler, bool)
Stream() []DataEventHandler
History() []DataEventHandler
Latest(string) DataEventHandler
List(string) []DataEventHandler
}
// Data is a basic data struct
type Data struct {
latest map[string]DataEventHandler
list map[string][]DataEventHandler
stream []DataEventHandler
streamHistory []DataEventHandler
}
// Load loads data endpoints into a stream.
// This method satisfies the DataLoeder interface, but should be overwritten
// by the specific data loading implamentation.
func (d *Data) Load(exchange string, currPair, start string, end string) error {
s := utils.StringToUnix(start)
e := utils.StringToUnix(end)
fmt.Println(s)
fmt.Println(e)
resp, err := http.Get(fmt.Sprintf("http://192.168.99.100:32368/api/history/%s/%s/%d/%d/%d", exchange, currPair, s, e, 300))
if err != nil {
return err
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
var arr []BarData
json.Unmarshal(body, &arr)
d.SetStream(arrToDataEventHandler(arr, currPair))
d.SortStream()
return nil
}
func arrToDataEventHandler(arr []BarData, symbol string) []DataEventHandler {
var data []DataEventHandler
for _, item := range arr {
event := Event{Time: utils.UnixToTime(item.Time), Symbol: symbol}
deh := Bar{event, item}
data = append(data, deh)
}
return data
}
// Reset implements the Reseter interface and rests the data struct to a clean state with loaded data points
func (d *Data) Reset() {
d.latest = nil
d.list = nil
d.stream = d.streamHistory
d.streamHistory = nil
}
// SetStream sets the data stream
func (d *Data) SetStream(stream []DataEventHandler) {
d.stream = stream
}
// Stream returns the data stream
func (d *Data) Stream() []DataEventHandler {
return d.stream
}
// Next returns the first element of the data stream
// deletes it from the stream and appends it to history
func (d *Data) Next() (dh DataEventHandler, ok bool) {
// check for element in datastream
if len(d.stream) == 0 {
return dh, false
}
dh = d.stream[0]
d.stream = d.stream[1:] // delete first element from stream
d.streamHistory = append(d.streamHistory, dh)
// update list of current data events
d.updateLatest(dh)
// update list of data events for single symbol
d.updateList(dh)
return dh, true
}
// History returns the historic data stream
func (d *Data) History() []DataEventHandler {
return d.streamHistory
}
// Latest returns the last known data event for a symbol.
func (d *Data) Latest(symbol string) DataEventHandler {
return d.latest[symbol]
}
// List returns the data event list for a symbol.
func (d *Data) List(symbol string) []DataEventHandler {
return d.list[symbol]
}
// SortStream sorts the dataStream
func (d *Data) SortStream() {
sort.Slice(d.stream, func(i, j int) bool {
b1 := d.stream[i]
b2 := d.stream[j]
// if date is equal sort by symbol
if b1.GetTime().Equal(b2.GetTime()) {
return b1.GetSymbol() < b2.GetSymbol()
}
// else sort by date
return b1.GetTime().Before(b2.GetTime())
})
}
// updateLatest puts the last current data event to the current list.
func (d *Data) updateLatest(event DataEventHandler) {
// check for nil map, else initialise the map
if d.latest == nil {
d.latest = make(map[string]DataEventHandler)
}
d.latest[event.GetSymbol()] = event
}
// updateList appends an event to the data list.
func (d *Data) updateList(event DataEventHandler) {
// Check for nil map, else initialise the map
if d.list == nil {
d.list = make(map[string][]DataEventHandler)
}
d.list[event.GetSymbol()] = append(d.list[event.GetSymbol()], event)
}