BacktestPosition#
- class qf_lib.backtesting.portfolio.backtest_position.BacktestPosition(ticker: Ticker)[source]#
Bases:
Position- end_time: datetime | None#
Time of the position closing (None in case if the position is still open).
- abstractmethod 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.
- abstractmethod 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: 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)