File size: 1,796 Bytes
75d9b3c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Sample Backtest Script — Test the walk-forward engine on a small subset.
"""
import logging
from backend.backtesting.backtester import run_portfolio_backtest
from config import INDIA_UNIVERSE, US_UNIVERSE

# Setup logging
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")

def main():
    # 1. Select a small test universe (5 India, 5 US)
    test_tickers = INDIA_UNIVERSE[:5] + US_UNIVERSE[:5]
    
    # 2. Run backtest for last 1 year
    print(f"Starting sample backtest on: {test_tickers}")
    results = run_portfolio_backtest(test_tickers, period="1y", initial_capital=10_00_000)
    
    if "error" in results:
        print(f"Error: {results['error']}")
        return

    # 3. Print Summary
    metrics = results["metrics"]
    print("\n" + "="*40)
    print("       BACKTEST RESULTS SUMMARY")
    print("="*40)
    print(f"Total Return:    {metrics['total_return_pct']}%")
    print(f"Max Drawdown:    {metrics['max_drawdown_pct']}%")
    print(f"Win Rate:        {metrics['win_rate_pct']}%")
    print(f"Total Trades:    {metrics['total_trades']}")
    print(f"Final Equity:    ₹{metrics['final_equity']:,.2f}")
    print("="*40)
    
    # 4. Run Monte Carlo Stress Test
    if results["trades"]:
        from backend.backtesting.monte_carlo import run_monte_carlo, print_mc_report
        mc_stats, _ = run_monte_carlo(results["trades"], 10_00_000)
        print_mc_report(mc_stats)

    # List last 5 trades
    if results["trades"]:
        print("\nLast 5 Trades:")
        for t in results["trades"][-5:]:
            status = t.get("status", "Open")
            print(f"- {t['ticker']}: Entry {t['entry_price']} | Exit {t.get('exit_price', 'N/A')} | PnL {t.get('pnl', 0):.2f} | {status}")

if __name__ == "__main__":
    main()