#From the book New Trading Dimensions by Bill Willams, PhD
#Coded by Henry Z Kaczmarczyk 01/2021
#Added Multiple Time Frame
# TEA 2.0 - TOS version - 2015-06-04
# From Andrew Falde's work - LessThanRandom.com
# Converted from TradeStation to TOS by John Chernicky
#Hint: TEA 2.0 by Andrew Falde at LessThanRandom.com
#Trend Advisor Diamond Phase by Chuck Dukas , Coded by Henry Z Kaczmarczyk
#Trend Advisor Line 1 is current Time Frame. Line 2 is next higher Time Frame. Line 3 is second higher time frame.
#Trend Advisor is shown as Squares Compares Close to the 50 & 200 SMA
#Recovery Phase is Light Green
#Warning Phase is Orange
#Accumulation Phase is Green
#Dispersition Phase is Red
#Bullish Phase is Dark_Green
#Bearish Phase is Dark_Red
#TEA v 2.0 are shown as Points on lines 0,5 TEAB First Higher Time frame Lines 6, TEAC second higher Time frame Lines 7
#Consolidation is shown as Blue Points on the zero line
# Trend Caclulation
# 1 - Accumulation color Dark_Red
# 2 - Mark Up Color Green
# 3 - Distribution Color Dark_Green
# 4 - Mark Down Color Red
# 0 - No Trend
# Bill Williams Zone Bars are shown as Triangles in 3 Different Time Frames
#Green Triangles show when the Awesome Oscillator & AccelerationBands Osc are Bullish
#Red Triangles show when the Awesome Oscillator & AccelerationBands Osc are Bearish
#Blue Triangles show when the Awesome Oscillator & AccelerationBands Osc are Neutral
#Line 9 is then current Time Frame, Line 10 is then next higher time frame, Line 11 is the second higher Time Frame
#SquatBars are shown as Squares on Lines 13,14,15. Line 10 is the currentRatio Time Frame, Line 13 is the next Higher Time Frame, Line 14 is the secondsFromTime Higher Time Frame.
#GreenBars are green and are Bullish
#Squat Bars are Blue and show trend reversal at the end of Trends
#Fade Bars are Red and are Bearish
#FakeBars are Orange
#APC = 1 thru 3 will paint bars for Trend Advisor
#APC = 5 thru 7 will paint bars for TEA v2.0
#APC = 9 thru 11 will paint bars for Zone Bars
#APC = 13 thru 15 will Paint Bars for Squat Bars
Declare Lower;
DefineGlobalColor("GreenZone",Color.Green);
DefineGlobalColor("RedZone",Color.Red);
DefineGlobalColor("Neutral",Color.Blue);
DefineGlobalColor("Greenbar", Color.Green);
DefineGlobalColor("Squatbar", Color.Blue);
DefineGlobalColor("Fadebar", Color.Red);
DefineGlobalColor("Fakebar", Color.Orange);
DefineGlobalColor("Zero", Color.Gray);
input agg1 = AggregationPeriod.FIVE_MIN;
input agg2 = AggregationPeriod.THIRTY_MIN;
input agg3 = AggregationPeriod.HOUR;
Input apc = 5;
Input DotSize = 3;
Input Length1 = 50;
Input Length2 = 200;
input Period = 21; # Bars for Price Analysis TEA
Def Price1 = HL2(Period = agg1);
Def Price2 = HL2(Period = agg2);
Def Price3 = HL2(Period = agg3);
Def Vol1 = Volume(Period = agg1);
Def Vol2 = Volume(Period = agg2);
Def Vol3 = Volume(Period = agg3);
Def H1 = High(Period = agg1);
Def H2 = High(Period = agg2);
Def H3 = High(Period = agg3);
Def L1 = Low(Period = agg1);
Def L2 = Low(Period = agg2);
Def L3 = Low(Period = agg3);
Def C1 = Close(Period = agg1);
Def C2 = Close(Period = agg2);
Def C3 = Close(Period = agg3);
Def ML1=Average(C1,Length1);
Def ML2=Average(C1,Length2);
Def BML1=Average(C2,Length1);
Def BML2=Average(C2,Length2);
Def CML1=Average(C3,Length1);
Def CML2=Average(C3,Length2);
Plot RecP = If (ML1 < ML2 AND C1 < ML2 AND C1 > ML1) then 1 else Double.Nan;
Plot WarnP = If(ML1 > ML2 AND C1 > ML2 AND C1 < ML1) then 1 Else Double.Nan;
Plot AcumP = If(ML1 < ML2 AND C1 > ML2 AND C1 > ML1) Then 1 Else Double.Nan;
Plot DispP = If(ML1 > ML2 AND C1 < ML2 AND C1 < ML1) then 1 else Double.Nan;
Plot BullP = If(ML1 > ML2 AND C1 > ML2 AND C1 > ML1) then 1 else Double.Nan;
Plot BearP = If(ML1 < ML2 AND C1 < ML2 AND C1 < ML1) then 1 else Double.Nan;
RecP.SetPaintingStrategy(PaintingStrategy.Squares);
RecP.SetDefaultColor(Color.Light_Green);
RecP.SetLineWeight(DotSize);
WarnP.SetPaintingStrategy(PaintingStrategy.Squares);
WarnP.SetDefaultColor(Color.Orange);
WarnP.SetLineWeight(DotSize);
AcumP.SetPaintingStrategy(PaintingStrategy.Squares);
AcumP.SetDefaultColor(Color. Green);
AcumP.SetLineWeight(DotSize);
DispP.SetPaintingStrategy(PaintingStrategy.Squares);
DispP.SetDefaultColor(Color.Red);
DispP.SetLineWeight(DotSize);
BullP.SetPaintingStrategy(PaintingStrategy.Squares);
BullP.SetDefaultColor(Color.Dark_Green);
BullP.SetLineWeight(DotSize);
BearP.SetPaintingStrategy(PaintingStrategy.Squares);
BearP.SetDefaultColor(Color.Dark_Red);
BearP.SetLineWeight(DotSize);
Plot RecPB = If (BML1 < BML2 AND C2 < BML2 AND C2 > BML1) then 2 else Double.Nan;
Plot WarnPB = If(BML1 > BML2 AND C2 > BML2 AND C2 < BML1) then 2 Else Double.Nan;
Plot AcumPB = If(BML1 < BML2 AND C2 > BML2 AND C2 > BML1) Then 2 Else Double.Nan;
Plot DispPB = If(BML1 > BML2 AND C2 < BML2 AND C2 < BML1) then 2 else Double.Nan;
Plot BullPB = If(BML1 > BML2 AND C2 > BML2 AND C2 > BML1) then 2 else Double.Nan;
Plot BearPB = If(BML1 < BML2 AND C2 < BML2 AND C2 < BML1) then 2 else Double.Nan;
RecPB.SetPaintingStrategy(PaintingStrategy.Squares);
RecPB.SetDefaultColor(Color.Light_Green);
RecPB.SetLineWeight(DotSize);
WarnPB.SetPaintingStrategy(PaintingStrategy.Squares);
WarnPB.SetDefaultColor(Color.Orange);
WarnPB.SetLineWeight(DotSize);
AcumPB.SetPaintingStrategy(PaintingStrategy.Squares);
AcumPB.SetDefaultColor(Color. Green);
AcumPB.SetLineWeight(DotSize);
DispPB.SetPaintingStrategy(PaintingStrategy.Squares);
DispPB.SetDefaultColor(Color.Red);
DispPB.SetLineWeight(DotSize);
BullPB.SetPaintingStrategy(PaintingStrategy.Squares);
BullPB.SetDefaultColor(Color.Dark_Green);
BullPB.SetLineWeight(DotSize);
BearPB.SetPaintingStrategy(PaintingStrategy.Squares);
BearPB.SetDefaultColor(Color.Dark_Red);
BearPb.SetLineWeight(DotSize);
Plot RecPC = If (CML1 < CML2 AND C3 < CML2 AND C3 > CML1) then 3 else Double.Nan;
Plot WarnPC = If(CML1 > CML2 AND C3 > BML2 AND C3 < CML1) then 3 Else Double.Nan;
Plot AcumPC = If(CML1 < CML2 AND C3 > BML2 AND C3 > CML1) Then 3 Else Double.Nan;
Plot DispPC = If(CML1 > CML2 AND C3 < BML2 AND C3 < CML1) then 3 else Double.Nan;
Plot BullPC = If(CML1 > CML2 AND C3 > BML2 AND C3 > CML1) then 3 else Double.Nan;
Plot BearPC = If(CML1 < CML2 AND C3 < BML2 AND C3 < CML1) then 3 else Double.Nan;
RecPC.SetPaintingStrategy(PaintingStrategy.Squares);
RecPC.SetDefaultColor(Color.Light_Green);
RecPC.SetLineWeight(DotSize);
WarnPC.SetPaintingStrategy(PaintingStrategy.Squares);
WarnPC.SetDefaultColor(Color.Orange);
WarnPC.SetLineWeight(DotSize);
AcumPC.SetPaintingStrategy(PaintingStrategy.Squares);
AcumPC.SetDefaultColor(Color. Green);
AcumPc.SetLineWeight(DotSize);
DispPC.SetPaintingStrategy(PaintingStrategy.Squares);
DispPC.SetDefaultColor(Color.Red);
DispPC.SetLineWeight(DotSize);
BullPC.SetPaintingStrategy(PaintingStrategy.Squares);
BullPC.SetDefaultColor(Color.Dark_Green);
BullPC.SetLineWeight(DotSize);
BearPC.SetPaintingStrategy(PaintingStrategy.Squares);
BearPC.SetDefaultColor(Color.Dark_Red);
BearPC.SetLineWeight(DotSize);
Plot Zeroline=0;
ZeroLine.SetDefaultColor(GetColor(7));
# Expansion Calculation
def ATR = reference ATR(Period, averageType = AverageType.SIMPLE);
def StdDev = StDev(data = Close, length = Period);
def squeeze = (ATR * .75) - StdDev;
def Expansion = If (squeeze < 0, 1, 0);
# Linear Regression
def lowest = Lowest(L1[1], Period);
def lowestB = Lowest(L2[1], Period);
def lowestC = Lowest(L3[1], Period);
def highest = Highest(H1[1], Period);
def highestB = Highest(H2[1], Period);
def highestC = Highest(H3[1], Period);
def EMA = ExpAverage(C1, Period);
def EMAB = ExpAverage(C2, Period);
def EMAC = ExpAverage(C3, Period);
def Resolve = C1 - ((highest + lowest) / 2 + EMA) / 2;
def ResolveB = C2 - ((highestB + lowestB) / 2 + EMAB) / 2;
def ResolveC = C3 - ((highestC + lowestC) / 2 + EMAC) / 2;
def Trendline = LinearRegCurve(0, Period, Resolve);
def TrendlineB = LinearRegCurve(0, Period, ResolveB);
def TrendlineC = LinearRegCurve(0, Period, ResolveC);
# Trend Caclulation
# 1 - Accumulation
# 2 - Mark Up
# 3 - Distribution
# 4 - Mark Down
# 0 - No Trend
def Trend = if Trendline <= 0 and Trendline > Trendline[1] then 1 else (
if Trendline > 0 and Trendline > Trendline[1] then 2 else (
if Trendline > 0 and Trendline < Trendline[1] then 3 else (
if Trendline <= 0 and Trendline < Trendline[1] then 4 else (
Double.NaN))));
def TrendB = if TrendlineB <= 0 and TrendlineB > TrendlineB[1] then 1 else (
if TrendlineB > 0 and TrendlineB > TrendlineB[1] then 2 else (
if TrendlineB > 0 and TrendlineB < TrendlineB[1] then 3 else (
if TrendlineB <= 0 and TrendlineB < TrendlineB[1] then 4 else (
Double.NaN))));
def TrendC = if TrendlineC <= 0 and TrendlineC > TrendlineC[1] then 1 else (
if TrendlineC > 0 and TrendlineC > TrendlineC[1] then 2 else (
if TrendlineC > 0 and TrendlineC < TrendlineC[1] then 3 else (
if TrendlineC <= 0 and TrendlineC < TrendlineC[1] then 4 else (
Double.NaN))));
plot TEA =
(if Trend == 1 then 5
else (if Trend == 2 then 5
else (if Trend == 3 then 5
else (if Trend == 4 then 5 else Double.NaN
))));
plot TEAB =
(if TrendB == 1 then 6
else (if TrendB == 2 then 6
else (if TrendB == 3 then 6
else (if TrendB == 4 then 6 else Double.NaN
))));
plot TEAC =
(if TrendC == 1 then 7
else (if TrendC == 2 then 7
else (if TrendC == 3 then 7
else (if TrendC == 4 then 7 else Double.NaN
))));
plot Consolidated = (if Expansion == 0 then 0 else Double.NaN);
TEA.AssignValueColor
(if Trend == 1 then Color.DARK_RED
else (if Trend == 2 then Color.GREEN
else (if Trend == 3 then Color.DARK_GREEN
else (if Trend == 4 then Color.RED else Color.DARK_GRAY
))));
TEA.SetPaintingStrategy(PaintingStrategy.POINTS);
TEA.SetLineWeight(4);
#TEA.HideTitle();
TEA.HideBubble();
TEAB.AssignValueColor
(if TrendB == 1 then Color.DARK_RED
else (if TrendB == 2 then Color.GREEN
else (if TrendB == 3 then Color.DARK_GREEN
else (if TrendB == 4 then Color.RED else Color.DARK_GRAY
))));
TEAB.SetPaintingStrategy(PaintingStrategy.POINTS);
TEAB.SetLineWeight(4);
#TEAB.HideTitle();
TEAB.HideBubble();
TEAC.AssignValueColor
(if TrendC == 1 then Color.DARK_RED
else (if TrendC == 2 then Color.GREEN
else (if TrendC == 3 then Color.DARK_GREEN
else (if TrendC == 4 then Color.RED else Color.DARK_GRAY
))));
TEAC.SetPaintingStrategy(PaintingStrategy.POINTS);
TEAC.SetLineWeight(4);
#TEAC.HideTitle();
TEAC.HideBubble();
Consolidated.AssignValueColor(Color.BLUE);
Consolidated.SetPaintingStrategy(PaintingStrategy.POINTS);
Consolidated.SetLineWeight(4);
#Consolidated.HideTitle();
Consolidated.HideBubble();
#AddLabel(yes, "LessThanRandom.com", Color.GRAY);
def AO1 = Average(Price1,5) - Average(Price1,34);
def AO2 = Average(Price2,5) - Average(Price2,34);
def AO3 = Average(Price3,5) - Average(Price3,34);
def AC1 = AO1 - Average(AO1,5);
def AC2 = AO2 - Average(AO2,5);
def AC3 = AO3 - Average(AO3,5);
def GZone1 = if AO1 > AO1[1] and AC1 > AC1[1] then 1 else 0;
def RZone1 = if AO1 < AO1[1] and AC1 < AC1[1] then 1 else 0;
def GZone2 = if AO2 > AO2[1] and AC2 > AC2[1] then 1 else 0;
def RZone2 = if AO2 < AO2[1] and AC2 < AC2[1] then 1 else 0;
def GZone3 = if AO3 > AO3[1] and AC3 > AC3[1] then 1 else 0;
def RZone3 = if AO3 < AO3[1] and AC3 < AC3[1] then 1 else 0;
def mfiA = if Vol1 > 0 then (H1 – L1) / Vol1 * 100 else 0;
def mfiB = if Vol2 > 0 then (H2 – L2) / Vol2 * 100 else 0;
def mfiC = if Vol3 > 0 then (H3 – L3) / Vol3 * 100 else 0;
def Con1A = if Vol1[1] > Vol1[2] then 1 else 0;
def Con2A = if mfiA[1] > mfiA[2] then 1 else 0;
def Con1B = if Vol2[1] > Vol2[2] then 1 else 0;
def Con2B = if mfiB[1] > mfiB[2] then 1 else 0;
def Con1C = if Vol3[1] > Vol3[2] then 1 else 0;
def Con2C = if mfiC[1] > mfiC[2] then 1 else 0;
def BW1a= if Con1A == 1 and Con2A == 1 then 1 else 0;
def BW2a= if Con1A == 1 and Con2A == 0 then 2 else 0;
def BW3a= if Con1A == 0 and Con2A == 0 then 3 else 0;
def BW4a= if Con1A == 0 and Con2A == 1 then 4 else 0;
def SBa = (BW1a + BW2a + BW3a + BW4a);
def BW1b= if Con1B == 1 and Con2B == 1 then 1 else 0;
def BW2b= if Con1B == 1 and Con2B == 0 then 2 else 0;
def BW3b= if Con1B == 0 and Con2B == 0 then 3 else 0;
def BW4b= if Con1B == 0 and Con2B == 1 then 4 else 0;
def SBb = (BW1b + BW2b + BW3b + BW4b);
def BW1c= if Con1C == 1 and Con2C == 1 then 1 else 0;
def BW2c= if Con1C == 1 and Con2C == 0 then 2 else 0;
def BW3c= if Con1C == 0 and Con2C == 0 then 3 else 0;
def BW4c= if Con1C == 0 and Con2C == 1 then 4 else 0;
def SBc = (BW1c + BW2c + BW3c + BW4c);
Plot Dot9 = 9;
Dot9.SetpaintingStrategy(PaintingStrategy.Triangles);
Dot9.SetLineWeight(DotSize);
Dot9.AssignValueColor( if GZone1 == 1 then GlobalColor("GreenZone") else if RZone1 == 1 then GlobalColor("RedZone") else GlobalColor("Neutral"));
Plot Dot10 = 10;
Dot10.SetpaintingStrategy(PaintingStrategy.Triangles);
Dot10.SetLineWeight(DotSize);
Dot10.AssignValueColor( if GZone2 == 1 then GlobalColor("GreenZone") else if RZone2 == 1 then GlobalColor("RedZone") else GlobalColor("Neutral"));
Plot Dot11 = 11;
Dot11.SetpaintingStrategy(PaintingStrategy.Triangles);
Dot11.SetLineWeight(DotSize);
Dot11.AssignValueColor( if GZone3 == 1 then GlobalColor("GreenZone") else if RZone3 == 1 then GlobalColor("RedZone") else GlobalColor("Neutral"));
Plot Dot13 =13;
Dot13.SetpaintingStrategy(PaintingStrategy.Squares);
Dot13.SetLineWeight(3);
Dot13.assignValueColor(if SBa == 1 then GlobalColor("Greenbar") else if SBa == 2 then GlobalColor("Squatbar") else if SBa == 3 then GlobalColor("Fadebar") else if SBa == 4 then GlobalColor("Fakebar") else GlobalColor("Zero"));
Plot Dot14 =14;
Dot14.SetpaintingStrategy(PaintingStrategy.Squares);
Dot14.SetLineWeight(4);
Dot14.assignValueColor(if SBb == 1 then GlobalColor("Greenbar") else if SBb == 2 then GlobalColor("Squatbar") else if SBb == 3 then GlobalColor("Fadebar") else if SBb == 4 then GlobalColor("Fakebar") else GlobalColor("Zero"));
Plot Dot15 =15;
Dot15.SetpaintingStrategy(PaintingStrategy.Squares);
Dot15.SetLineWeight(5);
Dot15.assignValueColor(if SBc == 1 then GlobalColor("Greenbar") else if SBc == 2 then GlobalColor("Squatbar") else if SBc == 3 then GlobalColor("Fadebar") else if SBc == 4 then GlobalColor("Fakebar") else GlobalColor("Zero"));
AssignPriceColor( if APC == 1 and RecP == 1 then Color.Light_Green else if APC == 1 and WarnP == 1 then Color.Orange else if APC == 1 and AcumP == 1 then Color.Green else if APC == 1 and DispP == 1 then Color.Red else if APC ==1 and BullP ==1 then Color.Dark_Green else if apc == 1 and BearP == 1 then Color.DarK_Red Else
if APC == 2 and RecP == 1 then Color.Light_Green else if APC == 2 and WarnP == 2 then Color.Orange else if APC == 2 and AcumP == 2 then Color.Green else if APC == 2 and DispP == 2 then Color.Red else if APC ==2 and BullP ==2 then Color.Dark_Green else if apc == 2 and BearP == 2 then Color.DarK_Red Else if APC == 3 and RecP == 3 then Color.Light_Green else if APC == 3 and WarnP == 3 then Color.Orange else if APC == 3 and AcumP == 3 then Color.Green else if APC == 3 and DispP == 3 then Color.Red else if APC ==3 and BullP ==3 then Color.Dark_Green else if apc == 3 and BearP == 3 then Color.DarK_Red Else if APC ==5 and Trend == 1 then Color.Dark_Red else if APC == 5 and Trend == 2 then color.Green else if APC==5 and Trend == 3 then Color.Dark_Green else if APC ==5 and Trend ==4 then Color.Red else if APC ==6 and TrendB == 1 then Color.Dark_Red else if APC == 6 and TrendB == 2 then color.Green else if APC==6 and TrendB == 3 then Color.Dark_Green else if APC ==6 and TrendB ==4 then Color.Red else if APC ==7 and TrendC == 1 then Color.Dark_Red else if APC == 7 and TrendC == 2 then color.Green else if APC==7 and TrendC == 3 then Color.Dark_Green else if APC ==7 and TrendC ==4 then Color.Red else if apc == 9 and GZone1 == 1 then GlobalColor("GreenZone") else if apc == 9 and RZone1 == 1 then GlobalColor("RedZone") else if apc ==9 and GZone1 == 0 and RZone1 == 0 then GlobalColor("Neutral") else if apc == 10 and GZone2 == 1 then GlobalColor("GreenZone") else if apc == 10 and RZone2 == 1 then GlobalColor("RedZone") else if apc == 10 and Gzone2 ==0 and RZone2 == 0 then GlobalColor("Neutral") else if apc == 11 and GZone3 == 1 then GlobalColor("GreenZone") else if apc == 11 and RZone3 == 1 then GlobalColor("RedZone") else if apc == 11 and Gzone3 ==0 and RZone3 ==0 then GlobalColor("Neutral") else if apc == 13 and SBa == 1 then GlobalColor(“Greenbar”) else if APC == 13 and SBa == 2 then GlobalColor(“Squatbar”) else if apc == 13 and SBa == 3 then GlobalColor(“Fadebar”) else if apc == 13 and SBa == 4 then GlobalColor(“Fakebar”) else if apc == 14 and SBb == 1 then GlobalColor(“Greenbar”) else if APC == 14 and SBb == 2 then GlobalColor(“Squatbar”) else if apc == 14 and SBb == 3 then GlobalColor(“Fadebar”) else if apc == 14 and SBb == 4 then GlobalColor(“Fakebar”)else if apc == 15 and SBc == 1 then GlobalColor(“Greenbar”) else if APC == 15 and SBc == 2 then GlobalColor(“Squatbar”) else if apc == 15 and SBc == 3 then GlobalColor(“Fadebar”) else if apc ==15 and SBc == 4 then GlobalColor(“Fakebar”) else Color.current);