Converting HalfTrend indicator to TradingView Strategy (Part-2)

Published on 24 May 2025 | 👁️ 362


In Part 1 of this series, we explored the basics of the HalfTrend indicator — a popular trend-following tool available on TradingView — and why it’s suitable for building a rule-based strategy. Now, in Part 2, we move from theory to application. We'll decode its logic, turn it into a backtestable strategy, and evaluate its performance across markets. Finally, we’ll attempt to enhance results using traditional filters — and introduce smarter alternatives when those fall short.

Read the Part 1 Article 

---

🔍 Understanding the HalfTrend Logic

The HalfTrend indicator signals potential trend reversals based on moving averages and price extremes:

📈 Buy Signal:

  • The SMA of lows rises above the highest high

  • The closing price is higher than the previous high

📉 Sell Signal:

  • The SMA of highs drops below the lowest low

  • The closing price is lower than the previous low

The plotted HalfTrend line:

  • Equals maxLowPrice during an uptrend

  • Equals minHighPrice during a downtrend

---

🛠️ Building the Strategy in Pine Script

We convert this logic into a Pine Script strategy:

  1. Open Pine Editor and start a new strategy template

  2. Paste the HalfTrend indicator logic

  3. Use the generated buySignal and sellSignal as longCondition and shortCondition

  4. Add entry logic using strategy.entry()

The basic strategy is now ready for testing

📌 Click here to view the strategy on TradingView

---

📊 Backtesting Across Markets

We tested the basic HalfTrend strategy across five markets and four timeframes from Jan 1, 2023 to Mar 1, 2025:

Markets:

  • NIFTY Futures (NSE India)
  • BANKNIFTY Futures (NSE India)
  • GOLD MINI (MCX India)
  • CRUDE MINI (MCX India)
  • BTCUSD (Binance)

Timeframes: 5M, 15M, 1H, 4H

Evaluation Metrics: Total Trades, Win Rate, Profit Factor, Sharpe Ratio

🔽 Summary of Results — Basic Strategy

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

While GOLD and NIFTY performed well(We have positive result across timeframes), results across other markets were inconsistent, especially in lower timeframes or sideways markets.

---

🎯 Why Filtering Is Necessary

Even though the HalfTrend logic detects trend changes, it struggles in non-trending or choppy conditions, generating many false signals.

This is expected for trend-following systems. Without a trend filter:

  • You trade noise, not momentum

  • False reversals dominate

  • Backtest performance appears unstable

Also, each market behaves differently. One-size-fits-all logic may fail.

Solution? Use filters to validate the market condition before acting on a HalfTrend signal.

---

🧪 Applying Traditional Filters to NIFTY

We added one filter at a time and tested their effect. Note: Only entries are filtered — all trades still exit on the opposite HalfTrend signal.

📘 1. 200 EMA Filter

Logic:

    • Buy only if price > 200 EMA

    • Sell only if price < 200 EMA

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

✅ Reduced noise, with modest improvement in performance.

📘 2. ADX Filter

Using ADX(17,14), requires trend strength and slope to rise

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

✅ Fewer trades, slightly better quality.

📘 3. MACD Filter

MACD line must be above signal line for buys

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 result — improved metrics on higher timeframes.

📘 4. RSI Filter

Trade long only when RSI > 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 slight improvement.

⚠️ Key Takeaway

While no single traditional filter consistently improved performance across all timeframes, that doesn’t mean filters are ineffective.
In fact, the data shows that filters can improve strategy results — but their impact is highly context-dependent.

The real challenge lies in this:

Finding the right filter or combination of filters that aligns with the HalfTrend signal and market condition is critical — and extremely difficult to do manually.

---

🤖 Solving This with Computer Algorithms

To overcome this complexity, we use Computer Algorithms — particularly Genetic Algorithms — to:

  • Automatically search through thousands of filter combinations

  • Evaluate them based on performance metrics (e.g. Profit Factor, Win Rate, Sharpe Ratio)

  • Identify the most promising setups that enhance the HalfTrend signal

This approach allows us to objectively discover high-performing filter structures that would be nearly impossible to test manually.

✅ Example Result (Using Genetic Algorithm)

We applied this method on NIFTY 15-minute timeframe, focusing on filter optimization and signal refinement.

  • Trades: 95

  • Win Rate: 50.53%

  • Profit Factor: 2.01

  • Sharpe Ratio: 0.38

---

 📌 View the advanced optimized strategy

 📈 See the full performance report here

⚠️ Note: This is a research strategy, meant to showcase the power of algorithmic optimization — not yet recommended for live trading.

⚠️ Curve-Fitting Warning

Machine learning and algorithmic filters can sometimes produce overfitted results — systems that perform well in backtests but fail in live trading.

To avoid this, you must perform:

  • Out-of-sample testing

  • Walk-forward analysis

  • Monte Carlo simulations

We'll explore these topics next.

---

🔮 What’s Coming in Part 3

Read The Trader’s Mindset: Building a Robust Foundation Before Going Live(Bonus Article – Before Deploying Our Strategy)

In the final part of this series, we’ll:

  • Explore robustness testing

  • Analyze live deployment readiness

  • Create a multi-strategy portfolio

  • Deploy a strategy for live tracking

---

👉 Stay tuned and subscribe to follow the full journey from TradingView script to robust, trade-ready system.