-
Notifications
You must be signed in to change notification settings - Fork 8
/
candle.go
81 lines (71 loc) · 1.82 KB
/
candle.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
package tickstore
import (
"fmt"
"time"
)
// CandleData represents OHLC candle data
type CandleData struct {
InstrumentToken uint32
TimeStamp time.Time
Open float64
High float64
Low float64
Close float64
}
// Candles is an array of CandleData
type Candles []CandleData
// Creates OHLC candle from tickdata
func (c *Client) FetchCandle(instrumentToken int, startTime time.Time, endTime time.Time) (Candles, error) {
startT := startTime.Format("2006-01-02 15:04:05")
endT := endTime.Format("2006-01-02 15:04:05")
// DB query to calculate OHLC between StartTime and EndTime for given instrument_token based on tickdata
candleQueryStmt := fmt.Sprintf(`SELECT
instrument_token,
time_minute,
groupArray(price)[1] AS open,
max(price) AS high,
min(price) AS low,
groupArray(price)[-1] AS close
FROM
(
SELECT
instrument_token,
toStartOfMinute(timestamp) AS time_minute,
price
FROM tickdata
WHERE (instrument_token = %d) AND
(timestamp >= '%s') AND
(timestamp <= '%s')
)
GROUP BY (instrument_token, time_minute)
ORDER BY time_minute ASC`, instrumentToken, startT, endT)
rows, err := c.dbClient.Query(candleQueryStmt)
if err != nil {
return nil, err
}
defer rows.Close()
var candleArray Candles
for rows.Next() {
var (
token uint32
time_minute time.Time
open float64
high float64
low float64
close float64
)
if err := rows.Scan(&token, &time_minute, &open, &high, &low, &close); err != nil {
return nil, err
}
candle := CandleData{
InstrumentToken: token,
TimeStamp: time_minute,
Open: open,
High: high,
Low: low,
Close: close,
}
candleArray = append(candleArray, candle)
}
return candleArray, nil
}