Market Structure Algo for ThinkOrSwim

samer800

Moderator - Expert
VIP
Lifetime
KJrhTgB.png


Author Message :
This advanced indicator is designed to analyze the market's structure through a combination of pivot highs and lows, creating a nuanced understanding of potential market movements.
Operational Mechanism:
  • The MS Algo calculates pivot highs and lows over specified periods (input by the user) to determine the market's current structure. It then evaluates the market's position relative to these pivot points to assign a market structure score, which can range from bullish to bearish extremes.
  • Signals for long and short positions, as well as exits, are generated based on the interaction between the close price and these pivot points.
  • Additionally, the indicator plots zones around the moving average, adjusted for the ATR and the specified 'Zone Distance,' providing a visual guide to areas where the market might find support or resistance.
https://www.tradingview.com/script/IUR1cEYW-Market-Structure-Algo/ i
CODE:

CSS:
#/ This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © OmegaTools
#indicator("Market Structure Algo", "MS Algo", true)
# Converted by Sam4Cok@Samer800    - 03/2024

input colorBars = yes;
input showExternalMarketStructureLines = yes;
input showInternalMarketStructureLines = no;
input InternalMS = 5; #, "Internal MS", minval = 2)
input ExternalMS = 30; #, "External MS", minval = 7)
input ZoneDistance = 2.00; #, "Zone Distance", minval = 1, step = 0.1, inline = "zone")
input showZone = yes; #(true, "", inline = "zone")

def na = Double.NaN;

def right = InternalMS;
def right1 = ExternalMS;

DefineGlobalColor("up", CreateColor(41, 98, 255)); # "Positive color"
DefineGlobalColor("dn", CreateColor(233, 30, 99)); # "Negative color"
DefineGlobalColor("bgup", CreateColor(0, 58, 220)); # "Positive color"
DefineGlobalColor("bgdn", CreateColor(170, 17, 69)); # "Negative color"

script Pivots {
    input series    = close;
    input leftBars  = 10;
    input rightBars = 10;
    input isHigh = yes;
    def na = Double.NaN;
    def HH = series == Highest(series, leftBars + 1);
    def LL = series == Lowest(series, leftBars + 1);
    def pivotRange = (leftBars + rightBars + 1);
    def leftEdgeValue = if series[pivotRange] == 0 then na else series[pivotRange];
    def pvtCond = !IsNaN(series) and leftBars > 0 and rightBars > 0 and !IsNaN(leftEdgeValue);
    def barIndexH = if pvtCond then
                    fold i = 1 to rightBars + 1 with p=1 while p do
                    series > GetValue(series, - i) else na;
    def barIndexL = if pvtCond then
                    fold j = 1 to rightBars + 1 with q=1 while q do
                    series < GetValue(series, - j) else na;
    def PivotPoint;
    if isHigh {
        PivotPoint = if HH and barIndexH then series else na;
    } else {
        PivotPoint = if LL and barIndexL then series else na;
    }
    plot pvt = PivotPoint;
}

#// Internal
def pvh = pivots(high, InternalMS, right, yes)[right];
def pvl = pivots(low, InternalMS, right, no)[right];
def ph = if !IsNaN(pvh) then pvh else ph[1];
def pl = if !IsNaN(pvl) then pvl else pl[1];

def currenth;
def lasth;
if !IsNaN(pvh) {
    lasth = currenth[1];
    currenth = high[right];
} else {
    lasth = lasth[1];
    currenth = currenth[1];
}
def currentl;
def lastl;
if !IsNaN(pvl) {
    lastl = currentl[1];
    currentl = low[right];
} else {
    lastl = lastl[1];
    currentl = currentl[1];
}
def ms = if currenth > lasth and currentl > lastl and close > ph then 2 else
         if close > ph then 1 else
         if currenth < lasth and currentl < lastl and close < pl then -2 else
         if close < pl then -1 else ms[1];
