Convert Figma logo to code with AI

pmorissette logoffn

ffn - a financial function library for Python

1,897
284
1,897
23

Top Related Projects

5,612

Portfolio and risk analytics in Python

13,247

Download market data from Yahoo! Finance's API

17,609

Zipline, a Pythonic Algorithmic Trading Library

15,357

Qlib is an AI-oriented quantitative investment platform that aims to realize the potential, empower research, and create value using AI technologies in quantitative investment, from exploring ideas to implementing productions. Qlib supports diverse machine learning modeling paradigms. including supervised learning, market dynamics modeling, and RL.

Python wrapper for TA-Lib (http://ta-lib.org/).

4,295

Technical Analysis Library using Pandas and Numpy

Quick Overview

FFN (Financial Functions for Python) is a Python library that provides a set of tools for quantitative finance. It offers functionality for performance and risk analysis, as well as a flexible backtesting engine. FFN is designed to work seamlessly with pandas, making it easy to integrate into existing financial analysis workflows.

Pros

  • Comprehensive set of financial functions and metrics
  • Seamless integration with pandas for easy data manipulation
  • Flexible backtesting engine for strategy evaluation
  • Well-documented with clear examples and API reference

Cons

  • Limited community support compared to larger financial libraries
  • May have a steeper learning curve for beginners in quantitative finance
  • Not as actively maintained as some other financial libraries
  • Lacks some advanced features found in more specialized libraries

Code Examples

  1. Calculating returns and risk metrics:
import ffn

# Create a sample price series
prices = ffn.random_series(100)

# Calculate returns and risk metrics
stats = prices.calc_stats()
print(stats.summary())
  1. Creating a portfolio and analyzing performance:
import ffn

# Create a portfolio of stocks
data = ffn.get('spy,agg,eem,iwm', start='2010-01-01')
portfolio = ffn.Portfolio(data)

# Analyze portfolio performance
perf = portfolio.perf_stats()
print(perf)
  1. Running a simple backtest:
import ffn

# Define a simple strategy
def simple_strategy(data):
    return data > data.rolling(50).mean()

# Run backtest
prices = ffn.get('spy', start='2010-01-01')
bt = ffn.BackTest(prices, simple_strategy)
bt.run()
print(bt.stats)

Getting Started

To get started with FFN, follow these steps:

  1. Install FFN using pip:

    pip install ffn
    
  2. Import FFN in your Python script:

    import ffn
    
  3. Start using FFN functions:

    # Fetch stock data
    data = ffn.get('aapl,msft,goog', start='2020-01-01')
    
    # Calculate returns
    returns = data.to_returns()
    
    # Analyze performance
    stats = returns.calc_stats()
    print(stats.summary())
    

Competitor Comparisons

5,612

Portfolio and risk analytics in Python

Pros of pyfolio

  • More comprehensive performance and risk analysis tools
  • Better visualization capabilities with interactive plots
  • Integrates well with Zipline for backtesting strategies

Cons of pyfolio

  • Steeper learning curve due to more complex features
  • Less frequently updated compared to ffn
  • Heavier dependencies, which may increase setup complexity

Code Comparison

ffn example:

import ffn

data = ffn.get('spy,agg', start='2010-01-01')
stats = ffn.stats(data)
print(stats)

pyfolio example:

import pyfolio as pf

returns, positions, transactions = pf.utils.extract_rets_pos_txn_from_zipline(results)
pf.create_full_tear_sheet(returns, positions=positions, transactions=transactions)

Both libraries offer financial analysis tools, but pyfolio provides more advanced features and integrations, while ffn focuses on simplicity and ease of use. The choice between them depends on the specific needs of the project and the user's experience level.

13,247

Download market data from Yahoo! Finance's API

Pros of yfinance

  • Focused on retrieving financial data from Yahoo Finance
  • Provides real-time and historical market data
  • Supports downloading multiple tickers simultaneously

Cons of yfinance

  • Limited to Yahoo Finance as a data source
  • Fewer built-in financial analysis tools
  • May be affected by changes to Yahoo Finance's API

Code Comparison

yfinance:

import yfinance as yf

ticker = yf.Ticker("AAPL")
hist = ticker.history(period="1mo")
print(hist.head())

ffn:

import ffn

data = ffn.get("AAPL", start="2023-01-01")
perf = ffn.calc_total_return(data)
print(perf.head())

Key Differences

  • yfinance is primarily for data retrieval, while ffn offers more comprehensive financial analysis tools
  • ffn provides performance metrics and risk analysis functions out-of-the-box
  • yfinance allows for more granular control over data fetching parameters
  • ffn integrates with multiple data sources, not limited to Yahoo Finance

Both libraries serve different purposes within the financial data ecosystem. yfinance excels at retrieving up-to-date market data, while ffn offers a broader suite of financial analysis tools for portfolio management and performance evaluation.

17,609

Zipline, a Pythonic Algorithmic Trading Library

Pros of Zipline

  • More comprehensive backtesting framework with event-driven architecture
  • Supports live trading integration
  • Larger community and more extensive documentation

Cons of Zipline

  • Steeper learning curve due to complexity
  • Slower performance for simple backtests
  • Less frequently updated (last commit over 2 years ago)

Code Comparison

FFN example:

import ffn

data = ffn.get('spy,agg', start='2010-01-01')
stats = data.calc_stats()
print(stats)

Zipline example:

from zipline.api import order, record, symbol
from zipline import run_algorithm

def initialize(context):
    context.asset = symbol('AAPL')

def handle_data(context, data):
    order(context.asset, 10)
    record(AAPL=data.current(context.asset, 'price'))

run_algorithm(start=start_date, end=end_date, initialize=initialize, handle_data=handle_data)

FFN is more straightforward for quick analysis, while Zipline offers a more robust framework for complex strategies.

15,357

Qlib is an AI-oriented quantitative investment platform that aims to realize the potential, empower research, and create value using AI technologies in quantitative investment, from exploring ideas to implementing productions. Qlib supports diverse machine learning modeling paradigms. including supervised learning, market dynamics modeling, and RL.

Pros of qlib

  • More comprehensive and feature-rich, offering advanced machine learning models for quantitative investing
  • Actively maintained with frequent updates and a larger community
  • Provides data processing pipelines and backtesting frameworks

Cons of qlib

  • Steeper learning curve due to its complexity and broader scope
  • Requires more computational resources for advanced features
  • May be overkill for simple financial analysis tasks

Code Comparison

ffn:

import ffn

data = ffn.get('spy,agg', start='2010-01-01')
perf = data.calc_stats()
print(perf.display())

qlib:

from qlib.contrib.data.handler import Alpha158
from qlib.contrib.model.gbdt import LGBModel
from qlib.workflow import R

dataset = Alpha158(instruments="csi300")
model = LGBModel()
R.run(dataset=dataset, model=model)

The code snippets demonstrate that ffn is more straightforward for basic financial analysis, while qlib offers more advanced capabilities for quantitative investing and machine learning-based predictions.

Python wrapper for TA-Lib (http://ta-lib.org/).

Pros of ta-lib-python

  • Comprehensive library with over 150 technical analysis indicators
  • Highly optimized C implementation for fast calculations
  • Well-established and widely used in the financial industry

Cons of ta-lib-python

  • Requires separate installation of TA-Lib C library
  • Less Pythonic API compared to ffn
  • Steeper learning curve for beginners

Code Comparison

ffn:

import ffn

data = ffn.get('SPY', start='2020-01-01')
returns = data.pct_change()
sharpe = ffn.calc_sharpe(returns)

ta-lib-python:

import talib
import numpy as np

close_prices = np.random.random(100)
sma = talib.SMA(close_prices, timeperiod=20)
rsi = talib.RSI(close_prices, timeperiod=14)

Summary

ffn focuses on financial analysis and performance metrics, while ta-lib-python specializes in technical analysis indicators. ffn offers a more user-friendly API and easier installation, but ta-lib-python provides a broader range of indicators and faster performance for large datasets. The choice between the two depends on specific project requirements and user preferences.

4,295

Technical Analysis Library using Pandas and Numpy

Pros of ta

  • Focuses specifically on technical analysis indicators and chart patterns
  • Provides a wider range of technical indicators and overlays
  • Actively maintained with more frequent updates

Cons of ta

  • Less comprehensive in terms of overall financial analysis capabilities
  • May require additional libraries for portfolio management and risk analysis
  • Steeper learning curve for users not familiar with technical analysis concepts

Code Comparison

ta:

import pandas as pd
import ta

df = pd.read_csv('your_data.csv')
df = ta.add_all_ta_features(df, "open", "high", "low", "close", "volume")

ffn:

import ffn

data = ffn.get('SPY, AAPL, MSFT', start='2010-01-01')
stats = data.calc_stats()
print(stats.display())

Summary

ta is more specialized for technical analysis, offering a broader range of indicators but with a narrower focus. ffn provides a more comprehensive financial analysis toolkit, including portfolio management features. The choice between the two depends on the specific needs of the user, with ta being better suited for those primarily interested in technical analysis, while ffn offers a more well-rounded approach to financial data analysis and portfolio management.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Build Status PyPI Version PyPI License

ffn - Financial Functions for Python

Alpha release - please let me know if you find any bugs!

If you are looking for a full backtesting framework, please check out bt. bt is built atop ffn and makes it easy and fast to backtest quantitative strategies.

Overview

ffn is a library that contains many useful functions for those who work in quantitative finance. It stands on the shoulders of giants (Pandas, Numpy, Scipy, etc.) and provides a vast array of utilities, from performance measurement and evaluation to graphing and common data transformations.

import ffn
returns = ffn.get('aapl,msft,c,gs,ge', start='2010-01-01').to_returns().dropna()
returns.calc_mean_var_weights().as_format('.2%')
    aapl    62.54%
    c       -0.00%
    ge      36.19%
    gs      -0.00%
    msft     1.26%
    dtype: object

Installation

The easiest way to install ffn is from the Python Package Index using pip.

pip install ffn

Since ffn has many dependencies, we strongly recommend installing the Anaconda Scientific Python Distribution. This distribution comes with many of the required packages pre-installed, including pip. Once Anaconda is installed, the above command should complete the installation.

Documentation

Read the docs at http://pmorissette.github.io/ffn