Files

105 lines
2.8 KiB
Go
Raw Permalink Normal View History

2026-06-07 21:01:40 +00:00
package marketdata
import (
"context"
"fmt"
"time"
"overnight-trading-bot/internal/domain"
"overnight-trading-bot/internal/repository"
2026-06-08 09:03:37 +00:00
"overnight-trading-bot/internal/timeutil"
2026-06-07 21:01:40 +00:00
"overnight-trading-bot/internal/tinvest"
)
type Loader struct {
repo repository.Repository
gateway tinvest.Gateway
2026-06-08 09:03:37 +00:00
clock timeutil.Clock
2026-06-07 21:01:40 +00:00
}
func NewLoader(repo repository.Repository, gateway tinvest.Gateway) Loader {
2026-06-08 09:03:37 +00:00
return Loader{repo: repo, gateway: gateway, clock: timeutil.RealClock{}}
}
func (l *Loader) SetClock(clock timeutil.Clock) {
if clock != nil {
l.clock = clock
}
2026-06-07 21:01:40 +00:00
}
func (l Loader) BackfillDaily(ctx context.Context, instruments []domain.Instrument, from, to time.Time) error {
2026-06-08 09:41:20 +00:00
eligible := 0
succeeded := 0
2026-06-07 21:01:40 +00:00
for _, instrument := range instruments {
if !instrument.Enabled || instrument.Quarantine {
continue
}
2026-06-08 09:41:20 +00:00
eligible++
2026-06-07 21:01:40 +00:00
candles, err := l.gateway.GetCandles(ctx, instrument.InstrumentUID, "day", from, to)
if err != nil {
2026-06-08 14:25:44 +00:00
return fmt.Errorf("load daily candles %s: %w", instrument.Ticker, err)
2026-06-07 21:01:40 +00:00
}
if err := l.repo.UpsertDailyCandles(ctx, candles); err != nil {
return fmt.Errorf("persist candles %s: %w", instrument.Ticker, err)
}
2026-06-08 09:41:20 +00:00
succeeded++
}
2026-06-08 14:25:44 +00:00
if eligible > 0 && succeeded == 0 {
return fmt.Errorf("no daily candles loaded for eligible instruments")
2026-06-07 21:01:40 +00:00
}
return nil
}
func (l Loader) BackfillMinute(ctx context.Context, instruments []domain.Instrument, from, to time.Time) error {
2026-06-08 09:41:20 +00:00
eligible := 0
succeeded := 0
2026-06-07 21:01:40 +00:00
for _, instrument := range instruments {
if !instrument.Enabled || instrument.Quarantine {
continue
}
2026-06-08 09:41:20 +00:00
eligible++
2026-06-07 21:01:40 +00:00
candles, err := l.gateway.GetCandles(ctx, instrument.InstrumentUID, "minute", from, to)
if err != nil {
2026-06-08 14:25:44 +00:00
return fmt.Errorf("load minute candles %s: %w", instrument.Ticker, err)
2026-06-07 21:01:40 +00:00
}
if err := l.repo.UpsertMinuteCandles(ctx, candles); err != nil {
return fmt.Errorf("persist minute candles %s: %w", instrument.Ticker, err)
}
2026-06-08 09:41:20 +00:00
succeeded++
}
2026-06-08 14:25:44 +00:00
if eligible > 0 && succeeded == 0 {
return fmt.Errorf("no minute candles loaded for eligible instruments")
2026-06-07 21:01:40 +00:00
}
return nil
}
func (l Loader) LatestQuote(ctx context.Context, instrumentUID string, depth int32, maxAge time.Duration) (domain.OrderBook, error) {
book, err := l.gateway.GetOrderBook(ctx, instrumentUID, depth)
if err != nil {
return domain.OrderBook{}, err
}
2026-06-08 11:55:36 +00:00
quoteTs := quoteTimestamp(book)
if quoteTs.IsZero() {
return domain.OrderBook{}, fmt.Errorf("quote timestamp is missing")
2026-06-07 21:01:40 +00:00
}
2026-06-08 11:55:36 +00:00
age := l.nowUTC().Sub(quoteTs)
2026-06-07 21:01:40 +00:00
if maxAge > 0 && age > maxAge {
return domain.OrderBook{}, fmt.Errorf("quote age %s exceeds %s", age, maxAge)
}
return book, nil
}
2026-06-08 09:03:37 +00:00
2026-06-08 11:55:36 +00:00
func quoteTimestamp(book domain.OrderBook) time.Time {
if !book.Time.IsZero() {
return book.Time.UTC()
}
return book.ReceivedAt.UTC()
}
2026-06-08 09:03:37 +00:00
func (l Loader) nowUTC() time.Time {
if l.clock == nil {
return time.Now().UTC()
}
return l.clock.Now().UTC()
}