Alpha Trend

BULLFIZZ

Member
Platform
  1. Thinkorswim
@barbaros I came across this indicator called Alpha trend on trading view i was wondering if you could help us convert it to TOS. your effort will be greatly appreciated. Happy trading. 🤝🤝🤝


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 Trendin 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 resistancelevels 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 abutton 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.

CODE.👇👇👇
Code:
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// author © KivancOzbilgic
// developer © KivancOzbilgic
//@version=5
indicator('AlphaTrend', shorttitle='AT', overlay=true, format=format.price, precision=2, timeframe='')
coeff = input.float(1, 'Multiplier', step=0.1)
AP = input(14, 'Common Period')
ATR = ta.sma(ta.tr, AP)
src = input(close)
showsignalsk = input(title='Show Signals?', defval=true)
novolumedata = input(title='Change calculation (no volume data)?', defval=false)
upT = low - ATR * coeff
downT = high + ATR * coeff
AlphaTrend = 0.0
AlphaTrend := (novolumedata ? ta.rsi(src, 14) >= 50 : ta.mfi(hlc3, 14) >= 50) ? upT < nz(AlphaTrend[1]) ? nz(AlphaTrend[1]) : upT : downT > nz(AlphaTrend[1]) ? nz(AlphaTrend[1]) : downT

