This document covers the position health subsystem: on-chain position discovery, collateral ratio assessment, trend alignment, and the decision loop that ties them together.
The subsystem has three layers:
- Discovery (
modules/position_discovery.ts) — scans an account's on-chain call orders and normalizes them into position objects. - Health assessment (
modules/position_health.ts) — classifies each position into a collateral ratio zone, checks trend alignment, and recommends actions. - Decision loop (
modules/decision_loop.ts) — orchestrates discovery, trend analysis, and health assessment into a single evaluation cycle.
The subsystem evaluates and recommends. It does not execute trades.
discoverPositions(accountName) scans the account's call orders on-chain and returns normalized position objects. It does not depend on PositionManager state files — it sees what actually exists on-chain.
Each discovered position includes:
- call order ID, borrower, market pair
- debt amount and collateral amount (float-converted)
- BTS-per-MPA derived from the settlement feed
- computed collateral ratio
- feed publication time
discoverPositionsSummary(accountName) returns a compact summary with rounded values for quick inspection.
The health assessor classifies collateral ratios into five zones based on HONEST asset MCR (1.4):
| Zone | CR range | Status | Meaning |
|---|---|---|---|
red_low |
below 1.7 | not_acceptable |
Near liquidation, immediate action needed |
orange_low |
1.7 – 2.0 | temporary |
Uncomfortable but survivable, act soon |
green |
2.0 – 2.5 | safe |
Healthy operating range |
orange_high |
2.5 – 3.0 | excess_collateral |
Capital sitting idle, consider deploying |
red_high |
above 3.0 | over_collateralized |
Significant capital inefficiency |
When a position needs adjustment, the assessor recommends actions in two layers:
- Layer 1 (primary): adjust debt first — reduce debt when CR is low, increase debt when CR is high.
- Layer 2 (fallback): adjust collateral if debt changes are insufficient — add collateral when CR is low, withdraw collateral when CR is high.
This mirrors the actual strategy levers: debt adjustments are the primary tool, collateral adjustments are the backup.
Action priorities:
| Zone | Priority | Layer 1 | Layer 2 |
|---|---|---|---|
red_low |
immediate |
reduce_debt |
add_collateral |
orange_low |
soon |
reduce_debt |
add_collateral |
green |
— | no action | no action |
orange_high |
soon |
increase_debt |
withdraw_collateral |
red_high |
immediate |
increase_debt |
withdraw_collateral |
When a trend signal is available, the assessor checks whether the position direction is aligned with the trend:
- A short position aligned with a
DOWNtrend isaligned. - A short position opposed to an
UPtrend with confidence >= 50% triggers areview_directionaction. - Neutral trends produce no directional action.
CR math is handled by the shared planner at modules/cr_planner.ts (root level, not claw), which provides debtDeltaForTargetCr() and collateralDeltaForTargetCr(). Claw's position health module consumes these through the planner interface. Any executed CR adjustment should be followed by a grid rebuild, because the available-funds baseline is no longer valid after the debt or collateral leg changes.
The decision loop in modules/decision_loop.ts combines all signals into a unified assessment:
- CR adjustment intent (debt first, collateral second)
- grid price offset percentage derived from trend direction and confidence
- weight distribution derived from trend analysis
- price range ratio recommendation based on historical price action
- a concrete
botPatchobject ready to apply to the bot config - the CR plan should be treated as structural, not tactical, so execution code must rebuild the grid after it applies
evaluate(accountName, options) runs one full cycle:
- Discover all on-chain positions for the account.
- Fetch trend input per market (feed price + market price).
- Update a per-market trend analyzer (KAMA-based, state persists across calls).
- Assess each position's health with the trend signal.
- Sort assessments by action priority (immediate first).
- Return assessments with a summary of zone distribution and action counts.
The summary includes:
- zone counts (
red_low,orange_low,green,orange_high,red_high) - immediate and soon action counts
allGreenflag for quick health checks
modules/position_manager_watch.ts is a PM2-compatible watcher process that keeps PositionManager state synchronized and reacts to fills. It does not invoke decision_loop.evaluate() itself.
npm run service:position-watch -- --account your-accountmodules/feed_price_source.ts— fetches on-chain settlement feed prices and order book mid-prices for trend input.modules/kibana_price_source.ts— alternative price source using Kibana for historical candle data (order book fills and LP pool swaps).modules/position_manager.ts— persistent short-position tracking (create, update, close, export). Separate from on-chain discovery.
The executable behavior lives in the modules. This document should be kept aligned with:
../cr_planner.ts(root level)modules/position_health.tsmodules/position_discovery.tsmodules/decision_loop.tsmodules/feed_price_source.tsmodules/position_manager_watch.tsclaw/tests/test_position_health.ts