fifth version
This commit is contained in:
@@ -7,16 +7,24 @@ import (
|
||||
|
||||
"overnight-trading-bot/internal/domain"
|
||||
"overnight-trading-bot/internal/repository"
|
||||
"overnight-trading-bot/internal/timeutil"
|
||||
"overnight-trading-bot/internal/tinvest"
|
||||
)
|
||||
|
||||
type Loader struct {
|
||||
repo repository.Repository
|
||||
gateway tinvest.Gateway
|
||||
clock timeutil.Clock
|
||||
}
|
||||
|
||||
func NewLoader(repo repository.Repository, gateway tinvest.Gateway) Loader {
|
||||
return Loader{repo: repo, gateway: gateway}
|
||||
return Loader{repo: repo, gateway: gateway, clock: timeutil.RealClock{}}
|
||||
}
|
||||
|
||||
func (l *Loader) SetClock(clock timeutil.Clock) {
|
||||
if clock != nil {
|
||||
l.clock = clock
|
||||
}
|
||||
}
|
||||
|
||||
func (l Loader) BackfillDaily(ctx context.Context, instruments []domain.Instrument, from, to time.Time) error {
|
||||
@@ -59,9 +67,16 @@ func (l Loader) LatestQuote(ctx context.Context, instrumentUID string, depth int
|
||||
if book.ReceivedAt.IsZero() {
|
||||
return domain.OrderBook{}, fmt.Errorf("quote received timestamp is missing")
|
||||
}
|
||||
age := time.Since(book.ReceivedAt)
|
||||
age := l.nowUTC().Sub(book.ReceivedAt)
|
||||
if maxAge > 0 && age > maxAge {
|
||||
return domain.OrderBook{}, fmt.Errorf("quote age %s exceeds %s", age, maxAge)
|
||||
}
|
||||
return book, nil
|
||||
}
|
||||
|
||||
func (l Loader) nowUTC() time.Time {
|
||||
if l.clock == nil {
|
||||
return time.Now().UTC()
|
||||
}
|
||||
return l.clock.Now().UTC()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user