#Hull_SuperTrend_Trading_System<break />
# assembled by Chewie 4/10/2022<break />
# many thanks to all the other noted contributors to this system.<break />
<break />
# SuperTrend Yahoo Finance Replica - Modified from Modius SuperTrend<break />
# Modified Modius ver. by RConner7<break />
# Modified by Barbaros to replicate look from TradingView version<break />
# Modified by Barbaros to add EMA cross for bubbles and alerts<break />
# Modified by Barbaros to update bar color painting<break />
# v3.3<break />
<break />
input Target_Bubbles = no;<break />
input Entry_SL_Bubbles = no;<break />
input Targetlines = yes;<break />
input Labels = yes;<break />
input alertON = yes;<break />
input Bands = yes;<break />
input EMA1 = 10;<break />
input EMA2 = 20;<break />
input AvgType = AverageType.HULL;<break />
input STAtrMult = 2.75;<break />
input nATR = 12;<break />
<break />
def ATR = ATR(length = nATR"]
) or (close[1] > UP_Band[1])) then UP_Band_Basic else UP_Band[1];
def LW_Band = if ((LW_Band_Basic > LW_Band[1]) or (close[1] < LW_Band[1])) then LW_Band_Basic else LW_Band[1];
def ST = if ((ST[1] == UP_Band[1]) and (close < UP_Band)) then UP_Band
else if ((ST[1] == UP_Band[1]) and (close > Up_Band)) then LW_Band
else if ((ST[1] == LW_Band[1]) and (close > LW_Band)) then LW_Band
else if ((ST[1] == LW_Band) and (close < LW_Band)) then UP_Band
else LW_Band;
def EMA1Val = MovAvgExponential(close, EMA1);
def EMA2Val = MovAvgExponential(close, EMA2);
def EMADirection = if EMA1Val > EMA2Val then 1 else if EMA1Val < EMA2Val then -1 else 0;
plot Long = if close > ST then ST else Double.NaN;
Long.AssignValueColor(Color.cyan);
Long.SetLineWeight(3);
plot Short = if close < ST then ST else Double.NaN;
Short.AssignValueColor(Color.magenta);
Short.SetLineWeight(3);
def LongTrigger = isNaN(Long[1]) and !isNaN(Long);
def ShortTrigger = isNaN(Short[1]) and !isNaN(Short);
plot LongDot = if LongTrigger then ST else Double.NaN;
LongDot.SetPaintingStrategy(PaintingStrategy.POINTS);
LongDot.AssignValueColor(Color.cyan);
LongDot.SetLineWeight(4);
plot ShortDot = if ShortTrigger then ST else Double.NaN;
ShortDot.SetPaintingStrategy(PaintingStrategy.POINTS);
ShortDot.AssignValueColor(Color.magenta);
ShortDot.SetLineWeight(4);
AddChartBubble(Entry_SL_Bubbles and LongTrigger, ST, "BUY", Color.GREEN, no);
AddChartBubble(Entry_SL_Bubbles and ShortTrigger, ST, "SELL", Color.RED, yes);
#Super Trend Labels
AddLabel(yes and labels and Long, "ST:LONG", color.CYAN);
AddLabel(yes and labels and Short, "ST:SHORT", color.magenta);
#
# Hull Moving Average Concavity and Turning Points
#
# Author: Seth Urion (Mahsume)
# Version: 2020-05-01 V4
#
# Now with support for ToS Mobile
#
declare upper;
input HMA_Length = 60;
input lookback = 3;
input arrows = no;
def price = HL2;
plot HMA = HullMovingAvg(price = price, length = HMA_Length);
def delta = HMA[1] - HMA[lookback + 1];
def delta_per_bar = delta / lookback;
def next_bar = HMA[1] + delta_per_bar;
def concavity = if HMA > next_bar then 1 else -1;
plot turning_point = if concavity[1] != concavity then HMA else double.nan;
HMA.AssignValueColor(color = if concavity[1] == -1 then
if HMA > HMA[1] then color.dark_orange else color.red else
if HMA < HMA[1] then color.dark_green else color.green);
HMA.SetLineWeight(3);
turning_point.SetLineWeight(2);
turning_point.SetPaintingStrategy(paintingStrategy = PaintingStrategy.POINTS);
turning_point.SetDefaultColor(color.white);
plot MA_Max = if HMA[-1] < HMA and HMA > HMA[1] then HMA else Double.NaN;
MA_Max.SetDefaultColor(Color.WHITE);
MA_Max.SetPaintingStrategy(PaintingStrategy.SQUARES);
MA_Max.SetLineWeight(5);
plot MA_Min = if HMA[-1] > HMA and HMA < HMA[1] then HMA else Double.Nan;
MA_Min.SetDefaultColor(Color.WHITE);
MA_Min.SetPaintingStrategy(PaintingStrategy.TRIANGLES);
MA_Min.SetLineWeight(5);
def BuySetup = HMA > HMA[1] and HMA[1] < HMA[2];
def SellSetup = HMA < HMA[1] and HMA[1] > HMA[2];
plot sell = if arrows and turning_point and concavity == -1 then high else double.nan;
sell.SetDefaultColor(Color.DARK_ORANGE);
sell.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
sell.SetLineWeight(3);
plot buy = if arrows and turning_point and concavity == 1 then low else double.nan;
buy.SetDefaultColor(Color.CYAN);
buy.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
buy.SetLineWeight(3);
def divergence = HMA - next_bar;
###################
#
# 2020-05-01
#
# MOBILE TOS SUPPORT
#
# Each color of the HMA needs to be a separate plot as ToS Mobile
# lacks the ability to assign colors the way ToS Desktop does.
# I recommend a plain colored HMA behind the line
# Set the line color of the HMA above to gray or some neutral
#
# CCD_D -> ConCave Down and Decreasing
# CCD_I -> ConCave Down and Increasing
# CCU_D -> ConCave Up and Decreasing
# CCU_I -> ConCave Up and Increasing
#
###################
plot CCD_D = if concavity == -1 and HMA < HMA[1] then HMA else double.nan;
CCD_D.SetDefaultColor(Color.RED);
CCD_D.SetLineWeight(1);
plot CCD_I = if concavity == -1 and HMA >= HMA[1] then HMA else double.nan;
CCD_I.SetDefaultColor(Color.DARK_ORANGE);
CCD_I.SetLineWeight(1);
plot CCU_D = if concavity == 1 and HMA <= HMA[1] then HMA else double.nan;
CCU_D.SetDefaultColor(COLOR.DARK_GREEN);
CCU_D.SetLineWeight(1);
plot CCU_I = if concavity == 1 and HMA > HMA[1] then HMA else double.nan;
CCU_I.SetDefaultColor(COLOR.GREEN);
CCU_I.SetLineWeight(1);
#Hull Label
AddLabel(yes and labels and CCD_D, "HULL:SELL", color.RED);
AddLabel(yes and labels and CCU_I, "HULL:BUY", color.green);
AddLabel(yes and labels and CCU_D, "HULL:WEAK SELL", color.dark_green);
AddLabel(yes and labels and CCD_I, "HULL:WEAK BUY", color.DARK_ORANGE);
#Target lines
# created by chewie
rec line = if IsNaN(MA_Min) then line[1] else MA_Min[0];
plot L_stoploss= if isnan(MA_Min) then line else double.nan;
L_stoploss.setpaintingStrategy(paintingStrategy.LINE);
L_stoploss.setlineWeight(3);
L_stoploss.setdefaultColor(color.dark_green);
L_stoploss.hideBubble();
def LSL =(if isNaN(L_stoploss[1]) then L_stoploss else Double.NaN);
addchartBubble(Entry_SL_Bubbles and L_stoploss, LSL,"L/SL",color.DARK_GREEN);
rec line2 = if IsNaN(MA_Max) then line2[1] else MA_Max[0];
plot S_stoploss = if IsNaN(MA_MAX) then line2 else double.nan;
S_stoploss.setpaintingStrategy(paintingStrategy.LINE);
S_stoploss.setlineWeight(3);
S_stoploss.setdefaultColor(color.dark_red);
S_stoploss.hideBubble();
def SSL =(if isNaN(S_stoploss[1]) then S_stoploss else Double.NaN);
addchartBubble(Entry_SL_Bubbles and S_stoploss, SSL,"S/SL",color.DARK_RED);
#Short Entry
rec line3 = if IsNaN(long) then line3[1] else long[0];
plot S_Entry = if IsNaN(long) then line3 else double.nan;
S_Entry.setpaintingStrategy(paintingStrategy.LINE);
S_Entry.setlineWeight(3);
S_Entry.setdefaultColor(color.red);
S_Entry.hideBubble();
def SE =(if isNaN(S_Entry[1]) then S_Entry else Double.NaN);
addchartBubble(Entry_SL_Bubbles and S_Entry, SE,"S/E",color.RED);
#Long Entry
rec line4 = if IsNaN(short) then line4[1] else short[0];
plot L_Entry = if IsNaN(short) then line4 else double.nan;
L_Entry.setpaintingStrategy(paintingStrategy.LINE);
L_Entry.setlineWeight(3);
L_Entry.setdefaultColor(color.green);
L_Entry.hideBubble();
def LE =(if isNaN(L_Entry[1]) then L_Entry else Double.NaN);
addchartBubble(Entry_SL_Bubbles and L_Entry, LE,"L/E",color.GREEN);
#HalfX Long
plot x1a_Long = if Targetlines then (L_Entry +(L_Entry - L_Stoploss)/2) else double.nan;
x1a_Long.setpaintingStrategy(paintingStrategy.dashes);
x1a_Long.setlineWeight(1);
x1a_Long.setdefaultColor(color.magenta);
x1a_Long.hideBubble();
#OneX Long
plot x1_Long = if Targetlines and x1a_Long > line4 then (L_Entry +(L_Entry - L_Stoploss)) else double.nan;
x1_Long.setpaintingStrategy(paintingStrategy.dashes);
x1_Long.setlineWeight(1);
x1_Long.setdefaultColor(color.yellow);
#TwoX Long
plot x2_Long = if Targetlines then x1_Long +(L_Entry - L_Stoploss) else double.nan;
x2_Long.setpaintingStrategy(paintingStrategy.line);
x2_Long.setlineWeight(2);
x2_Long.setdefaultColor(color.light_red);
def X2L =(if isNaN(x2_Long[1]) then x2_Long else Double.NaN);
addchartBubble(Target_Bubbles and x2_Long, x2L,"2xL",color.light_red);
#ThreeX Long
plot x3_Long = if Targetlines then x2_Long +(L_Entry - L_Stoploss) else double.nan;
x3_Long.setpaintingStrategy(paintingStrategy.dashes);
x3_Long.setlineWeight(1);
x3_Long.setdefaultColor(color.cyan);
def X3L =(if isNaN(x3_Long[1]) then x3_Long else Double.NaN);
addchartBubble(Target_Bubbles and x3_Long, x3L,"3xL",color.cyan);
#FourX Long
plot x4_Long = if Targetlines then x3_Long +(L_Entry - L_Stoploss) else double.nan;
x4_Long.setpaintingStrategy(paintingStrategy.dashes);
x4_Long.setlineWeight(1);
x4_Long.setdefaultColor(color.white);
def X4L =(if isNaN(x4_Long[1]) then x4_Long else Double.NaN);
addchartBubble(Target_Bubbles and x4_Long, x4L,"4xL",color.white);
#HalfX Short
plot x1a_Short = if Targetlines then (S_Entry -(S_Stoploss - S_Entry)/2) else double.nan;
x1a_Short.setpaintingStrategy(paintingStrategy.dashes);
x1a_Short.setlineWeight(1);
x1a_Short.setdefaultColor(color.magenta);
x1a_Short.hideBubble();
#OneX Short
plot x1_Short = if Targetlines and x1a_short < line3 then (S_Entry -(S_Stoploss - S_Entry)) else double.nan;
x1_Short.setpaintingStrategy(paintingStrategy.dashes);
x1_Short.setlineWeight(1);
x1_Short.setdefaultColor(color.yellow);
#TwoX Short
plot x2_Short = if Targetlines then x1_Short -(S_Stoploss - S_Entry) else double.nan;
x2_Short.setpaintingStrategy(paintingStrategy.line);
x2_Short.setlineWeight(2);
x2_Short.setdefaultColor(color.light_green);
def X2S =(if isNaN(x2_Short[1]) then x2_Short else Double.NaN);
addchartBubble(Target_Bubbles and x2_Short, x2S,"2xS",color.light_green);
#ThreeX Short
plot x3_Short = if Targetlines then x2_Short -(S_Stoploss - S_Entry) else double.nan;
x3_Short.setpaintingStrategy(paintingStrategy.dashes);
x3_Short.setlineWeight(1);
x3_Short.setdefaultColor(color.cyan);
def X3S =(if isNaN(x3_Short[1]) then x3_Short else Double.NaN);
addchartBubble(Target_Bubbles and x3_Short, x3S,"3xS",color.cyan);
#FourX Short
plot x4_Short = if Targetlines then x3_Short -(S_Stoploss - S_Entry) else double.nan;
x4_Short.setpaintingStrategy(paintingStrategy.dashes);
x4_Short.setlineWeight(1);
x4_Short.setdefaultColor(color.white);
def X4S =(if isNaN(x4_Short[1]) then x4_Short else Double.NaN);
addchartBubble(Target_Bubbles and x4_Short, x4S,"4xS",color.white);
#LinearRegCh100 RegressionDivergence - Trigger Lines - Trend Cross
# From Lizard Indicators Link:
https://www.lizardindicators.com/trigger-lines-cross-vs-thrust/
# Line #1 - Fast = LinReg (80)
# Line #2 - Slow = EXPEMA[LinReg (80)]
input LinRegLength = 80;
input EMAlength = 20;
input ColorOn = yes;
#Definitions
def price1 = close;
def displace = 0;
def LinReg = Inertia(price1[-displace], LinRegLength);
def EMA_LR = ExpAverage(LinReg[-displace], EMAlength);
def Body = (open + close)/2;
# Defining Long/Short Filters (these instructions determine entries / exits)
# Entry Requirements
def Long_Entry = close > LinReg and close > EMA_LR and body > LinReg and body > EMA_LR and close > high[1] and body > body[1];
# LinReg > LinReg[1] and
def Long_Stay_In = close > LinReg and close > EMA_LR;
def Long_Exit = (close < LinReg or close < EMA_LR) or Long_Stay_In == 0;
def Long_State = If Long_Entry then 1 else if Long_Exit then 0 else Long_State[1];
def Long1 = Long_State;
# Exit Requirements
def Short_Entry = close < LinReg and close < EMA_LR and body < LinReg and body < EMA_LR and close < low[1] and body < body[1];
# LinReg < LinReg[1] and
def Short_Stay_In = close < LinReg and close < EMA_LR;
def Short_Exit = (close > LinReg or close > EMA_LR) or Short_Stay_In == 0;
def Short_State = If Short_Entry then 1 else if Short_Exit then 0 else Short_State[1];
def Short1 = Short_State;
#Adding Linear Regression averages
plot LR = LinReg;
LR.SetDefaultColor(CreateColor(0, 130, 255));
plot EMA_LinReg = EMA_LR;
EMA_LinReg.SetDefaultColor(CreateColor(255, 215,0));
LR.setlineweight(1);
EMA_LinReg.setlineweight(2);
#DYNO Label
AddLabel(yes and labels and Short1, "DYNO:BEARISH", color.RED);
AddLabel(yes and labels and Long1, "DYNO:BULLISH", color.green);
AddLabel(yes and labels and Long1 == Short1, "DYNO:NEUTRAL", color.YELLOW);
#Regression Bands
input deviations = 1.618; #set your deviation units here.
input length = 500; #set your channel lookback period here.
def stdDeviation = StDevAll(price, length);
plot HighBand = if Bands then EMA_LinReg + deviations * stdDeviation else double.nan;
HighBand.SetDefaultColor(Color.red);
plot LowBand = if Bands then EMA_LinReg - deviations * stdDeviation else double.nan;
LowBand.SetDefaultColor(Color.green);
DefineGlobalColor("Bullish", Color.light_green);
DefineGlobalColor("Bearish", Color.light_RED);
#200 DAY MOVING AVERAGE
input lengthAvgEXP = 200;
plot AvgExp = ExpAverage(price[-displace], lengthAvgExp);
AvgExp.SetDefaultColor(Color.white);
AvgExp.setlineweight(2);
# Coloring Bars
AssignPriceColor(if ColorON and Long_State then Color.GREEN else if ColorON and Short_State then Color.RED else Color.Yellow);
DefineGlobalColor("Bullish", Color.dark_Green);
DefineGlobalColor("Bearish", Color.dark_Red);
AddCloud(EMA_LR, LinReg, GlobalColor("Bearish"), GlobalColor("Bullish"));
###################
#
# ALERTS
#
###################
Alert(alertON and LongTrigger, "Long Entry", Alert.BAR, Sound.Ding);
Alert(alertON and ShortTrigger, "Short Entry", Alert.BAR, Sound.Ding);
Alert(alertON and Buysetup, "HULL Buy", Alert.BAR, Sound.Bell);
Alert(alertON and Sellsetup, "HULL Sell", Alert.BAR, Sound.Bell);
Alert(alertON and high > highband, "Short Band", Alert.BAR, Sound.Ding);
Alert(alertON and low < lowband, "Long Band", Alert.BAR, Sound.Ding);