backtrader.position module¶
Position Module - Position tracking and management.
This module provides the Position class for tracking the size and price of trading positions. It maintains position state including opening and closing amounts.
- Classes:
Position: Tracks position size, price, and related attributes.
示例
Getting position from broker: >>> position = broker.getposition(data) >>> print(f"Size: {position.size}, Price: {position.price}")
- class backtrader.position.Position[源代码]¶
基类:
objectKeeps and updates the size and price of a position.
The Position object has no relationship to any specific asset. It only keeps size and price information.
- size¶
Current position size (positive for long, negative for short).
- price¶
Current price of the position.
- price_orig¶
Original price when position was opened.
- upopened¶
Amount of position opened in last update.
- upclosed¶
Amount of position closed in last update.
- adjbase¶
Adjustment base for position calculations.
示例
>>> position = Position(size=100, price=50.0) >>> print(len(position)) # Returns True if size != 0
- __init__(size=0, price=0.0)[源代码]¶
Initialize a Position instance.
- 参数:
size -- Initial position size (default: 0).
price -- Initial price for the position (default: 0.0).
- fix(size, price)[源代码]¶
Fix the position to a specific size and price.
- 参数:
size -- The new position size.
price -- The new position price.
- 返回:
True if size equals oldsize, False otherwise.
- 返回类型:
- set(size, price)[源代码]¶
Set position's size and price, calculating opening and closing amounts.
- 参数:
size -- The new position size.
price -- The new position price.
- 返回:
(size, price, upopened, upclosed) calculated values.
- 返回类型:
- clone()[源代码]¶
Create a copy of the current position.
- 返回:
A new Position instance with the same size and price.
- 返回类型:
- pseudoupdate(size, price)[源代码]¶
Create a pseudo-update by cloning and updating.
- 参数:
size -- The size to add to the position.
price -- The price for the update.
- 返回:
A new Position instance after update.
- 返回类型:
- update(size, price, dt=None)[源代码]¶
Updates the current position and returns the updated size, price and units used to open/close a position # Update current position and return updated size, price and position size needed to open/close
- 参数:
size (# Amount to update position) -- amount to update the position's size if size < 0: A sell operation has taken place if size > 0: A buy operation has taken place
size
0 (if size >)
executed (a buy operation will be)
0
executed
price (float) -- Must always be positive to ensure consistency
Price (#)
consistency (must always be positive to maintain)
dt (datetime.datetime) -- record datetime update (datetime.datetime)
- 返回:
- A tuple (non-named) contaning
- size - new position size
Simply the sum of the existing size plus the "size" argument
- price - new position price
If a position is increased, the new average price will be returned If a position is reduced, the price of the remaining size does not change If a position is closed, the price is nullified If a position is reversed, the price is the price given as argument
- opened - amount of contracts from argument "size" that were used
to open/increase a position. A position can be opened from 0 or can be a reversal. If a reversal is performed, then opened is less than "size", because part of "size" will have been used to close the existing position
- closed - the amount of units from arguments "size" that were used to
close/reduce a position
Both opened and closed carry the same sign as the "size" argument because they refer to a part of the "size" argument # Result will return a tuple containing the following data: # size represents new position size, simply existing position size plus new position increment # price represents new position price, returns different price based on different position # opened represents position size to be newly opened # closed represents position size to be closed