use std::f64;
use crate::common::{validate_inputs, validate_options};
pub use crate::indicator_types::TIndicatorState;
use crate::indicators::max::State as MaxState;
use crate::indicators::medprice::calc as calc_medprice;
use crate::indicators::min::State as MinState;
use crate::ring_buffer::single_buffer::generic_buffer::Buffer;
use crate::ring_buffer::single_buffer::mirror_buffer::{MinMaxBuffer, MirrorBuffer};
use crate::types::{DisplayGroup, DisplayType, IndicatorError, IndicatorType, Info};
use serde::{Deserialize, Serialize};
pub const INPUTS_WIDTH: usize = 2;
pub const OPTIONS_WIDTH: usize = 1;
#[cfg(feature = "simd_assets")]
pub use crate::indicators::simd_indicators::fisher_simd::indicator_by_assets;
#[cfg(feature = "simd_options")]
pub use crate::indicators::simd_indicators::fisher_simd::indicator_by_options;
#[cfg(feature = "simd_assets")]
pub mod by_assets {
pub use crate::indicators::simd_indicators::fisher_simd::indicator_by_assets as indicator;
}
#[cfg(feature = "simd_options")]
pub mod by_options {
pub use crate::indicators::simd_indicators::fisher_simd::indicator_by_options as indicator;
}
#[derive(Serialize, Deserialize)]
pub struct IndicatorState {
state: State,
period: usize,
}
impl IndicatorState {
pub fn new(state: State, period: usize) -> Self {
Self { state, period }
}
}
impl TIndicatorState<2> for IndicatorState {
fn batch_indicator(
&mut self,
inputs: &[&[f64]; INPUTS_WIDTH],
_optional_outputs: Option<&[bool]>,
) -> Result<Vec<Vec<f64>>, IndicatorError> {
validate_inputs(inputs, 1)?;
let (mut fisher_line, mut signal_line) = {
let capacity = inputs[0].len();
(
crate::uninit_vec!(f64, capacity),
crate::uninit_vec!(f64, capacity),
)
};
let [high, low] = inputs;
match self.period {
1..=12 => {
cycle_fisher::<1>(
(high, low),
self.period,
(&mut fisher_line, &mut signal_line),
&mut self.state,
);
}
13..30 => {
cycle_fisher::<4>(
(high, low),
self.period,
(&mut fisher_line, &mut signal_line),
&mut self.state,
);
}
_ => {
cycle_fisher::<8>(
(high, low),
self.period,
(&mut fisher_line, &mut signal_line),
&mut self.state,
);
}
}
Ok(vec![fisher_line, signal_line])
}
}
#[derive(Serialize, Deserialize)]
pub struct State {
pub buffer: Buffer,
pub min_state: MinState,
pub max_state: MaxState,
pub val1: f64,
pub fish: f64,
}
impl State {
pub fn new(high: f64, low: f64, period: usize) -> Self {
let medprice = calc_medprice(high, low);
let mut buffer = Buffer::new(period);
buffer.push(medprice);
State {
buffer,
min_state: MinState::new(medprice, period),
max_state: MaxState::new(medprice, period),
val1: 0.0,
fish: 0.0,
}
}
pub fn init_state(
high: &[f64],
low: &[f64],
period: usize,
fisher_line: &mut [f64],
signal_line: &mut [f64],
) -> Self {
let mut state = Self::new(high[0], low[0], period);
let mut i = 1;
while state.buffer.get_count() < state.buffer.get_capacity() - 1 {
state.buffer.push(calc_medprice(high[i], low[i]));
i += 1;
}
(fisher_line[0], signal_line[0]) = calc::<1>(&mut state, high[i], low[i], period);
state
}
}
pub const INFO: Info = Info {
name: "fisher",
full_name: "Fisher Transform",
indicator_type: IndicatorType::Momentum,
inputs: &["high", "low"],
options: &["period"],
outputs: &["fisher", "fisher_signal"],
optional_outputs: &[],
display_groups: &[DisplayGroup {
offset: None,
id: "fisher",
label: "FISHER",
display_type: DisplayType::Indicator,
outputs: &["fisher", "fisher_signal"],
}],
};
pub fn min_data(options: &[f64]) -> usize {
options[0] as usize
}
pub fn output_length(data_len: usize, options: &[f64]) -> usize {
data_len - min_data(options) + 1
}
pub fn indicator(
inputs: &[&[f64]; INPUTS_WIDTH],
options: &[f64; OPTIONS_WIDTH],
_optional_outputs: Option<&[bool]>,
) -> Result<(Vec<Vec<f64>>, IndicatorState), IndicatorError> {
validate_options(options)?;
validate_inputs(inputs, min_data(options))?;
let period = options[0] as usize;
let [high, low] = inputs;
let (mut fisher_line, mut signal_line) = {
let capacity = output_length(high.len(), options);
(vec![0.0; capacity], vec![0.0; capacity])
};
let mut state = State::init_state(high, low, period, &mut fisher_line, &mut signal_line);
let outputs = (&mut fisher_line[1..], &mut signal_line[1..]);
let inputs = (&high[period..], &low[period..]);
match period {
1..=4 => {
cycle_fisher::<1>(inputs, period, outputs, &mut state);
}
5..30 => {
cycle_fisher::<4>(inputs, period, outputs, &mut state);
}
_ => {
cycle_fisher::<8>(inputs, period, outputs, &mut state);
}
}
Ok((
vec![fisher_line, signal_line],
IndicatorState { state, period },
))
}
fn cycle_fisher<const N: usize>(
inputs: (&[f64], &[f64]),
period: usize,
output_lines: (&mut [f64], &mut [f64]),
state: &mut State,
) {
let (fisher_line, signal_line) = output_lines;
let (high, low) = inputs;
for i in 0..high.len() {
let (h, l) = unsafe { (*high.get_unchecked(i), *low.get_unchecked(i)) };
let (fisher, signal) = calc::<N>(state, h, l, period);
unsafe {
*fisher_line.get_unchecked_mut(i) = fisher;
*signal_line.get_unchecked_mut(i) = signal;
}
}
}
#[inline(always)]
pub fn calc<const N: usize>(state: &mut State, high: f64, low: f64, period: usize) -> (f64, f64) {
let medprice = calc_medprice(high, low);
state.buffer.push(medprice);
let (min, _) = state
.buffer
.min::<N>(&mut state.min_state, medprice, period);
let (max, _) = state
.buffer
.max::<N>(&mut state.max_state, medprice, period);
calc_fisher(min, max, medprice, state)
}
#[inline(always)]
fn calc_fisher(min: f64, max: f64, medprice: f64, state: &mut State) -> (f64, f64) {
const PRICE_WEIGHT: f64 = 0.66; const SMOOTH_WEIGHT: f64 = 0.67; const MIN_MM: f64 = 0.001;
let mut val1 = state.val1;
let mm = (max - min).max(MIN_MM);
val1 = PRICE_WEIGHT.mul_add((medprice - min) / mm - 0.5, SMOOTH_WEIGHT * val1);
if val1 > 0.99 {
val1 = 0.999;
} else if val1 < -0.99 {
val1 = -0.999;
}
state.val1 = val1;
let signal = state.fish;
let ln_arg = (1.0 + val1) / (1.0 - val1);
state.fish = 0.5 * (ln_arg.ln() + signal); (state.fish, signal)
}