I'm working on a script to find the different between two Zigzag high pivot points (would also like to do this for the low side also). Please see image. The bubble at "1" would green with the value of "93.74 and 43.01 (difference between previous high and current value of the Zigzag). If flip the scenario the bubble at "1" would red. Any help would be great.
This is the line of code I think I'm messing up.
def ATRSave = if !IsNaN(ZZ) then SMI1 else GetValue(ATRSave, 1);
def ATRchg = round(SMI1 - GetValue(ATRSave, 1));
plot ATRisUp = ATRchg >= 0;
ATRisUp.Hide();
def ATRisDown = ATRchg <= 0;
def isConf = AbsValue(ATRchg) >= reversalAmount or (IsNaN(getValue(ZZ, 1)) and getValue(isConf, 1));
plot ATRDiff= ATRchg;
ATRDiff.hide();
AddChartBubble(1, ZZ, SMI1+" "+ATRchg, if ATRisUp then Color.GREEN else if ATRisDown then Color.RED else Color.RED, ATRisUp);
[/CODE]
[/B]
This is the line of code I think I'm messing up.
def ATRSave = if !IsNaN(ZZ) then SMI1 else GetValue(ATRSave, 1);
def ATRchg = round(SMI1 - GetValue(ATRSave, 1));
plot ATRisUp = ATRchg >= 0;
ATRisUp.Hide();
def ATRisDown = ATRchg <= 0;
def isConf = AbsValue(ATRchg) >= reversalAmount or (IsNaN(getValue(ZZ, 1)) and getValue(isConf, 1));
plot ATRDiff= ATRchg;
ATRDiff.hide();
AddChartBubble(1, ZZ, SMI1+" "+ATRchg, if ATRisUp then Color.GREEN else if ATRisDown then Color.RED else Color.RED, ATRisUp);
[/CODE]
Ruby:
declare lower;
input length = 14;
input averageType1 = AverageType.WILDERS;
plot ADX = DMI(length, averageType1).ADX;
ADX.SetDefaultColor(GetColor(5));
input KPeriod = 8;
input n3 = 5;
input priceHl = high;
input priceLl = low;
input priceCl = close;
input slowing_period = 3;
input averageType = AverageType.SIMPLE;
input showBreakoutSignals = {default "No", "On FullK", "On FullD", "On FullK & FullD"};
input showlabels = yes;
def lowest_k = Lowest(priceLl, KPeriod);
def c1 = priceCl - lowest_k;
def c2 = Highest(priceHl, KPeriod) - lowest_k;
def FastK = if c2 != 0 then c1 / c2 * 100 else 0;
plot SMI1 = round(MovingAverage(averageType, FastK, slowing_period));
def o = open;
def h = high;
def l = low;
def c = close;
def priceH = SMI1;
def priceL = SMI1;
def priceC = SMI1;
input percentageReversal = 5.0;
input absoluteReversal = 0.0;
input atrLength = 5;
input atrReversal = 1;
input tickReversal = 0;
Assert(percentageReversal >= 0, "'percentage reversal' must not be negative: " + percentageReversal);
Assert(absoluteReversal >= 0, "'absolute reversal' must not be negative: " + absoluteReversal);
Assert(atrReversal >= 0, "'atr reversal' must not be negative: " + atrReversal);
Assert(tickReversal >= 0, "'ticks' must not be negative: " + tickReversal);
Assert(percentageReversal != 0 or absoluteReversal != 0 or atrReversal != 0 or tickReversal != 0, "Either 'percentage reversal' or 'absolute reversal' or 'atr reversal' or 'tick reversal' must not be zero");
def absReversal;
if (absoluteReversal != 0) {
absReversal = absoluteReversal;
} else {
absReversal = tickReversal * TickSize();
}
def hlPivot;
if (atrReversal != 0) {
hlPivot = percentageReversal / 100 + WildersAverage(TrueRange(SMI1, SMI1, SMI1), atrLength) / SMI1 * atrReversal;
} else {
hlPivot = percentageReversal / 100;
}
def state = {default init, undefined, uptrend, downtrend};
def maxPriceH;
def minPriceL;
def newMax;
def newMin;
def prevMaxH = GetValue(maxPriceH, 1);
def prevMinL = GetValue(minPriceL, 1);
if GetValue(state, 1) == GetValue(state.init, 0) {
maxPriceH = priceH;
minPriceL = priceL;
newMax = yes;
newMin = yes;
state = state.undefined;
} else if GetValue(state, 1) == GetValue(state.undefined, 0) {
if priceH >= prevMaxH {
state = state.uptrend;
maxPriceH = priceH;
minPriceL = prevMinL;
newMax = yes;
newMin = no;
} else if priceL <= prevMinL {
state = state.downtrend;
maxPriceH = prevMaxH;
minPriceL = priceL;
newMax = no;
newMin = yes;
} else {
state = state.undefined;
maxPriceH = prevMaxH;
minPriceL = prevMinL;
newMax = no;
newMin = no;
}
} else if GetValue(state, 1) == GetValue(state.uptrend, 0) {
if priceL <= prevMaxH - prevMaxH * hlPivot - absReversal {
state = state.downtrend;
maxPriceH = prevMaxH;
minPriceL = priceL;
newMax = no;
newMin = yes;
} else {
state = state.uptrend;
if (priceH >= prevMaxH) {
maxPriceH = priceH;
newMax = yes;
} else {
maxPriceH = prevMaxH;
newMax = no;
}
minPriceL = prevMinL;
newMin = no;
}
} else {
if priceH >= prevMinL + prevMinL * hlPivot + absReversal {
state = state.uptrend;
maxPriceH = priceH;
minPriceL = prevMinL;
newMax = yes;
newMin = no;
} else {
state = state.downtrend;
maxPriceH = prevMaxH;
newMax = no;
if (priceL <= prevMinL) {
minPriceL = priceL;
newMin = yes;
} else {
minPriceL = prevMinL;
newMin = no;
}
}
}
def barNumber = BarNumber();
def barCount = HighestAll(If(IsNaN(priceH), 0, barNumber));
def newState = GetValue(state, 0) != GetValue(state, 1);
def offset = barCount - barNumber + 1;
def highPoint = state == state.uptrend and priceH == maxPriceH;
def lowPoint = state == state.downtrend and priceL == minPriceL;
def lastH;
if highPoint and offset > 1 {
lastH = fold iH = 1 to offset with tH = priceH while !IsNaN(tH) and !GetValue(newState, -iH) do if GetValue(newMax, -iH) or iH == offset - 1 and GetValue(priceH, -iH) == tH then Double.NaN else tH;
} else {
lastH = Double.NaN;
}
def lastL;
if lowPoint and offset > 1 {
lastL = fold iL = 1 to offset with tL = priceL while !IsNaN(tL) and !GetValue(newState, -iL) do if GetValue(newMin, -iL) or iL == offset - 1 and GetValue(priceL, -iL) == tL then Double.NaN else tL;
} else {
lastL = Double.NaN;
}
plot ZZ;
if barNumber == 1 {
ZZ = fold iF = 1 to offset with tP = Double.NaN while IsNaN(tP) do if GetValue(state, -iF) == GetValue(state.uptrend, 0) then priceL else if GetValue(state, -iF) == GetValue(state.downtrend, 0) then priceH else Double.NaN;
} else if barNumber == barCount {
ZZ = if highPoint or state == state.downtrend and priceL > minPriceL then priceH else if lowPoint or state == state.uptrend and priceH < maxPriceH then priceL else Double.NaN;
} else {
ZZ = if !IsNaN(lastH) then lastH else if !IsNaN(lastL) then lastL else Double.NaN;
}
ZZ.SetDefaultColor(GetColor(1));
ZZ.EnableApproximation();
#ZZ.hide();
def bar = BarNumber();
input n = 10;
def CurrMACDh = if SMI1 > 0
then fold i = 1 to n + 1
with p = 1
while p
do SMI1 > GetValue(SMI1, -i)
else 0;
def CurrMACDPivotH = if (bar > n and
SMI1 == Highest(SMI1, n) and
CurrMACDh)
then SMI1
else Double.NaN;
def CurrMACDl = if SMI1 > 0
then fold j = 1 to n + 1
with q = 1
while q
do SMI1 < GetValue(SMI1, -j)
else 0;
def CurrMACDPivotL = if (bar > n and
SMI1 == Lowest(SMI1, n) and
CurrMACDl)
then SMI1
else Double.NaN;
def CurrPHBar = if !IsNaN(CurrMACDPivotH)
then bar
else CurrPHBar[1];
def CurrPLBar = if !IsNaN(CurrMACDPivotL)
then bar
else CurrPLBar[1];
def PHpoint = if !IsNaN(CurrMACDPivotH)
then CurrMACDPivotH
else PHpoint[1];
def priorPHBar = if PHpoint != PHpoint[1]
then CurrPHBar[1]
else priorPHBar[1];
def PLpoint = if !IsNaN(CurrMACDPivotL)
then CurrMACDPivotL
else PLpoint[1];
def priorPLBar = if PLpoint != PLpoint[1]
then CurrPLBar[1]
else priorPLBar[1];
def HighPivots = bar >= HighestAll(priorPHBar);
def LowPivots = bar >= HighestAll(priorPLBar);
def pivotHigh = if HighPivots
then CurrMACDPivotH
else Double.NaN;
plot PlotHline = pivotHigh;
PlotHline.EnableApproximation();
PlotHline.SetDefaultColor(GetColor(7));
PlotHline.SetStyle(Curve.SHORT_DASH);
plot pivotLow = if LowPivots
then CurrMACDPivotL
else Double.NaN;
pivotLow.EnableApproximation();
pivotLow.SetDefaultColor(GetColor(7));
pivotLow.SetStyle(Curve.SHORT_DASH);
plot PivotDot = if !IsNaN(pivotHigh)
then pivotHigh
else if !IsNaN(pivotLow)
then pivotLow
else Double.NaN;
PivotDot.SetDefaultColor(GetColor(7));
PivotDot.SetPaintingStrategy(PaintingStrategy.POINTS);
PivotDot.SetLineWeight(3);
input reversalamount =8.0;
[B]def ATRSave = if !IsNaN(ZZ) then SMI1 else GetValue(ATRSave, 1);
def ATRchg = round(SMI1 - GetValue(ATRSave, 1));
plot ATRisUp = ATRchg >= 0;
ATRisUp.Hide();
def ATRisDown = ATRchg <= 0;
def isConf = AbsValue(ATRchg) >= reversalAmount or (IsNaN(getValue(ZZ, 1)) and getValue(isConf, 1));
plot ATRDiff= ATRchg;
ATRDiff.hide();
AddChartBubble(1, ZZ, SMI1+" "+ATRchg, if ATRisUp then Color.GREEN else if ATRisDown then Color.RED else Color.RED, ATRisUp);
Last edited by a moderator: