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.
Example
Getting position from broker: >>> position = broker.getposition(data) >>> print(f”Size: {position.size}, Price: {position.price}”)
- class backtrader.position.Position[source]¶
Bases:
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.
Example
>>> position = Position(size=100, price=50.0) >>> print(len(position)) # Returns True if size != 0
- __init__(size=0, price=0.0)[source]¶
Initialize a Position instance.
- Parameters:
size – Initial position size (default: 0).
price – Initial price for the position (default: 0.0).
- fix(size, price)[source]¶
Fix the position to a specific size and price.
- Parameters:
size – The new position size.
price – The new position price.
- Returns:
True if size equals oldsize, False otherwise.
- Return type:
- set(size, price)[source]¶
Set position’s size and price, calculating opening and closing amounts.
- Parameters:
size – The new position size.
price – The new position price.
- Returns:
(size, price, upopened, upclosed) calculated values.
- Return type:
- clone()[source]¶
Create a copy of the current position.
- Returns:
A new Position instance with the same size and price.
- Return type:
- pseudoupdate(size, price)[source]¶
Create a pseudo-update by cloning and updating.
- Parameters:
size – The size to add to the position.
price – The price for the update.
- Returns:
A new Position instance after update.
- Return type:
- update(size, price, dt=None)[source]¶
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
- Parameters:
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)
- Returns:
- 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