def longsig   = close > ph and ms[1] < 0;
def shortsig  = close < pl and ms[1] > 0;
def longsig1  = close > ph and ms[1] > 0;
def shortsig1 = close < pl and ms[1] < 0;
def longsig2  = longsig1 and longsig1[1] == no and longsig[1] == no;
def shortsig2 = shortsig1 and shortsig1[1] == no and shortsig[1] == no;

#// External
def pvh1 = pivots(high, ExternalMS, right1, yes)[right1];
def pvl1 = pivots(low, ExternalMS, right1, no)[right1];
def ph1 = if !IsNaN(pvh1) then pvh1 else ph1[1];
def pl1 = if !IsNaN(pvl1) then pvl1 else pl1[1];
#// Zone
def ma = Average(close, ExternalMS);
def atr = ATR(Length = ExternalMS);
def ma2 = if ms > 0 and ms[1] > 0 then ma - atr * ZoneDistance else
          if ms < 0 and ms[1] < 0 then ma + atr * ZoneDistance else na;
def ma3 = if ms > 0 and ms[1] > 0 then ma2 + atr else
          if ms < 0 and ms[1] < 0 then ma2 - atr else na;

#// Plot
def r0 = Round(right/2,  0) + right;
def r1 = Round(right1/2, 0) + right1;
def l1 = if !IsNaN(pvh[-right]) then ph[-right] else l1[1];
def l2 = if !IsNaN(pvl[-right]) then pl[-right] else l2[1];
def l3 = if !IsNaN(pvh1[-right1]) then ph1[-right1] else l3[1];
def l4 = if !IsNaN(pvl1[-right1]) then pl1[-right1] else l4[1];
def c1 = if !IsNaN(pvh[-right]) then 0 else c1[1] + 1;
def c2 = if !IsNaN(pvl[-right]) then 0 else c2[1] + 1;
def c3 = if !IsNaN(pvh1[-right1]) then 0 else c3[1] + 1;
def c4 = if !IsNaN(pvl1[-right1]) then 0 else c4[1] + 1;

def col = if close > ma2 then 1 else if close < ma2 then -1 else 0;
def barCol = if ms >= 2 then  2 else
             if ms == 1 then  1 else
             if ms <=-2 then -2 else
             if ms ==-1 then -1 else 0;

plot Line4 = if showExternalMarketStructureLines and l4 and c4 <= r1 then l4 else na;
plot Line3 = if showExternalMarketStructureLines and l3 and c3 <= r1 then l3 else na;
plot Line2 = if showInternalMarketStructureLines and l2 and c2 <= r0 then l2 else na;
plot Line1 = if showInternalMarketStructureLines and l1 and c1 <= r0 then l1 else na;
Line4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Line3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Line2.SetPaintingStrategy(PaintingStrategy.DASHES);
Line1.SetPaintingStrategy(PaintingStrategy.DASHES);
Line4.setDefaultColor(GlobalColor("dn"));
Line3.setDefaultColor(GlobalColor("up"));
Line2.setDefaultColor(GlobalColor("bgdn"));
Line1.setDefaultColor(GlobalColor("bgup"));

plot ExternalZone = if showZone and col then ma2 else na; # "External Zone"
def InternalZone = if showZone and col then ma3 else na; # "Internal Zone"
ExternalZone.AssignValueColor(if col >0  then GlobalColor("up") else GlobalColor("dn"));

AddCloud(InternalZone, ExternalZone, GlobalColor("bgup"), GlobalColor("bgdn"));

#-- Signals
plot buy = if longsig2 then low else na;       # "Buy Signal"
plot sell = if shortsig2 then high else na;    # "Sell Signal"

buy.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
sell.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
buy.SetDefaultColor(GlobalColor("up"));
sell.SetDefaultColor(GlobalColor("dn"));

AddChartBubble(longsig[1], if showZone then ma2 else low, "Buy", Color.GREEN, no);
AddChartBubble(shortsig[1],if showZone then ma2 else high, "Sell", Color.RED);

