-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathutils.py
More file actions
141 lines (109 loc) · 4.28 KB
/
utils.py
File metadata and controls
141 lines (109 loc) · 4.28 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
# Utility Functions for Kraken Trading Bot
"""
Utility Helpers
===============
Shared utilities for the Kraken trading bot.
Functions
---------
``load_config(path)``
Load and parse ``config.toml``; raises ``FileNotFoundError`` if missing.
``validate_config(config)``
Check that all required sections and keys are present. Returns a bool
so callers can warn and fall back rather than crash.
``nas_paths(cfg_path)``
Return a dict of resolved ``pathlib.Path`` objects for NAS directories:
- ``nas_root`` — mount point (default ``/mnt/fritz_nas/Volume/kraken``)
- ``ohlc_2026`` — 2026 OHLC data directory
- ``ohlc_2025`` — 2025 OHLC data directory
- ``bot_cache`` — shared cache for pre-processed indicator data
All paths are sourced from the ``[paths]`` section of ``config.toml`` so
moving the NAS mount only requires editing one place.
"""
import toml
import logging
from pathlib import Path
_DEFAULT_CFG_PATH = Path(__file__).parent / "config.toml"
def load_config(config_path):
"""
Load configuration from a TOML file.
Args:
config_path (str): Path to the TOML configuration file.
Returns:
dict: Configuration dictionary.
"""
try:
if not Path(config_path).exists():
raise FileNotFoundError(f"Configuration file not found: {config_path}")
with open(config_path, 'r') as f:
config = toml.load(f)
logging.info(f"Configuration loaded successfully from {config_path}")
return config
except Exception as e:
logging.error(f"Error loading configuration: {e}")
raise
def nas_paths(cfg_path: Path = _DEFAULT_CFG_PATH) -> dict:
"""Return NAS path config as a dict of Path objects.
Keys: nas_root, ohlc_2026, ohlc_2025, bot_cache
Falls back to sensible defaults if config is missing.
"""
try:
cfg = toml.load(cfg_path).get("paths", {})
except Exception:
cfg = {}
root = Path(cfg.get("nas_root", "/mnt/fritz_nas/Volume/kraken"))
return {
"nas_root": root,
"ohlc_2026": Path(cfg.get("nas_ohlc_2026", str(root / "2026" / "ohlc"))),
"ohlc_2025": Path(cfg.get("nas_ohlc_2025", str(root / "2025" / "ohlcvt"))),
"bot_cache": Path(cfg.get("nas_bot_cache", str(root / "bot_cache"))),
}
"""
Load configuration from a TOML file.
Args:
config_path (str): Path to the TOML configuration file.
Returns:
dict: Configuration dictionary.
"""
try:
if not Path(config_path).exists():
raise FileNotFoundError(f"Configuration file not found: {config_path}")
with open(config_path, 'r') as f:
config = toml.load(f)
logging.info(f"Configuration loaded successfully from {config_path}")
return config
except Exception as e:
logging.error(f"Error loading configuration: {e}")
raise
def validate_config(config):
"""
Validate that all required configuration values are present.
Args:
config (dict): Configuration dictionary.
Returns:
bool: True if valid, False otherwise.
"""
required_sections = ['bot_settings', 'risk_management', 'logging']
for section in required_sections:
if section not in config:
logging.warning(f"Missing config section: {section}")
return False
bot_settings = config.get('bot_settings', {})
trade_amounts = bot_settings.get('trade_amounts', {})
# Accept both legacy single-pair config and current multi-pair config
has_pairs = bool(bot_settings.get('trade_pairs')) or bool(bot_settings.get('trade_pair'))
if not has_pairs:
logging.warning("Missing config key: bot_settings.trade_pairs (or legacy trade_pair)")
return False
if 'trade_amount_eur' not in trade_amounts:
logging.warning("Missing config key: bot_settings.trade_amounts.trade_amount_eur")
return False
risk = config.get('risk_management', {})
for k in ['max_drawdown_percent', 'stop_loss_percent']:
if k not in risk:
logging.warning(f"Missing config key: risk_management.{k}")
return False
logging_cfg = config.get('logging', {})
if 'log_level' not in logging_cfg:
logging.warning("Missing config key: logging.log_level")
return False
return True