-
Notifications
You must be signed in to change notification settings - Fork 129
/
stockQuotation.go
161 lines (143 loc) · 4.98 KB
/
stockQuotation.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 models
import (
"encoding/json"
"fmt"
"time"
clientInfluxdb "github.com/influxdata/influxdb1-client/v2"
)
// SetStockQuotationInflux stores a stock quotation to an influx batch.
func (datastore *DB) SetStockQuotation(sq StockQuotation) error {
fields := map[string]interface{}{
"priceAsk": sq.PriceAsk,
"priceBid": sq.PriceBid,
"sizeAsk": sq.SizeAskLot,
"sizeBid": sq.SizeBidLot,
"source": sq.Source,
}
tags := map[string]string{
"symbol": sq.Symbol,
"name": sq.Name,
"isin": sq.ISIN,
}
pt, err := clientInfluxdb.NewPoint(influxDbStockQuotationsTable, tags, fields, sq.Time)
if err != nil {
log.Errorln("NewOptionInflux:", err)
} else {
datastore.addPoint(pt)
}
err = datastore.WriteBatchInflux()
if err != nil {
log.Errorln("Write influx batch: ", err)
}
return err
}
// GetStockQuotationInflux returns the last quotation of @symbol before @timestamp.
func (db *DB) GetStockQuotation(source string, symbol string, timeInit time.Time, timeFinal time.Time) ([]StockQuotation, error) {
stockQuotations := []StockQuotation{}
unixtimeInit := timeInit.UnixNano()
unixtimeFinal := timeFinal.UnixNano()
query := "SELECT priceAsk,priceBid,sizeAsk,sizeBid,source,\"isin\",\"name\" FROM %s WHERE source='%s' and \"symbol\"='%s' and time>%d and time<=%d order by time desc"
q := fmt.Sprintf(query, influxDbStockQuotationsTable, source, symbol, unixtimeInit, unixtimeFinal)
res, err := queryInfluxDB(db.influxClient, q)
if err != nil {
fmt.Println("Error querying influx")
return stockQuotations, err
}
if len(res) > 0 && len(res[0].Series) > 0 {
layout := "2006-01-02T15:04:05Z"
vals := res[0].Series[0].Values
for i := 0; i < len(vals); i++ {
var stockQuotation StockQuotation
stockQuotation.Time, err = time.Parse(layout, vals[i][0].(string))
if err != nil {
log.Error(err)
}
stockQuotation.PriceAsk, err = vals[i][1].(json.Number).Float64()
if err != nil {
log.Error(err)
}
stockQuotation.PriceBid, err = vals[i][2].(json.Number).Float64()
if err != nil {
log.Error(err)
}
stockQuotation.SizeAskLot, err = vals[i][3].(json.Number).Float64()
if err != nil {
log.Error(err)
}
stockQuotation.SizeBidLot, err = vals[i][4].(json.Number).Float64()
if err != nil {
log.Error(err)
}
stockQuotation.Source = vals[i][5].(string)
stockQuotation.ISIN = vals[i][6].(string)
stockQuotation.Name = vals[i][7].(string)
stockQuotation.Symbol = symbol
stockQuotations = append(stockQuotations, stockQuotation)
}
return stockQuotations, nil
}
return stockQuotations, err
}
// GetStockSymbols returns all symbols available from @source.
func (db *DB) GetStockSymbols() (map[Stock]string, error) {
allStocks := make(map[Stock]string)
q := fmt.Sprintf("SELECT \"symbol\",\"name\",\"isin\",source FROM %s WHERE time>now()-7d", influxDbStockQuotationsTable)
res, err := queryInfluxDB(db.influxClient, q)
if err != nil {
log.Error("query stock symbols from influx: ", err)
return allStocks, err
}
if len(res) > 0 && len(res[0].Series) > 0 {
// make unique list of stocks - i.e. pairs (isin,source) should be unique.
vals := res[0].Series[0].Values
set := make(map[string]struct{})
for _, val := range vals {
if _, ok := set[val[3].(string)+val[4].(string)]; !ok {
allStocks[Stock{
Symbol: val[1].(string),
Name: val[2].(string),
ISIN: val[3].(string),
}] = val[4].(string)
set[val[3].(string)+val[4].(string)] = struct{}{}
}
}
}
return allStocks, nil
}
// func (db *DB) GetStockQuotfation(starttime time.Time, endtime time.Time, asset string, protocol string) ([]StockQuotation, error) {
// retval := []dia.DefiRate{}
// influxQuery := "SELECT \"asset\",borrowRate,lendingRate,\"protocol\" FROM %s WHERE time > %d and time < %d and asset = '%s' and protocol = '%s'"
// q := fmt.Sprintf(influxQuery, influxDbDefiRateTable, starttime.UnixNano(), endtime.UnixNano(), asset, protocol)
// fmt.Println("influx query: ", q)
// res, err := queryInfluxDB(db.influxClient, q)
// fmt.Println("res, err: ", res, err)
// if err != nil {
// return retval, err
// }
// if len(res) > 0 && len(res[0].Series) > 0 {
// for i := 0; i < len(res[0].Series[0].Values); i++ {
// currentRate := dia.DefiRate{}
// currentRate.Timestamp, err = time.Parse(time.RFC3339, res[0].Series[0].Values[i][0].(string))
// if err != nil {
// return retval, err
// }
// currentRate.Asset = res[0].Series[0].Values[i][1].(string)
// if err != nil {
// return retval, err
// }
// currentRate.BorrowingRate, err = res[0].Series[0].Values[i][2].(json.Number).Float64()
// if err != nil {
// return retval, err
// }
// currentRate.LendingRate, err = res[0].Series[0].Values[i][3].(json.Number).Float64()
// if err != nil {
// return retval, err
// }
// currentRate.Protocol = protocol
// retval = append(retval, currentRate)
// }
// } else {
// return retval, errors.New("Error parsing Defi Lending Rate from Database")
// }
// return retval, nil
// }