Skip to content

FOSC — Forecast Oscillator

Measures the percentage difference between the current price and the linear regression forecast value for that bar, showing how much prices deviate from their projected trend.

Inputs: [real]  |  Options: [period]  |  Outputs: [fosc]

Basic

use tulip_rs::indicators::fosc::indicator;

let close = vec![81.59, 81.06, 82.87, 83.00, 83.61,
                 83.15, 82.84, 83.99, 84.55, 84.36_f64];

let (outputs, _state) = indicator(&[close.as_slice()], &[14.0], None).unwrap();
println!("FOSC(14): {:?}", outputs[0]);

// State continuation
let partial = close[..8].to_vec();
let (outputs2, mut state) = indicator(&[partial.as_slice()], &[14.0], None).unwrap();
println!("Partial FOSC: {:?}", outputs2[0]);

let new_close = close[8..].to_vec();
let continued = state.batch_indicator(&[new_close.as_slice()], None).unwrap();
println!("Continued FOSC: {:?}", continued[0]);
import numpy as np
import tulip_rs

close = np.array([81.59, 81.06, 82.87, 83.00, 83.61,
                  83.15, 82.84, 83.99, 84.55, 84.36], dtype=np.float64)

outputs, state = tulip_rs.indicators.fosc.indicator([close], [14.0])
print("FOSC(14):", outputs[0])

# State continuation
partial = close[:8]
outputs2, state = tulip_rs.indicators.fosc.indicator([partial], [14.0])
new_close = close[8:]
continued = state.batch_indicator([new_close])
print("Continued FOSC:", continued[0])
import * as ti from 'tulip-rs-node';

const close = [81.59, 81.06, 82.87, 83.00, 83.61,
               83.15, 82.84, 83.99, 84.55, 84.36,
               85.53, 86.54, 86.89, 87.77, 87.29];

const [outputs, state] = ti.fosc.indicator([close], [14]);
console.log('FOSC(14):', outputs[0]);

// State continuation
const [, state2] = ti.fosc.indicator([close.slice(0, -5)], [14]);
const continued = state2.batchIndicator([close.slice(-5)]);
console.log('Continued FOSC:', continued[0]);
import { init } from 'tulip-rs-wasm';
import * as ti from 'tulip-rs-wasm';

await init(); // bundler resolves the WASM asset automatically

const close = [81.59, 81.06, 82.87, 83.00, 83.61,
               83.15, 82.84, 83.99, 84.55, 84.36,
               85.53, 86.54, 86.89, 87.77, 87.29];

const [outputs, state] = ti.fosc.indicator([close], [14]);
console.log('FOSC(14):', outputs[0]);

// State continuation
const [, state2] = ti.fosc.indicator([close.slice(0, -5)], [14]);
const continued = state2.batchIndicator([close.slice(-5)]);
console.log('Continued FOSC:', continued[0]);

Optional Outputs

fosc exposes 4 optional outputs: tsf, linreg, linregslope, linregintercept. Pass a boolean mask as the third argument — one bool per optional output, in order.

use tulip_rs::indicators::fosc::indicator;

let close = vec![81.59, 81.06, 82.87, 83.00, 83.61,
                 83.15, 82.84, 83.99, 84.55, 84.36_f64];

let mask = [true, true, false, false]; // one per optional output
let (outputs, _state) = indicator(&[close.as_slice()], &[14.0], Some(&mask)).unwrap();

let fosc   = &outputs[0]; // fosc (primary)
let tsf    = &outputs[1]; // tsf (optional — requested)
let linreg = &outputs[2]; // linreg (optional — requested)
// linregslope and linregintercept not requested
import numpy as np
import tulip_rs

close = np.array([81.59, 81.06, 82.87, 83.00, 83.61,
                  83.15, 82.84, 83.99, 84.55, 84.36], dtype=np.float64)

outputs, state = tulip_rs.indicators.fosc.indicator(
    [close], [14.0],
    optional_outputs=[True, True, False, False],
)

fosc   = outputs[0]  # fosc (primary)
tsf    = outputs[1]  # tsf (optional — requested)
linreg = outputs[2]  # linreg (optional — requested)
# linregslope and linregintercept not requested

