-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathoptions_chain.go
691 lines (652 loc) · 29.1 KB
/
options_chain.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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
// Package client provides functionalities to interact with the Options Chain endpoint.
// Retrieve a a complete or filtered options chain for a given underlying symbol. Both real-time and historical requests are possible.
//
// # Making Requests
//
// Utilize [OptionChainRequest] for querying the endpoint through one of the three available methods:
//
// | Method | Execution Level | Return Type | Description |
// |------------|-----------------|------------------------------|----------------------------------------------------------------------------------------------------------------------------------|
// | **Get** | Direct | `[]OptionQuote` | Immediately fetches a slice of `[]OptionQuote`, allowing direct access to the options chain data. |
// | **Packed** | Intermediate | `*OptionQuotesResponse` | Delivers a `*OptionQuotesResponse` object containing the data, which requires unpacking to access the `OptionQuote` data. |
// | **Raw** | Low-level | `*resty.Response` | Offers the unprocessed `*resty.Response` for those seeking full control and access to the raw JSON or `*http.Response`. |
package client
import (
"context"
"fmt"
"github.com/MarketDataApp/sdk-go/helpers/parameters"
"github.com/MarketDataApp/sdk-go/models"
"github.com/go-resty/resty/v2"
)
// OptionChainRequest represents a request to the [/v1/options/chain/] endpoint.
// It encapsulates parameters for symbol, date, and various option-specific parameters to be used in the request.
// This struct provides methods to set these parameters, such as UnderlyingSymbol(), Date(), Expiration(), and Strike(), among others.
//
// # Generated By
//
// - OptionChain() *OptionChainRequest: OptionChain creates a new *OptionChainRequest and returns a pointer to the request allowing for method chaining.
//
// # Setter Methods
//
// These methods are used to set the parameters of the request. They allow for method chaining
// by returning a pointer to the *OptionChainRequest instance they modify.
//
// - UnderlyingSymbol(string) *OptionChainRequest: Sets the underlying symbol parameter for the request.
// - Date(interface{}) *OptionChainRequest: Sets the date parameter for the request.
// - Expiration(interface{}) *OptionChainRequest: Sets the expiration parameter for the request.
// - Strike(float64) *OptionChainRequest: Sets the strike price parameter for the request.
// - Monthly(bool) *OptionChainRequest: Inclues or excludes monthly option expirations from the result.
// - Weekly(bool) *OptionChainRequest: Inclues or excludes weekly option expirations from the result.
// - Quarterly(bool) *OptionChainRequest: Inclues or excludes quarterly option expirations from the result.
// - Nonstandard(bool) *OptionChainRequest: Inclues or excludes non-standard option expirations from the result.
// - Month(int) *OptionChainRequest: Requests results from the specific specific month.
// - Year(int) *OptionChainRequest: Sets the year parameter for the request.
// - DTE(int) *OptionChainRequest: Sets the Days to Expiration parameter for the request.
// - Delta(float64) *OptionChainRequest: Sets the Delta parameter for the request.
// - Side(string) *OptionChainRequest: Sets the side of the market (call or put) for the request.
// - Range(string) *OptionChainRequest: Sets the range parameter for the request.
// - StrikeLimit(int) *OptionChainRequest: Sets the maximum number of strike prices to be included in the option chain.
// - MinOpenInterest(int) *OptionChainRequest: Sets the minimum open interest for options to be included in the option chain.
// - MinVolume(int) *OptionChainRequest: Sets the minimum volume for options to be included in the option chain.
// - MaxBidAskSpread(float64) *OptionChainRequest: Sets the maximum bid-ask spread for options to be included in the option chain.
// - MaxBidAskSpreadPct(float64) *OptionChainRequest: Sets the maximum bid-ask spread percentage for options to be included in the option chain.
//
// # Execution Methods
//
// These methods are used to send the request in different formats or retrieve the data.
// They handle the actual communication with the API endpoint.
//
// - Get() ([]OptionQuote, error): Sends the request, unpacks the response, and returns the data in a user-friendly format.
// - Packed() (*OptionQuotesResponse, error): Packs the request parameters and sends the request, returning a *OptionQuotesResponse response.
// - Raw() (*resty.Response, error): Sends the request as is and returns the raw HTTP response.
//
// [/v1/options/chain/]: https://www.marketdata.app/docs/api/options/chain
type OptionChainRequest struct {
*baseRequest
symbolParams *parameters.SymbolParams
dateParams *parameters.DateParams
optionParams *parameters.OptionParams
}
// UnderlyingSymbol sets the underlyingSymbol parameter for the OptionChainRequest.
// This method is used to specify the symbol of the underlying asset for which the option chain is requested.
// It modifies the symbolParams field of the OptionChainRequest instance to store the symbol value.
//
// # Parameters
//
// - string: The underlying symbol to be set.
//
// # Returns
//
// - *OptionChainRequest: This method returns a pointer to the OptionChainRequest instance it was called on. This allows for method chaining, where multiple setter methods can be called in a single statement. If the receiver (*OptionChainRequest) is nil, it returns nil to prevent a panic.
func (ocr *OptionChainRequest) UnderlyingSymbol(q string) *OptionChainRequest {
if ocr == nil {
return nil
}
err := ocr.symbolParams.SetSymbol(q)
if err != nil {
ocr.Error = err
}
return ocr
}
// Date sets the date parameter for the OptionChainRequest.
// This method is used to specify the date for which the option chain is requested.
// It modifies the dateParams field of the OptionChainRequest instance to store the date value.
//
// # Parameters
//
// - interface{}: An interface{} that represents the starting date. It can be a string, a time.Time object, a Unix timestamp or any other type that the underlying dates package can process.
//
// # Returns
//
// - *OptionChainRequest: This method returns a pointer to the OptionChainRequest instance it was called on. This allows for method chaining. If the receiver (*OptionChainRequest) is nil, it returns nil to prevent a panic.
func (ocr *OptionChainRequest) Date(q interface{}) *OptionChainRequest {
if ocr.dateParams == nil {
ocr.dateParams = ¶meters.DateParams{}
}
err := ocr.dateParams.SetDate(q)
if err != nil {
ocr.Error = err
}
return ocr
}
// Expiration sets the expiration parameter for the OptionChainRequest.
// This method is used to specify the expiration date for the options in the option chain.
// It modifies the optionParams field of the OptionChainRequest instance to store the expiration value.
//
// # Parameters
//
// - interface{}: An interface{} representing the expiration date to be set.
//
// # Returns
//
// - *OptionChainRequest: This method returns a pointer to the OptionChainRequest instance it was called on. This allows for method chaining. If the receiver (*OptionChainRequest) is nil, it returns nil to prevent a panic.
func (ocr *OptionChainRequest) Expiration(q interface{}) *OptionChainRequest {
if ocr.optionParams == nil {
ocr.optionParams = ¶meters.OptionParams{}
}
err := ocr.optionParams.SetExpiration(q)
if err != nil {
ocr.Error = err
}
return ocr
}
// Month sets the month parameter for the OptionChainRequest.
// This method is used to specify the month for which the option chain is requested.
// It modifies the optionParams field of the OptionChainRequest instance to store the month value.
//
// # Parameters
//
// - int: The month to be set.
//
// # Returns
//
// - *OptionChainRequest: This method returns a pointer to the OptionChainRequest instance it was called on. This allows for method chaining. If the receiver (*OptionChainRequest) is nil, it returns nil to prevent a panic.
func (ocr *OptionChainRequest) Month(month int) *OptionChainRequest {
if ocr.optionParams == nil {
ocr.optionParams = ¶meters.OptionParams{}
}
err := ocr.optionParams.SetMonth(month)
if err != nil {
ocr.Error = err
}
return ocr
}
// Year sets the year parameter for the OptionChainRequest.
// This method is used to specify the year for which the option chain is requested.
// It modifies the optionParams field of the OptionChainRequest instance to store the year value.
//
// # Parameters
//
// - int: The year to be set.
//
// # Returns
//
// - *OptionChainRequest: This method returns a pointer to the OptionChainRequest instance it was called on. This allows for method chaining. If the receiver (*OptionChainRequest) is nil, it returns nil to prevent a panic.
func (ocr *OptionChainRequest) Year(year int) *OptionChainRequest {
if ocr.optionParams == nil {
ocr.optionParams = ¶meters.OptionParams{}
}
err := ocr.optionParams.SetYear(year)
if err != nil {
ocr.Error = err
}
return ocr
}
// Weekly sets the weekly parameter for the OptionChainRequest.
// This method is used to specify whether to include weekly options in the option chain.
// It modifies the optionParams field of the OptionChainRequest instance to store the weekly value.
//
// # Parameters
//
// - bool: Whether to include weekly options.
//
// # Returns
//
// - *OptionChainRequest: This method returns a pointer to the OptionChainRequest instance it was called on. This allows for method chaining. If the receiver (*OptionChainRequest) is nil, it returns nil to prevent a panic.
func (ocr *OptionChainRequest) Weekly(weekly bool) *OptionChainRequest {
if ocr.optionParams == nil {
ocr.optionParams = ¶meters.OptionParams{}
}
err := ocr.optionParams.SetWeekly(weekly)
if err != nil {
ocr.Error = err
}
return ocr
}
// Monthly sets the monthly parameter for the OptionChainRequest.
// This method is used to specify whether to include monthly options in the option chain.
// It modifies the optionParams field of the OptionChainRequest instance to store the monthly value.
//
// # Parameters
//
// - bool: Whether to include monthly options.
//
// # Returns
//
// - *OptionChainRequest: This method returns a pointer to the OptionChainRequest instance it was called on. This allows for method chaining. If the receiver (*OptionChainRequest) is nil, it returns nil to prevent a panic.
func (ocr *OptionChainRequest) Monthly(monthly bool) *OptionChainRequest {
if ocr.optionParams == nil {
ocr.optionParams = ¶meters.OptionParams{}
}
err := ocr.optionParams.SetMonthly(monthly)
if err != nil {
ocr.Error = err
}
return ocr
}
// Quarterly sets the quarterly parameter for the OptionChainRequest.
// This method is used to specify whether to include quarterly options in the option chain.
// It modifies the optionParams field of the OptionChainRequest instance to store the quarterly value.
//
// # Parameters
//
// - bool: Whether to include quarterly options.
//
// # Returns
//
// - *OptionChainRequest: This method returns a pointer to the OptionChainRequest instance it was called on. This allows for method chaining. If the receiver (*OptionChainRequest) is nil, it returns nil to prevent a panic.
func (ocr *OptionChainRequest) Quarterly(quarterly bool) *OptionChainRequest {
if ocr.optionParams == nil {
ocr.optionParams = ¶meters.OptionParams{}
}
err := ocr.optionParams.SetQuarterly(quarterly)
if err != nil {
ocr.Error = err
}
return ocr
}
// Nonstandard sets the nonstandard parameter for the OptionChainRequest.
// This method is used to specify whether to include nonstandard options in the option chain.
// It modifies the optionParams field of the OptionChainRequest instance to store the nonstandard value.
//
// # Parameters
//
// - bool: Whether to include nonstandard options.
//
// # Returns
//
// - *OptionChainRequest: This method returns a pointer to the OptionChainRequest instance it was called on. This allows for method chaining. If the receiver (*OptionChainRequest) is nil, it returns nil to prevent a panic.
func (ocr *OptionChainRequest) Nonstandard(nonstandard bool) *OptionChainRequest {
if ocr.optionParams == nil {
ocr.optionParams = ¶meters.OptionParams{}
}
err := ocr.optionParams.SetNonstandard(nonstandard)
if err != nil {
ocr.Error = err
}
return ocr
}
// DTE (Days to Expiration) sets the DTE parameter for the OptionChainRequest.
// This method specifies the number of days to expiration for the options in the option chain.
//
// # Parameters
//
// - int: Requests an expiration date from the option chain based on the number of days from the present date.
//
// # Returns
//
// - *OptionChainRequest: This method returns a pointer to the OptionChainRequest instance it was called on. This allows for method chaining. If the receiver (*OptionChainRequest) is nil, it returns nil to prevent a panic.
func (ocr *OptionChainRequest) DTE(dte int) *OptionChainRequest {
if ocr.optionParams == nil {
ocr.optionParams = ¶meters.OptionParams{}
}
err := ocr.optionParams.SetDTE(dte)
if err != nil {
ocr.Error = err
}
return ocr
}
// Delta sets the Delta parameter for the OptionChainRequest.
// This method is used to specify a particular Delta value for the options in the option chain.
// It modifies the optionParams field of the OptionChainRequest instance to store the Delta value.
//
// # Parameters
//
// - float64: The Delta value to be set.
//
// # Returns
//
// - *OptionChainRequest: This method returns a pointer to the OptionChainRequest instance it was called on. This allows for method chaining. If the receiver (*OptionChainRequest) is nil, it returns nil to prevent a panic.
func (ocr *OptionChainRequest) Delta(delta float64) *OptionChainRequest {
if ocr.optionParams == nil {
ocr.optionParams = ¶meters.OptionParams{}
}
err := ocr.optionParams.SetDelta(delta)
if err != nil {
ocr.Error = err
}
return ocr
}
// Side sets the Side parameter for the OptionChainRequest.
// This method is used to specify the side of the market for the options in the option chain (e.g., call or put).
// It modifies the optionParams field of the OptionChainRequest instance to store the side value.
//
// # Parameters
//
// - string: A string representing the side of the market chain to be set. Expected value is "call" or "put".
//
// # Returns
//
// - *OptionChainRequest: This method returns a pointer to the OptionChainRequest instance it was called on. This allows for method chaining. If the receiver (*OptionChainRequest) is nil, it returns nil to prevent a panic.
func (ocr *OptionChainRequest) Side(side string) *OptionChainRequest {
if ocr.optionParams == nil {
ocr.optionParams = ¶meters.OptionParams{}
}
err := ocr.optionParams.SetSide(side)
if err != nil {
ocr.Error = err
}
return ocr
}
// Range sets the Range parameter for the OptionChainRequest.
// This method is used to specify the range of options to be included in the option chain based on their strike price.
// It modifies the optionParams field of the OptionChainRequest instance to store the range value.
//
// # Parameters
//
// - string: A string representing the range of options to be included. The expected values can vary, such as "ITM" (In The Money), "OTM" (Out of The Money), "ATM" (At The Money), etc.
//
// # Returns
//
// - *OptionChainRequest: This method returns a pointer to the OptionChainRequest instance it was called on. This allows for method chaining. If the receiver (*OptionChainRequest) is nil, it returns nil to prevent a panic.
func (ocr *OptionChainRequest) Range(rangeParam string) *OptionChainRequest {
if ocr.optionParams == nil {
ocr.optionParams = ¶meters.OptionParams{}
}
err := ocr.optionParams.SetRange(rangeParam)
if err != nil {
ocr.Error = err
}
return ocr
}
// Strike sets the strike price parameter for the OptionChainRequest.
// This method is used to specify a particular strike price for the options in the option chain.
// It modifies the optionParams field of the OptionChainRequest instance to store the strike price value.
//
// # Parameters
//
// - float64: The strike price to be set.
//
// # Returns
//
// - *OptionChainRequest: This method returns a pointer to the OptionChainRequest instance it was called on. This allows for method chaining. If the receiver (*OptionChainRequest) is nil, it returns nil to prevent a panic.
func (ocr *OptionChainRequest) Strike(strike float64) *OptionChainRequest {
if ocr.optionParams == nil {
ocr.optionParams = ¶meters.OptionParams{}
}
err := ocr.optionParams.SetStrike(strike)
if err != nil {
ocr.Error = err
}
return ocr
}
// StrikeLimit sets the StrikeLimit parameter for the OptionChainRequest.
// This method is used to specify the maximum number of strike prices to be included in the option chain.
// It modifies the optionParams field of the OptionChainRequest instance to store the strike limit value.
//
// # Parameters
//
// - int: The maximum number of strikes to be included.
//
// # Returns
//
// - *OptionChainRequest: This method returns a pointer to the OptionChainRequest instance it was called on. This allows for method chaining. If the receiver (*OptionChainRequest) is nil, it returns nil to prevent a panic.
func (ocr *OptionChainRequest) StrikeLimit(strikeLimit int) *OptionChainRequest {
if ocr.optionParams == nil {
ocr.optionParams = ¶meters.OptionParams{}
}
err := ocr.optionParams.SetStrikeLimit(strikeLimit)
if err != nil {
ocr.Error = err
}
return ocr
}
// MinOpenInterest sets the MinOpenInterest parameter for the OptionChainRequest.
// This method is used to specify the minimum open interest for options to be included in the option chain.
// It modifies the optionParams field of the OptionChainRequest instance to store the minimum open interest value.
//
// # Parameters
//
// - int: An int representing the minimum open interest required to be included in the result.
//
// # Returns
//
// - *OptionChainRequest: This method returns a pointer to the OptionChainRequest instance it was called on. This allows for method chaining. If the receiver (*OptionChainRequest) is nil, it returns nil to prevent a panic.
func (ocr *OptionChainRequest) MinOpenInterest(minOpenInterest int) *OptionChainRequest {
if ocr.optionParams == nil {
ocr.optionParams = ¶meters.OptionParams{}
}
err := ocr.optionParams.SetMinOpenInterest(minOpenInterest)
if err != nil {
ocr.Error = err
}
return ocr
}
// MinVolume sets the MinVolume parameter for the OptionChainRequest.
// This method is used to specify the minimum volume for options to be included in the option chain.
// It modifies the optionParams field of the OptionChainRequest instance to store the minimum volume value.
//
// # Parameters
//
// - int: An int representing the minimum volume needed to be included in the result.
//
// # Returns
//
// - *OptionChainRequest: This method returns a pointer to the OptionChainRequest instance it was called on. This allows for method chaining. If the receiver (*OptionChainRequest) is nil, it returns nil to prevent a panic.
func (ocr *OptionChainRequest) MinVolume(minVolume int) *OptionChainRequest {
if ocr.optionParams == nil {
ocr.optionParams = ¶meters.OptionParams{}
}
err := ocr.optionParams.SetMinVolume(minVolume)
if err != nil {
ocr.Error = err
}
return ocr
}
// MaxBidAskSpread sets the MaxBidAskSpread parameter for the OptionChainRequest.
// This method is used to specify the maximum bid-ask spread for options to be included in the option chain.
// It modifies the optionParams field of the OptionChainRequest instance to store the maximum bid-ask spread value.
//
// # Parameters
//
// - float64: A float64 representing the maximum bid-ask spread neeeded to be included in the result.
//
// # Returns
//
// - *OptionChainRequest: This method returns a pointer to the OptionChainRequest instance it was called on. This allows for method chaining. If the receiver (*OptionChainRequest) is nil, it returns nil to prevent a panic.
func (ocr *OptionChainRequest) MaxBidAskSpread(maxBidAskSpread float64) *OptionChainRequest {
if ocr.optionParams == nil {
ocr.optionParams = ¶meters.OptionParams{}
}
err := ocr.optionParams.SetMaxBidAskSpread(maxBidAskSpread)
if err != nil {
ocr.Error = err
}
return ocr
}
// MaxBidAskSpreadPct sets the MaxBidAskSpreadPct parameter for the OptionChainRequest.
// This method is used to specify the maximum bid-ask spread percentage for options to be included in the option chain.
// It modifies the optionParams field of the OptionChainRequest instance to store the maximum bid-ask spread percentage value.
//
// # Parameters
//
// - float64: A float64 representing the maximum bid-ask spread percentage to be included in the result.
//
// # Returns
//
// - *OptionChainRequest: This method returns a pointer to the OptionChainRequest instance it was called on. This allows for method chaining. If the receiver (*OptionChainRequest) is nil, it returns nil to prevent a panic.
func (ocr *OptionChainRequest) MaxBidAskSpreadPct(maxBidAskSpreadPct float64) *OptionChainRequest {
if ocr.optionParams == nil {
ocr.optionParams = ¶meters.OptionParams{}
}
err := ocr.optionParams.SetMaxBidAskSpreadPct(maxBidAskSpreadPct)
if err != nil {
ocr.Error = err
}
return ocr
}
// MinAsk sets the MinAsk parameter for the OptionChainRequest.
// This method is used to specify the minimum ask price for options to be included in the option chain.
// It modifies the optionParams field of the OptionChainRequest instance to store the minimum ask price value.
//
// # Parameters
//
// - float64: A float64 representing the minimum ask price to be included in the result.
//
// # Returns
//
// - *OptionChainRequest: This method returns a pointer to the OptionChainRequest instance it was called on. This allows for method chaining. If the receiver (*OptionChainRequest) is nil, it returns nil to prevent a panic.
func (ocr *OptionChainRequest) MinAsk(minAsk float64) *OptionChainRequest {
if ocr.optionParams == nil {
ocr.optionParams = ¶meters.OptionParams{}
}
err := ocr.optionParams.SetMinAsk(minAsk)
if err != nil {
ocr.Error = err
}
return ocr
}
// MaxAsk sets the MaxAsk parameter for the OptionChainRequest.
// This method is used to specify the maximum ask price for options to be included in the option chain.
// It modifies the optionParams field of the OptionChainRequest instance to store the maximum ask price value.
//
// # Parameters
//
// - float64: A float64 representing the maximum ask price to be included in the result.
//
// # Returns
//
// - *OptionChainRequest: This method returns a pointer to the OptionChainRequest instance it was called on. This allows for method chaining. If the receiver (*OptionChainRequest) is nil, it returns nil to prevent a panic.
func (ocr *OptionChainRequest) MaxAsk(maxAsk float64) *OptionChainRequest {
if ocr.optionParams == nil {
ocr.optionParams = ¶meters.OptionParams{}
}
err := ocr.optionParams.SetMaxAsk(maxAsk)
if err != nil {
ocr.Error = err
}
return ocr
}
// MinBid sets the MinBid parameter for the OptionChainRequest.
// This method is used to specify the minimum bid price for options to be included in the option chain.
// It modifies the optionParams field of the OptionChainRequest instance to store the minimum bid price value.
//
// # Parameters
//
// - float64: A float64 representing the minimum bid price to be included in the result.
//
// # Returns
//
// - *OptionChainRequest: This method returns a pointer to the OptionChainRequest instance it was called on. This allows for method chaining. If the receiver (*OptionChainRequest) is nil, it returns nil to prevent a panic.
func (ocr *OptionChainRequest) MinBid(minBid float64) *OptionChainRequest {
if ocr.optionParams == nil {
ocr.optionParams = ¶meters.OptionParams{}
}
err := ocr.optionParams.SetMinBid(minBid)
if err != nil {
ocr.Error = err
}
return ocr
}
// MaxBid sets the MaxBid parameter for the OptionChainRequest.
// This method is used to specify the maximum bid price for options to be included in the option chain.
// It modifies the optionParams field of the OptionChainRequest instance to store the maximum bid price value.
//
// # Parameters
//
// - float64: A float64 representing the maximum bid price to be included in the result.
//
// # Returns
//
// - *OptionChainRequest: This method returns a pointer to the OptionChainRequest instance it was called on. This allows for method chaining. If the receiver (*OptionChainRequest) is nil, it returns nil to prevent a panic.
func (ocr *OptionChainRequest) MaxBid(maxBid float64) *OptionChainRequest {
if ocr.optionParams == nil {
ocr.optionParams = ¶meters.OptionParams{}
}
err := ocr.optionParams.SetMaxBid(maxBid)
if err != nil {
ocr.Error = err
}
return ocr
}
// getParams packs the OptionChainRequest struct into a slice of interface{} and returns it.
// This method is used to gather all the parameters set in the OptionChainRequest into a single slice
// for easier manipulation and usage in subsequent requests.
//
// # Returns
//
// - []parameters.MarketDataParam: A slice containing all the parameters set in the OptionChainRequest.
// - error: An error object indicating failure to pack the parameters, nil if successful.
func (ocr *OptionChainRequest) getParams() ([]parameters.MarketDataParam, error) {
if ocr == nil {
return nil, fmt.Errorf("OptionChainRequest is nil")
}
params := []parameters.MarketDataParam{ocr.symbolParams, ocr.dateParams, ocr.optionParams}
return params, nil
}
// Raw executes the OptionChainRequest with the provided context and returns the raw *resty.Response.
// This method uses the default client for this request. The *resty.Response can be directly used to access the raw JSON or *http.Response.
//
// # Parameters
//
// - ctx context.Context: The context to use for the request execution.
//
// # Returns
//
// - *resty.Response: The raw HTTP response from the executed OptionChainRequest.
// - error: An error object if the OptionChainRequest is nil or if an error occurs during the request execution.
func (ocr *OptionChainRequest) Raw(ctx context.Context) (*resty.Response, error) {
return ocr.baseRequest.Raw(ctx)
}
// Packed sends the OptionChainRequest with the provided context and returns the OptionChainResponse.
// This method checks if the OptionChainRequest receiver is nil, returning an error if true.
// It proceeds to send the request using the default client and returns the OptionChainResponse along with any error encountered during the request.
//
// # Parameters
//
// - ctx context.Context: The context to use for the request execution.
//
// # Returns
//
// - *models.OptionQuotesResponse: A pointer to the OptionQuotesResponse obtained from the request.
// - error: An error object that indicates a failure in sending the request.
func (ocr *OptionChainRequest) Packed(ctx context.Context) (*models.OptionQuotesResponse, error) {
if ocr == nil {
return nil, fmt.Errorf("OptionChainRequest is nil")
}
var ocrResp models.OptionQuotesResponse
_, err := ocr.baseRequest.client.getFromRequest(ctx, ocr.baseRequest, &ocrResp)
if err != nil {
return nil, err
}
return &ocrResp, nil
}
// Get sends the OptionChainRequest with the provided context, unpacks the OptionChainResponse, and returns a slice of OptionQuote.
// It returns an error if the request or unpacking fails. This method is crucial for obtaining the actual option chain data
// from the option chain request. The method first checks if the OptionChainRequest receiver is nil, which would
// result in an error as the request cannot be sent. It then proceeds to send the request using the default client.
// Upon receiving the response, it unpacks the data into a slice of OptionQuote using the Unpack method from the response.
//
// # Parameters
//
// - ctx context.Context: The context to use for the request execution.
//
// # Returns
//
// - []models.OptionQuote: A slice of OptionQuote containing the unpacked option chain data from the response.
// - error: An error object that indicates a failure in sending the request or unpacking the response.
func (ocr *OptionChainRequest) Get(ctx context.Context) ([]models.OptionQuote, error) {
if ocr == nil {
return nil, fmt.Errorf("OptionChainRequest is nil")
}
// Use the Packed method to make the request
ocrResp, err := ocr.Packed(ctx)
if err != nil {
return nil, err
}
// Unpack the data using the Unpack method in the response
data, err := ocrResp.Unpack()
if err != nil {
return nil, err
}
return data, nil
}
// OptionChain creates a new OptionChainRequest and associates it with the default client. This function initializes the request
// with default parameters for symbol, date, and option-specific parameters, and sets the request path based on
// the predefined endpoints for option chains.
//
// # Returns
//
// - *OptionChainRequest: A pointer to the newly created OptionChainRequest with default parameters and associated client.
func OptionChain() *OptionChainRequest {
baseReq := newBaseRequest()
baseReq.path = endpoints[1]["options"]["chain"]
ocr := &OptionChainRequest{
baseRequest: baseReq,
symbolParams: ¶meters.SymbolParams{},
optionParams: ¶meters.OptionParams{},
dateParams: ¶meters.DateParams{},
}
baseReq.child = ocr
return ocr
}