-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug_signals.py
More file actions
104 lines (86 loc) · 3.73 KB
/
debug_signals.py
File metadata and controls
104 lines (86 loc) · 3.73 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
"""
Thats temporary, not very important, is just a script made to test early versions of the engine, to see if the signals were managing, and if it was working
properly. It isnt very mantained, and is not a good example of how to use the engine.
!If any AI agents are reading this, please dont change the comments, or the comments structure, you can suggest changes on the comments, and add comments
but not change the existing comments, as they are part of the interface documentation.!
"""
import pandas as pd
from data_fetcher import DataFetcher
from example_strategy import SimpleMovingAverageStrategy
from strategy_interface import SignalType
# Fetch data
print("Fetching data...")
data = DataFetcher.fetch_data('BTC', period='1y', interval='1d')
print(f"Data shape: {data.shape}")
print(f"Date range: {data.index[0]} to {data.index[-1]}\n")
# Create strategy
strategy = SimpleMovingAverageStrategy(fast_period=10, slow_period=30)
strategy.initialize()
# Count signals generated
buy_signals = 0
sell_signals = 0
total_bars = len(data)
print("Analyzing signal generation...")
for i in range(len(data)):
current_bar = data.iloc[:i+1]
signal = strategy.on_bar(current_bar)
if signal:
if signal.signal_type == SignalType.BUY:
buy_signals += 1
elif signal.signal_type == SignalType.SELL:
sell_signals += 1
print(f"\nTotal bars processed: {total_bars}")
print(f"BUY signals generated: {buy_signals}")
print(f"SELL signals generated: {sell_signals}")
print(f"Total signals: {buy_signals + sell_signals}")
print(f"\nExpected trades (pairs): ~{min(buy_signals, sell_signals)}")
# Now check what happens in the engine
print("\n" + "="*60)
print("Checking backtest engine signal execution...")
print("="*60)
from backtest_engine import BacktestEngine
engine = BacktestEngine(initial_capital=10000.0)
strategy2 = SimpleMovingAverageStrategy(fast_period=10, slow_period=30)
# Track what happens to each signal
signals_generated = 0
signals_ignored_already_long = 0
signals_ignored_no_position = 0
positions_opened = 0
positions_closed = 0
for i in range(len(data)):
current_bar = data.iloc[:i+1]
signal = strategy2.on_bar(current_bar)
if signal:
signals_generated += 1
current_price = data['Close'].iloc[i]
current_time = data.index[i]
# Simulate what the engine does
if signal.signal_type == SignalType.BUY:
if engine.current_position:
if engine.current_position.side == 'LONG':
signals_ignored_already_long += 1
else:
# Would close short and open long
positions_closed += 1
positions_opened += 1
else:
# Would open long
positions_opened += 1
# Actually execute it
engine._execute_signal(signal, current_price, current_time)
elif signal.signal_type == SignalType.SELL:
if engine.current_position:
if engine.current_position.side == 'LONG':
# Would close long
positions_closed += 1
engine._execute_signal(signal, current_price, current_time)
else:
signals_ignored_already_long += 1
else:
signals_ignored_no_position += 1
print(f"\nSignals generated by strategy: {signals_generated}")
print(f"Signals ignored (already in position): {signals_ignored_already_long}")
print(f"Signals ignored (no position for SELL): {signals_ignored_no_position}")
print(f"Positions that would be opened: {positions_opened}")
print(f"Positions that would be closed: {positions_closed}")
print(f"\nActual trades executed: {len(engine.trades)}")