mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-19 11:08:05 +00:00
Remove multiple Pine Script indicators: SSFDSP, STARCHANNEL, STBANDS, STC, UBANDS, UCHANNEL, VWAPBANDS, and VWAPSD. These indicators were deleted to streamline the library and remove unused or redundant code.
This commit is contained in:
@@ -1,118 +1,89 @@
|
||||
// The MIT License (MIT)
|
||||
// © mihakralj
|
||||
//@version=6
|
||||
indicator("Ehlers Hilbert Transform Phasor Components (HT_PHASOR)", shorttitle="HT_PHASOR", overlay=false)
|
||||
indicator("Ehlers Hilbert Transform Phasor Components (HT_PHASOR)", "HT_PHASOR", overlay=false)
|
||||
|
||||
//@function Calculates the Ehlers Phasor Angle, Derived Period, and Trend State.
|
||||
//@param src The source series to analyze.
|
||||
//@param period The fixed cycle period to correlate against. Default is 28.
|
||||
//@returns A tuple: `[float finalPhasorAngle, float derivedPeriod, int trendState]`.
|
||||
phasor(series float src, simple int period = 28) =>
|
||||
float sx_corr = 0.0
|
||||
float sy_cos_corr = 0.0
|
||||
float sxx_corr = 0.0
|
||||
float sxy_cos_corr = 0.0
|
||||
float syy_cos_corr = 0.0
|
||||
for i = 0 to period - 1
|
||||
float x_val = nz(src[i])
|
||||
float y_val_cos = math.cos(2 * math.pi * i / period)
|
||||
sx_corr += x_val
|
||||
sy_cos_corr += y_val_cos
|
||||
sxx_corr += x_val * x_val
|
||||
sxy_cos_corr += x_val * y_val_cos
|
||||
syy_cos_corr += y_val_cos * y_val_cos
|
||||
float real_part = 0.0
|
||||
float den_cos = (period * sxx_corr - sx_corr * sx_corr) * (period * syy_cos_corr - sy_cos_corr * sy_cos_corr)
|
||||
if den_cos > 0
|
||||
real_part := (period * sxy_cos_corr - sx_corr * sy_cos_corr) / math.sqrt(den_cos)
|
||||
sx_corr := 0.0
|
||||
sxx_corr := 0.0
|
||||
float sy_sin_corr = 0.0
|
||||
float sxy_sin_corr = 0.0
|
||||
float syy_sin_corr = 0.0
|
||||
for i = 0 to period - 1
|
||||
float x_val = nz(src[i])
|
||||
float y_val_sin = -math.sin(2 * math.pi * i / period) // Negative sine as per Ehlers
|
||||
sx_corr += x_val
|
||||
sxx_corr += x_val * x_val
|
||||
sy_sin_corr += y_val_sin
|
||||
sxy_sin_corr += x_val * y_val_sin
|
||||
syy_sin_corr += y_val_sin * y_val_sin
|
||||
float imag_part = 0.0
|
||||
float den_sin = (period * sxx_corr - sx_corr * sx_corr) * (period * syy_sin_corr - sy_sin_corr * sy_sin_corr)
|
||||
if den_sin > 0
|
||||
imag_part := (period * sxy_sin_corr - sx_corr * sy_sin_corr) / math.sqrt(den_sin)
|
||||
float current_raw_phase = 0.0
|
||||
if real_part != 0.0
|
||||
current_raw_phase := 90.0 - math.atan(imag_part / real_part) * 180.0 / math.pi
|
||||
if real_part < 0.0
|
||||
current_raw_phase -= 180.0
|
||||
else if imag_part != 0.0
|
||||
current_raw_phase := imag_part > 0.0 ? 0.0 : 180.0
|
||||
var float core_Phasor_unwrapped_state = na
|
||||
if not na(core_Phasor_unwrapped_state[1])
|
||||
float diff = current_raw_phase - core_Phasor_unwrapped_state[1]
|
||||
if diff > 180.0
|
||||
current_raw_phase -= 360.0
|
||||
else if diff < -180.0
|
||||
current_raw_phase += 360.0
|
||||
core_Phasor_unwrapped_state := na(core_Phasor_unwrapped_state[1]) ? current_raw_phase : core_Phasor_unwrapped_state[1] + (current_raw_phase - core_Phasor_unwrapped_state[1])
|
||||
float calculated_Phasor_val = core_Phasor_unwrapped_state
|
||||
var float final_Phasor_state = na
|
||||
if na(final_Phasor_state[1])
|
||||
final_Phasor_state := calculated_Phasor_val
|
||||
else
|
||||
if calculated_Phasor_val < final_Phasor_state[1] and ((calculated_Phasor_val > -135 and final_Phasor_state[1] < 135) or (calculated_Phasor_val < -90 and final_Phasor_state[1] < -90))
|
||||
final_Phasor_state := final_Phasor_state[1]
|
||||
else
|
||||
final_Phasor_state := calculated_Phasor_val
|
||||
var float derivedPeriod_calc_state = na
|
||||
float angle_Change_For_Period = final_Phasor_state - nz(final_Phasor_state[1], final_Phasor_state)
|
||||
if nz(angle_Change_For_Period) == 0 and not na(derivedPeriod_calc_state[1])
|
||||
if derivedPeriod_calc_state[1] != 0
|
||||
angle_Change_For_Period := 360.0 / derivedPeriod_calc_state[1]
|
||||
else
|
||||
angle_Change_For_Period := 0.0
|
||||
if nz(angle_Change_For_Period) <= 0 and not na(derivedPeriod_calc_state[1])
|
||||
if derivedPeriod_calc_state[1] != 0
|
||||
angle_Change_For_Period := 360.0 / derivedPeriod_calc_state[1]
|
||||
else
|
||||
angle_Change_For_Period := 0.0
|
||||
if nz(angle_Change_For_Period) != 0.0
|
||||
derivedPeriod_calc_state := 360.0 / angle_Change_For_Period
|
||||
else if not na(derivedPeriod_calc_state[1])
|
||||
derivedPeriod_calc_state := derivedPeriod_calc_state[1]
|
||||
else
|
||||
derivedPeriod_calc_state := 60.0
|
||||
derivedPeriod_calc_state := math.max(1.0, math.min(derivedPeriod_calc_state, 60.0))
|
||||
var int trendState_calc_state = 0
|
||||
float angle_Change_For_State = final_Phasor_state - nz(final_Phasor_state[1], final_Phasor_state)
|
||||
int currentTrendState_calc = 0
|
||||
if angle_Change_For_State <= 6.0
|
||||
if final_Phasor_state >= 90.0 or final_Phasor_state <= -90.0
|
||||
currentTrendState_calc := 1
|
||||
else if final_Phasor_state > -90.0 and final_Phasor_state < 90.0
|
||||
currentTrendState_calc := -1
|
||||
trendState_calc_state := currentTrendState_calc
|
||||
[final_Phasor_state, derivedPeriod_calc_state, trendState_calc_state]
|
||||
//@function Calculates Hilbert Transform Phasor Components using TA-Lib algorithm
|
||||
//@param source Series to analyze for phasor components
|
||||
//@returns Tuple [inphase, quadrature] - raw I1[3] and Q1 components
|
||||
ht_phasor(series float source) =>
|
||||
var float detrender = 0.0
|
||||
var float i1 = 0.0
|
||||
var float q1 = 0.0
|
||||
var float ji = 0.0
|
||||
var float jq = 0.0
|
||||
var float i2 = 0.0
|
||||
var float q2 = 0.0
|
||||
var float re = 0.0
|
||||
var float im = 0.0
|
||||
var float period = 0.0
|
||||
var float smooth_period = 0.0
|
||||
|
||||
// ---------- Inputs ----------
|
||||
i_period = input.int(28, "Period", minval=1, group="Phasor Settings")
|
||||
i_source = input.source(close, "Source", group="Phasor Settings")
|
||||
showDerivedPeriod = input.bool(false, "Show Derived Period", group="Optional Plots", inline="derived_period")
|
||||
showTrendState = input.bool(false, "Show Trend State Variable", group="Optional Plots", inline="trend_state")
|
||||
float price = nz(source)
|
||||
|
||||
// ---------- Calculations ----------
|
||||
// Call the main function to get all values
|
||||
[phasorAngle, derivedPeriodValue, trendStateValue] = phasor(i_source, i_period)
|
||||
// Step 1: WMA smoothing (4-tap: [4,3,2,1]/10)
|
||||
float smooth_price = (4.0 * price + 3.0 * nz(price[1]) + 2.0 * nz(price[2]) + nz(price[3])) / 10.0
|
||||
|
||||
// ---------- Plotting Phasor Angle ----------
|
||||
plot(phasorAngle, "Phasor Angle", color=color.yellow, linewidth=2)
|
||||
// Bandwidth uses period (not smooth_period) per TA-Lib
|
||||
float bandwidth = 0.075 * period + 0.54
|
||||
|
||||
// Step 2: Hilbert FIR detrender
|
||||
detrender := (0.0962 * smooth_price + 0.5769 * nz(smooth_price[2]) - 0.5769 * nz(smooth_price[4]) - 0.0962 * nz(smooth_price[6])) * bandwidth
|
||||
|
||||
// ---------- Optional Plots ----------
|
||||
// Plot for Derived Period
|
||||
plot(showDerivedPeriod ? derivedPeriodValue : na, "Derived Period", color=color.yellow, linewidth=2)
|
||||
// Step 3: Q1 computation (Hilbert FIR on detrender)
|
||||
q1 := (0.0962 * detrender + 0.5769 * nz(detrender[2]) - 0.5769 * nz(detrender[4]) - 0.0962 * nz(detrender[6])) * bandwidth
|
||||
|
||||
// Plot for Trend State
|
||||
plot(showTrendState ? trendStateValue : na, "Trend State", color=color.yellow, linewidth=2, style=plot.style_histogram)
|
||||
// Step 4: I1 = detrender delayed 3 bars
|
||||
i1 := nz(detrender[3])
|
||||
|
||||
// Step 5: Advance phase via JI/JQ
|
||||
ji := (0.0962 * i1 + 0.5769 * nz(i1[2]) - 0.5769 * nz(i1[4]) - 0.0962 * nz(i1[6])) * bandwidth
|
||||
jq := (0.0962 * q1 + 0.5769 * nz(q1[2]) - 0.5769 * nz(q1[4]) - 0.0962 * nz(q1[6])) * bandwidth
|
||||
|
||||
// Step 6: Smooth I2/Q2 with 2-bar EMA (used internally for period calc)
|
||||
i2 := 0.2 * (i1 - jq) + 0.8 * nz(i2[1])
|
||||
q2 := 0.2 * (q1 + ji) + 0.8 * nz(q2[1])
|
||||
|
||||
// Step 7: Homodyne discriminator
|
||||
re := 0.2 * (i2 * nz(i2[1]) + q2 * nz(q2[1])) + 0.8 * nz(re[1])
|
||||
im := 0.2 * (i2 * nz(q2[1]) - q2 * nz(i2[1])) + 0.8 * nz(im[1])
|
||||
|
||||
// Step 8: Period from atan (NOT atan2) + rate limiting + clamping
|
||||
float prev_period = period
|
||||
if math.abs(im) > 1e-12 and math.abs(re) > 1e-12
|
||||
float angle = math.atan(im / re)
|
||||
if math.abs(angle) > 1e-12
|
||||
period := 2.0 * math.pi / angle
|
||||
|
||||
// Rate limit: ±50% bar-to-bar
|
||||
if prev_period > 0
|
||||
period := math.min(period, 1.5 * prev_period)
|
||||
period := math.max(period, 0.67 * prev_period)
|
||||
|
||||
// Clamp to valid range
|
||||
period := math.max(6.0, math.min(50.0, period))
|
||||
|
||||
// Step 9: Smooth period with 0.2/0.8 EMA
|
||||
period := 0.2 * period + 0.8 * prev_period
|
||||
|
||||
// Step 10: Smooth smoothPeriod with 0.33/0.67 EMA
|
||||
smooth_period := 0.33 * period + 0.67 * smooth_period
|
||||
|
||||
// Step 11: Output raw I1[3] (inPhase) and Q1 (quadrature) per TA-Lib HT_PHASOR
|
||||
// TA-Lib outputs the detrender delayed by 3 bars as InPhase, and the raw Q1 as Quadrature
|
||||
float inphase_out = nz(i1[3])
|
||||
float quadrature_out = q1
|
||||
[inphase_out, quadrature_out]
|
||||
|
||||
// ---------- Main loop ----------
|
||||
|
||||
// Inputs
|
||||
i_source = input.source(hlc3, "Source")
|
||||
|
||||
// Calculation
|
||||
[inphase, quadrature] = ht_phasor(i_source)
|
||||
|
||||
// Plot
|
||||
plot(inphase, "InPhase", color=color.yellow, linewidth=2)
|
||||
plot(quadrature, "Quadrature", color=color.blue, linewidth=2)
|
||||
hline(0, "Zero", color=color.gray, linestyle=hline.style_solid)
|
||||
|
||||
Reference in New Issue
Block a user