# Ichimoku Tenkan/Kijun an early exit warning study_StanL
#the preferred version is version 2 because the arrows accurately show the START of a HA dowtrend
#Now version 2.1
#hint:This is a Ichimoku Tenkan/Kijun an early exit warning study that plots the difference between the tenkan and kijun in histogram format. A YELLOW down warning arrow plots when a Heikin Ashi downtrend starts.
#Background:The difference of the Tenkan and the Kijun indicate the strength of this bullish signal as shown by the histogram above zero values. When this strength difference weakens a downward trend may(often?) start. Heikin Ashi candles are adept at showing trend development. The plotted YELLOW arrows are points to consider exiting even though the entity may be in buillsh territory(i.e. above the cloud). In summary, this is an early warning signal that is productive and calls your attention to it to consider exiting.
#USAGE:When YELLOW down arrows are present, watch the Heikin Ashi style candles to see the extent of the downtrend.(Configure your Heikin Ashi for 'Red Fill' when down. The DMI_Oscillator, MACD and ADX are good complementary studies
#SUMMARY: The histograqm shows the change in strength of the bullish T/K signal while the YELLOW arrows call your attention to a possible start of a downtrend.
#TOS Title = Ichi_TK_Exit_Warning
# Version date = 5/17/14 by StanL, version 2
# Revision 2.1, added dots at the zero line 5/18/14
declare lower;
input Show_HA_Down = YES;#hint HA_Down:Toggles (ON/OFF) HA-Down-candle arrows showing the start of a HA-Down series. View the HA chart style to see the length of the down series.
#input Show_HA_Up = YES;#hint HA_Up:Toggles (ON/OFF) HA-Up-candle arrows showing the start of a HA-Up series. View the HA chart style to see the length of the up series.
plot Diff = Ichimoku().tenkan - Ichimoku().kijun;
plot ZeroLine = 0;
Diff.SetDefaultColor(GetColor(5));
Diff.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
Diff.SetLineWeight(3);
Diff.DefineColor("Positive and Up", Color.GREEN);
Diff.DefineColor("Positive and Down", Color.DARK_GREEN);
Diff.DefineColor("Negative and Down", Color.RED);
Diff.DefineColor("Negative and Up", Color.DARK_RED);
Diff.AssignValueColor(if Diff >= 0 then if Diff > Diff[1] then Diff.Color("Positive and Up") else Diff.Color("Positive and Down") else if Diff < Diff[1] then Diff.Color("Negative and Down") else Diff.Color("Negative and Up"));
ZeroLine.SetDefaultColor(GetColor(0));
plot HistoLiner = Diff;
HistoLiner.SetDefaultColor(Color.WHITE);
HistoLiner.SetPaintingStrategy(PaintingStrategy.LINE);
HistoLiner.SetLineWeight(1);
#################################################
#def HAopen = (open[1] + close[1]) / 2;
#def HAhigh = Max(high, close[1]);
#def HAlow = Min(low, close[1]);
#def HAclose = (open + high + low + close) / 4;
AddLabel(1, "Plot of Tenkan - Kijun early warning of T/K strength weakening", Color.WHITE);
AddLabel(1, "A YELLOW down arrow plots at the start of a HA downtrend.Watch the HA style chart candles to see the length of the downtrend", Color.YELLOW);
#######################################
### Extracted Mobius Heikin Ashi code #
#######################################
input Begin = 0000;
# Higher Aggregation
def barSeq = if SecondsTillTime(Begin) == 0 and
SecondsFromTime(Begin) == 0
then 0
else barSeq[1] + 1;
def agg = GetAggregationPeriod();
def BarsPerHour = 60 / (agg / 1000 / 60);
def bar = barSeq;
def barCount = if bar % BarsPerHour == 0
then bar
else Double.NaN;
def barCountOpen = if !IsNaN(barCount)
then open
else barCountOpen[1];
def barCountHigh = if IsNaN(barCount) and
high > barCountHigh[1]
then high
else if !IsNaN(barCount)
then high
else barCountHigh[1];
def barCountLow = if IsNaN(barCount) and
low < barCountLow[1]
then low
else if !IsNaN(barCount)
then low
else barCountLow[1];
def barCountClose = if !IsNaN(barCount)
then close[1]
else barCountClose[1];
def HAopen_EV = Round(if HAopen_EV[1] == 0
then barCountOpen
else (HAopen_EV[1] + barCountClose[1]) / 2 /
TickSize(), 0) * TickSize();
def HAclose_EV = Round(if HAopen_EV[1] == 0
then OHLC4
else (barCountOpen + barCountHigh + barCountLow + HAopen_EV[1])
/ 4 / TickSize(), 0) * TickSize();
def HAhigh_EV = Round(Max(barCountHigh, barCountClose[1]) /
TickSize(), 0) * TickSize();
def HAlow_EV = Round(Min(barCountLow, barCountClose[1]) /
TickSize(), 0) * TickSize();
# Color Block For Higher Aggregation
def Size = AbsValue(HAopen_EV - HAopen_EV);
def Block = CompoundValue(1, if HAopen_EV > Block[1] + Size[1]
then Block[1] + Size
else if HAopen_EV < Block[1] - Size[1]
then Block[1] - Size
else Block[1] , barCountClose);
plot cond1_EV = Block;
cond1_EV.Hide();
def cond2_EV = if Block != Block[1]
then Block[1]
else cond2_EV[1];
plot Block2 = cond2_EV;
Block2.Hide();
# AssignPriceColor(if HAclose < HAopen
# then Color.Red
# else Color.Green);
# Trade Management Conditions
def Bcond = CompoundValue(1, if HAclose_EV crosses above HAopen_EV
then Bcond[1] + 1
else if HAclose_EV crosses below HAopen_EV
then 0
else Bcond[1], 0);
#UP arrow is the start of an HA-up series. Need to view HA Chart style to see the extent & end
#plot ArrowUp = if Bcond crosses above 0 && Show_HA_Up && Diff < 0 then HistoLiner[0] else Double.NaN;# this line used when up HA arrows are implemented
plot ArrowUp = if Bcond crosses above 0 && Diff < 0 then HistoLiner[0] else Double.NaN;
ArrowUp.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
ArrowUp.SetLineWeight(5);
ArrowUp.SetDefaultColor(Color.YELLOW);
def Scond = CompoundValue(1, if HAclose_EV crosses below HAopen_EV
then Scond[1] + 1
else if barCountClose crosses above HAopen_EV
then 0
else Scond[1], 0);
#DOWN arrow is the start of an HA-down series. Need to view HA Chart style to see the extent & end
plot ArrowDn = if Scond crosses above 0 && Show_HA_Down && Diff > 0
then HistoLiner[0]
else Double.NaN;
ArrowDn.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
ArrowDn.SetLineWeight(5);
ArrowDn.SetDefaultColor(Color.YELLOW);
def lineCond = if ArrowUp < ArrowDn
then 1
else if ArrowDn > ArrowUp
then 1
else -1;
plot line = if IsNaN(ArrowUp)
then ArrowDn - (TickSize() * 2)
else ArrowUp + (TickSize() * 2);
line.EnableApproximation();
line.SetLineWeight(2);
line.SetStyle(Curve.SHORT_DASH);
line.AssignValueColor(if lineCond == 1
then Color.GREEN
else if lineCond == -1
then Color.RED
else Color.WHITE);
######## define dot on zero line when above the cloud ######
def IsAboveCloud = if close > Ichimoku()."Span A" && close > Ichimoku()."Span B" then 1 else 0; # close is above the cloud
def IsBelowCloud = if close < Ichimoku()."Span A" && close < Ichimoku()."Span B" then 1 else 0; # close is below the cloud
Plot AboveCloudDot = if IsAboveCloud then 0 else double.nan;
AboveCloudDot.SetPaintingStrategy(PaintingStrategy.POINTS);
AboveCloudDot.SetLineWeight(3);
AboveCloudDot.SetDefaultColor(Color.GREEN);
Plot BelowCloudDot = if IsBelowCloud then 0 else double.nan;
BelowCloudDot.SetPaintingStrategy(PaintingStrategy.POINTS);
BelowCloudDot.SetLineWeight(3);
BelowCloudDot.SetDefaultColor(Color.RED);
#Define variables used to place a bubble
#========================================
def barNum = BarNumber();
def FirstBar = if barNum == 1 then 1 else 0;
#==========================================
AddChartBubble(FirstBar, 0, "GREEN dot = close IS ABOVE CLOUD\nRED dot = close IS BELOW CLOUD", Color.PINK,no);
#---- End Of Code ---