Longbridge.MarketContext (longbridge v0.1.0)

Copy Markdown View Source

Market context.

Provides market-level data: trading status, broker holdings, A/H share premiums, trade statistics, index constituents, and anomaly alerts.

All functions accept a Longbridge.Config struct and return {:ok, data} | {:error, reason} tuples.

Usage

config = Longbridge.Config.new(...)

{:ok, status} = Longbridge.MarketContext.market_session(config, "US")
{:ok, constituents} = Longbridge.MarketContext.index_constituents(config, "HSI.HK")

Summary

Functions

Returns A/H share premium K-line data for a dual-listed security.

Lists market anomaly alerts (trading halts, suspensions, etc.).

Returns top broker holdings (buy/sell leaders) for a symbol.

Returns the constituents of a market index.

Returns the current trading session for all markets.

Returns the ranked security list for a rank category.

Returns top market movers — stocks whose price movement exceeds their 20-day standard deviation, with linked news context.

Returns buy/sell/neutral trade statistics for a symbol.

Functions

ah_premium(config, symbol, opts \\ [], http_opts \\ [])

@spec ah_premium(Longbridge.Config.t(), String.t(), keyword(), keyword()) ::
  {:ok, map()} | {:error, term()}

Returns A/H share premium K-line data for a dual-listed security.

symbol must be the H-share counterpart, e.g. "2318.HK" (Ping An). The upstream derives the A-share pair server-side.

Options

  • :period:day (default), :week, :month, :quarter, :year
  • :count — number of klines (default 100)

anomaly_alerts(config, market \\ "US", opts \\ [])

@spec anomaly_alerts(Longbridge.Config.t(), String.t(), keyword()) ::
  {:ok, map()} | {:error, term()}

Lists market anomaly alerts (trading halts, suspensions, etc.).

market is a region code: "US", "HK", "CN", "SG". Defaults to "US".

Endpoint: GET /v1/quote/changes?market=<m>&category=0. The response has "all_off" (true if no active alerts) and "changes" (list of %{"symbol", "type", "title_cn" | "title_en", "update_at"} entries).

broker_holdings(config, symbol, opts \\ [], http_opts \\ [])

@spec broker_holdings(Longbridge.Config.t(), String.t(), keyword(), keyword()) ::
  {:ok, map()} | {:error, term()}

Returns top broker holdings (buy/sell leaders) for a symbol.

Options

  • :period:rct_1 (1d, default), :rct_5, :rct_20, :rct_60

index_constituents(config, index_symbol, opts \\ [])

@spec index_constituents(Longbridge.Config.t(), String.t(), keyword()) ::
  {:ok, map()} | {:error, term()}

Returns the constituents of a market index.

index_symbol examples: "HSI.HK", "HSCEI.HK", "HSTECH.HK".

market_session(config, opts \\ [])

@spec market_session(
  Longbridge.Config.t(),
  keyword()
) :: {:ok, map()} | {:error, term()}

Returns the current trading session for all markets.

The upstream /v1/quote/market-status endpoint does not accept a market filter; the response includes US/HK/CN/SG entries that callers filter client-side.

rank_categories(config, opts \\ [])

@spec rank_categories(
  Longbridge.Config.t(),
  keyword()
) :: {:ok, list()} | {:error, term()}

Lists rank categories for rank_list/2.

Endpoint: GET /v1/quote/market/rank/categories

Returns a list of categories keyed by their ib_<key> IDs. Pass the value of a category entry's key field to rank_list/2.

rank_list(config, key, opts \\ [], http_opts \\ [])

@spec rank_list(Longbridge.Config.t(), String.t(), keyword(), keyword()) ::
  {:ok, map()} | {:error, term()}

Returns the ranked security list for a rank category.

Endpoint: GET /v1/quote/market/rank/list?key=ib_<key>

Pass the key value from one of the entries returned by rank_categories/1. The ib_ prefix is added automatically if missing.

Options

  • :need_article — boolean (default false). When true, the response includes article content.

top_movers(config, opts \\ [], http_opts \\ [])

@spec top_movers(Longbridge.Config.t(), keyword(), keyword()) ::
  {:ok, map()} | {:error, term()}

Returns top market movers — stocks whose price movement exceeds their 20-day standard deviation, with linked news context.

Endpoint: POST /v1/quote/market/stock-events

Options

  • :markets — list of "HK" | "US" | "CN" | "SG". Omit (or pass []) for all markets.

  • :sort:hot (default), :time, or :change.
  • :date"YYYY-MM-DD" filter. Optional.
  • :limit — integer 1-100, default 20.

Renamed from stock_events in longbridge/openapi 4.2.0.

trade_status(config, symbol, opts \\ [])

@spec trade_status(Longbridge.Config.t(), String.t(), keyword()) ::
  {:ok, map()} | {:error, term()}

Returns buy/sell/neutral trade statistics for a symbol.

Endpoint: GET /v1/quote/trades-statistics?counter_id=.... The response is shaped as %{"statistics" => %{"buy_ratio" => ..., "sell_ratio" => ..., ...}, "trades" => [%{"price", "volume", "direction", "ts"}, ...]} — a windowed summary plus the recent trade tape used to derive it.

trading_days(config, start_date, end_date, market)

This function is deprecated. Use Longbridge.CalendarContext.fetch/5 with category: :closed.
@spec trading_days(Longbridge.Config.t(), String.t(), String.t(), String.t()) ::
  {:error, :removed_upstream}