Compare commits

...

29 Commits

Author SHA1 Message Date
32cd7184ea Merge pull request 'feat: implement conversational AI agent with tool-calling' (#52) from feat/conversational-agent-with-tools into main 2026-04-10 09:39:45 +02:00
shokollm
765e390b9b feat: implement conversational AI agent with tool-calling
Prototype implementation that allows:
1. Normal conversation with the AI
2. Tool-calling to update trading strategies

Created new ConversationalAgent that uses CrewAI with tools:
- get_current_strategy: Check current bot strategy
- update_trading_strategy: Update bot's trading configuration

The agent can now respond to questions like 'What is this?' without
forcing JSON output, and can update strategies when user provides
specific parameters.

Refs #51
2026-04-10 05:00:22 +00:00
21ce282cae Merge pull request 'fix: add fallback UUID generator for crypto.randomUUID compatibility' (#50) from fix/crypto-randomuuid-fallback into main 2026-04-10 06:26:49 +02:00
shokollm
4fa9b0456a fix: add fallback UUID generator for crypto.randomUUID compatibility
crypto.randomUUID() is not available in all environments (e.g., older browsers,
non-secure contexts). Added a fallback UUID v4 implementation.
2026-04-10 04:19:45 +00:00
af9900d0ba Merge pull request 'fix: add timeout for chat requests and improve error handling' (#49) from fix/chat-timeout-handling into main 2026-04-10 06:15:45 +02:00
shokollm
b3ab004447 fix: add timeout for chat requests and improve error handling
Changes:
1. Add 30-second timeout for chat API requests using AbortController
2. User's message now shows immediately before API response (already done in previous PR)
3. Differentiate between timeout errors and other errors in error messages
4. API client now accepts optional signal parameter for abort support
2026-04-10 04:09:30 +00:00
d394bc0857 Merge pull request 'fix: display user messages in chat interface' (#48) from fix/display-user-messages into main 2026-04-10 06:04:23 +02:00
shokollm
dfa806ab53 fix: add user's message to frontend chat store when sending
Previously, only the assistant's response was added to the frontend store.
Now both user and assistant messages are stored, so the conversation
displays correctly in the chat interface.
2026-04-10 04:00:51 +00:00
3493775b7f Merge pull request 'fix: update MiniMaxConnector default model to MiniMax-M2.7' (#47) from fix/minimax-connector-model into main 2026-04-10 06:00:36 +02:00
shokollm
82645dfb3b fix: update MiniMaxConnector default model to MiniMax-M2.7 2026-04-10 03:53:26 +00:00
c17fa243a1 Merge pull request 'fix: use MiniMax text/chatcompletion_v2 endpoint' (#46) from fix/minimax-endpoint-v2 into main 2026-04-10 05:44:25 +02:00
shokollm
a55ed9cc04 fix: use MiniMax text/chatcompletion_v2 endpoint instead of chat/completions
The /v1/chat/completions endpoint returns 529 (overloaded) while
/v1/text/chatcompletion_v2 works reliably.
2026-04-10 03:42:20 +00:00
d1408b74b4 Merge pull request 'fix: properly configure MiniMax API endpoint and CrewAI LLM' (#45) from fix/minimax-api-endpoint-v2 into main 2026-04-10 05:31:13 +02:00
shokollm
4197475eed fix: properly configure CrewAI LLM with MiniMax api_base
- Use CrewAI's LLM class directly with api_base parameter instead of custom subclass
- Remove broken MiniMaxLLM inheritance from LLM
- Update agent creation to use LLM(model, api_key, api_base) pattern

The issue was that inheriting from CrewAI's LLM class caused the api_base
to be set to None. Now we use CrewAI's LLM directly with the correct parameters.

Fixes #43
2026-04-10 03:19:51 +00:00
87bac8894a Merge pull request 'fix: update MiniMax API endpoint to api.minimax.io' (#44) from fix/minimax-api-endpoint into main 2026-04-10 05:10:17 +02:00
shokollm
bef4479675 fix: update MiniMax API endpoint and default model
Changes:
1. Updated API endpoint from api.minimax.chat to api.minimax.io
2. Changed default model from MiniMax-Text-01 to MiniMax-M2.7
   (MiniMax-Text-01 is not available for all API key plans)
3. Updated .env.example with correct default model

MiniMax API docs: https://platform.minimax.io/docs/api-reference/text-anthropic-api

Fixes #43
2026-04-10 03:07:02 +00:00
75970c57e3 Merge pull request 'feat: return access token on user registration' (#42) from feat/41-return-token-on-register into main 2026-04-10 03:31:15 +02:00
shokollm
f23044465a feat: return access token on user registration
After successful registration, the backend now returns an access token
(along with token_type) so the frontend can:
- Store the token in localStorage
- Fetch the user profile
- Redirect to dashboard

Fixes #41
2026-04-10 01:28:01 +00:00
a6e4d28aa7 Merge pull request 'fix: add bcrypt version constraint for passlib compatibility' (#40) from fix/bcrypt-compatibility into main 2026-04-10 02:55:36 +02:00
shokollm
8693946cb8 fix: add bcrypt version constraint for passlib compatibility
bcrypt 5.0.0 is incompatible with passlib 1.7.x - passlib tries to
access bcrypt.__about__.__version__ which was removed in bcrypt 5.x.

Constrain bcrypt to >=4.0,<5.0 to maintain compatibility.
2026-04-10 00:55:18 +00:00
a2f549c056 Merge pull request 'fix: correct import paths in ai_agent module' (#39) from fix/ai-agent-imports into main 2026-04-09 17:32:21 +02:00
shokollm
ad6e57655d fix: correct import paths in ai_agent module
- Fix relative import path in crew.py (from ..core to ...core)
- Update __init__.py exports to match actual class names
- Remove incorrect CrewAgent and LLMConnector exports
2026-04-09 15:27:09 +00:00
ac5e9d8b81 Merge pull request 'fix: add error logging to simulate engine to prevent silent failures' (#38) from fix/issue-30 into main 2026-04-09 12:19:36 +02:00
shokollm
81f3342365 fix: add error logging to simulate engine to prevent silent failures
Errors during price fetching are now logged and stored in an errors list,
allowing users to see error count/warnings in simulation results.

Acceptance Criteria:
- [x] Errors are logged (not silently swallowed)
- [x] User can see error count/warnings in simulation results
- [x] Simulation completes even if some price fetches fail (graceful degradation)
2026-04-09 10:16:22 +00:00
6adad0701d Merge pull request 'fix: consolidate AveCloudClient to single implementation' (#37) from fix/issue-29 into main 2026-04-09 12:11:59 +02:00
shokollm
405b35c3ba fix: consolidate AveCloudClient to single implementation in services/ave/client.py 2026-04-09 10:06:16 +00:00
dd25d38e7e Merge pull request 'feat: implement stop-loss and take-profit risk management' (#36) from fix/issue-28 into main 2026-04-09 11:39:50 +02:00
shokollm
da8327c0e0 feat: implement stop-loss and take-profit in backtest and simulate engines 2026-04-09 09:14:08 +00:00
8d33ea9a44 Merge pull request 'fix: flatten strategy config schema (backtesting broken)' (#35) from fix/issue-25 into main 2026-04-09 09:32:49 +02:00
15 changed files with 455 additions and 167 deletions

View File

@@ -32,7 +32,7 @@ MINIMAX_API_KEY=your-minimax-api-key
# MiniMax model to use # MiniMax model to use
# Common options: MiniMax-Text-01, MiniMax-M2.1 # Common options: MiniMax-Text-01, MiniMax-M2.1
MINIMAX_MODEL=MiniMax-Text-01 MINIMAX_MODEL=MiniMax-M2.7
# ============================================================================= # =============================================================================
# AVE CLOUD API # AVE CLOUD API

View File

@@ -58,7 +58,7 @@ def get_current_user(
@router.post( @router.post(
"/register", response_model=UserResponse, status_code=status.HTTP_201_CREATED "/register", response_model=Token, status_code=status.HTTP_201_CREATED
) )
def register(user: UserCreate, db: Session = Depends(get_db)): def register(user: UserCreate, db: Session = Depends(get_db)):
existing_user = db.query(User).filter(User.email == user.email).first() existing_user = db.query(User).filter(User.email == user.email).first()
@@ -75,7 +75,10 @@ def register(user: UserCreate, db: Session = Depends(get_db)):
db.add(db_user) db.add(db_user)
db.commit() db.commit()
db.refresh(db_user) db.refresh(db_user)
return db_user
# Generate and return access token so frontend can proceed immediately
access_token = create_access_token(data={"sub": db_user.id})
return Token(access_token=access_token, token_type="bearer")
@router.post("/login", response_model=Token) @router.post("/login", response_model=Token)

View File

@@ -16,6 +16,7 @@ from ..db.schemas import (
) )
from ..db.models import Bot, BotConversation, User from ..db.models import Bot, BotConversation, User
from ..services.ai_agent.crew import get_trading_crew from ..services.ai_agent.crew import get_trading_crew
from ..services.ai_agent.conversational import get_conversational_agent
router = APIRouter() router = APIRouter()
MAX_BOTS_PER_USER = 3 MAX_BOTS_PER_USER = 3
@@ -183,69 +184,45 @@ def chat(
.order_by(BotConversation.created_at) .order_by(BotConversation.created_at)
.all() .all()
) )
history_for_crew = [ history_for_agent = [
{"role": conv.role, "content": conv.content} {"role": conv.role, "content": conv.content}
for conv in conversation_history[-10:] for conv in conversation_history[-10:]
] ]
user_message = request.message user_message = request.message
if request.strategy_config:
crew = get_trading_crew()
result = crew.chat(user_message, history_for_crew)
assistant_content = result.get("response", "I couldn't process your request.") # Use ConversationalAgent for natural chat with tool-calling
if result.get("success") and result.get("strategy_config"): agent = get_conversational_agent(bot_id=bot_id)
bot.strategy_config = result["strategy_config"] result = agent.chat(user_message, history_for_agent)
db.commit()
db_conversation = BotConversation( assistant_content = result.get("response", "I couldn't process your request.")
bot_id=bot_id,
role="user",
content=user_message,
)
db.add(db_conversation)
db_assistant = BotConversation( # Save conversation
bot_id=bot_id, db_conversation = BotConversation(
role="assistant", bot_id=bot_id,
content=assistant_content, role="user",
) content=user_message,
db.add(db_assistant) )
db.commit() db.add(db_conversation)
db.refresh(db_assistant)
return BotChatResponse( db_assistant = BotConversation(
response=assistant_content, bot_id=bot_id,
strategy_config=result.get("strategy_config"), role="assistant",
success=result.get("success", False), content=assistant_content,
) )
else: db.add(db_assistant)
crew = get_trading_crew() db.commit()
result = crew.chat(user_message, history_for_crew) db.refresh(db_assistant)
assistant_content = result.get("response", "I couldn't process your request.") # If strategy was updated via tool, refresh bot data
if result.get("strategy_updated"):
db.refresh(bot)
db_conversation = BotConversation( return BotChatResponse(
bot_id=bot_id, response=assistant_content,
role="user", strategy_config=bot.strategy_config if result.get("strategy_updated") else None,
content=user_message, success=result.get("success", False),
) )
db.add(db_conversation)
db_assistant = BotConversation(
bot_id=bot_id,
role="assistant",
content=assistant_content,
)
db.add(db_assistant)
db.commit()
db.refresh(db_assistant)
return BotChatResponse(
response=assistant_content,
strategy_config=result.get("strategy_config"),
success=result.get("success", False),
)
@router.get("/{bot_id}/history", response_model=List[BotConversationResponse]) @router.get("/{bot_id}/history", response_model=List[BotConversationResponse])

View File

@@ -1,4 +1,4 @@
from .crew import CrewAgent from .crew import TradingCrew, get_trading_crew
from .llm_connector import LLMConnector from .llm_connector import MiniMaxLLM, MiniMaxConnector
__all__ = ["CrewAgent", "LLMConnector"] __all__ = ["TradingCrew", "get_trading_crew", "MiniMaxLLM", "MiniMaxConnector"]

View File

@@ -0,0 +1,167 @@
"""
Conversational Trading Agent
This agent can:
1. Have normal conversations with users
2. Update trading strategies when user provides specific instructions
Uses CrewAI's tool-calling capabilities for structured updates.
"""
from typing import List, Optional, Dict, Any
from crewai import Agent, LLM
from crewai.tools import tool
from sqlalchemy.orm import Session
from ...core.config import get_settings
from ...db.models import Bot
# Tool definitions
@tool
def get_current_strategy(bot_id: str) -> str:
"""Get the current trading strategy configuration for a bot.
Use this tool to check the current strategy before making changes.
Args:
bot_id: The ID of the bot to get strategy for
Returns:
JSON string with current strategy configuration
"""
from ...core.database import get_db
from ...db.models import Bot
db = next(get_db())
bot = db.query(Bot).filter(Bot.id == bot_id).first()
if not bot:
return '{"error": "Bot not found"}'
return str(bot.strategy_config)
@tool
def update_trading_strategy(
bot_id: str,
conditions: List[Dict],
actions: List[Dict],
risk_management: Optional[Dict] = None
) -> str:
"""Update the trading strategy configuration for a bot.
Call this tool when the user provides specific trading parameters like:
- Buy/sell conditions (price drops, price rises, etc.)
- Take profit percentages
- Stop loss percentages
Args:
bot_id: The ID of the bot to update
conditions: List of trigger conditions (e.g., [{"type": "price_drop", "token": "PEPE", "threshold": 5}])
actions: List of actions to take (e.g., [{"type": "buy", "amount_percent": 50}])
risk_management: Optional risk settings (e.g., {"stop_loss_percent": 10, "take_profit_percent": 50})
Returns:
Confirmation message with updated strategy
"""
from ...core.database import get_db
from ...db.models import Bot
db = next(get_db())
bot = db.query(Bot).filter(Bot.id == bot_id).first()
if not bot:
return '{"error": "Bot not found"}'
new_config = {
"conditions": conditions,
"actions": actions,
}
if risk_management:
new_config["risk_management"] = risk_management
bot.strategy_config = new_config
db.commit()
return f'Successfully updated trading strategy. New config: {new_config}'
SYSTEM_PROMPT = """You are a helpful AI trading assistant. You can:
1. Have normal conversations - answer questions about trading, tokens, strategies, etc.
2. Help users configure their trading bots when they provide specific parameters
When a user asks general questions, just answer conversationally.
When a user provides specific trading parameters (like percentages, tokens, conditions),
use the update_trading_strategy tool to save their configuration.
Example conversations:
- User: "What is this?" → Answer conversationally about the trading bot platform
- User: "I want take profit at 200%" → Use update_trading_strategy with that parameter
- User: "Alert me when PEPE drops 5%" → Use update_trading_strategy with that condition
Be friendly, helpful, and clear in your responses."""
class ConversationalAgent:
def __init__(self, api_key: str, model: str = "MiniMax-M2.7", bot_id: str = None):
self.api_key = api_key
self.model = model
self.bot_id = bot_id
self.llm = LLM(
model=model,
api_key=api_key,
api_base="https://api.minimax.io/v1"
)
# Create agent with tools
self.agent = Agent(
role="Trading Assistant",
goal="Help users with trading strategies and general questions",
backstory=SYSTEM_PROMPT,
tools=[get_current_strategy, update_trading_strategy],
llm=self.llm,
verbose=True,
allow_delegation=False,
)
def chat(self, user_message: str, conversation_history: List[Dict] = None) -> Dict[str, Any]:
"""Process a user message and return a response.
Args:
user_message: The user's message
conversation_history: Optional list of previous messages
Returns:
Dict with 'response' (the assistant's reply) and 'strategy_updated' (bool)
"""
# Execute agent using kickoff
try:
result = self.agent.kickoff(user_message)
# Check if strategy was updated
result_str = str(result)
strategy_updated = "update_trading_strategy" in result_str or \
"Successfully updated" in result_str
return {
"response": result_str,
"strategy_updated": strategy_updated,
"success": True
}
except Exception as e:
return {
"response": f"I encountered an error: {str(e)}. Please try again.",
"strategy_updated": False,
"success": False
}
def get_conversational_agent(api_key: str = None, model: str = None, bot_id: str = None) -> ConversationalAgent:
"""Get or create a ConversationalAgent instance."""
if api_key is None:
settings = get_settings()
api_key = settings.MINIMAX_API_KEY
if model is None:
settings = get_settings()
model = settings.MINIMAX_MODEL
return ConversationalAgent(api_key=api_key, model=model, bot_id=bot_id)

View File

@@ -1,7 +1,7 @@
from typing import List, Optional, Dict, Any from typing import List, Optional, Dict, Any
from crewai import Agent, Task, Crew from crewai import Agent, Task, Crew, LLM
from .llm_connector import MiniMaxConnector, MiniMaxLLM from .llm_connector import MiniMaxConnector
from ..core.config import get_settings from ...core.config import get_settings
class StrategyValidator: class StrategyValidator:
@@ -120,7 +120,7 @@ class StrategyExplainer:
def create_trading_designer_agent( def create_trading_designer_agent(
api_key: str, model: str = "MiniMax-Text-01" api_key: str, model: str = "MiniMax-M2.7"
) -> Agent: ) -> Agent:
connector = MiniMaxConnector(api_key=api_key, model=model) connector = MiniMaxConnector(api_key=api_key, model=model)
@@ -141,13 +141,13 @@ def create_trading_designer_agent(
role="Trading Strategy Designer", role="Trading Strategy Designer",
goal="Convert natural language trading requests into precise strategy configurations", goal="Convert natural language trading requests into precise strategy configurations",
backstory=system_prompt, backstory=system_prompt,
llm=MiniMaxLLM(api_key=api_key, model=model), llm=LLM(model=model, api_key=api_key, api_base="https://api.minimax.io/v1"),
verbose=True, verbose=True,
) )
def create_strategy_validator_agent( def create_strategy_validator_agent(
api_key: str, model: str = "MiniMax-Text-01" api_key: str, model: str = "MiniMax-M2.7"
) -> Agent: ) -> Agent:
return Agent( return Agent(
role="Strategy Validator", role="Strategy Validator",
@@ -155,13 +155,13 @@ def create_strategy_validator_agent(
backstory="""You are a meticulous strategy validator with expertise in trading systems. backstory="""You are a meticulous strategy validator with expertise in trading systems.
You check that all required parameters are present, values are reasonable, and the You check that all required parameters are present, values are reasonable, and the
strategy makes logical sense. You never approve strategies with missing or invalid data.""", strategy makes logical sense. You never approve strategies with missing or invalid data.""",
llm=MiniMaxLLM(api_key=api_key, model=model), llm=LLM(model=model, api_key=api_key, api_base="https://api.minimax.io/v1"),
verbose=True, verbose=True,
) )
def create_strategy_explainer_agent( def create_strategy_explainer_agent(
api_key: str, model: str = "MiniMax-Text-01" api_key: str, model: str = "MiniMax-M2.7"
) -> Agent: ) -> Agent:
return Agent( return Agent(
role="Strategy Explainer", role="Strategy Explainer",
@@ -169,13 +169,13 @@ def create_strategy_explainer_agent(
backstory="""You are a patient trading strategy explainer. You translate complex backstory="""You are a patient trading strategy explainer. You translate complex
strategy configurations into easy-to-understand language. You help users understand strategy configurations into easy-to-understand language. You help users understand
exactly what their strategies will do when triggered.""", exactly what their strategies will do when triggered.""",
llm=MiniMaxLLM(api_key=api_key, model=model), llm=LLM(model=model, api_key=api_key, api_base="https://api.minimax.io/v1"),
verbose=True, verbose=True,
) )
class TradingCrew: class TradingCrew:
def __init__(self, api_key: str, model: str = "MiniMax-Text-01"): def __init__(self, api_key: str, model: str = "MiniMax-M2.7"):
self.api_key = api_key self.api_key = api_key
self.model = model self.model = model
self.validator = StrategyValidator() self.validator = StrategyValidator()

View File

@@ -1,14 +1,12 @@
from typing import Optional, List, Dict, Any from typing import Optional, List, Dict, Any
import httpx import httpx
from crewai import LLM
class MiniMaxLLM(LLM): class MiniMaxLLM:
def __init__(self, api_key: str, model: str = "MiniMax-Text-01", **kwargs): def __init__(self, api_key: str, model: str = "MiniMax-M2.7", **kwargs):
super().__init__(**kwargs)
self.api_key = api_key self.api_key = api_key
self.model = model self.model = model
self.base_url = "https://api.minimax.chat/v1" self.base_url = "https://api.minimax.io/v1"
def _call(self, messages: List[Dict[str, str]], **kwargs) -> str: def _call(self, messages: List[Dict[str, str]], **kwargs) -> str:
headers = { headers = {
@@ -23,7 +21,7 @@ class MiniMaxLLM(LLM):
} }
with httpx.Client(timeout=60.0) as client: with httpx.Client(timeout=60.0) as client:
response = client.post( response = client.post(
f"{self.base_url}/chat/completions", f"{self.base_url}/text/chatcompletion_v2",
headers=headers, headers=headers,
json=payload, json=payload,
) )
@@ -35,7 +33,7 @@ class MiniMaxLLM(LLM):
class MiniMaxConnector: class MiniMaxConnector:
def __init__(self, api_key: str, model: str = "MiniMax-Text-01"): def __init__(self, api_key: str, model: str = "MiniMax-M2.7"):
self.api_key = api_key self.api_key = api_key
self.model = model self.model = model

View File

@@ -90,6 +90,22 @@ class AveCloudClient:
return data.get("data", []) return data.get("data", [])
raise Exception(f"Failed to fetch klines: {data}") raise Exception(f"Failed to fetch klines: {data}")
async def get_token_price(self, token_id: str) -> Optional[Dict[str, Any]]:
url = f"{self.DATA_API_URL}/v2/tokens/price"
async with httpx.AsyncClient() as client:
response = await client.post(
url,
headers=self._data_headers(),
json={"token_ids": [token_id]},
timeout=30.0,
)
response.raise_for_status()
data = response.json()
if data.get("status") == 200:
prices = data.get("data", {})
return prices.get(token_id)
return None
async def get_trending_tokens( async def get_trending_tokens(
self, chain: Optional[str] = None, limit: int = 20 self, chain: Optional[str] = None, limit: int = 20
) -> List[Dict[str, Any]]: ) -> List[Dict[str, Any]]:

View File

@@ -1,70 +0,0 @@
import httpx
from typing import List, Dict, Any, Optional
from datetime import datetime
class AveCloudClient:
BASE_URL = "https://prod.ave-api.com"
def __init__(self, api_key: str, plan: str = "free"):
self.api_key = api_key
self.plan = plan
def _headers(self) -> Dict[str, str]:
return {"X-API-KEY": self.api_key}
async def get_klines(
self,
token_id: str,
interval: str = "1h",
limit: int = 100,
start_time: Optional[int] = None,
end_time: Optional[int] = None,
) -> List[Dict[str, Any]]:
url = f"{self.BASE_URL}/v2/klines/token/{token_id}"
params = {"interval": interval, "limit": limit}
if start_time:
params["start_time"] = start_time
if end_time:
params["end_time"] = end_time
async with httpx.AsyncClient() as client:
response = await client.get(
url, headers=self._headers(), params=params, timeout=30.0
)
response.raise_for_status()
data = response.json()
if data.get("status") == 200:
return data.get("data", [])
raise Exception(f"Failed to fetch klines: {data}")
async def get_token_price(self, token_id: str) -> Optional[Dict[str, Any]]:
url = f"{self.BASE_URL}/v2/tokens/price"
async with httpx.AsyncClient() as client:
response = await client.post(
url,
headers=self._headers(),
json={"token_ids": [token_id]},
timeout=30.0,
)
response.raise_for_status()
data = response.json()
if data.get("status") == 200:
prices = data.get("data", {})
return prices.get(token_id)
return None
async def get_batch_prices(self, token_ids: List[str]) -> Dict[str, Dict[str, Any]]:
url = f"{self.BASE_URL}/v2/tokens/price"
async with httpx.AsyncClient() as client:
response = await client.post(
url,
headers=self._headers(),
json={"token_ids": token_ids},
timeout=30.0,
)
response.raise_for_status()
data = response.json()
if data.get("status") == 200:
return data.get("data", {})
return {}

View File

@@ -2,7 +2,7 @@ import uuid
import asyncio import asyncio
from datetime import datetime from datetime import datetime
from typing import Dict, Any, List, Optional from typing import Dict, Any, List, Optional
from .ave_client import AveCloudClient from ..ave.client import AveCloudClient
class BacktestEngine: class BacktestEngine:
@@ -20,10 +20,15 @@ class BacktestEngine:
self.strategy_config = config.get("strategy_config", {}) self.strategy_config = config.get("strategy_config", {})
self.conditions = self.strategy_config.get("conditions", []) self.conditions = self.strategy_config.get("conditions", [])
self.actions = self.strategy_config.get("actions", []) self.actions = self.strategy_config.get("actions", [])
self.risk_management = self.strategy_config.get("risk_management", {})
self.stop_loss_percent = self.risk_management.get("stop_loss_percent")
self.take_profit_percent = self.risk_management.get("take_profit_percent")
self.initial_balance = config.get("initial_balance", 10000.0) self.initial_balance = config.get("initial_balance", 10000.0)
self.current_balance = self.initial_balance self.current_balance = self.initial_balance
self.position = 0.0 self.position = 0.0
self.position_token = "" self.position_token = ""
self.entry_price: Optional[float] = None
self.entry_time: Optional[int] = None
self.trades: List[Dict[str, Any]] = [] self.trades: List[Dict[str, Any]] = []
self.running = False self.running = False
@@ -103,11 +108,73 @@ class BacktestEngine:
timestamp = kline.get("timestamp", 0) timestamp = kline.get("timestamp", 0)
if self.position > 0 and self.entry_price is not None:
exit_info = self._check_risk_management(price, timestamp)
if exit_info:
await self._execute_risk_exit(price, timestamp, exit_info)
continue
for condition in self.conditions: for condition in self.conditions:
if self._check_condition(condition, klines, i, price): if self._check_condition(condition, klines, i, price):
await self._execute_actions(price, timestamp, condition) await self._execute_actions(price, timestamp, condition)
break break
def _check_risk_management(
self, current_price: float, timestamp: int
) -> Optional[Dict[str, Any]]:
if self.position <= 0 or self.entry_price is None:
return None
if self.stop_loss_percent is not None:
stop_loss_price = self.entry_price * (1 - self.stop_loss_percent / 100)
if current_price <= stop_loss_price:
return {"reason": "stop_loss", "price": stop_loss_price}
if self.take_profit_percent is not None:
take_profit_price = self.entry_price * (1 + self.take_profit_percent / 100)
if current_price >= take_profit_price:
return {"reason": "take_profit", "price": take_profit_price}
return None
async def _execute_risk_exit(
self, price: float, timestamp: int, exit_info: Dict[str, Any]
):
if self.position <= 0:
return
reason = exit_info["reason"]
sell_amount = self.position * price
self.current_balance += sell_amount
self.trades.append(
{
"type": "sell",
"token": self.position_token,
"price": price,
"amount": sell_amount,
"quantity": self.position,
"timestamp": timestamp,
"exit_reason": reason,
}
)
self.signals.append(
{
"id": str(uuid.uuid4()),
"bot_id": self.bot_id,
"run_id": self.run_id,
"signal_type": "sell",
"token": self.position_token,
"price": price,
"confidence": 1.0,
"reasoning": f"Risk management triggered {reason}",
"executed": False,
"created_at": datetime.utcnow(),
}
)
self.position = 0
self.entry_price = None
self.entry_time = None
def _check_condition( def _check_condition(
self, self,
condition: Dict[str, Any], condition: Dict[str, Any],
@@ -173,6 +240,8 @@ class BacktestEngine:
self.position += amount / price self.position += amount / price
self.current_balance -= amount self.current_balance -= amount
self.position_token = token self.position_token = token
self.entry_price = price
self.entry_time = timestamp
self.trades.append( self.trades.append(
{ {
"type": "buy", "type": "buy",
@@ -209,9 +278,12 @@ class BacktestEngine:
"amount": sell_amount, "amount": sell_amount,
"quantity": self.position, "quantity": self.position,
"timestamp": timestamp, "timestamp": timestamp,
"exit_reason": "manual",
} }
) )
self.position = 0 self.position = 0
self.entry_price = None
self.entry_time = None
self.signals.append( self.signals.append(
{ {
"id": str(uuid.uuid4()), "id": str(uuid.uuid4()),

View File

@@ -1,8 +1,11 @@
import uuid import uuid
import asyncio import asyncio
import logging
from datetime import datetime from datetime import datetime
from typing import Dict, Any, List, Optional from typing import Dict, Any, List, Optional
from ..backtest.ave_client import AveCloudClient from ..ave.client import AveCloudClient
logger = logging.getLogger(__name__)
class SimulateEngine: class SimulateEngine:
@@ -20,6 +23,9 @@ class SimulateEngine:
self.strategy_config = config.get("strategy_config", {}) self.strategy_config = config.get("strategy_config", {})
self.conditions = self.strategy_config.get("conditions", []) self.conditions = self.strategy_config.get("conditions", [])
self.actions = self.strategy_config.get("actions", []) self.actions = self.strategy_config.get("actions", [])
self.risk_management = self.strategy_config.get("risk_management", {})
self.stop_loss_percent = self.risk_management.get("stop_loss_percent")
self.take_profit_percent = self.risk_management.get("take_profit_percent")
self.check_interval = config.get("check_interval", 60) self.check_interval = config.get("check_interval", 60)
self.duration_seconds = config.get("duration_seconds", 3600) self.duration_seconds = config.get("duration_seconds", 3600)
self.auto_execute = config.get("auto_execute", False) self.auto_execute = config.get("auto_execute", False)
@@ -29,6 +35,13 @@ class SimulateEngine:
self.started_at: Optional[datetime] = None self.started_at: Optional[datetime] = None
self.last_price: Optional[float] = None self.last_price: Optional[float] = None
self.last_volume: Optional[float] = None self.last_volume: Optional[float] = None
self.position: float = 0.0
self.position_token: str = ""
self.entry_price: Optional[float] = None
self.entry_time: Optional[int] = None
self.current_balance: float = config.get("initial_balance", 10000.0)
self.trades: List[Dict[str, Any]] = []
self.errors: List[str] = []
async def run(self) -> Dict[str, Any]: async def run(self) -> Dict[str, Any]:
self.running = True self.running = True
@@ -65,7 +78,9 @@ class SimulateEngine:
self.last_volume = current_volume self.last_volume = current_volume
except Exception as e: except Exception as e:
pass logger.warning(f"Failed to get price for {token_id}: {e}")
self.errors.append(f"Price fetch failed for {token_id}: {str(e)}")
continue
for _ in range(self.check_interval): for _ in range(self.check_interval):
if not self.running: if not self.running:
@@ -83,6 +98,8 @@ class SimulateEngine:
self.results = self.results or {} self.results = self.results or {}
self.results["total_signals"] = len(self.signals) self.results["total_signals"] = len(self.signals)
self.results["total_errors"] = len(self.errors)
self.results["errors"] = self.errors
self.results["signals"] = self.signals self.results["signals"] = self.signals
self.results["started_at"] = self.started_at self.results["started_at"] = self.started_at
self.results["ended_at"] = datetime.utcnow() self.results["ended_at"] = datetime.utcnow()
@@ -94,11 +111,70 @@ class SimulateEngine:
): ):
timestamp = int(datetime.utcnow().timestamp() * 1000) timestamp = int(datetime.utcnow().timestamp() * 1000)
if self.position > 0 and self.entry_price is not None:
exit_info = self._check_risk_management(current_price, timestamp)
if exit_info:
await self._execute_risk_exit(current_price, timestamp, exit_info)
return
for condition in self.conditions: for condition in self.conditions:
if self._check_condition(condition, current_price, current_volume): if self._check_condition(condition, current_price, current_volume):
await self._execute_actions(current_price, timestamp, condition) await self._execute_actions(current_price, timestamp, condition)
break break
def _check_risk_management(
self, current_price: float, timestamp: int
) -> Optional[Dict[str, Any]]:
if self.position <= 0 or self.entry_price is None:
return None
if self.stop_loss_percent is not None:
stop_loss_price = self.entry_price * (1 - self.stop_loss_percent / 100)
if current_price <= stop_loss_price:
return {"reason": "stop_loss", "price": stop_loss_price}
if self.take_profit_percent is not None:
take_profit_price = self.entry_price * (1 + self.take_profit_percent / 100)
if current_price >= take_profit_price:
return {"reason": "take_profit", "price": take_profit_price}
return None
async def _execute_risk_exit(
self, price: float, timestamp: int, exit_info: Dict[str, Any]
):
if self.position <= 0:
return
reason = exit_info["reason"]
self.trades.append(
{
"type": "sell",
"token": self.position_token,
"price": price,
"quantity": self.position,
"timestamp": timestamp,
"exit_reason": reason,
}
)
self.signals.append(
{
"id": str(uuid.uuid4()),
"bot_id": self.bot_id,
"run_id": self.run_id,
"signal_type": "sell",
"token": self.position_token,
"price": price,
"confidence": 1.0,
"reasoning": f"Risk management triggered {reason}",
"executed": self.auto_execute,
"created_at": datetime.utcnow(),
}
)
self.position = 0
self.entry_price = None
self.entry_time = None
def _check_condition( def _check_condition(
self, self,
condition: Dict[str, Any], condition: Dict[str, Any],
@@ -146,20 +222,41 @@ class SimulateEngine:
token = matched_condition.get("token", self.token) token = matched_condition.get("token", self.token)
reasoning = f"Condition {matched_condition.get('type')} triggered" reasoning = f"Condition {matched_condition.get('type')} triggered"
signal = { for action in self.actions:
"id": str(uuid.uuid4()), action_type = action.get("type", "")
"bot_id": self.bot_id, if action_type == "buy":
"run_id": self.run_id, amount_percent = action.get("amount_percent", 10)
"signal_type": "signal", amount = self.current_balance * (amount_percent / 100)
"token": token, self.position += amount / price
"price": price, self.position_token = token
"confidence": 0.8, self.entry_price = price
"reasoning": reasoning, self.entry_time = timestamp
"executed": self.auto_execute, self.current_balance -= amount
"created_at": datetime.utcnow(), self.trades.append(
} {
"type": "buy",
"token": token,
"price": price,
"amount": amount,
"quantity": amount / price,
"timestamp": timestamp,
}
)
self.signals.append(signal) signal = {
"id": str(uuid.uuid4()),
"bot_id": self.bot_id,
"run_id": self.run_id,
"signal_type": action_type,
"token": token,
"price": price,
"confidence": 0.8,
"reasoning": reasoning,
"executed": self.auto_execute,
"created_at": datetime.utcnow(),
}
self.signals.append(signal)
async def stop(self): async def stop(self):
self.running = False self.running = False

View File

@@ -6,6 +6,7 @@ pydantic-settings>=2.1.0
email-validator>=2.0.0 email-validator>=2.0.0
python-jose[cryptography]>=3.3.0 python-jose[cryptography]>=3.3.0
passlib[bcrypt]>=1.7.4 passlib[bcrypt]>=1.7.4
bcrypt>=4.0,<5.0 # Required for passlib compatibility
crewai>=0.1.0 crewai>=0.1.0
anthropic>=0.18.0 anthropic>=0.18.0
httpx>=0.26.0 httpx>=0.26.0

View File

@@ -104,11 +104,12 @@ export const api = {
} }
}, },
async chat(id: string, message: string): Promise<BotChatResponse> { async chat(id: string, message: string, signal?: AbortSignal): Promise<BotChatResponse> {
const response = await fetch(`${API_URL}/bots/${id}/chat`, { const response = await fetch(`${API_URL}/bots/${id}/chat`, {
method: 'POST', method: 'POST',
headers: getAuthHeaders(), headers: getAuthHeaders(),
body: JSON.stringify({ message } as BotChatRequest) body: JSON.stringify({ message } as BotChatRequest),
signal
}); });
return handleResponse<BotChatResponse>(response); return handleResponse<BotChatResponse>(response);
}, },

View File

@@ -8,12 +8,25 @@ export interface ChatMessage {
timestamp: Date; timestamp: Date;
} }
// Fallback UUID generator for environments where crypto.randomUUID is not available
function generateId(): string {
if (typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function') {
return crypto.randomUUID();
}
// Fallback: simple UUID v4 implementation
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
const r = (Math.random() * 16) | 0;
const v = c === 'x' ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
}
export const chatStore = writable<ChatMessage[]>([]); export const chatStore = writable<ChatMessage[]>([]);
export function addMessage(message: Omit<ChatMessage, 'id' | 'timestamp'>) { export function addMessage(message: Omit<ChatMessage, 'id' | 'timestamp'>) {
const newMessage: ChatMessage = { const newMessage: ChatMessage = {
...message, ...message,
id: crypto.randomUUID(), id: generateId(),
timestamp: new Date() timestamp: new Date()
}; };
chatStore.update(messages => [...messages, newMessage]); chatStore.update(messages => [...messages, newMessage]);

View File

@@ -44,8 +44,17 @@
isSending = true; isSending = true;
// Add user's message immediately so it shows even before API response
addMessage({ role: 'user', content: message });
try { try {
const response = await api.bots.chat(botId, message); // Add timeout to prevent hanging requests
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 30000);
const response = await api.bots.chat(botId, message, controller.signal);
clearTimeout(timeoutId);
addMessage({ role: 'assistant', content: response.response }); addMessage({ role: 'assistant', content: response.response });
if (response.strategy_config) { if (response.strategy_config) {
@@ -53,7 +62,11 @@
setCurrentBot(bot); setCurrentBot(bot);
} }
} catch (e) { } catch (e) {
addMessage({ role: 'assistant', content: 'Sorry, I encountered an error. Please try again.' }); if (e instanceof Error && e.name === 'AbortError') {
addMessage({ role: 'assistant', content: 'Request timed out. Please try again.' });
} else {
addMessage({ role: 'assistant', content: 'Sorry, I encountered an error. Please try again.' });
}
} finally { } finally {
isSending = false; isSending = false;
} }