here my conversion with option of AlphaTrend and AlphaTrend Max.
Author states:
With Trend Magic; we had some problems.
Alpha Trend tries to solve those problems such as:
1-To minimize stop losses and overcome sideways market conditions.
2-To have more accurate BUY/SELL signals during trending market conditions.
3- To have significant support and resistance levels.
4- To bring together indicators from different categories that are compatible with each other and make a meaningful combination regarding momentum, trend, volatility, volume and trailing stop loss.
according to those purposes, Alpha Trend:
1- Acts like a dead indicator like its ancestor Magic Trend sideways market conditions and doesn't give many false signals.
2- With another line with 2 bars offsetted off the original one Alpha Trend have BUY and SELL signals from their crossovers.
BUY / LONG when Alpha Trend line crosses above its 2 bars offsetted line and there would be a green filling between them
SELL / SHORT when Alpha Trend line crosses below its 2 bars offsetted line and filling would be red then.
3- Alpha Trend lines
-act as support levels when an uptrend occurs trailing 1*ATR (default coefficient) distance from bar's low values
-conversely, act as resistance levels when a downtrend occurs trailing 1*ATR (default coefficient) distance from bar's high values
and acting as trailing stop losses
the more Alpha Trend lines straighter the more supports and resistances become stronger.
4- Trend Magic has CCI in calculation
Alpha Trend has MFI as momentum, but when there's no volume data MFI has 0 values, so there's a button to change calculation considering RSI after checking the relevant box to overcome this problem when there is no volume data in that chart.
Momentum: RSI and MFI
Trend: Magic Trend
Volatility: ATR,
Trailing STOP: ATR TRAILING STOP
Volume: MFI
Alpha trend is really a combination of different types...
default values:
coefficient: 1 which is the factor of trailing ATR value
common period: 14 which is the length of ATR MFI and RSI
UPDATE - Added MTF, minor signal fix
Author states:
With Trend Magic; we had some problems.
Alpha Trend tries to solve those problems such as:
1-To minimize stop losses and overcome sideways market conditions.
2-To have more accurate BUY/SELL signals during trending market conditions.
3- To have significant support and resistance levels.
4- To bring together indicators from different categories that are compatible with each other and make a meaningful combination regarding momentum, trend, volatility, volume and trailing stop loss.
according to those purposes, Alpha Trend:
1- Acts like a dead indicator like its ancestor Magic Trend sideways market conditions and doesn't give many false signals.
2- With another line with 2 bars offsetted off the original one Alpha Trend have BUY and SELL signals from their crossovers.
BUY / LONG when Alpha Trend line crosses above its 2 bars offsetted line and there would be a green filling between them
SELL / SHORT when Alpha Trend line crosses below its 2 bars offsetted line and filling would be red then.
3- Alpha Trend lines
-act as support levels when an uptrend occurs trailing 1*ATR (default coefficient) distance from bar's low values
-conversely, act as resistance levels when a downtrend occurs trailing 1*ATR (default coefficient) distance from bar's high values
and acting as trailing stop losses
the more Alpha Trend lines straighter the more supports and resistances become stronger.
4- Trend Magic has CCI in calculation
Alpha Trend has MFI as momentum, but when there's no volume data MFI has 0 values, so there's a button to change calculation considering RSI after checking the relevant box to overcome this problem when there is no volume data in that chart.
Momentum: RSI and MFI
Trend: Magic Trend
Volatility: ATR,
Trailing STOP: ATR TRAILING STOP
Volume: MFI
Alpha trend is really a combination of different types...
default values:
coefficient: 1 which is the factor of trailing ATR value
common period: 14 which is the length of ATR MFI and RSI
UPDATE - Added MTF, minor signal fix
CSS:
#https://www.tradingview.com/script/o50NYLAZ-AlphaTrend/
#// author © KivancOzbilgic
#// developer © KivancOzbilgic
# Converted and mod by Sam4Cok@Samer800 - 09/2022
# Added MTF option and minor signal fix by Sam4Cok@Samer800 - 03/2025
#indicator('AlphaTrend', shorttitle='AT',
input timeframe = AggregationPeriod.MIN;
input showLabel = no; # Show Lebel?
input showBubbles = yes;
input SignalType = {"Non", default "AlphaTrend", "AlphaMax", "Both"};
input MomType = {default "MFI", "RSI", "FireFly"};
input source = FundamentalType.CLOSE;
input CommonPeriod = 14; # 'Common Period'
input AlphaTrendWidth = 2;
input AlphaMaxLength = 10; # 'AlphaMaxLength'
input Multiplier = 1.1; # 'Multiplier'
def na = Double.NaN;
def GAP = GetAggregationPeriod();
def tf = Max(GAP, timeframe);
def last = IsNaN(close);
def lastHT = IsNaN(volume(Period = tf));
def src = Fundamental(source, Period = tf);
def lab = SignalType != SignalType."Non";
def both = SignalType == SignalType."Both";
def alTrend = SignalType == SignalType."AlphaTrend" or both;
def alMax = SignalType == SignalType."AlphaMax" or both;
#-- Color
DefineGlobalColor("up", CreateColor(33, 150, 243));
DefineGlobalColor("dn", CreateColor(156, 39, 176));
AddLabel(showLabel and lab, "Signal Type (" + SignalType + ")", Color.WHITE);
##### Script
#rescale(_src, _oldMin, _oldMax, _newMin, _newMax) =>
script rescale {
input _src = close;
input _oldMin = -50;
input _oldMax = 150;
input _newMin = 0;
input _newMax = 100;
def maxx = Max(_oldMax - _oldMin, 0.00001);
def rescale = _newMin + (_newMax - _newMin) * (_src - _oldMin) / maxx;
plot return = rescale;
}
#barssince(Condition) =>
script barssince {
input Condition = 0;
def barssince = if Condition then 0 else barssince[1] + 1;
plot return = barssince;
}
### Filters
def rsi1 = RSI(Price = src, Length = CommonPeriod);
#def mfi1 = MoneyFlowIndex(Length = CommonPeriod);
def vol = if !last then volume(Period = tf) / 100000 else vol[1];
def change = Fundamental(source, Period = tf) - Fundamental(source, Period = tf)[1];
def upper = sum(vol * (if change <= 0.0 then 0.0 else src), CommonPeriod);
def lower = sum(vol * (if change >= 0.0 then 0.0 else src), CommonPeriod);
def mfi1 = 100.0 - (100.0 / (1.0 + upper / lower));
AddLabel(1, change);
#def mfi1 = MoneyFlow(high(Period = tf), close(Period = tf), low(Period = tf), volume(Period = tf), CommonPeriod);
### FireFly Oscillator
def v2 = (high(Period = tf) + low(Period = tf) + close(Period = tf) * 2) / 4;
def v3 = ExpAverage(v2, CommonPeriod);
def v4 = StDev(v2, CommonPeriod);
def v44 = if v4 == 0 then 1 else v4;
def v5 = (v2 - v3) * 100 / v44;
def v6 = ExpAverage(v5, 3);
def v7 = ExpAverage(v6, CommonPeriod) + 100;
def ww = v7 / 2 - 4;
def FireFly = rescale(ww, -50, 150, 0, 100);
#######
def tr = TrueRange(high(Period = tf), close(Period = tf), low(Period = tf));
def nATR = Average(tr, CommonPeriod);
def upT = low(Period = tf) - nATR * Multiplier;
def dnT = high(Period = tf) + nATR * Multiplier;
#### AlphaTrend Calc
def TrendTh; # =
switch (MomType) {
case "RSI":
TrendTh = rsi1 >= 50;
case "FireFly":
TrendTh = FireFly >= 50;
default :
TrendTh = mfi1 >= 50;
}
def AlphaTrend = if TrendTh then
if upT < (AlphaTrend[1]) then (AlphaTrend[1]) else upT else
if dnT > (AlphaTrend[1]) then (AlphaTrend[1]) else dnT;
###########
def buySignalk = (AlphaTrend > AlphaTrend[AlphaTrendWidth]) and (AlphaTrend[1] <= AlphaTrend[AlphaTrendWidth]);
def sellSignalk = (AlphaTrend < AlphaTrend[AlphaTrendWidth]) and (AlphaTrend[1] >= AlphaTrend[AlphaTrendWidth]);
def K1 = barssince(buySignalk);
def K2 = barssince(sellSignalk);
def O1 = barssince(buySignalk[1]);
def O2 = barssince(sellSignalk[1]);
plot BuySig = if alTrend and buySignalk and O1 > K2 and K2 > 3 then AlphaTrend[AlphaTrendWidth] else na;
plot SellSig = if alTrend and sellSignalk and O2 > K1 and K1 > 3 then AlphaTrend[AlphaTrendWidth] else na;
BuySig.SetPaintingStrategy(PaintingStrategy.LINE_VS_SQUARES);
SellSig.SetPaintingStrategy(PaintingStrategy.LINE_VS_SQUARES);
BuySig.SetDefaultColor(Color.CYAN);
SellSig.SetDefaultColor(Color.MAGENTA);
AddChartBubble(showBubbles and BuySig, low , "Buy", GlobalColor("up"), no);
AddChartBubble(showBubbles and SellSig, high, "Sell", GlobalColor("dn"), yes);
#### Alpha Max
#//----
def hhama = Highest(high(Period = tf), AlphaMaxLength);
def llama = Lowest(low(Period = tf), AlphaMaxLength);
def hh = Max(Sign(hhama - hhama[1]), 0);
def ll = Max(Sign(llama - llama[1]) * -1, 0);
def val = if hh then 1 else if ll then 1 else 0;
def tc = Sqr(Average(val, AlphaMaxLength));
def AlphaMx = if IsNaN(AlphaMx[1]) then src else if !AlphaMx[1] then src else
AlphaMx[1] + tc * (src - AlphaMx[1]);
#def AlphaMx = ama;
def crosMxUp = (AlphaMx > AlphaTrend[AlphaTrendWidth]) and (AlphaMx[1] <= AlphaTrend[AlphaTrendWidth]);
def crosMxDn = (AlphaMx < AlphaTrend[AlphaTrendWidth]) and (AlphaMx[1] >= AlphaTrend[AlphaTrendWidth]);
def buySignalMx = crosMxUp and AlphaMx > AlphaMx[1];
def sellSignalMx = crosMxDn and AlphaMx < AlphaMx[1];
def K11 = barssince(buySignalMx);
def K22 = barssince(sellSignalMx);
def O11 = barssince(buySignalMx[1]);
def O22 = barssince(sellSignalMx[1]);
plot BuySigMx = if alMax and buySignalMx and O11 > K22 and K22 > 3 then AlphaTrend[AlphaTrendWidth] else na;
plot SellSigMx = if alMax and sellSignalMx and O22 > K11 and K11 > 3 then AlphaTrend[AlphaTrendWidth] else na;
BuySigMx.SetPaintingStrategy(PaintingStrategy.LINE_VS_SQUARES);
SellSigMx.SetPaintingStrategy(PaintingStrategy.LINE_VS_SQUARES);
BuySigMx.SetDefaultColor(Color.GREEN);
SellSigMx.SetDefaultColor(Color.RED);
AddChartBubble(showBubbles and BuySigMx, low, "Max", Color.GREEN, no);
AddChartBubble(showBubbles and SellSigMx, high, "Max", Color.RED, yes);
#-- plots
plot Alpha1 = if !last and AlphaTrend then AlphaTrend else na;
plot Alpha2 = if !last and AlphaTrend[AlphaTrendWidth] then AlphaTrend[AlphaTrendWidth] else na;
Alpha1.SetDefaultColor(GlobalColor("up"));
Alpha2.SetDefaultColor(GlobalColor("dn"));
AddCloud(Alpha1, Alpha2, GlobalColor("up"), GlobalColor("dn"));
##### END
CSS:
#https://www.tradingview.com/script/o50NYLAZ-AlphaTrend/
#// author © KivancOzbilgic
#// developer © KivancOzbilgic
# Converted and mod by Sam4Cok@Samer800 - 09/2022
#indicator('AlphaTrend', shorttitle='AT',
input showLebel = no; # Show Lebel?
input SignalType = {Non,Default AlphaTrend, AlphaMax, Both};
input MomType = {default MFI, RSI, FireFly};
input AlphaTrendWidth = 2;
input Multiplier = 1.1; # 'Multiplier'
input Period = 14; # 'Common Period'
input AlphaMaxLength =10; # 'AlphaMaxLength'
input src = close;
def na = Double.NaN;
AddLabel(showLebel, SignalType, Color.WHITE);
##### Script
script nz {
input data = 0;
input replacement = 0;
def ret_val = if IsNaN(data) then replacement else data;
plot return = ret_val;
}
#rescale(_src, _oldMin, _oldMax, _newMin, _newMax) =>
script rescale {
input _src = close;
input _oldMin = 0;
input _oldMax = 0;
input _newMin = 0;
input _newMax = 0;
def rescale = _newMin + (_newMax - _newMin) * (_src - _oldMin) / Max(_oldMax - _oldMin, 0.0000000010);
plot return = rescale;
}
# stoch(source, high, low, length) =>
script stoch {
input src = close;
input h = high;
input l = low;
input len = 0;
def stoch = 100 * (src - Lowest(l, len)) / (Highest(h, len) - Lowest(l, len));
plot return = stoch;
}
#averageS(bp, tr_, length) =>
script averageS {
input bp = close;
input tr_ = close;
input length = 0;
def average = Sum(bp, length) / Sum(tr_, length);
plot return = average;
}
#barssince(Condition) =>
script barssince {
input Condition = 0;
def barssince = if Condition then 1 else barssince[1] + 1;
plot return = barssince;
}
### Filters
def rsi1 = RSI(Price = src, Length = Period);
def mfi1 = MoneyFlowIndex(Length = Period);
### FireFly Oscillator
def v2 = (high + low + close * 2) / 4;
def v3 = ExpAverage(v2, Period);
def v4 = StDev(v2, Period);
def v5 = (v2 - v3) * 100 / If(v4 == 0, 1, v4);
def v6 = ExpAverage(v5, 3);
def v7 = v6;
def ww = (ExpAverage(v7, Period) + 100) / 2 - 4;
def FireFly = rescale(ww, -50, 150, 0, 100);
#######
def nATR = SimpleMovingAvg(TrueRange(high, close, low), Period);
def upT = low - nATR * Multiplier;
def downT = high + nATR * Multiplier;
#### AlphaTrend Calc
def AlphaTrend =
if (if MomType == MomType.RSI then rsi1 >= 50 else
if MomType == MomType.MFI then mfi1 >= 50 else
if MomType == MomType.FireFly then FireFly >= 50 else na) then
if upT < nz(AlphaTrend[1]) then nz(AlphaTrend[1]) else upT else
if downT > nz(AlphaTrend[1]) then nz(AlphaTrend[1]) else downT;
def color1 = if AlphaTrend > AlphaTrend[2] then 1 else
if AlphaTrend < AlphaTrend[2] then -1 else
if AlphaTrend[1] > AlphaTrend[3] then 1 else -1;
plot Alpha = AlphaTrend;
Alpha.SetDefaultColor(CreateColor(33, 150, 243));
plot Alpha2 = AlphaTrend[AlphaTrendWidth];
Alpha2.SetDefaultColor(CreateColor(156, 39, 176));
AddCloud(Alpha, Alpha2, CreateColor(33, 150, 243), CreateColor(136, 14, 79));
###########
def buySignalk = AlphaTrend crosses above AlphaTrend[AlphaTrendWidth];
def sellSignalk = AlphaTrend crosses below AlphaTrend[AlphaTrendWidth];
def K1 = barssince(buySignalk);
def K2 = barssince(sellSignalk);
def O1 = barssince(buySignalk[1]);
def O2 = barssince(sellSignalk[1]);
def BuySig = if (SignalType == SignalType.AlphaTrend or SignalType == SignalType.Both) and buySignalk and O1 > K2 then AlphaTrend[AlphaTrendWidth] * 0.9999 else na;
def SellSig = if (SignalType == SignalType.AlphaTrend or SignalType == SignalType.Both) and sellSignalk and O2 > K1 then AlphaTrend[AlphaTrendWidth] * 1.0001 else na;
AddChartBubble(BuySig, low - 7 * TickSize(), "Buy", CreateColor(33, 150, 243), no);
AddChartBubble(SellSig, high + 7 * TickSize(), "Sell", CreateColor(156, 39, 176), yes);
#### Alpha Max
#//----
def ama;
def hhama = highest(high, AlphaMaxLength);
def llama = lowest(low, AlphaMaxLength);
def hh = max(sign(hhama-hhama[1]),0);
def ll = max(sign(llama-llama[1])*-1,0);
def tc = power(SimpleMovingAvg(if hh or ll then 1 else 0,AlphaMaxLength),2);
ama = nz(ama[1]+tc*(src-ama[1]),src);
def AlphaMx = ama; #,"Plot",#ff1100,2)
def buySignalMx = ((AlphaMx crosses above AlphaTrend[AlphaTrendWidth]) and AlphaMx > AlphaMx[1]);
def sellSignalMx = ((AlphaMx crosses below AlphaTrend[AlphaTrendWidth]) and AlphaMx < AlphaMx[1]);
def BuySigMx = if (SignalType == SignalType.AlphaMax or SignalType == SignalType.Both) and buySignalMx and O1 > K2 then AlphaTrend[AlphaTrendWidth] * 0.9999 else na;
def SellSigMx = if (SignalType == SignalType.AlphaMax or SignalType == SignalType.Both) and sellSignalMx and O2 > K1 then AlphaTrend[AlphaTrendWidth] * 1.0001 else na;
AddChartBubble(BuySigMx, low - 7 * TickSize(), "Max", Color.GREEN, no);
AddChartBubble(SellSigMx, high + 7 * TickSize(), "Max", Color.RED, yes);
##### END
Last edited: