Pre-refactor: commit before converting indicators to self-contained files
This commit is contained in:
@ -25,12 +25,6 @@ from pydantic import BaseModel, Field
|
||||
# Imports for backtest runner
|
||||
from src.data_collector.database import DatabaseManager
|
||||
from src.data_collector.indicator_engine import IndicatorEngine, IndicatorConfig
|
||||
from src.data_collector.brain import Brain
|
||||
from src.data_collector.backtester import Backtester
|
||||
|
||||
# Imports for strategy discovery
|
||||
import importlib
|
||||
from src.strategies.base import BaseStrategy
|
||||
|
||||
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
@ -103,45 +97,6 @@ async def root():
|
||||
}
|
||||
|
||||
|
||||
@app.get("/api/v1/strategies")
|
||||
async def list_strategies(response: Response):
|
||||
"""List all available trading strategies with metadata"""
|
||||
# Prevent caching
|
||||
response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
|
||||
response.headers["Pragma"] = "no-cache"
|
||||
response.headers["Expires"] = "0"
|
||||
|
||||
# Strategy registry from brain.py
|
||||
strategy_registry = {
|
||||
"ma_trend": "src.strategies.ma_strategy.MAStrategy",
|
||||
}
|
||||
|
||||
strategies = []
|
||||
|
||||
for strategy_id, class_path in strategy_registry.items():
|
||||
try:
|
||||
module_path, class_name = class_path.rsplit('.', 1)
|
||||
module = importlib.import_module(module_path)
|
||||
strategy_class = getattr(module, class_name)
|
||||
|
||||
# Instantiate to get metadata
|
||||
strategy_instance = strategy_class()
|
||||
|
||||
strategies.append({
|
||||
"id": strategy_id,
|
||||
"name": strategy_instance.display_name,
|
||||
"description": strategy_instance.description,
|
||||
"required_indicators": strategy_instance.required_indicators
|
||||
})
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to load strategy {strategy_id}: {e}")
|
||||
|
||||
return {
|
||||
"strategies": strategies,
|
||||
"count": len(strategies)
|
||||
}
|
||||
|
||||
|
||||
@app.get("/api/v1/candles")
|
||||
async def get_candles(
|
||||
symbol: str = Query("BTC", description="Trading pair symbol"),
|
||||
@ -417,50 +372,6 @@ async def list_backtests(symbol: Optional[str] = None, limit: int = 20):
|
||||
return [dict(row) for row in rows]
|
||||
|
||||
|
||||
class BacktestRequest(BaseModel):
|
||||
symbol: str = "BTC"
|
||||
intervals: list[str] = ["37m"]
|
||||
start_date: str = "2025-01-01" # ISO date
|
||||
end_date: Optional[str] = None
|
||||
|
||||
|
||||
async def run_backtest_task(req: BacktestRequest):
|
||||
"""Background task to run backtest"""
|
||||
db = DatabaseManager(
|
||||
host=DB_HOST, port=DB_PORT, database=DB_NAME,
|
||||
user=DB_USER, password=DB_PASSWORD
|
||||
)
|
||||
await db.connect()
|
||||
|
||||
try:
|
||||
# Load configs (hardcoded for now to match main.py)
|
||||
configs = [
|
||||
IndicatorConfig("ma44", "sma", 44, req.intervals),
|
||||
IndicatorConfig("ma125", "sma", 125, req.intervals)
|
||||
]
|
||||
|
||||
engine = IndicatorEngine(db, configs)
|
||||
brain = Brain(db, engine)
|
||||
backtester = Backtester(db, engine, brain)
|
||||
|
||||
start = datetime.fromisoformat(req.start_date).replace(tzinfo=timezone.utc)
|
||||
end = datetime.fromisoformat(req.end_date).replace(tzinfo=timezone.utc) if req.end_date else datetime.now(timezone.utc)
|
||||
|
||||
await backtester.run(req.symbol, req.intervals, start, end)
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Backtest failed: {e}")
|
||||
finally:
|
||||
await db.disconnect()
|
||||
|
||||
|
||||
@app.post("/api/v1/backtests")
|
||||
async def trigger_backtest(req: BacktestRequest, background_tasks: BackgroundTasks):
|
||||
"""Start a backtest in the background"""
|
||||
background_tasks.add_task(run_backtest_task, req)
|
||||
return {"message": "Backtest started", "params": req.dict()}
|
||||
|
||||
|
||||
@app.get("/api/v1/ta")
|
||||
async def get_technical_analysis(
|
||||
symbol: str = Query("BTC", description="Trading pair symbol"),
|
||||
|
||||
Reference in New Issue
Block a user