Simulation
The Simulation class enables users to initialize simulation instances, set parameters, load the preprocessed LOB Data into the simulation, run the simulation, and return results.
Conceptually, you first set the parameters of the simulation (battery, dynamic programming, and simulation settings), then decide which days of LOB data to feed, before subsequently running the simulation for the desired amount of time. Order book traversals and optimizations happen in C++, while pre-/post-processing and settings are done in Python. Results are returned as Pandas dataframes and can be fed into the post-processing described below.
Source code in bitepy/simulation.py
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 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 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 | |
__init__(start_date, end_date, trading_start_date=None, storage_max=10.0, lin_deg_cost=4.0, loss_in=0.95, loss_out=0.95, trading_fee=0.09, num_stor_states=11, trading_delay=0, tec_delay=0, fixed_solve_time=0, solve_frequency=0.0, withdraw_max=5.0, inject_max=5.0, log_transactions=False, cycle_limit=None)
Initialize a Simulation instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start_date
|
Timestamp
|
The start datetime of the simulation, i.e. which products are loaded into the simulation. Must be timezone aware. |
required |
end_date
|
Timestamp
|
The end datetime of the simulation, i.e. which products are loaded into the simulation. Must be timezone aware. |
required |
trading_start_date
|
Timestamp
|
The start datetime of the trading, i.e. when the trading starts. Must be timezone aware. If None, the trading starts at the same time as the start_date. |
None
|
storage_max
|
float
|
The maximum storage capacity of the storage unit (MWh). Default is 10.0. |
10.0
|
lin_deg_cost
|
float
|
The linear degradation cost of the storage unit (€/MWh). Default is 4.0. |
4.0
|
loss_in
|
float
|
The injection efficiency of the storage unit (0-1]. Default is 0.95. |
0.95
|
loss_out
|
float
|
The withdrawal efficiency of the storage unit (0-1]. Default is 0.95. |
0.95
|
trading_fee
|
float
|
The trading fee for the exchange (€/MWh). Default is 0.09. |
0.09
|
num_stor_states
|
int
|
The number of storage states for dynamic programming. Default is 11. |
11
|
trading_delay
|
int
|
The trading delay of the storage unit, i.e., when to start trading all new products after gate opening. (min, >= 0 and < 480 mins (8 hours)). Default is 0. |
0
|
tec_delay
|
int
|
The technical delay of the storage unit (ms, >= 0). Default is 0. |
0
|
fixed_solve_time
|
int
|
The fixed solve time for dynamic programming (ms, >= 0 or -1 for realistic solve times). Default is 0. |
0
|
solve_frequency
|
float
|
The frequency at which the dynamic programming solver is run (min). Default is 0.0. |
0.0
|
withdraw_max
|
float
|
The maximum withdrawal power of the storage unit (MW). Default is 5.0. |
5.0
|
inject_max
|
float
|
The maximum injection power of the storage unit (MW). Default is 5.0. |
5.0
|
log_transactions
|
bool
|
If True, we run the simulation only to log transactions data of the market, no optimization is performed. Default is False. |
False
|
cycle_limit
|
float
|
The limit on the number of cycles per Berlin-time day. Setting it comes at a cost in terms of solve time. (float, > 0). Default is None, where no cycle limit is enforced. |
None
|
Source code in bitepy/simulation.py
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 | |
add_bin_to_orderqueue(bin_data)
Add an order binary file to the simulation's order queue.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bin_data
|
str
|
The path to the order binary file. |
required |
Source code in bitepy/simulation.py
161 162 163 164 165 166 167 168 | |
add_df_to_orderqueue(df)
Add a DataFrame of orders to the simulation's order queue.
The DataFrame must have the same columns as the saved CSV files, with timestamps in UTC (seconds and milliseconds).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
A DataFrame containing the orders to be added. |
required |
Processing Steps
- Validate that the timestamp columns ('start', 'transaction', 'validity') are timezone aware.
- Ensure that all timestamps are in the same timezone.
- Convert all timestamps to UTC and format them in ISO 8601.
Source code in bitepy/simulation.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 | |
get_data_bins_for_each_day(base_path, start_date, end_date)
Generate a list of file paths for binary order book data for each day within a date range.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_path
|
str
|
The base directory path where the binary files are stored. |
required |
start_date
|
Timestamp
|
The start date of the range. |
required |
end_date
|
Timestamp
|
The end date of the range. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
list |
A list of file paths for each day's binary order book file. |
Source code in bitepy/simulation.py
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 | |
get_limit_order_matches()
Get the limit order matches collected during simulation using the forecast tracking mechanism.
This method retrieves match information for submitted limit orders that were processed during the simulation run. The orders are tracked using the forecast flag system.
Processing Steps
- Retrieves match data from the C++ simulation backend.
- Converts timestamps to timezone-aware datetime objects.
- Clears the internal match storage after retrieval.
Returns:
| Type | Description |
|---|---|
|
pd.DataFrame: A DataFrame containing information about which limit orders were matched against which existing orders. The DataFrame contains the following columns: - submitted_order_id: The ID of the submitted limit order. - matched_order_id: The ID of the existing order that was matched. - match_timestamp: The timestamp when the match occurred. - delivery_hour: The delivery hour for the matched order. - match_price: The price at which the orders were matched, i.e., the price of the existing (partially) matched order (€/MWh). - match_volume: The volume that was matched (MWh). - submitted_order_side: The side of the submitted order ('buy' or 'sell'). - existing_order_side: The side of the existing order ('buy' or 'sell'). |
Source code in bitepy/simulation.py
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 | |
get_logs()
Retrieve the logs generated by the simulation.
Returns:
| Name | Type | Description |
|---|---|---|
dict |
A dictionary containing simulation logs with the following keys: - decision_record: Final simulation schedule. - price_record: CID price data over the simulation duration. - accepted_orders: Limit orders accepted by the RI. - executed_orders: Orders sent to the exchange by the RI. - killed_orders: Orders that were missed at the exchange. |
Source code in bitepy/simulation.py
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 | |
get_transactions()
Retrieve all transactions that have occurred since the last call and clear the internal transaction log.
Returns:
| Type | Description |
|---|---|
|
pd.DataFrame: A DataFrame containing all transactions that occurred, with the following columns: - timestamp: The UTC timestamp when the transaction occurred. - delivery_hour: The UTC timestamp of the delivery hour for the traded product. - price: The execution price of the transaction (EUR/MWh). - volume: The volume of the transaction (MW). - buy_order_type: The type of the buy order ('Market' or 'Limit'). - sell_order_type: The type of the sell order ('Market' or 'Limit'). - buy_order_id: The ID of the buy order. - sell_order_id: The ID of the sell order. |
Source code in bitepy/simulation.py
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 | |
group_transactions(transactions)
Group transactions by timestamp and delivery hour, calculating volume-weighted average prices.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transactions
|
DataFrame
|
A DataFrame containing the transactions to be grouped. |
required |
Processing Steps
- Group the transactions by timestamp and delivery_hour.
- Calculate the volume weighted average price for each group.
- Return a DataFrame with aggregated transaction data.
Returns:
| Type | Description |
|---|---|
|
pd.DataFrame: A DataFrame with the following columns: - timestamp: The UTC timestamp when the transaction occurred. - delivery_hour: The UTC timestamp of the delivery hour for the traded product. - vwap: The volume weighted average price of the transaction. - total_volume: The total volume of the transaction. - num_transactions: The number of transactions in the group. |
Source code in bitepy/simulation.py
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 | |
print_parameters()
Print the simulation parameters, including start/end times, storage settings, and various limits and costs.
Processing Steps
- Extract simulation start, end, and trading start times from internal parameters.
- Display all relevant storage configuration parameters.
- Show trading and technical constraints.
Source code in bitepy/simulation.py
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 | |
return_vol_price_pairs(is_last, frequency, volumes)
Retrieve volume-price pairs from the simulation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
is_last
|
bool
|
If True, indicates this is the last iteration of data. |
required |
frequency
|
int
|
The frequency (in seconds) at which price data is retrieved. |
required |
volumes
|
ndarray
|
A 1D numpy array of volumes for which prices are returned. |
required |
Processing Steps
- Validate input parameters for correct format and values.
- Extract volume-price data from the simulation at specified frequency.
- Convert timestamps to UTC format for consistency.
Returns:
| Type | Description |
|---|---|
|
pd.DataFrame: A DataFrame with columns: - current_time: Time of the export (UTC). - delivery_hour: Delivery period time (UTC). - volume: The volume for which the price is exported (MWh). - price_full: The full price (cashflow) for the volume (€). - worst_accepted_price: Market price of the worst matched order (€/MWh). |
Source code in bitepy/simulation.py
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 | |
run(data_path, verbose=True)
Execute the simulation using binary data files.
The files must be named as: orderbook_YYYY-MM-DD.bin.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_path
|
str
|
The directory containing the binary data files. |
required |
verbose
|
bool
|
If True, display progress logs. Default is True. |
True
|
Processing Steps
- Retrieve the list of binary file paths for the simulation period.
- Iterate through each day's data, add the file to the order queue, and run the simulation for that day.
Returns:
| Type | Description |
|---|---|
|
pd.DataFrame: A DataFrame containing the transactions if log_transactions is True, otherwise None. |
Source code in bitepy/simulation.py
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 | |
run_one_day(is_last)
Run the simulation for a single day.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
is_last
|
bool
|
If True, indicates that this is the last iteration of data. |
required |
Processing Steps
- Execute the simulation for the provided day's data.
Source code in bitepy/simulation.py
381 382 383 384 385 386 387 388 389 390 391 | |
submit_limit_orders(df)
Submit a list of limit orders and track their matches without battery optimization.
This method validates input data and queues the limit orders for submission at specified times. The orders will be submitted during the normal simulation run without triggering battery optimization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
A DataFrame containing the limit orders to be submitted. The DataFrame must have the following columns: - transaction_time: The time when the order should be submitted (timezone aware, up to millisecond precision). - price: The price of the limit order (€/MWh). - volume: The volume of the limit order (MWh, positive for buy, negative for sell). - side: The side of the order ('buy' or 'sell'). - delivery_time: The delivery time for the order (timezone aware, required). |
required |
Processing Steps
- Validates input data format and required columns.
- Ensures timezone awareness and proper formatting of timestamps.
- Queues the limit orders for submission during simulation execution.
- Orders are processed without triggering battery optimization.
Returns:
| Name | Type | Description |
|---|---|---|
None |
This method queues orders but does not return match information. After running the simulation, use get_limit_order_matches() to retrieve match details. |
Note
Call this method to queue own limit orders, then run the simulation to process them and collect matches. Use get_limit_order_matches() after simulation to retrieve final match results.
Source code in bitepy/simulation.py
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 | |