summaryrefslogtreecommitdiff
path: root/cli/src/trading_cli/commands/backtest.py
diff options
context:
space:
mode:
Diffstat (limited to 'cli/src/trading_cli/commands/backtest.py')
-rw-r--r--cli/src/trading_cli/commands/backtest.py45
1 files changed, 23 insertions, 22 deletions
diff --git a/cli/src/trading_cli/commands/backtest.py b/cli/src/trading_cli/commands/backtest.py
index 01fe092..c17c61f 100644
--- a/cli/src/trading_cli/commands/backtest.py
+++ b/cli/src/trading_cli/commands/backtest.py
@@ -20,9 +20,9 @@ def backtest():
@backtest.command()
@click.option("--strategy", required=True, help="Strategy name to backtest")
-@click.option("--symbol", required=True, help="Trading symbol (e.g. BTCUSDT)")
+@click.option("--symbol", required=True, help="Trading symbol (e.g. AAPL)")
@click.option("--timeframe", default="1h", show_default=True, help="Candle timeframe")
-@click.option("--balance", default=10000.0, show_default=True, help="Initial balance in USDT")
+@click.option("--balance", default=10000.0, show_default=True, help="Initial balance in USD")
@click.option(
"--output",
"output_format",
@@ -35,11 +35,12 @@ def backtest():
def run(strategy, symbol, timeframe, balance, output_format, file_path):
"""Run a backtest for a strategy."""
try:
- from strategy_engine.plugin_loader import load_strategies
from backtester.engine import BacktestEngine
- from backtester.reporter import format_report, export_csv, export_json
- from shared.db import Database
+ from backtester.reporter import export_csv, export_json, format_report
+ from strategy_engine.plugin_loader import load_strategies
+
from shared.config import Settings
+ from shared.db import Database
from shared.models import Candle
except ImportError as e:
click.echo(f"Error: Could not import required modules: {e}", err=True)
@@ -58,7 +59,7 @@ def run(strategy, symbol, timeframe, balance, output_format, file_path):
async def _run():
settings = Settings()
- db = Database(settings.database_url)
+ db = Database(settings.database_url.get_secret_value())
await db.connect()
try:
candle_rows = await db.get_candles(symbol, timeframe, limit=500)
@@ -66,20 +67,19 @@ def run(strategy, symbol, timeframe, balance, output_format, file_path):
click.echo(f"Error: No candles found for {symbol} {timeframe}", err=True)
sys.exit(1)
- candles = []
- for row in reversed(candle_rows): # get_candles returns DESC, we need ASC
- candles.append(
- Candle(
- symbol=row["symbol"],
- timeframe=row["timeframe"],
- open_time=row["open_time"],
- open=row["open"],
- high=row["high"],
- low=row["low"],
- close=row["close"],
- volume=row["volume"],
- )
+ candles = [
+ Candle(
+ symbol=row["symbol"],
+ timeframe=row["timeframe"],
+ open_time=row["open_time"],
+ open=row["open"],
+ high=row["high"],
+ low=row["low"],
+ close=row["close"],
+ volume=row["volume"],
)
+ for row in reversed(candle_rows) # get_candles returns DESC, we need ASC
+ ]
engine = BacktestEngine(strat, Decimal(str(balance)))
result = engine.run(candles)
@@ -111,10 +111,11 @@ def run(strategy, symbol, timeframe, balance, output_format, file_path):
def walk_forward(strategy, symbol, timeframe, balance, windows):
"""Run walk-forward analysis to detect overfitting."""
try:
- from strategy_engine.plugin_loader import load_strategies
from backtester.walk_forward import WalkForwardEngine
- from shared.db import Database
+ from strategy_engine.plugin_loader import load_strategies
+
from shared.config import Settings
+ from shared.db import Database
from shared.models import Candle
except ImportError as e:
click.echo(f"Error: Could not import required modules: {e}", err=True)
@@ -131,7 +132,7 @@ def walk_forward(strategy, symbol, timeframe, balance, windows):
async def _run():
settings = Settings()
- db = Database(settings.database_url)
+ db = Database(settings.database_url.get_secret_value())
await db.connect()
try:
rows = await db.get_candles(symbol, timeframe, limit=2000)