Skip to content

backtest usage

Benny Thadikaran edited this page Oct 28, 2024 · 8 revisions

backtest.py is a visual backtesting tool for scanning historical patterns.

It offers a broader perspective on the historical conditions leading up to the pattern's formation and how that pattern subsequently unfolded.

It can be used to compare, evaluate and study patterns more effectively.

m&m backtest result

At minimum, you must specify

  • A date to calculate all other periods. (Using -d or --date option)
  • A pattern name to scan. (Using -p or --pattern option)

Basic algorithm

The total data required is approximately 310 candles.

  • 30 candles for the scan period,
  • 160 candles before the scan period - Look back period,
  • 120 candles after the scan period - look ahead period.

These periods are backwards calculated starting from the date provided by the user.

  • Scan end date starts 120 candles from the user provided date.
  • Scan start date is 30 candles from the scan end date.

For each candle in the scan period,

  • We take the slice of last 160 candles (including the current candle).
  • Scan for any patterns ending on the current candle.
  • Patterns are tracked, so if the same pattern is detected in subsequent candles it is discarded (to avoid duplication).

This is equivalent to running the below code for every date in the scan period

py init.py -p vcpu -d 2024-08-15

  • If a pattern is detected, it plots the chart, starting 160 candles prior to the pattern and ending 120 days after the pattern.

Basic Usage:

Ensure you have user.json setup.

Use -p or --pattern to specify the pattern name.

Use -d or --date to specify the date. Date must be in ISO format YYYY-MM-DD or YYYY-MM-DDTHH:MM.

  • If you provide date as 2024-10-23 it is interpreted as 2024-10-23T00:00.
  • To include the data for 23rd Oct provide a day forward date like 2024-10-24 or specify the time 2024-10-23T11:59 for more granular precision.

Both options are required to run the scan.

py backtest.py -p vcpu -d 2024-05-07

Custom Watchlist

By default, it reads the SYM_LIST from user.json to get the watchlist.

To provide a custom watchlist of symbols as a CSV or TXT file. use the --file or -f with a filepath.

py backtest.py -p hnsd -d 2024-05-07 -f symlist.csv

Provide a list of symbols to scan

You can use the --sym with a list of symbols (space separated)

py backtest.py -p vcpu -d 2024-10-21 --sym tcs infy marksans

Set Scan parameters

By default, scan period is 30 candles. You can override it with --period

py backtest.py -p trng -d 2023-12-01 --period 60

Set timeframe

You can also specify the timeframe using --tf option.

py backtest.py -p trng --d 2023-12-01 --tf weekly

If configured for intraday data, you can pass an intraday timeframe as well.

py backtest.py -p hnsd -d 2024-05-11 --tf 60


For IEODFileLoader, valid values for DEFAULT_TF are 1, 5, 10, 15, 25, 30, 60, 75, 125, 2h, 4h.

For EODFileLoader, valid values for DEFAULT_TF are daily, weekly, monthly, quarterly.

Help

Use -h to see help

py backtest.py -h

usage: backtest.py [-h] [-c filepath] [--tf TF] [-p str] [--idx IDX] [-d DATE] [--period PERIOD] (--plot PLOT | -f FILE | --sym SYM [SYM ...])

Run backdated pattern scan

options:
  -h, --help            show this help message and exit
  -c filepath, --config filepath
                        Custom config file
  --tf TF               Timeframe string.
  -p str, --pattern str
                        Pattern to test. One of vcpu, vcpd, dbot, dtop, hnsu, hnsd, trng, uptl, dntl, abcdu, abcdd, batu, batd
  --idx IDX             Index to plot
  -d DATE, --date DATE  Last available date on file. The scan end date is 120 periods prior to this date. Scan start date is 30 days from the scan
                        end date. Use ISO FORMAT date YYYY-MM-DD.
  --period PERIOD       Scan Period, prior to ending date. Default 30
  --plot PLOT           Plot results from json file
  -f FILE, --file FILE  Filepath with symbol list. One on each line
  --sym SYM [SYM ...]   Space separated list of stock symbols.