OPEN-SOURCE SCRIPT
Single AHR DCA (HM) — AHR Pane (customized quantile)

Customized note
1. Price series & windows
2. Harmonic moving average (HM)
3. Log-regression baseline (LR)
4. HM-based AHR (valuation ratio)
5. Empirical quantile thresholds (Opp / Risk)
6. DCA simulation (lightweight, rolling window)
- []The log-regression window LR length controls how long a long-term fair value path is estimated from historical data.
[]The AHR window AHR window length controls over which historical regime you measure whether the coin is “cheap / expensive”. - When you choose a log-regression window of length L (years) and an AHR window of length A (years), you can intuitively read the indicator as:
“Within the last A years of this regime, relative to the long-term trend estimated over the same A years, the current price is cheap / neutral / expensive.”
[]Guidelines:- []In general, set the AHR window equal to or slightly longer than the LR window:
- []If the AHR window is much longer than LR, you mix different baselines (different LR regimes) into one distribution.
[]If the AHR window is much shorter than LR, quantiles mostly reflect a very local slice of history.
[]For major altcoins (BNB / SOL / XRP and similar high-beta assets), it is recommended to use equal or slightly shorter horizons, e.g. LR ≈ 2–3 years, AHR window ≈ 2–3 years. - []If the AHR window is much longer than LR, you mix different baselines (different LR regimes) into one distribution.
- []In general, set the AHR window equal to or slightly longer than the LR window:
1. Price series & windows
- []Working timeframe: daily (1D).
[]Let the daily close of the current symbol on day t be P_t.
[]Main length parameters:- []HM window: L_HM = maLen (default 200 days)
[]Log-regression window: L_LR = lrLen (default 1095 days ≈ 3 years)
[]AHR window (regime window): W = windowLen (default 1095 days ≈ 3 years)
- []HM window: L_HM = maLen (default 200 days)
2. Harmonic moving average (HM)
- On a window of length L_HM, define the harmonic mean:
HM_t = [ (1 / L_HM) * sum_{i=0..L_HM-1} ( 1 / max(P_{t-i}, eps) ) ]^(-1)
[]Here eps = 1e-10 is used to avoid division by zero.
[]Intuition: HM is more sensitive to low prices – an extremely low price inside the window will drag HM down significantly.
3. Log-regression baseline (LR)
- []On a window of length L_LR, perform a linear regression on log price:
[]Over the last L_LR bars, build the series
x_k = log( max(P_k, eps) ), for k = t-L_LR+1 ... t, and fit
x_k ≈ a + b * k.
The fitted value at the current index t is
log_P_hat_t = a + b * t.
Exponentiate to get the log-regression baseline:
LR_t = exp( log_P_hat_t ). - Interpretation: LR_t is the long-term trend / fair value path of the current regime over the past L_LR days.
4. HM-based AHR (valuation ratio)
- At each time t, build an HM-based AHR (valuation multiple):
AHR_t = ( P_t / HM_t ) * ( P_t / LR_t )
[]Interpretation:- []P_t / HM_t : deviation of price from the mid-term HM (e.g. 200-day harmonic mean).
[]P_t / LR_t : deviation of price from the long-term log-regression trend.
[]Multiplying them means:- []if price is above both HM and LR, “expensiveness” is amplified;
[]if price is below both, “cheapness” is amplified.
- []if price is above both HM and LR, “expensiveness” is amplified;
- []AHR_t < 1 : price is below both mid-term mean and long-term trend → statistically cheaper.
- AHR_t > 1 : price is above both mid-term mean and long-term trend → statistically more expensive.
- []P_t / HM_t : deviation of price from the mid-term HM (e.g. 200-day harmonic mean).
5. Empirical quantile thresholds (Opp / Risk)
- On each new day, whenever AHR_t is valid, add it into a rolling array:
A_t_window = { AHR_{t-W+1}, ..., AHR_t } (at most W = windowLen elements)
[]On this empirical distribution, define two quantiles:
[]Opportunity quantile: q_opp (default 15%)
[]Risk quantile: q_risk (default 65%)
[]Using standard percentile computation (order statistics + linear interpolation), we get:
Opp threshold:
theta_opp = Percentile( A_t_window, q_opp )
Risk threshold:
theta_risk = Percentile( A_t_window, q_risk ) - We also compute the percentile rank of the current AHR inside the same history:
q_now = PercentileRank( A_t_window, AHR_t ) ∈ [0, 100]
[]This yields three valuation zones:
[]Opportunity zone: AHR_t <= theta_opp
(corresponds to roughly the cheapest ~q_opp% of historical states in the last W days.)
[]Neutral zone: theta_opp < AHR_t < theta_risk
[]Risk zone: AHR_t >= theta_risk
(corresponds to roughly the most expensive ~(100 - q_risk)% of historical states in the last W days.) - All quantiles are purely empirical and symbol-specific: they are computed only from the current asset’s own history, without reusing BTC thresholds or assuming cross-asset similarity.
6. DCA simulation (lightweight, rolling window)
- []Given:
[]a daily budget B (input: budgetPerDay), and
[]a DCA simulation window H (input: dcaWindowLen, default 900 days ≈ 2.5 years),
[]The script applies the following rule on each new day t:
[]If thresholds are unavailable or AHR_t > theta_risk
→ classify as Risk zone → buy = 0
[]If AHR_t <= theta_opp
→ classify as Opportunity zone → buy = 2B (double size)
[]Otherwise (Neutral zone)
→ buy = B (normal DCA)
[]Daily invested cash:
C_t ∈ {0, B, 2B} - Daily bought quantity:
DeltaQ_t = C_t / P_t
[]The script keeps rolling sums over the last H days:
[]Cumulative position:
Q_H = sum_{k=t-H+1..t} DeltaQ_k
Cumulative invested cash:
C_H = sum_{k=t-H+1..t} C_k
Current portfolio value:
PortVal_t = Q_H * P_t
Cumulative P&L:
PnL_t = PortVal_t - C_H
Active days:
number of days in the last H with C_k > 0. - These results are only used to visualize how this AHR-quantile-driven DCA rule would have behaved over the recent regime, and do not constitute financial advice.
オープンソーススクリプト
TradingViewの精神に則り、このスクリプトの作者はコードをオープンソースとして公開してくれました。トレーダーが内容を確認・検証できるようにという配慮です。作者に拍手を送りましょう!無料で利用できますが、コードの再公開はハウスルールに従う必要があります。
免責事項
この情報および投稿は、TradingViewが提供または推奨する金融、投資、トレード、その他のアドバイスや推奨を意図するものではなく、それらを構成するものでもありません。詳細は利用規約をご覧ください。
オープンソーススクリプト
TradingViewの精神に則り、このスクリプトの作者はコードをオープンソースとして公開してくれました。トレーダーが内容を確認・検証できるようにという配慮です。作者に拍手を送りましょう!無料で利用できますが、コードの再公開はハウスルールに従う必要があります。
免責事項
この情報および投稿は、TradingViewが提供または推奨する金融、投資、トレード、その他のアドバイスや推奨を意図するものではなく、それらを構成するものでもありません。詳細は利用規約をご覧ください。