OPEN-SOURCE SCRIPT
Hodrick-Prescott filter [Jamallo]

(2025)
Intro
The Hodrick-Prescott (HP) filter is a tool originally developed by economists to remove short-term noise from data and reveal the underlying long-term trend — think of it like a moving average, but mathematically smarter.
Instead of just averaging recent prices, it solves for the smoothest possible trendline that still follows the data. Applied to price, it produces a cleaner baseline than most traditional moving averages.
---
However it comes with 3 problems in practical day trading.
Problem #1 Standard HP Repaints History (core issue)
The standard HP filter is non-causal — the value at any point in time depends on future values CEPR, meaning past signals can change retroactively.
How I solved it:
Switching to a one-sided implementation fixes this — it only looks at past data, so history never changes once a bar closes.
Problem #2 One-Sided HP Hooks at Turns
Going one-sided introduces an endpoint artifact — near turning points the trend line briefly curves in the wrong direction before reversing, producing a false signal.
How I solved it:
Feeding the HP output into a range filter solves this. The line only moves when HP breaks outside a volatility band, so the false hook gets absorbed into a flat zone and never fires a signal.
Problem #3 Fixed Band Would Be Too Rigid
A fixed band would whipsaw in volatile markets and lag in calm ones.
How I solved it:
Instead the band is sized dynamically using a percentile of True Range, scaling automatically with current market conditions.
Intro
The Hodrick-Prescott (HP) filter is a tool originally developed by economists to remove short-term noise from data and reveal the underlying long-term trend — think of it like a moving average, but mathematically smarter.
Instead of just averaging recent prices, it solves for the smoothest possible trendline that still follows the data. Applied to price, it produces a cleaner baseline than most traditional moving averages.
---
However it comes with 3 problems in practical day trading.
Problem #1 Standard HP Repaints History (core issue)
The standard HP filter is non-causal — the value at any point in time depends on future values CEPR, meaning past signals can change retroactively.
How I solved it:
Switching to a one-sided implementation fixes this — it only looks at past data, so history never changes once a bar closes.
Problem #2 One-Sided HP Hooks at Turns
Going one-sided introduces an endpoint artifact — near turning points the trend line briefly curves in the wrong direction before reversing, producing a false signal.
How I solved it:
Feeding the HP output into a range filter solves this. The line only moves when HP breaks outside a volatility band, so the false hook gets absorbed into a flat zone and never fires a signal.
Problem #3 Fixed Band Would Be Too Rigid
A fixed band would whipsaw in volatile markets and lag in calm ones.
How I solved it:
Instead the band is sized dynamically using a percentile of True Range, scaling automatically with current market conditions.
สคริปต์โอเพนซอร์ซ
ด้วยเจตนารมณ์หลักของ TradingView ผู้สร้างสคริปต์นี้ได้ทำให้เป็นโอเพนซอร์ส เพื่อให้เทรดเดอร์สามารถตรวจสอบและยืนยันฟังก์ชันการทำงานของมันได้ ขอชื่นชมผู้เขียน! แม้ว่าคุณจะใช้งานได้ฟรี แต่โปรดจำไว้ว่าการเผยแพร่โค้ดซ้ำจะต้องเป็นไปตาม กฎระเบียบการใช้งาน ของเรา
Join the Growing (Econophysics Trading) Community! ⚛️
discord.com/invite/qb4RKFdwYJ
Trade the physics of price.
discord.com/invite/qb4RKFdwYJ
Trade the physics of price.
คำจำกัดสิทธิ์ความรับผิดชอบ
ข้อมูลและบทความไม่ได้มีวัตถุประสงค์เพื่อก่อให้เกิดกิจกรรมทางการเงิน, การลงทุน, การซื้อขาย, ข้อเสนอแนะ หรือคำแนะนำประเภทอื่น ๆ ที่ให้หรือรับรองโดย TradingView อ่านเพิ่มเติมใน ข้อกำหนดการใช้งาน
สคริปต์โอเพนซอร์ซ
ด้วยเจตนารมณ์หลักของ TradingView ผู้สร้างสคริปต์นี้ได้ทำให้เป็นโอเพนซอร์ส เพื่อให้เทรดเดอร์สามารถตรวจสอบและยืนยันฟังก์ชันการทำงานของมันได้ ขอชื่นชมผู้เขียน! แม้ว่าคุณจะใช้งานได้ฟรี แต่โปรดจำไว้ว่าการเผยแพร่โค้ดซ้ำจะต้องเป็นไปตาม กฎระเบียบการใช้งาน ของเรา
Join the Growing (Econophysics Trading) Community! ⚛️
discord.com/invite/qb4RKFdwYJ
Trade the physics of price.
discord.com/invite/qb4RKFdwYJ
Trade the physics of price.
คำจำกัดสิทธิ์ความรับผิดชอบ
ข้อมูลและบทความไม่ได้มีวัตถุประสงค์เพื่อก่อให้เกิดกิจกรรมทางการเงิน, การลงทุน, การซื้อขาย, ข้อเสนอแนะ หรือคำแนะนำประเภทอื่น ๆ ที่ให้หรือรับรองโดย TradingView อ่านเพิ่มเติมใน ข้อกำหนดการใช้งาน