Context API

The Context object provides access to all platform services during execution.

Properties

  • context.data — Access market and fundamental data
  • context.orders — Submit and manage orders
  • context.portfolio — Query current positions and balances
  • context.tools — Access calculation tools
  • context.log — Write execution logs

get_price

Returns the current price for a given symbol.

python
get_price(symbol: str) -> float

Parameters

NameTypeRequiredDescription
symbolstrThe 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}")

history

Returns historical data for the specified symbol and field.

python
history(symbol: str, field: str, lookback: int) -> List[float]

Parameters

NameTypeRequiredDescription
symbolstrThe ticker symbol to query
fieldstrThe price field: 'open', 'high', 'low', 'close', 'volume'
lookbackintNumber 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)

get_position

Returns the current position for a symbol, or None if no position.

python
get_position(symbol: str) -> Position | None

Parameters

NameTypeRequiredDescription
symbolstrThe 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")

log

Writes a log message that will be persisted with the execution.

python
log(message: str, level: str = "info") -> None

Parameters

NameTypeRequiredDescription
messagestrThe log message to write
levelstrLog 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")