-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·416 lines (341 loc) · 15 KB
/
main.py
File metadata and controls
executable file
·416 lines (341 loc) · 15 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#!/usr/bin/env python3
"""
Turtle Trading Bot - Main Entry Point
Automated Turtle Trading Strategy for Cryptocurrency Markets
"""
import sys
import time
import logging
from pathlib import Path
from datetime import datetime, timezone
from typing import Dict, List
# Local imports
from config import load_config
from core.turtle_engine import TurtleEngine
from risk.risk_manager import RiskManager
from risk.portfolio_manager import PortfolioManager
from utils.state import BotState
from utils.notifications import Notifier, Colors, colored
from utils.logging_config import setup_logging
from exchange.base import CCXTAdapter
from utils.multi_exchange import MultiExchangeFetcher
logger = logging.getLogger(__name__)
# === MARKET DATA FETCHING ===
def fetch_market_data(multi_fetcher: MultiExchangeFetcher, symbols: List[str], config) -> Dict:
"""
Fetch OHLC and current prices for all symbols using multi-exchange fallback
Args:
multi_fetcher: MultiExchangeFetcher instance
symbols: List of symbols to fetch
config: Config instance
Returns:
Dict of symbol -> {'ohlc': [...], 'price': float, 'ticker': dict, 'source': str}
"""
return multi_fetcher.fetch_market_data(
symbols,
timeframe=config.OHLC_TIMEFRAME,
limit=config.OHLC_LIMIT,
batch_size=config.BATCH_SIZE
)
def calculate_atrs(market_data: Dict, turtle_engine: TurtleEngine) -> Dict:
"""
Calculate ATR for all symbols
Args:
market_data: Market data dict
turtle_engine: TurtleEngine instance
Returns:
Updated market_data with 'atr' field
"""
for symbol, data in market_data.items():
ohlc = data.get('ohlc', [])
if ohlc:
atr = turtle_engine.calculate_atr(ohlc)
data['atr'] = atr
return market_data
def get_coin_universe(config, quote_currency: str = 'USDT') -> List[str]:
"""
Get list of coins to trade
Args:
config: Config instance
quote_currency: Quote currency (default: USDT)
Returns:
List of trading symbols
"""
import json as _json
from pathlib import Path as _Path
# Load blocked coins list
blocked_coins_path = _Path(__file__).parent / 'blocked_coins.json'
try:
with open(blocked_coins_path, 'r') as f:
blocked_data = _json.load(f)
blocked_set = set(blocked_data.get('blocked_coins', []))
logger.info(f"Loaded {len(blocked_set)} blocked coins from blocked_coins.json")
except Exception as e:
logger.warning(f"Could not load blocked_coins.json: {e} — proceeding with no block list")
blocked_set = set()
if config.SCAN_TOP_COINS:
# Fetch top N coins by volume from Kraken
logger.info(f"Fetching top {config.TOP_N_COINS} coins from Kraken by volume...")
from utils.kraken_discovery import KrakenDiscovery
top_pairs = KrakenDiscovery().get_top_pairs_by_volume(
limit=config.TOP_N_COINS,
min_volume_usd=100_000,
blocked_coins=blocked_set,
)
symbols = [p['symbol'] for p in top_pairs]
logger.info(f"Got {len(symbols)} quality pairs from Kraken (filtered by volume)")
return symbols
else:
# Use fixed coin list
fixed_keys = [c for c in config.FIXED_COINS if c.upper() not in blocked_set]
filtered_count = len(config.FIXED_COINS) - len(fixed_keys)
if filtered_count:
logger.info(f"Blocked coins filter removed {filtered_count} coin(s) from fixed coins list")
return [f"{coin}/{quote_currency}" for coin in fixed_keys]
# === MAIN BOT FUNCTION ===
def run_bot():
"""Main bot execution"""
# Load configuration
try:
config = load_config()
except ValueError as e:
print(colored(f"\n!! Configuration Error:\n{e}\n", Colors.RED))
sys.exit(1)
# Setup logging
setup_logging(config.LOG_FILE, log_format=config.LOG_FORMAT)
# Print configuration summary
config.print_summary()
# Initialize components
logger.info("Initializing components...")
# Turtle Engine
turtle_engine = TurtleEngine(config)
# Risk Manager
risk_manager = RiskManager(config)
# Notifier
notifier = Notifier(config)
notifier.print_banner()
# Exchange adapters
exchanges = {}
quote_currency = 'USDT' # USDT-only trading
try:
# Kraken exchange (only exchange)
creds = config.get_api_credentials()
exchanges['kraken'] = CCXTAdapter(
'kraken',
api_key=creds['api_key'],
api_secret=creds['api_secret'],
paper_trading=config.PAPER_TRADING,
slippage=config.PAPER_SLIPPAGE,
)
logger.info(f"Initialized Kraken exchange "
f"({'PAPER TRADING' if config.PAPER_TRADING else 'LIVE TRADING'})")
# Kraken data fetcher (Kraken also used for coin discovery in get_coin_universe)
multi_fetcher = MultiExchangeFetcher(
primary_exchange=exchanges['kraken'],
quote_currency=quote_currency
)
logger.info(f"Kraken fetcher initialized (quote: {quote_currency})")
except Exception as e:
print(colored(f"\n!! Exchange Initialization Error: {e}\n", Colors.RED))
sys.exit(1)
# Portfolio Manager
portfolio_manager = PortfolioManager(
config,
turtle_engine,
risk_manager,
exchanges,
notifier
)
# Load state
logger.info(f"Loading state from {config.STATE_FILE}...")
state = BotState.load(config.STATE_FILE, config.ACCOUNT_SIZE)
# Initialize equity if not set
if state.initial_equity == 0:
state.initial_equity = config.ACCOUNT_SIZE
state.current_equity = config.ACCOUNT_SIZE
state.peak_equity = config.ACCOUNT_SIZE
# Get coin universe (USDT pairs only)
symbols = get_coin_universe(config, quote_currency=quote_currency)
logger.info(f"Trading universe: {len(symbols)} symbols (quote: {quote_currency})")
# Check if bot was previously emergency-stopped — require manual reset
if state.emergency_stopped:
print(colored("\n!! Bot is locked after an emergency stop.", Colors.RED))
print(colored(f" Triggered at: {state.emergency_stopped_at}", Colors.RED))
print(colored(" Edit bot_state.json and set emergency_stopped to false to restart.", Colors.YELLOW))
sys.exit(1)
# Main loop
print(colored(f"\n>> Turtle Bot started at {datetime.now(timezone.utc).strftime('%Y-%m-%d %H:%M UTC')}", Colors.CYAN))
print(colored(f"$$ Initial Equity: ${state.initial_equity:,.2f}", Colors.WHITE))
print(colored(f" Press Ctrl+C to stop\n", Colors.GRAY))
try:
while True:
state.iteration += 1
print("\n" + colored("=" * 75, Colors.BLUE))
print(colored(f"~ Update #{state.iteration}", Colors.BLUE) +
colored(f" | {datetime.now(timezone.utc).strftime('%Y-%m-%d %H:%M UTC')}", Colors.GRAY))
print(colored("=" * 75, Colors.BLUE))
# Fetch market data (with multi-exchange fallback)
logger.info("Fetching market data...")
market_data = fetch_market_data(multi_fetcher, symbols, config)
if not market_data:
logger.warning("No market data fetched, skipping cycle")
time.sleep(config.CHECK_INTERVAL)
continue
# Calculate ATRs
market_data = calculate_atrs(market_data, turtle_engine)
logger.info(f"Fetched data for {len(market_data)} symbols")
# Get current prices
current_prices = {sym: data['price'] for sym, data in market_data.items()}
# Get account balance
account_balance = exchanges[config.PRIMARY_EXCHANGE].get_balance()
if config.PAPER_TRADING:
# Use realized cash balance for position sizing (not total equity)
# This prevents double-counting unrealized P&L
if state.cash_balance == 0:
# Initialize cash balance on first run
state.cash_balance = state.current_equity
account_balance = state.cash_balance
# === PRIORITY 0: CHECK EMERGENCY STOP (HIGHEST PRIORITY) ===
# Check emergency stop BEFORE any trading decisions
if risk_manager.check_emergency_stop(state.current_equity, state.initial_equity):
print(colored("\n!! EMERGENCY STOP TRIGGERED!", Colors.RED))
print(colored(f"Drawdown: {risk_manager.emergency_stop_loss * 100:.1f}%", Colors.RED))
print(colored("Closing all positions...\n", Colors.YELLOW))
# Close all positions
for symbol in list(state.active_positions.keys()):
portfolio_manager.execute_exit(
symbol,
current_prices.get(symbol, 0),
'EMERGENCY_STOP',
state
)
# Lock bot to prevent restart without manual reset
state.emergency_stopped = True
state.emergency_stopped_at = datetime.now(timezone.utc).isoformat()
# Save state and stop
state.save(config.STATE_FILE)
logger.warning("Emergency stop triggered - exiting")
break
# === PRIORITY 1: CHECK STOPS ===
logger.info("Checking stops...")
stop_hits = portfolio_manager.scan_for_stops(market_data, state.active_positions)
for symbol in stop_hits:
position = state.get_position(symbol)
if position:
notifier.alert_stop_hit(
symbol,
current_prices[symbol],
position.stop_price,
position.get_market_value(current_prices[symbol])
)
# Execute exit
portfolio_manager.execute_exit(
symbol,
current_prices[symbol],
'STOP_HIT',
state
)
# === PRIORITY 1.5: UPDATE TRAILING STOPS ===
# Run after stop checks so a newly-moved trailing stop is
# evaluated on the *next* cycle (not the same cycle it moved).
portfolio_manager.scan_trailing_stops(market_data, state.active_positions)
# === PRIORITY 2: CHECK EXIT SIGNALS ===
logger.info("Checking exit signals...")
exit_symbols = portfolio_manager.scan_for_exits(market_data, state.active_positions)
for symbol in exit_symbols:
# Execute exit
portfolio_manager.execute_exit(
symbol,
current_prices[symbol],
'EXIT_SIGNAL',
state
)
# === PRIORITY 3: CHECK PYRAMID OPPORTUNITIES ===
logger.info("Checking pyramid opportunities...")
pyramid_ops = portfolio_manager.scan_for_pyramids(
market_data,
state.active_positions,
account_balance
)
for pyramid in pyramid_ops:
# Execute pyramid
portfolio_manager.execute_pyramid(
pyramid['symbol'],
pyramid['quantity'],
pyramid['price'],
state
)
# === PRIORITY 4: CHECK ENTRY SIGNALS (LOWEST PRIORITY) ===
# Skip entry signals if bot is paused
if state.is_paused:
logger.info("Bot is paused - skipping entry signals")
print(colored(f"\n|| Bot PAUSED: {state.pause_reason}", Colors.YELLOW))
print(colored(f" Paused since: {state.paused_at.strftime('%Y-%m-%d %H:%M UTC') if state.paused_at else 'Unknown'}", Colors.GRAY))
print(colored(" Still managing existing positions (stops, exits, pyramids)", Colors.GRAY))
else:
logger.info("Checking entry signals...")
entry_signals = portfolio_manager.scan_for_entries(
market_data,
state.active_positions,
account_balance
)
for signal in entry_signals:
# Execute entry
portfolio_manager.execute_entry(
signal['symbol'],
signal['system'],
config.PRIMARY_EXCHANGE,
signal['quantity'],
signal['price'],
signal['atr'],
state
)
# Update equity
total_pnl = sum(
pos.calculate_pnl(current_prices.get(pos.symbol, 0))
for pos in state.active_positions.values()
)
state.update_equity(account_balance + total_pnl)
# Display portfolio
if state.active_positions:
notifier.print_portfolio_summary(state, current_prices)
else:
print(colored("\n>> No active positions", Colors.GRAY))
# Display performance
if state.total_trades > 0:
notifier.print_performance(state)
# Save state
state.save(config.STATE_FILE)
logger.info(f"State saved to {config.STATE_FILE}")
# Sleep until next check
if state.iteration > 0:
print(colored(f"\nzz Next update in {config.CHECK_INTERVAL // 60} minutes...", Colors.GRAY))
time.sleep(config.CHECK_INTERVAL)
except KeyboardInterrupt:
print("\n\n" + colored("=" * 75, Colors.PURPLE))
print(colored("!! Stopping Turtle Bot...", Colors.YELLOW))
# Save final state
state.save(config.STATE_FILE)
print(colored(f">> State saved to {config.STATE_FILE}", Colors.GREEN))
# Display final summary
if state.active_positions:
print(colored(f"\n>> Final Portfolio ({len(state.active_positions)} positions):", Colors.PURPLE))
for symbol, position in state.active_positions.items():
print(f" {colored(symbol, Colors.WHITE)}: {position.unit_count} units, "
f"Avg Entry: ${position.avg_entry_price:.2f}, Stop: ${position.stop_price:.2f}")
if state.total_trades > 0:
notifier.print_performance(state)
print(colored("\n" + "=" * 75, Colors.PURPLE))
print(colored(" Turtle Bot stopped. Trade by the Turtle rules!\n", Colors.CYAN))
except Exception as e:
logger.error(f"Unexpected error: {e}", exc_info=True)
notifier.alert_error("Critical error in main loop", e)
# Save state on error
try:
state.save(config.STATE_FILE)
print(colored(f"\n>> State saved despite error", Colors.YELLOW))
except Exception:
pass
sys.exit(1)
if __name__ == "__main__":
run_bot()