F12: add price transforms and rolling linear regression
- Rust core: typical_price.rs ((H+L+C)/3), median_price.rs ((H+L)/2), weighted_close.rs ((H+L+2C)/4) — stateless per-bar OHLC transforms — and linreg.rs (LinearRegression — endpoint of a rolling ordinary-least-squares fit) and linreg_slope.rs (LinRegSlope — slope of that fit). Each with a full Indicator impl, runnable doctest and reference / property / warmup / reset / batch==streaming tests. - Python: PyTypicalPrice / PyMedianPrice / PyWeightedClose / PyLinearRegression / PyLinRegSlope PyO3 classes + module registration + .pyi stubs. - Node: explicit TypicalPriceNode / MedianPriceNode / WeightedCloseNode / LinearRegressionNode / LinRegSlopeNode; index.d.ts and index.js updated. - WASM: explicit WasmTypicalPrice / WasmMedianPrice / WasmWeightedClose; WasmLinearRegression / WasmLinRegSlope via the scalar macro. - Wiki: a new indicators/statistics/ folder with five Indicator-*.md pages, a new "Statistics" family in Indicators-Overview.md and Home.md. cargo fmt + clippy (core/wickra/data/wasm/node) clean; 454 core tests, 25 data tests and 66 doctests green.
This commit is contained in:
@@ -140,6 +140,14 @@ Rust / Python / Node examples. They are grouped by family, mirroring the
|
||||
- [Indicator-ForceIndex.md](indicators/volume/Indicator-ForceIndex.md)
|
||||
- [Indicator-EaseOfMovement.md](indicators/volume/Indicator-EaseOfMovement.md)
|
||||
|
||||
**Statistics** — price transforms and rolling regressions.
|
||||
|
||||
- [Indicator-TypicalPrice.md](indicators/statistics/Indicator-TypicalPrice.md)
|
||||
- [Indicator-MedianPrice.md](indicators/statistics/Indicator-MedianPrice.md)
|
||||
- [Indicator-WeightedClose.md](indicators/statistics/Indicator-WeightedClose.md)
|
||||
- [Indicator-LinearRegression.md](indicators/statistics/Indicator-LinearRegression.md)
|
||||
- [Indicator-LinRegSlope.md](indicators/statistics/Indicator-LinRegSlope.md)
|
||||
|
||||
## See also
|
||||
|
||||
- Source code: <https://github.com/kingchenc/wickra>
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
# Indicators Overview
|
||||
|
||||
Wickra ships 58 indicators, organised in source under the four classical
|
||||
families — trend, momentum, volatility, volume — that map directly to the
|
||||
directory structure of `crates/wickra-core/src/indicators/`. The same family
|
||||
labels are used here, plus a second-level grouping that reflects how the
|
||||
indicators actually behave (which output range they live in, what data they
|
||||
need, what question they answer).
|
||||
Wickra ships 63 indicators, organised under the four classical families —
|
||||
trend, momentum, volatility, volume — plus a fifth **statistics** group for
|
||||
price transforms and rolling regressions. The same family labels are used
|
||||
here, with a second-level grouping that reflects how the indicators actually
|
||||
behave (which output range they live in, what data they need, what question
|
||||
they answer).
|
||||
|
||||
Every indicator is an O(1) state machine that consumes one input at a time
|
||||
and produces either `Option<f64>` (Rust), `float | None` (Python), or
|
||||
@@ -185,6 +185,32 @@ price closes within each bar and how much volume backed the move.
|
||||
| `ForceIndex` | `EMA((close − prev_close) · volume, period)`; the conviction behind a move. | `Candle` | `f64` | unbounded around zero | `period = 13` (Python) | `period + 1` | [Indicator-ForceIndex.md](indicators/volume/Indicator-ForceIndex.md) |
|
||||
| `EaseOfMovement` | `SMA` of distance travelled per unit of volume. | `Candle` | `f64` | unbounded around zero | `(period=14, divisor=1e8)` (Python) | `period + 1` | [Indicator-EaseOfMovement.md](indicators/volume/Indicator-EaseOfMovement.md) |
|
||||
|
||||
## Statistics
|
||||
|
||||
Price transforms and rolling regressions. The transforms collapse a full
|
||||
OHLC bar to a single representative price; the regressions fit a
|
||||
least-squares line to a sliding window of prices.
|
||||
|
||||
### Price transforms
|
||||
|
||||
Stateless per-bar reductions of an OHLC candle to one price. Each emits from
|
||||
the very first candle (`warmup = 1`).
|
||||
|
||||
| Indicator | One-liner | Input | Output | Range | Defaults | Warmup | Deep dive |
|
||||
|-----------|-----------|-------|--------|-------|----------|--------|-----------|
|
||||
| `TypicalPrice` | `(high + low + close) / 3`. | `Candle` | `f64` | unbounded (price scale) | (no parameters) | `1` | [Indicator-TypicalPrice.md](indicators/statistics/Indicator-TypicalPrice.md) |
|
||||
| `MedianPrice` | `(high + low) / 2`. | `Candle` | `f64` | unbounded (price scale) | (no parameters) | `1` | [Indicator-MedianPrice.md](indicators/statistics/Indicator-MedianPrice.md) |
|
||||
| `WeightedClose` | `(high + low + 2·close) / 4`. | `Candle` | `f64` | unbounded (price scale) | (no parameters) | `1` | [Indicator-WeightedClose.md](indicators/statistics/Indicator-WeightedClose.md) |
|
||||
|
||||
### Regression
|
||||
|
||||
Rolling ordinary-least-squares fits over the last `period` prices.
|
||||
|
||||
| Indicator | One-liner | Input | Output | Range | Defaults | Warmup | Deep dive |
|
||||
|-----------|-----------|-------|--------|-------|----------|--------|-----------|
|
||||
| `LinearRegression` | Endpoint of the rolling least-squares line — a low-lag smoothed price. | `f64` | `f64` | unbounded (price scale) | `period = 14` (Python) | `period` | [Indicator-LinearRegression.md](indicators/statistics/Indicator-LinearRegression.md) |
|
||||
| `LinRegSlope` | Slope of the rolling least-squares line — trend steepness per bar. | `f64` | `f64` | unbounded around zero | `period = 14` (Python) | `period` | [Indicator-LinRegSlope.md](indicators/statistics/Indicator-LinRegSlope.md) |
|
||||
|
||||
## Pick the right indicator for…
|
||||
|
||||
A short cheat-sheet of "I want X, which indicator?" answers, grounded in
|
||||
|
||||
@@ -0,0 +1,150 @@
|
||||
# LinRegSlope
|
||||
|
||||
> Linear Regression Slope — the slope of a rolling ordinary-least-squares
|
||||
> fit over the last `period` prices.
|
||||
|
||||
## Quick reference
|
||||
|
||||
| Field | Value |
|
||||
|-------|-------|
|
||||
| Family | Statistics |
|
||||
| Sub-category | Regression |
|
||||
| Input type | `f64` (price) |
|
||||
| Output type | `f64` |
|
||||
| Output range | unbounded around zero (price units per bar) |
|
||||
| Default parameters | `period = 14` (Python) |
|
||||
| Warmup period | `period` |
|
||||
| Interpretation | How steeply price trends; positive up, negative down, zero flat. |
|
||||
|
||||
## Formula
|
||||
|
||||
Over the last `period` inputs, indexed `x = 0, 1, …, period − 1`:
|
||||
|
||||
```
|
||||
b = (n·Σxy − Σx·Σy) / (n·Σxx − (Σx)²)
|
||||
```
|
||||
|
||||
`LinRegSlope` fits a straight line to the window by ordinary least squares —
|
||||
the same fit as [`LinearRegression`](Indicator-LinearRegression.md) — but
|
||||
reports the *slope* `b` instead of the endpoint. The slope is in price units
|
||||
per bar: positive while price trends up, negative while it trends down, near
|
||||
zero when it is ranging. This is TA-Lib's `LINEARREG_SLOPE`.
|
||||
|
||||
## Parameters
|
||||
|
||||
`period` — the regression window. Must be at least `2` (a line needs two
|
||||
points). The Python binding defaults it to `14`; the Rust and Node
|
||||
constructors require it explicitly.
|
||||
|
||||
## Inputs / Outputs
|
||||
|
||||
From `crates/wickra-core/src/indicators/linreg_slope.rs`:
|
||||
|
||||
```rust
|
||||
impl Indicator for LinRegSlope {
|
||||
type Input = f64;
|
||||
type Output = f64;
|
||||
// update(&mut self, input: f64) -> Option<f64>
|
||||
}
|
||||
```
|
||||
|
||||
`LinRegSlope` is a **scalar** indicator: it consumes one `f64` price per step.
|
||||
Because `Input = f64` it can sit inside a [`Chain`](../../Indicator-Chaining.md).
|
||||
|
||||
## Warmup
|
||||
|
||||
`LinRegSlope::new(14).warmup_period() == 14`. The first value lands once the
|
||||
window holds a full `period` prices — on input index `period − 1`.
|
||||
|
||||
## Edge cases
|
||||
|
||||
- **`period < 2`.** Rejected at construction — a regression line is undefined
|
||||
for fewer than two points.
|
||||
- **Perfect line.** Fed a series rising by a fixed step, the slope is exactly
|
||||
that step (`perfect_line_returns_its_step` pins this).
|
||||
- **Constant series.** A flat input returns a slope of `0`.
|
||||
- **Falling series.** A descending input returns a negative slope.
|
||||
- **Reset.** `ls.reset()` clears the rolling window.
|
||||
|
||||
## Examples
|
||||
|
||||
### Rust
|
||||
|
||||
```rust
|
||||
use wickra::{BatchExt, Indicator, LinRegSlope};
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let mut ls = LinRegSlope::new(3)?;
|
||||
// Fit over [1, 2, 9]: the least-squares line is y = 4x, slope 4.
|
||||
let out = ls.batch(&[1.0, 2.0, 9.0]);
|
||||
println!("{:?}", out);
|
||||
Ok(())
|
||||
}
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```
|
||||
[None, None, Some(4.0)]
|
||||
```
|
||||
|
||||
This matches the `reference_values` test in
|
||||
`crates/wickra-core/src/indicators/linreg_slope.rs`.
|
||||
|
||||
### Python
|
||||
|
||||
```python
|
||||
import numpy as np
|
||||
import wickra as ta
|
||||
|
||||
ls = ta.LinRegSlope(3)
|
||||
print(ls.batch(np.array([1.0, 2.0, 9.0])))
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```
|
||||
[nan nan 4.]
|
||||
```
|
||||
|
||||
### Node
|
||||
|
||||
```javascript
|
||||
const ta = require('wickra');
|
||||
const ls = new ta.LinRegSlope(3);
|
||||
console.log(ls.batch([1, 2, 9]));
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```
|
||||
[ NaN, NaN, 4 ]
|
||||
```
|
||||
|
||||
## Interpretation
|
||||
|
||||
`LinRegSlope` is a momentum gauge: its sign is the trend direction and its
|
||||
magnitude is the trend's steepness in price-per-bar. A slope crossing zero
|
||||
marks a trend change; a slope that flattens while price still rises warns the
|
||||
trend is losing pace. Unlike a difference-based oscillator it uses every bar
|
||||
in the window, so it is less jumpy.
|
||||
|
||||
## Common pitfalls
|
||||
|
||||
- **Comparing slopes across instruments.** The slope is in the instrument's
|
||||
own price units per bar — normalise (e.g. divide by price) to compare.
|
||||
- **Tiny periods.** `period = 2` reduces the slope to the last simple
|
||||
difference; use a meaningful window.
|
||||
|
||||
## References
|
||||
|
||||
The slope of an ordinary least-squares fit to a rolling price window; matches
|
||||
TA-Lib's `LINEARREG_SLOPE`.
|
||||
|
||||
## See also
|
||||
|
||||
- [Indicator-LinearRegression.md](Indicator-LinearRegression.md) — the
|
||||
endpoint of the same rolling fit.
|
||||
- [Indicator-Mom.md](../momentum/Indicator-Mom.md) — raw price-difference
|
||||
momentum, the unsmoothed cousin.
|
||||
- [Indicators-Overview.md](../../Indicators-Overview.md) — the full taxonomy.
|
||||
@@ -0,0 +1,152 @@
|
||||
# LinearRegression
|
||||
|
||||
> Linear Regression — the endpoint of a rolling ordinary-least-squares fit
|
||||
> over the last `period` prices.
|
||||
|
||||
## Quick reference
|
||||
|
||||
| Field | Value |
|
||||
|-------|-------|
|
||||
| Family | Statistics |
|
||||
| Sub-category | Regression |
|
||||
| Input type | `f64` (price) |
|
||||
| Output type | `f64` |
|
||||
| Output range | unbounded (price scale) |
|
||||
| Default parameters | `period = 14` (Python) |
|
||||
| Warmup period | `period` |
|
||||
| Interpretation | A low-lag smoothed price — the trend line extrapolated to now. |
|
||||
|
||||
## Formula
|
||||
|
||||
Over the last `period` inputs, indexed `x = 0, 1, …, period − 1`:
|
||||
|
||||
```
|
||||
b (slope) = (n·Σxy − Σx·Σy) / (n·Σxx − (Σx)²)
|
||||
a (intercept) = (Σy − b·Σx) / n
|
||||
LinearReg = a + b·(period − 1)
|
||||
```
|
||||
|
||||
The indicator fits a straight line to the window by ordinary least squares,
|
||||
then reports that line's value at the most recent bar. Because it
|
||||
extrapolates the *local trend* forward rather than averaging it away, it lags
|
||||
a same-period [`Sma`](../trend/Indicator-Sma.md) noticeably less. This is
|
||||
TA-Lib's `LINEARREG`.
|
||||
|
||||
## Parameters
|
||||
|
||||
`period` — the regression window. Must be at least `2` (a line needs two
|
||||
points). The Python binding defaults it to `14`; the Rust and Node
|
||||
constructors require it explicitly.
|
||||
|
||||
## Inputs / Outputs
|
||||
|
||||
From `crates/wickra-core/src/indicators/linreg.rs`:
|
||||
|
||||
```rust
|
||||
impl Indicator for LinearRegression {
|
||||
type Input = f64;
|
||||
type Output = f64;
|
||||
// update(&mut self, input: f64) -> Option<f64>
|
||||
}
|
||||
```
|
||||
|
||||
`LinearRegression` is a **scalar** indicator: it consumes one `f64` price per
|
||||
step. Because `Input = f64` it can sit inside a [`Chain`](../../Indicator-Chaining.md).
|
||||
|
||||
## Warmup
|
||||
|
||||
`LinearRegression::new(14).warmup_period() == 14`. The first value lands once
|
||||
the window holds a full `period` prices — on input index `period − 1`.
|
||||
|
||||
## Edge cases
|
||||
|
||||
- **`period < 2`.** Rejected at construction — a regression line is undefined
|
||||
for fewer than two points.
|
||||
- **Perfect line.** Fed a perfectly linear series, the fit *is* that line, so
|
||||
the endpoint equals the current value (`perfect_line_returns_current_value`
|
||||
pins this).
|
||||
- **Constant series.** A flat input returns that constant.
|
||||
- **Reset.** `lr.reset()` clears the rolling window.
|
||||
|
||||
## Examples
|
||||
|
||||
### Rust
|
||||
|
||||
```rust
|
||||
use wickra::{BatchExt, Indicator, LinearRegression};
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let mut lr = LinearRegression::new(3)?;
|
||||
// Fit over [1, 2, 9]: the least-squares line is y = 4x, endpoint 4·2 = 8.
|
||||
let out = lr.batch(&[1.0, 2.0, 9.0]);
|
||||
println!("{:?}", out);
|
||||
Ok(())
|
||||
}
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```
|
||||
[None, None, Some(8.0)]
|
||||
```
|
||||
|
||||
This matches the `reference_values` test in
|
||||
`crates/wickra-core/src/indicators/linreg.rs`.
|
||||
|
||||
### Python
|
||||
|
||||
```python
|
||||
import numpy as np
|
||||
import wickra as ta
|
||||
|
||||
lr = ta.LinearRegression(3)
|
||||
print(lr.batch(np.array([1.0, 2.0, 9.0])))
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```
|
||||
[nan nan 8.]
|
||||
```
|
||||
|
||||
### Node
|
||||
|
||||
```javascript
|
||||
const ta = require('wickra');
|
||||
const lr = new ta.LinearRegression(3);
|
||||
console.log(lr.batch([1, 2, 9]));
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```
|
||||
[ NaN, NaN, 8 ]
|
||||
```
|
||||
|
||||
## Interpretation
|
||||
|
||||
Read `LinearRegression` as a low-lag moving average: it tracks price more
|
||||
closely than an SMA of the same period because it projects the window's trend
|
||||
to the current bar instead of centring on the window. A shorter `period`
|
||||
hugs price; a longer one is a smoother trend line. Pair it with
|
||||
[`LinRegSlope`](Indicator-LinRegSlope.md) to read the same fit's steepness.
|
||||
|
||||
## Common pitfalls
|
||||
|
||||
- **Confusing it with an SMA.** It is a *projected* fit, not a centred
|
||||
average, so it leads an SMA of the same period.
|
||||
- **Tiny periods.** `period = 2` is allowed but the "fit" just passes through
|
||||
the last two points; use a meaningful window.
|
||||
|
||||
## References
|
||||
|
||||
Ordinary least-squares linear regression applied to a rolling price window;
|
||||
the endpoint formulation matches TA-Lib's `LINEARREG`.
|
||||
|
||||
## See also
|
||||
|
||||
- [Indicator-LinRegSlope.md](Indicator-LinRegSlope.md) — the slope of the same
|
||||
rolling fit.
|
||||
- [Indicator-Sma.md](../trend/Indicator-Sma.md) — the centred average it is
|
||||
often compared against.
|
||||
- [Indicators-Overview.md](../../Indicators-Overview.md) — the full taxonomy.
|
||||
@@ -0,0 +1,136 @@
|
||||
# MedianPrice
|
||||
|
||||
> Median Price — the bar's `(high + low) / 2`, the midpoint of its range.
|
||||
|
||||
## Quick reference
|
||||
|
||||
| Field | Value |
|
||||
|-------|-------|
|
||||
| Family | Statistics |
|
||||
| Sub-category | Price transforms |
|
||||
| Input type | `Candle` (uses `high`, `low`) |
|
||||
| Output type | `f64` |
|
||||
| Output range | unbounded (price scale) |
|
||||
| Default parameters | none (no parameters) |
|
||||
| Warmup period | `1` |
|
||||
| Interpretation | The midpoint of the bar's range, ignoring open and close. |
|
||||
|
||||
## Formula
|
||||
|
||||
```
|
||||
MedianPrice = (high + low) / 2
|
||||
```
|
||||
|
||||
The median price is the centre of the bar's range — it discards where the bar
|
||||
opened and closed entirely. It is the price series Bill Williams'
|
||||
[`AwesomeOscillator`](../momentum/Indicator-AwesomeOscillator.md) is built on,
|
||||
and a useful close substitute when the close is noisy relative to the range.
|
||||
|
||||
## Parameters
|
||||
|
||||
`MedianPrice` takes **no parameters** — `MedianPrice::new()` in Rust,
|
||||
`wickra.MedianPrice()` in Python, `new ta.MedianPrice()` in Node.
|
||||
|
||||
## Inputs / Outputs
|
||||
|
||||
From `crates/wickra-core/src/indicators/median_price.rs`:
|
||||
|
||||
```rust
|
||||
impl Indicator for MedianPrice {
|
||||
type Input = Candle;
|
||||
type Output = f64;
|
||||
// update(&mut self, input: Candle) -> Option<f64>
|
||||
}
|
||||
```
|
||||
|
||||
`MedianPrice` is a **candle-input** indicator that reads `high` and `low`. In
|
||||
Python the streaming `update` accepts a 6-tuple or a dict; the batch helper
|
||||
takes `high`, `low` numpy arrays. Node and WASM expose `update(high, low)` and
|
||||
the matching `batch`.
|
||||
|
||||
## Warmup
|
||||
|
||||
`MedianPrice::new().warmup_period() == 1`. It is a stateless per-bar transform
|
||||
— it emits a value from the very first candle.
|
||||
|
||||
## Edge cases
|
||||
|
||||
- **No warmup.** Every candle produces a value immediately.
|
||||
- **Reset.** `mp.reset()` only clears the `is_ready` flag; there is no
|
||||
rolling state to discard.
|
||||
|
||||
## Examples
|
||||
|
||||
### Rust
|
||||
|
||||
```rust
|
||||
use wickra::{Candle, Indicator, MedianPrice};
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let mut mp = MedianPrice::new();
|
||||
let v = mp.update(Candle::new(10.0, 12.0, 8.0, 11.0, 1.0, 0)?);
|
||||
println!("{:?}", v);
|
||||
Ok(())
|
||||
}
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```
|
||||
Some(10.0)
|
||||
```
|
||||
|
||||
`(12 + 8) / 2 = 10`. This matches the `reference_value` test in
|
||||
`crates/wickra-core/src/indicators/median_price.rs`.
|
||||
|
||||
### Python
|
||||
|
||||
```python
|
||||
import numpy as np
|
||||
import wickra as ta
|
||||
|
||||
mp = ta.MedianPrice()
|
||||
print(mp.batch(np.array([12.0]), np.array([8.0])))
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```
|
||||
[10.]
|
||||
```
|
||||
|
||||
### Node
|
||||
|
||||
```javascript
|
||||
const ta = require('wickra');
|
||||
const mp = new ta.MedianPrice();
|
||||
console.log(mp.batch([12], [8]));
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```
|
||||
[ 10 ]
|
||||
```
|
||||
|
||||
## Interpretation
|
||||
|
||||
The median price is the most range-centric of the three transforms — it is
|
||||
blind to the close. Use it when the question is "where did this bar trade?"
|
||||
rather than "where did it settle?", or as the input to a Bill Williams setup.
|
||||
|
||||
## Common pitfalls
|
||||
|
||||
- **Expecting the close to matter.** It does not — by definition the median
|
||||
price ignores both the open and the close.
|
||||
|
||||
## References
|
||||
|
||||
The Median Price; the `(H + L) / 2` definition is standard (TA-Lib's
|
||||
`MEDPRICE`).
|
||||
|
||||
## See also
|
||||
|
||||
- [Indicator-TypicalPrice.md](Indicator-TypicalPrice.md) — `(H + L + C) / 3`.
|
||||
- [Indicator-WeightedClose.md](Indicator-WeightedClose.md) — `(H + L + 2C) / 4`.
|
||||
- [Indicators-Overview.md](../../Indicators-Overview.md) — the full taxonomy.
|
||||
@@ -0,0 +1,137 @@
|
||||
# TypicalPrice
|
||||
|
||||
> Typical Price — the bar's `(high + low + close) / 3`, a single
|
||||
> representative price per candle.
|
||||
|
||||
## Quick reference
|
||||
|
||||
| Field | Value |
|
||||
|-------|-------|
|
||||
| Family | Statistics |
|
||||
| Sub-category | Price transforms |
|
||||
| Input type | `Candle` (uses `high`, `low`, `close`) |
|
||||
| Output type | `f64` |
|
||||
| Output range | unbounded (price scale) |
|
||||
| Default parameters | none (no parameters) |
|
||||
| Warmup period | `1` |
|
||||
| Interpretation | A representative per-bar price; a smoother stand-in for the close. |
|
||||
|
||||
## Formula
|
||||
|
||||
```
|
||||
TypicalPrice = (high + low + close) / 3
|
||||
```
|
||||
|
||||
The typical price collapses a full OHLC bar to one number, giving the close
|
||||
no more weight than the two extremes. It is the price series that
|
||||
[`Cci`](../momentum/Indicator-Cci.md) and [`Mfi`](../momentum/Indicator-Mfi.md)
|
||||
are defined on, and a common input to feed any close-driven indicator when you
|
||||
want the bar's range reflected in the value.
|
||||
|
||||
## Parameters
|
||||
|
||||
`TypicalPrice` takes **no parameters** — `TypicalPrice::new()` in Rust,
|
||||
`wickra.TypicalPrice()` in Python, `new ta.TypicalPrice()` in Node.
|
||||
|
||||
## Inputs / Outputs
|
||||
|
||||
From `crates/wickra-core/src/indicators/typical_price.rs`:
|
||||
|
||||
```rust
|
||||
impl Indicator for TypicalPrice {
|
||||
type Input = Candle;
|
||||
type Output = f64;
|
||||
// update(&mut self, input: Candle) -> Option<f64>
|
||||
}
|
||||
```
|
||||
|
||||
`TypicalPrice` is a **candle-input** indicator that reads `high`, `low` and
|
||||
`close`. In Python the streaming `update` accepts a 6-tuple or a dict; the
|
||||
batch helper takes `high`, `low`, `close` numpy arrays. Node and WASM expose
|
||||
`update(high, low, close)` and the matching `batch`.
|
||||
|
||||
## Warmup
|
||||
|
||||
`TypicalPrice::new().warmup_period() == 1`. It is a stateless per-bar
|
||||
transform — it emits a value from the very first candle.
|
||||
|
||||
## Edge cases
|
||||
|
||||
- **No warmup.** Every candle produces a value immediately.
|
||||
- **Reset.** `tp.reset()` only clears the `is_ready` flag; there is no
|
||||
rolling state to discard.
|
||||
|
||||
## Examples
|
||||
|
||||
### Rust
|
||||
|
||||
```rust
|
||||
use wickra::{Candle, Indicator, TypicalPrice};
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let mut tp = TypicalPrice::new();
|
||||
let v = tp.update(Candle::new(9.0, 12.0, 6.0, 9.0, 1.0, 0)?);
|
||||
println!("{:?}", v);
|
||||
Ok(())
|
||||
}
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```
|
||||
Some(9.0)
|
||||
```
|
||||
|
||||
`(12 + 6 + 9) / 3 = 9`. This matches the `reference_value` test in
|
||||
`crates/wickra-core/src/indicators/typical_price.rs`.
|
||||
|
||||
### Python
|
||||
|
||||
```python
|
||||
import numpy as np
|
||||
import wickra as ta
|
||||
|
||||
tp = ta.TypicalPrice()
|
||||
print(tp.batch(np.array([12.0]), np.array([6.0]), np.array([9.0])))
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```
|
||||
[9.]
|
||||
```
|
||||
|
||||
### Node
|
||||
|
||||
```javascript
|
||||
const ta = require('wickra');
|
||||
const tp = new ta.TypicalPrice();
|
||||
console.log(tp.batch([12], [6], [9]));
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```
|
||||
[ 9 ]
|
||||
```
|
||||
|
||||
## Interpretation
|
||||
|
||||
Use it wherever you would use the close but want the bar's range to count —
|
||||
feeding a moving average, an oscillator, or a band. It is marginally smoother
|
||||
than the raw close because a wild close is pulled back toward the bar's mid.
|
||||
|
||||
## Common pitfalls
|
||||
|
||||
- **Feeding it scalar prices.** It needs the full `high`/`low`/`close` bar.
|
||||
|
||||
## References
|
||||
|
||||
The Typical Price (also "pivot price"); the `(H + L + C) / 3` definition is
|
||||
standard (StockCharts, TA-Lib's `TYPPRICE`).
|
||||
|
||||
## See also
|
||||
|
||||
- [Indicator-MedianPrice.md](Indicator-MedianPrice.md) — `(H + L) / 2`.
|
||||
- [Indicator-WeightedClose.md](Indicator-WeightedClose.md) — `(H + L + 2C) / 4`.
|
||||
- [Indicators-Overview.md](../../Indicators-Overview.md) — the full taxonomy.
|
||||
@@ -0,0 +1,137 @@
|
||||
# WeightedClose
|
||||
|
||||
> Weighted Close — the bar's `(high + low + 2·close) / 4`, a per-bar price
|
||||
> that gives the close double weight.
|
||||
|
||||
## Quick reference
|
||||
|
||||
| Field | Value |
|
||||
|-------|-------|
|
||||
| Family | Statistics |
|
||||
| Sub-category | Price transforms |
|
||||
| Input type | `Candle` (uses `high`, `low`, `close`) |
|
||||
| Output type | `f64` |
|
||||
| Output range | unbounded (price scale) |
|
||||
| Default parameters | none (no parameters) |
|
||||
| Warmup period | `1` |
|
||||
| Interpretation | A representative per-bar price that leans on the close. |
|
||||
|
||||
## Formula
|
||||
|
||||
```
|
||||
WeightedClose = (high + low + 2·close) / 4
|
||||
```
|
||||
|
||||
Like the [`TypicalPrice`](Indicator-TypicalPrice.md), the weighted close
|
||||
collapses an OHLC bar to one number — but it counts the close twice, so the
|
||||
result sits closer to where the bar settled than to its range. Reach for it
|
||||
when the closing print carries more signal than the extremes.
|
||||
|
||||
## Parameters
|
||||
|
||||
`WeightedClose` takes **no parameters** — `WeightedClose::new()` in Rust,
|
||||
`wickra.WeightedClose()` in Python, `new ta.WeightedClose()` in Node.
|
||||
|
||||
## Inputs / Outputs
|
||||
|
||||
From `crates/wickra-core/src/indicators/weighted_close.rs`:
|
||||
|
||||
```rust
|
||||
impl Indicator for WeightedClose {
|
||||
type Input = Candle;
|
||||
type Output = f64;
|
||||
// update(&mut self, input: Candle) -> Option<f64>
|
||||
}
|
||||
```
|
||||
|
||||
`WeightedClose` is a **candle-input** indicator that reads `high`, `low` and
|
||||
`close`. In Python the streaming `update` accepts a 6-tuple or a dict; the
|
||||
batch helper takes `high`, `low`, `close` numpy arrays. Node and WASM expose
|
||||
`update(high, low, close)` and the matching `batch`.
|
||||
|
||||
## Warmup
|
||||
|
||||
`WeightedClose::new().warmup_period() == 1`. It is a stateless per-bar
|
||||
transform — it emits a value from the very first candle.
|
||||
|
||||
## Edge cases
|
||||
|
||||
- **No warmup.** Every candle produces a value immediately.
|
||||
- **Reset.** `wc.reset()` only clears the `is_ready` flag; there is no
|
||||
rolling state to discard.
|
||||
|
||||
## Examples
|
||||
|
||||
### Rust
|
||||
|
||||
```rust
|
||||
use wickra::{Candle, Indicator, WeightedClose};
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let mut wc = WeightedClose::new();
|
||||
let v = wc.update(Candle::new(10.0, 12.0, 8.0, 11.0, 1.0, 0)?);
|
||||
println!("{:?}", v);
|
||||
Ok(())
|
||||
}
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```
|
||||
Some(10.5)
|
||||
```
|
||||
|
||||
`(12 + 8 + 2·11) / 4 = 42 / 4 = 10.5`. This matches the `reference_value`
|
||||
test in `crates/wickra-core/src/indicators/weighted_close.rs`.
|
||||
|
||||
### Python
|
||||
|
||||
```python
|
||||
import numpy as np
|
||||
import wickra as ta
|
||||
|
||||
wc = ta.WeightedClose()
|
||||
print(wc.batch(np.array([12.0]), np.array([8.0]), np.array([11.0])))
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```
|
||||
[10.5]
|
||||
```
|
||||
|
||||
### Node
|
||||
|
||||
```javascript
|
||||
const ta = require('wickra');
|
||||
const wc = new ta.WeightedClose();
|
||||
console.log(wc.batch([12], [8], [11]));
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```
|
||||
[ 10.5 ]
|
||||
```
|
||||
|
||||
## Interpretation
|
||||
|
||||
The weighted close sits on the spectrum between the raw close and the
|
||||
[`TypicalPrice`](Indicator-TypicalPrice.md): closer to the close, but still
|
||||
nudged by the bar's range. Use it as a drop-in close replacement when you want
|
||||
the settlement to dominate without ignoring the extremes entirely.
|
||||
|
||||
## Common pitfalls
|
||||
|
||||
- **Feeding it scalar prices.** It needs the full `high`/`low`/`close` bar.
|
||||
|
||||
## References
|
||||
|
||||
The Weighted Close; the `(H + L + 2C) / 4` definition is standard (TA-Lib's
|
||||
`WCLPRICE`).
|
||||
|
||||
## See also
|
||||
|
||||
- [Indicator-TypicalPrice.md](Indicator-TypicalPrice.md) — `(H + L + C) / 3`.
|
||||
- [Indicator-MedianPrice.md](Indicator-MedianPrice.md) — `(H + L) / 2`.
|
||||
- [Indicators-Overview.md](../../Indicators-Overview.md) — the full taxonomy.
|
||||
Reference in New Issue
Block a user