File size: 2,170 Bytes
72fc0ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"""
QuantLib Pro SDK — Backtesting Resource
"""
from typing import Any, Dict, List
from quantlib_api.resources.base import BaseResource


class BacktestingResource(BaseResource):
    """Strategy backtesting and performance analysis."""

    PREFIX = "/api/v1/backtesting"

    def strategies(self) -> Dict[str, Any]:
        """List available backtesting strategies."""
        return self._http.get(self._url("/strategies"))

    def run(
        self,
        strategy: str,
        tickers: List[str],
        start_date: str = None,
        end_date: str = None,
        initial_capital: float = 100000.0,
        parameters: Dict[str, Any] = None,
    ) -> Dict[str, Any]:
        """
        Run backtest for a strategy.

        Parameters
        ----------
        strategy : str
            Strategy name (e.g., "momentum", "mean_reversion", "pairs_trading")
        tickers : list of str
            Universe of tickers
        start_date : str
            Backtest start date (YYYY-MM-DD)
        end_date : str
            Backtest end date (YYYY-MM-DD)
        initial_capital : float
            Starting capital
        parameters : dict
            Strategy-specific parameters

        Returns
        -------
        dict
            Backtest results: returns, Sharpe, drawdown, trade log
        """
        return self._http.post(
            self._url("/run"),
            json={
                "strategy": strategy,
                "tickers": tickers,
                "start_date": start_date,
                "end_date": end_date,
                "initial_capital": initial_capital,
                "parameters": parameters or {},
            },
        )

    def performance(
        self,
        backtest_id: str,
    ) -> Dict[str, Any]:
        """Get detailed performance metrics for a backtest."""
        return self._http.get(self._url(f"/performance/{backtest_id}"))

    def compare(
        self,
        backtest_ids: List[str],
    ) -> Dict[str, Any]:
        """Compare multiple backtest results."""
        return self._http.post(
            self._url("/compare"),
            json={"backtest_ids": backtest_ids},
        )