#-- BarColor
AssignPriceColor(if !colorBars then Color.CURRENT else
                 if barCol == 2 then Color.GREEN else
                 if barCol == 1 then Color.DARK_GREEN else
                 if barCol ==-2 then Color.RED else
                 if barCol ==-1 then Color.DARK_RED else Color.GRAY);

#-- END of CODE
 
Last edited by a moderator:

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

KJrhTgB.png


Author Message :
This advanced indicator is designed to analyze the market's structure through a combination of pivot highs and lows, creating a nuanced understanding of potential market movements.
Operational Mechanism:
  • The MS Algo calculates pivot highs and lows over specified periods (input by the user) to determine the market's current structure. It then evaluates the market's position relative to these pivot points to assign a market structure score, which can range from bullish to bearish extremes.
  • Signals for long and short positions, as well as exits, are generated based on the interaction between the close price and these pivot points.
  • Additionally, the indicator plots zones around the moving average, adjusted for the ATR and the specified 'Zone Distance,' providing a visual guide to areas where the market might find support or resistance.
https://www.tradingview.com/script/IUR1cEYW-Market-Structure-Algo/ i
CODE:

CSS:
#/ This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © OmegaTools
#indicator("Market Structure Algo", "MS Algo", true)
# Converted by Sam4Cok@Samer800    - 03/2024

input colorBars = yes;
input showExternalMarketStructureLines = yes;
input showInternalMarketStructureLines = no;
input InternalMS = 5; #, "Internal MS", minval = 2)
input ExternalMS = 30; #, "External MS", minval = 7)
input ZoneDistance = 2.00; #, "Zone Distance", minval = 1, step = 0.1, inline = "zone")
input showZone = yes; #(true, "", inline = "zone")

def na = Double.NaN;

def right = InternalMS;
def right1 = ExternalMS;

DefineGlobalColor("up", CreateColor(41, 98, 255)); # "Positive color"
DefineGlobalColor("dn", CreateColor(233, 30, 99)); # "Negative color"
DefineGlobalColor("bgup", CreateColor(0, 58, 220)); # "Positive color"
DefineGlobalColor("bgdn", CreateColor(170, 17, 69)); # "Negative color"

script Pivots {
    input series    = close;
    input leftBars  = 10;
    input rightBars = 10;
    input isHigh = yes;
    def na = Double.NaN;
    def HH = series == Highest(series, leftBars + 1);
    def LL = series == Lowest(series, leftBars + 1);
    def pivotRange = (leftBars + rightBars + 1);
    def leftEdgeValue = if series[pivotRange] == 0 then na else series[pivotRange];
    def pvtCond = !IsNaN(series) and leftBars > 0 and rightBars > 0 and !IsNaN(leftEdgeValue);
    def barIndexH = if pvtCond then
                    fold i = 1 to rightBars + 1 with p=1 while p do
                    series > GetValue(series, - i) else na;
    def barIndexL = if pvtCond then
                    fold j = 1 to rightBars + 1 with q=1 while q do
                    series < GetValue(series, - j) else na;
    def PivotPoint;
    if isHigh {
        PivotPoint = if HH and barIndexH then series else na;
    } else {
        PivotPoint = if LL and barIndexL then series else na;
    }
    plot pvt = PivotPoint;
}

#// Internal
def pvh = pivots(high, InternalMS, right, yes)[right];
def pvl = pivots(low, InternalMS, right, no)[right];
def ph = if !IsNaN(pvh) then pvh else ph[1];
def pl = if !IsNaN(pvl) then pvl else pl[1];

def currenth;
def lasth;
if !IsNaN(pvh) {
    lasth = currenth[1];
    currenth = high[right];
} else {
    lasth = lasth[1];
    currenth = currenth[1];
}
def currentl;
def lastl;
if !IsNaN(pvl) {
    lastl = currentl[1];
    currentl = low[right];
} else {
    lastl = lastl[1];
    currentl = currentl[1];
}
def ms = if currenth > lasth and currentl > lastl and close > ph then 2 else
         if close > ph then 1 else
         if currenth < lasth and currentl < lastl and close < pl then -2 else
         if close < pl then -1 else ms[1];
