A Synthetic-Price Example for Checking Backtest Order Mechanics
Summary
This example demonstrates how to run a local backtest with synthetic minute-level prices and a simple strategy. The strategy waits for its first trading iteration and then submits a buy order for one share of a demonstration stock. The backtest uses a small, rising sequence of generated prices, pandas-based data, and a specified intraday time window. It is designed to check that the backtesting engine and order workflow operate in an offline setup.
The example does not use historical market data and is not evidence of a trading strategy’s performance. Its stated purpose is to demonstrate order mechanics, and it disables plots, tearsheets, progress display, and logfile output. It also checks an environment setting to prevent selecting an external data source. The printed portfolio value and trade log are outputs of this synthetic installation check; they should not be interpreted as investment results or a realistic simulation of market execution.
Key ideas
- The example runs a backtest using generated minute-level prices and no external market data.
- The strategy submits a single buy order on its first trading iteration.
- The setup is intended to check the backtest engine and order workflow offline.
- Synthetic price outputs demonstrate mechanics and do not establish historical performance or investment merit.
Tags
Full text
# first_backtest.py
```py
"""Offline installation check using synthetic prices and the real backtest engine.
Run with: python -m lumibot.example_strategies.first_backtest
No credentials, network data, model calls or browser opening are required.
The results demonstrate order mechanics, not an investment opportunity.
"""
from datetime import datetime
import os
import pandas as pd
from lumibot.backtesting import PandasDataBacktesting
from lumibot.entities import Asset, Data
from lumibot.strategies import Strategy
class FirstBacktest(Strategy):
def initialize(self):
self.sleeptime = "1M"
def on_trading_iteration(self):
if self.first_iteration:
self.submit_order(self.create_order("DEMO", 1, "buy"))
def run_example():
override = os.environ.get("BACKTESTING_DATA_SOURCE", "").strip().lower()
if override not in {"", "none"}:
raise ValueError("For this offline check, set BACKTESTING_DATA_SOURCE=none before running.")
asset = Asset("DEMO", Asset.AssetType.STOCK)
prices = [100.0, 101.0, 102.0, 103.0, 104.0, 105.0]
frame = pd.DataFrame(
{"open": prices, "high": prices, "low": prices, "close": prices, "volume": 1000},
index=pd.date_range("2025-01-06 09:30", periods=6, freq="min", tz="America/New_York"),
)
return FirstBacktest.run_backtest(
PandasDataBacktesting,
datetime(2025, 1, 6, 9, 30), datetime(2025, 1, 6, 9, 35),
pandas_data={asset: Data(asset, frame, timestep="minute")},
budget=10_000, benchmark_asset=None, analyze_backtest=False,
show_plot=False, show_tearsheet=False, save_tearsheet=False,
show_indicators=False, save_logfile=False, show_progress_bar=False,
)
if __name__ == "__main__":
_, strategy = run_example()
print("Synthetic installation check complete; not historical performance.")
print(f"Ending portfolio value: {strategy.portfolio_value:.2f}")
print(strategy.broker._trade_event_log_df.to_string(index=False))
```Shown in full with attribution under the source's licence. Licence: GPL-3.0
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.