Breakthrough Highs and Lows Backtesting Strategy

Author: ChaoZhang, Date: 2024-01-08 16:13:44
Tags:

img

Overview

This strategy judges trend direction based on breaking through periodic highs and lows. It goes long when price breaks through the periodic high and goes short when price breaks below the periodic low. It belongs to the trend tracking strategy.

Principle

The strategy first reads the user-defined cycle (daily, weekly, etc.) and lookback periods. Then it gets the highest and lowest prices for the lookback period based on these parameters. For example, if it is set to daily cycle and lookback 1 period, it takes the highest and lowest prices for the previous day.

In actual trading, if the closing price is greater than or equal to the lowest price of the lookback period, it is judged as an upward breakthrough and it goes long. If the closing price is less than or equal to the highest price of the lookback period, it is judged as a downward breakthrough and it goes short.

By capturing the trend direction through breaking through periodic highs and lows, this strategy belongs to a kind of trend tracking strategy.

Advantage Analysis

The main advantages of this strategy are:

  1. Capturing the big trend after strong consolidation by judging direction based on breakthrough points.

  2. Simple and easy to understand, very suitable for beginners to learn and use.

  3. Easy to optimize by adjusting periodic parameters, applicable to different varieties.

  4. Can set reverse input for reverse operation, enriching strategy use.

  5. Drawing periodic highs and lows to assist judgement and form multi-validation.

Risk Analysis

There are also some risks:

  1. Cannot effectively filter sideways volatility, possible multiple mis-operations.

  2. Cannot control stop loss, certain degree of loss risk exists.

  3. Sensitive to trading costs, actual PnL may deviate.

  4. Cannot limit position size, over-trading risk exists.

To address these risks, methods like setting stop loss, optimizing filter conditions, controlling position size can be used.

Optimization

The main optimization directions are:

  1. Adding filter mechanisms to avoid frequent opening during sideways. Set price channel, volatility filters etc.

  2. Set trailing stop loss or time stop loss to control single loss risk and ensure overall profitability.

  3. Optimize position sizing and money management to prevent over-trading and ensure stability.

  4. Test effects of different periodic parameters and select optimal parameter combinations.

  5. Increase algorithmic trading modules, use machine learning algorithms to improve decision efficiency.

Summary

In summary, this breakthrough high low backtest strategy is simple to operate based on trend tracking, suitable for beginners to learn, but risks being trapped exist. By adding optimizations like filters, stops, position control, these risks can be reduced and strategy results improved. It can provide ideas and references for our further research and improvements.


/*backtest
start: 2023-01-01 00:00:00
end: 2024-01-07 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=2
////////////////////////////////////////////////////////////
//  Copyright by HPotter v1.0 03/07/2018
// This script shows a high and low period value.
//    Width - width of lines
//    SelectPeriod - Day or Week or Month and etc.
//    LookBack - Shift levels 0 - current period, 1 - previous and etc.
//
// You can change long to short in the Input Settings
// WARNING:
// - For purpose educate only
// - This script to change bars colors.
////////////////////////////////////////////////////////////
strategy(title="High and Low Levels Backtest", shorttitle="HL Levels", overlay = true)
SelectPeriod = input("D", defval="D")
LookBack = input(1,  minval=0)
reverse = input(false, title="Trade reverse")
xHigh  = request.security(syminfo.tickerid, SelectPeriod, high[LookBack])
xLow   = request.security(syminfo.tickerid, SelectPeriod, low[LookBack])
vS1 = xHigh
vR1 = xLow
pos = iff(close > vR1, 1,
       iff(close < vS1, -1, nz(pos[1], 0))) 
possig = iff(reverse and pos == 1, -1,
          iff(reverse and pos == -1, 1, pos))	   
if (possig == 1) 
    strategy.entry("Long", strategy.long)
if (possig == -1)
    strategy.entry("Short", strategy.short)	   	    
barcolor(possig == -1 ? red: possig == 1 ? green : blue ) 

More