-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstat_request_options.go
137 lines (122 loc) · 3.73 KB
/
stat_request_options.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
package moexiss
import (
"net/url"
"strconv"
"strings"
)
// TradingSession represents a type of trading sessions for intermediate day summary
// of MoEx ISS API
type TradingSession uint8
// These constants represent possible values of TradingSession
const (
TradingSessionUndefined TradingSession = 0
TradingSessionMain TradingSession = 1
TradingSessionAdditional TradingSession = 2
TradingSessionTotal TradingSession = 3
)
// String representations of TradingSession values
func (ts TradingSession) String() string {
return strconv.Itoa(int(ts))
}
// getTradingSession returns TradingSession from a passed string
func getTradingSession(value string) TradingSession {
switch value {
case "1":
return TradingSessionMain
case "2":
return TradingSessionAdditional
case "3":
return TradingSessionTotal
default:
return TradingSessionUndefined
}
}
// StatRequestOptions contains options which can be used as arguments
// for building requests to get intermediate day summary.
// MoEx ISS API docs: https://iss.moex.com/iss/reference/823
type StatRequestOptions struct {
TradingSessionType TradingSession // `tradingsession` query parameter in url.URL
TickerIds []string // `securities` query parameter in url.URL
BoardId []string // `boardid` query parameter in url.URL
}
// StatReqOptionsBuilder represents a builder of StatRequestOptions struct
type StatReqOptionsBuilder struct {
options *StatRequestOptions
}
// NewStatReqOptionsBuilder is a constructor of StatReqOptionsBuilder
func NewStatReqOptionsBuilder() *StatReqOptionsBuilder {
return &StatReqOptionsBuilder{options: &StatRequestOptions{}}
}
// Build builds StatRequestOptions from StatReqOptionsBuilder
func (b *StatReqOptionsBuilder) Build() *StatRequestOptions {
return b.options
}
// TypeTradingSession sets a type of trading session parameter to a request
// It allows to show data only for the required session.
func (b *StatReqOptionsBuilder) TypeTradingSession(ts TradingSession) *StatReqOptionsBuilder {
b.options.TradingSessionType = ts
return b
}
// AddTicker adds a ticker to a request
// It allows to show data only for the required tickers.
// No more than 10 tickers.
func (b *StatReqOptionsBuilder) AddTicker(ticker string) *StatReqOptionsBuilder {
b.options.TickerIds = append(b.options.TickerIds, ticker)
return b
}
// AddBoard adds a board to a request
// Filter the output by trading mode.
// No more than 10 boards.
func (b *StatReqOptionsBuilder) AddBoard(boardId string) *StatReqOptionsBuilder {
b.options.BoardId = append(b.options.BoardId, boardId)
return b
}
// addStatRequestOptions sets parameters into *url.URL
// from StatRequestOptions struct and returns it back
func addStatRequestOptions(url *url.URL, options *StatRequestOptions) *url.URL {
q := url.Query()
q.Set("iss.meta", "off")
q.Set("iss.json", "extended")
if options == nil {
url.RawQuery = q.Encode()
return url
}
trType := options.TradingSessionType
if trType != TradingSessionUndefined &&
(trType == TradingSessionMain ||
trType == TradingSessionAdditional ||
trType == TradingSessionTotal) {
q.Set("tradingsession", trType.String())
}
limit := 10
if len(options.TickerIds) > 0 {
addArrayParams(&q, "securities", options.TickerIds, limit)
}
if len(options.BoardId) > 0 {
addArrayParams(&q, "boardid", options.BoardId, limit)
}
url.RawQuery = q.Encode()
return url
}
func addArrayParams(q *url.Values, key string, values []string, limit int) {
bld := strings.Builder{}
counter := 0
for _, value := range values {
if value == "" {
continue
}
counter++
if counter > limit {
break
}
if counter > 1 {
bld.WriteString(",")
}
bld.WriteString(value)
}
if bld.Len() == 0 {
return
}
str := bld.String()
q.Set(key, str)
}