sixth version
This commit is contained in:
@@ -463,6 +463,12 @@ func (s *Scheduler) placeEntryOrders(ctx context.Context, now time.Time) error {
|
||||
if err != nil {
|
||||
tradingStatus = domain.TradingStatusUnknown
|
||||
}
|
||||
if err := s.checkEntryInstrumentBeforeOrder(instrument, tradingStatus); err != nil {
|
||||
if insertErr := s.recordPreTradeReject(ctx, sig.InstrumentUID, err.Error(), `{"reason":"instrument_pre_trade"}`); insertErr != nil {
|
||||
return insertErr
|
||||
}
|
||||
continue
|
||||
}
|
||||
portfolio, err = s.svc.Gateway.GetPortfolio(ctx, s.svc.AccountID)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -1153,6 +1159,12 @@ func (s Scheduler) repostPreTradeCheck(ctx context.Context, now time.Time, order
|
||||
if err != nil {
|
||||
tradingStatus = domain.TradingStatusUnknown
|
||||
}
|
||||
if order.Side == domain.SideBuy {
|
||||
if err := s.checkEntryInstrumentBeforeOrder(instrument, tradingStatus); err != nil {
|
||||
_ = s.recordPreTradeReject(ctx, order.InstrumentUID, err.Error(), `{"reason":"instrument_pre_trade","stage":"repost"}`)
|
||||
return err
|
||||
}
|
||||
}
|
||||
portfolio, err := s.svc.Gateway.GetPortfolio(ctx, s.svc.AccountID)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -1172,6 +1184,16 @@ func (s Scheduler) repostPreTradeCheck(ctx context.Context, now time.Time, order
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s Scheduler) checkEntryInstrumentBeforeOrder(instrument domain.Instrument, tradingStatus domain.TradingStatus) error {
|
||||
if err := instruments.CheckInstrument(instrument, tradingStatus); err != nil {
|
||||
return err
|
||||
}
|
||||
if s.cfg.RequireZeroCommission && instrument.ExpectedCommissionBpsPerSide.IsPositive() {
|
||||
return errors.New(signal.ReasonCommission)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s Scheduler) preTradeCheck(ctx context.Context, now time.Time, instrumentUID string, portfolio domain.Portfolio, openPositions int, tradingStatus domain.TradingStatus, quoteReceivedAt time.Time) (risk.PreTradeResult, error) {
|
||||
metrics, err := s.riskMetrics(ctx, now, portfolio)
|
||||
if err != nil {
|
||||
|
||||
@@ -288,6 +288,34 @@ func TestNonZeroCommissionQuarantinesInstrumentAndHalts(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestEntryInstrumentPreTradeRejectsQuarantineAndCommission(t *testing.T) {
|
||||
s := Scheduler{cfg: Config{RequireZeroCommission: true}}
|
||||
err := s.checkEntryInstrumentBeforeOrder(domain.Instrument{
|
||||
InstrumentUID: "uid",
|
||||
Ticker: "TRUR",
|
||||
Enabled: true,
|
||||
Quarantine: true,
|
||||
Lot: 1,
|
||||
MinPriceIncrement: decimal.NewFromInt(1),
|
||||
Currency: "RUB",
|
||||
}, domain.TradingStatusNormal)
|
||||
if err == nil {
|
||||
t.Fatal("expected quarantine rejection")
|
||||
}
|
||||
err = s.checkEntryInstrumentBeforeOrder(domain.Instrument{
|
||||
InstrumentUID: "uid",
|
||||
Ticker: "TRUR",
|
||||
Enabled: true,
|
||||
Lot: 1,
|
||||
MinPriceIncrement: decimal.NewFromInt(1),
|
||||
Currency: "RUB",
|
||||
ExpectedCommissionBpsPerSide: decimal.NewFromInt(1),
|
||||
}, domain.TradingStatusNormal)
|
||||
if err == nil || err.Error() != signalengine.ReasonCommission {
|
||||
t.Fatalf("err=%v, want commission rejection", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPreTradeDailyLossBreachHalts(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
repo := testutil.NewMemoryRepository()
|
||||
@@ -481,13 +509,14 @@ func TestPlaceEntryRejectsWideSpreadBeforeOrder(t *testing.T) {
|
||||
repo := testutil.NewMemoryRepository()
|
||||
tradeDate := time.Date(2026, 6, 6, 0, 0, 0, 0, time.UTC)
|
||||
instrument := domain.Instrument{
|
||||
InstrumentUID: "uid",
|
||||
Ticker: "TRUR",
|
||||
ClassCode: "TQTF",
|
||||
Enabled: true,
|
||||
Lot: 1,
|
||||
MinPriceIncrement: decimal.RequireFromString("0.01"),
|
||||
Currency: "RUB",
|
||||
InstrumentUID: "uid",
|
||||
Ticker: "TRUR",
|
||||
ClassCode: "TQTF",
|
||||
Enabled: true,
|
||||
Lot: 1,
|
||||
MinPriceIncrement: decimal.RequireFromString("0.01"),
|
||||
Currency: "RUB",
|
||||
FreeOrderLimitPerDay: -1,
|
||||
}
|
||||
if err := repo.UpsertInstrument(ctx, instrument); err != nil {
|
||||
t.Fatal(err)
|
||||
|
||||
Reference in New Issue
Block a user