def longsig   = close > ph and ms[1] < 0;
def shortsig  = close < pl and ms[1] > 0;
def longsig1  = close > ph and ms[1] > 0;
def shortsig1 = close < pl and ms[1] < 0;
def longsig2  = longsig1 and longsig1[1] == no and longsig[1] == no;
def shortsig2 = shortsig1 and shortsig1[1] == no and shortsig[1] == no;

#// External
def pvh1 = pivots(high, ExternalMS, right1, yes)[right1];
def pvl1 = pivots(low, ExternalMS, right1, no)[right1];
def ph1 = if !IsNaN(pvh1) then pvh1 else ph1[1];
def pl1 = if !IsNaN(pvl1) then pvl1 else pl1[1];
#// Zone
def ma = Average(close, ExternalMS);
def atr = ATR(Length = ExternalMS);
def ma2 = if ms > 0 and ms[1] > 0 then ma - atr * ZoneDistance else
          if ms < 0 and ms[1] < 0 then ma + atr * ZoneDistance else na;
def ma3 = if ms > 0 and ms[1] > 0 then ma2 + atr else
          if ms < 0 and ms[1] < 0 then ma2 - atr else na;

#// Plot
def r0 = Round(right/2,  0) + right;
def r1 = Round(right1/2, 0) + right1;
def l1 = if !IsNaN(pvh[-right]) then ph[-right] else l1[1];
def l2 = if !IsNaN(pvl[-right]) then pl[-right] else l2[1];
def l3 = if !IsNaN(pvh1[-right1]) then ph1[-right1] else l3[1];
def l4 = if !IsNaN(pvl1[-right1]) then pl1[-right1] else l4[1];
def c1 = if !IsNaN(pvh[-right]) then 0 else c1[1] + 1;
def c2 = if !IsNaN(pvl[-right]) then 0 else c2[1] + 1;
def c3 = if !IsNaN(pvh1[-right1]) then 0 else c3[1] + 1;
def c4 = if !IsNaN(pvl1[-right1]) then 0 else c4[1] + 1;

def col = if close > ma2 then 1 else if close < ma2 then -1 else 0;
def barCol = if ms >= 2 then  2 else
             if ms == 1 then  1 else
             if ms <=-2 then -2 else
             if ms ==-1 then -1 else 0;

plot Line4 = if showExternalMarketStructureLines and l4 and c4 <= r1 then l4 else na;
plot Line3 = if showExternalMarketStructureLines and l3 and c3 <= r1 then l3 else na;
plot Line2 = if showInternalMarketStructureLines and l2 and c2 <= r0 then l2 else na;
plot Line1 = if showInternalMarketStructureLines and l1 and c1 <= r0 then l1 else na;
Line4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Line3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Line2.SetPaintingStrategy(PaintingStrategy.DASHES);
Line1.SetPaintingStrategy(PaintingStrategy.DASHES);
Line4.setDefaultColor(GlobalColor("dn"));
Line3.setDefaultColor(GlobalColor("up"));
Line2.setDefaultColor(GlobalColor("bgdn"));
Line1.setDefaultColor(GlobalColor("bgup"));

plot ExternalZone = if showZone and col then ma2 else na; # "External Zone"
def InternalZone = if showZone and col then ma3 else na; # "Internal Zone"
ExternalZone.AssignValueColor(if col >0  then GlobalColor("up") else GlobalColor("dn"));

AddCloud(InternalZone, ExternalZone, GlobalColor("bgup"), GlobalColor("bgdn"));

#-- Signals
plot buy = if longsig2 then low else na;       # "Buy Signal"
plot sell = if shortsig2 then high else na;    # "Sell Signal"

