Back to Hub
CREATED BY CHAINLINK LABS

Automated Portfolio Rebalancing

Automatically rebalance crypto portfolios by continuously monitoring allocation drift and executing portfolio adjustments when predefined thresholds are exceeded, while preserving the confidentiality of exchange API keys, LLM reasoning, portfolio allocation thresholds, and execution preferences.

Get the template
cre init --template=automated-portfolio-rebalancing-ts
View on GitHub

This standalone CRE project implements a confidential portfolio rebalancing workflow for crypto allocations.

Description

The workflow continuously tracks allocation drift and triggers rebalancing when policy thresholds are exceeded. It is designed to restore user-defined target weights while protecting sensitive operational inputs, including exchange credentials, model credentials, policy thresholds, and execution preferences inside confidential execution.

Target Customer

  • Professional retail traders
  • Developer shops
  • Founders building trading products

Structure

  • project.yaml: project-level target settings
  • secrets.yaml: secret ID mappings used by the workflow
  • mock-server.js: local deterministic API server
  • automated-portfolio-rebalancing-ts/: TypeScript workflow implementation
  • automated-portfolio-rebalancing-go/: Go workflow implementation

Private Inputs

The following inputs are handled as confidential:

  • Exchange API credentials used to read holdings, reserve data, and execution context, including stablecoin reserve depth and cash balance.
  • LLM reasoning API credentials.
  • Portfolio policy settings such as target allocation mix, minimum drift threshold, maximum trade size per execution, and required stablecoin reserve floor.
  • Execution preferences such as venue priority, slippage limits, and trade chunking/ordering behavior.

Workflow Notes

  1. Monitor portfolio state. The workflow gathers market prices, current asset weights, drift from target allocations, reserve health, and volatility signals.
  2. Enforce user-defined portfolio constraints. Confidential reasoning validates weight constraints, drift triggers, rebalance sizing limits, reserve protection requirements, and slippage controls.
  3. Build a rebalance action plan. The plan can include buying underweight assets, selling overweight assets, enforcing reserve floors, capping per-trade notionals, and optimizing execution through chunking and smart venue routing.
  4. Execute rebalance actions across venues. Depending on route selection, the workflow can execute both on-chain operations (such as swaps) and off-chain operations (such as centralized exchange API trades).

Note: Reasoning stages can be implemented with deterministic rule-based logic instead of an LLM when a fully rules-driven execution model is preferred.

Required Environment Variables

Copy .env.example to .env and provide values for:

  • CRE_ETH_PRIVATE_KEY (optional for local simulate)
  • MOCK_PORT
  • MOCK_EXCHANGE_API_KEY
  • MOCK_OPENAI_API_KEY
  • MOCK_REBALANCING_TARGET_ALLOCATION_BTC_PCT
  • MOCK_REBALANCING_TARGET_ALLOCATION_ETH_PCT
  • MOCK_REBALANCING_TARGET_ALLOCATION_USDC_PCT
  • MOCK_REBALANCING_DRIFT_THRESHOLD_PCT
  • MOCK_REBALANCING_MAX_TRADE_USD
  • MOCK_REBALANCING_RESERVE_FLOOR_USDC
  • MOCK_REBALANCING_MAX_SLIPPAGE_BPS
  • MOCK_REBALANCING_PREFERRED_VENUES
  • MOCK_REBALANCING_ORDER_SEQUENCE_PREFERENCE

The local mock server for this project only exposes routes under /rebalancing/*.

Quick navigation:

TypeScript Quick Start

  1. Install dependencies
bun install
  1. Create environment file
cp .env.example .env
  1. Start mock server
bun run mock:server
  1. In another terminal, run checks
bun run typecheck
bun run test
  1. Simulate workflow
cre workflow simulate ./automated-portfolio-rebalancing-ts --target=staging-settings

Go Quick Start

  1. Create environment file (at the shared project root)
cp ../.env.example ../.env
  1. Start the mock server (requires Node or Bun)
bun mock-server.js
  1. In another terminal, run checks
go vet ./...
go test ./...
  1. Simulate workflow
cd .. && cre workflow simulate ./automated-portfolio-rebalancing-go --target=staging-settings

Get the latest Chainlink content straight to your inbox.