Files
randebu/src/frontend/src/lib/components/BotInfoPanel.svelte
shokollm f46cad4379 WIP: Multiple fixes and improvements
Backend:
- Fixed auth issue where get_optional_user wasn't properly extracting tokens
- Added user_id to conversational agent for proper auth context
- Fixed DCA buy logic to support multiple buys on dips
- Fixed sell logic to use amount_percent
- Added comprehensive backtest engine tests
- Fixed kline data validation for bad price data
- Fixed chained tool calls handling

Frontend:
- BotCard now links to chat page instead of bot page
- Chat page handles direct bot loading from dashboard
- Various UI improvements and fixes

Tests:
- Added test_agent.py with mock client tests
- Added test_backtest_engine.py with 7 comprehensive tests
2026-04-15 15:20:42 +00:00

435 lines
8.6 KiB
Svelte

<script lang="ts">
import type { Bot, Condition, Action, RiskManagement } from '$lib/api';
interface Props {
bot?: Bot | null;
onSelectBot?: () => void;
onBacktest?: () => void;
onSimulate?: () => void;
}
let { bot = null, onSelectBot, onBacktest, onSimulate }: Props = $props();
// Helper to get human-readable condition name
function getConditionLabel(condition: Condition): string {
const labels: Record<string, string> = {
'price_drop': 'Price Drop',
'price_rise': 'Price Rise',
'volume_spike': 'Volume Spike',
'price_level': 'Price Level'
};
return labels[condition.type] || condition.type;
}
// Format condition to readable string
function formatCondition(condition: Condition): string {
const parts: string[] = [getConditionLabel(condition)];
if (condition.token) {
parts.push(condition.token.toUpperCase());
}
if (condition.direction) {
parts.push(condition.direction);
}
if (condition.threshold) {
parts.push(`>${condition.threshold}%`);
}
if (condition.price) {
parts.push(`$${condition.price}`);
}
if (condition.timeframe) {
parts.push(`(${condition.timeframe})`);
}
return parts.join(' ');
}
// Format action to readable string
function formatAction(action: Action): string {
const parts: string[] = [];
if (action.type === 'buy') {
parts.push('Buy');
if (action.amount_percent) {
parts.push(`${action.amount_percent}%`);
}
if (action.token) {
parts.push(`of ${action.token.toUpperCase()}`);
}
} else if (action.type === 'sell') {
parts.push('Sell');
if (action.amount_percent) {
parts.push(`${action.amount_percent}%`);
}
} else {
parts.push('Hold');
}
return parts.join(' ');
}
let hasStrategy = $derived(
bot?.strategy_config &&
((bot.strategy_config.conditions && bot.strategy_config.conditions.length > 0) ||
(bot.strategy_config.actions && bot.strategy_config.actions.length > 0) ||
bot.strategy_config.risk_management)
);
</script>
<div class="bot-info-panel">
<h3>Bot Details</h3>
{#if !bot}
<div class="no-bot">
<p>No bot selected</p>
{#if onSelectBot}
<button class="btn btn-secondary" onclick={onSelectBot}>
Select Bot
</button>
{/if}
</div>
{:else}
<div class="bot-details">
<div class="bot-name">{bot.name}</div>
<div class="detail-row">
<span class="label">Status:</span>
<span class="value" class:active={bot.status === 'active'}>
{bot.status}
</span>
</div>
</div>
<div class="strategy-section">
<h4>Strategy</h4>
{#if hasStrategy}
{#if bot.strategy_config?.conditions && bot.strategy_config.conditions.length > 0}
<div class="strategy-group">
<div class="strategy-group-label">When:</div>
{#each bot.strategy_config.conditions as condition}
<div class="condition-item">
<span class="condition-icon">📊</span>
{formatCondition(condition)}
</div>
{/each}
</div>
{/if}
{#if bot.strategy_config?.actions && bot.strategy_config.actions.length > 0}
<div class="strategy-group">
<div class="strategy-group-label">Then:</div>
{#each bot.strategy_config.actions as action}
<div class="action-item">
<span class="action-icon">{action.type === 'buy' ? '🟢' : action.type === 'sell' ? '🔴' : '⏸️'}</span>
{formatAction(action)}
</div>
{/each}
</div>
{/if}
{#if bot.strategy_config?.risk_management}
<div class="strategy-group">
<div class="strategy-group-label">Risk:</div>
<div class="risk-item">
{#if bot.strategy_config.risk_management.stop_loss_percent}
<span class="risk-tag loss">Stop Loss: {bot.strategy_config.risk_management.stop_loss_percent}%</span>
{/if}
{#if bot.strategy_config.risk_management.take_profit_percent}
<span class="risk-tag profit">Take Profit: {bot.strategy_config.risk_management.take_profit_percent}%</span>
{/if}
</div>
</div>
{/if}
{:else}
<div class="no-strategy">
<div class="no-strategy-icon">⚙️</div>
<p>No strategy configured</p>
<span class="no-strategy-hint">Chat with the bot to set up your trading strategy</span>
</div>
{/if}
</div>
{#if onSelectBot}
<button class="btn btn-outline" onclick={onSelectBot}>
Change Bot
</button>
{/if}
<div class="action-buttons">
<h4>Tools</h4>
<div class="button-row">
<button
class="btn btn-tool btn-backtest"
onclick={onBacktest}
disabled={!bot || !hasStrategy}
>
📊 Backtest
</button>
<button
class="btn btn-tool btn-simulate"
onclick={onSimulate}
disabled={!bot || !hasStrategy}
>
🎮 Simulate
</button>
</div>
{#if !hasStrategy}
<p class="tool-hint">Configure a strategy first to use these tools</p>
{/if}
</div>
{/if}
</div>
<style>
.bot-info-panel {
padding: 1.25rem;
height: 100%;
overflow-y: auto;
}
h3 {
font-size: 0.85rem;
text-transform: uppercase;
letter-spacing: 0.5px;
color: #888;
margin: 0 0 1rem;
font-weight: 600;
}
h4 {
font-size: 0.8rem;
text-transform: uppercase;
letter-spacing: 0.5px;
color: #666;
margin: 1rem 0 0.75rem;
font-weight: 600;
}
.no-bot {
text-align: center;
padding: 1rem 0;
}
.no-bot p {
color: #666;
margin: 0 0 1rem;
}
.bot-details {
margin-bottom: 0.5rem;
}
.bot-name {
font-size: 1.25rem;
font-weight: 600;
color: #fff;
margin-bottom: 1rem;
}
.detail-row {
display: flex;
justify-content: space-between;
padding: 0.5rem 0;
border-bottom: 1px solid rgba(255, 255, 255, 0.05);
}
.label {
color: #888;
font-size: 0.9rem;
}
.value {
color: #ccc;
font-size: 0.9rem;
}
.value.active {
color: #3fb950;
}
.strategy-section {
background: rgba(255, 255, 255, 0.03);
border-radius: 8px;
padding: 1rem;
margin-top: 0.5rem;
}
.strategy-group {
margin-bottom: 0.75rem;
}
.strategy-group:last-child {
margin-bottom: 0;
}
.strategy-group-label {
font-size: 0.75rem;
color: #667eea;
font-weight: 600;
text-transform: uppercase;
margin-bottom: 0.25rem;
}
.condition-item,
.action-item {
display: flex;
align-items: center;
gap: 0.5rem;
padding: 0.35rem 0;
font-size: 0.85rem;
color: #ccc;
}
.condition-icon,
.action-icon {
font-size: 0.9rem;
}
.risk-item {
display: flex;
gap: 0.5rem;
flex-wrap: wrap;
}
.risk-tag {
font-size: 0.75rem;
padding: 0.25rem 0.5rem;
border-radius: 4px;
font-weight: 500;
}
.risk-tag.loss {
background: rgba(248, 81, 73, 0.2);
color: #f85149;
}
.risk-tag.profit {
background: rgba(63, 185, 80, 0.2);
color: #3fb950;
}
.no-strategy {
text-align: center;
padding: 1rem 0.5rem;
}
.no-strategy-icon {
font-size: 2rem;
margin-bottom: 0.5rem;
opacity: 0.5;
}
.no-strategy p {
color: #888;
margin: 0 0 0.25rem;
font-size: 0.9rem;
}
.no-strategy-hint {
font-size: 0.75rem;
color: #666;
}
.action-buttons {
margin-top: 1.5rem;
padding-top: 1rem;
border-top: 1px solid rgba(255, 255, 255, 0.1);
}
.action-buttons h4 {
font-size: 0.75rem;
text-transform: uppercase;
letter-spacing: 0.5px;
color: #666;
margin: 0 0 0.75rem;
font-weight: 600;
}
.button-row {
display: flex;
gap: 0.5rem;
}
.btn-tool {
flex: 1;
padding: 0.6rem 0.5rem;
border-radius: 6px;
font-size: 0.8rem;
font-weight: 500;
cursor: pointer;
transition: all 0.2s;
display: flex;
align-items: center;
justify-content: center;
gap: 0.35rem;
}
.btn-tool:disabled {
opacity: 0.4;
cursor: not-allowed;
}
.btn-backtest {
background: rgba(102, 126, 234, 0.15);
color: #667eea;
border: 1px solid rgba(102, 126, 234, 0.3);
}
.btn-backtest:hover:not(:disabled) {
background: rgba(102, 126, 234, 0.25);
}
.btn-simulate {
background: rgba(63, 185, 80, 0.15);
color: #3fb950;
border: 1px solid rgba(63, 185, 80, 0.3);
}
.btn-simulate:hover:not(:disabled) {
background: rgba(63, 185, 80, 0.25);
}
.tool-hint {
font-size: 0.7rem;
color: #555;
margin: 0.5rem 0 0;
text-align: center;
}
.btn {
width: 100%;
padding: 0.75rem 1rem;
border-radius: 8px;
font-size: 0.9rem;
font-weight: 500;
cursor: pointer;
transition: all 0.2s;
margin-top: 1rem;
}
.btn-secondary {
background: rgba(255, 255, 255, 0.1);
color: #fff;
border: 1px solid rgba(255, 255, 255, 0.2);
}
.btn-secondary:hover {
background: rgba(255, 255, 255, 0.15);
}
.btn-outline {
background: transparent;
color: #667eea;
border: 1px solid #667eea;
}
.btn-outline:hover {
background: rgba(102, 126, 234, 0.1);
}
</style>