From c1293082cfa62f990c84d85037ad5f1e963fb5b6 Mon Sep 17 00:00:00 2001 From: Tom Lister Date: Tue, 12 Jan 2021 00:09:42 +1100 Subject: [PATCH 1/3] Change period of historical data --- data.go | 22 ++++++++++++++++++++-- examples/historical/main.go | 2 +- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/data.go b/data.go index 6c84a1a..947c646 100644 --- a/data.go +++ b/data.go @@ -63,11 +63,29 @@ type Historical struct { TravelTime int `json:"travelTime"` } +// TimeUnit represents a unit of time. +type TimeUnit string + +const ( + // Min - 1 to 30 minutes. + Min TimeUnit = "min" + // Hour - 1 to 8 hours. + Hour TimeUnit = "h" + // Day - 1 to 1000 days. + Day TimeUnit = "d" + // Week - 1 to 792 weeks. + Week TimeUnit = "w" + // Month - 1 to 182 months. + Month TimeUnit = "m" + // Year - 1 to 15 years. + Year TimeUnit = "y" +) + // Historical retrieves historical market data for a security. -func (s Security) Historical() Historical { +func (s Security) Historical(period int, unit TimeUnit) Historical { client := resty.New() client.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true}) - resp, err := client.R().Get(base + "/api/iserver/marketdata/history?conid=" + strconv.Itoa(s.Conid) + "&period=2d") + resp, err := client.R().Get(base + "/api/iserver/marketdata/history?conid=" + strconv.Itoa(s.Conid) + "&period=" + strconv.Itoa(period) + string(unit)) if err != nil { log.Panic(err) } diff --git a/examples/historical/main.go b/examples/historical/main.go index b328a23..bfd5188 100644 --- a/examples/historical/main.go +++ b/examples/historical/main.go @@ -30,7 +30,7 @@ func main() { // Create a new reference to a security from a position sec := broker.Security(p) // Retrieve the historical data for that security - historical := sec.Historical() + historical := sec.Historical(30, ib.Min) fmt.Println(historical) } } From b6a663323c6ce9ae6fde629c62918b776d2a9f47 Mon Sep 17 00:00:00 2001 From: Tom Lister Date: Tue, 12 Jan 2021 00:20:24 +1100 Subject: [PATCH 2/3] Set historical data bar size --- data.go | 4 ++-- examples/historical/main.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/data.go b/data.go index 947c646..3f14f23 100644 --- a/data.go +++ b/data.go @@ -82,10 +82,10 @@ const ( ) // Historical retrieves historical market data for a security. -func (s Security) Historical(period int, unit TimeUnit) Historical { +func (s Security) Historical(period int, unit TimeUnit, barSize int, barUnit TimeUnit) Historical { client := resty.New() client.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true}) - resp, err := client.R().Get(base + "/api/iserver/marketdata/history?conid=" + strconv.Itoa(s.Conid) + "&period=" + strconv.Itoa(period) + string(unit)) + resp, err := client.R().Get(base + "/api/iserver/marketdata/history?conid=" + strconv.Itoa(s.Conid) + "&period=" + strconv.Itoa(period) + string(unit) + "&bar=" + strconv.Itoa(barSize) + string(barUnit)) if err != nil { log.Panic(err) } diff --git a/examples/historical/main.go b/examples/historical/main.go index bfd5188..a9bec45 100644 --- a/examples/historical/main.go +++ b/examples/historical/main.go @@ -30,7 +30,7 @@ func main() { // Create a new reference to a security from a position sec := broker.Security(p) // Retrieve the historical data for that security - historical := sec.Historical(30, ib.Min) + historical := sec.Historical(2, ib.Day, 1, ib.Hour) fmt.Println(historical) } } From e6741a026d7432879bf3e665651b3bcb4419ef7b Mon Sep 17 00:00:00 2001 From: Tom Lister Date: Tue, 12 Jan 2021 00:21:13 +1100 Subject: [PATCH 3/3] Update example --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 9803fdf..5cfd7ae 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ func main() { futures := positions.FilterAssets(ib.Futures) for _, p := range futures { sec := broker.Security(p) - historical := sec.Historical() + historical := sec.Historical(2, ib.Day, 1, ib.Hour) fmt.Println(historical) } }