-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_strategy.py
More file actions
106 lines (90 loc) · 3.52 KB
/
Copy pathtest_strategy.py
File metadata and controls
106 lines (90 loc) · 3.52 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
104
105
106
from connection import AlpacaConnection
from strategies.moving_average_crossover import MovingAverageCrossoverStrategy
import logging
from config import LOG_LEVEL, LOG_FILE
import time
from datetime import datetime
# Configure logging
logging.basicConfig(
level=getattr(logging, LOG_LEVEL),
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler(LOG_FILE),
logging.StreamHandler()
]
)
logger = logging.getLogger(__name__)
def get_market_status(api):
"""Get and display market status"""
try:
clock = api.get_clock()
next_open = clock.next_open
next_close = clock.next_close
if clock.is_open:
logger.info("Market is currently OPEN")
logger.info(f"Market closes at: {next_close}")
else:
logger.info("Market is currently CLOSED")
logger.info(f"Next market open: {next_open}")
logger.info(f"Next market close: {next_close}")
return clock.is_open
except Exception as e:
logger.error(f"Error getting market status: {str(e)}")
return False
def test_strategy():
"""Test the moving average crossover strategy"""
# Initialize connection
alpaca = AlpacaConnection()
try:
# Connect to Alpaca
logger.info("Testing Alpaca connection...")
if not alpaca.connect():
logger.error("Failed to connect to Alpaca")
return False
# Check market status
is_market_open = get_market_status(alpaca.api)
if not is_market_open:
logger.info("Market is closed. Strategy will wait for market open.")
# Initialize strategy
logger.info("Initializing strategy...")
strategy = MovingAverageCrossoverStrategy(
api=alpaca.api,
symbol='AAPL', # Trading Apple stock as an example
short_window=20,
long_window=50
)
# Run strategy for 5 minutes
logger.info("Running strategy for 5 minutes...")
start_time = time.time()
while time.time() - start_time < 300: # Run for 5 minutes
try:
# Get historical data
df = strategy.get_historical_data()
if df is not None:
logger.info(f"Latest price for {strategy.symbol}: ${df['close'].iloc[-1]:.2f}")
# Calculate signals
df = strategy.calculate_signals(df)
if df is not None:
current_signal = df['signal'].iloc[-1]
logger.info(f"Current signal: {current_signal}")
# Execute trade based on signal
strategy.execute_trade(current_signal, df['close'].iloc[-1])
# Wait for 1 minute before next iteration
time.sleep(60)
except Exception as e:
logger.error(f"Error in strategy loop: {str(e)}")
time.sleep(60)
logger.info("Strategy test completed!")
return True
except KeyboardInterrupt:
logger.info("Strategy test interrupted by user")
return False
except Exception as e:
logger.error(f"Unexpected error: {str(e)}")
return False
finally:
# Clean up
if alpaca.is_connected():
alpaca.disconnect()
if __name__ == "__main__":
test_strategy()