Skip to content

Commit

Permalink
added mutual fund candles
Browse files Browse the repository at this point in the history
  • Loading branch information
MarketDataApp committed May 2, 2024
1 parent 6e0b26c commit fd01fd3
Show file tree
Hide file tree
Showing 17 changed files with 1,237 additions and 57 deletions.
1 change: 1 addition & 0 deletions .scripts/gomarkdoc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ process_group "options_lookup"
process_group "options_quotes"
process_group "options_strikes"
process_group "options_chain"
process_group "funds_candles"
process_group "client"
process_group "logging"

Expand Down
1 change: 1 addition & 0 deletions .scripts/process_markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"https://www.marketdata.app/docs/api/options/strikes": {"title": "Strikes", "sidebar_position": 3},
"https://www.marketdata.app/docs/api/options/chain": {"title": "Option Chain", "sidebar_position": 4},
"https://www.marketdata.app/docs/api/options/quotes": {"title": "Quotes", "sidebar_position": 5},
"https://www.marketdata.app/docs/api/funds/candles": {"title": "Candles", "sidebar_position": 1},
"https://www.marketdata.app/docs/sdk/go/client": {"title": "Client", "sidebar_position": 2},
"https://www.marketdata.app/docs/sdk/go/logging": {"title": "Logging", "sidebar_position": 3},

Expand Down
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div align="center">

# Go SDK for Market Data v1.1
# Go SDK for Market Data v1.2
### Access Financial Data with Ease

