OptionChain

463
OptionChain

Overview

OptionChain is a Pine Script v6 library for reading an option chain from TradingView data inside an indicator or strategy. Each strike is requested with one call, scored with a smoothed volume put/call ratio (PCR) and a price / volume flow score, and the whole chain can be summarised into one chain PCR and one chain flow reading.

Tickers use TradingView's option format, for example NSE:NIFTY260224C23500 (exchange, root symbol, expiry as YYMMDD, C or P, strike).

What's inside

  • row(exch, root, expiry, strike): requests the strike's call and put (last price and volume) and returns a Row with both legs, their previous-bar values, the SWMA-smoothed volume PCR (and its previous value) and the combined flow score. A strike that does not exist returns na values instead of an error.
  • summary(rows): SWMA-smoothed PCR of the total put and call volume, and the SWMA-smoothed sum of the row flow scores.
  • pcrAt(), ceLtpAt(), peLtpAt(): look a strike up in the chain (na when the strike is not in it).
  • volumePcr(putVol, callVol): the put/call ratio with defined results when a side is zero.
  • pvFlow(px, prevPx, vol, prevVol, isCall): price / volume flow score of one option from the underlying's point of view. For a call, rising price on rising volume scores +2 and falling price on rising volume -2; a put is the mirror.
  • flowBias(), pcrBias(), setupText(): readable labels for flow, PCR and the combined setup (LONG / SHORT / WAIT with the reason).
  • drawTable(t, theme, center, ceStrike, peStrike, ...): draws the chain as a table: CE price and volume, strike, smoothed PCR, flow, PE volume and price for every strike, each with its change against the previous bar, the traded strikes and the center strike highlighted, and a footer with the chain PCR, chain flow and setup. Uses my UIKit library for the table theme.


Quick start

Pine Script®


Notes

  • Each row() call makes two request.security() calls. Keep the strike list fixed (for example from inputs) so each ticker counts once toward TradingView's request limit, and mind that limit when combining the chain with other requests.
  • Call row() and summary() once per bar, unconditionally, so the SWMA smoothing and the previous-bar values stay consistent.
  • Option volume and prices come from TradingView's data for the listed contracts; illiquid strikes may have gaps.


Demo

Add the library to a chart on its own to see a demo that uses every exported function. It reads a 5-strike NIFTY chain, so set the expiry and center strike in its settings to a listed series. Single-value readings are in the Data Window, and the bias and setup labels are written to the Pine Logs pane.


Library "OptionChain"
Option chain on TradingView data. `row()` requests one strike's call and put (last price and volume)
and scores it: an SWMA-smoothed volume put/call ratio and a price/volume flow score. `summary()` gives
the chain-wide PCR and flow, the `*At()` methods look a strike up, and the text helpers label biases.
Tickers use TradingView's option format, e.g. NSE:NIFTY260224C23500.

volumePcr(putVol, callVol)
  Volume put/call ratio.
  Parameters:
    putVol (float): (series float) Put volume.
    callVol (float): (series float) Call volume.
  Returns: (float) put / call; 0 when both are 0, 10 when only the call side is 0, `na` when either is `na`.

pvFlow(px, prevPx, vol, prevVol, isCall)
  Price / volume flow score of one option, from the underlying's point of view. Call: price and volume up
= +2 (buyers), price up on falling volume = +1, price down on rising volume = -2 (writers), else 0.
Put: the mirror, plus price and volume down = +1.
  Parameters:
    px (float): (series float) Option price.
    prevPx (float): (series float) Previous option price.
    vol (float): (series float) Option volume.
    prevVol (float): (series float) Previous option volume.
    isCall (bool): (series bool) `true` for a call, `false` for a put.
  Returns: (float) The score, or `na` when any input is `na`.

flowBias(score)
  Flow bias label: >= 3 "Strong Bull", >= 1 "Bull", <= -3 "Strong Bear", <= -1 "Bear", else "Neutral".
  Parameters:
    score (float): (series float) A flow score.
  Returns: (string) The label, or "NA".

pcrBias(pcr, bullAbove, bearBelow)
  PCR bias label.
  Parameters:
    pcr (float): (series float) A put/call ratio.
    bullAbove (float): (series float) Bullish above this PCR.
    bearBelow (float): (series float) Bearish below this PCR.
  Returns: (string) "Bullish", "Bearish", "Neutral" or "NA".

setupText(pcr, flow, bullAbove, bearBelow)
  Combined PCR + flow setup: LONG when both are bullish (Strong when flow >= 3), SHORT when both are
