策略參數¶
參數的 schema(名稱、範圍、預設值),以及各 K 棒 interval 使用的預設組合。
cjtrade.pkgs.strategy.parameters.schema
¶
cjtrade.pkgs.strategy.parameters.schema ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Parameter schema: definition, type, range, constraints.
Enables: - Parameter validation - Grid search / optimization (know the search space) - Documentation of what params are available
validate_param(key, value)
¶
Check if param value is valid according to schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Parameter name |
required |
value
|
Any
|
Parameter value |
required |
Returns:
| Type | Description |
|---|---|
(is_valid, error_message)
|
Tuple of (bool, str). If valid, error_message is empty. |
Source code in src/cjtrade/pkgs/strategy/parameters/schema.py
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 | |
can_optimize(key)
¶
Check if param can be optimized (not fixed).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Parameter name |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if parameter can be optimized |
Source code in src/cjtrade/pkgs/strategy/parameters/schema.py
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 | |
get_param_range(key)
¶
Get optimization range for a parameter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Parameter name |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Dict with 'min', 'max', 'step', 'type' and 'default'; empty if the key is unknown or fixed. |
Source code in src/cjtrade/pkgs/strategy/parameters/schema.py
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 | |
cjtrade.pkgs.strategy.parameters.presets
¶
cjtrade.pkgs.strategy.parameters.presets ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Interval-based parameter presets.
Different intervals (1m, 5m, 1h, 1d) require different parameter values for the same strategy to work effectively.
This module centralizes all preset values per interval.
get_interval_presets(interval)
¶
Get base presets for given interval.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
interval
|
str
|
'1m', '5m', '1h', '1d' |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Preset parameters for this interval (deep copy to avoid mutations) |
Raises:
| Type | Description |
|---|---|
ValueError
|
If interval is not supported |
Warnings
Window parameters are in bars, so each interval has its own values; do not reuse one interval's preset on another.
Source code in src/cjtrade/pkgs/strategy/parameters/presets.py
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 | |
get_supported_intervals()
¶
Get list of supported intervals.
Returns:
| Type | Description |
|---|---|
list[str]
|
List of supported interval strings |
Source code in src/cjtrade/pkgs/strategy/parameters/presets.py
170 171 172 173 174 175 176 177 178 | |
cjtrade.pkgs.strategy.parameters.config
¶
cjtrade.pkgs.strategy.parameters.config ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ParameterConfig: encapsulates all parameters + metadata for one run.
Tracks: - Final parameter values - Which preset was used as base - What user overrides were applied - Hash for experiment reproducibility
ParameterConfig(interval, strategy_name, params, base_preset=dict(), user_overrides=dict(), created_at=(lambda: datetime.now().isoformat())())
dataclass
¶
Holds all parameters for a strategy run.
Attributes:
| Name | Type | Description |
|---|---|---|
interval |
str
|
KBar interval ('1m', '5m', '1h', '1d') |
strategy_name |
str
|
Strategy name ('BollingerStrategy', 'SupportResistanceStrategy', etc.) |
params |
dict
|
Final resolved parameters (preset + overrides) |
base_preset |
dict
|
The interval preset we started with |
user_overrides |
dict
|
User-specified overrides (what params they changed) |
created_at |
str
|
ISO timestamp of when this config was created |
Warnings
created_at defaults to the wall-clock time (datetime.now()); it is not
part of params_hash.
params_hash
property
¶
Hash of params for reproducibility and experiment tracking.
Returns:
| Type | Description |
|---|---|
str
|
8-char SHA256 hash of params |
to_dict()
¶
Serialize to dict for logging/saving.
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary representation suitable for JSON serialization |
Source code in src/cjtrade/pkgs/strategy/parameters/config.py
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | |
to_json()
¶
Serialize to JSON string.
Returns:
| Type | Description |
|---|---|
str
|
JSON representation |
Source code in src/cjtrade/pkgs/strategy/parameters/config.py
88 89 90 91 92 93 94 95 96 | |