Team Ai
Apppublic

diegobeyl/backtesting

sourceHugging Faceupdated 9mo agoView on Hugging Face
2likes
analyze_stop.py65 linesDownload Raw Back to analysis
1import sys
2sys.path.insert(0, 'backtesting_app/backtesting_app')
3
4import yfinance as yf
5import pandas as pd
6from algorithms.fast_ariascampos import FastAriasCampos
7from core.backtester import Backtester
8
9df = yf.download('AAPL', start='2023-01-01', end='2024-06-01', interval='1mo', progress=False)
10df = df[['Open', 'High', 'Low', 'Close', 'Volume']]
11df.columns = ['open', 'high', 'low', 'close', 'volume']
12
13algo = FastAriasCampos()
14params = algo.get_default_params()
15params['timeframe'] = '1mo'
16
17backtester = Backtester(
18    algorithm=algo,
19    initial_capital=10000,
20    commission_pct=0.1,
21    position_sizing='risk',
22    risk_percent=3.0
23)
24
25result = backtester.run(df, params, symbol='AAPL', timeframe='1mo')
26
27# Buscar el trade LONG
28for trade in result.trades:
29    if trade.direction == 'LONG' and trade.entry_date.year == 2023:
30        print(f"=== TRADE LONG {trade.entry_date.strftime('%Y-%m-%d')} ===\n")
31        print(f"Entry Price: ${trade.entry_price:.2f}")
32        print(f"Stop Loss Inicial: ${trade.initial_sl:.2f}")
33        
34        if trade.sl_updates:
35            print(f"\nSL UPDATES ({len(trade.sl_updates)} veces):")
36            for idx, sl in enumerate(trade.sl_updates, 1):
37                print(f"  {idx}. ${sl:.2f}")
38            final_sl = trade.sl_updates[-1]
39        else:
40            final_sl = trade.initial_sl
41            
42        print(f"\nStop Loss FINAL usado: ${final_sl:.2f}")
43        print(f"\nExit Date: {trade.exit_date.strftime('%Y-%m-%d')}")
44        print(f"Exit Price: ${trade.exit_price:.2f}")
45        print(f"Exit Reason: {trade.exit_reason}")
46        
47        # Mostrar barras
48        print(f"\n=== BARRAS DEL TRADE ===")
49        entry_idx = df.index.get_loc(trade.entry_date)
50        exit_idx = df.index.get_loc(trade.exit_date)
51        
52        for i in range(entry_idx, exit_idx + 1):
53            bar = df.iloc[i]
54            state = result.algorithm_result.states[i]
55            soporte_str = f"${state.support:.2f}" if state.support else "None"
56            
57            # Check if low touches SL
58            hit_sl = ""
59            if i > entry_idx:  # Skip entry bar
60                current_sl = trade.sl_updates[min(i - entry_idx - 1, len(trade.sl_updates) - 1)] if trade.sl_updates else trade.initial_sl
61                if bar['low'] <= current_sl:
62                    hit_sl = f" <<< HIT SL! (${current_sl:.2f})"
63            
64            print(f"{bar.name.strftime('%Y-%m-%d')} | Low: ${bar['low']:7.2f} | Soporte: {soporte_str:>9s}{hit_sl}")
65