# HalfTrend

This study calculates and displays a HalfTrend study of the Price Data.

Let $$H$$, $$L$$, and $$C$$ be random variables denoting the High, Low, and Close Prices, respectively, and let $$H_t$$, $$L_t$$, and $$C_t$$ be their respective values at Index $$t$$. Let the Inputs Length, ATR Length, and Channel Deviation be denoted as $$n$$, $$n_{ATR}$$, and $$CD$$, respectively.

We begin by defining the following functions.

• Highest High Price: $$\max_t(H,n)$$ - See documentation for Moving Maximum
• Lowest Low Price: $$\min_t(L,n)$$ - See documentation for Moving Minimum
• Simple Moving Average of High Price: $$SMA_t(H,n)$$
• Simple Moving Average of Low Price: $$SMA_t(L,n)$$
• Average True Range: $$ATR_t(n_{ATR})$$
• Next Trend: $$NextTrend_t(n) = NextTrend_{t - 1}(n)$$
• Trend: $$Trend_t(n) = Trend_{t - 1}(n)$$
• Min High Price: $$H^{(min)}_t(n) = H^{(min)}_{t - 1}(n)$$
• Max Low Price: $$L^{(max)}_t(n) = L^{(max)}_{t - 1}(n)$$
• Arrow Up: $$ArrowUp_t(n,n_{ATR}) = ArrowUp_{t - 1}(n,n_{ATR})$$
• Arrow Down: $$ArrowDown_t(n,n_{ATR}) = ArrowDown_{t - 1}(n,n_{ATR})$$

The Average True Range is computed using an Exponential Moving Average by default. This can be changed by appropriately setting the ATR Moving Average Type Input.

We make the following initializations at $$t = 0$$.

• $$Trend_0(n) = 0$$
• $$NextTrend_0(n) = 0$$
• $$H^{(min)}_0(n) = H_0$$
• $$L^{(max)}_0(n) = L_0$$
• HalfTrend_0(n,n_{ATR},CD) = C_0

$$HalfTrend_t(n,n_{ATR},CD)$$ is the main Subgraph in this study.

For $$t \geq 0$$, we execute an elaborate series of calculations as follows.

• If $$NextTrend_t(n) = 1$$:
• $$L^{(max)}_t(n) = \max\{\min_t(L,n), L^{(max)}_t(n)\}$$
• If $$SMA_t(H,n) < L^{(max)}_t(n)$$ and $$C_t < L_{t - 1}$$
• $$Trend_t(n) = 1$$
• $$NextTrend_t(n) = 0$$
• $$H^{(min)}_t(n) = \max_t(H,n)$$

• If $$NextTrend_t(n) = 0$$:
• $$H^{(min)}_t(n) = \max\{\max_t(H,n), H^{(min)}_t(n)\}$$
• If $$SMA_t(L,n) > H^{(min)}_t(n)$$ and $$C_t > H_{t - 1}$$
• $$Trend_t(n) = 0$$
• $$NextTrend_t(n) = 1$$
• $$L^{(max)}_t(n) = \min_t(L,n)$$

In the next block of calculations we introduce four new functions: Up, Down, ATR Channel High, and ATR Channel Low, denoted respectively as $$Up_t(n)$$, $$Down_t(n)$$, $$ATR^{(High)}_t(n, n_{ATR}, CD)$$, and $$ATR^{(Low)}_t(n, n_{ATR}, CD)$$.

• If $$Trend_t(n) = 0$$:
• If $$Trend_{t - 1} = 1$$
• $$Up_t(n) = Down_{t - 1}(n)$$
• $$ArrowUp_t(n,n_{ATR}) = Up_t(n) - \frac{1}{2}ATR_t(n_{ATR})$$
• If $$Trend_{t - 1} = 0$$
• $$Up_t(n) = \max\{L^{(max)}_t(n), Up_{t - 1}(n)\}$$
• $$ATR^{(High)}_t(n, n_{ATR}, CD) = Up_t(n) + \frac{1}{2}CD \cdot ATR_t(n_{ATR})$$
• $$ATR^{(Low)}_t(n, n_{ATR}, CD) = Up_t(n) - \frac{1}{2}CD \cdot ATR_t(n_{ATR})$$
• Down_t(n) = 0

• If $$Trend_t(n) = 1$$:
• If $$Trend_{t - 1} = 0$$
• $$Down_t(n) = Up_{t - 1}(n)$$
• $$ArrowDown_t(n,n_{ATR}) = Down_t(n) + \frac{1}{2}ATR_t(n_{ATR})$$
• If $$Trend_{t - 1} = 1$$
• $$Down_t(n) = \min\{H^{(min)}_t(n), Down_{t - 1}(n)\}$$
• $$ATR^{(High)}_t(n, n_{ATR}, CD) = Down_t(n) + \frac{1}{2}CD \cdot ATR_t(n_{ATR})$$
• $$ATR^{(Low)}_t(n, n_{ATR}, CD) = Down_t(n) - \frac{1}{2}CD \cdot ATR_t(n_{ATR})$$
• Up_t(n) = 0

Finally, we compute the HalfTrend as follows.

$$\displaystyle{HT_t(n) = \left\{ \begin{matrix} Up_t(n) & Trend_t(n) = 0 \\ Down_t(n) & Trend_t(n) = 1 \end{matrix}\right .}$$

The study displays a total of 5 Subgraphs: $$HT_t(n)$$ (as a solid line), $$ATR^{(High)}_t(n, n_{ATR}, CD)$$ and $$ATR^{(Low)}_t(n, n_{ATR}, CD)$$ (as dashed lines), and $$ArrowUp_t(n,n_{ATR})$$ and $$ArrowDown_t(n,n_{ATR})$$ (as arrows).