Author MEssage:
This is a trend-following indicator that utilizes the Fractal Adaptive Moving Average (FRAMA) to create a dynamic channel around the price. The FRAMA Channel helps identify uptrends, downtrends, and ranging markets by examining the relationship between the price and the channel's boundaries. It also marks trend changes with arrows, optionally displaying either price values or average volume at these key points.
CODE:
CSS:
#// Indicator for TOS
#// © BigBeluga
#indicator("FRAMA Channel [BigBeluga]", overlay=true, max_labels_count = 500)
# Converted by Sam4Cok@Samer800 - 12/2024
input timeframe = AggregationPeriod.MIN;
input colorBars = yes;
input SignalsData = {default "Price", "Average Volume", "Don't Show"}; # // Source data for signals
input source = FundamentalType.HL2; # // Use hl2 as the default source
input framaLength = 26; # // Length for FRAMA calculation
input BandsDistance = 1.5; # // Distance for channel bands
input framaFactor = 4.6;
def na = Double.NaN;
def cap = GetAggregationPeriod();
def tf = Max(cap, timeframe);
def bar_index = bar_index[1] + 1;
def src = Fundamental(FundamentalType = source, Period = tf);
def avgVol = SignalsData == SignalsData."Average Volume";
def hilo = SignalsData == SignalsData."Price";
def N = Ceil(framaLength / 2);
#-- Color
DefineGlobalColor("up", CreateColor(0, 117, 117));
DefineGlobalColor("dn", CreateColor(127, 0, 127));
#/ CALCULATIONS--
#// Perform calculations for the FRAMA Channel
def volatility = Average(high(Period = tf) - low(Period = tf), 200); # // Calculate volatility using the average true range
def volAvg = Average(volume(Period = tf), 10) / 1000;
#// Calculate N3 for the fractal dimension
def HH = Highest(high(Period = tf), framaLength);
def LL = Lowest(low(Period = tf), framaLength);
def vN3 = (HH - LL) / framaLength;
#// Loop to calculate N1
def vHH1 = fold countH = 0 to N with pH = high(Period = tf) do
if high(Period = tf)[countH] > pH then high(Period = tf)[countH] else pH;
def vLL1 = fold countL = 0 to N with pL = low(Period = tf) do
if low(Period = tf)[countL] < pL then low(Period = tf)[countL] else pL;
def vN1 = (vHH1 - vLL1) / N;
def vHH2 = fold countH1 = N to framaLength with pH1 = high(Period = tf)[N] do
if high(Period = tf)[countH1] > pH1 then high(Period = tf)[countH1] else pH1;
def vLL2 = fold countL1 = N to framaLength with pL1 = low(Period = tf)[N] do
if low(Period = tf)[countL1] < pL1 then low(Period = tf)[countL1] else pL1;
def vN2 = (vHH2 - vLL2) / N;
#// Calculate the fractal dimension
def vDimen = if (vN1 > 0 and vN2 > 0 and vN3 > 0) then
(Log(vN1 + vN2) - Log(vN3)) / Log(2) else vDimen[1];
#// Calculate alpha for FRAMA
def alpha = Exp(-framaFactor * (vDimen - 1));
def valpha = Max(Min(alpha, 1), 0.01); # // Clamp alpha between 0.01 and 1
#/ Calculate the FRAMA filtered value
def Filt;
def Filt_ = if isNaN(Filt[1]) then src else if !Filt[1] then src else valpha * src + (1 - valpha) * Filt[1];
Filt = Average(if (bar_index < framaLength + 1) then src else Filt_, 5); # // Apply SMA for smoothing
#// Calculate the channel bands
def Filt1 = Filt + volatility * BandsDistance;
def Filt2 = Filt - volatility * BandsDistance;
#// Define conditions for plotting and coloring
def cross = (close(Period = tf) crosses Filt);
def break_up = (src crosses above Filt1) and !IsNaN(close[1]);
def break_dn = (src crosses below Filt2) and !IsNaN(close[1]);
def color = if break_up then 1 else if break_dn then -1 else if cross then 0 else color[1];
def col = if IsNaN(color) then 0 else color;
def count1;
def count2;
if break_up {
count1 = count1[1] + 1;
count2 = 0;
} else if break_dn {
count1 = 0;
count2 = count2[1] + 1;
} else {
count1 = count1[1];
count2 = count2[1];
}
#-- plot
plot bandUp = if Filt1 then Filt1 else na;
plot base = if Filt then Filt else na;
plot bandDn = if Filt2 then Filt2 else na;
base.SetStyle(Curve.SHORT_DASH);
bandUp.AssignValueColor(if col > 0 then Color.CYAN else if col < 0 then Color.MAGENTA else Color.GRAY);
base.AssignValueColor(if col > 0 then Color.CYAN else if col < 0 then Color.MAGENTA else Color.GRAY);
bandDn.AssignValueColor(if col > 0 then Color.CYAN else if col < 0 then Color.MAGENTA else Color.GRAY);
AddCloud(if (col > 0 or col[-1] > 0) then bandUp else na, bandDn, GlobalColor("up"));
AddCloud(if (col < 0 or col[-1] < 0) then bandUp else na, bandDn, GlobalColor("dn"));
AddCloud(if (!col or !col[-1]) then bandUp else na, bandDn, Color.DARK_GRAY);
#-- Signal
AddChartBubble(hilo and count1 == 1 and count1 != count1[1], Filt2, AsDollars(close(Period = tf)), Color.CYAN, no);
AddChartBubble(hilo and count2 == 1 and count2 != count2[1], Filt1, AsDollars(close(Period = tf)), Color.MAGENTA);
AddChartBubble(avgVol and count1 == 1 and count1 != count1[1], Filt2, AsText(volAvg) + "k", Color.CYAN, no);
AddChartBubble(avgVol and count2 == 1 and count2 != count2[1], Filt1, AsText(volAvg) + "k", Color.MAGENTA);
# Bar Color
AssignPriceColor(if !colorBars then Color.CURRENT else
if col > 0 then Color.GREEN else if col < 0 then Color.RED else Color.GRAY);
# -- END of CODE