Skip to content

Commit

Permalink
Feature: Ticks#Goto API
Browse files Browse the repository at this point in the history
Skip to any timeframe within tick's time boundary
  • Loading branch information
edward-yakop committed Jan 25, 2021
1 parent 65bddec commit 32521aa
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
27 changes: 25 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ type FXTHeader struct {
}
```

## 4 Streaming API
## 4 Streaming API (From v0.1)

Stream tick data given the start and end time boundary.

Expand All @@ -208,7 +208,7 @@ location, _ := time.LoadLocation("America/New_York")
start := time.Date(2017, time.January, 10, 22, 0, 0, 0, location)
end := start.Add(4 * 24 * time.Hour)

// Create new stream that each tick will only be called withing start and end boundary
// Create new stream that each tick will only be called within start and end boundary
stream := stream.New("GBPJPY", start, end, createEmptyDir(t))

// Stream ticks with the requested parameter
Expand All @@ -221,3 +221,26 @@ stream.EachTick(func(time time.Time, tick *tickdata.TickData, err error) bool {
})
```

## 5 Tick API (From v0.2)

Iterate tick data given the start and end time boundary.

``` Golang
location, _ := time.LoadLocation("America/New_York")
start := time.Date(2017, time.January, 10, 22, 0, 0, 0, location)
end := start.Add(4 * 24 * time.Hour)

// Create new ticks that iterate within start and end boundary
ticks := New("GBPJPY", start, end, createEmptyDir(t))

for !ticks.IsCompleted() {
isSuccess, err := ticks.Next() // Ticks start before start, must start with Next().
// true is returned if next successully executed
// false if ticks has reached end or error occurred

tick := ticks.Curr() // To retrieve the current tick

isGotoSucceed, err := ticks.Goto(tick.UTC().Add(time.Hour)) // Skip one hour forward
tick = ticks.Curr() // tick is now the first tick after an hour later
}
```
11 changes: 9 additions & 2 deletions api/tickdata/ticks/ticks.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ticks
import (
"github.com/ed-fx/go-duka/api/tickdata"
"github.com/ed-fx/go-duka/internal/bi5"
"github.com/pkg/errors"
"time"
"unknwon.dev/clog/v2"
)
Expand Down Expand Up @@ -60,8 +61,14 @@ func (t *Ticks) Next() (isSuccess bool, err error) {
}
}

start := t.nextDownloadHour()
for currTime := start; currTime.Before(t.end); currTime = currTime.Add(time.Hour) {
return t.Goto(t.nextDownloadHour())
}

func (t *Ticks) Goto(to time.Time) (isSuccess bool, err error) {
if to.Before(t.start) || to.After(t.end) {
return false, errors.New("")
}
for currTime := to; currTime.Before(t.end); currTime = currTime.Add(time.Hour) {
bi := bi5.New(currTime, t.symbol, t.downloadFolderPath)

// Download might return errors when there's no tick data during weekend or holiday
Expand Down
20 changes: 19 additions & 1 deletion api/tickdata/ticks/ticks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import (
_ "time/tzdata" // Ensure that custom timezone is included
)

func TestTick_IncludingWeekend(t *testing.T) {
func TestTick_IncludingWeekendAndGoto(t *testing.T) {
start := time.Date(2017, time.January, 6, 21, 0, 0, 0, time.UTC)
end := time.Date(2017, time.January, 8, 22, 59, 0, 0, time.UTC)
ticks := New("GBPJPY", start, end, createEmptyDir(t))

var isSkip = true
for {
isSuccess, nErr := ticks.Next()
assert.NoError(t, nErr)
Expand All @@ -28,6 +29,23 @@ func TestTick_IncludingWeekend(t *testing.T) {
} else {
assert.False(t, isSuccess)
}

if isSkip {
nextHour := start.Add(time.Hour)
isSuccess, nErr = ticks.Goto(nextHour)
assert.True(t, isSuccess)
assert.NoError(t, nErr)

// Confirm that the
tickTime := ticks.Current().UTC()
assert.Equal(t, nextHour.Year(), tickTime.Year())
assert.Equal(t, nextHour.Month(), tickTime.Month())
assert.Equal(t, nextHour.Day(), tickTime.Day())
assert.Equal(t, nextHour.Hour(), tickTime.Hour())
assert.Equal(t, nextHour.Minute(), tickTime.Minute())

isSkip = false
}
}
}

Expand Down

0 comments on commit 32521aa

Please sign in to comment.