← All packages

@lacspace/rules

v1.2.0StockKit0 deps

Trading rules as data: a small, safe JSON rule language for "buy when …, sell when …" (price, SMA/EMA, RSI, MACD, ATR, Bollinger, Supertrend direction, N-candle high/low, average volume, % change; above / below / crosses above / crosses below; all/any groups; ATR stop and target), evaluated per candle on OHLCV arrays. Includes a validator that coerces AI-written JSON (no code execution), plain-English descriptions, a next-open backtester (stop-first, costs both sides) and a last-candle screener with sector/price/turnover filters and ranking — all on the same engine. Zero-dependency.

npm i @lacspace/rules

Usage

rules.ts
import { cleanRules, evaluate, backtestRules, screen, describeRules } from "@lacspace/rules";

const rules = cleanRules({
  name: "EMA cross with RSI filter",
  entry: { mode: "all", conds: [
    { a: { k: "ema", n: 20 }, op: "crossAbove", b: { k: "ema", n: 50 } },
    { a: { k: "rsi", n: 14 }, op: ">", b: { k: "num", v: 50 } },
  ] },
  exit: { mode: "any", conds: [{ a: { k: "ema", n: 20 }, op: "crossBelow", b: { k: "ema", n: 50 } }] },
  stopAtr: 2,
}); // → Rules | null (null when there's no valid entry condition)

describeRules(rules!); // "Buy when EMA 20 crosses above EMA 50 and RSI 14 is above 50; sell when …; stop 2 × ATR below entry."
evaluate(rules!, bars); // { entry: boolean[], exit: boolean[], atr, need }, read on each candle's close

Exports 19

OPERAND_KINDSOPSSTRATEGY_TEMPLATESbacktestRulescleanRulescondToTextdefaultOperanddescribedescribeConddescribeOperanddescribeRulesevaluateoperandSeriesoperandToTextparseRulesrulesStrategyrulesToTextrunBacktestscreen

Keywords

trading-rulesstrategybacktestbacktestingscreenerstock-screener

More in StockKit

@lacspace/marketwrap

Deterministic, no-AI stock-market wrap writer in English and Nepali (Devanagari). From the index close/change, breadth, turnover, sector moves and top gainers/losers it writes a factual daily wrap such as "NEPSE fell 11.90 points (−0.45%) to 2,587.25. Decliners led 238 to 95. Turnover Rs 4.29 arba." / "नेप्से ११.९० अंक (−०.४५%) घटेर २,५८७.२५ मा बन्द भयो।", with seeded phrasing variants so it doesn't read the same every day, South-Asian number formatting (lakh/crore/arba), a headline, and the computed facts. Every number comes from your input; no advice language. Zero-dependency.

@lacspace/patterns

Rule-based chart-pattern detection on OHLCV bars: ATR-sized swings (zigzag), harmonic XABCD patterns (Gartley, Bat, Butterfly, Crab, Cypher, AB=CD) with potential-reversal zones, an Elliott impulse count that obeys the three hard rules, classic chart patterns (double tops/bottoms, head & shoulders, triangles, wedges, channels, ranges) with necklines and measured moves, per-session TPO market profiles and volume profiles, and Kagi / Point & Figure transforms. Drawing-ready paths and zones plus plain data. Descriptive geometry, never a forecast. Zero-dependency.

@lacspace/portfolio

Portfolio analytics from holdings {symbol, qty, wacc} and daily closes: unrealised P/L vs WACC per holding and in total, market-value weights, HHI concentration (0–10,000) and effective number of holdings, sector weights, annualised volatility (daily log returns × √periods, default 240 for NEPSE), beta and correlation against an index on shared dates, max drawdown with peak/trough dates, Sharpe and Sortino with a risk-free rate. Duplicate lots merged, missing closes forward-filled. Deterministic, zero-dependency.

@lacspace/indicators

30+ streaming technical indicators (RSI, MACD, EMA, Bollinger, ATR, Supertrend, ADX, VWAP, Ichimoku, Parabolic SAR, StochRSI, CCI, MFI, Keltner, Donchian, OBV, CMF and more) with O(1) incremental next() updates for live price feeds, plus batch helpers. Zero-dependency.

@lacspace/market

Stock-market money math — P&L, returns, CAGR, XIRR, tick/lot rounding, circuit limits, position sizing and an Indian brokerage & charges calculator (STT, GST, SEBI, stamp), plus OHLCV candle resampling & gap detection, VWAP, corporate-action adjusted-close and beta/log-returns. Zero-dependency.

@lacspace/market-clock

Holiday-aware, timezone-correct trading clock — is NEPSE/NSE/BSE/NYSE/LSE open now, current session segment, next open/close, half-days, next N sessions. Built-in exchange presets + bring your own spec. Zero-dependency.