buy.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
sell.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
buy.SetDefaultColor(GlobalColor("up"));
sell.SetDefaultColor(GlobalColor("dn"));

AddChartBubble(longsig[1], if showZone then ma2 else low, "Buy", Color.GREEN, no);
AddChartBubble(shortsig[1],if showZone then ma2 else high, "Sell", Color.RED);

#-- BarColor
AssignPriceColor(if !colorBars then Color.CURRENT else
                 if barCol == 2 then Color.GREEN else
                 if barCol == 1 then Color.DARK_GREEN else
                 if barCol ==-2 then Color.RED else
                 if barCol ==-1 then Color.DARK_RED else Color.GRAY);

#-- END of CODE
Looks like a great chart. Is there a scan to find stocks where the "buy" and/or "sell" signal just appear?
 
Looks like a great chart. Is there a scan to find stocks where the "buy" and/or "sell" signal just appear?
try this. change the plot to scan long/short

CSS:
#/ This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © OmegaTools
#indicator("Market Structure Algo", "MS Algo", true)
# Scan by Sam4Cok@Samer800    - 03/2024

input InternalMS = 5; #, "Internal MS", minval = 2)
input ExternalMS = 30; #, "External MS", minval = 7)
input ZoneDistance = 2.00; #, "Zone Distance", minval = 1, step = 0.1, inline = "zone")

def na = Double.NaN;

def right = InternalMS;
#def right1 = ExternalMS;

DefineGlobalColor("up", CreateColor(41, 98, 255)); # "Positive color"
DefineGlobalColor("dn", CreateColor(233, 30, 99)); # "Negative color"
DefineGlobalColor("bgup", CreateColor(0, 58, 220)); # "Positive color"
DefineGlobalColor("bgdn", CreateColor(170, 17, 69)); # "Negative color"

script Pivots {
    input series    = close;
    input leftBars  = 10;
    input rightBars = 10;
    input isHigh = yes;
    def na = Double.NaN;
    def HH = series == Highest(series, leftBars + 1);
    def LL = series == Lowest(series, leftBars + 1);
    def pivotRange = (leftBars + rightBars + 1);
    def leftEdgeValue = if series[pivotRange] == 0 then na else series[pivotRange];
    def pvtCond = !IsNaN(series) and leftBars > 0 and rightBars > 0 and !IsNaN(leftEdgeValue);
    def barIndexH = if pvtCond then
                    fold i = 1 to rightBars + 1 with p=1 while p do
                    series > GetValue(series, - i) else na;
    def barIndexL = if pvtCond then
                    fold j = 1 to rightBars + 1 with q=1 while q do
                    series < GetValue(series, - j) else na;
    def PivotPoint;
    if isHigh {
        PivotPoint = if HH and barIndexH then series else na;
    } else {
        PivotPoint = if LL and barIndexL then series else na;
    }
    plot pvt = PivotPoint;
}

#// Internal
def pvh = pivots(high, InternalMS, right, yes)[right];
def pvl = pivots(low, InternalMS, right, no)[right];
def ph = if !IsNaN(pvh) then pvh else ph[1];
def pl = if !IsNaN(pvl) then pvl else pl[1];

def currenth;
def lasth;
if !IsNaN(pvh) {
    lasth = currenth[1];
    currenth = high[right];
} else {
    lasth = lasth[1];
    currenth = currenth[1];
}
def currentl;
def lastl;
if !IsNaN(pvl) {
    lastl = currentl[1];
    currentl = low[right];
} else {
    lastl = lastl[1];
    currentl = currentl[1];
}
def ms = if currenth > lasth and currentl > lastl and close > ph then 2 else
         if close > ph then 1 else
         if currenth < lasth and currentl < lastl and close < pl then -2 else
         if close < pl then -1 else ms[1];
def longsig   = close > ph and ms[1] < 0;
def shortsig  = close < pl and ms[1] > 0;

plot long = longsig[1] within 5 bars;
#plot short = shortsig[1] within 3 bars;


#-- END of CODE
 
