BacktestPosition

class qf_lib.backtesting.portfolio.backtest_position.BacktestPosition(ticker: qf_lib.common.tickers.tickers.Ticker)[source]

Bases: qf_lib.backtesting.portfolio.position.Position

Methods

market_value()

It tells us what is the Market Value of the position.

quantity()

Number of shares or contracts held currently in the portfolio.

total_commission()

Sum of all commissions of transactions.

total_exposure()

It tells us what is the total exposure of the position to the market in currency units

transact_transaction(transaction)

Update the state of a Position by using the Transaction containing information about datetime, price, quantity and commission.

update_price(bid_price, ask_price)

Sets the current price of the security in a way that takes into account the bid-ask spread This is used for market valuation of the open position.

Attributes

total_pnl

Total pnl including all commissions and fees.

unrealised_pnl

Unrealised profit or loss associated with a transaction expressed in currency units

start_time

Time of the position creation.

end_time

Time of the position closing (None in case if the position is still open).

end_time: Optional[datetime]

Time of the position closing (None in case if the position is still open).

abstract market_value() → float[source]

It tells us what is the Market Value of the position.

For cash securities (Equities, Bonds, ETFs, etc)

It represents the value of the position (quantity * price) which is equal to the amount we could receive when we close the position

For margin securities (Futures, Options)

It represents the P&L of the position. So right after we buy a future contract its market_value is 0. It also represents the cash we could receive when we liquidate the position

Different behaviour comes form the fact that when we buy cash security we need to spend cash

(cash is removed from the portfolio)

However, when we buy a margin instrument (for example future contract) our cash is intact,

we only secure the margin which is not deducted from the cash we have.

quantity() → int[source]

Number of shares or contracts held currently in the portfolio. Positive value means this is a Long position Negative value corresponds to a Short position.

start_time: Optional[datetime]

Time of the position creation.

total_commission() → float[source]

Sum of all commissions of transactions.

abstract total_exposure() → float[source]

It tells us what is the total exposure of the position to the market in currency units

For cash securities (Equities, Bonds, ETFs, etc)

It represents the value of the position (quantity * price)

For margin securities (Futures, Options)

It represents Notional of the position (quantity * price * contract size)

property total_pnl

Total pnl including all commissions and fees.

transact_transaction(transaction: qf_lib.backtesting.portfolio.transaction.Transaction) → float[source]

Update the state of a Position by using the Transaction containing information about datetime, price, quantity and commission.

Returns

  • transaction cost

  • For BUY transaction (how much we paid for the transaction including commission (it will be a negative number))

  • For SELL transaction (how much we received for selling shares including commission) – (it will be a positive number)

property unrealised_pnl

Unrealised profit or loss associated with a transaction expressed in currency units Does not include transaction costs and commissions

update_price(bid_price: float, ask_price: float)[source]

Sets the current price of the security in a way that takes into account the bid-ask spread This is used for market valuation of the open position. This method should be called every time we have have a new price