# Strategy Marketplace — Admin Publishing Feature

> Added: 2026-02-20

## Overview

The Strategy Marketplace allows administrators to convert any live trading strategy profile (Strategy Cluster) into a reusable template that all platform users can adopt.

---

## Database Schema

**Table**: `public.strategy_presets`  
**Migration**: `schema/016_add_strategy_marketplace.sql`

| Column | Type | Description |
|:---|:---|:---|
| `id` | `TEXT` (PK) | Format: `template-{profile_id}-{timestamp}` |
| `name` | `TEXT` | Display name of the strategy |
| `description` | `TEXT` | Auto-generated summary including rule count |
| `risk_style_id` | `TEXT` | `safe`, `balanced`, or `aggressive` |
| `recommended_assets` | `TEXT[]` | Array of symbols (e.g. `['BTC/USDT', 'ETH/USDT']`) |
| `typical_trades_per_day` | `TEXT` | e.g. `'3-5'`, `'8-12'` |
| `performance_tag` | `TEXT` | e.g. `'Institutional Template'` |
| `is_popular` | `BOOLEAN` | Featured/highlighted in marketplace |
| `created_at` | `TIMESTAMPTZ` | Auto-set on insert |
| `created_by` | `UUID` | Admin user ID (FK → `auth.users`) |
| `original_profile_id` | `UUID` | Source profile (FK → `trade_profiles`) |
| `strategy_config` | `JSONB` | Full strategy rule and risk config snapshot |
| `role_required` | `TEXT` | `'free'`, `'pro'`, or `'elite'` |

### Row Level Security

- **Read**: All authenticated users can read all presets (`FOR SELECT USING (true)`).
- **Write**: Only admins can insert/update/delete (`role = 'admin'` check against `public.users`).

---

## Admin Publishing Flow

### Trigger
Admin clicks the **↗ (Publish)** icon on any strategy card in the **Strategy Clusters** tab.

### Auto-Detection Logic
The `handlePublish` function in `TradeProfileManager.tsx` derives the `risk_style_id` from `minRulePassRatio`:

| `minRulePassRatio` | `risk_style_id` |
|:---|:---|
| < 0.9 | `aggressive` |
| ≥ 0.9 and < 1.0 | `balanced` |
| ≥ 1.0 | `safe` |

### Payload Construction
```json
{
  "id": "template-{profile.id}-{timestamp}",
  "name": "{profile.name}",
  "description": "Admin-published strategy based on {name}. Features N optimized rules.",
  "risk_style_id": "balanced",
  "recommended_assets": ["BTC/USDT", "ETH/USDT"],
  "typical_trades_per_day": "3-5",
  "performance_tag": "Institutional Template",
  "is_popular": true,
  "created_by": "{admin_user_id}",
  "original_profile_id": "{profile.id}",
  "strategy_config": { ... full config ... }
}
```

---

## Frontend Integration

### Marketplace Display (`PresetMarketplace.tsx`)
- On mount, fetches all rows from `strategy_presets` ordered by `created_at DESC`.
- Maps `snake_case` DB columns to `camelCase` frontend `StrategyPreset` interface.
- Merges with hardcoded system presets from `PresetRegistry.ts`.
- Total count displayed in header reflects both system + admin-published templates.

### Adoption Flow
- User clicks **"USE THIS STRATEGY"** on any card.
- The preset's `riskStyleId` and `recommendedAssets` seed the `StrategyWizard`.
- User customizes capital/assets and deploys as their own profile.

---

## Files Modified/Created

| File | Change |
|:---|:---|
| `schema/016_add_strategy_marketplace.sql` | New migration — creates `strategy_presets` table |
| `src/lib/const.ts` | Added `tableNameMarketplace = 'strategy_presets'` |
| `src/components/TradeProfileManager.tsx` | Added `handlePublish` and Publish button for admins |
| `src/components/PresetMarketplace.tsx` | Added Supabase fetch + mapping for dynamic templates |
| `src/tabs/MarketplaceTab.tsx` | Added AI Setups + Top Volatile contextual panels |
