OPEN-SOURCE SCRIPT
Discrete Stochastic Volatility Optimal Stopping

## Technical Documentation: Discrete Approximation of Bivariate Optimal Stopping Boundaries
### Overview
This document outlines the mathematical methodology for discretizing a continuous-time stochastic volatility model to identify optimal execution boundaries. The algorithm maps an asset's price and variance processes into standardized state spaces to detect joint extrema, triggering execution when predefined statistical thresholds are breached.
---
### 1. Price Process Normalization
To evaluate structural price dislocation, the raw asset price $P_t$ is transformed into a standardized normal state space (Z-score).
**Mathematical Formulation:**
$$Z^{(S)}_t = \frac{P_t - \mu_S}{\sigma_S}$$
Where the sample mean ($\mu_S$) and sample standard deviation ($\sigma_S$) are calculated over an $N$-period lookback window:
* **Sample Mean:** $\mu_S = \frac{1}{N} \sum_{i=0}^{N-1} P_{t-i}$
* **Sample Standard Deviation:** $\sigma_S = \sqrt{\frac{1}{N} \sum_{i=0}^{N-1} (P_{t-i} - \mu_S)^2}$
**Purpose:** This isolates the magnitude of the price deviation relative to its recent equilibrium, providing the orthogonal $x$-axis for the state space.
---
### 2. Instantaneous Variance Estimation
Continuous-time models rely on instantaneous variance, which is unobservable in discrete time. The algorithm approximates this using the annualized rolling realized variance of geometric returns.
**Mathematical Formulation:**
$$v_t = \frac{252}{N} \sum_{i=0}^{N-1} r_{t-i}^2$$
Where the continuously compounded return $r_t$ is defined as:
$$r_t = \ln\left(\frac{P_t}{P_{t-1}}\right)$$
**Purpose:** Assuming the mean daily return is zero ($\mu \approx 0$), the squared log return $r_t^2$ serves as an unbiased estimator of daily variance. The factor $\frac{252}{N}$ standardizes the sum of these squared returns into an annualized volatility metric ($v_t$).
---
### 3. Variance Process Normalization
Because variance $v_t$ is heteroskedastic and mean-reverting, the empirical variance series must also be standardized to evaluate expansion or compression relative to its own baseline.
**Mathematical Formulation:**
$$Z^{(v)}_t = \frac{v_t - \mu_v}{\sigma_v}$$
Where $\mu_v$ and $\sigma_v$ are the $N$-period sample mean and standard deviation of the variance series $v_t$.
**Purpose:** This yields a unitless metric representing the statistical extremity of the current volatility regime, forming the orthogonal $y$-axis of the state space.
---
### 4. Boundary Evaluation Logic
The discrete optimal stopping conditions approximate the analytical Hamilton-Jacobi-Bellman (HJB) boundaries by evaluating the intersection of the two state variables ($Z^{(S)}_t$ and $Z^{(v)}_t$) against arbitrary static thresholds ($\alpha$ for maxima, $\beta$ for minima).
**Entry Condition (Buy):**
Execution is triggered exclusively at the joint minimum of price and variance, defined by the logical intersection:
$$\tau_B = \inf \{ t \ge 0 \mid (Z^{(S)}_t \le \beta_S) \land (Z^{(v)}_t \le \beta_v) \}$$
*Requires price to be heavily discounted while the market regime is highly compressed.*
**Exit Condition (Sell):**
Liquidation is triggered exclusively at the joint maximum, defined by the logical intersection:
$$\tau_S = \inf \{ t \ge \tau_B \mid (Z^{(S)}_t \ge \alpha_S) \land (Z^{(v)}_t \ge \alpha_v) \}$$
*Requires price to be statistically overextended during a regime of extreme variance expansion.*
### Overview
This document outlines the mathematical methodology for discretizing a continuous-time stochastic volatility model to identify optimal execution boundaries. The algorithm maps an asset's price and variance processes into standardized state spaces to detect joint extrema, triggering execution when predefined statistical thresholds are breached.
---
### 1. Price Process Normalization
To evaluate structural price dislocation, the raw asset price $P_t$ is transformed into a standardized normal state space (Z-score).
**Mathematical Formulation:**
$$Z^{(S)}_t = \frac{P_t - \mu_S}{\sigma_S}$$
Where the sample mean ($\mu_S$) and sample standard deviation ($\sigma_S$) are calculated over an $N$-period lookback window:
* **Sample Mean:** $\mu_S = \frac{1}{N} \sum_{i=0}^{N-1} P_{t-i}$
* **Sample Standard Deviation:** $\sigma_S = \sqrt{\frac{1}{N} \sum_{i=0}^{N-1} (P_{t-i} - \mu_S)^2}$
**Purpose:** This isolates the magnitude of the price deviation relative to its recent equilibrium, providing the orthogonal $x$-axis for the state space.
---
### 2. Instantaneous Variance Estimation
Continuous-time models rely on instantaneous variance, which is unobservable in discrete time. The algorithm approximates this using the annualized rolling realized variance of geometric returns.
**Mathematical Formulation:**
$$v_t = \frac{252}{N} \sum_{i=0}^{N-1} r_{t-i}^2$$
Where the continuously compounded return $r_t$ is defined as:
$$r_t = \ln\left(\frac{P_t}{P_{t-1}}\right)$$
**Purpose:** Assuming the mean daily return is zero ($\mu \approx 0$), the squared log return $r_t^2$ serves as an unbiased estimator of daily variance. The factor $\frac{252}{N}$ standardizes the sum of these squared returns into an annualized volatility metric ($v_t$).
---
### 3. Variance Process Normalization
Because variance $v_t$ is heteroskedastic and mean-reverting, the empirical variance series must also be standardized to evaluate expansion or compression relative to its own baseline.
**Mathematical Formulation:**
$$Z^{(v)}_t = \frac{v_t - \mu_v}{\sigma_v}$$
Where $\mu_v$ and $\sigma_v$ are the $N$-period sample mean and standard deviation of the variance series $v_t$.
**Purpose:** This yields a unitless metric representing the statistical extremity of the current volatility regime, forming the orthogonal $y$-axis of the state space.
---
### 4. Boundary Evaluation Logic
The discrete optimal stopping conditions approximate the analytical Hamilton-Jacobi-Bellman (HJB) boundaries by evaluating the intersection of the two state variables ($Z^{(S)}_t$ and $Z^{(v)}_t$) against arbitrary static thresholds ($\alpha$ for maxima, $\beta$ for minima).
**Entry Condition (Buy):**
Execution is triggered exclusively at the joint minimum of price and variance, defined by the logical intersection:
$$\tau_B = \inf \{ t \ge 0 \mid (Z^{(S)}_t \le \beta_S) \land (Z^{(v)}_t \le \beta_v) \}$$
*Requires price to be heavily discounted while the market regime is highly compressed.*
**Exit Condition (Sell):**
Liquidation is triggered exclusively at the joint maximum, defined by the logical intersection:
$$\tau_S = \inf \{ t \ge \tau_B \mid (Z^{(S)}_t \ge \alpha_S) \land (Z^{(v)}_t \ge \alpha_v) \}$$
*Requires price to be statistically overextended during a regime of extreme variance expansion.*
オープンソーススクリプト
TradingViewの精神に則り、このスクリプトの作者はコードをオープンソースとして公開してくれました。トレーダーが内容を確認・検証できるようにという配慮です。作者に拍手を送りましょう!無料で利用できますが、コードの再公開はハウスルールに従う必要があります。
免責事項
これらの情報および投稿は、TradingViewが提供または承認する金融、投資、取引、またはその他の種類の助言もしくは推奨であることを意図したものではなく、またこれらに該当するものでもありません。詳細は利用規約をご覧ください。
オープンソーススクリプト
TradingViewの精神に則り、このスクリプトの作者はコードをオープンソースとして公開してくれました。トレーダーが内容を確認・検証できるようにという配慮です。作者に拍手を送りましょう!無料で利用できますが、コードの再公開はハウスルールに従う必要があります。
免責事項
これらの情報および投稿は、TradingViewが提供または承認する金融、投資、取引、またはその他の種類の助言もしくは推奨であることを意図したものではなく、またこれらに該当するものでもありません。詳細は利用規約をご覧ください。