Converting the HalfTrend Indicator into a TradingView Strategy (Part 2)

HalfTrend turned into a Pine Script strategy, tested on 5 markets and 4 timeframes, then improved with filters. Full results, including where it failed.

Tanay Roy · 24 May 2025 · Updated 5 October 2026 · 7 min read

Updated October 2026. The original ending of this post looked ahead to live deployment. The series now stops where a careful trader should: at testing. The full report of the optimised strategy is in Half Trend Strategy on NIFTY.

In Part 1 we picked the HalfTrend indicator, a popular trend-following tool on TradingView.

In this part we decode its logic, turn it into a strategy, and test it across markets. Then we try to improve it with filters, and see where that helps and where it doesn’t.

How HalfTrend decides

HalfTrend signals a trend change from moving averages and price extremes.

Buy signal

  • The SMA of lows rises above the highest high
  • The close is above the previous high

Sell signal

  • The SMA of highs drops below the lowest low
  • The close is below the previous low

The plotted HalfTrend line equals maxLowPrice in an uptrend and minHighPrice in a downtrend.

Building the strategy in Pine Script

  1. Open the Pine Editor and start a new strategy template.
  2. Paste in the HalfTrend indicator logic.
  3. Use its buySignal and sellSignal as the long and short conditions.
  4. Add entries with strategy.entry().

Every trade exits on the opposite HalfTrend signal.

The basic strategy is ready to test: HalfTrend Strategy Basic on TradingView.

Testing across markets

I tested the basic strategy on five markets and four timeframes, from 1 January 2023 to 1 March 2025:

  • Markets: NIFTY futures and BANKNIFTY futures (NSE), GOLD MINI and CRUDE MINI (MCX), BTCUSD (Binance)
  • Timeframes: 5 minutes, 15 minutes, 1 hour, 4 hours
  • Measured: number of trades, win rate, profit factor, Sharpe ratio

NIFTY

Timeframe Trades Win rate (%) Profit factor Sharpe ratio
5M 1928 37.76 1.175 0.453
15M 649 37.29 1.213 0.29
H1 200 36 1.406 0.296
H4 52 40.38 1.276 0.248

BANKNIFTY

Timeframe Trades Win rate (%) Profit factor Sharpe ratio
5M 1907 34.98 0.985 -0.011
15M 621 38.65 1.167 0.33
H1 201 37.31 1.099 0.165
H4 28 35.71 0.65 0.317

GOLD

Timeframe Trades Win rate (%) Profit factor Sharpe ratio
5M 5618 36.15 1.11 0.485
15M 1701 36.80 1.232 0.648
H1 390 42.31 1.728 0.647
H4 110 44.55 1.741 0.332

CRUDE

Timeframe Trades Win rate (%) Profit factor Sharpe ratio
5M 4854 34.07 0.972 -0.209
15M 1565 35.14 0.991 -0.1
H1 411 38.44 1.121 0.084
H4 123 32.52 0.807 -0.256

BTCUSD

Timeframe Trades Win rate (%) Profit factor Sharpe ratio
5M 14125 34.51 1 -0.022
15M 4113 33.26 0.972 -0.159
H1 924 34.96 0.925 -0.278
H4 204 35.78 1.195 0.137

What this shows: NIFTY and GOLD had a profit factor above 1 on every timeframe. BANKNIFTY was mixed. CRUDE and BTCUSD mostly lost money, especially on lower timeframes.

The same logic behaves very differently from one market to the next.

Why filters are needed

HalfTrend detects trend changes. In choppy, sideways markets it keeps detecting changes that aren’t there.

Without a filter:

  • you trade noise instead of trend,
  • false reversals dominate, and
  • results swing from one period to the next.

The idea of a filter is simple: check the market condition first, and only act on a HalfTrend signal when it agrees.

Testing traditional filters on NIFTY

I added one filter at a time. Only entries are filtered; every trade still exits on the opposite HalfTrend signal.

1. 200 EMA filter

Buy only when price is above the 200 EMA; sell only when it’s below.

Timeframe Trades Win rate (%) Profit factor Sharpe ratio
5M 979 40.04 1.301 0.525
15M 325 40.92 1.318 0.29
H1 95 33.68 1.27 0.215
H4 22 50 1.654 0.242

Less noise, and a modest improvement.

2. ADX filter

ADX(17, 14): trade only when trend strength is present and rising.

Timeframe Trades Win rate (%) Profit factor Sharpe ratio
5M 198 41.41 1.444 0.243
15M 70 40 1.455 0.165
H1 18 16.67 0.251 -0.538
H4 8 50 2.462 0.21

Far fewer trades. Better quality on some timeframes, but the samples become too small to rely on.

3. MACD filter

Buy only when the MACD line is above its signal line.

Timeframe Trades Win rate (%) Profit factor Sharpe ratio
5M 1482 36.98 1.093 0.218
15M 476 36.76 1.236 0.347
H1 144 36.81 1.474 0.291
H4 21 28.57 0.459 -0.306

Mixed: better on 15-minute and 1-hour, worse on 4-hour.

4. RSI filter

Go long only when RSI is above 50.

Timeframe Trades Win rate (%) Profit factor Sharpe ratio
5M 1528 37.7 1.206 0.45
15M 505 36.63 1.126 0.21
H1 92 25 0.771 -0.185
H4 25 32 0.417 -0.349

Only a slight improvement on 5-minute, and worse on higher timeframes.

What the filter tests tell us

No single traditional filter improved results on every timeframe.

That doesn’t mean filters don’t work. It means their effect depends heavily on the market and the timeframe.

Finding the right filter, or combination of filters, by hand is slow and very easy to get wrong.

Searching for filters with an algorithm

To search more widely, I used a genetic algorithm. It:

  • tried thousands of filter combinations automatically,
  • scored each on profit factor, win rate and Sharpe ratio, and
  • kept the combinations that improved the HalfTrend signal most.

On NIFTY 15-minute, the best combination it found gave:

Basic strategy Optimised filters
Trades 649 95
Win rate 37.29% 50.53%
Profit factor 1.213 2.01
Sharpe ratio 0.29 0.38

The optimised script: HalfTrend Strategy Advanced on TradingView.

Why this result needs more testing

A search over thousands of combinations will always find something that looks good on the data it searched. That is overfitting, and it’s the most common way a great backtest fails in live trading.

The optimised version also trades far less: 95 trades instead of 649. Small samples make lucky results look like skill.

Before trusting a result like this, it needs:

  • out-of-sample testing on data the search never saw,
  • walk-forward analysis,
  • Monte Carlo simulation, and
  • the best trades removed, to see what’s left.

I look at the full report of this optimised strategy, and five specific reasons to be careful, in Half Trend Strategy on NIFTY: a backtest that looks great, and why I don’t trust it yet.

Next in the series

Educational content only, not investment advice. The author is not a SEBI-registered investment adviser or research analyst. Backtest results are hypothetical and do not guarantee future performance.

Follow along for new scripts and research: TradingView · YouTube