'use client'; import { Text, View, XStack, YStack } from '@hanzo/gui'; import * as React from 'react'; // --------------------------------------------------------------------------- // Types // --------------------------------------------------------------------------- export interface StrategyLeg { action: 'buy' | 'sell'; type: 'call' | 'put'; strike: number; expiration: string; quantity: number; premium?: number; } export type StrategyType = | 'bull_call_spread' | 'bear_put_spread' | 'iron_condor' | 'butterfly' | 'straddle' | 'strangle' | 'collar' | 'custom'; export interface StrategySubmission { type: string; legs: StrategyLeg[]; netPremium: number; } export interface StrategyBuilderProps extends Omit, 'onSubmit'> { readonly underlying: string; readonly expirations: string[]; readonly strikes: number[]; readonly spotPrice: number; readonly onSubmit?: (strategy: StrategySubmission) => void; } // --------------------------------------------------------------------------- // Strategy presets // --------------------------------------------------------------------------- interface StrategyPreset { label: string; value: StrategyType; description: string; buildLegs: (strikes: number[], spotPrice: number, expiration: string) => StrategyLeg[]; } function findATM(strikes: number[], spot: number): number { if (strikes.length === 0) return spot; return strikes.reduce((prev, curr) => Math.abs(curr - spot) < Math.abs(prev - spot) ? curr : prev, ); } function findOTM(strikes: number[], spot: number, type: 'call' | 'put', offset: number): number { const sorted = [...strikes].sort((a, b) => a - b); const atm = findATM(strikes, spot); const atIdx = sorted.indexOf(atm); if (type === 'call') { return sorted[Math.min(atIdx + offset, sorted.length - 1)] ?? atm; } return sorted[Math.max(atIdx - offset, 0)] ?? atm; } const STRATEGY_PRESETS: StrategyPreset[] = [ { label: 'Bull Call Spread', value: 'bull_call_spread', description: 'Buy lower strike call, sell higher strike call', buildLegs: (strikes, spot, exp) => { const lower = findATM(strikes, spot); const upper = findOTM(strikes, spot, 'call', 2); return [ { action: 'buy', type: 'call', strike: lower, expiration: exp, quantity: 1 }, { action: 'sell', type: 'call', strike: upper, expiration: exp, quantity: 1 }, ]; }, }, { label: 'Bear Put Spread', value: 'bear_put_spread', description: 'Buy higher strike put, sell lower strike put', buildLegs: (strikes, spot, exp) => { const upper = findATM(strikes, spot); const lower = findOTM(strikes, spot, 'put', 2); return [ { action: 'buy', type: 'put', strike: upper, expiration: exp, quantity: 1 }, { action: 'sell', type: 'put', strike: lower, expiration: exp, quantity: 1 }, ]; }, }, { label: 'Iron Condor', value: 'iron_condor', description: 'Sell OTM put + call, buy further OTM put + call', buildLegs: (strikes, spot, exp) => { const sellPut = findOTM(strikes, spot, 'put', 1); const buyPut = findOTM(strikes, spot, 'put', 3); const sellCall = findOTM(strikes, spot, 'call', 1); const buyCall = findOTM(strikes, spot, 'call', 3); return [ { action: 'buy', type: 'put', strike: buyPut, expiration: exp, quantity: 1 }, { action: 'sell', type: 'put', strike: sellPut, expiration: exp, quantity: 1 }, { action: 'sell', type: 'call', strike: sellCall, expiration: exp, quantity: 1 }, { action: 'buy', type: 'call', strike: buyCall, expiration: exp, quantity: 1 }, ]; }, }, { label: 'Butterfly', value: 'butterfly', description: 'Buy 1 low, sell 2 middle, buy 1 high', buildLegs: (strikes, spot, exp) => { const mid = findATM(strikes, spot); const low = findOTM(strikes, spot, 'put', 2); const high = findOTM(strikes, spot, 'call', 2); return [ { action: 'buy', type: 'call', strike: low, expiration: exp, quantity: 1 }, { action: 'sell', type: 'call', strike: mid, expiration: exp, quantity: 2 }, { action: 'buy', type: 'call', strike: high, expiration: exp, quantity: 1 }, ]; }, }, { label: 'Straddle', value: 'straddle', description: 'Buy call + put at same strike', buildLegs: (strikes, spot, exp) => { const atm = findATM(strikes, spot); return [ { action: 'buy', type: 'call', strike: atm, expiration: exp, quantity: 1 }, { action: 'buy', type: 'put', strike: atm, expiration: exp, quantity: 1 }, ]; }, }, { label: 'Strangle', value: 'strangle', description: 'Buy OTM call + OTM put', buildLegs: (strikes, spot, exp) => { const otmCall = findOTM(strikes, spot, 'call', 2); const otmPut = findOTM(strikes, spot, 'put', 2); return [ { action: 'buy', type: 'put', strike: otmPut, expiration: exp, quantity: 1 }, { action: 'buy', type: 'call', strike: otmCall, expiration: exp, quantity: 1 }, ]; }, }, { label: 'Custom', value: 'custom', description: 'Build your own multi-leg strategy', buildLegs: (_strikes, spot, exp) => [ { action: 'buy', type: 'call', strike: spot, expiration: exp, quantity: 1 }, ], }, ]; // --------------------------------------------------------------------------- // Shared native-control shape — same tokens input.tsx's own control uses. // `` render no gui Text of their own, so every text-facing // property (colour, size, family) rides the `style` escape hatch here rather // than a typed prop: View's typed props are the same regardless of what tag // `render` points at, and none of them cover text — only Text has those. // --------------------------------------------------------------------------- const MONO = 'monospace'; const CONTROL_BOX = { backgroundColor: 'var(--color-input-bg)', borderWidth: 1, borderColor: 'var(--color-input-border)', outlineWidth: 0, focusStyle: { borderColor: 'var(--color-input-border-focus)' }, } as const; const CONTROL_INK = { color: 'var(--color-input-fg)' }; // --------------------------------------------------------------------------- // LegRow // --------------------------------------------------------------------------- interface LegRowProps { readonly leg: StrategyLeg; readonly index: number; readonly strikes: number[]; readonly expirations: string[]; readonly removable: boolean; readonly onChange: (index: number, leg: StrategyLeg) => void; readonly onRemove: (index: number) => void; } function LegRow({ leg, index, strikes, expirations, removable, onChange, onRemove }: LegRowProps) { const update = (patch: Partial) => { onChange(index, { ...leg, ...patch }); }; return ( { index + 1 } {/* Action */} } onClick={ () => update({ action: leg.action === 'buy' ? 'sell' : 'buy' }) } paddingHorizontal={ 8 } paddingVertical={ 2 } borderRadius={ 4 } borderWidth={ 0 } cursor="pointer" transition="quick" backgroundColor={ (leg.action === 'buy' ? 'color-mix(in srgb, var(--color-status-good) 20%, transparent)' : 'color-mix(in srgb, var(--color-status-bad) 20%, transparent)') as never } > { leg.action === 'buy' ? 'BUY' : 'SELL' } {/* Type — call vs put is a rank difference, not a hue difference. */} } onClick={ () => update({ type: leg.type === 'call' ? 'put' : 'call' }) } paddingHorizontal={ 8 } paddingVertical={ 2 } borderRadius={ 4 } borderWidth={ 0 } cursor="pointer" transition="quick" backgroundColor={ (leg.type === 'call' ? 'var(--secondary)' : 'var(--muted)') as never } > { leg.type === 'call' ? 'CALL' : 'PUT' } {/* Strike. `value`/`onChange` are native