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: Algo-Trading Framework for Machine Learning, TradFi, CeFi and DeFi ready.

GitHub stars PyPI downloads PyPI PyPI - Support Python Versions Jupyter Book Badge Poetry

PFund (/piː fʌnd/), which stands for "Personal Fund", is an algo-trading framework designed for using machine learning models 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.

PFund allows traders to:

  • perform vectorized or event-driven backtesting with
    • different resolutions of data, e.g. orderbook data, tick data, bar data etc.
    • different data tools, e.g. pandas, polars etc.
  • train machine learning models using their favorite frameworks, i.e. PFund is ML-framework agnostic
  • tune strategy (hyper)parameters by splitting data into training sets, development sets and test sets
  • go from backtesting to live trading by just changing ONE line of code!!
  • execute trades manually/semi-manually via a trading app (frontend+backend)

It is created to enable trading for PFund.ai - a trading platform that bridges algo-trading and manual trading using AI (LLM).

Since PFund's sole purpose is for trading only, for all the data work, 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

Project Status

Caution: PFund is at a VERY EARLY stage, use it at your own risk.

PFund is currently under active development, the framework design will be prioritized first over stability and scalability.

Please note that the available version is a dev version, not a stable one.
You are encouraged to play with the dev version, but only use it when a stable version is released.

PFund for the time being only supports vectorized backtesting using Bybit and Yahoo Finance data for testing purpose.

Mission

As an algo-trader, if you aim to quickly try out some trading ideas to see if they work, and if they do, deploy them for live traidng, it is actually not a trivial task since it involves multiple stages:

  • Ideation
  • Strategy development
  • Backtesting
  • Model development (if using machine learning)
  • Model training (if using machine learning)
  • Parameter training / hyperparameter tuning
  • Strategy deployment
  • Portfolio monitoring

This overview already omits some intricate steps, such as data handling and API integration.

PFund's mission is to enable traders to concentrate solely on strategy formulation while the framework manages the rest. With PFund serving as the core trade engine, it empowers retail traders to have a fund management experience on PFund.ai as if they are operating their personal hedge fund, hence the name PFund.

Core Features

  • Easily switch environments with just one line of code, transitioning from backtesting to live trading
  • Supports machine learning models, features, technical analysis indicators
  • Both Strategy() and Model() are treated as first-class citizens
  • 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
  • Allows choosing your preferred data tool, e.g. pandas, polars, pyspark etc.
  • Features a modern frontend using Mantine UI and TradingView's Charts library
  • Supports manual/semi-manual trading using the frontend

Installation

Using Poetry (Recommended)

poetry add pfund

# update to the latest version:
poetry update pfund

Using Pip

pip install pfund

# install the latest version:
pip install -U pfund

Checking your installation

$ pfund --version

pfund, version 0.0.1.dev4

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()

Model Hub

Imagine a space where algo-traders can share their 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.

Model Hub is coming soon on PFund.ai, Stay Tuned!

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 pipeline 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.1.dev8.tar.gz (221.3 kB view details)

Uploaded Source

Built Distribution

pfund-0.0.1.dev8-py3-none-any.whl (267.7 kB view details)

Uploaded Python 3

File details

Details for the file pfund-0.0.1.dev8.tar.gz.

File metadata

  • Download URL: pfund-0.0.1.dev8.tar.gz
  • Upload date:
  • Size: 221.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.12.1 Darwin/23.4.0

File hashes

Hashes for pfund-0.0.1.dev8.tar.gz
Algorithm Hash digest
SHA256 9c2e6239f27076bc4c502c057e847fc806f9ee982737ddb0d448972d5dae36d7
MD5 d0f84e8e983ec60e7d2664c181891567
BLAKE2b-256 1caaebd79c573934175de76644a6c4e83b7d9cf6fc43cec2479397f21f4b60c2

See more details on using hashes here.

File details

Details for the file pfund-0.0.1.dev8-py3-none-any.whl.

File metadata

  • Download URL: pfund-0.0.1.dev8-py3-none-any.whl
  • Upload date:
  • Size: 267.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.12.1 Darwin/23.4.0

File hashes

Hashes for pfund-0.0.1.dev8-py3-none-any.whl
Algorithm Hash digest
SHA256 adb2f10f76c0eafd395cad4ca116b17cf949bd0ce54429f6d64d85de243190bc
MD5 715d49526be3f3389c15cf4eaca2d56a
BLAKE2b-256 20afabcdde029b0536796acf0186d64ec8dcd3aae8a3ef54373627ece7f78e04

See more details on using hashes here.

Supported by

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