Backtesting - Functions
Scoring - Functions
Base.:*
— MethodBase.:*(ss1::SignalScores{T}, ss2::SignalScores{T}) where T<:SignalType
Multiplies two SignalScores
objects of the same SignalType
by applying the *
operation element-wise to the scores.
Base.:+
— MethodBase.:+(ss1::SignalScores{T}, ss2::SignalScores{T}) where T<:SignalType
Adds two SignalScores
objects of the same SignalType
by applying the +
operation element-wise to the scores.
Base.:-
— MethodBase.:-(ss1::SignalScores{T}, ss2::SignalScores{T}) where T<:SignalType
Subtracts two SignalScores
objects of the same SignalType
by applying the -
operation element-wise to the scores.
TFire.BuyScores
— MethodBuyScores(col::Collection, score::Float64)
Assigns a buy signal score to each asset in the provided Collection
. The score is applied equally to all assets.
Returns a SignalScores{BuySignal}
object.
TFire.BuyScores
— MethodBuyScores(col::Collection, dvs::Vector, score_func::Function)
Generates SignalScores for a collection based on data views and a scoring function, where the signal type is BuySignal.
Args: col: Collection to score dvs: Vector of data views score_func: Function that takes data view values and returns a score (Float64)
Returns: SignalScores{BuySignal}
TFire.BuyScores
— MethodBuyScores(pad::PortfolioAtDate, score::Float64)
Assigns a buy signal score to each asset in the provided PortfolioAtDate
. The score is applied equally to all assets.
Returns a SignalScores{BuySignal}
object.
TFire.CloseScores
— MethodCloseScores(col::Collection, score::Float64)
Assigns a close signal score to each asset in the provided Collection
. The score is applied equally to all assets.
Returns a SignalScores{CloseSignal}
object.
TFire.CloseScores
— MethodCloseScores(col::Collection, dvs::Vector, score_func::Function)
Generates SignalScores for a collection based on data views and a scoring function, where the signal type is CloseSignal.
Args: col: Collection to score dvs: Vector of data views score_func: Function that takes data view values and returns a score (Float64)
Returns: SignalScores{CloseSignal}
TFire.CloseScores
— MethodCloseScores(pad::PortfolioAtDate, score::Float64)
Assigns a close signal score to each asset in the provided PortfolioAtDate
. The score is applied equally to all assets.
Returns a SignalScores{CloseSignal}
object.
TFire.SellScores
— MethodSellScores(col::Collection, score::Float64)
Assigns a sell signal score to each asset in the provided Collection
. The score is applied equally to all assets.
Returns a SignalScores{SellSignal}
object.
TFire.SellScores
— MethodSellScores(col::Collection, dvs::Vector, score_func::Function)
Generates SignalScores for a collection based on data views and a scoring function, where the signal type is SellSignal.
Args: col: Collection to score dvs: Vector of data views score_func: Function that takes data view values and returns a score (Float64)
Returns: SignalScores{SellSignal}
TFire.SellScores
— MethodSellScores(pad::PortfolioAtDate, score::Float64)
Assigns a sell signal score to each asset in the provided PortfolioAtDate
. The score is applied equally to all assets.
Returns a SignalScores{SellSignal}
object.
TFire._add_score!
— Method_add_score!(signal_scores::SignalScores{T}, date::DateTime, ts::TimeSeries, score::Float64) where T<:SignalType
Adds a score to the specified SignalScores
object for the given date
and TimeSeries
. The score must be non-negative. If there is already a score for the given date
and TimeSeries
, the new score is added to the existing score.
TFire.close_at_end_of_day
— Methodclose_at_end_of_day(col::Collection{A}) where A <: AssetsDisc
Returns CloseScores at end of the day for all days in the collection.
TFire.extend_scores
— Methodextend_scores(signal_scores::SignalScores{T}, td::TradingDates; constant_ticks::Integer=0,
linear_ticks::Integer=0, trailing_stoploss::Bool=false, trailing_stoploss_val::Float64=0.05) where T <: Union{BuySignal, SellSignal}
Extend signal scores over trading dates. Applies constant scores for constant_ticks
and linearly decreasing scores for linear_ticks
. Supports buy and sell signals. Returns PositionScores.
TFire.signal_from_collection
— Methodsignal_from_collection(::Type{T}, col::Collection, score::Float64) where T<:SignalType
Assigns a signal score to each asset in the provided Collection
. The score is applied equally to all assets.
Returns a SignalScores{T}
object.
TFire.signal_from_function
— Methodsignal_from_function(col::Collection, dvs::Vector, score_func::Function; signal_type::Type{T}=BuySignal) where T<:SignalType
Generates SignalScores
for a Collection
based on a vector of DataViews
and a scoring function. The signal type is specified by the signal_type
parameter.
Args: col: The Collection
to score. dvs: A vector of DataViews
to use for scoring. scorefunc: A function that takes the values of the DataViews
and returns a score (Float64). signaltype: The type of signal to generate (default is BuySignal
).
Returns: A SignalScores{T}
object containing the calculated scores.
TFire.signal_from_portfolio
— Methodsignal_from_portfolio(::Type{T}, pad::PortfolioAtDate, score::Float64) where T<:SignalType
Assigns a signal score to each asset in the provided PortfolioAtDate
. The score is applied equally to all assets.
Args: T: The type of signal (BuySignal, SellSignal, CloseSignal) pad: The PortfolioAtDate to assign the signal scores to score: The score to assign to each asset
Returns: A SignalScores{T}
object containing the assigned scores.
TFire.signal_scores_shift
— Methodsignal_scores_shift(ss::SignalScores{S}, signal_type::Type{T}, score::Float64,
shift_ticks=1; resolution="1d", threshold=0.00001) where {S<:SignalType, T<:SignalType}
Creates a new SignalScores object by shifting existing signals in time.
Arguments
ss::SignalScores{S}
: Source SignalScores object to shift signals fromsignal_type::Type{T}
: Type of signal to generate (e.g., BuySignal, SellSignal, CloseSignal)score::Float64
: Value to assign to each shifted signalshift_ticks::Integer=1
: Number of ticks to shift the signals byresolution::String="1d"
: Resolution for shifting signals ("1d" for days or "1m" for minutes)threshold::Float64=0.00001
: Minimum absolute score required in source signals to generate shifted signals
Returns
SignalScores{T}
: New SignalScores object of the specified type
Example
# Shift signals to generate buy signals 1 day later
buy_scores = signal_scores_shift(existing_scores, BuySignal, 1.0, shift_ticks=1)
# Shift signals to generate sell signals 5 minutes later
sell_scores = signal_scores_shift(existing_scores, SellSignal, 1.0,
shift_ticks=5, resolution="1m")
TFire.CashScore
— MethodCashScore(ss::SignalScores{T}, score::Float64) where T<:SignalType
Returns CashScore with score score at all the datetimes in
ss`.
TFire.PositionScores
— MethodPositionScores(buy_scores::SignalScores{B}, sell_scores::SignalScores{S}, close_scores::SignalScores{C};
trading_dates::Union{TradingDates,Nothing}=nothing, cash_scores::CashScore=CashScore()) where {B <: BuySignal, S <: SellSignal, C<: CloseSignal}
Construct a PositionScores object from buy, sell, and close signal scores. Combines and processes scores across specified trading dates, applying buy (positive), sell (negative), and close (reducing) signals. Handles cash scores and filters out negligible positions.
TFire.add_up_to
— Functionadd_up_to(ps::PositionScores, target_sum::Float64=1.0)
Creates a new normalized copy of the PositionScores object where for each datetime, the sum of all scores (including cash) equals the target_sum.
Args: ps: PositionScores object to normalize target_sum: The desired sum of scores at each datetime (default: 1.0)
Returns: A new normalized PositionScores object
TFire.add_up_to!
— Functionadd_up_to!(ps::PositionScores, target_sum::Float64=1.0)
Adjusts the cash scores in a PositionScores object so that for each datetime, the sum of all scores (including cash) equals the targetsum. If there are no asset scores at a particular datetime, sets the cash score to targetsum. The asset scores remain unchanged.
Args: ps: PositionScores object to modify target_sum: The desired sum of scores at each datetime (default: 1.0)
Returns: The modified PositionScores object
TFire.buy_and_hold
— Methodbuy_and_hold(score_buy::SignalScores{BuySignal}, port::PortfolioAtDate, ticks::Integer)
Computes the buy-and-hold portfolio given a buy signal and a portfolio at a specific date.
Args: score_buy::SignalScores{BuySignal}: The buy signal scores. port::PortfolioAtDate: The portfolio at a specific date. ticks::Integer: The number of ticks to compute the buy-and-hold portfolio for.
Returns: PositionScores: The buy-and-hold portfolio.
TFire.buy_and_hold
— Methodbuy_and_hold(score::SignalScores{BuySignal}, td::TradingDates, ticks::Int; cash_signal::Float64=0.0)
Computes the buy-and-hold portfolio given a buy signal and a set of trading dates.
Args: score (SignalScores{BuySignal}): The buy signal scores. td (TradingDates): The set of trading dates to compute the buy-and-hold portfolio for. ticks (Int): The number of ticks to compute the buy-and-hold portfolio for. cash_signal (Float64): The cash signal to use for each date (default is 0.0).
Returns: PositionScores: The buy-and-hold portfolio.
TFire.fill_in_with_cash_score_where_empty!
— Methodfill_in_with_cash_score_where_empty!(ps::PositionScores)
Fills in cash score of 1.0 whenever the total score of holdings is approx 0
TFire.get_daily_scores
— Methodget_daily_scores(position_scores::PositionScores)
Computes the daily scores from the provided PositionScores
object. The daily scores are returned as a Dict{Date, PositionScores}
, where the keys are the dates and the values are PositionScores
objects containing the scores for each time series on that date.
TFire.limit_maximum_allocation!
— Functionlimit_maximum_allocation!(position_scores::PositionScores, max_share::Float64=1.0)::PositionScores
Limits the maximum allocation of each position in the PositionScores
to a specified percentage of the total position value.
Args: positionscores (PositionScores): The PositionScores
object to limit the maximum allocation for. maxshare (float): The maximum percentage of the total position value that any single position can have. Defaults to 1.0 (100%).
Returns: PositionScores: A new PositionScores
object with the maximum allocation limited.
TFire.plot_scores
— Methodplot_scores(position_scores::PositionScores, ticker::String;
start_date::Union{DateTime,Nothing}=nothing,
end_date::Union{DateTime,Nothing}=nothing)
Plots the position scores and price for a given ticker over a specified date range.
Args: positionscores::PositionScores: The position scores to plot. ticker::String: The ticker to plot. startdate::Union{DateTime,Nothing}: The start date for the plot. If not provided, the earliest date in the data is used. end_date::Union{DateTime,Nothing}: The end date for the plot. If not provided, the latest date in the data is used.
The plot will display the position scores as colored markers, with the score value displayed on top of each marker. The price data for the ticker will be plotted as a line chart on a logarithmic y-axis.
TFire.print_table
— Methodprint_table(score::Union{PositionScores, SignalScores}; first_date=nothing, last_date=nothing)
Prints a formatted HTML table displaying the scores for a given set of tickers and dates.
Args: score::Union{PositionScores, SignalScores}: The score data to display in the table. firstdate::Union{Nothing, DateTime}: The first date to include in the table. If not provided, the earliest date in the score data is used. lastdate::Union{Nothing, DateTime}: The last date to include in the table. If not provided, the latest date in the score data is used.
The table will display the scores for each ticker on each date, with the scores colored based on their magnitude. For PositionScores, the cash score will also be displayed.
Portfolio - Functions
TFire.TradingDates
— MethodTradingDates(pf::PortfolioAtDate)
Returns a TradingDates
object containing all the unique trading dates across the holdings in the given PortfolioAtDate
.
TFire.compound_returns
— Methodcompound_returns(pfh::PortfolioHistory)
Returns a vector of the compound returns for each step in the given PortfolioHistory
. The compound returns for step ind
is calculated by dividing the total value for step ind
by the initial total value.
TFire.get_dates
— Methodget_dates(phd::PortfolioHistoryDaily)
Returns a vector of the dates for eachPortfolioHistory
within the given PortfolioHistoryDaily
.
TFire.get_dates
— Methodget_dates(pfh::PortfolioHistory)
Returns a vector of the dates for each PortfolioAtDate
in the given PortfolioHistory
.
TFire.get_number_of_holdings
— Methodget_number_of_holdings(pfh::PortfolioHistory, ind::Integer; cutoff=0.0000001)
Returns the number of holdings in the PortfolioAtDate
at the given index ind
in the PortfolioHistory
, where the absolute value of the quantity is greater than the given cutoff
value.
TFire.get_number_of_holdings
— Methodget_number_of_holdings(pfh::PortfolioHistory; cutoff=0.0000001)
Returns a vector of the number of holdings in each PortfolioAtDate
in the PortfolioHistory
, where the absolute value of the quantity is greater than the given cutoff
value.
TFire.get_price
— Methodget_price(holding::HoldingAtDate)
Returns the price of the given holding at the given date.
TFire.get_prices
— Methodget_prices(pf::PortfolioAtDate)
Retrieves the prices of all holdings in the given PortfolioAtDate.
Returns
Am array containing:
- Prices of each holding
- Three additional elements set to 1.0 (representing cash positions)
The length of the returned array is the number of holdings plus 3.
TFire.get_quantities
— Methodget_quantities(pf::PortfolioAtDate)
Retrieves the quantities of all holdings and cash positions in the given PortfolioAtDate.
Returns
Am array containing:
- Quantities of each holding
- Cash amount
- Short sell cash amount
- Margin cash amount
The length of the returned array is the number of holdings plus 3 (for the cash positions).
TFire.get_quantities
— Methodget_quantities(pfh::PortfolioHistory)
Returns a vector of the quantities for each PortfolioAtDate
in the given PortfolioHistory
.
TFire.get_quantity
— Methodget_quantity(holding::HoldingAtDate)
Returns the quantity of the given holding at the given date.
TFire.get_ticker
— Methodget_ticker(holding::HoldingAtDate)
Returns the ticker of the given holding at the given date.
TFire.get_tickers
— Methodget_tickers(pf::PortfolioAtDate)
Returns a list of tickers for all holdings in the given PortfolioAtDate.
TFire.get_total_value
— Methodget_total_value(pf::PortfolioAtDate)
Returns the total value of the portfolio.
TFire.get_total_values
— Methodget_total_values(phd::PortfolioHistoryDaily)
Returns a vector of the total values for the last PortfolioAtDate
in each PortfolioHistory
within the given PortfolioHistoryDaily
.
TFire.get_total_values
— Methodget_total_values(pfh::PortfolioHistory)
Returns a vector of the total value for each PortfolioAtDate
in the given PortfolioHistory
.
TFire.get_value
— Methodget_value(holding::HoldingAtDate)
Returns the value (quantity * price) of the given holding at the given date.
TFire.get_values
— Methodget_values(pf::PortfolioAtDate)
Calculates and retrieves the values of all holdings and cash positions in the given PortfolioAtDate.
Returns
An array containing:
- Values of each holding
- Cash value
- Short sell cash value
- Margin cash value
The length of the returned array is the number of holdings plus 3 (for the cash positions).
TFire.initialize_portfolio
— Methodinitialize_portfolio(collection::Collection{AssetsCon{T}}, date::D, cash::Float64=1.;verbose=true) where {T <: Assets, D <: Union{Date,DateTime}}
Initializes a portfolio at a given date, usually the first trading date. Makes all assets in the collection available as holdings in the portfolio.
TFire.returns
— Methodreturns(pfh::Union{PortfolioHistory, PortfolioHistoryDaily})
Returns a vector of the step-by-step returns for the given PortfolioHistory
or PortfolioHistoryDaily
. The return for each step is calculated as the change in total value divided by the previous total value.
TFire.returns_log
— Methodreturns_log(pfh::Union{PortfolioHistory, PortfolioHistoryDaily})
Returns a vector of the step-by-step log returns for the given PortfolioHistory
or PortfolioHistoryDaily
. The log return for each step is calculated as the logarithm of the ratio of the current total value to the previous total value.
TFire.step_return
— Methodstep_return(pfh::PortfolioHistory, ind)
Returns the change in total value between the portfolio at step ind
and step ind
- 1.
Portfolio Propagation - Functions
TFire.PortfolioHistory
— MethodPortfolioHistory(ps::PositionScores; start_date::D=Date(0000, 01, 01), to_date::D=Date(0000, 01, 01),
start_cash=1.0, fee_type=:free, prop_fee=0.0, fixed_fee=0, name="") where {D<:Union{Date,DateTime}}
start_cash=1.0, fee_type=:free, prop_fee=0.0, fixed_fee=0, name="") where {D<:Union{Date,DateTime}}
Construct a portfolio history by propagating a ScoreHold through time.
# Arguments
- `ps::PositionScores`: The PositionScores to propagate over time.
- `start_date=DateTime(0000,01,01)`: The start date for the PortfolioHistory.
- `to_date=D(0000,01,01)`: The end date for the PortfolioHistory.
- `start_cash=1.`: The starting cash amount. Default is 1.0.
- `fee_type=:free`: The fee model to use. Can be :free, :fixed, or :prop. Default is :free.
- `prop_fee=0.`: The proportional fee amount if using :prop fee type.
- `fixed_fee=0`: The fixed fee amount per trade if using :fixed fee type.
- `name=""`: The name of the portfolio. Default is a random string.
# Returns
- `PortfolioHistory`: The full portfolio history over time
TFire.propagate_portfolio
— Methodpropagate_portfolio(pf::PortfolioAtDate, scoring::ScoreHold; to_date::D=Date(0000,01,01), fee_type=:free, prop_fee=0.,
fixed_fee=0., keep_weighting_if_unchanged=true, name="") where D <: Union{Date, DateTime}
Propagate a portfolio forward in time by reweighting and stepping forward at each date.
# Arguments
- `pf::PortfolioAtDate`: The initial portfolio
- `scoring::ScoreHold`: The scores to use for reweighting
- `to_date`: The date to propagate the portfolio to. Default is the max date of the scoring.
- `fee_type`: The fee model to use. Can be :free, :fixed, :proportional.
- `prop_fee`: The proportional fee percentage if using :proportional fee model.
- `fixed_fee`: The fixed fee amount per trade if using :fixed fee model.
- `keep_weighting_if_unchanged`: If true, keep the portfolio weighting when no change from scores.
- `name`: Name given to the propagated portfolio history.
# Returns
- `PortfolioHistory`: The propagated portfolio history.
Portfolio Analysis - Functions
TFire.maximum_drawdown
— Methodmaximum_drawdown(pfh::Union{PortfolioHistory, PortfolioHistoryDaily})
Calculates the maximum drawdown for the given PortfolioHistory
or PortfolioHistoryDaily
. The maximum drawdown is the largest peak-to-trough decline in the portfolio's total value. It is calculated as the maximum percentage decline from the highest portfolio value to the lowest portfolio value.
TFire.plot_portfolio
— Methodplot_portfolio(pfh::PortfolioHistory, pfh2::PortfolioHistory)
Plots the portfolio history for the given PortfolioHistory
instances.
Arguments:
pfh::PortfolioHistory
: The first portfolio history to plot.pfh2::PortfolioHistory
: The second portfolio history to plot.
Returns:
- A plot of the portfolio histories.
TFire.plot_portfolio
— Methodplot_portfolio(phd::PortfolioHistoryDaily, phd2::PortfolioHistoryDaily)
Plots the portfolio history for the given PortfolioHistoryDaily
instances.
Arguments:
phd::PortfolioHistoryDaily
: The first portfolio history to plot.phd2::PortfolioHistoryDaily
: The second portfolio history to plot.
Returns:
- A plot of the portfolio histories.
TFire.plot_portfolio
— Methodplot_portfolio(phd::PortfolioHistoryDaily)
Plots the portfolio history for the given PortfolioHistoryDaily
instance.
Arguments:
phd::PortfolioHistoryDaily
: The portfolio history to plot.
Returns:
- A plot of the portfolio history.
TFire.plot_portfolio
— Methodplot_portfolio(pfh::PortfolioHistory; plot_proportions=true)
Plots the portfolio history for the given PortfolioHistory
object.
Args: pfh::PortfolioHistory: The portfolio history to plot. plot_proportions::Bool: Whether to plot the proportions of the portfolio in addition to the total value.
Returns: Nothing, but displays the plot in the default browser.
TFire.plot_portfolio
— Methodplot_portfolio(phd_vect::Vector{PortfolioHistoryDaily})
Plots the portfolio histories for the given vector of PortfolioHistoryDaily
instances.
Arguments:
phd_vect::Vector{PortfolioHistoryDaily}
: The vector of portfolio histories to plot.
Returns:
- A plot of the portfolio histories.
TFire.plot_portfolio
— Methodplot_portfolio(pfh_vect::Vector{PortfolioHistory}; plot_proportions=false)
Plots the portfolio history for the given vector of PortfolioHistory
instances.
Arguments:
pfh_vect::Vector{PortfolioHistory}
: The vector of portfolio histories to plot.plot_proportions::Bool=false
: Whether to plot the portfolio proportions in addition to the total portfolio value.
Returns:
- A plot of the portfolio histories.
TFire.print_portfolio_table
— MethodGenerates an HTML table displaying the portfolio history for a given `PortfolioHistoryDaily` object.
Args:
phd (PortfolioHistoryDaily): The portfolio history daily object to display.
cutoff (float): The minimum holding percentage to include in the table (default is 0.000001).
reweighted (bool): Whether to display the portfolio after reweighting (default is False).
Returns:
None
TFire.print_portfolio_table
— MethodGenerates an HTML table displaying the portfolio history for a given `PortfolioHistory` object.
Args:
pf_history (PortfolioHistory): The portfolio history object to display.
cutoff (float): The minimum holding percentage to include in the table (default is 0.000001).
reweighted (bool): Whether to display the portfolio after reweighting (default is False).
first_date (Union{Nothing, Date}): The first date to include in the table (default is the first date in the portfolio history).
last_date (Union{Nothing, Date}): The last date to include in the table (default is the last date in the portfolio history).
Returns:
None
TFire.reference_portfolio
— Methodreference_portfolio(phd::PortfolioHistoryDaily)::PortfolioHistoryDaily
Generates a reference portfolio history based on the provided PortfolioHistoryDaily
. The reference portfolio is created by applying a simple buy-and-hold strategy to the first portfolio in each PortfolioHistory
within the PortfolioHistoryDaily
. The resulting portfolio histories are then propagated to the end date of the original PortfolioHistoryDaily
.
TFire.reference_portfolio
— Methodreference_portfolio(pfh::PortfolioHistory)::PortfolioHistory
Generates a reference portfolio history based on the provided PortfolioHistory
. The reference portfolio is created by applying a simple buy-and-hold strategy to the first portfolio in the PortfolioHistory
. The resulting portfolio history is then propagated to the end date of the original PortfolioHistory
.
TFire.sharpe_ratio
— Methodsharpe_ratio(pf::Union{PortfolioHistory, PortfolioHistoryDaily})
Calculates the Sharpe ratio for the given PortfolioHistory
or PortfolioHistoryDaily
. The Sharpe ratio is a measure of the risk-adjusted return of an investment. Here calculated as:
sqrt(252)*mean(pf_returns)/std(pf_returns)
where pf_returns is the vector of returns for the portfolio.