Skip to main content

Context

Context()

The running replay, as a strategy handler sees it.

Valid only inside the handler call that received it; afterwards every method raises RuntimeError.

Attributes

Name Type Description
now int The replay’s clock: the end of the period being handled, ns since the epoch (UTC).

Methods

Name Description
cash Cash in the replay’s currency (KRW).
position The net quantity held of a symbol.
target Hold a number of shares of a symbol.

cash

Context.cash()

Cash in the replay’s currency (KRW).

Returns

Name Type Description
float or None The cash, or None if none is recorded; it includes short sale proceeds.

position

Context.position(symbol)

The net quantity held of a symbol.

Parameters

Name Type Description Default
symbol str One of the replay’s symbols. required

Returns

Name Type Description
float Shares held, 0 when none, negative for a short.

target

Context.target(symbol, quantity)

Hold a number of shares of a symbol.

The runner sends the orders the difference from the current position and working orders needs; a target set at a bar’s close fills at the next bar’s open (with fill="decision-close", at the symbol’s last observed close when the target was set; with fill="next-close", at the next bar’s close).

Parameters

Name Type Description Default
symbol str One of the replay’s symbols. required
quantity int The shares to hold; 0 closes the position. Below zero is a short position, refused (quantity_not_sellable) unless the replay was made with shorts=True; the whole sale is refused, so a long position is not closed either. required

Raises

Name Type Description
ValueError If the symbol is not one of the replay’s.