(barService: BarService)
| 32 | } |
| 33 | |
| 34 | export function createAnalysisTools(barService: BarService) { |
| 35 | return { |
| 36 | calculateIndicator: tool({ |
| 37 | description: `Calculate technical indicators by ticker (vendor data, auto-selected — typically yfinance, whose end-of-day bars can lag a day or two). Quick path for "what's AAPL's RSI" when freshness doesn't matter. |
| 38 | |
| 39 | For anything you trade, or anything time-sensitive, prefer **calculateQuant** — it's barId-keyed, so you can target a broker's realtime K-lines (matching a held position's symbology) or mix sources in one expression. This tool (calculateIndicator) is the simpler vendor-default path: plain ticker + asset class, no barId. Note the syntax differs: HERE functions are UPPERCASE in a formula string (SMA(CLOSE('AAPL','1d'),50)); calculateQuant uses a lowercase pandas-style script (sma(s.close, 50)). |
| 40 | |
| 41 | Asset classes: "equity" for stocks, "crypto" for cryptocurrencies, "currency" for forex pairs, "commodity" for commodities (use canonical names: gold, crude_oil, copper, etc.). |
| 42 | |
| 43 | Data access (returns array — use [-1] for latest value): |
| 44 | CLOSE('AAPL', '1d'), HIGH, LOW, OPEN, VOLUME — args: symbol, interval (e.g. '1d', '1w', '1h'). |
| 45 | CLOSE('AAPL', '1d')[-1] → latest close price as a single number. |
| 46 | |
| 47 | Statistics (returns a single number — do NOT use [-1]): |
| 48 | SMA(data, period), EMA, STDEV, MAX, MIN, SUM, AVERAGE. |
| 49 | |
| 50 | Technical — trend / momentum (returns a single number or object — do NOT use [-1]): |
| 51 | RSI(data, 14) → number. BBANDS(data, 20, 2) → {upper, middle, lower}. |
| 52 | MACD(data, 12, 26, 9) → {macd, signal, histogram}. ATR(highs, lows, closes, 14) → number. |
| 53 | |
| 54 | Technical — volume / right-side confirmation (returns a single number): |
| 55 | RVOL(VOLUME(...), 20) → relative volume: latest bar vs its 20-bar average. >1 = heavier than usual; 2-3+ on a move = volume-confirmed. The right way to read volume — raw VOLUME is not comparable across tickers. |
| 56 | OBV(CLOSE(...), VOLUME(...)) → on-balance volume (accumulation/distribution). |
| 57 | MFI(HIGH(...), LOW(...), CLOSE(...), VOLUME(...), 14) → money-flow index, 0-100 (volume-weighted RSI). |
| 58 | VWAP(HIGH(...), LOW(...), CLOSE(...), VOLUME(...)) → volume-weighted average price; price above it = buyers in control. |
| 59 | |
| 60 | Arithmetic: +, -, *, / operators between numbers. E.g. CLOSE(...)[-1] - SMA(..., 50). |
| 61 | Note: arithmetic needs scalars on both sides, so reduce a series with [-1] or a function first — e.g. RVOL via formula is VOLUME(...)[-1] / SMA(VOLUME(...), 20), or just call RVOL(VOLUME(...), 20). |
| 62 | |
| 63 | Examples: |
| 64 | SMA(CLOSE('AAPL', '1d'), 50) → equity 50-day moving average |
| 65 | RSI(CLOSE('BTCUSD', '1d'), 14) → crypto RSI (single number, no [-1]) |
| 66 | RVOL(VOLUME('AAPL', '1d'), 20) → is AAPL trading on unusual volume today? |
| 67 | VWAP(HIGH('TSLA','1d'), LOW('TSLA','1d'), CLOSE('TSLA','1d'), VOLUME('TSLA','1d')) |
| 68 | CLOSE('gold', '1d')[-1] → latest gold price (canonical name) |
| 69 | |
| 70 | Returns { value, dataRange } where dataRange shows the actual date span of the data used. |
| 71 | Use marketSearchForResearch to find the correct symbol first.`, |
| 72 | inputSchema: z.object({ |
| 73 | asset: z.enum(['equity', 'crypto', 'currency', 'commodity']).describe('Asset class'), |
| 74 | formula: z.string().describe("Formula expression, e.g. SMA(CLOSE('AAPL', '1d'), 50)"), |
| 75 | precision: z.number().int().min(0).max(10).optional().describe('Decimal places (default: 4)'), |
| 76 | }).meta({ examples: [{ asset: 'equity', formula: "SMA(CLOSE('AAPL', '1d'), 50)" }] }), |
| 77 | execute: async ({ asset, formula, precision }) => { |
| 78 | const context = buildContext(asset, barService) |
| 79 | const calculator = new IndicatorCalculator(context) |
| 80 | return await calculator.calculate(formula, precision) |
| 81 | }, |
| 82 | }), |
| 83 | } |
| 84 | } |
no test coverage detected