Psychological Line

Parameters: length = 20

Overview

Psychological Line converts a stream of closes into a simple participation measure: how many of the recent bars closed above the prior bar, expressed as a percentage of the active window. Instead of focusing on price distance or volatility, it focuses on the persistence of advancing periods inside a rolling sequence.

That makes it useful as a compact momentum breadth gauge. High readings indicate that most of the recent bars were positive, while low readings indicate persistent weakness. VectorTA supports candle-based input, direct slices, streaming updates, and batch sweeps across the window length.

Defaults: `length = 20`.

Implementation Examples

Run Psychological Line on a direct source slice or on candle closes with the default source.

use vector_ta::indicators::psychological_line::{
    psychological_line,
    PsychologicalLineInput,
    PsychologicalLineParams,
};
use vector_ta::utilities::data_loader::{Candles, read_candles_from_csv};

let direct = psychological_line(&PsychologicalLineInput::from_slice(
    &close,
    PsychologicalLineParams { length: Some(20) },
))?;

let candles: Candles = read_candles_from_csv("data/sample.csv")?;
let from_candles = psychological_line(&PsychologicalLineInput::with_default_candles(&candles))?;

println!("latest = {:?}", direct.values.last());
println!("candle latest = {:?}", from_candles.values.last());

API Reference

Input Methods
PsychologicalLineInput::from_candles(&Candles, "close", PsychologicalLineParams)
    -> PsychologicalLineInput

PsychologicalLineInput::from_slice(&[f64], PsychologicalLineParams)
    -> PsychologicalLineInput

PsychologicalLineInput::with_default_candles(&Candles)
    -> PsychologicalLineInput
Parameters Structure
pub struct PsychologicalLineParams {
    pub length: Option<usize>, // default 20
}
Output Structure
pub struct PsychologicalLineOutput {
    pub values: Vec<f64>,
}
Validation & Warmup
  • The input slice must not be empty and must contain at least one finite value.
  • length must be greater than 0 and cannot exceed the available data length.
  • The direct path requires one prior comparison plus a full window of advancing-bar tests, so early values are initialized to NaN.
  • The stream returns None until it has seen enough comparisons to fill the rolling window.
  • The Python stream wrapper uses the reset-on-NaN path, so non-finite updates clear the rolling state.
  • Batch mode validates the length sweep and rejects non-batch kernels.
Builder, Streaming & Batch APIs
PsychologicalLineBuilder::new()
    .length(usize)
    .kernel(Kernel)
    .apply(&Candles)
    .apply_slice(&[f64])
    .into_stream()

PsychologicalLineStream::try_new(params)
stream.update(value) -> Option<f64>
stream.update_reset_on_nan(value) -> Option<f64>

PsychologicalLineBatchBuilder::new()
    .length_range(start, end, step)
    .length_static(usize)
    .kernel(Kernel)
    .apply_slice(&[f64])
    .apply_candles(&Candles, "close")

Python Bindings

Python exposes a scalar function returning one NumPy array, a stream class returning the latest Psychological Line value, and a batch helper returning a matrix plus the resolved length grid.

from vector_ta import (
    psychological_line,
    psychological_line_batch,
    PsychologicalLineStream,
)

values = psychological_line(close, length=20)

stream = PsychologicalLineStream(length=20)
point = stream.update(close[-1])

batch = psychological_line_batch(
    close,
    length_range=(10, 20, 5),
)

print(values[-1])
print(batch["rows"], batch["cols"])

JavaScript/WASM Bindings

The WASM layer exposes a scalar function returning one value series, a batch helper returning flattened matrix output, and allocation and into-buffer APIs for host-managed memory.

import init, {
  psychological_line_js,
  psychological_line_batch_js,
  psychological_line_alloc,
  psychological_line_free,
  psychological_line_into,
  psychological_line_into_host,
  psychological_line_batch_into,
} from "vector-ta-wasm";

await init();

const single = psychological_line_js(close, 20);

const batch = psychological_line_batch_js(close, {
  length_range: [10, 20, 5],
});

console.log(single);

CUDA Bindings (Rust)

Additional details for the CUDA bindings can be found inside the VectorTA repository.

Performance Analysis

Comparison:
View:
Placeholder data (no recorded benchmarks for this indicator)

Across sizes, Rust CPU runs about 1.14× faster than Tulip C in this benchmark.

Loading chart...

AMD Ryzen 9 9950X (CPU) | NVIDIA RTX 4090 (GPU)

Related Indicators