#study("NSDT HAMA Candles", overlay=true)
#//The follow gradient code is taken from the Pinecoders Gradient Framework example.
#//https://www.tradingview.com/script/hqH4YIFa-Color-Gradient-Framework-PineCoders/
#https://www.tradingview.com/script/k7nrF2oI-NSDT-HAMA-Candles/
# included SSL Channel https://www.tradingview.com/script/6y9SkpnV-SSL-Channel/
# Youtube strategy https://www.youtube.com/watch?v=YJrELewfIrU
#Converted by Sam4Cok @ 07/2022
#Ver 1.1 by Sam4Cok @ 07/2022 -- Added Filters (ADX / TDFI / Range Indicator and options to Show/Hide indicators
#Ver 1.2 by Sam4COK @ 07/2022 -- Added Background Color, RSI Filter, signal enhancment.
# updated by Sam4COK@Samer800 - 12/2022 - Added method 2 for HA calc
#//INPUTS
input ColorBar = no;
input showArrow = yes;
input BackgroundColor = no;
### NSDT ###
input HamaCalcMethod = {Method1,Default Method2};
input TrendSource = close;
input HAMALength = 55;
input HAMAType = {default EMA, WMA, SMA, HMA};
input HAMABar = yes;
input HAMAMa = yes;
#//MA INFO
input OpenLength = 25; #"Length Open"
input OpenType = {default EMA, SMA, WMA}; #"Type Open"
input HighLength = 20; #"Length High"
input HighType = {default EMA, SMA, WMA}; #"Type High"
input LowLength = 20; #"Low"
input LowType = {default EMA, SMA, WMA}; #"Type Low"
input CloseLength = 20; #"Length Close"
input CloseType = {default EMA, SMA, WMA}; #"Close"
### SSL1Source ###
input ShowSSL = yes; #(true, "Highlight State ?")
input SSLWicks = no;#(false, "Take Wicks into Account ?")
input SSLType = {WMA, default SMA, EMA, HMA};
input SSL_MaLength = 55; #"MA "Channel ?
### RSI ###
input RSIFilter = no;
input RSILength = 30;
input RSIMovAvg = 200;
### TDFI ###
input TDFIFilter = no;
input TDFIPeriod = 11; # "Lookback"
input TDFIAbove = 0.03; # "Filter High"
input TDFIBelow = -0.03; # "Filter Low"
### ADX ###
input ADXFilter = no;
input ADXLength = 11;
input ADXLimit = 20;
### Range Indicator
input RangeIndicatorFilter = no;
input RILength = 11;
input RangeLimit = 20;
input RangeSmoothing = 5;
##################
def na = Double.NaN;
def Op = open; def Cl = close; def Hi = High; def Lo = low;
def method = if HamaCalcMethod==HamaCalcMethod.Method1 then 1 else
if HamaCalcMethod==HamaCalcMethod.Method2 then 0 else method[1];
########## Colors ########
DefineGlobalColor("Bull" , CreateColor(38, 166, 154)); # HAMA GREEN - Teal
DefineGlobalColor("Bear" , CreateColor(239, 83, 80)); # HAMA Red
DefineGlobalColor("WeakBull" , CreateColor(204, 204, 0)); # HAMA DARK Yellow
DefineGlobalColor("WeakBear" , CreateColor(255, 128, 0)); # HAMA Orange
DefineGlobalColor("Neutral" , CreateColor(255, 255, 0)); # HAMA Yellow
DefineGlobalColor("BullBar" , CreateColor(0, 255, 0)); # GREEN
DefineGlobalColor("WeakBullBar" , CreateColor(0, 128, 0)); # Dark GREEN
DefineGlobalColor("BearBar" , CreateColor(255, 0, 0)); # Red
DefineGlobalColor("WeakBearBar" , CreateColor(128, 0, 0)); # DARK RED
DefineGlobalColor("NeutralBar" , CreateColor(255, 255, 0)); # yellow
DefineGlobalColor("BGBull" , CreateColor (153, 255, 153));
DefineGlobalColor("BGBear" , CreateColor (255, 153, 153));
#////////////////////////////////////////////////////////////////////////////////
#mat(source, length, type) =>
script mat {
input source = close;
input length = 69;
input type = "EMA";
def mat;
mat =
if type == "SMA" then SimpleMovingAvg(source, length) else
if type == "EMA" then ExpAverage(source, length) else
if type == "WMA" then WMA(source, length) else
if type == "HMA" then WMA(2 * WMA(source, length / 2) - WMA(source, length), Round(Sqrt(length)))
else Double.NaN;
plot result = mat;
}
def ma = WMA(TrendSource, HAMALength);
#//GRADIENT AREA
def center = ExpAverage(ma, 3);
def xUp = ma crosses above center;
def xDn = ma crosses below center;
def chg = ma - ma[1];
def up = chg > 0;
def dn = chg < 0;
def srcBull = ma > center;
def srcBear = ma < center;
#### TREND####
def StrongUP = srcBull and up;
def WeakUp = srcBull and dn;
def StrongDN = srcBear and dn;
def WeakDN = srcBear and up;
def Neutral = xUp or xDn or !StrongUP and !StrongDN and !WeakUp and !WeakDN;
def SourceClose = (Op + Hi + Lo + Cl) / 4;
def LengthClose = CloseLength;
def SourceOpen = CompoundValue(1, (SourceOpen[1] + SourceClose[1]) / 2, SourceClose) ;
def LengthOpen = OpenLength;
def SourceHigh = Max(Max(Hi, SourceOpen), SourceClose);
def LengthHigh = HighLength;
def SourceLow = Min(Min(Lo, SourceOpen), SourceClose);
def LengthLow = LowLength;
def SrcOpen = (Op[1] + Cl[1]) / 2;
def SrcHigh = Max(Hi, Cl);
def SrcLow = Min(Lo, Cl);
def SrcClose = SourceClose;
#funcCalcMA1(type1, src1, len1) =>
script funcCalcMA1 {
input type1 = "EMA";
input src1 = close;
input len1 = 0;
def funcCalcMA1;
funcCalcMA1 = if type1 == "SMA" then SimpleMovingAvg(src1, len1) else
if type1 == "EMA" then ExpAverage(src1, len1) else WMA(src1, len1);
plot result = funcCalcMA1;
}
#funcCalcOpen(TypeOpen, SourceOpen, LengthOpen) =>
script funcCalcOpen {
input TypeOpen = "EMA";
input SourceOpen = close;
input LengthOpen = 0;
def funcCalcOpen;
funcCalcOpen = if TypeOpen == "SMA" then SimpleMovingAvg(SourceOpen, LengthOpen) else
if TypeOpen == "EMA" then ExpAverage(SourceOpen, LengthOpen) else WMA(SourceOpen, LengthOpen);
plot result = funcCalcOpen;
}
#funcCalcHigh(TypeHigh, SourceHigh, LengthHigh) =>
script funcCalcHigh {
input TypeHigh = "EMA";
input SourceHigh = close;
input LengthHigh = 0;
def funcCalcHigh;
funcCalcHigh = if TypeHigh == "SMA" then SimpleMovingAvg(SourceHigh, LengthHigh) else
if TypeHigh == "EMA" then ExpAverage(SourceHigh, LengthHigh) else WMA(SourceHigh, LengthHigh);
plot result = funcCalcHigh;
}
#funcCalcLow(TypeLow, SourceLow, LengthLow) =>
script funcCalcLow {
input TypeLow = "EMA";
input SourceLow = close;
input LengthLow = 0;
def funcCalcLow;
funcCalcLow = if TypeLow == "SMA" then SimpleMovingAvg(SourceLow, LengthLow) else
if TypeLow == "EMA" then ExpAverage(SourceLow, LengthLow) else WMA(SourceLow, LengthLow);
plot result = funcCalcLow;
}
#funcCalcClose(TypeClose, SourceClose, LengthClose) =>
script funcCalcClose {
input TypeClose = "EMA";
input SourceClose = close;
input LengthClose = 0;
def funcCalcClose;
funcCalcClose = if TypeClose == "SMA" then SimpleMovingAvg(SourceClose, LengthClose) else
if TypeClose == "EMA" then ExpAverage(SourceClose, LengthClose) else WMA(SourceClose, LengthClose);
plot result = funcCalcClose;
}
# Plot the new Chart
def CandleOpen1 = funcCalcOpen(OpenType, SourceOpen, LengthOpen);
def CandleHigh1 = funcCalcHigh(HighType, SourceHigh, LengthHigh);
def CandleLow1 = funcCalcLow(LowType, SourceLow, LengthLow);
def CandleClose1 = funcCalcClose(CloseType, SourceClose, LengthClose);
def CandleOpen2 = funcCalcOpen(OpenType, SrcOpen, LengthOpen);
def CandleHigh2 = funcCalcHigh(HighType, SrcHigh, LengthHigh);
def CandleLow2 = funcCalcLow(LowType, SrcLow, LengthLow);
def CandleClose2 = funcCalcClose(CloseType, SrcClose, LengthClose);
def MAValue = funcCalcMA1(HAMAType, TrendSource, HAMALength);
plot MALine = if HAMAMa then MAValue else na;
MALine.SetStyle(Curve.FIRM);
MALine.AssignValueColor( if StrongUP then GlobalColor("Bull") else
if WeakUp then GlobalColor("WeakBull") else
if StrongDN then GlobalColor("Bear") else
if WeakDN then GlobalColor("WeakBear") else GlobalColor("Neutral"));
def CandleOpen = if method then CandleOpen1 else CandleOpen2;
def CandleHigh = if method then CandleHigh1 else CandleHigh2;
def CandleLow = if method then CandleLow1 else CandleLow2;
def CandleClose = if method then CandleClose1 else CandleClose2;
# Plot STRONG UP
def UpO1;
def UpH1;
def UpL1;
def UpC1;
if StrongUP and HAMABar
then {
UpO1 = if CandleOpen < CandleClose then CandleClose else CandleOpen;
UpH1 = if CandleHigh > CandleClose then CandleClose else CandleHigh;
UpL1 = if CandleLow < CandleOpen then CandleOpen else CandleLow;
UpC1 = if CandleOpen < CandleClose then CandleOpen else CandleClose;
} else
{
UpO1 = na;
UpH1 = na;
UpL1 = na;
UpC1 = na;
}
# Plot WEAK UP
def UpO;
def UpH;
def UpL;
def UpC;
if WeakUp and HAMABar
then {
UpO = if CandleOpen < CandleClose then CandleClose else CandleOpen;
UpH = if CandleHigh > CandleClose then CandleClose else CandleHigh;
UpL = if CandleLow < CandleOpen then CandleOpen else CandleLow;
UpC = if CandleOpen < CandleClose then CandleOpen else CandleClose;
} else
{
UpO = na;
UpH = na;
UpL = na;
UpC = na;
}
# Plot WEAK DOWN
def DnO;
def DnH;
def DnL;
def DnC;
if WeakDN and HAMABar
then {
DnO = if CandleOpen < CandleClose then CandleClose else CandleOpen;
DnH = if CandleHigh > CandleOpen then CandleOpen else CandleHigh;
DnL = if CandleLow < CandleClose then CandleClose else CandleLow;
DnC = if CandleOpen < CandleClose then CandleOpen else CandleClose;
} else
{
DnO = na;
DnH = na;
DnL = na;
DnC = na;
}
# Plot STRONG DOWN
def DnO1;
def DnH1;
def DnL1;
def DnC1;
if StrongDN and HAMABar
then {
DnO1 = if CandleOpen < CandleClose then CandleClose else CandleOpen;
DnH1 = if CandleHigh > CandleOpen then CandleOpen else CandleHigh;
DnL1 = if CandleLow < CandleClose then CandleClose else CandleLow;
DnC1 = if CandleOpen < CandleClose then CandleOpen else CandleClose;
} else
{
DnO1 = na;
DnH1 = na;
DnL1 = na;
DnC1 = na;
}
# Plot Neutral
def NuO;
def NuH;
def NuL;
def NuC;
if Neutral and HAMABar
then {
NuO = if CandleOpen < CandleClose then CandleClose else CandleOpen;
NuH = if CandleHigh > CandleOpen then CandleOpen else CandleHigh;
NuL = if CandleLow < CandleClose then CandleClose else CandleLow;
NuC = if CandleOpen < CandleClose then CandleOpen else CandleClose;
} else
{
NuO = na;
NuH = na;
NuL = na;
NuC = na;
}
# Plot the new Chart
AddChart(high = UpH1, low = UpL1, open = UpO1, close = UpC1,
type = ChartType.CANDLE, growcolor = GlobalColor("Bull"));
AddChart(high = UpH , low = UpL , open = UpO, close = UpC,
type = ChartType.CANDLE, growcolor = GlobalColor("WeakBull"));
AddChart(high = DnH1, low = DnL1, open = DnO1, close = DnC1,
type = ChartType.CANDLE, growcolor = GlobalColor("Bear"));
AddChart(high = DnH , low = DnL , open = DnO, close = DnC,
type = ChartType.CANDLE, growcolor = GlobalColor("WeakBear"));
AddChart(high = NuH , low = NuL , open = NuO, close = NuC,
type = ChartType.CANDLE, growcolor = GlobalColor("Neutral"));
########
#indicator("SSL Channel", by MissTricky)
#https://www.tradingview.com/script/6y9SkpnV-SSL-Channel/
def ma1 = mat(Hi,SSL_MaLength, SSLType);
def ma2 = mat(Lo ,SSL_MaLength, SSLType);
def Hlv1 = if (if SSLWicks then Hi else Cl) > ma1 then 1 else
if (if SSLWicks then Lo else Cl) < ma2 then -1 else Hlv1[1];
def Hlv = Hlv1;
def sslUp = if Hlv < 0 then ma2 else ma1;
def sslDown = if Hlv < 0 then ma1 else ma2;
def SSLBull = Hlv1 > 0 and Hlv1[1] < 0;
def SSLBear = Hlv1 < 0 and Hlv1[1] > 0;
plot highLine = if ShowSSL then sslUp else na;
plot lowLine = if ShowSSL then sslDown else na;
highLine.AssignValueColor(if Hlv > 0 then Color.GREEN else color.RED);
lowLine.AssignValueColor( if Hlv > 0 then Color.GREEN else color.RED);
highLine.AssignValueColor(if Hlv > 0 then Color.GREEN else Color.RED);
lowLine.AssignValueColor( if Hlv > 0 then Color.GREEN else Color.RED);
AddCloud (highLine, lowLine, Color.DARK_GREEN, Color.DARK_RED, no);
##### RSI
def RSIup = WildersAverage(Max(Cl - Cl[1], 0), rsiLength);
def RSIdown = WildersAverage(-Min(Cl - Cl[1], 0), rsiLength);
def RSIvalue = if RSIdown == 0 then 100 else if RSIup == 0 then 0 else 100 - (100 / (1 + RSIup / RSIdown));
def RSIAvg = SimpleMovingAvg(RSIvalue, RSIMovAvg);
def RSI = if RSIvalue > RSIAvg then 1 else
if RSIvalue < RSIAvg then -1 else na;
##### TDFI
def mma = ExpAverage(Cl * 1000, TDFIPeriod);
def smma = ExpAverage(mma, TDFIPeriod);
def impetmma = mma - mma[1];
def impetsmma = smma - smma[1];
def divma = AbsValue(mma - smma);
def averimpet = (impetmma + impetsmma) / 2;
def tdf = Power(divma, 1) * Power(averimpet, 3);
def TDFIvalue = tdf / Highest(AbsValue(tdf), TDFIPeriod * 3);
def TDFI = if TDFIvalue > TDFIAbove then 1 else
if TDFIvalue < TDFIBelow then -1 else na;
###### ADX Filter
def ADXvalue = DMI(ADXLength, AverageType.WILDERS).ADX;
def ADX = if ADXvalue >= ADXlimit then 1 else na;
###### Range Indicator
def data = TrueRange(Hi, Cl, Lo) / if Cl > Cl[1] then (Cl - Cl[1]) else 1;
def hData = Highest(data, RILength);
def lData = Lowest(data, RILength);
def range = 100 * (data - lData) / if hData > lData then (hData - lData) else 1;
def RIvalue = ExpAverage(range, RangeSmoothing);
def RI = if RIvalue >= Rangelimit then 1 else na;
### Signal###
def UpOpen = if CandleOpen < CandleClose then CandleClose else CandleOpen;
def UpHigh = if CandleHigh > CandleClose then CandleClose else CandleHigh;
def UpLow = if CandleLow < CandleOpen then CandleOpen else CandleLow;
def UpClose= if CandleOpen < CandleClose then CandleOpen else CandleClose;
def DnOpen = if CandleOpen < CandleClose then CandleClose else CandleOpen;
def DnHigh = if CandleHigh > CandleOpen then CandleOpen else CandleHigh;
def DnLow = if CandleLow < CandleClose then CandleClose else CandleLow;
def DnClose= if CandleOpen < CandleClose then CandleOpen else CandleClose;
def HAMAHigh = Cl > CandleLow;
def HAMALow = Lo < DnLow;
def SigUp = if TDFIFilter then (SSLBull and Cl > MAValue and HAMAHigh and TDFI > 0)
else (SSLBull and Cl > MAValue and HAMAHigh);
def SigUp1 = if ADXFilter then SigUp and ADX else SigUp;
def SigUp2 = if RangeIndicatorFilter then SigUp1 and RI else SigUp1;
def SignalUp = if RSIFilter then SigUp2 and RSI > 0 else SigUp2;
def SigDN = if TDFIFilter then (SSLBear and Cl < MAValue and HAMALow and TDFI < 0)
else (SSLBear and Cl < MAValue and HAMALow);
def SigDn1 = if ADXFilter then SigDn and ADX else SigDn;
def SigDn2 = if RangeIndicatorFilter then SigDn1 and RI else SigDn1;
def SignalDn = if RSIFilter then SigDn2 and RSI < 0 else SigDn2;
plot Arrow_Up = if showArrow and SignalUp then Lo else na;
plot Arrow_Dn = if showArrow and SignalDn then Hi else na;
Arrow_Up.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
Arrow_Up.SetDefaultColor(Color.WHITE);
Arrow_Up.SetLineWeight(3);
Arrow_Dn.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
Arrow_Dn.SetDefaultColor(Color.YELLOW);
Arrow_Dn.SetLineWeight(3);
##### Background & Price Color
AssignPriceColor(if ColorBar then
if Neutral then GlobalColor("NeutralBar") else
if StrongUP and Cl < UpHigh then GlobalColor("WeakBullBar") else
if StrongUP and Cl > UpHigh then GlobalColor("BullBar") else
if StrongDN and Cl < DnLow then GlobalColor("BearBar") else
if StrongDN and Cl > DnLow then GlobalColor("WeakBearBar") else
Color.DARK_ORANGE else Color.CURRENT);
AssignBackgroundColor(if BackgroundColor then
if StrongUP and Cl > UpHigh then GlobalColor("BGBull") else
if StrongDN and Cl < DnLow then GlobalColor("BGBear") else
Color.CURRENT else Color.CURRENT);
### ENd ####