'use client'; import { Text, View } from '@hanzo/gui'; import * as React from 'react'; // --------------------------------------------------------------------------- // Types // --------------------------------------------------------------------------- export interface OptionQuote { symbol: string; bid: number; ask: number; last: number; volume: number; openInterest: number; iv: number; delta: number; gamma: number; theta: number; vega: number; } export interface OptionContract { symbol: string; type: 'call' | 'put'; strike: number; bid: number; ask: number; } export interface Strike { strike: number; call: OptionQuote | null; put: OptionQuote | null; } // --------------------------------------------------------------------------- // Helpers // --------------------------------------------------------------------------- function fmt(n: number | undefined | null, decimals = 2): string { if (n === undefined || n === null) return '-'; return n.toFixed(decimals); } function fmtInt(n: number | undefined | null): string { if (n === undefined || n === null) return '-'; if (n >= 1_000_000) return `${(n / 1_000_000).toFixed(1)}M`; if (n >= 1_000) return `${(n / 1_000).toFixed(1)}K`; return n.toLocaleString(); } function fmtPct(n: number | undefined | null): string { if (n === undefined || n === null) return '-'; return `${(n * 100).toFixed(1)}%`; } // A cell's own text is centred/right/left via flex, not text-align: the host // is a (unstyled, so it keeps native table-cell layout) and the content // is a gui Text, which has no cascading effect from a parent's CSS text-align. const justify = (align: 'left' | 'right' | 'center'): 'flex-start' | 'flex-end' | 'center' => align === 'right' ? 'flex-end' : align === 'left' ? 'flex-start' : 'center'; // --------------------------------------------------------------------------- // QuoteCell — clickable cell in the option chain // --------------------------------------------------------------------------- interface QuoteCellProps { readonly value: string; readonly highlight?: boolean; readonly align?: 'left' | 'right'; readonly onClick?: () => void; readonly className?: string; } function QuoteCell({ value, highlight, align = 'right', onClick, className }: QuoteCellProps) { return ( } onClick={ onClick } paddingHorizontal={ 6 } paddingVertical={ 4 } cursor={ onClick ? 'pointer' : undefined } transition="quick" hoverStyle={ onClick ? ({ backgroundColor: 'var(--color-popover-item-hover)' } as never) : undefined } style={ { display: 'flex', alignItems: 'center', justifyContent: justify(align) } as never } className={ className } > { value } ); } // --------------------------------------------------------------------------- // HeadCell — in either header row // --------------------------------------------------------------------------- interface HeadCellProps { readonly children?: React.ReactNode; readonly colSpan?: number; readonly align?: 'left' | 'right' | 'center'; readonly size?: 'group' | 'column'; readonly color?: string; } function HeadCell({ children, colSpan, align = 'center', size = 'column', color }: HeadCellProps) { return ( : } paddingHorizontal={ size === 'group' ? 8 : 6 } paddingVertical={ size === 'group' ? 6 : 4 } style={ { display: 'flex', alignItems: 'center', justifyContent: justify(align) } as never } > { children != null && ( { children } ) } ); } // --------------------------------------------------------------------------- // Column headers // --------------------------------------------------------------------------- const CALL_HEADERS = ['Bid', 'Ask', 'Last', 'Vol', 'OI', 'IV', 'Delta'] as const; const PUT_HEADERS = ['Delta', 'IV', 'OI', 'Vol', 'Last', 'Ask', 'Bid'] as const; // --------------------------------------------------------------------------- // OptionChain // --------------------------------------------------------------------------- export interface OptionChainProps extends React.HTMLAttributes { readonly symbol: string; readonly expiration: string; readonly strikes: Strike[]; readonly spotPrice: number; readonly onSelectContract?: (contract: OptionContract) => void; } export const OptionChain = React.forwardRef( function OptionChain(props, ref) { const { symbol: _symbol, expiration: _expiration, strikes, spotPrice, onSelectContract, className, ...rest } = props; const handleClick = React.useCallback( (quote: OptionQuote | null, type: 'call' | 'put', strike: number) => { if (!quote || !onSelectContract) return; onSelectContract({ symbol: quote.symbol, type, strike, bid: quote.bid, ask: quote.ask, }); }, [onSelectContract], ); return ( } width="100%" style={ { borderCollapse: 'collapse' } as never }> }> } borderBottomWidth={ 1 } borderColor={ 'var(--color-border-divider)' as never }> {/* Call headers */} Calls {/* Strike header */} Strike {/* Put headers */} Puts } borderBottomWidth={ 1 } borderColor={ 'var(--color-popover-item-hover)' as never }> { CALL_HEADERS.map((h) => ( { h } )) } { PUT_HEADERS.map((h) => ( { h } )) } }> { strikes.map((row) => { const isATM = Math.abs(row.strike - spotPrice) <= (strikes.length > 1 ? Math.abs((strikes[1]?.strike ?? 0) - (strikes[0]?.strike ?? 0)) / 2 : 0.5); const callITM = row.strike < spotPrice; const putITM = row.strike > spotPrice; const c = row.call; const p = row.put; const rowBg = isATM ? 'var(--secondary)' : callITM ? 'color-mix(in srgb, var(--color-status-good) 12%, transparent)' : putITM ? 'color-mix(in srgb, var(--color-status-bad) 12%, transparent)' : 'transparent'; return ( } borderBottomWidth={ 1 } borderColor={ 'color-mix(in srgb, var(--color-border-divider) 50%, transparent)' as never } transition="quick" backgroundColor={ rowBg as never } hoverStyle={ isATM || callITM || putITM ? undefined : ({ backgroundColor: 'var(--color-popover-item-hover)' } as never) } > {/* Call side */} handleClick(c, 'call', row.strike) } /> handleClick(c, 'call', row.strike) } /> {/* Strike */} } paddingHorizontal={ 8 } paddingVertical={ 4 } style={ { display: 'flex', alignItems: 'center', justifyContent: 'center' } as never } > { fmt(row.strike) } {/* Put side (mirrored order) */} handleClick(p, 'put', row.strike) } /> handleClick(p, 'put', row.strike) } /> ); }) } ); }, );