thirteenth version

This commit is contained in:
2026-06-09 21:04:01 +00:00
parent f877907b20
commit 4dec14f57c
19 changed files with 602 additions and 110 deletions
+24
View File
@@ -17,6 +17,7 @@ var ErrNotFound = errors.New("not found")
type Gateway interface {
GetInstrument(ctx context.Context, ticker, classCode string) (domain.Instrument, error)
GetCandles(ctx context.Context, instrumentUID string, interval string, from, to time.Time) ([]domain.Candle, error)
GetTradingDays(ctx context.Context, exchange string, from, to time.Time) ([]time.Time, error)
GetOrderBook(ctx context.Context, instrumentUID string, depth int32) (domain.OrderBook, error)
GetTradingStatus(ctx context.Context, instrumentUID string) (domain.TradingStatus, error)
PostLimitOrder(ctx context.Context, accountID, instrumentUID string, side domain.Side, lots int64, price decimal.Decimal, clientOrderID string) (domain.Order, error)
@@ -34,6 +35,8 @@ type FakeGateway struct {
InstrumentErrors map[string]error
Candles map[string][]domain.Candle
CandleErrors map[string]error
TradingDays []time.Time
TradingDayError error
OrderBooks map[string]domain.OrderBook
Statuses map[string]domain.TradingStatus
Orders map[string]domain.Order
@@ -88,6 +91,22 @@ func (f *FakeGateway) GetCandles(_ context.Context, instrumentUID string, _ stri
return out, nil
}
func (f *FakeGateway) GetTradingDays(_ context.Context, _ string, from, to time.Time) ([]time.Time, error) {
f.mu.Lock()
defer f.mu.Unlock()
if f.TradingDayError != nil {
return nil, f.TradingDayError
}
var out []time.Time
for _, day := range f.TradingDays {
day = dateOnly(day)
if !day.Before(dateOnly(from)) && !day.After(dateOnly(to)) {
out = append(out, day)
}
}
return out, nil
}
func (f *FakeGateway) GetOrderBook(_ context.Context, instrumentUID string, _ int32) (domain.OrderBook, error) {
f.mu.Lock()
defer f.mu.Unlock()
@@ -226,6 +245,11 @@ func (f *FakeGateway) GetServerTime(context.Context) (time.Time, error) {
return f.ServerTime, nil
}
func dateOnly(ts time.Time) time.Time {
year, month, day := ts.UTC().Date()
return time.Date(year, month, day, 0, 0, 0, 0, time.UTC)
}
func isTerminalFakeOrder(status domain.OrderStatus) bool {
return status == domain.OrderStatusFilled ||
status == domain.OrderStatusCancelled ||
+7
View File
@@ -39,6 +39,13 @@ func (g *PaperGateway) GetCandles(ctx context.Context, instrumentUID string, int
return g.Fake().GetCandles(ctx, instrumentUID, interval, from, to)
}
func (g *PaperGateway) GetTradingDays(ctx context.Context, exchange string, from, to time.Time) ([]time.Time, error) {
if g.market != nil {
return g.market.GetTradingDays(ctx, exchange, from, to)
}
return g.Fake().GetTradingDays(ctx, exchange, from, to)
}
func (g *PaperGateway) GetOrderBook(ctx context.Context, instrumentUID string, depth int32) (domain.OrderBook, error) {
if g.market != nil {
return g.market.GetOrderBook(ctx, instrumentUID, depth)
+32 -1
View File
@@ -59,7 +59,7 @@ func NewRealGateway(ctx context.Context, opts Options) (*RealGateway, error) {
AppName: opts.AppName,
AccountId: opts.AccountID,
MaxRetries: 0,
}, logging.SDKLogger{Logger: opts.Logger})
}, logging.NewSDKLogger(opts.Logger))
if err != nil {
return nil, err
}
@@ -156,6 +156,37 @@ func (g *RealGateway) GetCandles(ctx context.Context, instrumentUID string, inte
return out, nil
}
func (g *RealGateway) GetTradingDays(ctx context.Context, exchange string, from, to time.Time) ([]time.Time, error) {
if err := ctx.Err(); err != nil {
return nil, err
}
resp, err := requestWithTimeout(ctx, g.requestTimeout, func(callCtx context.Context) (*pb.TradingSchedulesResponse, error) {
return retryValue(callCtx, g.retryAttempts, g.retryBackoff, func() (*pb.TradingSchedulesResponse, error) {
return g.instrumentsPB.TradingSchedules(callCtx, &pb.TradingSchedulesRequest{
Exchange: &exchange,
From: investgo.TimeToTimestamp(from),
To: investgo.TimeToTimestamp(to),
})
})
})
if err != nil {
return nil, err
}
var days []time.Time
for _, schedule := range resp.GetExchanges() {
if !strings.EqualFold(schedule.GetExchange(), exchange) {
continue
}
for _, day := range schedule.GetDays() {
if !day.GetIsTradingDay() || day.GetDate() == nil {
continue
}
days = append(days, dateOnly(day.GetDate().AsTime()))
}
}
return days, nil
}
func (g *RealGateway) GetOrderBook(ctx context.Context, instrumentUID string, depth int32) (domain.OrderBook, error) {
if err := ctx.Err(); err != nil {
return domain.OrderBook{}, err