-
Notifications
You must be signed in to change notification settings - Fork 0
/
history.go
217 lines (199 loc) · 5.66 KB
/
history.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
/*
This module contains the definition of History, that is the basic
representation for price and volume time series.
*/
package yfgo
import (
_ "database/sql"
"fmt"
_ "log"
"math"
"encoding/json"
"reflect"
"strconv"
"strings"
"time"
"github.com/jailop/yfgo/arrayops"
_ "github.com/marcboeker/go-duckdb"
)
/**
Represent an Stock Price/Volume history.
Every field is a array. All the fields
will have the same size.
*/
type History struct {
Time []int64 `json:"time"`
Open []float64 `json:"open"`
Low []float64 `json:"low"`
High []float64 `json:"high"`
Close []float64 `json:"close"`
Volume []int64 `json:"volume"`
}
/**
This structure is used to represent
a point on time from a History object.
*/
type HistoryRecord struct {
Time int64
Open float64
Low float64
High float64
Close float64
Volume int64
}
func (history *History) Append(record HistoryRecord) {
history.Time = append(history.Time, record.Time)
history.Open = append(history.Open, record.Open)
history.Low = append(history.Low, record.Low)
history.High = append(history.High, record.High)
history.Close = append(history.Close, record.Close)
history.Volume = append(history.Volume, record.Volume)
}
func (history *History) AppendFromHistory(another History, idx int) {
history.Time = append(history.Time, another.Time[idx])
history.Open = append(history.Open, another.Open[idx])
history.Low = append(history.Low, another.Low[idx])
history.High = append(history.High, another.High[idx])
history.Close = append(history.Close, another.Close[idx])
history.Volume = append(history.Volume, another.Volume[idx])
}
func (history *History) AppendHistory(another History) {
for idx := range len(another.Time) {
history.AppendFromHistory(another, idx)
}
}
func (history *History) FillMissed() {
history.Open = FillMissed(history.Open, 0.0)
history.Low = FillMissed(history.Low, 0.0)
history.High = FillMissed(history.High, 0.0)
history.Close = FillMissed(history.Close, 0.0)
}
func (history History) Print() {
for i := range len(history.Time) {
fmt.Printf("%14s %10.2f %10.2f %10.2f %10.2f %10d\n",
time.Unix(history.Time[i], 0).UTC(),
history.Open[i],
history.Low[i],
history.High[i],
history.Close[i],
history.Volume[i],
)
}
}
func copyArray[T int64 | float64](values []T) []T {
ret := make([]T, len(values))
for i := range len(values) {
ret[i] = values[i]
}
return ret
}
func (history History) Clone() History {
return History{
copyArray(history.Time),
copyArray(history.Open),
copyArray(history.Low),
copyArray(history.High),
copyArray(history.Close),
copyArray(history.Volume),
}
}
func GenerateHistoryFromParsedJSON(body []byte) (History, error) {
content, err := ParseJSON(body)
if err != nil {
fmt.Println(err)
return History{}, err
}
return History{
content.Chart.Result[0].TimeStamp,
NaNZeroPrices(content.Chart.Result[0].Indicators.Quote[0].Open),
NaNZeroPrices(content.Chart.Result[0].Indicators.Quote[0].Low),
NaNZeroPrices(content.Chart.Result[0].Indicators.Quote[0].High),
NaNZeroPrices(content.Chart.Result[0].Indicators.Quote[0].Close),
content.Chart.Result[0].Indicators.Quote[0].Volume,
}, nil
}
// Retrieves data from Yahoo Finance API
// @param symbol: Ticker symbol
// @param start_time: seconds from the epoch
// @param end_time: seconds from the epoch
// @returns An History object or error
func GetHistory(symbol string, start_time int64, end_time int64) (History, error) {
params := []QueryParam{
{Name: "interval", Value: "1m"},
{Name: "period1", Value: strconv.FormatInt(start_time, 10)},
{Name: "period2", Value: strconv.FormatInt(end_time, 10)},
}
baseURL := "https://query2.finance.yahoo.com/v8/finance/chart"
url := MakeURL(baseURL, symbol, params)
body, err := RetrieveJSON(url)
if err != nil {
return History{}, err
}
history, err := GenerateHistoryFromParsedJSON(body)
if err != nil {
return History{}, err
}
return history, nil
}
func (history History) Sort() History {
n := len(history.Time)
time_ := make([]int64, n)
open_ := make([]float64, n)
low_ := make([]float64, n)
high_ := make([]float64, n)
close_ := make([]float64, n)
volume_ := make([]int64, n)
indices := arrayops.SortedIndices(history.Time)
for i, pos := range indices {
time_[i] = history.Time[pos]
open_[i] = history.Open[pos]
low_[i] = history.Low[pos]
high_[i] = history.High[pos]
close_[i] = history.Close[pos]
volume_[i] = history.Volume[pos]
}
return History{time_, open_, low_, high_, close_, volume_}
}
func (history History) ToJSON() ([]byte, error) {
return json.Marshal(history)
}
func (history History) Tail(n int) History {
history_size := len(history.Time)
size := n
if size > history_size {
size = history_size
}
offset := history_size - size
return History {
history.Time[offset:],
history.Open[offset:],
history.Low[offset:],
history.High[offset:],
history.Close[offset:],
history.Volume[offset:],
}
}
func NaNZeroPrices(prices []float64) []float64 {
modPrices := make([]float64, len(prices))
for i := range prices {
if prices[i] == 0 {
modPrices[i] = math.NaN()
} else {
modPrices[i] = prices[i]
}
}
return modPrices
}
func InsertStatement() string {
h := new(History)
query := "INSERT INTO history (symbol,"
questionMarks := "?,"
fields := reflect.TypeOf(*h).NumField()
for i := range fields {
query += strings.ToLower(reflect.TypeOf(*h).Field(i).Name) + ","
questionMarks += "?,"
}
query = query[:len(query)-1] + ") VALUES ("
query += questionMarks[:len(questionMarks)-1] + ")"
return query
}