Last edited by a moderator:
try this. change the plot to scan long/short

CSS:
#/ This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © OmegaTools
#indicator("Market Structure Algo", "MS Algo", true)
# Scan by Sam4Cok@Samer800    - 03/2024

input InternalMS = 5; #, "Internal MS", minval = 2)
input ExternalMS = 30; #, "External MS", minval = 7)
input ZoneDistance = 2.00; #, "Zone Distance", minval = 1, step = 0.1, inline = "zone")

def na = Double.NaN;

def right = InternalMS;
#def right1 = ExternalMS;

DefineGlobalColor("up", CreateColor(41, 98, 255)); # "Positive color"
DefineGlobalColor("dn", CreateColor(233, 30, 99)); # "Negative color"
DefineGlobalColor("bgup", CreateColor(0, 58, 220)); # "Positive color"
DefineGlobalColor("bgdn", CreateColor(170, 17, 69)); # "Negative color"

script Pivots {
    input series    = close;
    input leftBars  = 10;
    input rightBars = 10;
    input isHigh = yes;
    def na = Double.NaN;
    def HH = series == Highest(series, leftBars + 1);
    def LL = series == Lowest(series, leftBars + 1);
    def pivotRange = (leftBars + rightBars + 1);
    def leftEdgeValue = if series[pivotRange] == 0 then na else series[pivotRange];
    def pvtCond = !IsNaN(series) and leftBars > 0 and rightBars > 0 and !IsNaN(leftEdgeValue);
    def barIndexH = if pvtCond then
                    fold i = 1 to rightBars + 1 with p=1 while p do
                    series > GetValue(series, - i) else na;
    def barIndexL = if pvtCond then
                    fold j = 1 to rightBars + 1 with q=1 while q do
                    series < GetValue(series, - j) else na;
    def PivotPoint;
    if isHigh {
        PivotPoint = if HH and barIndexH then series else na;
    } else {
        PivotPoint = if LL and barIndexL then series else na;
    }
    plot pvt = PivotPoint;
}

#// Internal
def pvh = pivots(high, InternalMS, right, yes)[right];
def pvl = pivots(low, InternalMS, right, no)[right];
def ph = if !IsNaN(pvh) then pvh else ph[1];
def pl = if !IsNaN(pvl) then pvl else pl[1];

def currenth;
def lasth;
if !IsNaN(pvh) {
    lasth = currenth[1];
    currenth = high[right];
} else {
    lasth = lasth[1];
    currenth = currenth[1];
}
def currentl;
def lastl;
if !IsNaN(pvl) {
    lastl = currentl[1];
    currentl = low[right];
} else {
    lastl = lastl[1];
    currentl = currentl[1];
}
def ms = if currenth > lasth and currentl > lastl and close > ph then 2 else
         if close > ph then 1 else
         if currenth < lasth and currentl < lastl and close < pl then -2 else
         if close < pl then -1 else ms[1];
def longsig   = close > ph and ms[1] < 0;
def shortsig  = close < pl and ms[1] > 0;

plot long = longsig[1] within 5 bars;
#plot short = shortsig[1] within 3 bars;


#-- END of CODE
Wow! Thank you! That appears to work.
 
KJrhTgB.png


Author Message :
This advanced indicator is designed to analyze the market's structure through a combination of pivot highs and lows, creating a nuanced understanding of potential market movements.
Operational Mechanism:
  • The MS Algo calculates pivot highs and lows over specified periods (input by the user) to determine the market's current structure. It then evaluates the market's position relative to these pivot points to assign a market structure score, which can range from bullish to bearish extremes.
  • Signals for long and short positions, as well as exits, are generated based on the interaction between the close price and these pivot points.
  • Additionally, the indicator plots zones around the moving average, adjusted for the ATR and the specified 'Zone Distance,' providing a visual guide to areas where the market might find support or resistance.
https://www.tradingview.com/script/IUR1cEYW-Market-Structure-Algo/ i
CODE:

CSS:
#/ This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © OmegaTools
#indicator("Market Structure Algo", "MS Algo", true)
# Converted by Sam4Cok@Samer800    - 03/2024

input colorBars = yes;
input showExternalMarketStructureLines = yes;
input showInternalMarketStructureLines = no;
input InternalMS = 5; #, "Internal MS", minval = 2)
input ExternalMS = 30; #, "External MS", minval = 7)
input ZoneDistance = 2.00; #, "Zone Distance", minval = 1, step = 0.1, inline = "zone")
input showZone = yes; #(true, "", inline = "zone")

def na = Double.NaN;

def right = InternalMS;
def right1 = ExternalMS;

DefineGlobalColor("up", CreateColor(41, 98, 255)); # "Positive color"
DefineGlobalColor("dn", CreateColor(233, 30, 99)); # "Negative color"
DefineGlobalColor("bgup", CreateColor(0, 58, 220)); # "Positive color"
DefineGlobalColor("bgdn", CreateColor(170, 17, 69)); # "Negative color"

script Pivots {
    input series    = close;
    input leftBars  = 10;
    input rightBars = 10;
    input isHigh = yes;
    def na = Double.NaN;
    def HH = series == Highest(series, leftBars + 1);
    def LL = series == Lowest(series, leftBars + 1);
    def pivotRange = (leftBars + rightBars + 1);
    def leftEdgeValue = if series[pivotRange] == 0 then na else series[pivotRange];
    def pvtCond = !IsNaN(series) and leftBars > 0 and rightBars > 0 and !IsNaN(leftEdgeValue);
    def barIndexH = if pvtCond then
                    fold i = 1 to rightBars + 1 with p=1 while p do
                    series > GetValue(series, - i) else na;
    def barIndexL = if pvtCond then
                    fold j = 1 to rightBars + 1 with q=1 while q do
                    series < GetValue(series, - j) else na;
    def PivotPoint;
    if isHigh {
        PivotPoint = if HH and barIndexH then series else na;
    } else {
        PivotPoint = if LL and barIndexL then series else na;
    }
    plot pvt = PivotPoint;
}

#// Internal
def pvh = pivots(high, InternalMS, right, yes)[right];
def pvl = pivots(low, InternalMS, right, no)[right];
def ph = if !IsNaN(pvh) then pvh else ph[1];
def pl = if !IsNaN(pvl) then pvl else pl[1];

def currenth;
def lasth;
if !IsNaN(pvh) {
    lasth = currenth[1];
    currenth = high[right];
} else {
    lasth = lasth[1];
    currenth = currenth[1];
}
def currentl;
def lastl;
if !IsNaN(pvl) {
    lastl = currentl[1];
    currentl = low[right];
} else {
    lastl = lastl[1];
    currentl = currentl[1];
}
def ms = if currenth > lasth and currentl > lastl and close > ph then 2 else
         if close > ph then 1 else
         if currenth < lasth and currentl < lastl and close < pl then -2 else
         if close < pl then -1 else ms[1];
def longsig   = close > ph and ms[1] < 0;
def shortsig  = close < pl and ms[1] > 0;
def longsig1  = close > ph and ms[1] > 0;
def shortsig1 = close < pl and ms[1] < 0;
def longsig2  = longsig1 and longsig1[1] == no and longsig[1] == no;
def shortsig2 = shortsig1 and shortsig1[1] == no and shortsig[1] == no;

#// External
def pvh1 = pivots(high, ExternalMS, right1, yes)[right1];
def pvl1 = pivots(low, ExternalMS, right1, no)[right1];
def ph1 = if !IsNaN(pvh1) then pvh1 else ph1[1];
def pl1 = if !IsNaN(pvl1) then pvl1 else pl1[1];
#// Zone
def ma = Average(close, ExternalMS);
def atr = ATR(Length = ExternalMS);
def ma2 = if ms > 0 and ms[1] > 0 then ma - atr * ZoneDistance else
          if ms < 0 and ms[1] < 0 then ma + atr * ZoneDistance else na;