color1 = AlphaTrend > AlphaTrend[2] ? #00E60F : AlphaTrend < AlphaTrend[2] ? #80000B : AlphaTrend[1] > AlphaTrend[3] ? #00E60F : #80000B
k1 = plot(AlphaTrend, color=color.new(#0022FC, 0), linewidth=3)
k2 = plot(AlphaTrend[2], color=color.new(#FC0400, 0), linewidth=3)

fill(k1, k2, color=color1)


alertcondition(ta.cross(close, AlphaTrend), title='Cross Alert', message='Price - AlphaTrend Crossing!')
alertcondition(ta.crossover(low, AlphaTrend), title='CrossOver Alarm', message='BUY SIGNAL!')
alertcondition(ta.crossunder(high, AlphaTrend), title='CrossUnder Alarm', message='SELL SIGNAL!')

alertcondition(ta.cross(close[1], AlphaTrend[1]), title='Cross Alert After Bar Close', message='Price - AlphaTrend Crossing!')
alertcondition(ta.crossover(low[1], AlphaTrend[1]), title='CrossOver Alarm After Bar Close', message='BUY SIGNAL!')
alertcondition(ta.crossunder(high[1], AlphaTrend[1]), title='CrossUnder Alarm After Bar Close', message='SELL SIGNAL!')




buySignalk = ta.crossover(AlphaTrend, AlphaTrend[2])
sellSignalk = ta.crossunder(AlphaTrend, AlphaTrend[2])


K1 = ta.barssince(buySignalk)
K2 = ta.barssince(sellSignalk)
O1 = ta.barssince(buySignalk[1])
O2 = ta.barssince(sellSignalk[1])

plotshape(buySignalk and showsignalsk and O1 > K2 ? AlphaTrend[2] * 0.9999 : na, title='BUY', text='BUY', location=location.absolute, style=shape.labelup, size=size.tiny, color=color.new(#0022FC, 0), textcolor=color.new(color.white, 0))

plotshape(sellSignalk and showsignalsk and O2 > K1 ? AlphaTrend[2] * 1.0001 : na, title='SELL', text='SELL', location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.new(color.maroon, 0), textcolor=color.new(color.white, 0))
 

barbaros

Administrator
Staff member
First attempt. Some limitation in Thinkscript.

3nJ2tqi.png


Code:
# AlphaTrend
# Converted for https://b4indicators.com/threads/alpha-trend.174/
# v1.0 - barbaros

input coeff = 1.0;
input AP = 14;
input src = close;
input noVolumeData = no;

def _ATR = ATR(AP);
def upT = low - _ATR * coeff;
def downT = high + _ATR * coeff;

def AlphaTrend = if BarNumber() == 1 then 0
                 else if (if novolumedata then RSI(price=src, length=14) >= 50 else MoneyFlowIndex(length=14) >= 50) then
                    if upT < AlphaTrend[1] then AlphaTrend[1] else upT
                 else
                    if downT > AlphaTrend[1] then AlphaTrend[1] else downT;


plot k1 = AlphaTrend;
k1.SetDefaultColor(CreateColor(0, 34, 252));
k1.SetLineWeight(2);

plot k2 = AlphaTrend[2];
k2.SetDefaultColor(CreateColor(252, 4, 0));
k2.SetLineWeight(2);

# TODO: Cannot add dynamic color in Thinkscript
# if AlphaTrend > AlphaTrend[2] then CreateColor(0,230,15)
#        else if AlphaTrend < AlphaTrend[2] then CreateColor(128, 0, 11)
#        else if AlphaTrend[1] > AlphaTrend[3] then CreateColor(0, 230, 15)
#        else CreateColor(128, 0, 11)
AddCloud(k1, k2);

def direction = if BarNumber() == 1 then 0
                else if direction[1] != 1 and crosses(AlphaTrend, AlphaTrend[2], CrossingDirection.ABOVE) then 1
                else if direction[1] != -1 and crosses(AlphaTrend, AlphaTrend[2], CrossingDirection.BELOW) then -1
                else direction[1];

plot buySignal = direction == 1 and direction[1] != 1;
buySignal.SetDefaultColor(Color.GREEN);
buySignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);

plot sellSignal = direction == -1 and direction[1] != -1;
sellSignal.SetDefaultColor(Color.RED);
sellSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
 
Last edited:
Is anyone else having issues displaying this on a chart? My chart gets all scrunched up Is there a scaling issue or am I doing something wrong
 

barbaros

Administrator
Staff member
Here is a version with bar coloring option.

Code:
# AlphaTrend
# Converted for https://b4indicators.com/threads/alpha-trend.174/
# v1.0 - barbaros
# v1.1 - barbaros - added bar coloring

input coeff = 1.0;
input AP = 14;
input src = close;
input noVolumeData = no;
input paintBars = no;

def _ATR = ATR(AP);
def upT = low - _ATR * coeff;
def downT = high + _ATR * coeff;

def AlphaTrend = if BarNumber() == 1 then 0
                 else if (if novolumedata then RSI(price=src, length=14) >= 50 else MoneyFlowIndex(length=14) >= 50) then
                    if upT < AlphaTrend[1] then AlphaTrend[1] else upT
                 else
                    if downT > AlphaTrend[1] then AlphaTrend[1] else downT;


plot k1 = AlphaTrend;
k1.SetDefaultColor(CreateColor(0, 34, 252));
k1.SetLineWeight(2);

plot k2 = AlphaTrend[2];
k2.SetDefaultColor(CreateColor(252, 4, 0));
k2.SetLineWeight(2);

# TODO: Cannot add dynamic color in Thinkscript
# if AlphaTrend > AlphaTrend[2] then CreateColor(0,230,15)
#        else if AlphaTrend < AlphaTrend[2] then CreateColor(128, 0, 11)
#        else if AlphaTrend[1] > AlphaTrend[3] then CreateColor(0, 230, 15)
#        else CreateColor(128, 0, 11)
AddCloud(k1, k2);

def direction = if BarNumber() == 1 then 0
                else if direction[1] != 1 and crosses(AlphaTrend, AlphaTrend[2], CrossingDirection.ABOVE) then 1
                else if direction[1] != -1 and crosses(AlphaTrend, AlphaTrend[2], CrossingDirection.BELOW) then -1
                else direction[1];

plot buySignal = direction == 1 and direction[1] != 1;
buySignal.SetDefaultColor(Color.GREEN);
buySignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);

plot sellSignal = direction == -1 and direction[1] != -1;
sellSignal.SetDefaultColor(Color.RED);
sellSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);

AssignPriceColor(if paintBars and direction == 1 then Color.GREEN else if paintBars and direction == -1 then Color.RED else if paintBars then Color.GRAY else Color.CURRENT);
 
That looks great. I tried making a scan from the 2nd version with arrow signals scanning for the buy /sell arrow to be true but it just keeps timing out. Must need a custom scan written, not just the fill in the blanks version. Care to take a shot at it @barbaros ?
 
I tried smaller subset and no other conditions of any kind and still can't scan for buy/sell signals/arrows. Looks like it will prolly need a custom scan if you are up to it. If it is possible to code in close greater then 5 and average daily volume greater then 500K to this would be great. When I try to make it a condition seperate from the main indicator of the scan it usually makes it too complex to scan unless it can be hard code written into scan. Thanks in advance if you choose to tackle this one. I hope you take a look at the QQQ RelaxedTrader strategy you said you might tackle this weekend too.
 

barbaros

Administrator
Staff member
I tried smaller subset and no other conditions of any kind and still can't scan for buy/sell signals/arrows. Looks like it will prolly need a custom scan if you are up to it. If it is possible to code in close greater then 5 and average daily volume greater then 500K to this would be great. When I try to make it a condition seperate from the main indicator of the scan it usually makes it too complex to scan unless it can be hard code written into scan. Thanks in advance if you choose to tackle this one. I hope you take a look at the QQQ RelaxedTrader strategy you said you might tackle this weekend too.
We'll figure it out. :)
 
Top