Customizable Backtest Start Time Strategy
The purpose of this strategy is to allow users to customize the start time of backtesting for more flexible and customizable backtesting.
Overview
The purpose of this strategy is to allow users to customize the start time of backtesting for more flexible and customizable backtesting.
Strategy Logic
This strategy uses Pine Script's time and timestamp functions to implement a customizable backtest start time.
It first allows users to input a customized backtest start year, month, date, hour, and minute in the settings. It then uses these inputs to generate a timestamp and stores it in the startTime variable.
In the strategy's condition check, it adds a new startTime condition. The strategy will only start when the current time is greater than or equal to startTime.
For example:
longCondition = crossover(sma(close, 14), sma(close, 28))
if (longCondition and startTime)
strategy.entry("My Long Entry Id", strategy.long)
This allows implementing a customizable backtest start time. Users can flexibly configure the start time of backtesting instead of being limited to hardcoded times.
Advantage Analysis
This customizable backtest start time strategy has the following advantages:
More Flexible: Users can fully customize the backtest start time instead of being limited to a fixed point in time.
More Realistic: The start time can be set to the actual runtime of the strategy, making the backtest more realistic.
Convenient for Event-driven Backtesting: The start time can be set based on the occurrence time of an event for backtesting specific events.
Easy Condition Adjustment: The backtest start conditions can be easily adjusted for targeted backtesting of different stages.
Repeatable and Reliable: Parameterizing the backtest start time allows repeatable and reliable backtest results.
Risk Analysis
Using a customizable backtest start time also has some risks:
Results Depend on Start Time: Different start times may lead to very different backtest results.
Start Time Needs Careful Selection: Unreasonable start times may cause distortion in backtest results.
Increased Curve Fitting Risk: Easily overfit by adjusting the start time to historical data.
Reduced Comparability: The results of this strategy are less comparable to fixed start time backtests.
Solutions:
Backtest multiple times to evaluate the impact of start time changes on results.
Choose significant event times as start times to minimize distortion.
Carefully adjust start times to avoid overfitting historical data.
Keep fixed start time backtests as benchmark for comparison with customized backtests.
Optimization Directions
This customizable backtest start time strategy can also be improved in the following aspects:
Support customization of both start and end times for full flexible configuration of the backtest time window.
Support multiple time modes: specific dates, relative dates, event-driven, etc. for smarter and more convenient time configuration.
Support graphical configuration interface for more intuitive time parameter setting.
Support configuration of different time granularities: year, month, day, hour, minute, second, etc.
Record backtest time configuration for reproducible, traceable, and comparable results.
Add validation of improper time configurations to avoid low-quality backtests due to unreasonable time settings.
Provide start time binding to easily synchronize start times across multiple strategies.
Summary
This strategy enables customizable and flexible configuration of backtest start times to reduce limitations and make backtests more realistic. But the dependency of results on start times needs to be watched out for using multiple backtests, event-driven models, etc. to reduce distortion. This strategy also has many directions for improvement to achieve smarter and more convenient backtest time configuration in the future.
Parameters
| Name | Default | What it controls |
|---|---|---|
| v_input_1 | 2018 | Start Year |
| v_input_2 | true | Start Month |
| v_input_3 | true | Start Day |
| v_input_4 | false | Start Hour |
| v_input_5 | false | Start Minute |
Source Pine Script
/*backtest
start: 2022-09-19 00:00:00
end: 2023-09-25 00:00:00
period: 2d
basePeriod: 1d
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=3
strategy("C320up Strategy Tester Start Time", overlay = true)
// Copy and paste below into your strategy
// Strategy Tester Start Time
xYear = input(2018, title = "Start Year")
xMonth = input(01, title = "Start Month", minval = 01, maxval = 12)
xDay = input(01, title = "Start Day", minval = 01, maxval = 31)
xHour = input(00, title = "Start Hour", minval = 00, maxval = 23)
xMinute = input(00, title = "Start Minute", minval = 00, maxval = 59)
startTime = time >= timestamp(xYear, xMonth, xDay, xHour, xMinute)
// End copy and paste
// Add (and startTime) at the end of your condition/s to activate
// The strategy below is just an example
longCondition = crossover(sma(close, 14), sma(close, 28))
if (longCondition and startTime)
strategy.entry("My Long Entry Id", strategy.long)
shortCondition = crossunder(sma(close, 14), sma(close, 28))
if (shortCondition and startTime)
strategy.entry("My Short Entry Id", strategy.short)
// Happy trading!
Published by ChaoZhang on the FMZ strategy square · last modified 2023-09-26. Collected in the Quant Trading Vault. Unvalidated research: re-test with your own data and costs before trusting it.
Build this with AgenKit
A spec is not a trading system. Paste this into your coding agent and AgenKit runs a specialist team — architecture, test-first build, risk gates, review — with paper trading on by default.
/agenkit build a production-grade trading bot with a backtest engine, risk controls and paper trading by default from strategies/Customizable-Backtest-Start-Time-Strategy.md- npx agenkit install engineering-kit in your project
- Paste the command into your agent and approve each phase
- Get tests, backtest, risk controls and a paper-trading switch
Understand it first
Before you run this, read the lesson behind the idea and how to backtest it honestly. Specs like this one are the survivors of an unknown number of trials; deflate their results accordingly.
Related specs
- Angle Attack Follow Line IndicatorPine Script · Other · 15m
- Gann High LowPine Script · Other · 4h
- MACD StrategyPine Script · Other
- Monthly Performance PnL Calendar StrategyPine Script · Other · 1d
- QQE MOD SSL Hybrid Waddah Attar ExplosionPine Script · Other · 4h
- RedK Volume Accelerated Directional Energy RatioPine Script · Other · 1h