bearish, otherwise WAIT (Conflict / Unconfirmed / Neutral).
  Parameters:
    pcr (float): (series float) A put/call ratio.
    flow (float): (series float) A flow score.
    bullAbove (float): (series float) Bullish above this PCR.
    bearBelow (float): (series float) Bearish below this PCR.
  Returns: (string) The setup label.

row(exch, root, expiry, strike)
  Requests one strike's call and put (two `request.security()` calls on the chart timeframe; a strike
that doesn't exist returns `na` values) and scores it. Call it once per bar per strike, unconditionally,
so each call keeps its own history.
  Parameters:
    exch (string): (series string) TradingView exchange of the options, e.g. "NSE".
    root (string): (series string) Underlying root symbol, e.g. "NIFTY".
    expiry (string): (series string) Expiry in TradingView's YYMMDD form, e.g. "260224".
    strike (int): (series int) Strike price.
  Returns: (Row) The strike's data and scores.

summary(rows)
  Chain-wide readings: the SWMA-smoothed PCR of the total put and call volume, and the SWMA-smoothed sum
of the row flow scores. Call it once per bar, unconditionally.
  Parameters:
    rows (array<Row>): (array<Row>) The chain's rows.
  Returns: ([float, float]) The smoothed chain PCR, and the smoothed chain flow (`na` when no row has a flow score).

method pcrAt(rows, strike)
  Smoothed PCR of a strike.
  Namespace types: array<Row>
  Parameters:
    rows (array<Row>): (array<Row>) The chain's rows.
    strike (int): (series int) Strike to look up.
  Returns: (float) The strike's smoothed PCR, or `na` when the strike isn't in the chain.

method ceLtpAt(rows, strike)
  Call last price of a strike.
  Namespace types: array<Row>
  Parameters:
    rows (array<Row>): (array<Row>) The chain's rows.
    strike (int): (series int) Strike to look up.
  Returns: (float) The call's last price, or `na` when the strike isn't in the chain.

method peLtpAt(rows, strike)
  Put last price of a strike.
  Namespace types: array<Row>
  Parameters:
    rows (array<Row>): (array<Row>) The chain's rows.
    strike (int): (series int) Strike to look up.
  Returns: (float) The put's last price, or `na` when the strike isn't in the chain.

method drawTable(rows, t, th, center, ceStrike, peStrike, cumPcr, cumPcrPrev, cumFlow, cumFlowPrev, bullAbove, bearBelow)
  Draws the chain into a table with 7 columns and `rows.size() + 2` rows (create it with `UIKit.newTable()`):
CE LTP and volume, strike, smoothed PCR, flow, PE volume and LTP for every strike, each with its change
against the previous bar, the traded CE / PE strikes and the center strike highlighted, and a footer with
the chain PCR, chain flow and the combined setup. Call it on the last bar.
  Namespace types: array<Row>
  Parameters:
    rows (array<Row>): (array<Row>) The chain's rows, lowest strike first.
    t (table): (table) The table.
    th (Theme type from jabez4jc/UIKit/1): (UIKit.Theme) Colours and text size.
    center (int): (series int) Center strike (highlighted).
    ceStrike (int): (series int) Strike of the call that would be traded (highlighted).
    peStrike (int): (series int) Strike of the put that would be traded (highlighted).
    cumPcr (float): (series float) Chain PCR from `summary()`.
    cumPcrPrev (float): (series float) Chain PCR on the previous bar.
    cumFlow (float): (series float) Chain flow from `summary()`.
    cumFlowPrev (float): (series float) Chain flow on the previous bar.
    bullAbove (float): (series float) Bullish above this PCR.
    bearBelow (float): (series float) Bearish below this PCR.
  Returns: (void)

Row
  One strike of the chain, returned by `row()`. `*Prev` fields hold the previous bar's values.
  Fields:
    strike (series int): Strike price.
    ceLtp (series float): Call last price.
    ceVol (series float): Call volume.
    peLtp (series float): Put last price.
    peVol (series float): Put volume.
    ceLtpPrev (series float): Previous call last price.
    ceVolPrev (series float): Previous call volume.
    peLtpPrev (series float): Previous put last price.
    peVolPrev (series float): Previous put volume.
    pcr (series float): SWMA-smoothed volume PCR (a missing PCR counts as 1).
    pcrPrev (series float): Previous smoothed PCR.
    flow (series float): Call + put flow score (`na` when both are `na`).

Thông báo miễn trừ trách nhiệm

Thông tin và các ấn phẩm này không nhằm mục đích, và không cấu thành, lời khuyên hoặc khuyến nghị về tài chính, đầu tư, giao dịch hay các loại khác do TradingView cung cấp hoặc xác nhận. Đọc thêm tại Điều khoản Sử dụng.