Core API¶
AlphaFlow¶
AlphaFlow
¶
Event-driven backtesting engine for trading strategies.
Source code in alphaflow/__init__.py
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 | |
__init__(*, on_missing_price='raise')
¶
Initialize the AlphaFlow backtest engine.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
on_missing_price
|
str
|
Behavior when price data is missing. Options are: - "raise": Raise an error (default) - "warn": Log a warning and return zero price - "ignore": Silently return zero price |
'raise'
|
Source code in alphaflow/__init__.py
set_benchmark(symbol)
¶
Set the benchmark symbol for performance comparison.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
The ticker symbol to use as a benchmark (e.g., "SPY"). |
required |
add_equity(symbol)
¶
Add an equity symbol to the trading universe.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
The ticker symbol to add (e.g., "AAPL"). |
required |
set_data_feed(data_feed)
¶
Set the data feed for retrieving market data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_feed
|
DataFeed
|
A DataFeed instance that will provide market data. |
required |
Source code in alphaflow/__init__.py
add_strategy(strategy)
¶
Add a trading strategy to the backtest.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
strategy
|
Strategy
|
A Strategy instance that will generate trading signals. |
required |
Source code in alphaflow/__init__.py
add_analyzer(analyzer)
¶
Add an analyzer for performance metrics and visualization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
analyzer
|
Analyzer
|
An Analyzer instance for computing metrics and generating reports. |
required |
Source code in alphaflow/__init__.py
set_broker(broker)
¶
Set the broker for order execution simulation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
broker
|
Broker
|
A Broker instance that will simulate order execution. |
required |
Source code in alphaflow/__init__.py
set_cash(cash)
¶
Set the initial cash balance for the portfolio.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cash
|
float
|
The initial cash amount in the portfolio currency. |
required |
set_data_start_timestamp(timestamp)
¶
Set the start timestamp for loading data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timestamp
|
datetime | str
|
Start datetime or ISO format string. Data will be loaded from this point. |
required |
Source code in alphaflow/__init__.py
set_backtest_start_timestamp(timestamp)
¶
Set the start timestamp for the backtest period.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timestamp
|
datetime | str
|
Start datetime or ISO format string. Strategies will begin trading from this point. |
required |
Source code in alphaflow/__init__.py
set_backtest_end_timestamp(timestamp)
¶
Set the end timestamp for the backtest period.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timestamp
|
datetime | str
|
End datetime or ISO format string. Backtest will stop at this point. |
required |
Source code in alphaflow/__init__.py
get_timestamps()
¶
Return all unique timestamps from loaded market data, sorted chronologically.
Returns:
| Type | Description |
|---|---|
list[datetime]
|
Sorted list of datetime objects representing all data points in the backtest. |
Source code in alphaflow/__init__.py
run(is_backtest=True)
¶
Run the backtest simulation.
Load all market data for symbols in the universe, publishes events chronologically through the EventBus, and runs all analyzers after completion.
Events are processed through a priority queue to ensure correct ordering: 1. All MarketDataEvents are loaded and added to the queue 2. The queue processes events in timestamp order 3. When strategies generate OrderEvents, they're added to the queue 4. When brokers generate FillEvents, they're added to the queue 5. All events at timestamp T are fully processed before moving to T+1
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
is_backtest
|
bool
|
Whether to run in backtest mode. Live trading not yet implemented. |
True
|
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If is_backtest is False (live trading not supported). |
ValueError
|
If data_feed is not set before running. |
Source code in alphaflow/__init__.py
get_price(symbol, timestamp)
¶
Get the closing price for a symbol at or after a specific timestamp.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
The ticker symbol. |
required |
timestamp
|
datetime
|
The timestamp to look up the price for. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The closing price at or after the given timestamp. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no price data exists after the timestamp. |
Source code in alphaflow/__init__.py
Strategy¶
Strategy
¶
Bases: Subscriber
Defines the interface for strategies.
Source code in alphaflow/__init__.py
Broker¶
Broker
¶
Bases: Subscriber
Defines the interface for brokers.
Source code in alphaflow/__init__.py
DataFeed¶
DataFeed
¶
Defines the interface for data feeds.
Source code in alphaflow/__init__.py
Portfolio¶
Portfolio
¶
Bases: Subscriber
Manages portfolio state including cash, positions, and performance calculations.
Source code in alphaflow/__init__.py
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 | |
__init__(alpha_flow)
¶
Initialize the portfolio.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
alpha_flow
|
AlphaFlow
|
The AlphaFlow backtest engine instance. |
required |
Source code in alphaflow/__init__.py
topic_subscriptions()
¶
set_cash(cash)
¶
get_cash()
¶
get_position(symbol)
¶
Get the current position quantity for a symbol.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
The ticker symbol. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The number of shares held (0 if no position). |
Source code in alphaflow/__init__.py
update_cash(amount)
¶
Update the cash balance by adding an amount.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
amount
|
float
|
The amount to add (can be negative). |
required |
update_position(symbol, qty)
¶
Update the position quantity for a symbol.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
The ticker symbol. |
required |
qty
|
float
|
The quantity to add (can be negative for sells). |
required |
Source code in alphaflow/__init__.py
get_position_value(symbol, timestamp)
¶
Get the market value of a position at a specific timestamp.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
The ticker symbol. |
required |
timestamp
|
datetime
|
The timestamp for price lookup. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The position value (shares * price). |
Source code in alphaflow/__init__.py
get_positions_value(timestamp)
¶
Get the total market value of all positions at a specific timestamp.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timestamp
|
datetime
|
The timestamp for price lookup. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The total value of all positions. |
Source code in alphaflow/__init__.py
get_portfolio_value(timestamp)
¶
Get the total portfolio value (cash + positions) at a specific timestamp.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timestamp
|
datetime
|
The timestamp for price lookup. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The total portfolio value. |
Source code in alphaflow/__init__.py
get_buying_power(margin, timestamp)
¶
Calculate available buying power with margin.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
margin
|
float
|
The margin multiplier (e.g., 2.0 for 2x margin). |
required |
timestamp
|
datetime
|
The timestamp for price lookup. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The available buying power. |
Source code in alphaflow/__init__.py
get_benchmark_values()
¶
Get benchmark prices for all timestamps in the backtest.
Returns:
| Type | Description |
|---|---|
dict[datetime, float]
|
Dictionary mapping timestamps to benchmark prices. |
Source code in alphaflow/__init__.py
read_event(event)
¶
Read and process the event.
Source code in alphaflow/__init__.py
Analyzer¶
Analyzer
¶
Bases: Subscriber
Defines the interface for analyzers.