def ma3 = if ms > 0 and ms[1] > 0 then ma2 + atr else
          if ms < 0 and ms[1] < 0 then ma2 - atr else na;

#// Plot
def r0 = Round(right/2,  0) + right;
def r1 = Round(right1/2, 0) + right1;
def l1 = if !IsNaN(pvh[-right]) then ph[-right] else l1[1];
def l2 = if !IsNaN(pvl[-right]) then pl[-right] else l2[1];
def l3 = if !IsNaN(pvh1[-right1]) then ph1[-right1] else l3[1];
def l4 = if !IsNaN(pvl1[-right1]) then pl1[-right1] else l4[1];
def c1 = if !IsNaN(pvh[-right]) then 0 else c1[1] + 1;
def c2 = if !IsNaN(pvl[-right]) then 0 else c2[1] + 1;
def c3 = if !IsNaN(pvh1[-right1]) then 0 else c3[1] + 1;
def c4 = if !IsNaN(pvl1[-right1]) then 0 else c4[1] + 1;

def col = if close > ma2 then 1 else if close < ma2 then -1 else 0;
def barCol = if ms >= 2 then  2 else
             if ms == 1 then  1 else
             if ms <=-2 then -2 else
             if ms ==-1 then -1 else 0;

plot Line4 = if showExternalMarketStructureLines and l4 and c4 <= r1 then l4 else na;
plot Line3 = if showExternalMarketStructureLines and l3 and c3 <= r1 then l3 else na;
plot Line2 = if showInternalMarketStructureLines and l2 and c2 <= r0 then l2 else na;
plot Line1 = if showInternalMarketStructureLines and l1 and c1 <= r0 then l1 else na;
Line4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Line3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Line2.SetPaintingStrategy(PaintingStrategy.DASHES);
Line1.SetPaintingStrategy(PaintingStrategy.DASHES);
Line4.setDefaultColor(GlobalColor("dn"));
Line3.setDefaultColor(GlobalColor("up"));
Line2.setDefaultColor(GlobalColor("bgdn"));
Line1.setDefaultColor(GlobalColor("bgup"));

plot ExternalZone = if showZone and col then ma2 else na; # "External Zone"
def InternalZone = if showZone and col then ma3 else na; # "Internal Zone"
ExternalZone.AssignValueColor(if col >0  then GlobalColor("up") else GlobalColor("dn"));

AddCloud(InternalZone, ExternalZone, GlobalColor("bgup"), GlobalColor("bgdn"));

#-- Signals
plot buy = if longsig2 then low else na;       # "Buy Signal"
plot sell = if shortsig2 then high else na;    # "Sell Signal"

buy.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
sell.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
buy.SetDefaultColor(GlobalColor("up"));
sell.SetDefaultColor(GlobalColor("dn"));

AddChartBubble(longsig[1], if showZone then ma2 else low, "Buy", Color.GREEN, no);
AddChartBubble(shortsig[1],if showZone then ma2 else high, "Sell", Color.RED);

#-- BarColor
AssignPriceColor(if !colorBars then Color.CURRENT else
                 if barCol == 2 then Color.GREEN else
                 if barCol == 1 then Color.DARK_GREEN else
                 if barCol ==-2 then Color.RED else
                 if barCol ==-1 then Color.DARK_RED else Color.GRAY);

#-- END of CODE
What part of the code would I change or adjust if I wanted to move the cloud and/or buy & sell bubble forward or back a few candles. I've found the signals to be a little late to the party some time. Another great one from @samer800
 
What part of the code would I change or adjust if I wanted to move the cloud and/or buy & sell bubble forward or back a few candles. I've found the signals to be a little late to the party some time. Another great one from @samer800

Yes, this indicator has a slight lag built-in. It needs to wait until the current candle closes in order to complete its calculations. No, the lag is not adjustable.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
285 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top