Moving Constant Bands
Moving Constant Bands
Moving Constant Bands are a volatility indicator that creates price bands around a moving average using a statistical deviation measure multiplied by a constant. This indicator is a generic implementation of Bollinger Bands, allowing traders to choose different types of moving averages and deviation models to adapt the bands to their specific trading style and market conditions.
What It Measures
Moving Constant Bands measure price volatility and potential overbought or oversold conditions by establishing dynamic bands around a central moving average. The indicator consists of three lines: an upper band (moving average + multiplier × deviation), a middle line (the moving average itself), and a lower band (moving average - multiplier × deviation). Unlike fixed envelopes, these bands expand and contract based on the chosen deviation model, reflecting current market volatility.
When to Use
This indicator is most effective in trending and volatile markets where dynamic adjustment to price movements is valuable. It works well for identifying potential reversal points when prices reach the band boundaries, and can be used to generate buy signals when prices touch the lower band and sell signals when prices touch the upper band. The flexibility to choose different models (Simple Moving Average, Exponential Moving Average, Median, etc.) and deviation measures (Standard Deviation, Mean Absolute Deviation) makes it adaptable to various asset classes and market conditions.
Interpretation
Traders interpret the indicator by monitoring price interactions with the bands and observing band width changes. When price touches or breaks below the lower band, it may indicate an oversold condition and a potential buying opportunity. Conversely, when price touches or breaks above the upper band, it may indicate an overbought condition and a potential selling opportunity. The width of the bands reflects volatility—wider bands indicate higher volatility and more uncertain conditions, while narrower bands suggest lower volatility and potential consolidation before a breakout. The multiplier parameter controls sensitivity, with higher values creating wider bands that generate fewer but potentially more reliable signals.
Default Usage
use rust_ti::candle_indicators::bulk::moving_constant_bands;
use rust_ti::{ConstantModelType, DeviationModel};
pub fn main() {
// fetch the data in your preferred way
// let close = vec![...]; // closing prices
let moving_constant_bands = moving_constant_bands(&close, ConstantModelType::SimpleMovingAverage, DeviationModel::StandardDeviation, 2.0, 20);
println!("{:?}", moving_constant_bands);
}
import pytechnicalindicators as pti
# fetch the data in your preferred way
# close = [...] # closing prices
moving_constant_bands = pti.candle_indicators.bulk.moving_constant_bands(close, model="SimpleMovingAverage", deviation="StandardDeviation", multiplier=2.0, period=20)
print(moving_constant_bands)
// WASM import
import init, { candle_bulk_movingConstantBands, ConstantModelType, DeviationModelType } from 'https://cdn.jsdelivr.net/npm/ti-engine@latest/dist/web/ti_engine.js';
await init();
// fetch the data in your preferred way
// const close = [...]; // closing prices
const movingConstantBands = candle_bulk_movingConstantBands(close, ConstantModelType["SimpleMovingAverage"], DeviationModelType["StandardDeviation"], 2.0, 20);
console.log(movingConstantBands);
Optimization
The best way to determine what the best parameters for your indicator are is to build a simple optimization loop that tests all possible parameter combinations between a defined min and max value, and rate the output.
Below is an example of how to do this in Rust.
use rust_ti::chart_trends::{peaks, valleys};
use rust_ti::candle_indicators::bulk::moving_constant_bands;
use rust_ti::{ConstantModelType, DeviationModel};
use std::time::Instant;
fn proximity_rating(fuzzed_location: &usize, price_location: &usize) -> f64 {
1.0 / (*fuzzed_location as f64 - *price_location as f64).abs()
}
pub fn main() {
// fetch the data in your preferred way
let indicator_loop = Instant::now();
// get buy and sell points, in an ideal world we would buy at the lowest point in the dip and sell at the highest point in the peak
// In the course of a 20-day period (1 month of trading days), we want to find the highest peak and lowest valley within 5 days of each other
let sell_points = peaks(&close, 20, 5).into_iter().map(|(_, i)| i).collect::<Vec<usize>>();
let buy_points = valleys(&close, 20, 5).into_iter().map(|(_, i)| i).collect::<Vec<usize>>();
// Define the ranges for optimization
let max_period = 126;
let min_period = 2;
let min_constant_multiplier = 0;
let max_constant_multiplier = 1000;
let fuzz_parameter = 5; // Allowable distance from buy/sell points
// Store the best parameters found
let mut best_rating = 0.0;
let mut best_period = 0;
let mut best_constant_multiplier = 0.0;
let mut best_model = ConstantModelType::SimpleMovingAverage;
let mut best_deviation_model = DeviationModel::StandardDeviation;
let mut best_indicators = vec![];
let total_count = (max_period - min_period) * (max_constant_multiplier - min_constant_multiplier) * 5 * 2;
let mut iteration_count = 0;
println!("
Running optimization loop with {} total iterations...", total_count);
for multiplier in min_constant_multiplier..=max_constant_multiplier {
let multiplier = multiplier as f64 / 1000.0;
for &ma_type in &[ConstantModelType::SimpleMovingAverage, ConstantModelType::ExponentialMovingAverage, ConstantModelType::SmoothedMovingAverage, ConstantModelType::SimpleMovingMedian, ConstantModelType::SimpleMovingMode] {
for &deviation_type in &[DeviationModel::StandardDeviation, DeviationModel::MeanAbsoluteDeviation] {
for period in min_period..=max_period {
iteration_count += 1;
if iteration_count % (total_count / 20) == 0 {
let next_log_percent = (iteration_count * 100) / total_count;
println!("Optimization is {}% complete...", next_log_percent);
}
let indicators = moving_constant_bands(&close, ma_type, deviation_type, multiplier, period);
let mut rating = vec![];
let mut matched_sell = vec![];
let mut matched_buy = vec![];
for i in 0..indicators.len() {
let price_location = i + period + 1; // Adjust for indicator lag
if i >= price_location { break; }
if price_location >= close.len() { break; }
let oversold = indicators[i].0;
let overbought = indicators[i].2;
if close[price_location] > overbought {
if sell_points.contains(&price_location) {
// If sell point == indicator signal, rate positively
rating.push(1.0);
matched_sell.push(price_location);
} else if buy_points.contains(&price_location) {
// If buy point == sell signal, rate negatively
rating.push(-1.0);
} else {
let mut found_sell = false;
for fuzzed_location in (price_location - fuzz_parameter)..=(price_location + fuzz_parameter) {
// It's ok if we count multiple times for fuzzed locations as we reduce the rating
// based off of distance from the actual sell point which will impact the final rating
if sell_points.contains(&fuzzed_location) {
rating.push(proximity_rating(&fuzzed_location, &price_location));
matched_sell.push(fuzzed_location);
found_sell = true;
}
if buy_points.contains(&fuzzed_location) {
// Note the `-` here to penalize for selling instead of buying
if !matched_sell.contains(&fuzzed_location) {
rating.push(-proximity_rating(&fuzzed_location, &price_location));
}
}
}
if !found_sell {
rating.push(0.0);
}
}
} else if close[price_location] < oversold {
if buy_points.contains(&price_location) {
// If buy point == indicator signal, rate positively
rating.push(1.0);
matched_buy.push(price_location);
} else if sell_points.contains(&price_location) {
rating.push(-1.0);
} else {
let mut found_buy = false;
for fuzzed_location in (price_location - fuzz_parameter)..=(price_location + fuzz_parameter) {
// It's ok if we count multiple times for fuzzed locations as we reduce the rating
// based off of distance from the actual sell point which will impact the final rating
if buy_points.contains(&fuzzed_location) {
rating.push(proximity_rating(&fuzzed_location, &price_location));
matched_buy.push(fuzzed_location);
found_buy = true;
}
if sell_points.contains(&fuzzed_location) {
// Note the `-` here to penalize for buying instead of selling
if !matched_buy.contains(&fuzzed_location) {
rating.push(-proximity_rating(&fuzzed_location, &price_location));
}
}
}
if !found_buy {
rating.push(0.0);
}
}
}
}
// Look for any missed buy/sell points and penalize
for missed_sell in sell_points.iter() {
if !matched_sell.contains(missed_sell) {
rating.push(-1.0);
}
}
for missed_buy in buy_points.iter() {
if !matched_buy.contains(missed_buy) {
rating.push(-1.0);
}
}
let total_rating: f64 = rating.iter().sum::<f64>() / (rating.len() as f64);
if total_rating > best_rating {
best_rating = total_rating;
best_period = period;
best_constant_multiplier = multiplier;
best_model = ma_type;
best_deviation_model = deviation_type;
best_indicators = indicators.clone();
}
}
}
}
}
println!(
"Indicators optimization loop took {} s to run",
indicator_loop.elapsed().as_secs()
);
println!("
Best Indicator parameters found:");
println!("period = {}", best_period);
println!("model = {:?}", best_model);
println!("constant_multiplier = {}", best_constant_multiplier);
println!("deviation_model = {:?}", best_deviation_model);
println!("Rating: {}", best_rating);
println!("Best Indicator values: {:?}", best_indicators);
}
Optimization Output
Below is an example output from the optimization code above run on a year of S&P data.
Best Indicator parameters found:
period = 22
model = SimpleMovingMedian
constant_multiplier = 1.88
deviation_model = MeanAbsoluteDeviation
Rating: 0.35457227138643066
Best Indicator values: [(5129.586889256198, 5203.96, 5278.333110743802), (5118.812003305785, 5203.96, 5289.107996694215), (5108.492823140496, 5203.96, 5299.4271768595045), ...]
Interactive Chart
To better illustrate how the indicator performs with different parameters, an interactive chart is provided below comparing default parameters (blue) with optimized parameters (green).
Analysis
The optimized Moving Constant Bands strategy (22-period Simple Moving Median with 1.88 multiplier and Mean Absolute Deviation) slightly outperformed the default configuration (20-period Simple Moving Average with 2.0 multiplier and Standard Deviation). Starting with $1,000 and investing 20% per trade, the optimized parameters generated a profit of $9.07 (0.91% gain) with 17 trades executed, while the default parameters resulted in a profit of $7.12 (0.71% gain) with 13 trades. The optimized strategy's use of the median instead of mean and MAD instead of standard deviation reduces sensitivity to price outliers, while the slightly tighter bands (1.88 vs 2.0 multiplier) generated more trading signals that captured smaller price movements effectively.
Optimized trading simulation
- SideLONG
- Shares0.0345315909273988
- Entry$5955.25
- Value$194.72
Default trading simulation
- SideLONG
- Shares0.034186107222101976
- Entry$5955.25
- Value$192.77
Trading simulation code
For those you want to run their own simulation to compare results
use rust_ti::candle_indicators::bulk::moving_constant_bands;
use rust_ti::{ConstantModelType, DeviationModel};
fn chart_simulate_trading(best_indicator: &[(f64, f64, f64)], best_period: usize, close: &[f64]) {
println!("
--- Trading Simulation ---");
let initial_capital = 1000.0;
let mut capital = initial_capital;
let investment_pct = 0.20;
struct Position {
entry_price: f64,
shares: f64,
}
let mut open_long: Option<Position> = None;
let mut open_short: Option<Position> = None;
// Print table header
println!("{:<5} | {:<19} | {:<10} | {:<10} | {:<12} | {:<15} | {:<10}",
"Day", "Event", "Indicator", "Price", "Shares", "Capital", "P/L");
println!("{}", "-".repeat(95));
for i in 0..best_indicator.len() {
let price_index = i + best_period + 1;
if price_index >= close.len() { break; }
let indicator_overbought = best_indicator[i].2;
let indicator_oversold = best_indicator[i].0;
let current_price = close[price_index];
let day = price_index;
// --- Handle Long Position ---
if let Some(long_pos) = open_long.take() {
if current_price > indicator_overbought as f64 {
let sale_value = long_pos.shares * current_price;
let profit = sale_value - (long_pos.shares * long_pos.entry_price);
capital += sale_value;
println!("{:<5} | {:<19} | {:<10.2} | ${:<9.2} | {:<12.4} | ${:<14.2} | ${:<9.2}",
day, "Sell (Close Long)", indicator_overbought, current_price, long_pos.shares, capital, profit);
} else {
open_long = Some(long_pos); // Put it back if not selling
}
} else if current_price < indicator_oversold as f64 && open_short.is_none() { // Don't buy if short is open
let investment = capital * investment_pct;
let shares_bought = investment / current_price;
open_long = Some(Position { entry_price: current_price, shares: shares_bought });
capital -= investment;
println!("{:<5} | {:<19} | {:<10.2} | ${:<9.2} | {:<12.4} | ${:<14.2} | {}",
day, "Buy (Open Long)", indicator_oversold, current_price, shares_bought, capital, "-");
}
// --- Handle Short Position ---
if let Some(short_pos) = open_short.take() {
if current_price < indicator_oversold as f64 {
let cost_to_cover = short_pos.shares * current_price;
let profit = (short_pos.shares * short_pos.entry_price) - cost_to_cover;
capital += profit; // Add profit to capital
println!("{:<5} | {:<19} | {:<10.2} | ${:<9.2} | {:<12.4} | ${:<14.2} | ${:<9.2}",
day, "Cover (Close Short)", indicator_oversold, current_price, short_pos.shares, capital, profit);
} else {
open_short = Some(short_pos); // Put it back if not covering
}
} else if current_price > indicator_overbought as f64 && open_long.is_none() { // Don't short if long is open
let short_value = capital * investment_pct;
let shares_shorted = short_value / current_price;
open_short = Some(Position { entry_price: current_price, shares: shares_shorted });
// Capital doesn't change when opening a short, it's held as collateral
println!("{:<5} | {:<19} | {:<10.2} | ${:<9.2} | {:<12.4} | ${:<14.2} | {}",
day, "Short (Open Short)", indicator_overbought, current_price, shares_shorted, capital, "-");
}
}
println!("
--- Final Results ---");
if let Some(pos) = open_long {
println!("Simulation ended with an OPEN LONG position:");
println!(" - Shares: {:.4}", pos.shares);
println!(" - Entry Price: ${:.2}", pos.entry_price);
let last_price = close.last().unwrap_or(&0.0);
let current_value = pos.shares * last_price;
capital += current_value;
println!(" - Position value at last price (${:.2}): ${:.2}", last_price, current_value);
println!("{{ position = "LONG", shares = {}, entry_price = "${:.2}", position_value_at_last_price = "${:.2}" }}", pos.shares, pos.entry_price, current_value);
}
if let Some(pos) = open_short {
println!("Simulation ended with an OPEN SHORT position:");
println!(" - Shares: {:.4}", pos.shares);
println!(" - Entry Price: ${:.2}", pos.entry_price);
let last_price = close.last().unwrap_or(&0.0);
let cost_to_cover = pos.shares * last_price;
let pnl = (pos.shares * pos.entry_price) - cost_to_cover;
capital += pnl;
println!(" - Unrealized P/L at last price (${:.2}): ${:.2}", last_price, pnl);
println!("{{ position = "SHORT", shares = {}, entry_price = "${:.2}", position_value_at_last_price = "${:.2}" }}", pos.shares, pos.entry_price, pnl);
}
let final_pnl = capital - initial_capital;
println!("
Initial Capital: ${:.2}", initial_capital);
println!("final_capital = "${:.2}"", capital);
println!("total_P_L = "${:.2}"", final_pnl);
}
fn main() {
// Fetch data and perform optimization as shown in the optimization code above
chart_simulate_trading(&best_indicators, best_period, &close);
println!("
Default Indicator values for comparison:");
let default_mcb = moving_constant_bands(&close, ConstantModelType::SimpleMovingAverage, DeviationModel::StandardDeviation, 2.0, 20);
println!("{:?}", default_mcb);
chart_simulate_trading(&default_mcb, 20, &close);
}