ticker

package module
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 16, 2026 License: MIT Imports: 8 Imported by: 0

README

kite-mcp-ticker

Go Reference

Websocket ticker service for the algo2go ecosystem. Provides live tick subscription via the Kite WebSocket API, callback dispatch, leak-sentinel goroutine cleanup, and race-flag-on/off lifecycle testing.

Used by Sundeepg98/kite-mcp-server for live market data subscription, alert evaluation triggers, trailing-stop monitoring, and dashboard SSE streaming.

Why a separate module?

Live tick subscription is a foundational primitive for any algo2go project that consumes streaming market data independent of kite-mcp-server. Hosting as a module:

  • Centralizes the websocket lifecycle + callback dispatch contract
  • Pairs with algo2go/kite-mcp-broker (Kite SDK adapter)

Stability promise

v0.x — unstable. Pin v0.1.0 deliberately.

Install

go get github.com/algo2go/kite-mcp-ticker@v0.1.0

Public API

  • Service — websocket ticker lifecycle (Start/Stop/Subscribe/Unsubscribe)
  • Callback registration for tick + connect + disconnect events
  • Leak-sentinel goroutine accounting for tests

Dependencies

  • github.com/algo2go/kite-mcp-broker v0.1.0 — broker adapter + ticker port + zerodha subpackage
  • github.com/algo2go/kite-mcp-money v0.1.0 (transitive)
  • github.com/zerodha/gokiteconnect/v4 — Kite SDK
  • github.com/stretchr/testify — assertions
  • go.uber.org/goleak — leak detection

All algo2go deps published; no upstream replace directives needed.

Reference consumer

Sundeepg98/kite-mcp-server — consumed across kc/manager_init.go, kc/broker_services.go, kc/usecases/ticker_usecases.go, kc/ops/data.go, kc/telegram/bot.go, mcp/alerts/alert_tools.go, mcp/misc/ticker_tools.go, mcp/trade/trailing_tools.go.

License

MIT — see LICENSE.

Authors

Original design: Sundeepg98 (Zerodha Tech). Multi-module promotion (2026-05-10): algo2go contributors.

Documentation

Index

Constants

View Source
const (
	ModeLTP   = brokerticker.ModeLTP
	ModeQuote = brokerticker.ModeQuote
	ModeFull  = brokerticker.ModeFull
)

Mode aliases for the broker-agnostic mode constants. Same byte values as kiteticker.ModeLTP/ModeQuote/ModeFull (typed string "ltp"/"quote"/"full"); existing kc/ticker.ModeLTP callsites continue to work unchanged.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Logger *slog.Logger
	OnTick TickCallback // optional: global tick handler (e.g. alert evaluator)
}

Config holds configuration for creating a new ticker Service.

type Mode

type Mode = brokerticker.Mode

Mode aliases the broker-agnostic broker/ticker.Mode so external consumers (mcp/ticker_tools.go, mcp/alert_tools.go, mcp/trailing_ tools.go) keep using kc/ticker.Mode unchanged. Migration target for post-launch cleanup: callsites import broker/ticker directly.

type Service

type Service struct {
	// contains filtered or unexported fields
}

Service manages per-user WebSocket ticker connections.

func New

func New(cfg Config) *Service

New creates a new ticker Service.

func (*Service) GetStatus

func (s *Service) GetStatus(email string) (*Status, error)

GetStatus returns the current status of a user's ticker.

func (*Service) IsRunning

func (s *Service) IsRunning(email string) bool

IsRunning returns true if a ticker is active for the given email.

func (*Service) ListAll

func (s *Service) ListAll() []UserTickerInfo

ListAll returns a summary of all active ticker connections.

func (*Service) Shutdown

func (s *Service) Shutdown()

Shutdown stops all running tickers.

func (*Service) Start

func (s *Service) Start(email, apiKey, accessToken string) error

Start creates and starts a WebSocket ticker for the given user. If a ticker is already running for this email, it returns an error.

func (*Service) Stop

func (s *Service) Stop(email string) error

Stop stops the ticker for the given user.

func (*Service) Subscribe

func (s *Service) Subscribe(email string, tokens []uint32, mode brokerticker.Mode) error

Subscribe subscribes the user's ticker to the given instrument tokens with the specified mode.

func (*Service) Unsubscribe

func (s *Service) Unsubscribe(email string, tokens []uint32) error

Unsubscribe removes instrument tokens from the user's ticker.

func (*Service) UpdateToken

func (s *Service) UpdateToken(email, apiKey, accessToken string) error

UpdateToken stops the existing ticker and starts a new one with the fresh token. Preserves subscriptions across the restart. The entire operation is serialized under s.mu to prevent concurrent callers from seeing inconsistent state between stop and start.

type Status

type Status struct {
	Running       bool               `json:"running"`
	Connected     bool               `json:"connected"`
	StartedAt     time.Time          `json:"started_at,omitempty"`
	Uptime        string             `json:"uptime,omitempty"`
	Subscriptions []SubscriptionInfo `json:"subscriptions,omitempty"`
}

Status represents the current state of a user's ticker connection.

type SubscriptionInfo

type SubscriptionInfo struct {
	InstrumentToken uint32 `json:"instrument_token"`
	Mode            string `json:"mode"`
}

SubscriptionInfo represents a subscribed instrument.

type TickCallback

type TickCallback func(email string, tick models.Tick)

TickCallback is invoked on each incoming tick for a user. Carries gokiteconnect models.Tick because kc/alerts.Evaluator.Evaluate takes that signature today; the broker/ticker.Tick translation happens INSIDE wireCallbacks so the kiteticker dependency stays hidden from anything outside this package + kc/alerts.

Future cleanup (out of scope for the multi-broker port-adapter commit): kc/alerts.Evaluator.Evaluate accepts broker/ticker.Tick; this signature swaps too; the round-trip translation in wireCallbacks is dropped.

type UserTicker

type UserTicker struct {
	Email       string
	APIKey      string
	AccessToken string
	Ticker      brokerticker.Ticker
	Cancel      context.CancelFunc
	Connected   bool
	StartedAt   time.Time
	Subscribed  map[uint32]brokerticker.Mode // token -> mode
	// contains filtered or unexported fields
}

UserTicker holds a single user's WebSocket ticker connection.

Ticker holds a broker/ticker.Ticker port — production wires *zerodha.TickerAdapter (which wraps *kiteticker.Ticker); future non-Zerodha adapters slot in here without touching this struct.

type UserTickerInfo

type UserTickerInfo struct {
	Email         string    `json:"email"`
	Connected     bool      `json:"connected"`
	StartedAt     time.Time `json:"started_at"`
	Subscriptions int       `json:"subscriptions"`
}

UserTickerInfo is a summary of a user's ticker connection for admin display.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL