API

ffn Package

core Module

class ffn.core.GroupStats(*prices, annualization_factor=None)[source]

Bases: dict

GroupStats enables one to compare multiple series side by side. It is a wrapper around a dict of {price.name: PerformanceStats} and provides many convenience methods.

The order of the series passed in will be preserved. Individual PerformanceStats objects can be accessed via index position or name via the [] accessor.

Each series’ stats are computed from that series’ own available observations, matching what PerformanceStats returns for the series on its own. Cross-sectional views (prices, correlations) use the merged calendar, keeping only dates where every series has data.

Parameters:
  • prices (*) – Multiple price series to be compared.

  • annualization_factor (*) – Annualization factor used for each series.

\* stats

Dataframe containing stats for each series provided. Stats in rows, series in columns.

Type:

DataFrame

\* lookback_returns

Returns for diffrent lookback periods (1m, 3m, 6m, ytd…) Period in rows, series in columns.

Type:

DataFrame

\* prices

The merged and rebased prices.

Type:

DataFrame

display()[source]

Display summary stats table.

display_lookback_returns()[source]

Displays the current lookback returns for each series.

plot(freq=None, figsize=(15, 5), title=None, logy=False, **kwargs)[source]

Helper function for plotting the series.

Parameters:
  • freq (*) – Data frequency used for display purposes. Refer to pandas docs for valid freq strings.

  • figsize (*) – figure size

  • title (*) – Title if default not appropriate

  • logy (*) – log-scale for y axis

  • kwargs (*) – passed to pandas’ plot method

plot_correlation(freq=None, title=None, figsize=(12, 6), **kwargs)[source]

Utility function to plot correlations.

Parameters:
  • freq (*) – Pandas data frequency alias string

  • title (*) – Plot title

  • figsize (*) – figure size

  • kwargs (*) – passed to Pandas’ plot_corr_heatmap function

plot_histograms(freq=None, title=None, figsize=(10, 10), **kwargs)[source]

Wrapper around pandas’ hist.

Parameters:
  • freq (*) – Data frequency used for display purposes. Refer to pandas docs for valid freq strings.

  • figsize (*) – figure size

  • title (*) – Title if default not appropriate

  • kwargs (*) – passed to pandas’ hist method

plot_scatter_matrix(freq=None, title=None, figsize=(10, 10), **kwargs)[source]

Wrapper around pandas’ scatter_matrix.

Parameters:
  • freq (*) – Data frequency used for display purposes. Refer to pandas docs for valid freq strings.

  • figsize (*) – figure size

  • title (*) – Title if default not appropriate

  • kwargs (*) – passed to pandas’ scatter_matrix method

set_date_range(start=None, end=None)[source]

Update date range of stats, charts, etc. If None then the original date range is used. So to reset to the original range, just call with no args.

Parameters:
  • start (*) – start date

  • end (*) – end date

set_riskfree_rate(rf)[source]

Set annual risk-free rate property and calculate properly annualized monthly and daily rates. Then performance stats are recalculated. Affects only those instances of PerformanceStats that are children of this GroupStats object.

Parameters:

rf (*) – Annual risk-free rate or risk-free rate price series (not returns)

to_csv(sep=',', path=None)[source]

Returns a CSV string with appropriate formatting. If path is not None, the string will be saved to file at path.

Parameters:
  • sep (*) – Separator

  • path (*) – If None, CSV string returned. Else file written to specified path.

class ffn.core.PerformanceStats(prices, rf=0.0, annualization_factor=None)[source]

Bases: object

PerformanceStats is a convenience class used for the performance evaluation of a price series. It contains various helper functions to help with plotting and contains a large amount of descriptive statistics.

Parameters:
  • prices (*) – A price series. Unavailable outer observations are excluded from endpoint statistics when total return is available.

  • rf (*) –

    Risk-free rate used in various calculation. Should be expressed as a yearly (annualized) return if it is a floating scalar. Otherwise rf should be a price series — it is converted internally with to_returns(). Note this differs from calc_sharpe and calc_sortino_ratio, which take rf as a return series. Passing a return series here silently behaves like rf=0.

\* name

Name, derived from price series name

Type:

str

\* return_table

A table of monthly returns with YTD figures as well.

Type:

DataFrame

\* lookback_returns

Returns for different lookback periods (1m, 3m, 6m, ytd…)

Type:

Series

\* stats

A series that contains all the stats

Type:

Series

\* annualization_factor

Annualization factor used in various calculations; aka nperiods, 252

Type:

float

display()[source]

Displays an overview containing descriptive stats for the Series provided.

display_lookback_returns()[source]

Displays the current lookback returns.

display_monthly_returns()[source]

Display a table containing monthly returns and ytd returns for every year in range.

plot(freq=None, figsize=(15, 5), title=None, logy=False, **kwargs)[source]

Helper function for plotting the series.

Parameters:
  • freq (*) – Data frequency used for display purposes. Refer to pandas docs for valid freq strings.

  • figsize (*) – figure size

  • title (*) – Title if default not appropriate

  • logy (*) – log-scale for y axis

  • kwargs (*) – passed to pandas’ plot method

plot_histogram(freq=None, figsize=(15, 5), title=None, bins=20, **kwargs)[source]

Plots a histogram of returns given a return frequency.

Parameters:
  • freq (*) – Data frequency used for display purposes. This will dictate the type of returns (daily returns, monthly, …) Refer to pandas docs for valid period strings.

  • figsize (*) – figure size

  • title (*) – Title if default not appropriate

  • bins (*) – number of bins for the histogram

  • kwargs (*) – passed to pandas’ hist method

set_date_range(start=None, end=None)[source]

Update date range of stats, charts, etc. If None then the original date is used. So to reset to the original range, just call with no args.

Parameters:
  • start (*) – start date

  • end (*) – end date

set_riskfree_rate(rf)[source]

Set annual risk-free rate property and calculate properly annualized monthly and daily rates. Then performance stats are recalculated. Affects only this instance of the PerformanceStats.

Parameters:

rf (*) –

Annual risk-free rate, or a risk-free price series (not returns)

to_csv(sep=',', path=None)[source]

Returns a CSV string with appropriate formatting. If path is not None, the string will be saved to file at path.

Parameters:
  • sep (*) – Separator

  • path (*) – If None, CSV string returned. Else file written to specified path.

ffn.core.annualize(returns, durations, one_year=365.0)[source]

Annualize returns using their respective durations.

Formula used is:

(1 + returns) ** (1 / (durations / one_year)) - 1

ffn.core.asfreq_actual(series, freq, method='ffill', how='end', normalize=False)[source]

Similar to pandas’ asfreq but keeps the actual dates. For example, if last data point in Jan is on the 29th, that date will be used instead of the 31st.

ffn.core.calc_cagr(prices)[source]

Calculates the CAGR (compound annual growth rate) for a given price series.

Parameters:

prices (*) – A Series of prices.

Returns:

  • float – cagr.

ffn.core.calc_calmar_ratio(prices)[source]

Calculates the Calmar ratio given a series of prices

Parameters:

prices (*) – Price series

ffn.core.calc_clusters(returns, n=None, plot=False)[source]

Calculates the clusters based on k-means clustering.

Parameters:
  • returns (*) – DataFrame of returns

  • n (*) – Specify # of clusters. If None, this will be automatically determined

  • plot (*) – Show plot?

Returns:

{cluster# : [col names]}

Return type:

  • dict with structure

ffn.core.calc_deflated_sharpe_ratio(returns, trial_sharpe_ratios, rf=0.0, nperiods=None, annualized_trials=True)[source]

Calculates the deflated Sharpe ratio of a strategy selected as the best of several trials: the probability that its true Sharpe ratio exceeds zero, after correcting for the multiple testing of the selection and for the non-normality of the returns.

Use it on the winner of a strategy search or a parameter optimization. trial_sharpe_ratios must cover all trials that were evaluated, not only the ones that were kept: their count and dispersion set the hurdle (see calc_expected_max_sharpe()) that the winner is measured against. Values close to 1 mean the winner clears the bar its own search sets by chance; values below ~0.95 suggest the result may be an artifact of having tried many candidates.

Where the trials are strongly correlated (e.g. a dense grid of similar parameters), the effective number of independent trials is lower than their count and the result is accordingly conservative.

The sample size, Sharpe ratio, skew and kurtosis are all taken over the excess returns, skipping missing observations.

Source: Bailey, D. and Lopez de Prado, M. (2014), “The Deflated Sharpe Ratio: Correcting for Selection Bias, Backtest Overfitting, and Non-Normality”, Journal of Portfolio Management, 40(5), 94-107.

Parameters:
  • returns (*) – Return series of the selected (best) trial.

  • trial_sharpe_ratios (*) – Sharpe ratios of all evaluated trials, e.g. as returned by calc_sharpe().

  • rf (*) –

    Risk-free rate expressed in yearly (annualized) terms or return series.

  • nperiods (*) – Frequency of returns (252 for daily, 12 for monthly, etc.). Inferred from returns if not provided.

  • annualized_trials (*) – Whether trial_sharpe_ratios are annualized, as calc_sharpe() returns them by default.

Returns:

  • float – probability [0, 1] that the selected trial’s Sharpe ratio is greater than zero.

ffn.core.calc_erc_weights(returns, initial_weights=None, risk_weights=None, covar_method='ledoit-wolf', risk_parity_method='ccd', maximum_iterations=100, tolerance=1e-08)[source]

Calculates the equal risk contribution / risk parity weights given a DataFrame of returns.

Parameters:
  • returns (*) – Returns for multiple securities.

  • initial_weights (*) – Starting asset weights [default inverse vol].

  • risk_weights (*) – Risk target weights [default equal weight].

  • covar_method (*) – Covariance matrix estimation method: ledoit-wolf (default) or standard.

  • risk_parity_method (*) – Risk parity estimation method: ccd (cyclical coordinate descent, default) or slsqp (scipy’s implementation of sequential least squares programming).

  • maximum_iterations (*) – Maximum iterations in iterative solutions.

  • tolerance (*) – Tolerance level in iterative solutions.

Returns:

weight}

Return type:

Series {col_name

ffn.core.calc_expected_max_sharpe(n_trials, sr_std)[source]

Calculates the expected maximum Sharpe ratio of n_trials skill-less strategies, i.e. the hurdle the best of that many trials is expected to clear by chance alone.

Searching over many strategies (or many parameter sets of one strategy) and keeping the best is a multiple testing problem: even with no skill, the maximum Sharpe ratio of the search grows with the number and the dispersion of the trials.

sr_std and the returned value are expressed in the same terms, so passing an annualized dispersion returns an annualized hurdle.

Parameters:
  • n_trials (*) – Number of trials (strategies or parameter sets) evaluated.

  • sr_std (*) – Standard deviation of the Sharpe ratios across those trials.

Returns:

  • float – expected maximum Sharpe ratio under the null of no skill.

ffn.core.calc_ftca(returns, threshold=0.5)[source]

Implementation of David Varadi’s Fast Threshold Clustering Algorithm (FTCA).

http://cssanalytics.wordpress.com/2013/11/26/fast-threshold-clustering-algorithm-ftca/ # NOQA

More stable than k-means for clustering purposes. If you want more clusters, use a higher threshold.

Parameters:
  • where (* returns - expects a pandas dataframe of returns) – each column is the name of a given security.

  • threshold (*) – Threshold parameter - use higher value for more clusters. Basically controls how similar (correlated) series have to be.

Returns:

dict of cluster name (a number) and list of securities in cluster

ffn.core.calc_information_ratio(returns, benchmark_returns)[source]

Calculates the Information ratio (or from Wikipedia).

ffn.core.calc_inv_vol_weights(returns)[source]

Calculates weights proportional to inverse volatility of each column.

Returns weights that are inversely proportional to the column’s volatility resulting in a set of portfolio weights where each position has the same level of volatility.

Note, that assets with returns all equal to NaN or 0 are excluded from the portfolio (their weight is set to NaN).

Returns:

weight}

Return type:

Series {col_name

ffn.core.calc_max_drawdown(prices)[source]

Calculates the max drawdown of a price series. If you want the actual drawdown series, please use to_drawdown_series.

ffn.core.calc_mean_var_weights(returns, weight_bounds=(0.0, 1.0), rf=0.0, covar_method='ledoit-wolf', options=None)[source]

Calculates the mean-variance weights given a DataFrame of returns.

Parameters:
  • returns (*) – Returns for multiple securities.

  • weight_bounds (*) – Weigh limits for optimization.

  • rf (*) –

    Risk-free rate used in utility calculation

  • covar_method (*) –

    Covariance matrix estimation method: ledoit-wolf or standard.

  • options (*) – options for minimizing, e.g. {‘maxiter’: 10000 }

Returns:

weight}

Return type:

Series {col_name

ffn.core.calc_mtd(daily_prices, monthly_prices)[source]

Calculates mtd return of a price series. Use daily_prices if prices are only available from same month else use monthly_prices

ffn.core.calc_perf_stats(prices, risk_free_rate=0.0, annualization_factor=252)[source]

Calculates the performance statistics given an object. The object should be a Series of prices.

A PerformanceStats object will be returned containing all the stats.

Parameters:
  • prices (*) – Series of prices

  • risk_free_rate (*) – Annual risk-free rate or risk-free rate price series

  • annualization_factor (*) – Annualizing factor. Default is 252 (trading days)

ffn.core.calc_prob_mom(returns, other_returns)[source]

Probabilistic momentum (see momentum investing)

Basically the “probability or confidence that one asset is going to outperform the other”.

Source:

http://cssanalytics.wordpress.com/2014/01/28/are-simple-momentum-strategies-too-dumb-introducing-probabilistic-momentum/ # NOQA

ffn.core.calc_risk_return_ratio(returns)[source]

Calculates the return / risk ratio. Basically the Sharpe ratio without factoring in the risk-free rate.

ffn.core.calc_sharpe(returns, rf=0.0, nperiods=None, annualize=True)[source]

Calculates the Sharpe ratio (see Sharpe vs. Sortino).

If rf is a non-zero floating scalar, you must specify nperiods. In this case, rf is assumed to be expressed in yearly (annualized) terms.

Returns NaN when the aligned excess returns have no dispersion, independently for each DataFrame column.

Parameters:
  • returns (*) – Input return series

  • rf (*) –

    Risk-free rate expressed as a yearly (annualized) return or return series (unlike PerformanceStats, which takes rf as a price series)

  • nperiods (*) – Frequency of returns (252 for daily, 12 for monthly, etc.)

ffn.core.calc_sortino_ratio(returns, rf=0.0, nperiods=None, annualize=True)[source]

Calculates the Sortino ratio given a series of returns (see Sharpe vs. Sortino).

Parameters:
  • returns (*) – Returns

  • rf (*) –

    Risk-free rate expressed in yearly (annualized) terms or return series.

  • nperiods (*) – Number of periods used for annualization. Must be provided or inferable if rf is a non-zero scalar

ffn.core.calc_stats(prices, annualization_factor=None)[source]

Calculates performance stats of a given object.

If object is Series, a PerformanceStats object is returned. If object is DataFrame, a GroupStats object is returned.

Parameters:
  • prices (*) – Set of prices

  • annualization_factor (*) – Annualization factor used in calculations

ffn.core.calc_total_return(prices)[source]

Calculates the total return of a series.

last / first - 1

ffn.core.calc_ytd(daily_prices, yearly_prices)[source]

Calculates ytd return of a price series. Use daily_prices if prices are only available from same year else use yearly_prices

ffn.core.deannualize(returns, nperiods)[source]

Convert return expressed in annual terms on a different basis.

Parameters:
  • returns (*) – Return(s)

  • nperiods (*) – Target basis, typically 252 for daily, 12 for monthly, etc.

ffn.core.drawdown_details(drawdown, index_type=<class 'pandas.DatetimeIndex'>)[source]

Returns a data frame with start, end, days (duration) and drawdown for each drawdown in a drawdown series.

Note

days are actual calendar days, not trading days

Parameters:

drawdown (*) – A drawdown Series (can be obtained w/ drawdown(prices).

Returns:

  • pandas.DataFrame – A data frame with the following

    columns: start, end, days, drawdown.

ffn.core.drop_duplicate_cols(df)[source]

Removes duplicate columns from a dataframe and keeps column w/ longest history

ffn.core.extend_pandas()[source]

Extends pandas’ PandasObject (Series, Series, DataFrame) with some functions defined in this file.

This facilitates common functional composition used in quant finance.

Ex:

prices.to_returns().dropna().calc_clusters() (where prices would be a DataFrame)

ffn.core.get_num_days_required(offset, period='d', perc_required=0.9, annualization_factor=252)[source]

Estimates the number of days required to assume that data is OK.

Helper function used to determine if there are enough “good” data days over a given period.

Parameters:
  • offset (*) – Offset (lookback) period.

  • period (*) – Period string.

  • perc_required (*) – percentage of number of days expected required.

ffn.core.infer_freq(data)[source]

Infer the most likely frequency given the input index. If the frequency is uncertain or index is not DateTime like, just return None

Parameters:

data (*) – Any timeseries dataframe or series

ffn.core.infer_nperiods(data, annualization_factor=None)[source]
ffn.core.limit_weights(weights, limit=0.1)[source]

Limits weights and redistributes excedent amount proportionally.

ex:
  • weights are {a: 0.7, b: 0.2, c: 0.1}

  • call with limit=0.5

  • excess 0.2 in a is ditributed to b and c

    proportionally. - result is {a: 0.5, b: 0.33, c: 0.167}

Parameters:
  • weights (*) – A series describing the weights

  • limit (*) – Maximum weight allowed

ffn.core.merge(*series)[source]

Merge Series and/or DataFrames together.

Returns a DataFrame.

ffn.core.plot_corr_heatmap(data, **kwargs)[source]

Plots the correlation heatmap for a given DataFrame.

ffn.core.plot_heatmap(data, title='Heatmap', show_legend=True, show_labels=True, label_fmt='.2f', vmin=None, vmax=None, figsize=None, label_color='w', cmap='RdBu', **kwargs)[source]

Plot a heatmap using matplotlib’s pcolor.

Parameters:
  • data (*) – DataFrame to plot. Usually small matrix (ex. correlation matrix).

  • title (*) – Plot title

  • show_legend (*) – Show color legend

  • show_labels (*) – Show value labels

  • label_fmt (*) – Label format string

  • vmin (*) – Min value for scale

  • vmax (*) – Max value for scale

  • cmap (*) – Color map

  • kwargs (*) – Passed to matplotlib’s pcolor

ffn.core.random_weights(n, bounds=(0.0, 1.0), total=1.0)[source]

Generate pseudo-random weights.

Returns a list of random weights that is of length n, where each weight is in the range bounds, and where the weights sum up to total.

Useful for creating random portfolios when benchmarking.

Parameters:
  • n (*) – number of random weights

  • bounds (*) – bounds for each weight

  • total (*) – total sum of the weights

ffn.core.rebase(prices, value=100)[source]

Rebase each price series to a given initial value.

Each series uses its first non-missing price as the baseline. Missing prices remain missing in the returned object.

Parameters:
  • prices (*) – Expects a price Series or DataFrame

  • value (*) – starting value for all series.

ffn.core.resample_returns(returns, func, seed=0, num_trials=100)[source]

Resample the returns and calculate any statistic on every new sample.

https://en.wikipedia.org/wiki/Resampling_(statistics)

Parameters:
  • DataFrame) (returns (Series,) – Returns

  • func – Given the resampled returns calculate a statistic

  • seed – Seed for random number generator

  • num_trials – Number of times to resample and run the experiment

Returns:

Series of resampled statistics

ffn.core.rescale(x, min=0.0, max=1.0, axis=0)[source]

Rescale values to fit a certain range [min, max]

ffn.core.rollapply(data, window, fn)[source]

Apply a function fn over a rolling window of size window.

Parameters:
  • data (*) – Series or DataFrame

  • window (*) – Window size

  • fn (*) – Function to apply over the rolling window. For a series, the return value is expected to be a single number. For a DataFrame, it shuold return a new row.

Returns:

  • Object of same dimensions as data

ffn.core.to_drawdown_series(prices)[source]

Calculates the drawdown series.

This returns a series representing a drawdown. When the price is at all time highs, the drawdown is 0. However, when prices are below high water marks, the drawdown series = current / hwm - 1

The max drawdown can be obtained by simply calling .min() on the result (since the drawdown series is negative)

Method ignores all gaps of NaN’s in the price series.

Parameters:

prices (*) – Series of prices.

ffn.core.to_excess_returns(returns, rf, nperiods=None)[source]

Given a series of returns, it will return the excess returns over rf.

A Series risk-free return is aligned to the return index. For DataFrame returns, it is subtracted from every column by date.

Parameters:
  • returns (*) – Returns

  • rf (*) – Risk-Free rate(s) expressed in annualized term or return series

  • nperiods (*) – Optional. If provided, will convert rf to different frequency using deannualize only if rf is a floating scalar

Returns:

Returns - rf

Return type:

  • excess_returns (Series, DataFrame)

ffn.core.to_log_returns(prices)[source]

Calculates the log returns of a price series.

Formula is: ln(p1/p0)

Parameters:

prices (*) – Expects a price series

ffn.core.to_monthly(series, method='ffill', how='end')[source]

Convenience method that wraps asfreq_actual with ‘M’ param (method=’ffill’, how=’end’).

ffn.core.to_price_index(returns, start=100)[source]

Returns a price index given a series of returns.

Parameters:
  • returns (*) – Expects a return series

  • start (*) – Starting level

Assumes arithmetic returns.

The first value of the returned price index is always start. When a Series begins with NaN, or every column of a DataFrame begins with NaN (as produced by to_returns()), the NaN position is replaced by start and the output length equals the input length. Otherwise start is prepended so that every non-missing return is reflected in the prices. Missing returns are treated as zero, and the round-trip to_returns(to_price_index(r)) recovers the non-missing returns.

Formula is: start, start * cumprod(1+r)

ffn.core.to_returns(prices)[source]

Calculates the simple arithmetic returns of a price series.

Formula is: (t1 / t0) - 1

Parameters:

prices (*) – Expects a price series

ffn.core.to_ulcer_index(prices)[source]

Calculates the Ulcer Index for a series of investment returns.

Converts from prices -> Ulcer index

See https://en.wikipedia.org/wiki/Ulcer_index

Method ignores all gaps of NaN’s in the price series.

Parameters:

prices (*) – Prices

Returns:

  • float (Series if prices has more than one column) – The Ulcer Index.

ffn.core.to_ulcer_performance_index(prices, rf=0.0, nperiods=None)[source]

Converts from prices -> ulcer performance index.

See https://en.wikipedia.org/wiki/Ulcer_index

Method ignores all gaps of NaN’s in the price series.

Parameters:
  • prices (*) – Prices

  • rf (*) – Risk-free rate of return. Assumed to be expressed in yearly (annualized) terms or return series

  • nperiods (*) – Used to deannualize rf if rf is provided (non-zero)

ffn.core.winsorize(x, axis=0, limits=0.01)[source]

Winsorize values based on limits

ffn.core.year_frac(start, end)[source]

Similar to excel’s yearfrac function. Returns a year fraction between two dates (i.e. 1.53 years).

Approximation using the average number of seconds in a year.

Parameters:
  • start (*) – start date

  • end (*) – end date

data Module

ffn.data.DEFAULT_PROVIDER(ticker: str, field, start=None, end=None, mrefresh=False) Series | DataFrame
ffn.data.csv(ticker: str, path='data.csv', field='', mrefresh=False, **kwargs) Series[source]

Data provider wrapper around pandas’ read_csv. Provides memoization.

ffn.data.get(tickers: Sequence[str], provider=None, common_dates=True, forward_fill=False, clean_tickers=True, column_names=None, ticker_field_sep=':', mrefresh=False, existing=None, **kwargs) DataFrame[source]

Helper function for retrieving data as a DataFrame.

Parameters:
  • tickers (*) – Tickers to download.

  • provider (*) – Provider to use for downloading data. By default it will be ffn.DEFAULT_PROVIDER if not provided.

  • common_dates (*) – Keep common dates only? Drop na’s.

  • forward_fill (*) – forward fill values if missing. Only works if common_dates is False, since common_dates will remove all nan’s, so no filling forward necessary.

  • clean_tickers (*) – Should the tickers be ‘cleaned’ using ffn.utils.clean_tickers? Basically remove non-standard characters (^VIX -> vix) and standardize to lower case.

  • column_names (*) – List of column names if clean_tickers is not satisfactory.

  • ticker_field_sep (*) – separator used to determine the ticker and field. This is in case we want to specify particular, non-default fields. For example, we might want: AAPL:Low,AAPL:High,AAPL:Close. ‘:’ is the separator.

  • mrefresh (*) – Ignore memoization.

  • existing (*) – Existing DataFrame to append returns to - used when we download from multiple sources

  • kwargs (*) – passed to provider

ffn.data.web(ticker: str, field=None, start=None, end=None, mrefresh=False, source='yahoo')[source]

Data provider wrapper around pandas.io.data provider. Provides memoization.

ffn.data.yf(ticker: str, field, start=None, end=None, mrefresh=False) Series | DataFrame[source]

utils Module

ffn.utils.as_format(item: DataFrame | Series, format_str='.2f') DataFrame | Series[source]

Map a format string over a pandas object.

ffn.utils.as_percent(self, digits=2)[source]
ffn.utils.clean_ticker(ticker: str) str[source]

Cleans a ticker for easier use throughout MoneyTree

Splits by space and only keeps first bit. Also removes any characters that are not letters. Returns as lowercase.

>>> clean_ticker('^VIX')
'vix'
>>> clean_ticker('SPX Index')
'spx'
ffn.utils.clean_tickers(tickers: Sequence[str]) list[str][source]

Maps clean_ticker over tickers.

ffn.utils.fmtn(number: float) str[source]

Formatting helper - float

ffn.utils.fmtp(number: float) str[source]

Formatting helper - percent

ffn.utils.fmtpn(number: float) str[source]

Formatting helper - percent no % sign

ffn.utils.get_freq_name(period: str) str | None[source]
ffn.utils.memoize(f, refresh_keyword='mrefresh')[source]

Memoize decorator. The refresh keyword is the keyword used to bypass the cache (in the function call).

ffn.utils.parse_arg(arg: str | list[str] | tuple[str])[source]

Parses arguments for convenience. Argument can be a csv list (‘a,b,c’), a string, a list, a tuple.

Returns a list.

ffn.utils.scale(val: float, src: Sequence[float], dst: Sequence[float]) float[source]

Scale value from src range to dst range. If value outside bounds, it is clipped and set to the low or high bound of dst.

Ex:

scale(0, (0.0, 99.0), (-1.0, 1.0)) == -1.0 scale(-5, (0.0, 99.0), (-1.0, 1.0)) == -1.0