fosc exposes 4 optional outputs: tsf, linreg, linregslope, linregintercept.

const [allOut] = ti.fosc.indicator([close], [14], [true, true, true, true]);
const fosc            = allOut[0]; // primary
const tsf             = allOut[1]; // optional 0: tsf
const linreg          = allOut[2]; // optional 1: linreg
const linregslope     = allOut[3]; // optional 2: linregslope
const linregintercept = allOut[4]; // optional 3: linregintercept

// Request only tsf
const [partial] = ti.fosc.indicator([close], [14], [true, false, false, false]);

SIMD

By assets — same period applied to 4 assets in parallel:

use tulip_rs::indicators::fosc::indicator_by_assets;

let a1 = vec![81.59, 81.06, 82.87, 83.00, 83.61, 83.15, 82.84, 83.99, 84.55, 84.36_f64];
let a2 = vec![72.10, 72.85, 73.40, 73.00, 74.20, 74.85, 75.10, 75.60, 76.00, 76.50_f64];
let a3 = vec![55.30, 55.80, 56.10, 56.40, 56.90, 57.20, 57.50, 57.80, 58.10, 58.40_f64];
let a4 = vec![100.1, 100.5, 101.0, 101.3, 101.8, 102.0, 102.5, 103.0, 103.3, 103.8_f64];

let inputs: [&[&[f64]; 1]; 4] = [
    &[a1.as_slice()],
    &[a2.as_slice()],
    &[a3.as_slice()],
    &[a4.as_slice()],
];

let results = indicator_by_assets::<4>(&inputs, &[14.0], None).unwrap();
for (i, asset_outputs) in results.0.iter().enumerate() {
    println!("Asset {}: {:?}", i + 1, asset_outputs[0]);
}

By options — same asset, 4 different periods in parallel:

use tulip_rs::indicators::fosc::indicator_by_options;

let close = vec![81.59, 81.06, 82.87, 83.00, 83.61,
                 83.15, 82.84, 83.99, 84.55, 84.36_f64];

let opts: [&[f64; 1]; 4] = [&[5.0], &[10.0], &[14.0], &[20.0]];

let results = indicator_by_options::<4>(&[close.as_slice()], &opts, None).unwrap();
for (i, opt_outputs) in results.0.iter().enumerate() {
    println!("Period set {}: {:?}", i + 1, opt_outputs[0]);
}

By assets — same period applied to N assets in parallel (must be 2, 4, 8, or 16):

import numpy as np
import tulip_rs

close = np.array([81.59, 81.06, 82.87, 83.00, 83.61,
                  83.15, 82.84, 83.99, 84.55, 84.36], dtype=np.float64)

simd_inputs = [[close], [close + 5.0], [close - 5.0], [close * 1.02]]
outputs_list, states = tulip_rs.indicators.fosc.simd_by_assets(simd_inputs, [14.0])
for i, out in enumerate(outputs_list):
    print(f"Asset {i + 1}: {out[0]}")

By options — same asset, N different periods in parallel:

import numpy as np
import tulip_rs

close = np.array([81.59, 81.06, 82.87, 83.00, 83.61,
                  83.15, 82.84, 83.99, 84.55, 84.36], dtype=np.float64)

simd_options = [[5.0], [10.0], [14.0], [20.0]]
outputs_list, states = tulip_rs.indicators.fosc.simd_by_options([close], simd_options)
for i, out in enumerate(outputs_list):
    print(f"Period set {i + 1}: {out[0]}")

By assets — same period applied to 4 assets in parallel:

const simdInputs = [[[...close]], [close.map(v => v * 1.1)], [close.map(v => v * 0.9)], [close.map(v => v * 1.02)]];
const [results] = ti.fosc.simdByAssets(simdInputs, [14]);
results.forEach((out, i) => console.log(`Asset ${i + 1}:`, out[0]));

By options — same asset, 4 different periods in parallel:

const simdOptions = [[5], [10], [14], [20]];
const [results] = ti.fosc.simdByOptions([close], simdOptions);
results.forEach((out, i) => console.log(`Period ${simdOptions[i][0]}:`, out[0]));