tradai.data.core.entities¶
OHLCV data models and related entities.
Core entities for tradai-data library.
Value objects that eliminate code duplication: - DateRange: Eliminates 6 duplicate validation sites - SymbolList: Eliminates 4 duplicate conversions - Timeframe: Eliminates 3 fragile regex parsings - OHLCVData: Validated DataFrame wrapper
All entities are immutable (frozen=True) for thread safety.
DateRange ¶
Bases: BaseModel
Validated date range for queries.
Eliminates duplicate validation logic across 6 sites in the codebase. Provides duration calculations and range checks.
Example
dr = DateRange.from_strings("2024-01-01", "2024-01-31") dr.duration_days 30 dr.contains(datetime(2024, 1, 15)) True
Source code in libs/tradai-data/src/tradai/data/core/entities.py
duration_days: int property ¶
Calculate duration in days.
validate_range(v: datetime, info: ValidationInfo) -> datetime classmethod ¶
Ensure end is after start.
Source code in libs/tradai-data/src/tradai/data/core/entities.py
from_strings(start: str | datetime | pd.Timestamp | np.datetime64, end: str | datetime | pd.Timestamp | np.datetime64) -> DateRange classmethod ¶
Create DateRange from various date formats.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start | str | datetime | Timestamp | datetime64 | Start date (ISO string, datetime, pandas Timestamp, or numpy datetime64) | required |
end | str | datetime | Timestamp | datetime64 | End date (ISO string, datetime, pandas Timestamp, or numpy datetime64) | required |
Returns:
| Type | Description |
|---|---|
DateRange | Validated DateRange instance |
Example
DateRange.from_strings("2024-01-01", "2024-01-31") DateRange.from_strings(pd.Timestamp("2024-01-01"), datetime(2024, 1, 31))
Source code in libs/tradai-data/src/tradai/data/core/entities.py
SymbolList ¶
Bases: BaseModel
Validated list of trading symbols.
Eliminates duplicate symbol preparation logic across 4 sites. Handles str/list inputs, deduplication, and validation.
Example
sl = SymbolList.from_input(["BTC/USDT:USDT", "ETH/USDT:USDT"]) sl.to_list() ['BTC/USDT:USDT', 'ETH/USDT:USDT']
Source code in libs/tradai-data/src/tradai/data/core/entities.py
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 | |
validate_non_empty(v: frozenset[str]) -> frozenset[str] classmethod ¶
Ensure at least one symbol.
Source code in libs/tradai-data/src/tradai/data/core/entities.py
from_input(symbols: str | list[str]) -> SymbolList classmethod ¶
Create SymbolList from string or list input.
Automatically deduplicates symbols using frozenset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbols | str | list[str] | Single symbol string or list of symbols | required |
Returns:
| Type | Description |
|---|---|
SymbolList | Validated SymbolList instance |
Raises:
| Type | Description |
|---|---|
ValidationError | If symbols is empty string or empty list |
Example
SymbolList.from_input("BTC/USDT:USDT") SymbolList.from_input(["BTC/USDT:USDT", "ETH/USDT:USDT"]) SymbolList.from_input(["BTC/USDT:USDT", "BTC/USDT:USDT"]) # Deduplicates
Source code in libs/tradai-data/src/tradai/data/core/entities.py
Timeframe ¶
Bases: BaseModel
Validated CCXT-compatible timeframe.
Uses pandas.Timedelta for flexible input parsing (handles aliases like "1 hour", "5min", etc.) but validates against CCXT-supported timeframes.
Supported timeframes: 1s, 1m, 3m, 5m, 15m, 30m, 1h, 2h, 4h, 6h, 8h, 12h, 1d, 3d, 1w, 1M
Example
tf = Timeframe.parse("1h") # Standard tf = Timeframe.parse("1 hour") # Alias works tf = Timeframe.parse("4 hours") # Alias works tf.seconds 3600 tf.normalized '1h'
Source code in libs/tradai-data/src/tradai/data/core/entities.py
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 | |
minutes: int property ¶
Get timeframe in minutes.
normalized: str property ¶
Get normalized CCXT timeframe string for API calls.
parse(timeframe: str) -> Timeframe classmethod ¶
Parse timeframe string with alias support, validated against CCXT.
Uses pandas.Timedelta for flexible parsing, then validates that the resulting duration matches a CCXT-supported timeframe exactly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timeframe | str | Timeframe string (e.g., "1h", "1 hour", "5min", "4 hours") | required |
Returns:
| Type | Description |
|---|---|
Timeframe | Validated Timeframe instance |
Raises:
| Type | Description |
|---|---|
ValidationError | If timeframe is invalid or not CCXT-supported |
Example
Timeframe.parse("1h") # -> Timeframe(value='1h', seconds=3600) Timeframe.parse("1 hour") # -> Timeframe(value='1h', seconds=3600) Timeframe.parse("1h 30m") # Raises: not a valid CCXT timeframe
Source code in libs/tradai-data/src/tradai/data/core/entities.py
OHLCVData ¶
Bases: BaseModel
Validated OHLCV market data container.
Wraps pandas DataFrame with validation and immutability guarantees. Required columns: symbol, date, open, high, low, close, volume.
Example
df = pd.DataFrame({...}) data = OHLCVData.from_dataframe(df) data.symbols {'BTC/USDT:USDT', 'ETH/USDT:USDT'} data.row_count 100
Source code in libs/tradai-data/src/tradai/data/core/entities.py
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 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 | |
is_empty: bool property ¶
Check if data is empty.
date_range: DateRange property ¶
Get actual date range from data.
from_dataframe(df: pd.DataFrame) -> OHLCVData classmethod ¶
Create OHLCVData from pandas DataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df | DataFrame | DataFrame with required columns | required |
Returns:
| Type | Description |
|---|---|
OHLCVData | Validated OHLCVData instance |
Raises:
| Type | Description |
|---|---|
ValidationError | If DataFrame is empty or missing required columns |
Example
df = pd.DataFrame({ ... "symbol": ["BTC/USDT:USDT"], ... "date": [datetime(2024, 1, 1)], ... "open": [45000.0], ... "high": [45200.0], ... "low": [44900.0], ... "close": [45100.0], ... "volume": [100.0], ... }) data = OHLCVData.from_dataframe(df)
Source code in libs/tradai-data/src/tradai/data/core/entities.py
to_dataframe() -> pd.DataFrame ¶
get_symbol_data(symbol: str) -> pd.DataFrame ¶
empty() -> OHLCVData classmethod ¶
Create an empty OHLCVData instance.
Useful for returning when no data is collected.
Returns:
| Type | Description |
|---|---|
OHLCVData | Empty OHLCVData with no rows |
Example
empty_data = OHLCVData.empty() empty_data.row_count 0 empty_data.is_empty True
Source code in libs/tradai-data/src/tradai/data/core/entities.py
merge(datasets: list[OHLCVData]) -> OHLCVData classmethod ¶
Merge multiple OHLCVData instances into one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
datasets | list[OHLCVData] | List of OHLCVData to merge | required |
Returns:
| Type | Description |
|---|---|
OHLCVData | Combined OHLCVData with all data |
Raises:
| Type | Description |
|---|---|
ValueError | If datasets list is empty |
Example
btc_data = OHLCVData.from_dataframe(btc_df) eth_data = OHLCVData.from_dataframe(eth_df) combined = OHLCVData.merge([btc_data, eth_data])