Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add order imbalance stock messages #313

Merged
merged 2 commits into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions marketdata/stream/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ func (sc *StocksClient) configure(o stockOptions) {
sc.handler.updatedBarHandler = o.updatedBarHandler
sc.handler.dailyBarHandler = o.dailyBarHandler
sc.handler.tradingStatusHandler = o.tradingStatusHandler
sc.handler.imbalanceHandler = o.imbalanceHandler
sc.handler.luldHandler = o.luldHandler
sc.handler.cancelErrorHandler = o.cancelErrorHandler
sc.handler.correctionHandler = o.correctionHandler
Expand Down
25 changes: 25 additions & 0 deletions marketdata/stream/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,8 @@ func TestSubscribeBeforeConnectStocks(t *testing.T) {
assert.Equal(t, ErrSubscriptionChangeBeforeConnect, err)
err = c.SubscribeToStatuses(func(_ TradingStatus) {})
assert.Equal(t, ErrSubscriptionChangeBeforeConnect, err)
err = c.SubscribeToImbalances(func(_ Imbalance) {})
assert.Equal(t, ErrSubscriptionChangeBeforeConnect, err)
err = c.SubscribeToLULDs(func(_ LULD) {})
assert.Equal(t, ErrSubscriptionChangeBeforeConnect, err)
err = c.UnsubscribeFromTrades()
Expand All @@ -340,6 +342,8 @@ func TestSubscribeBeforeConnectStocks(t *testing.T) {
assert.Equal(t, ErrSubscriptionChangeBeforeConnect, err)
err = c.UnsubscribeFromStatuses()
assert.Equal(t, ErrSubscriptionChangeBeforeConnect, err)
err = c.UnsubscribeFromImbalances()
assert.Equal(t, ErrSubscriptionChangeBeforeConnect, err)
err = c.UnsubscribeFromLULDs()
assert.Equal(t, ErrSubscriptionChangeBeforeConnect, err)
}
Expand Down Expand Up @@ -1023,6 +1027,7 @@ func TestCoreFunctionalityStocks(t *testing.T) {
updatedBars: []string{"ALPACA"},
dailyBars: []string{"LPACA"},
statuses: []string{"ALPACA"},
imbalances: []string{"ALPACA"},
lulds: []string{"ALPACA"},
})

Expand All @@ -1032,6 +1037,7 @@ func TestCoreFunctionalityStocks(t *testing.T) {
updatedBars := make(chan Bar, 10)
dailyBars := make(chan Bar, 10)
tradingStatuses := make(chan TradingStatus, 10)
imbalances := make(chan Imbalance, 10)
lulds := make(chan LULD, 10)
cancelErrors := make(chan TradeCancelError, 10)
corrections := make(chan TradeCorrection, 10)
Expand All @@ -1042,6 +1048,7 @@ func TestCoreFunctionalityStocks(t *testing.T) {
WithUpdatedBars(func(b Bar) { updatedBars <- b }, "ALPACA"),
WithDailyBars(func(b Bar) { dailyBars <- b }, "LPACA"),
WithStatuses(func(ts TradingStatus) { tradingStatuses <- ts }, "ALPACA"),
WithImbalances(func(i Imbalance) { imbalances <- i }, "ALPACA"),
WithLULDs(func(l LULD) { lulds <- l }, "ALPACA"),
WithCancelErrors(func(tce TradeCancelError) { cancelErrors <- tce }),
WithCorrections(func(tc TradeCorrection) { corrections <- tc }),
Expand Down Expand Up @@ -1104,6 +1111,15 @@ func TestCoreFunctionalityStocks(t *testing.T) {
Tape: "C",
},
})
// sending an order imbalance
connection.readCh <- serializeToMsgpack(t, []interface{}{
imbalanceWithT{
Type: "i",
Symbol: "ALPACA",
Price: 123.456,
Tape: "C",
},
})
// sending a LULD
connection.readCh <- serializeToMsgpack(t, []interface{}{
luldWithT{
Expand Down Expand Up @@ -1190,6 +1206,13 @@ func TestCoreFunctionalityStocks(t *testing.T) {
require.Fail(t, "no trading status received in time")
}

select {
case oi := <-imbalances:
assert.EqualValues(t, 123.456, oi.Price)
case <-time.After(time.Second):
require.Fail(t, "no imbalance received in time")
}

select {
case l := <-lulds:
assert.EqualValues(t, 42.1789, l.LimitUpPrice)
Expand Down Expand Up @@ -1540,6 +1563,7 @@ func writeInitialFlowMessagesToConn(
UpdatedBars: sub.updatedBars,
DailyBars: sub.dailyBars,
Statuses: sub.statuses,
Imbalances: sub.imbalances,
LULDs: sub.lulds,
CancelErrors: sub.trades, // Subscribe automatically.
Corrections: sub.trades, // Subscribe automatically.
Expand Down Expand Up @@ -1571,6 +1595,7 @@ func checkInitialMessagesSentByClient(
require.ElementsMatch(t, sub.updatedBars, s["updatedBars"])
require.ElementsMatch(t, sub.dailyBars, s["dailyBars"])
require.ElementsMatch(t, sub.statuses, s["statuses"])
require.ElementsMatch(t, sub.imbalances, s["imbalances"])
require.ElementsMatch(t, sub.lulds, s["lulds"])
require.NotContains(t, s, "cancelErrors")
require.NotContains(t, s, "corrections")
Expand Down
8 changes: 8 additions & 0 deletions marketdata/stream/entities.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ type TradingStatus struct {
Tape string
}

// Imbalance is an order imbalance message during LULD halts for a security
type Imbalance struct {
Symbol string
Price float64
Timestamp time.Time
Tape string
}

// LULD is a Limit Up Limit Down message
type LULD struct {
Symbol string
Expand Down
Loading
Loading