#// Indicator for TOS
#// © AlgoAlpha
#indicator(title="Swing Failure Zones and Signals [AlgoAlpha]"
# Converted by Sam4Cok@Samer800 - 07/2024
# Added FIBONACCI levels for Long and Short directions-Chewie 07/2024
input useChartTimeframe = {default "Yes", "No"};
input manualTimeframe = AggregationPeriod.FIFTEEN_MIN;
input displayOptions = {"Zones", "Avg Lines", default "Zones & Avg Lines", "Don't show"};
input FailureCandle = {default "High/Low", "Close"};
input LookbackPeriod = 14; #, "Range High/Low Lookback Period")
input addVolatilityToZone = no;
input labelson = yes;
input long = yes;
input short = no;
def na = Double.NaN;
def last = IsNaN(close);
#-- zones
def TopBtm;
def Avg;
switch (displayOptions) {
case "Zones" :
TopBtm = yes;
Avg = no;
case "Avg Lines" :
TopBtm = no;
Avg = yes;
case "Don't show" :
TopBtm = no;
Avg = no;
default :
TopBtm = yes;
Avg = yes;
}
#-- MTF
def c;
def h;
def l;
def h1;
def l1;
switch (useChartTimeframe) {
case "No" :
c = close(Period = manualTimeframe);
h = high(Period = manualTimeframe);
l = low(Period = manualTimeframe);
h1 = high(Period = manualTimeframe)[1];
l1 = low(Period = manualTimeframe)[1];
default :
c = close;
h = high;
l = low;
h1 = high[1];
l1 = low[1];
}
def srcHi;
def srcLo;
switch (FailureCandle) {
case "Close" :
srcHi = c;
srcLo = c;
default :
srcHi = h;
srcLo = l;
}
def volAdj = Min(ATR(Length = 30) * 0.3, c * (0.3 / 100));
def Zband = if addVolatilityToZone then volAdj[20] * 2 else 0;
#// Calculating
def hh = h == Highest(h, LookbackPeriod);
def ll = l == Lowest(l, LookbackPeriod);
def bull1 = l < l1 and c > h1 and ll;
def bear1 = h > h1 and c < l1 and hh;
def bull = if IsNaN(bull1) then 0 else bull1;
def bear = if IsNaN(bear1) then 0 else bear1;
def dir1 = if bull then 1 else if bear then 0 else dir1[1];
def dir = if IsNaN(dir1) then 0 else dir1;
def bearmit;
def bearTop;
def bearBtm;
def bearCnt;
if bear and (dir[1] or (!bearTop[1])) {
bearCnt = 0;
bearTop = h + Zband;
bearBtm = l1;
bearmit = 0;
} else if (srcHi > bearTop[1]) or bearCnt[1] >= 500 {
bearCnt = 0;
bearTop = 0;
bearBtm = 0;
bearmit = 1;
} else {
bearCnt = bearCnt[1] + 1;
bearTop = if !bearTop[1] then h else bearTop[1];
bearBtm = bearBtm[1];
bearmit = 0;
}
def bullmit;
def bullTop;
def bullBtm;
def bullCnt;
if bull and (!dir[1] or !bullBtm[1]) {
bullCnt = 0;
bullTop = h1;
bullBtm = l - Zband;
bullmit = 0;
} else if (srcLo < bullBtm[1]) or bullCnt[1] >= 500 {
bullCnt = 0;
bullTop = 0;
bullBtm = 0;
bullmit = 1;
} else {
bullCnt = bullCnt[1] + 1;
bullTop = bullTop[1];
bullBtm = bullBtm[1];
bullmit = 0;
}
# avg Lines
def bearLAvg = (bearTop + bearBtm) / 2;
def bullLAvg = (bullTop + bullBtm) / 2;
plot TopBear = if !last and TopBtm and bearTop then bearTop else na;
plot BtmBear = if !last and TopBtm and bearBtm then bearBtm else na;
plot avgBear = if !last and Avg and bearLAvg then bearLAvg else na;
plot TopBull = if !last and TopBtm and bullTop then bullTop else na;
plot BtmBull = if !last and TopBtm and bullBtm then bullBtm else na;
plot avgBull = if !last and Avg and bullLAvg then bullLAvg else na;
TopBear.SetDefaultColor(Color.DARK_RED);
BtmBear.SetDefaultColor(Color.DARK_RED);
TopBull.SetDefaultColor(Color.DARK_GREEN);
BtmBull.SetDefaultColor(Color.DARK_GREEN);
TopBear.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
BtmBear.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
TopBull.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
BtmBull.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
avgBear.SetPaintingStrategy(PaintingStrategy.DASHES);
avgBull.SetPaintingStrategy(PaintingStrategy.DASHES);
avgBear.SetDefaultColor(Color.DARK_RED);
avgBull.SetDefaultColor(Color.DARK_GREEN);
AddCloud(TopBear, BtmBear, Color.DARK_RED);
AddCloud(TopBull, BtmBull, Color.DARK_GREEN);
#AddChartBubble(bear and !bear[1], high, "R", Color.RED);
#AddChartBubble(bull and !bull[1], low, "S", Color.GREEN, no);
plot Support = if bear and !bear[1] then low else na;
plot Resistance = if bull and !bull[1] then high else na;
Resistance.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
Support.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
Support.SetDefaultColor(Color.RED);
Resistance.SetDefaultColor(Color.GREEN);
Support.SetLineWeight(4);
Resistance.SetLineWeight(4);
#-- Breakout
plot BreakUp = if bearmit and !bearmit[1] then low else na;
plot BreakDn = if bullmit and !bullmit[1] then high else na;
BreakUp.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
BreakDn.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
BreakUp.SetDefaultColor(Color.CYAN);
BreakDn.SetDefaultColor(Color.MAGENTA);
BreakUp.SetLineWeight(4);
BreakDn.SetLineWeight(4);
#-- END of ORIGINAL CODE
# LONG FIB Plots
plot F_100 = if long then btmbull else na;
F_100.SetDefaultColor(Color.DARK_RED);
F_100.SetLineWeight(2);
F_100.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
def F_0100 = (if IsNaN(F_100[1]) then F_100 else Double.NaN);
AddChartBubble(labelson and F_100, F_0100, "100%", Color.DARK_RED);
plot F_886 = if long then topbull else na;
F_886.SetDefaultColor(Color.violet);
F_886.SetLineWeight(1);
F_886.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
def F_0886 = (if IsNaN(F_886[1]) then F_886 else Double.NaN);
#addchartBubble(LabelsOn and F_786, F_0786,"78.6%",color.light_red);
plot F_0 = if long then (F_886 - F_100) * 8.88 + F_100 else na;
F_0.SetDefaultColor(Color.RED);
F_0.SetLineWeight(2);
F_0.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
def F_00 = (if IsNaN(F_0[1]) then F_0 else Double.NaN);
AddChartBubble(labelson and F_0, F_00, "0%: " + AsDollars(F_00), Color.RED);
plot F_124 = if long then (F_0 - F_100) / 1.17 + F_100 else na;
F_124.SetDefaultColor(Color.DARK_ORANGE);
F_124.SetLineWeight(1);
F_124.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
def F_0124 = (if IsNaN(F_124[1]) then F_124 else Double.NaN);
#addchartBubble(LabelsOn and F_124, F_0124,"12.4%",color.dark_orange);
plot F_236 = if long then (F_0 - F_100) / 1.3089 + F_100 else na;
F_236.SetDefaultColor(Color.PLUM);
F_236.SetLineWeight(1);
F_236.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
def F_0236 = (if IsNaN(F_236[1]) then F_236 else Double.NaN);
#addchartBubble(LabelsOn and F_236, F_0236,"23.6%",color.plum);
plot F_382 = if long then (F_0 - F_100) / 1.618 + F_100 else na;
F_382.SetDefaultColor(Color.CYAN);
F_382.SetLineWeight(1);
F_382.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
def F_0382 = (if IsNaN(F_382[1]) then F_382 else Double.NaN);
#addchartBubble(LabelsOn and F_382, F_0382,"38.2%",color.cyan);
plot F_50 = if long then (F_0 - F_100) / 2 + F_100 else na;
F_50.SetDefaultColor(Color.GREEN);
F_50.SetLineWeight(2);
F_50.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
def F_050 = (if IsNaN(F_50[1]) then F_50 else Double.NaN);
AddChartBubble(labelson and F_50, F_050, "50%", Color.GREEN);
plot F_618 = if long then (F_0 - F_100) / 2.618 + F_100 else na;
F_618.SetDefaultColor(Color.YELLOW);
F_618.SetLineWeight(2);
F_618.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
def F_0618 = (if IsNaN(F_618[1]) then F_618 else Double.NaN);
AddChartBubble(labelson and F_618, F_0618, "61.8%", Color.YELLOW);
plot F_786 = if long then(F_0 - F_100) / 4.675 + F_100 else na;
F_786.SetDefaultColor(Color.light_red);
F_786.SetLineWeight(1);
F_786.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
def F_0786 = (if IsNaN(F_886[1]) then F_886 else Double.NaN);
#addchartBubble(LabelsOn and F_886, F_0886,"88.6%",color.light_red);
plot FE_236 = if long then(F_0 - F_100) * 1.236 + F_100 else na;
FE_236.SetDefaultColor(Color.CYAN);
#FE_236.SetStyle(Curve.LONG_DASH);
FE_236.SetLineWeight(3);
FE_236.SetPaintingStrategy(PaintingStrategy.dashes);
def FE_0236 = (if IsNaN(FE_236[1]) then FE_236 else Double.NaN);
AddChartBubble(labelson and FE_236, FE_0236, "-23.6%", Color.CYAN);
plot FE_50 = if long then(F_0 - F_100) * 1.5 + F_100 else na;
FE_50.SetDefaultColor(Color.dark_green);
FE_50.SetLineWeight(3);
FE_50.SetPaintingStrategy(PaintingStrategy.dashes);
def FE_050 = (if IsNaN(FE_50[1]) then FE_50 else Double.NaN);
AddChartBubble(labelson and FE_50, FE_050, "-50%", Color.dark_green);
plot FR_236 = if long then(F_0 - F_100) * -0.236 + F_100 else na;
FR_236.SetDefaultColor(Color.CYAN);
FR_236.SetLineWeight(5);
FR_236.SetPaintingStrategy(PaintingStrategy.dashes);
def FR_0236 = (if IsNaN(FR_236[1]) then FR_236 else Double.NaN);
AddChartBubble(labelson and FR_236, FR_0236, "23.6%", Color.CYAN);
# SHORT FIB PLOTS
plot S_100 = if short then topbear else na;
S_100.SetDefaultColor(Color.dark_red);
S_100.SetLineWeight(1);
S_100.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
def S_0100 =(if isNaN(S_100[1]) then S_100 else Double.NaN);
addchartBubble(LabelsOn and S_100, S_0100,"100%",color.dark_red);
plot S_886 = if short then btmbear else na;
S_886.SetDefaultColor(Color.violet);
S_886.SetLineWeight(2);
S_886.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
def S_0886 =(if isNaN(S_886[1]) then S_886 else Double.NaN);
#addchartBubble(LabelsOn and S_886, S_0886,"88.6%",color.violet);
plot S_0 = if short then (S_100-S_886) * -8.88 + S_100 else na;
S_0.setdefaultcolor(color.red);
S_0.setlineweight(2);
s_0.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
def S_00 =(if isNaN(S_0[1]) then S_0 else Double.NaN);
addchartBubble(LabelsOn and S_0, S_00,"0%: " + asDollars(S_00),color.red);
plot S_124 = if short then (S_100-S_0)/-1.17 + S_100 else na;
S_124.setdefaultcolor(color.dark_orange);
S_124.setlineweight(1);
s_124.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
def S_0124 =(if isNaN(S_124[1]) then S_124 else Double.NaN);
#addchartBubble(LabelsOn and S_124, S_0124,"12.4%",color.dark_orange);
plot S_236 = if short then (S_100-S_0)/-1.3089+ S_100 else na;
S_236.setdefaultcolor(color.plum);
S_236.setlineweight(1);
S_236.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
def S_0236 = (if isNaN(S_236[1]) then S_236 else Double.NaN);
#addchartBubble(LabelsOn and S_236, S_0236,"23.6%",color.plum);
plot S_382 = if short then (S_100-S_0)/-1.618+ S_100 else na;
S_382.setdefaultcolor(color.cyan);
S_382.setlineweight(1);
S_382.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
def S_0382 =(if isNaN(S_382[1]) then S_382 else Double.NaN);
#addchartBubble(LabelsOn and S_382, S_0382,"38.2%",color.cyan);
plot S_50 = if short then (S_100-S_0)/-2 + S_100 else na;
S_50.setdefaultcolor(color.green);
S_50.setlineweight(2);
S_50.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
def S_050 =(if isNaN(S_50[1]) then S_50 else Double.NaN);
addchartBubble(LabelsOn and S_50, S_050,"50%",color.green);
plot S_618 = if short then (S_100-S_0)/-2.618+ S_100 else na;
S_618.setdefaultcolor(color.yellow);
S_618.setlineweight(2);
S_618.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
def S_0618 =(if isNaN(S_618[1]) then S_618 else Double.NaN);
addchartBubble(LabelsOn and S_618, S_0618,"61.8%",color.yellow);
plot S_786 = if short then (S_100-S_0)/-4.675+ S_100 else na;
S_786.setdefaultcolor(color.light_red);
S_786.setlineweight(1);
S_786.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
def S_0786 =(if isNaN(S_786[1]) then S_786 else Double.NaN);
#addchartBubble(LabelsOn and S_886, S_0786,"78.6%",color.light_red);
plot SE_236 = if short then (S_100-S_0)*-1.236 + S_100 else na;
SE_236.setdefaultcolor(color.cyan);
SE_236.setlineweight(3);
SE_236.SetPaintingStrategy(PaintingStrategy.dashes);
def SE_0236 =(if isNaN(SE_236[1]) then SE_236 else Double.NaN);
addchartBubble(LabelsOn and SE_236, SE_0236,"-23.6%",color.cyan);
plot SE_50 = if short then (S_100-S_0)*-1.5 + S_100 else na;
SE_50.setdefaultcolor(color.GREEN);
SE_50.setlineweight(3);
SE_50.SetPaintingStrategy(PaintingStrategy.dashes);
def SE_050 =(if isNaN(SE_50[1]) then SE_50 else Double.NaN);
addchartBubble(LabelsOn and SE_50, SE_050,"-50%",color.GREEN);
plot SR_236 = if short then (S_100-S_0)*0.236 + S_100 else na;
SR_236.setdefaultcolor(color.cyan);
SR_236.setlineweight(5);
SR_236.SetPaintingStrategy(PaintingStrategy.dashes);
def SR_0236 =(if isNaN(SR_236[1]) then SR_236 else Double.NaN);
addchartBubble(LabelsOn and SR_236, SR_0236,"23.6%",color.cyan);