> This is the official Go SDK for [Market Data](https://www.marketdata.app/). It provides developers with a powerful, easy-to-use interface to obtain real-time and historical financial data. Ideal for building financial applications, trading bots, and investment strategies.
Expand All @@ -12,7 +12,7 @@
[![License](https://img.shields.io/github/license/MarketDataApp/sdk-go.svg)](https://github.com/MarketDataApp/sdk-go/blob/master/LICENSE)
![SDK Version](https://img.shields.io/badge/version-1.0.0-blue.svg)
![GitHub go.mod Go version](https://img.shields.io/github/go-mod/go-version/MarketDataApp/sdk-go)
![Lines of Code](https://img.shields.io/badge/lines_of_code-8688-blue)
![Lines of Code](https://img.shields.io/badge/lines_of_code-8710-blue)

#### Connect With The Market Data Community

Expand Down Expand Up @@ -235,10 +235,14 @@ Market Data's Go SDK covers the vast majority of v1 endpoints. See our complete
| | [Candles](https://www.marketdata.app/docs/api/indices/candles) | ✅ | ❌ |
| | [Quotes](https://www.marketdata.app/docs/api/indices/quotes) | ✅ | ❌ |
| | | | |
| **[FUNDS](https://www.marketdata.app/docs/api/funds)** | | | |
| | [Candles](https://www.marketdata.app/docs/api/indices/candles) | ✅ | ❌ |
| | | | |
| **[UTILITIES](https://www.marketdata.app/docs/api/utilities)** | | | |
| | [Status](https://www.marketdata.app/docs/api/utilities/status) | ❌ | ❌ |
| | [Headers](https://www.marketdata.app/docs/api/utilities/headers) | ❌ | ❌ |
> Note on v2: Even though some v2 endpoints are available for use in this SDK, Market Data has not yet released v2 of its API for clients and v2 usage is restricted to admins only. Clients should only use v1 endpoints at this time. Even after v2 is released, we do not plan on deprecating v1 endpoints, so please build your applications with confidence using v1 endpoints.
> Note on v2: Even though some v2 endpoints are available for use in this SDK, Market Data has not yet released v2 of its API for clients and v2 usage is restricted to admins only. Clients should only use v1 endpoints at this time. Even after v2 is released, we do not plan on deprecating v1 endpoints, so please build your applications with confidence using our v1 endpoints.
### SDK License & Data Usage Terms
Expand Down
8 changes: 8 additions & 0 deletions baseRequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,14 @@ func (br *baseRequest) getParams() ([]parameters.MarketDataParam, error) {
return params, nil
}

if fcr, ok := br.child.(*FundCandlesRequest); ok {
params, err := fcr.getParams()
if err != nil {
return nil, err
}
return params, nil
}

return []parameters.MarketDataParam{}, nil
}

Expand Down
3 changes: 3 additions & 0 deletions endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ var endpoints = map[int]map[string]map[string]string{
"quotes": "/v1/indices/quotes/{symbol}/",
"candles": "/v1/indices/candles/{resolution}/{symbol}/",
},
"funds": {
"candles": "/v1/funds/candles/{resolution}/{symbol}/",
},
},
2: {
"stocks": {
Expand Down
282 changes: 282 additions & 0 deletions funds_candles.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,282 @@
// Package client includes types and methods to access the Funds / Candles endpoint. Retrieve historical price candles for any supported stock symbol.
//
// # Making Requests
//
// Use [FundCandlesRequest] to make requests to the endpoint using any of the three supported execution methods:
//
// | Method | Execution | Return Type | Description |
// |------------|---------------|-----------------------------|------------------------------------------------------------------------------------------------------------|
// | **Get** | Direct | `[]Candle` | Directly returns a slice of `[]Candle`, facilitating individual access to each candle. |
// | **Packed** | Intermediate | `*FundCandlesResponse` | Returns a packed `*FundCandlesResponse` object. Must be unpacked to access the `[]Candle` slice. |
// | **Raw** | Low-level | `*resty.Response` | Provides the raw `*resty.Response` for maximum flexibility. Direct access to raw JSON or `*http.Response`. |
package client

import (
"fmt"

"github.com/MarketDataApp/sdk-go/helpers/parameters"
"github.com/MarketDataApp/sdk-go/models"
"github.com/go-resty/resty/v2"
)

// FundCandlesRequest represents a request to the [/v1/funds/candles/] endpoint.
// It encapsulates parameters for resolution, symbol, date, and additional stock-specific parameters to be used in the request.
// This struct provides methods such as Resolution(), Symbol(), Date(), From(), To(), Countback(), AdjustSplits(), AdjustDividends(), Extended(), and Exchange() to set these parameters respectively.
//
// # Generated By
//
// - FundCandles() *FundCandlesRequest: FundCandles creates a new *FundCandlesRequest and returns a pointer to the request allowing for method chaining.
//
// # Setter Methods
//
// - Resolution(string) *FundCandlesRequest: Sets the resolution parameter for the request.
// - Symbol(string) *FundCandlesRequest: Sets the symbol parameter for the request.
// - Date(interface{}) *FundCandlesRequest: Sets the date parameter for the request.
// - From(interface{}) *FundCandlesRequest: Sets the 'from' date parameter for the request.
// - To(interface{}) *FundCandlesRequest: Sets the 'to' date parameter for the request.
// - Countback(int) *FundCandlesRequest: Sets the countback parameter for the request.
// - AdjustSplits(bool) *FundCandlesRequest: Sets the adjust splits parameter for the request.
// - AdjustDividends(bool) *FundCandlesRequest: Sets the adjust dividends parameter for the request.
// - Extended(bool) *FundCandlesRequest: Sets the extended hours data parameter for the request.
// - Exchange(string) *FundCandlesRequest: Sets the exchange parameter for the request.
//
// # 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() ([]Candle, error): Sends the request, unpacks the response, and returns the data in a user-friendly format.
// - Packed() (*FundCandlesResponse, error): Returns a struct that contains equal-length slices of primitives. This packed response mirrors Market Data's JSON response.
// - Raw() (*resty.Response, error): Sends the request as is and returns the raw HTTP response.
//
// [/v1/funds/candles/]: https://www.marketdata.app/docs/api/funds/candles
type FundCandlesRequest struct {
*baseRequest
stockCandleParams *parameters.StockCandleParams
resolutionParams *parameters.ResolutionParams
symbolParams *parameters.SymbolParams
dateParams *parameters.DateParams
}

// Resolution sets the resolution parameter for the FundCandlesRequest.
// This method is used to specify the granularity of the candle data to be retrieved.
//
// # Parameters
//
// - string: A string representing the resolution to be set.
//
// # Returns
//
// - *FundCandlesRequest: This method returns a pointer to the *FundCandlesRequest instance it was called on. This allows for method chaining.
func (cr *FundCandlesRequest) Resolution(q string) *FundCandlesRequest {
if cr == nil {
return nil
}
err := cr.resolutionParams.SetResolution(q)
if err != nil {
cr.Error = err
}
return cr
}

// Symbol sets the symbol parameter for the FundCandlesRequest.
// This method is used to specify the stock symbol for which candle data is requested.
//
// # Parameters
//
// - string: A string representing the stock symbol to be set.
//
// # Returns
//
// - *FundCandlesRequest: This method returns a pointer to the *FundCandlesRequest instance it was called on. This allows for method chaining.
func (fcr *FundCandlesRequest) Symbol(q string) *FundCandlesRequest {
if fcr == nil {
return nil
}
err := fcr.symbolParams.SetSymbol(q)
if err != nil {
fcr.Error = err
}
return fcr
}

// Date sets the date parameter for the FundCandlesRequest.
// This method is used to specify the date for which the stock candle data is requested.
//
// # Parameters
//
// - interface{}: An interface{} representing the date to be set. It can be a string, a time.Time object, a Unix timestamp as an int, or any other type that the underlying dates package method can process.
//
// # Returns
//
// - *FundCandlesRequest: This method returns a pointer to the *FundCandlesRequest instance it was called on. This allows for method chaining.
func (fcr *FundCandlesRequest) Date(q interface{}) *FundCandlesRequest {
err := fcr.dateParams.SetDate(q)
if err != nil {
fcr.baseRequest.Error = err
}
return fcr
}

// From sets the 'from' date parameter for the FundCandlesRequest.
// This method is used to specify the starting point of the date range for which the stock candle data is requested.
//
// # Parameters
//
// - interface{}: An interface{} representing the date to be set. It can be a string, a time.Time object, a Unix timestamp as an int, or any other type that the underlying dates package method can process.
//
// # Returns
//
// - *FundCandlesRequest: This method returns a pointer to the *FundCandlesRequest instance it was called on. This allows for method chaining.
func (fcr *FundCandlesRequest) From(q interface{}) *FundCandlesRequest {
err := fcr.dateParams.SetFrom(q)
if err != nil {
fcr.baseRequest.Error = err
}
return fcr
}

// To sets the 'to' date parameter for the FundCandlesRequest.
// This method is used to specify the ending point of the date range for which the stock candle data is requested.
//
// # Parameters
//
// - interface{}: An interface{} representing the date to be set. It can be a string, a time.Time object, a Unix timestamp as an int, or any other type that the underlying dates package method can process.
//
// # Returns
//
// - *FundCandlesRequest: This method returns a pointer to the *FundCandlesRequest instance it was called on
func (fcr *FundCandlesRequest) To(q interface{}) *FundCandlesRequest {
err := fcr.dateParams.SetTo(q)
if err != nil {
fcr.baseRequest.Error = err
}
return fcr
}

// Countback sets the countback parameter for the FundCandlesRequest.
// This method specifies the number of candles to return, counting backwards from the 'to' date.
//
// # Parameters
//
// - int: The number of candles to return.
//
// # Returns
//
// - *FundCandlesRequest: This method returns a pointer to the *FundCandlesRequest instance it was called on. This allows for method chaining.
func (fcr *FundCandlesRequest) Countback(q int) *FundCandlesRequest {
err := fcr.dateParams.SetCountback(q)
if err != nil {
fcr.baseRequest.Error = err
}
return fcr
}

// AdjustSplits sets the adjust splits parameter for the FundCandlesRequest.
// This method indicates whether the returned data should be adjusted for stock splits.
//
// # Parameters
//
// - bool: Whether to adjust for splits.
//
// # Returns
//
// - *FundCandlesRequest: This method returns a pointer to the *FundCandlesRequest instance it was called on. This allows for method chaining.
func (fcr *FundCandlesRequest) AdjustSplits(q bool) *FundCandlesRequest {
if fcr == nil {
return nil
}
fcr.stockCandleParams.SetAdjustSplits(q)
return fcr
}

// getParams packs the CandlesRequest struct into a slice of interface{} and returns it.
func (fcr *FundCandlesRequest) getParams() ([]parameters.MarketDataParam, error) {
if fcr == nil {
return nil, fmt.Errorf("FundCandlesRequest is nil")
}
params := []parameters.MarketDataParam{fcr.dateParams, fcr.symbolParams, fcr.resolutionParams}
return params, nil
}

// Raw executes the FundCandlesRequest and returns the raw *resty.Response.
// This method returns the raw JSON or *http.Response for further processing without accepting an alternative MarketDataClient.
//
// # Returns
//
// - *resty.Response: The raw HTTP response from the executed request.
// - error: An error object if the request fails due to execution errors.
func (fcr *FundCandlesRequest) Raw() (*resty.Response, error) {
return fcr.baseRequest.Raw()
}

// Packed sends the FundCandlesRequest and returns the FundCandlesResponse.
////
// # Returns
//
// - *FundCandlesResponse: A pointer to the FundCandlesResponse obtained from the request.
// - error: An error object that indicates a failure in sending the request.
func (fcr *FundCandlesRequest) Packed() (*models.FundCandlesResponse, error) {
if fcr == nil {
return nil, fmt.Errorf("FundCandlesRequest is nil")
}

var fcrResp models.FundCandlesResponse
_, err := fcr.baseRequest.client.getFromRequest(fcr.baseRequest, &fcrResp)
if err != nil {
return nil, err
}

return &fcrResp, nil
}

// Get sends the FundCandlesRequest, unpacks the FundCandlesResponse, and returns a slice of StockCandle.
// It returns an error if the request or unpacking fails.
//
// # Returns
//
// - []Candle: A slice of []Candle containing the unpacked candle data from the response.
// - error: An error object that indicates a failure in sending the request or unpacking the response.
func (fcr *FundCandlesRequest) Get() ([]models.Candle, error) {
if fcr == nil {
return nil, fmt.Errorf("FundCandlesRequest is nil")
}

// Use the Packed method to make the request
fcrResp, err := fcr.Packed()
if err != nil {
return nil, err
}

// Unpack the data using the Unpack method in the response
data, err := fcrResp.Unpack()
if err != nil {
return nil, err
}

return data, nil
}

// FundCandles initializes a new FundCandlesRequest with default parameters.
// This function prepares a request to fetch stock candle data. It sets up all necessary parameters
// and configurations to make the request ready to be sent.
//
// # Returns
//
// - *FundCandlesRequest: A pointer to the newly created FundCandlesRequest instance. This instance contains all the necessary parameters set to their default values and is ready to have additional parameters set or to be sent.
func FundCandles() *FundCandlesRequest {
baseReq := newBaseRequest()
baseReq.path = endpoints[1]["funds"]["candles"]

fcr := &FundCandlesRequest{
baseRequest: baseReq,
dateParams: &parameters.DateParams{},
resolutionParams: &parameters.ResolutionParams{},
symbolParams: &parameters.SymbolParams{},
}

// Set the date to the current time
baseReq.child = fcr

return fcr
}
41 changes: 41 additions & 0 deletions funds_candles_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package client

import "fmt"

func ExampleFundCandlesRequest_raw() {
fcr, err := FundCandles().Resolution("D").Symbol("VFINX").From("2023-01-01").To("2023-01-06").Raw()
if err != nil {
fmt.Print(err)
return
}
fmt.Println(fcr)

// Output: {"s":"ok","t":[1672722000,1672808400,1672894800,1672981200],"o":[352.76,355.43,351.35,359.38],"h":[352.76,355.43,351.35,359.38],"l":[352.76,355.43,351.35,359.38],"c":[352.76,355.43,351.35,359.38]}
}

func ExampleFundCandlesRequest_packed() {
fcr, err := FundCandles().Resolution("D").Symbol("VFINX").From("2023-01-01").To("2023-01-06").Packed()
if err != nil {
fmt.Print(err)
return
}
fmt.Println(fcr)

// Output: FundCandlesResponse{Date: [1672722000 1672808400 1672894800 1672981200], Open: [352.76 355.43 351.35 359.38], High: [352.76 355.43 351.35 359.38], Low: [352.76 355.43 351.35 359.38], Close: [352.76 355.43 351.35 359.38]}
}

func ExampleFundCandlesRequest_get() {
fcr, err := FundCandles().Resolution("D").Symbol("VFINX").From("2023-01-01").To("2023-01-06").Get()
if err != nil {
fmt.Print(err)
return
}

for _, candle := range fcr {
fmt.Println(candle)
}
// Output: Candle{Date: 2023-01-03, Open: 352.76, High: 352.76, Low: 352.76, Close: 352.76}
// Candle{Date: 2023-01-04, Open: 355.43, High: 355.43, Low: 355.43, Close: 355.43}
// Candle{Date: 2023-01-05, Open: 351.35, High: 351.35, Low: 351.35, Close: 351.35}
// Candle{Date: 2023-01-06, Open: 359.38, High: 359.38, Low: 359.38, Close: 359.38}
}
Loading

0 comments on commit fd01fd3

Please sign in to comment.