# Architecture: Deterministic Workflows for Crypto Futures Trading

> Deterministic workflows chain exact calculation primitives into trade decisions. No AI guesswork: same inputs always return the same verdict. Explore the workflow taxonomy and how each one works.

Source: https://docs.tradingcalc.io/architecture

A deterministic workflow answers a specific trader question by orchestrating several calculation primitives and applying a classification layer on top. The answer is always the same for the same inputs: no model variation, no hallucination, no rounding drift.

## Two different tools

Calculators are primitives: they compute one number. Workflows are decision tools: they chain primitives and tell you what to do with the result.

- **Calculator:** Returns a number. You interpret it. "Liquidation price: $76,884." What does that mean for this setup? Up to you.
- **Workflow:** Returns a verdict + explanation. "WARN: liquidation is 9.5% away, closer than your stop. Consider reducing leverage." The decision layer is built in.

## Three layers of abstraction

TradingCalc organises its tools into three levels. Each level builds on the one below.

- **Primitive** (Single formula, one output): PnL, Liquidation price, Breakeven, Position size, Funding cost
- **Analyzer** (Primitive with safety classification): Max safe leverage, Scenario PnL table, Compound funding decay
- **Workflow** (Orchestrates multiple primitives → verdict): Pre-trade check, Risk/reward, Portfolio risk, Carry trade, DCA entry, Average down, Scale-out, Funding breakeven

## How a workflow is built

Each workflow is a pure TypeScript function in `src/lib/workflows/` that calls one or more calculator modules, aggregates the outputs, and applies a classification rule. The UI layer renders the verdict; it never touches the math.

```ts
export function compute(input: PreTradeInput): PreTradeOutput {
  const size    = position_size(input);      // Primitive 1
  const liq     = liquidation_price(input);  // Primitive 2
  const funding = funding_cost(input);       // Primitive 3
  const be      = breakeven_price(input);    // Primitive 4

  const verdict = classify(liq, size, funding);  // Decision layer
  return { size, liq, funding, be, verdict, verdict_summary: explain(verdict, input) };
}
// Same inputs → same verdict, every time
```

Pipeline: position_size, liquidation_price, funding_cost, breakeven_price, verdict.

## All 8 workflows

### Should I open this trade?

[Pre-trade check](https://tradingcalc.io/pre-trade-check). Runs four primitives in one pass. Surfaces the liquidation distance, carry cost, and required margin before you commit capital.

Primitives: Position size, Liquidation price, Funding cost, Breakeven.

Verdict: CLEAR / WARN / HIGH RISK / ERROR

### Is this trade worth taking?

[Risk/reward](https://tradingcalc.io/risk-reward). Computes expected reward vs defined risk. Verdict is based on the R:R ratio and a configurable minimum threshold.

Primitives: PnL at target, PnL at stop, R:R ratio.

Verdict: Take it / Worth the risk / Think twice / Skip this one

### How exposed am I across all my open positions?

[Portfolio risk](https://tradingcalc.io/portfolio-risk). Aggregates P&L, margin usage, and liquidation distance across every open position. Linear positions sum in USD; inverse positions group by settlement coin.

Primitives: Liquidation price, PnL.

Verdict: Healthy / Watch / Reduce / Critical

### Is this funding rate worth capturing?

[Carry trade](https://tradingcalc.io/carry-trade). Sizes the delta-neutral hedge and computes the net funding yield after hedging costs. Shows if the carry is actually profitable.

Primitives: Funding cost, Hedge ratio, Net yield.

Verdict: Positive carry / Negative carry / Break-even point

### How should I scale into this position?

[DCA entry](https://tradingcalc.io/dca-entry). Distributes position size across price levels. Shows the blended average entry and checks liquidation safety at full deployment.

Primitives: Average entry, Position size per level, Liquidation at full size.

Verdict: Entry plan with per-level sizing and blended average

### Should I average down?

[Average down](https://tradingcalc.io/average-down). Compares adding to a losing position against buying the same total size fresh at the current price. Shows the new average entry, liquidation, breakeven, and total risk at your stop, before and after.

Primitives: Average entry, Liquidation price, Breakeven, PnL.

Verdict: Extra risk versus a fresh entry at today's price, and whether the new liquidation lands before your stop

### How should I take profit in stages?

[Scale-out](https://tradingcalc.io/scale-out). Plans partial exits at multiple targets. Shows the PnL at each level, the reduced risk after each close, and the running breakeven.

Primitives: PnL per exit level, Remaining size, Breakeven after partial close.

Verdict: Exit plan with per-level PnL and remaining risk

### How long before funding erases my profit?

[Funding breakeven](https://tradingcalc.io/funding-breakeven). Given an unrealized profit and a funding rate, computes how long you can hold before carry cost neutralises the gain.

Primitives: Funding cost accumulation, PnL at target, Hold time to breakeven.

Verdict: Hours / days until carry cost equals unrealized PnL

## Determinism matters specifically for workflows

A probabilistic workflow (e.g. an LLM asked "should I open this trade?") will give a different answer each time, even for identical inputs. For a one-off analysis that may be fine. For a risk monitor that fires alerts, a backtester replaying 10,000 trades, or an AI agent making sequential decisions, it is not.

Deterministic workflows are also composable: the output of "Is this trade worth taking?" can feed directly into "How should I size the position?", and the combined result is still auditable end-to-end.

## Use workflows via API

Every workflow is callable through the MCP JSON-RPC endpoint. The same function that powers the UI is the one your agent or bot calls.

```bash
curl -X POST https://tradingcalc.io/api/mcp \
  -H "Content-Type: application/json" \
  -d '{
    "jsonrpc": "2.0", "id": 1, "method": "tools/call",
    "params": {
      "name": "workflow.run_pre_trade_check",
      "arguments": {
        "side": "long", "entry_price": 85000, "leverage": 10,
        "account_balance": 10000, "risk_pct": 1, "stop_loss": 83000,
        "funding_rate": 0.0001, "hold_hours": 24
      }
    }
  }'

// result.content[0].text
{ "safety": "ok", "recommended_size": 0.059, "breakeven_price": 85085.00, ... }
```

## Start with a workflow

Open the pre-trade check, enter your setup, and get a verdict in under 10 seconds. No account needed.

- [Try the pre-trade check](https://tradingcalc.io/pre-trade-check)
- [All workflows](https://tradingcalc.io/workflows)
- [API access](https://docs.tradingcalc.io/api)
