Context API
The Context object provides access to all platform services during execution.
Properties
context.data— Access market and fundamental datacontext.orders— Submit and manage orderscontext.portfolio— Query current positions and balancescontext.tools— Access calculation toolscontext.log— Write execution logs
Returns the current price for a given symbol.
python
get_price(symbol: str) -> floatParameters
| Name | Type | Required | Description |
|---|---|---|---|
| symbol | str | ✓ | The ticker symbol to query (e.g., 'AAPL') |
Returns
float: The current price
Example
python
1
2
3
# Get current price
price = ctx.get_price("AAPL")
ctx.log(f"AAPL is trading at {price}")Returns historical data for the specified symbol and field.
python
history(symbol: str, field: str, lookback: int) -> List[float]Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| symbol | str | ✓ | The ticker symbol to query |
| field | str | ✓ | The price field: 'open', 'high', 'low', 'close', 'volume' |
| lookback | int | ✓ | Number of periods to return |
Returns
List[float]: Historical values ordered oldest to newest
Example
python
1
2
3
# Get last 50 close prices
prices = ctx.history("AAPL", "close", 50)
sma_50 = sum(prices) / len(prices)Returns the current position for a symbol, or None if no position.
python
get_position(symbol: str) -> Position | NoneParameters
| Name | Type | Required | Description |
|---|---|---|---|
| symbol | str | ✓ | The ticker symbol to query |
Returns
Position | None: The position object or None
Example
python
1
2
3
4
# Check if we have a position
pos = ctx.get_position("AAPL")
if pos and pos.quantity > 0:
ctx.log(f"Current AAPL position: {pos.quantity} shares")Writes a log message that will be persisted with the execution.
python
log(message: str, level: str = "info") -> NoneParameters
| Name | Type | Required | Description |
|---|---|---|---|
| message | str | ✓ | The log message to write |
| level | str | — | Log level: 'debug', 'info', 'warning', or 'error' |
Returns
None
Example
python
1
2
3
4
# Log messages
ctx.log("Starting analysis")
ctx.log("High volatility detected", "warning")
ctx.log("Order failed: insufficient funds", "error")