Position Timing

Calculate the timing of the positions of the portfolio as López de Prado describes it.

How often each position was flattened (sold to zero) or flipped (from long to short or back), the number of bets these end, and the average holding period of its units. A bet is a position held from the moment it opens until it is flattened or flipped, so the number of bets counts the independent positions taken in a ticker; a position still held ends a bet on the latest date. The average holding period weighs the time every unit was held by the units: each purchase moves the average entry time of the position towards its date, and each sale closes units held since that average entry time.

  • Average Entry Time_t = (Average Entry Time_t-1 × \(\vert p_{t} - 1 \vert + t \times \vert \Delta p_{t} \vert\)) / |p_t| when the position grows
  • Average Holding Period = \(\Sigma\) (t − Average Entry Time) × |units closed at \(t \vert / \sum\) |units closed|

Also known as: bet frequency, holding period, flattening and flips.

Example

Install the Finance Toolkit with pip install financetoolkit -U, then call get_position_timing:

from financetoolkit import Portfolio

portfolio = Portfolio(example=True, api_key="FINANCIAL_MODELING_PREP_KEY")

portfolio.get_position_timing()

Which returns:

Parameters

get_position_timing accepts the following parameters.

ParameterTypeDescriptionExample
roundingint | NoneThe number of decimals to round the results to. Defaults to None, which uses the Portfolio’s rounding.2
show_columnsstr | list[str] | NoneThe columns to keep, a name or a list of names, matched on every column level, or the rows when no column has those names (such as the line items of a financial statement). Names that do not exist are logged, and when none exist the result is returned in full. Defaults to None, which keeps every column.None, which keeps every column

References

  • Paper López de Prado, M. (2018). “Advances in Financial Machine Learning.” Wiley, section 14.3 (Snippets 14.1 and 14.2).
Share