-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain.py
More file actions
130 lines (107 loc) · 3.91 KB
/
main.py
File metadata and controls
130 lines (107 loc) · 3.91 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
# Kraken Automated Trading Bot
# Main Script
import os
import sys
import logging
import atexit
from logging.handlers import RotatingFileHandler
from pathlib import Path
from dotenv import load_dotenv
# Load .env BEFORE importing any module that reads env vars at module level
load_dotenv()
from kraken_interface import KrakenAPI
from trading_bot import TradingBot, Backtester
from utils import load_config, validate_config
try:
import fcntl
except Exception: # pragma: no cover
fcntl = None
CONFIG_PATH = "config.toml"
LOCK_FILE = "/tmp/kraken_bot.lock"
_lock_fp = None
def acquire_single_instance_lock():
global _lock_fp
if fcntl is None:
return
_lock_fp = open(LOCK_FILE, "w")
try:
fcntl.flock(_lock_fp.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
_lock_fp.write(str(os.getpid()))
_lock_fp.flush()
except BlockingIOError:
print("Another kraken_bot instance is already running. Exiting.")
sys.exit(1)
def release_lock():
global _lock_fp
try:
if _lock_fp and fcntl is not None:
fcntl.flock(_lock_fp.fileno(), fcntl.LOCK_UN)
_lock_fp.close()
except Exception:
pass
atexit.register(release_lock)
acquire_single_instance_lock()
try:
config = load_config(CONFIG_PATH)
except FileNotFoundError:
print(f"Error: Configuration file '{CONFIG_PATH}' not found.")
sys.exit(1)
except Exception as e:
print(f"Error loading configuration: {e}")
sys.exit(1)
if not validate_config(config):
print("Warning: Configuration validation failed. Some settings may be missing.")
log_dir = Path(config['logging'].get('log_file_path', 'logs/bot_activity.log')).parent
log_dir.mkdir(parents=True, exist_ok=True)
log_file = config['logging']['log_file_path'] if config['logging'].get('log_to_file', True) else None
root_logger = logging.getLogger()
root_logger.setLevel(config['logging'].get('log_level', 'INFO'))
_fmt = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
if log_file:
try:
Path(log_file).parent.mkdir(parents=True, exist_ok=True)
_fh = RotatingFileHandler(
log_file,
maxBytes=5 * 1024 * 1024, # 5 MB per file
backupCount=5,
encoding='utf-8',
)
_fh.setFormatter(_fmt)
root_logger.addHandler(_fh)
except Exception as e:
print(f"Warning: Could not configure log file {log_file}: {e}")
_sh = logging.StreamHandler(sys.stdout)
_sh.setFormatter(_fmt)
root_logger.addHandler(_sh)
logger = logging.getLogger(__name__)
api_key = os.getenv('KRAKEN_API_KEY', '')
api_secret = os.getenv('KRAKEN_API_SECRET', '')
if not api_key or not api_secret:
logger.warning("API credentials not configured. Set KRAKEN_API_KEY and KRAKEN_API_SECRET.")
print("WARNING: Kraken API credentials are not configured.")
kraken = KrakenAPI(api_key=api_key, api_secret=api_secret)
trading_bot = TradingBot(kraken, config)
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description="Kraken Automated Trading Bot")
parser.add_argument("--backtest", action="store_true", help="Run backtesting mode.")
parser.add_argument("--test", action="store_true", help="Run test mode (check API connection).")
args = parser.parse_args()
if args.test:
logger.info("Running test mode...")
print("Testing Kraken API connection...")
balance = kraken.get_account_balance()
if balance is not None:
print("[OK] Successfully connected to Kraken API")
print(f"Account balance: {balance}")
else:
print("[ERROR] Failed to connect to Kraken API")
sys.exit(0)
elif args.backtest:
logger.info("Starting backtesting...")
backtester = Backtester(kraken, config)
backtester.run()
else:
logger.info("Starting live trading...")
print("Starting Kraken Trading Bot...")
trading_bot.start_trading()