Skip to main content

A Python backtesting engine for trading strategies, designed for SimTrading platform.

Project description

SimTrading Engine

A robust and flexible Python backtesting engine designed for testing trading strategies with the SimTrading platform. It supports both local execution with your own data and remote execution connected to the SimTrading platform.

Project Structure

The project is organized around a few key components:

src/simtrading/
├── backtest_engine/       # Core backtesting logic
│   ├── broker/            # Broker simulation (validation, liquidation, portfolio updates)
│   ├── entities/          # Data structures (Candle, OrderIntent, Position, etc.)
│   ├── portfolio/         # Portfolio management logic
│   ├── strategy/          # Strategy interface and context
│   └── engine.py          # Main event loop
├── remote/                # Remote execution components (client, provider, exporter)
└── runners/               # Entry points for running backtests (custom and platform)

Core Components

Broker (BacktestBroker)

The broker simulates a real exchange. It is responsible for:

  • Order Validation: Checks if you have enough cash/margin to open a position.
  • Execution: Simulates order execution (currently supports MARKET orders).
  • Liquidation: Monitors your margin level and liquidates positions if the maintenance margin is breached.
  • Portfolio Management: Updates cash and positions based on trades and market price updates.

Engine (BacktestEngine)

The engine orchestrates the backtest. It:

  • Iterates through historical data candle by candle.
  • Updates the portfolio snapshot.
  • Calls your strategy's on_bar method with the current context.
  • Sends your order intents to the broker for execution.
  • Logs all events for analysis.

Usage

1. Installation

pip install simtrading-engine

2. Writing a Strategy

To create a strategy, you must inherit from BaseStrategy and implement the on_bar method.

from simtrading import BaseStrategy
from simtrading import OrderIntent
from simtrading import Side

class MyStrategy(BaseStrategy):
    def on_bar(self, context):
        # This method is called for every timestamp in the backtest
        
        # 1. Access Data 
        # Get the current close price for a symbol
        current_price = context.candle['BTC-USD'].close
        
        # Get historical close prices (e.g., for moving average)
        closes = context.get_series('BTC-USD', 'close', limit=20)
        
        # 2. Check Portfolio
        # Check if we are already long
        if not context.is_long('BTC-USD'):
            # 3. Generate Order Intents
            # Buy 0.1 BTC
            return [
                OrderIntent(
                    symbol='BTC-USD',
                    side=Side.BUY,
                    quantity=0.1,
                    order_type='MARKET'
                )
            ]
        
        return [] # No action

Strategy Context (StrategyContext)

The context object passed to on_bar provides everything you need:

  • Market Data:

    • context.candle: Dictionary of current candles {symbol: Candle}.
    • context.past_candles: Dictionary of historical candles.
    • context.get_series(symbol, field, limit): Helper to get a list of values (e.g., closes).
    • context.current_timestamp(): Current timestamp.
  • Portfolio State:

    • context.cash: Available cash.
    • context.equity: Total portfolio value.
    • context.is_long(symbol) / context.is_short(symbol): Check position direction.
    • context.get_position(symbol): Get detailed position info.

Inputs & Outputs

  • Input: context (StrategyContext)
  • Output: A list of OrderIntent objects.

3. Running a Backtest

Custom Backtest

Run a backtest on your own machine using your own custom data.

from simtrading import run_custom_backtest
from simtrading import Candle

# 1. Prepare Data
# You need a dictionary mapping symbols to lists of Candle objects
candles_data = {
    'BTC-USD': [
        Candle(symbol='BTC-USD', timestamp=1000, date='2023-01-01', open=100, high=110, low=90, close=105, volume=1000),
        # ... more candles
    ]
}

# 2. Run Backtest
run_custom_backtest(
    initial_cash=10000.0,
    strategy=MyStrategy(),
    fee_rate=0.001,           # 0.1% fee
    margin_requirement=1.0,   # 1.0 = no leverage, 0.5 = 2x leverage
    candles_by_symbol=candles_data,
    output_dir="my_results"
)

Custom Backtest with Platform Export

You can run a backtest locally with your own data and automatically export the results to the SimTrading platform for visualization.

run_custom_backtest(
    # ... standard parameters ...
    api_key="your-api-key",
    base_url="https://simtrading.fr",
    export_to_server=True
)

⚠️ Important Note on Visualization: When exporting results from a custom backtest using custom data (e.g., CSV files), the platform will display the Equity Curve, Trade History, and Performance Metrics correctly. However, the Price Chart (candlestick graph) might not be displayed if:

  1. The symbol used (e.g., "MY-CUSTOM-TOKEN") does not exist in the platform's database.
  2. The timeframe used is not supported by the platform.

In these cases, you will still see your PnL evolution and trade list, but the trades will not be overlaid on a price chart.

Platform Backtest

Run a backtest using data and configuration from the SimTrading platform.

from simtrading.runners.platform_runner import run_platform_backtest

run_platform_backtest(
    backtest_id="your-backtest-id",
    api_key="your-api-key",
    base_url="https://simtrading.fr",
    strategy=MyStrategy()
)

Data Structures

Candle

Represents a single bar of market data.

  • symbol: str
  • timestamp: int
  • date: str
  • open, high, low, close: float
  • volume: float

OrderIntent

Represents a request to place an order.

  • symbol: str
  • side: Side.BUY or Side.SELL
  • quantity: float (must be positive)
  • order_type: str (currently only 'MARKET')

PortfolioSnapshot

Represents the state of your portfolio at a specific time.

  • cash: Available liquidity.
  • equity: Net worth (Cash + Unrealized PnL).
  • positions: List of open positions.

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

simtrading_engine-0.2.1.tar.gz (31.1 kB view details)

Uploaded Source

Built Distribution

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

simtrading_engine-0.2.1-py3-none-any.whl (34.4 kB view details)

Uploaded Python 3

File details

Details for the file simtrading_engine-0.2.1.tar.gz.

File metadata

  • Download URL: simtrading_engine-0.2.1.tar.gz
  • Upload date:
  • Size: 31.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for simtrading_engine-0.2.1.tar.gz
Algorithm Hash digest
SHA256 4130c21f43342d22c13470007c879e002a83ff675e8f3a477fc5c10af56178e5
MD5 b6744ea8b3228a06f1ec3dfe844bf0dc
BLAKE2b-256 fd7da8faf088addf35a6afffa7e9a6b43dec1361b3fb54b4e011d6a91982e29d

See more details on using hashes here.

File details

Details for the file simtrading_engine-0.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for simtrading_engine-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b14f40b1c406aa64ace1f2e3876a03d41d143613068e7734993cc5753cb53225
MD5 881ad58786c5094ce23e030deb392afd
BLAKE2b-256 99c4efed28d7b0008c4d9148d74a6ac6e159e6e3230f25f62885a94c51861b0b

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