Skip to main content

Pear2Pear package

Project description

Pear2Pear

Pear2Pear is a Python library for genetic discovery and optimization of trading patterns. It leverages evolutionary algorithms, Numba acceleration, and parallelized backtesting to automatically discover, test, and evolve profitable signal combinations from historical financial data.

Designed for quantitative research, algorithmic trading, and financial AI experimentation, Pear2Pear autonomously evolves pattern-based strategies based on profitability metrics such as the profit factor.


✨ Key Features

  • 🧬 Genetic Trading Pattern Evolution Generates and evolves complex trading conditions across multiple market indicators using crossover, mutation, and elitism.

  • Performance-Driven Core trading simulations and gain calculations are compiled with Numba, providing large performance gains over standard Python.

  • 🧠 Adaptive Anti-Overfitting System Monitors fitness improvements and re-evaluates on unseen test data to detect overfitting automatically.

  • 🔁 Parallelized Fitness Evaluation Scales to modern CPUs using the multiprocess module for concurrent evaluation of multiple patterns.

  • 📈 Profit Factor Optimization Each evolved pattern is scored based on its ability to generate consistent gains versus losses over a test dataset.


📦 Installation

Install directly from PyPI:

pip install pear2pear

🚀 Quick Start

Below is a minimal working example of how to use Pear2Pear.

from pear2pear import startGeneration

# Example input data
givenData = [
    [["rsi", 2, ">", "rsi", 1], ["atr", 2, ">", "atr", 3], ["atr", 2, ">", "atr", 1]],
    [["dochiant_mid_5", 4, ">", "dochiant_mid_5", 3], ["dochiant_up_5", 3, ">", "dochiant_up", 2], ["atr", 2, "<", "atr", 1]]
]

pipeline = [
    {
        "name": "Stock - test ( scalp )",
        "columns_list": [
            "ema",
            "dochiant_low_5",
            "dochiant_up_5",
            "dochiant_mid_5",
            "dochiant_low",
            "dochiant_up",
            "dochiant_mid",
            "obv",
            "atr",
            "rsi"
        ],
        "status": False,
        "isBellow": True
    }
]

# Start the genetic pattern evolution
startGeneration(givenData, pipeline)

This will:

  1. Load the training dataset (default: ./Pear2Pear/dataset_1h.csv)
  2. Generate an initial population of random indicator-based patterns
  3. Evolve, crossbreed, and mutate patterns to maximize profit factor
  4. Re-test the best-performing patterns on a separate dataset to avoid overfitting

🧩 Core Concepts

🔹 Pattern Representation

Each pattern is a structured condition linking indicators, indices, and relational operators:

["rsi", 2, ">", "rsi", 1]

This means “RSI(2) is greater than RSI(1)”, representing a potential buy or sell trigger.

🔹 Pipeline

The pipeline defines which columns (indicators) can be used and whether to train bullish or bearish logic.

🔹 Fitness Function

Each generation evaluates its patterns’ performance using a simulated wallet system. The Profit Factor is computed as:

[ \text{Profit Factor} = \frac{\text{Sum of Profitable Trades}}{\text{Sum of Losing Trades}} ]

The genetic process seeks to maximize this ratio across thousands of simulations.


⚙️ Configuration

📁 Default Dataset Paths

  • Training data: ./Pear2Pear/dataset_1h.csv
  • Testing data: TESTFILEPATH (environment variable)

🔧 Evolution Parameters

Parameter Default Description
population_size 200–500 Number of candidate patterns per generation
mutation_chance 0.015–0.5 Probability of random pattern mutation
refresh_chance 0.015–0.2 Probability of full pattern regeneration
elitism_num adaptive Number of top-performing patterns kept unchanged

🧪 Under the Hood

Pear2Pear implements a multi-stage evolutionary loop consisting of:

  1. Population Initialization – Random patterns are generated based on a configurable schema of indicators.
  2. Fitness Evaluation – Each pattern is applied to historical candle data to simulate gains and losses.
  3. Selection – Top-performing patterns are selected using weighted fitness probabilities.
  4. Crossover & Mutation – Parents are combined and altered to explore new possibilities.
  5. Overfitting Detection – Best candidates are validated on unseen datasets; if overfit, retraining is triggered.
  6. Iteration – The process continues until convergence or a performance plateau.

All key computational components are JIT-compiled using Numba for speed.


🧰 Dependencies

  • Python ≥ 3.9
  • pandas
  • numpy
  • numba
  • multiprocess

Install dependencies automatically with:

pip install -r requirements.txt

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

pear2pear-0.1.6.tar.gz (13.1 kB view details)

Uploaded Source

File details

Details for the file pear2pear-0.1.6.tar.gz.

File metadata

  • Download URL: pear2pear-0.1.6.tar.gz
  • Upload date:
  • Size: 13.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.0

File hashes

Hashes for pear2pear-0.1.6.tar.gz
Algorithm Hash digest
SHA256 a6f797acd7810549e1a9107aa364579c7097eda85cd142bc243610a1bfbac34d
MD5 66f1896431d1f14080f7a4c0f152aefd
BLAKE2b-256 edacd4eb6c3c6119817383c40057771f9ea1736bfaac5d984613373ed987c9fa

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