-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnse.go
457 lines (402 loc) · 11.3 KB
/
nse.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
package nse
import (
"bytes"
"compress/gzip"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"time"
"github.com/golang/glog"
)
const (
kOcBankNifty = "BANKNIFTY"
kOcBankNiftyStep = 100
kOcNifty = "NIFTY"
kOcNiftyStep = 50
kOcFinNifty = "FINNIFTY"
kOcFinNiftyStep = 50
kOcFiltered = "filtered"
kOcFilteredData = "data"
kOcRecords = "records"
kOcRecordsExpiryDates = "expiryDates"
kOcRecordsTimestamp = "timestamp"
kOcRecordsUnderlyingValue = "underlyingValue"
kOcRecordsStrikePrices = "strikePrices"
kOcRecordsDataExpiryDate = "expiryDate"
kOcRecordsDataStrikePrice = "strikePrice"
kOcRecordsDataPe = "PE"
kOcRecordsDataCe = "CE"
kOcRowOpenInterest = "openInterest"
kOcRowChangeinOpenInterest = "changeinOpenInterest"
kOcRowLastPrice = "lastPrice"
kOcRowTotalTradedVolume = "totalTradedVolume"
)
type NseResponse struct {
respBuf *bytes.Buffer
}
func NewNseResponse(buf *bytes.Buffer) *NseResponse {
return &NseResponse{
respBuf: buf,
}
}
func (self *NseResponse) ResponseBuffer() *bytes.Buffer {
return self.respBuf
}
type NseOcResponse struct {
symbol string
fetchedJson map[string]interface{}
step int32
}
func NewNseOcResponse(
symbol string,
resp map[string]interface{}) *NseOcResponse {
return &NseOcResponse{
symbol: symbol,
fetchedJson: resp,
step: 0,
}
}
func (self *NseOcResponse) SetOptionStep(step int32) {
self.step = step
}
func (self *NseOcResponse) parseRecords() (map[string]interface{}, error) {
recordInt, ok := self.fetchedJson[kOcRecords]
if !ok {
msg := fmt.Sprintf("Parsing OC failed. Field %s not found.", kOcRecords)
glog.Error(msg)
return map[string]interface{}{}, errors.New(msg)
}
records, ok := recordInt.(map[string]interface{})
if !ok {
msg := fmt.Sprintf("Parsing OC failed. Incorrect field %s type.",
kOcRecords)
glog.Error(msg)
return map[string]interface{}{}, errors.New(msg)
}
return records, nil
}
func (self *NseOcResponse) ExpiryDates() ([]string, error) {
records, err := self.parseRecords()
if err != nil {
return []string{}, err
}
arrayStrInt, err := getArrayField(records, kOcRecordsExpiryDates)
if err != nil {
return []string{}, err
}
return convertToStringSlice(arrayStrInt), nil
}
func (self *NseOcResponse) Timestamp() (string, error) {
records, err := self.parseRecords()
if err != nil {
return "", err
}
return getStrField(records, kOcRecordsTimestamp)
}
func (self *NseOcResponse) UnderlyingValue() (float64, error) {
records, err := self.parseRecords()
if err != nil {
return 0, err
}
return getFloat64Field(records, kOcRecordsUnderlyingValue)
}
func (self *NseOcResponse) parseFiltered() (map[string]interface{}, error) {
recordInt, ok := self.fetchedJson[kOcRecords]
if !ok {
msg := fmt.Sprintf("Parsing OC failed. Field %s not found.", kOcRecords)
glog.Error(msg)
return map[string]interface{}{}, errors.New(msg)
}
records, ok := recordInt.(map[string]interface{})
if !ok {
msg := fmt.Sprintf("Parsing OC failed. Incorrect field %s type.",
kOcRecords)
glog.Error(msg)
return map[string]interface{}{}, errors.New(msg)
}
return records, nil
}
func (self *NseOcResponse) FilteredData() ([]interface{}, error) {
records, err := self.parseFiltered()
if err != nil {
return []interface{}{}, err
}
return getArrayField(records, kOcFilteredData)
}
func (self *NseOcResponse) stringExists(arr []string, target string) bool {
for _, str := range arr {
if str == target {
return true
}
}
return false
}
func (self *NseOcResponse) GetExpiryOc(
symbol string,
expiryDate string) (*NseOc, error) {
availableExpiries, err := self.ExpiryDates()
if err != nil {
glog.Error("Failed to fetch available expiry dates for option chain.")
return nil, err
}
if !self.stringExists(availableExpiries, expiryDate) {
msg := fmt.Sprintf("No option chain for expiry=%s.", expiryDate)
glog.Error(msg)
glog.Error(availableExpiries)
return nil, errors.New(msg)
}
timestamp, err := self.Timestamp()
if err != nil {
msg := fmt.Sprintf("Failed to fetch timestamp.")
glog.Error(msg)
return nil, errors.New(msg)
}
underlyingValue, err := self.UnderlyingValue()
if err != nil {
msg := fmt.Sprintf("Failed to fetch underlying asset value.")
glog.Error(msg)
return nil, errors.New(msg)
}
recordsDataInt, err := self.FilteredData()
if err != nil {
msg := fmt.Sprintf("Failed to fetch option chain data records.")
glog.Error(msg)
return nil, errors.New(msg)
}
expiryDataRecords, err :=
self.getExpiryDataRecords(expiryDate, recordsDataInt)
if err != nil {
msg := fmt.Sprintf("Failed to fetch data records with error=%s", err)
glog.Error(msg)
return nil, err
}
oc := NewNseOc(symbol, expiryDate, timestamp, underlyingValue)
oc.SetOcDataRecords(expiryDataRecords)
return oc, nil
}
func (self *NseOcResponse) getExpiryDataRecords(
expiryDate string,
recordsDataInt []interface{}) ([]map[string]interface{}, error) {
expiryDataRecords := []map[string]interface{}{}
for _, recordInt := range recordsDataInt {
dataRecord, ok := recordInt.(map[string]interface{})
if !ok {
msg := fmt.Sprintf("Unexpected data record.")
return []map[string]interface{}{}, errors.New(msg)
}
dataRecordExpiryDate, err :=
getStrField(dataRecord, kOcRecordsDataExpiryDate)
if err != nil {
msg := fmt.Sprintf("Failed to fetch expiry data from data records.")
return []map[string]interface{}{}, errors.New(msg)
}
if dataRecordExpiryDate != expiryDate {
continue
}
expiryDataRecords = append(expiryDataRecords, dataRecord)
}
return expiryDataRecords, nil
}
type NSE struct {
fetchCookie bool
cookie *http.Cookie
urlOc string
urlIndex string
fnoParticipantOiUrlPreix string
session *http.Client
cookies map[string]string
headers map[string]string
}
func NewNSE() *NSE {
return &NSE{
fetchCookie: true,
cookie: nil,
urlOc: "https://www.nseindia.com/option-chain",
urlIndex: "https://www.nseindia.com/api/option-chain-indices?symbol=",
fnoParticipantOiUrlPreix: "https://archives.nseindia.com/content/nsccl/fao_participant_oi_",
session: &http.Client{},
cookies: make(map[string]string),
headers: map[string]string{
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36",
"accept-language": "en,gu;q=0.9,hi;q=0.8",
"accept-encoding": "gzip",
},
}
}
func (self *NSE) FetchCookie() {
urlStr := self.urlOc
req, _ := http.NewRequest("GET", urlStr, nil)
for k, v := range self.headers {
req.Header.Set(k, v)
}
glog.Info("Fetching URL ", urlStr)
resp, err := self.session.Do(req)
if err != nil {
msg := fmt.Sprintf("Fetching %s failed with error %s\n", urlStr, err)
glog.Error(msg)
self.fetchCookie = true
return
}
for _, c := range resp.Cookies() {
self.cookies[c.Name] = c.Value
}
}
func (self *NSE) NewGetRequest(url string) *http.Request {
req, _ := http.NewRequest("GET", url, nil)
for k, v := range self.headers {
req.Header.Set(k, v)
}
for k, v := range self.cookies {
req.AddCookie(&http.Cookie{Name: k, Value: v})
}
return req
}
func (self *NSE) FetchUrl(url string) (
*http.Response, *NseResponse, error) {
var resp *http.Response = nil
var err error
retry := true
retryCount := 0
for retry {
if self.fetchCookie {
self.FetchCookie()
}
self.fetchCookie = false
glog.Info("Fetching URL ", url)
req := self.NewGetRequest(url)
resp, err = self.session.Do(req)
if err != nil {
msg := fmt.Sprintf("Fetching URL=%s failed with error=%s", url, err)
glog.Error(msg)
return nil, nil, err
}
retry = true
errMsg := fmt.Sprintf("Fetching URL=%s failed with status=%d. ",
url, resp.StatusCode)
switch resp.StatusCode {
case http.StatusOK:
retry = false
break
case http.StatusUnauthorized:
// http.StatusUnauthorized is 401
glog.Error(errMsg, "Fetching Cookie.")
self.fetchCookie = true
case http.StatusForbidden:
// 403
glog.Error(errMsg, "Sleeping for 5 minutes.")
self.fetchCookie = true
time.Sleep(5 * time.Minute)
default:
retryCount += 1
if retryCount >= 5 {
return nil, nil, errors.New("Failed with error " + strconv.Itoa(resp.StatusCode))
}
time.Sleep(1 * time.Second)
glog.Error(errMsg, "Retrying...")
}
}
var respBuf *bytes.Buffer
switch resp.Header.Get("Content-Encoding") {
case "gzip":
respBuf, err = self.readGzipResponse(resp)
default:
respBuf, err = self.readResponse(resp)
}
if err != nil {
msg := fmt.Sprintf("Reading the HTTP response failed with error=%s", err)
glog.Error(msg)
return nil, nil, err
}
msg := fmt.Sprintf("Successfully fetched URL=%s.", url)
glog.Info(msg)
return resp, NewNseResponse(respBuf), nil
}
func (self *NSE) readGzipResponse(
resp *http.Response) (*bytes.Buffer, error) {
reader, err := gzip.NewReader(resp.Body)
if err != nil {
return nil, err
}
defer reader.Close()
buf := new(bytes.Buffer)
_, err = buf.ReadFrom(reader)
if err != nil {
return nil, err
}
return buf, nil
}
func (self *NSE) readResponse(resp *http.Response) (*bytes.Buffer, error) {
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return bytes.NewBuffer(body), err
}
func (self *NSE) FetchOptionChainUrl(symbol string) (*NseOcResponse, error) {
ocUrl := self.urlIndex + url.PathEscape(symbol)
_, resp, err := self.FetchUrl(ocUrl)
if err != nil {
msg := fmt.Sprintf("Fetching OC for symbol=%s failed with err=%s",
symbol, err)
glog.Error(msg)
return nil, err
}
var jsonData map[string]interface{}
err = json.Unmarshal(resp.ResponseBuffer().Bytes(), &jsonData)
if err != nil {
msg := fmt.Sprintf("Parsing OC response failed with error=%s.", err)
glog.Error(msg)
return nil, err
}
return NewNseOcResponse(symbol, jsonData), nil
}
func (self *NSE) FetchOptionChain(symbol string, expiryDate string) (*NseOc, error) {
fetchResp, err := self.FetchOptionChainUrl(symbol)
if err != nil {
msg := fmt.Sprintf("Failed to fetch %s option chain.", symbol)
glog.Error(msg)
return nil, err
}
return fetchResp.GetExpiryOc(symbol, expiryDate)
}
func (self *NSE) FetchBankNiftyOc(expiryDate string) (*NseOc, error) {
oc, err := self.FetchOptionChain(kOcBankNifty, expiryDate)
if err != nil {
return nil, err
}
oc.SetStrikeStep(kOcBankNiftyStep)
return oc, nil
}
func (self *NSE) FetchNiftyOc(expiryDate string) (*NseOc, error) {
oc, err := self.FetchOptionChain(kOcNifty, expiryDate)
if err != nil {
return nil, err
}
oc.SetStrikeStep(kOcNiftyStep)
return oc, nil
}
func (self *NSE) FetchFinNiftyOc(expiryDate string) (*NseOc, error) {
oc, err := self.FetchOptionChain(kOcFinNifty, expiryDate)
if err != nil {
return nil, err
}
oc.SetStrikeStep(kOcFinNiftyStep)
return oc, nil
}
func (self *NSE) FetchFOParticipantData(
date time.Time) ([]NseFODataRecord, error) {
suffix := NseFOData{}.DateToNseFOtData(date)
url := fmt.Sprintf("%s%s.csv", self.fnoParticipantOiUrlPreix, suffix)
_, data, err := self.FetchUrl(url)
if err != nil {
msg := fmt.Sprintf("Fetching futures data failed with error=%s", err)
glog.Error(msg)
return nil, err
}
return NseFOData{}.Parse(data.ResponseBuffer())
}