Skip to main content

A Complete Algo-Trading Framework for Machine Learning, enabling trading across TradFi, CeFi and DeFi. Supports Vectorized and Event-Driven Backtesting, Paper and Live Trading

Project description

PFund: A Complete Algo-Trading Framework powered by Machine Learning and Data Engineering, TradFi, CeFi and DeFi ready.

Twitter Follow Downloads PyPI PyPI - Support Python Versions afterpython Ask DeepWiki View Code Wiki Discussions

This library is NOT ready for use, please wait for 0.1.0 release.

Problem

Machine learning (AI) and data engineering (Big Data) fields are advancing every year, but everyday traders are not able to enjoy the benefits of these improvements, leading to a widening gap between retail traders and professional traders.

Solution

A modern algo-trading framework is needed to bridge the gap between algo-trading, machine learning and data engineering, empowering retail traders with state-of-the-art machine learning models and data engineering tools so that traders only need to focus on strategy research and the framework takes care of the rest.


PFund (/piː fʌnd/), which stands for "Personal Fund", is an algo-trading framework designed for using machine learning models natively to trade across TradFi (Traditional Finance, e.g. Interactive Brokers), CeFi (Centralized Finance, e.g. Binance) and DeFi (Decentralized Finance, e.g. dYdX), or in simple terms, Stocks and Cryptos.

Core Features

  • Supports vectorized and event-driven backtesting with different resolutions of data, e.g. tick data, second data and minute data etc.
  • Allows choosing your preferred data tool, e.g. pandas, polars, pyspark etc.
  • Supports machine learning models, features, technical analysis indicators
  • Trains machine learning models using your favorite frameworks, i.e. PFund is ML-framework agnostic
  • Offers LEGO-style strategy and model building, allowing strategies to add other strategies, models to add other models
  • Streamlines the algo-trading flow, from vectorized backtesting for strategy prototyping and event-driven backtesting for strategy development, to live trading for strategy deployment
  • Enables parallel data processing, e.g. Interactive Brokers and Binance each have their own process for receiving data feeds
  • Switches from backtesting to live trading by just changing ONE line of code!!

As PFund is for trading only, for all the data workloads, there is a separate library to handle that:
PFeed - Data pipeline for algo-trading, helping traders in getting real-time and historical data, and storing them in a local data lake for quantitative research.


Table of Contents

Installation

Using uv (Recommended)

uv add pfund

# pick your data sources, e.g. yfinance for Yahoo Finance
uv add pfeed[yfinance]

# [optional] install pfund-plot for plotting, pick your notebook, e.g. marimo notebook and/or jupyter notebook
uv add pfund-plot[marimo,jupyter]

# [optional] install mtflow for handling trading ops, e.g. adding monitors, running dashboards, etc.
uv add mtflow

# update to the latest version:
uv update pfund

Using Pip

# same as above, you can choose to install "pfund[all]", "pfund[data,ml,fe]", "pfund[data,ml]", "pfund[data]" or "pfund"
pip install "pfund[all]"

# install the latest version:
pip install -U pfund

Checking your installation

$ pfund --version

Quick Start

Backtesting

import pfund as pf

# NOTE: for more exciting strategies, please visit pfund.ai
class YourStrategy(pf.Strategy):
    # triggered by bar/kline data (e.g. 1-minute data)
    def on_bar(self):
        # write your trading logic here
        pass


engine = pf.BacktestEngine(mode='vectorized')
strategy = engine.add_strategy(YourStrategy(), name='your_strategy')
strategy.add_data(
  'IB', 'AAPL', 'USD', 'STK', resolutions=['1d'],
  backtest={
    # NOTE: since IB does not provide any historical data for backtesting purpose, use data from 'YAHOO_FINANCE'
    'data_source': 'YAHOO_FINANCE',
    'start_date': '2024-01-01',
    'end_date': '2024-02-01',
  }
)
engine.run()

Live Trading

Just change one line of code, from 'BacktestEngine' to 'TradeEngine'. BOOM! you can now start live trading.

import pfund as pf

engine = pf.TradeEngine(env='LIVE')
strategy = engine.add_strategy(YourStrategy(), name='your_strategy')
strategy.add_data(
  'IB', 'AAPL', 'USD', 'STK', resolutions=['1d'],
  # for convenience, you can keep the kwarg `backtest`, `TradeEngine` will ignore it
  backtest={
    # NOTE: since IB does not provide any historical data for backtesting purpose, use data from 'YAHOO_FINANCE'
    'data_source': 'YAHOO_FINANCE',
    'start_date': '2024-01-01',
    'end_date': '2024-02-01',
  }
)
engine.run()

Parameter Training / Hyperparameter Tuning

The correct term should be "Hyperparameter Tuning", but since not all traders are familiar with machine learning, the framework uses a more well-known term "training".

import pfund as pf

engine = pf.TrainEngine()
strategy = engine.add_strategy(...)
strategy.add_data(...)
strategy.add_indicator(...)
engine.run()

Building LEGO-Style Strategy and Model

import pfund as pf

engine = pf.TradeEngine(env='LIVE')
strategy = engine.add_strategy(...)
strategy.add_data(...)
model = strategy.add_model(...)

model.add_data(...)  # using different data than strategy's
sub_model = model.add_model(...)  # YES, model can add another model to its use
# You can keep going: 
# sub_sub_model = sub_model.add_model(...)

engine.run()

PFund Hub

Imagine a space where algo-traders can share their trading strategies and machine learning models with one another. Strategy and model development could be so much faster since you can build on top of an existing working model.


Supported Trading Venues

Trading Venue Vectorized Backtesting Event-Driven Backtesting Paper Trading Live Trading
Bybit 🟢 🟡 🟡 🟡
*Interactive Brokers (IB) 🟡 🟡 🟡 🟡
Binance 🔴 🔴 🔴 🔴
OKX 🔴 🔴 🔴 🔴
*Alpaca 🔴 🔴 🔴 🔴
*Futu 🔴 🔴 🔴 🔴
dYdX 🔴 🔴 🔴 🔴

🟢 = finished
🟡 = in progress
🔴 = todo
* = use a separate data source (e.g. FirstRate Data) for backtesting

Related Projects

  • PFeed — Data engine for algo-trading, helping traders in getting real-time and historical data, and storing them in a local data lake for quantitative research.
  • PyTrade.org - A curated list of Python libraries and resources for algorithmic trading.

Disclaimer

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

This algo-trading framework is intended for educational and research purposes only. It should not be used for real trading without understanding the risks involved. Trading in financial markets involves significant risk, and there is always the potential for loss. Your trading results may vary. No representation is being made that any account will or is likely to achieve profits or losses similar to those discussed on this platform.

The developers of this framework are not responsible for any financial losses incurred from using this software. Users should conduct their due diligence and consult with a professional financial advisor before engaging in real trading activities.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pfund-0.0.10.tar.gz (166.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pfund-0.0.10-py3-none-any.whl (243.1 kB view details)

Uploaded Python 3

File details

Details for the file pfund-0.0.10.tar.gz.

File metadata

  • Download URL: pfund-0.0.10.tar.gz
  • Upload date:
  • Size: 166.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.14 {"installer":{"name":"uv","version":"0.11.14","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pfund-0.0.10.tar.gz
Algorithm Hash digest
SHA256 6801c2c1b1d92b15e1f77c67e350bedc19c880111c621dc522d8f504e3a6958c
MD5 50b59bb290a0e123cb43583108a9a2f4
BLAKE2b-256 7c6e3cd2a75564a714caccd69eaf505a9eb521fb8a4fc9cb1126eecdbd090e65

See more details on using hashes here.

File details

Details for the file pfund-0.0.10-py3-none-any.whl.

File metadata

  • Download URL: pfund-0.0.10-py3-none-any.whl
  • Upload date:
  • Size: 243.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.14 {"installer":{"name":"uv","version":"0.11.14","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pfund-0.0.10-py3-none-any.whl
Algorithm Hash digest
SHA256 25626a7820bb192dd9ebb6a293810f221da4e78696cc97eef5cb968a38aacc7d
MD5 e69cedddfa56440fcbbd039b6819e4ad
BLAKE2b-256 7256dcac6dc00ff6ede16a6522bae3aed2ffe8910d4b3edb7fe66a7e366eb72d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page