//@version=5
indicator("Advanced Fractal Dimension and Trend Analysis", overlay=true,
shorttitle="AFDTA")
// Input parameters
length = input.int(14, title="Fractal Dimension Period")
threshold_high = input.float(1.6, title="High Dimension Threshold")
threshold_low = input.float(1.4, title="Low Dimension Threshold")
zone_activation_threshold = input.float(1.5, title="Zone Activation
Threshold")
zone_width_multiplier = input.float(1.0, title="Zone Width Multiplier")
alpha_smooth = input.float(0.1, title="Smoothing Alpha")
band_filter_type = input.string("MA", title="Band Filter Type",
options=["MA", "WAP"])
hull_transformation = input.bool(true, title="Apply Hull
Transformation")
risk_management = input.bool(true, title="Enable Risk Management")
sl_tp_type = input.string("ATR", title="SL/TP Type", options=["Ticks",
"Pips", "Points", "Percent", "ATR", "Band Range", "Zone Range",
"Absolute"])
risk_scale = input.float(1.0, title="Risk Scale for SL/TP")
entry_dimension_low = input.float(1.4, title="Low Entry Dimension
Threshold")
entry_dimension_high = input.float(1.6, title="High Entry Dimension
Threshold")
max_consecutive_trades = input.int(3, title="Max Consecutive Trades")
exit_dimension_target = input.float(1.6, title="Exit Dimension Target")
num_signals_for_exit = input.int(1, title="Number of Signals for 100%
Exit")
background_bands_intensity = input.int(10, title="Background Bands
Intensity Range", minval=1, maxval=10)
// Price data
price = close
// Band Shelf EQ Algorithm (Placeholder - Needs Implementation)
band_shelf_eq(data) =>
// Implement the Band Shelf EQ algorithm here
data
// Compute Fractal Dimension (Placeholder Function - Needs
Implementation)
calculate_fractal_dimension(data, length) =>
// Implement the fractal dimension calculation here
1.5 // Placeholder value
// Error Bands Calculation
error_bands(data, length, dimension) =>
basis = ta.sma(data, length)
multiplier = dimension
high_band = basis + multiplier
low_band = basis - multiplier
[high_band, low_band]
// High Dimension Zone Calculation
high_dimension_zone(dimension, length) =>
activation = dimension >= zone_activation_threshold
high_line = ta.sma(close, length) + zone_width_multiplier *
dimension
low_line = ta.sma(close, length) - zone_width_multiplier * dimension
mid_line = (high_line + low_line) / 2
[activation, high_line, low_line, mid_line]
// Trend Detection
detect_trend(band_high, band_low, zone_high, zone_low, trend_type) =>
trend = 0
switch trend_type
"Band Trend (Outer)" =>
if close > band_high
trend := 1 // Bullish
else if close < band_low
trend := -1 // Bearish
"Band Trend (Median)" =>
median_band = (band_high + band_low) / 2
if close > median_band
trend := 1 // Bullish
else if close < median_band
trend := -1 // Bearish
"Zone Trend (Expansion)" =>
if close > zone_high
trend := 1 // Bullish
else if close < zone_low
trend := -1 // Bearish
"Zone Trend (Outer Levels)" =>
if close > zone_high
trend := 1 // Bullish
else if close < zone_low
trend := -1 // Bearish
"Zone Trend (Median)" =>
median_zone = (zone_high + zone_low) / 2
if close > median_zone
trend := 1 // Bullish
else if close < median_zone
trend := -1 // Bearish
trend
// Signal Generation
generate_signals(trend, error_band_high, error_band_low, zone_mid,
entry_dimension_low, entry_dimension_high, max_consecutive_trades) =>
var float entry_signal = na
var float exit_signal = na
var int consecutive_trades = 0
// Ensure dimension is computed
dimension = calculate_fractal_dimension(close, length)
if trend == 1
// Bullish entry condition
if close > error_band_high and dimension >= entry_dimension_low
and dimension <= entry_dimension_high
entry_signal := close
consecutive_trades := consecutive_trades + 1
if consecutive_trades > max_consecutive_trades
consecutive_trades := 0 // Reset count
else if trend == -1
// Bearish entry condition
if close < error_band_low and dimension >= entry_dimension_low
and dimension <= entry_dimension_high
entry_signal := close
consecutive_trades := consecutive_trades + 1
if consecutive_trades > max_consecutive_trades
consecutive_trades := 0 // Reset count
// Example logic for generating exit signals
if not na(entry_signal)
if (entry_signal > zone_mid and close < zone_mid) or
(entry_signal < zone_mid and close > zone_mid)
exit_signal := close
[entry_signal, exit_signal]
// Risk Management (SL/TP Calculation Placeholder - Needs
Implementation)
risk_management_sl_tp(entry_price, sl_tp_type, scale) =>
var float sl = na
var float tp = na
if sl_tp_type == "ATR"
atr = ta.atr(14)
sl := entry_price - scale * atr
tp := entry_price + scale * atr
else
// Implement other SL/TP types here
sl := entry_price - scale * close // Example calculation
tp := entry_price + scale * close // Example calculation
[sl, tp]
// Compute Fractal Dimension
dimension = calculate_fractal_dimension(price, length)
// Compute Error Bands
[error_band_high, error_band_low] = error_bands(price, length,
dimension)
// Compute High Dimension Zone
[zone_active, zone_high, zone_low, zone_mid] =
high_dimension_zone(dimension, length)
// Detect Trend
trend_type = input.string("Band Trend (Outer)", title="Select Trend
Type", options=["Band Trend (Outer)", "Band Trend (Median)", "Zone Trend
(Expansion)", "Zone Trend (Outer Levels)", "Zone Trend (Median)"])
trend = detect_trend(error_band_high, error_band_low, zone_high,
zone_low, trend_type)
// Generate Signals
[entry_signal, exit_signal] = generate_signals(trend, error_band_high,
error_band_low, zone_mid, entry_dimension_low, entry_dimension_high,
max_consecutive_trades)
// Risk Management
[sl, tp] = risk_management_sl_tp(close, sl_tp_type, risk_scale)
// Intensity Buffer
var float buffer_intensity = na
var int buffer_counter = na
if na(buffer_intensity)
buffer_intensity := 0
buffer_counter := 0
else
if trend != 0
buffer_counter += 1
buffer_intensity := buffer_counter
else
buffer_intensity := 0
buffer_counter := 0
// Normalize color intensity to an integer value between 0 and 255
color_intensity = na(buffer_intensity) ? 0 : math.min(math.max(0,
int(buffer_intensity * (255 / background_bands_intensity))), 255)
// Background Color
bgcolor(color=color.new(trend == 1 ? color.green : color.red, 255 -
color_intensity), title="Background Trend Bands")
// Plotting
plot(zone_high, title="High Dimension Zone", color=color.green,
linewidth=2)
plot(zone_low, title="Low Dimension Zone", color=color.red, linewidth=2)
plot(error_band_high, title="Error Band High", color=color.blue,
linewidth=1)
plot(error_band_low, title="Error Band Low", color=color.orange,
linewidth=1)
// Plot Entry and Exit Signals
plotshape(series=entry_signal, location=location.belowbar,
color=color.green, style=shape.labelup, title="Entry Signal")
plotshape(series=exit_signal, location=location.abovebar,
color=color.red, style=shape.labeldown, title="Exit Signal")
// Alerts
alertcondition(entry_signal, title="Entry Alert", message="Entry Signal
Triggered")
alertcondition(exit_signal, title="Exit Alert", message="Exit Signal
Triggered")
alertcondition(close <= sl, title="Stop Loss Alert", message="Stop Loss
Triggered")
alertcondition(close >= tp, title="Take Profit Alert", message="Take
Profit Triggered")