forked from leifcr/go-duka
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Automatically download requested tick data from dukascopy bound by start and end time * Iterator style API
- Loading branch information
1 parent
21dbcc9
commit 65bddec
Showing
7 changed files
with
283 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package ticks | ||
|
||
import ( | ||
"github.com/ed-fx/go-duka/api/tickdata" | ||
"github.com/ed-fx/go-duka/internal/bi5" | ||
"time" | ||
"unknwon.dev/clog/v2" | ||
) | ||
|
||
type Ticks struct { | ||
symbol string | ||
start time.Time | ||
end time.Time | ||
downloadFolderPath string | ||
|
||
currTick *tickdata.TickData | ||
ticksIdx int | ||
ticks []*tickdata.TickData | ||
ticksDayHour time.Time | ||
isCompleted bool | ||
} | ||
|
||
func (t Ticks) Start() time.Time { | ||
return t.start | ||
} | ||
|
||
func (t Ticks) End() time.Time { | ||
return t.end | ||
} | ||
|
||
func (t Ticks) Current() *tickdata.TickData { | ||
return t.currTick | ||
} | ||
|
||
func (t Ticks) IsCompleted() bool { | ||
return t.isCompleted | ||
} | ||
|
||
func (t *Ticks) Next() (isSuccess bool, err error) { | ||
if t.isCompleted { | ||
return | ||
} | ||
|
||
// IF ticks loaded, let's check whether it's between boundaries | ||
if !t.ticksDayHour.IsZero() { | ||
nextTickIdx := t.ticksIdx + 1 | ||
if nextTickIdx < len(t.ticks) { | ||
nextTick := t.ticks[nextTickIdx] | ||
nextTickTime := nextTick.UTC() | ||
if nextTickTime.Before(t.end) || nextTickTime.Equal(t.end) { | ||
t.currTick = nextTick | ||
t.ticksIdx = nextTickIdx | ||
|
||
isSuccess = true | ||
return | ||
} else { | ||
t.complete() | ||
return | ||
} | ||
} | ||
} | ||
|
||
start := t.nextDownloadHour() | ||
for currTime := start; 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 | ||
if bi.Download() == nil { | ||
t.ticks, err = bi.Ticks() | ||
if err != nil { | ||
t.complete() | ||
return | ||
} else if len(t.ticks) != 0 { | ||
t.ticksDayHour = currTime | ||
t.ticksIdx = t.searchTickIdx() | ||
t.currTick = nil | ||
|
||
return t.Next() | ||
} | ||
} | ||
} | ||
|
||
t.End() | ||
return | ||
} | ||
|
||
func (t *Ticks) complete() { | ||
t.isCompleted = true | ||
t.ticksIdx = -1 | ||
t.ticks = nil | ||
t.currTick = nil | ||
} | ||
|
||
func (t Ticks) nextDownloadHour() time.Time { | ||
var next time.Time | ||
if t.currTick == nil { | ||
next = t.start.UTC() | ||
} else { | ||
next = t.currTick.UTC().Add(time.Hour) | ||
} | ||
|
||
return time.Date(next.Year(), next.Month(), next.Day(), next.Hour(), 0, 0, 0, time.UTC) | ||
} | ||
|
||
func (t Ticks) searchTickIdx() (idx int) { | ||
count := len(t.ticks) | ||
for idx = 0; idx < count; idx++ { | ||
tick := t.ticks[idx] | ||
if !tick.UTC().Before(t.start) { | ||
break | ||
} | ||
} | ||
|
||
return idx - 1 | ||
} | ||
|
||
var isLogSetup = false | ||
|
||
// time are in UTC | ||
func New(symbol string, start time.Time, end time.Time, downloadFolderPath string) *Ticks { | ||
if !isLogSetup { | ||
isLogSetup = true | ||
clog.NewConsole(0, clog.ConsoleConfig{ | ||
Level: clog.LevelInfo, | ||
}) | ||
} | ||
|
||
return &Ticks{ | ||
symbol: symbol, | ||
start: start, | ||
end: end, | ||
downloadFolderPath: downloadFolderPath, | ||
|
||
ticksDayHour: time.Time{}, | ||
ticksIdx: -1, | ||
isCompleted: false, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package ticks | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"io/ioutil" | ||
"os" | ||
"testing" | ||
"time" | ||
_ "time/tzdata" // Ensure that custom timezone is included | ||
) | ||
|
||
func TestTick_IncludingWeekend(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)) | ||
|
||
for { | ||
isSuccess, nErr := ticks.Next() | ||
assert.NoError(t, nErr) | ||
|
||
if !ticks.IsCompleted() { | ||
assert.True(t, isSuccess) | ||
tick := ticks.Current() | ||
|
||
assert.NotNil(t, tick) | ||
assertTime(t, tick.UTC(), start, end) | ||
break | ||
} else { | ||
assert.False(t, isSuccess) | ||
} | ||
} | ||
} | ||
|
||
func createEmptyDir(t *testing.T) string { | ||
dir, err := ioutil.TempDir(".", "test") | ||
if !assert.NoError(t, err) { | ||
t.FailNow() | ||
} | ||
t.Cleanup(func() { | ||
_ = os.RemoveAll(dir) | ||
}) | ||
return dir | ||
} | ||
|
||
func assertTime(t *testing.T, time time.Time, start time.Time, end time.Time) { | ||
if !assert.True(t, start.Before(time) || start.Equal(time)) { | ||
t.FailNow() | ||
} | ||
if !assert.True(t, end.Equal(time) || end.After(time)) { | ||
t.FailNow() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.