Skip to content

Commit

Permalink
Merge pull request #1 from tomlister/feature-historical-period
Browse files Browse the repository at this point in the history
Feature: Historical Market Data Period and Bar Size
  • Loading branch information
tomlister authored Jan 11, 2021
2 parents 9141b68 + e6741a0 commit f6bf32d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
22 changes: 20 additions & 2 deletions data.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, 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=2d")
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)
}
Expand Down
2 changes: 1 addition & 1 deletion examples/historical/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(2, ib.Day, 1, ib.Hour)
fmt.Println(historical)
}
}

0 comments on commit f6bf32d

Please sign in to comment.