# Marty Schwartz and Terry Laundry's "MAGIC T Theory"

#### mashume

##### Well-known member
VIP
OK. So here's what I have:

The basic idea is to take Mobius' Pivot Points, count up bars since a high pivot, at the next low pivot start counting down from however many bars you counted up. Plot while those numbers are above zero. You can see in this image that it finds the pivots, and draws the right half of the T, but it does not display the left top bar of the T. I can't for the life of me get it to draw that part.
The lower indicator, called debug shows the bar counts since a high pivot, and the countdown for the right bar. This one has a bunch of additional labels as I was trying to sort out what was wonky with the left T bar. This image represents the indicator as it is uploaded here. There is no ToS link, as the indicator is not really functional yet.

Work in progress, uploaded for community help in finishing / polishing.

It is quite sensitive to the length parameter in the pivot point part of the indicator, though I'm not sure I understand quite why.

-mashume
UPPER
Code:
``````declare upper;

## Inputs
## ProjectionPivots_v03_JQ
## 03.04.2019
## Original Code and Concept by Mobius:
# V01.08.2012 Projection Pivots
# mobius

# Notes:
# 03.04.2019 added linits on extensions
# 03.05.2019 adjusted limits on extensions by adding user input upper and lower extenion percent limits

#declare Once_Per_Bar;

## Inputs
input n = 21;
input showLines = yes;
input showValues = no;
input showBarNumbers = no;
input ExtensionLengthBars = 20; # added to control length of Entension
input UpperExtensionPercentLimit = 5;
input LowerExtensionPercentLimit = 5;
# input DisplayLabel = yes;  ##  JQ 7.8.2018 added
#     addlabel (DisplayLabel, "Projection Pivots n:" + n + " " , color.WHITE);  ##  JQ 7.8.2018 added

#     code from various sources including Mobius, NoLongerNube and others
# Comment out unnecessary portions to preserve tos memory and enhance speed

# Universal Definitions using Padawan variable naming convention (JQ) v03.04.2019
# iData Definitions
def vHigh = high;  # creates the variable vHigh.  Use of the variable reduce data calls to tos iData server
#    def initHigh =  CompoundValue(1, high, high);  # creates and initialized variable for High
def vLow = low;
#    def initLow = CompoundValue(1, low, low);
def vOpen = open;
#    def initOpen = CompoundValue(1, open, open);
def vClose = close;
#    def initClose = CompoundValue(1, close, close);
def vVolume = volume;
#    def initVolume = CompoundValue(1, volume, volume);
def nan = Double.NaN;
# Bar Time & Date
def bn = BarNumber();
def currentBar = HighestAll(if !IsNaN(vHigh) then bn else nan);
#    def Today = GetDay() ==GetLastDay();
#    def time = GetTime();
#    def GlobeX = GetTime() < RegularTradingStart(GetYYYYMMDD());
# def globeX_v2 = if time crosses below RegularTradingEnd(GetYYYYMMDD()) then bn else GlobeX;
#    def RTH = GetTime() > RegularTradingStart(GetYYYYMMDD());
#    def RTH_v2 = if time crosses above RegularTradingStart(GetYYYYMMDD()) then bn else RTH;

# bars that start and end the sessions  #(borrowed from nube)
#    def rthStartBar    = CompoundValue(1,
#                         if   !IsNaN(vClose)
#                         &&   time crosses above RegularTradingStart(GetYYYYMMDD())
#                         then bn
#                         else rthStartBar, 0);
#    def rthEndBar      = CompoundValue(1,
#                         if   !IsNaN(vClose)
#                         &&   time crosses above RegularTradingEnd(GetYYYYMMDD())
#                         then bn
#                         else rthEndBar, 1);
#    def globexStartBar = CompoundValue(1,
#                         if   !IsNaN(vClose)
#                         &&   time crosses below RegularTradingEnd(GetYYYYMMDD())
#                         then bn
#                         else globexStartBar, 1);
#    def rthSession = if   bn crosses above rthStartBar #+ barsExtendedBeyondSession
#                     then 1
#                     else if   bn crosses above rthEndBar #+ barsExtendedBeyondSession
#                          then 0
#                     else rthSession;

# Bubble Locations
def x_AxisLastExpansionBar = BarNumber() == HighestAll(BarNumber());  #corrected 11.12.2018 (JQ)
# syntax: addChartBubble(x_AxisLastExpansionBar, y-axis coordinate," text", Color.LIME); #verified 12.25.2018 (JQ)

def PH;
def PL;
def hh = fold i = 1 to n + 1
with p = 1
while p
do vHigh > getValue(vHigh, -i);
PH = if (bn > n and
vHigh == highest(vHigh, n) and
hh)
then vHigh
else double.NaN;
def ll = fold j = 1 to n + 1
with q = 1
while q
do vLow < getValue(low, -j);
PL = if (bn > n and
vLow == lowest(vLow, n) and
ll)
then vLow
else double.NaN;
def PHBar = if !isNaN(PH)
then bn
else PHBar;
def PLBar = if !isNaN(PL)
then bn
else PLBar;
def PHL = if !isNaN(PH)
then PH
else PHL;
def priorPHBar = if PHL != PHL
then PHBar
else priorPHBar;
def PLL = if !isNaN(PL)
then PL
else PLL;
def priorPLBar = if PLL != PLL
then PLBar
else priorPLBar;
def HighPivots = bn >= highestAll(priorPHBar);
def LowPivots = bn >= highestAll(priorPLBar);
def FirstRpoint = if HighPivots
then bn - PHBar
else 0;
def PriorRpoint = if HighPivots
then bn - PriorPHBar
else 0;
def RSlope = (getvalue(PH, FirstRpoint) - getvalue(PH, PriorRpoint))
/ (PHBar - PriorPHBar);
def FirstSpoint = if LowPivots
then bn - PLBar
else 0;
def PriorSpoint = if LowPivots
then bn - PriorPLBar
else 0;
def SSlope = (getvalue(PL, FirstSpoint) - getvalue(PL, PriorSpoint))
/ (PLBar - PriorPLBar);
def RExtend = if bn == highestall(PHBar)
then 1
else RExtend;
def SExtend = if bn == highestall(PLBar)
then 1
else SExtend;

def pivotHigh = if HighPivots
then PH
else double.NaN;

def pivotHighLine = if PHL > 0 and
HighPivots
then PHL
else double.NaN;

def pivotHighLineBound = if PHL > 0 and
HighPivots
then PHL * 0.98
else double.NaN;

def RLine = pivotHigh;

# Added code to limit resistance estension line (JQ 03.04.2019)
def calc_ResistanceExtension = if RExtend
then (bn - PHBar) * RSlope + PHL
else double.NaN;

# Low Plots
def pivotLow = if LowPivots
then PL
else double.NaN;

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);

# End Code

def left_side_count = if !isNaN(pivotHigh) then 0 else if !isNan(left_side_count) then left_side_count + 1 else double.nan;
def right_side_count = if  !isNaN(pivotLow) then left_side_count else right_side_count - 1;

def lsc = left_side_count;
def rsc = right_side_count;
def zero = 0;

def left_side_plot = if !IsNaN(left_side_count)
# and left_side_count >= right_side_count
and right_side_count >= 0 then pivotHighLine else Double.NaN;
def right_side_plot = if right_side_count > 0 then if !IsNaN(left_side_plot) then left_side_plot else right_side_plot else Double.NaN;

plot ls = left_side_plot;
plot rs = right_side_plot;
addVerticalLine(!isNan(PivotLow) and !isNaN(left_side_plot), color = Color.BLUE, stroke = Curve.FIRM);
plot end_dot = if right_side_plot and isNaN(right_side_plot) then right_side_plot else double.nan;
end_Dot.SetDefaultColor(GetColor(7));
end_Dot.SetPaintingStrategy(PaintingStrategy.POINTS);
end_Dot.SetLineWeight(3);
addChartBubble(pivotDot, HIGH, text = "DOT ==> rsc: " + rsc + "  lsc: " + lsc, color.white);
addChartBubble(pivotHigh, high + 5, "HIGH", color.light_gray);

def start_left_side = pivotDot and pivotHigh and rsc != 0 and lsc == 0;

start_left_side
,high + 10,
"START_LEFT_SIDE!",
color.light_gray);

def end_left_side = pivotDot and pivotLow;

addchartbubble(pivotLow, low - 5, "LOW", color.light_gray);

end_left_side
,high + 5,
"END Left Side!",
color.light_gray);

def left_side_line = if start_left_side then high
else if end_left_side then double.nan
else left_side_line;
plot new_left_side_plot = left_side_line;``````

LOWER
Code:
``````declare upper;

## Inputs
## ProjectionPivots_v03_JQ
## 03.04.2019
## Original Code and Concept by Mobius:
# V01.08.2012 Projection Pivots
# mobius

# Notes:
# 03.04.2019 added linits on extensions
# 03.05.2019 adjusted limits on extensions by adding user input upper and lower extenion percent limits

#declare Once_Per_Bar;

## Inputs
input n = 21;
input showLines = yes;
input showValues = no;
input showBarNumbers = no;
input ExtensionLengthBars = 20; # added to control length of Entension
input UpperExtensionPercentLimit = 5;
input LowerExtensionPercentLimit = 5;
# input DisplayLabel = yes;  ##  JQ 7.8.2018 added
#     addlabel (DisplayLabel, "Projection Pivots n:" + n + " " , color.WHITE);  ##  JQ 7.8.2018 added

#     code from various sources including Mobius, NoLongerNube and others
# Comment out unnecessary portions to preserve tos memory and enhance speed

# Universal Definitions using Padawan variable naming convention (JQ) v03.04.2019
# iData Definitions
def vHigh = high;  # creates the variable vHigh.  Use of the variable reduce data calls to tos iData server
#    def initHigh =  CompoundValue(1, high, high);  # creates and initialized variable for High
def vLow = low;
#    def initLow = CompoundValue(1, low, low);
def vOpen = open;
#    def initOpen = CompoundValue(1, open, open);
def vClose = close;
#    def initClose = CompoundValue(1, close, close);
def vVolume = volume;
#    def initVolume = CompoundValue(1, volume, volume);
def nan = Double.NaN;
# Bar Time & Date
def bn = BarNumber();
def currentBar = HighestAll(if !IsNaN(vHigh) then bn else nan);
#    def Today = GetDay() ==GetLastDay();
#    def time = GetTime();
#    def GlobeX = GetTime() < RegularTradingStart(GetYYYYMMDD());
# def globeX_v2 = if time crosses below RegularTradingEnd(GetYYYYMMDD()) then bn else GlobeX;
#    def RTH = GetTime() > RegularTradingStart(GetYYYYMMDD());
#    def RTH_v2 = if time crosses above RegularTradingStart(GetYYYYMMDD()) then bn else RTH;

# bars that start and end the sessions  #(borrowed from nube)
#    def rthStartBar    = CompoundValue(1,
#                         if   !IsNaN(vClose)
#                         &&   time crosses above RegularTradingStart(GetYYYYMMDD())
#                         then bn
#                         else rthStartBar, 0);
#    def rthEndBar      = CompoundValue(1,
#                         if   !IsNaN(vClose)
#                         &&   time crosses above RegularTradingEnd(GetYYYYMMDD())
#                         then bn
#                         else rthEndBar, 1);
#    def globexStartBar = CompoundValue(1,
#                         if   !IsNaN(vClose)
#                         &&   time crosses below RegularTradingEnd(GetYYYYMMDD())
#                         then bn
#                         else globexStartBar, 1);
#    def rthSession = if   bn crosses above rthStartBar #+ barsExtendedBeyondSession
#                     then 1
#                     else if   bn crosses above rthEndBar #+ barsExtendedBeyondSession
#                          then 0
#                     else rthSession;

# Bubble Locations
def x_AxisLastExpansionBar = BarNumber() == HighestAll(BarNumber());  #corrected 11.12.2018 (JQ)
# syntax: addChartBubble(x_AxisLastExpansionBar, y-axis coordinate," text", Color.LIME); #verified 12.25.2018 (JQ)

def PH;
def PL;
def hh = fold i = 1 to n + 1
with p = 1
while p
do vHigh > getValue(vHigh, -i);
PH = if (bn > n and
vHigh == highest(vHigh, n) and
hh)
then vHigh
else double.NaN;
def ll = fold j = 1 to n + 1
with q = 1
while q
do vLow < getValue(low, -j);
PL = if (bn > n and
vLow == lowest(vLow, n) and
ll)
then vLow
else double.NaN;
def PHBar = if !isNaN(PH)
then bn
else PHBar;
def PLBar = if !isNaN(PL)
then bn
else PLBar;
def PHL = if !isNaN(PH)
then PH
else PHL;
def priorPHBar = if PHL != PHL
then PHBar
else priorPHBar;
def PLL = if !isNaN(PL)
then PL
else PLL;
def priorPLBar = if PLL != PLL
then PLBar
else priorPLBar;
def HighPivots = bn >= highestAll(priorPHBar);
def LowPivots = bn >= highestAll(priorPLBar);
def FirstRpoint = if HighPivots
then bn - PHBar
else 0;
def PriorRpoint = if HighPivots
then bn - PriorPHBar
else 0;
def RSlope = (getvalue(PH, FirstRpoint) - getvalue(PH, PriorRpoint))
/ (PHBar - PriorPHBar);
def FirstSpoint = if LowPivots
then bn - PLBar
else 0;
def PriorSpoint = if LowPivots
then bn - PriorPLBar
else 0;
def SSlope = (getvalue(PL, FirstSpoint) - getvalue(PL, PriorSpoint))
/ (PLBar - PriorPLBar);
def RExtend = if bn == highestall(PHBar)
then 1
else RExtend;
def SExtend = if bn == highestall(PLBar)
then 1
else SExtend;

def pivotHigh = if HighPivots
then PH
else double.NaN;

def pivotHighLine = if PHL > 0 and
HighPivots
then PHL
else double.NaN;

def pivotHighLineBound = if PHL > 0 and
HighPivots
then PHL * 0.98
else double.NaN;

def RLine = pivotHigh;

# Added code to limit resistance estension line (JQ 03.04.2019)
def calc_ResistanceExtension = if RExtend
then (bn - PHBar) * RSlope + PHL
else double.NaN;

# Low Plots
def pivotLow = if LowPivots
then PL
else double.NaN;

def PivotDot = if !isNaN(pivotHigh)
then pivotHigh
else if !isNaN(pivotLow)
then pivotLow
else double.NaN;

# End Code

def left_side_count = if !isNaN(pivotHigh) then 0 else if !isNan(left_side_count) then left_side_count + 1 else double.nan;
def right_side_count = if  !isNaN(pivotLow) then left_side_count else right_side_count - 1;

plot lsc = left_side_count;
plot rsc = right_side_count;
plot zero = 0;

def left_side_plot = if (!IsNaN(left_side_count)
# and left_side_count >= right_side_count
and right_side_count >= 0 )
then pivotHighLine else
if left_side_count == 0 then pivotHighLine else Double.NaN;

def left = if pivotHigh
# and isnan(pivotHigh)
then pivotHighLine
else if pivotLow then double.nan else left;

plot l = left;

def right_side_plot = if right_side_count > 0 then if !IsNaN(left_side_plot) then left_side_plot else right_side_plot else Double.NaN;

# plot ls = left_side_plot;
plot rs = right_side_plot;
addVerticalLine(!isNan(PivotLow) and !isNaN(left_side_plot), color = Color.BLUE, stroke = Curve.FIRM);
plot end_dot = if right_side_plot and isNaN(right_side_plot) then right_side_plot else double.nan;
end_Dot.SetDefaultColor(GetColor(7));
end_Dot.SetPaintingStrategy(PaintingStrategy.POINTS);
end_Dot.SetLineWeight(3);

def start_left_side = if pivotDot and pivotHigh and rsc != 0 and lsc == 0 then 1 else 0;

start_left_side
,high + 10,
"START_LEFT_SIDE!",
color.light_gray);

def end_left_side = pivotDot and pivotLow ;

# addchartbubble(pivotLow, low - 5, "LOW", color.light_gray);

# end_left_side
# ,high + 5,
# "END Left Side!",
# color.light_gray);

def left_side_line = if start_left_side != 0  then 50
# else if !isNaN(end_left_side) then double.nan
# else if !isNaN(left_side_line) then left_side_line
else 100;
def a =  if pivotDot then 100 else a;
plot new_left_side_plot =a;``````

There may be scale issues if it tries to plot some of the dots on the lower study. I turn things on and off all the time.

OK. So here's what I have:

The basic idea is to take Mobius' Pivot Points, count up bars since a high pivot, at the next low pivot start counting down from however many bars you counted up. Plot while those numbers are above zero. You can see in this image that it finds the pivots, and draws the right half of the T, but it does not display the left top bar of the T. I can't for the life of me get it to draw that part.
The lower indicator, called debug shows the bar counts since a high pivot, and the countdown for the right bar. This one has a bunch of additional labels as I was trying to sort out what was wonky with the left T bar. This image represents the indicator as it is uploaded here. There is no ToS link, as the indicator is not really functional yet.

Work in progress, uploaded for community help in finishing / polishing.

It is quite sensitive to the length parameter in the pivot point part of the indicator, though I'm not sure I understand quite why.

-mashume
UPPER
Code:
``````declare upper;

## Inputs
## ProjectionPivots_v03_JQ
## 03.04.2019
## Original Code and Concept by Mobius:
# V01.08.2012 Projection Pivots
# mobius

# Notes:
# 03.04.2019 added linits on extensions
# 03.05.2019 adjusted limits on extensions by adding user input upper and lower extenion percent limits

#declare Once_Per_Bar;

## Inputs
input n = 21;
input showLines = yes;
input showValues = no;
input showBarNumbers = no;
input ExtensionLengthBars = 20; # added to control length of Entension
input UpperExtensionPercentLimit = 5;
input LowerExtensionPercentLimit = 5;
# input DisplayLabel = yes;  ##  JQ 7.8.2018 added
#     addlabel (DisplayLabel, "Projection Pivots n:" + n + " " , color.WHITE);  ##  JQ 7.8.2018 added

#     code from various sources including Mobius, NoLongerNube and others
# Comment out unnecessary portions to preserve tos memory and enhance speed

# Universal Definitions using Padawan variable naming convention (JQ) v03.04.2019
# iData Definitions
def vHigh = high;  # creates the variable vHigh.  Use of the variable reduce data calls to tos iData server
#    def initHigh =  CompoundValue(1, high, high);  # creates and initialized variable for High
def vLow = low;
#    def initLow = CompoundValue(1, low, low);
def vOpen = open;
#    def initOpen = CompoundValue(1, open, open);
def vClose = close;
#    def initClose = CompoundValue(1, close, close);
def vVolume = volume;
#    def initVolume = CompoundValue(1, volume, volume);
def nan = Double.NaN;
# Bar Time & Date
def bn = BarNumber();
def currentBar = HighestAll(if !IsNaN(vHigh) then bn else nan);
#    def Today = GetDay() ==GetLastDay();
#    def time = GetTime();
#    def GlobeX = GetTime() < RegularTradingStart(GetYYYYMMDD());
# def globeX_v2 = if time crosses below RegularTradingEnd(GetYYYYMMDD()) then bn else GlobeX;
#    def RTH = GetTime() > RegularTradingStart(GetYYYYMMDD());
#    def RTH_v2 = if time crosses above RegularTradingStart(GetYYYYMMDD()) then bn else RTH;

# bars that start and end the sessions  #(borrowed from nube)
#    def rthStartBar    = CompoundValue(1,
#                         if   !IsNaN(vClose)
#                         &&   time crosses above RegularTradingStart(GetYYYYMMDD())
#                         then bn
#                         else rthStartBar, 0);
#    def rthEndBar      = CompoundValue(1,
#                         if   !IsNaN(vClose)
#                         &&   time crosses above RegularTradingEnd(GetYYYYMMDD())
#                         then bn
#                         else rthEndBar, 1);
#    def globexStartBar = CompoundValue(1,
#                         if   !IsNaN(vClose)
#                         &&   time crosses below RegularTradingEnd(GetYYYYMMDD())
#                         then bn
#                         else globexStartBar, 1);
#    def rthSession = if   bn crosses above rthStartBar #+ barsExtendedBeyondSession
#                     then 1
#                     else if   bn crosses above rthEndBar #+ barsExtendedBeyondSession
#                          then 0
#                     else rthSession;

# Bubble Locations
def x_AxisLastExpansionBar = BarNumber() == HighestAll(BarNumber());  #corrected 11.12.2018 (JQ)
# syntax: addChartBubble(x_AxisLastExpansionBar, y-axis coordinate," text", Color.LIME); #verified 12.25.2018 (JQ)

def PH;
def PL;
def hh = fold i = 1 to n + 1
with p = 1
while p
do vHigh > getValue(vHigh, -i);
PH = if (bn > n and
vHigh == highest(vHigh, n) and
hh)
then vHigh
else double.NaN;
def ll = fold j = 1 to n + 1
with q = 1
while q
do vLow < getValue(low, -j);
PL = if (bn > n and
vLow == lowest(vLow, n) and
ll)
then vLow
else double.NaN;
def PHBar = if !isNaN(PH)
then bn
else PHBar;
def PLBar = if !isNaN(PL)
then bn
else PLBar;
def PHL = if !isNaN(PH)
then PH
else PHL;
def priorPHBar = if PHL != PHL
then PHBar
else priorPHBar;
def PLL = if !isNaN(PL)
then PL
else PLL;
def priorPLBar = if PLL != PLL
then PLBar
else priorPLBar;
def HighPivots = bn >= highestAll(priorPHBar);
def LowPivots = bn >= highestAll(priorPLBar);
def FirstRpoint = if HighPivots
then bn - PHBar
else 0;
def PriorRpoint = if HighPivots
then bn - PriorPHBar
else 0;
def RSlope = (getvalue(PH, FirstRpoint) - getvalue(PH, PriorRpoint))
/ (PHBar - PriorPHBar);
def FirstSpoint = if LowPivots
then bn - PLBar
else 0;
def PriorSpoint = if LowPivots
then bn - PriorPLBar
else 0;
def SSlope = (getvalue(PL, FirstSpoint) - getvalue(PL, PriorSpoint))
/ (PLBar - PriorPLBar);
def RExtend = if bn == highestall(PHBar)
then 1
else RExtend;
def SExtend = if bn == highestall(PLBar)
then 1
else SExtend;

def pivotHigh = if HighPivots
then PH
else double.NaN;

def pivotHighLine = if PHL > 0 and
HighPivots
then PHL
else double.NaN;

def pivotHighLineBound = if PHL > 0 and
HighPivots
then PHL * 0.98
else double.NaN;

def RLine = pivotHigh;

# Added code to limit resistance estension line (JQ 03.04.2019)
def calc_ResistanceExtension = if RExtend
then (bn - PHBar) * RSlope + PHL
else double.NaN;

# Low Plots
def pivotLow = if LowPivots
then PL
else double.NaN;

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);

# End Code

def left_side_count = if !isNaN(pivotHigh) then 0 else if !isNan(left_side_count) then left_side_count + 1 else double.nan;
def right_side_count = if  !isNaN(pivotLow) then left_side_count else right_side_count - 1;

def lsc = left_side_count;
def rsc = right_side_count;
def zero = 0;

def left_side_plot = if !IsNaN(left_side_count)
# and left_side_count >= right_side_count
and right_side_count >= 0 then pivotHighLine else Double.NaN;
def right_side_plot = if right_side_count > 0 then if !IsNaN(left_side_plot) then left_side_plot else right_side_plot else Double.NaN;

plot ls = left_side_plot;
plot rs = right_side_plot;
addVerticalLine(!isNan(PivotLow) and !isNaN(left_side_plot), color = Color.BLUE, stroke = Curve.FIRM);
plot end_dot = if right_side_plot and isNaN(right_side_plot) then right_side_plot else double.nan;
end_Dot.SetDefaultColor(GetColor(7));
end_Dot.SetPaintingStrategy(PaintingStrategy.POINTS);
end_Dot.SetLineWeight(3);
addChartBubble(pivotDot, HIGH, text = "DOT ==> rsc: " + rsc + "  lsc: " + lsc, color.white);
addChartBubble(pivotHigh, high + 5, "HIGH", color.light_gray);

def start_left_side = pivotDot and pivotHigh and rsc != 0 and lsc == 0;

start_left_side
,high + 10,
"START_LEFT_SIDE!",
color.light_gray);

def end_left_side = pivotDot and pivotLow;

addchartbubble(pivotLow, low - 5, "LOW", color.light_gray);

end_left_side
,high + 5,
"END Left Side!",
color.light_gray);

def left_side_line = if start_left_side then high
else if end_left_side then double.nan
else left_side_line;
plot new_left_side_plot = left_side_line;``````

LOWER
Code:
``````declare upper;

## Inputs
## ProjectionPivots_v03_JQ
## 03.04.2019
## Original Code and Concept by Mobius:
# V01.08.2012 Projection Pivots
# mobius

# Notes:
# 03.04.2019 added linits on extensions
# 03.05.2019 adjusted limits on extensions by adding user input upper and lower extenion percent limits

#declare Once_Per_Bar;

## Inputs
input n = 21;
input showLines = yes;
input showValues = no;
input showBarNumbers = no;
input ExtensionLengthBars = 20; # added to control length of Entension
input UpperExtensionPercentLimit = 5;
input LowerExtensionPercentLimit = 5;
# input DisplayLabel = yes;  ##  JQ 7.8.2018 added
#     addlabel (DisplayLabel, "Projection Pivots n:" + n + " " , color.WHITE);  ##  JQ 7.8.2018 added

#     code from various sources including Mobius, NoLongerNube and others
# Comment out unnecessary portions to preserve tos memory and enhance speed

# Universal Definitions using Padawan variable naming convention (JQ) v03.04.2019
# iData Definitions
def vHigh = high;  # creates the variable vHigh.  Use of the variable reduce data calls to tos iData server
#    def initHigh =  CompoundValue(1, high, high);  # creates and initialized variable for High
def vLow = low;
#    def initLow = CompoundValue(1, low, low);
def vOpen = open;
#    def initOpen = CompoundValue(1, open, open);
def vClose = close;
#    def initClose = CompoundValue(1, close, close);
def vVolume = volume;
#    def initVolume = CompoundValue(1, volume, volume);
def nan = Double.NaN;
# Bar Time & Date
def bn = BarNumber();
def currentBar = HighestAll(if !IsNaN(vHigh) then bn else nan);
#    def Today = GetDay() ==GetLastDay();
#    def time = GetTime();
#    def GlobeX = GetTime() < RegularTradingStart(GetYYYYMMDD());
# def globeX_v2 = if time crosses below RegularTradingEnd(GetYYYYMMDD()) then bn else GlobeX;
#    def RTH = GetTime() > RegularTradingStart(GetYYYYMMDD());
#    def RTH_v2 = if time crosses above RegularTradingStart(GetYYYYMMDD()) then bn else RTH;

# bars that start and end the sessions  #(borrowed from nube)
#    def rthStartBar    = CompoundValue(1,
#                         if   !IsNaN(vClose)
#                         &&   time crosses above RegularTradingStart(GetYYYYMMDD())
#                         then bn
#                         else rthStartBar, 0);
#    def rthEndBar      = CompoundValue(1,
#                         if   !IsNaN(vClose)
#                         &&   time crosses above RegularTradingEnd(GetYYYYMMDD())
#                         then bn
#                         else rthEndBar, 1);
#    def globexStartBar = CompoundValue(1,
#                         if   !IsNaN(vClose)
#                         &&   time crosses below RegularTradingEnd(GetYYYYMMDD())
#                         then bn
#                         else globexStartBar, 1);
#    def rthSession = if   bn crosses above rthStartBar #+ barsExtendedBeyondSession
#                     then 1
#                     else if   bn crosses above rthEndBar #+ barsExtendedBeyondSession
#                          then 0
#                     else rthSession;

# Bubble Locations
def x_AxisLastExpansionBar = BarNumber() == HighestAll(BarNumber());  #corrected 11.12.2018 (JQ)
# syntax: addChartBubble(x_AxisLastExpansionBar, y-axis coordinate," text", Color.LIME); #verified 12.25.2018 (JQ)

def PH;
def PL;
def hh = fold i = 1 to n + 1
with p = 1
while p
do vHigh > getValue(vHigh, -i);
PH = if (bn > n and
vHigh == highest(vHigh, n) and
hh)
then vHigh
else double.NaN;
def ll = fold j = 1 to n + 1
with q = 1
while q
do vLow < getValue(low, -j);
PL = if (bn > n and
vLow == lowest(vLow, n) and
ll)
then vLow
else double.NaN;
def PHBar = if !isNaN(PH)
then bn
else PHBar;
def PLBar = if !isNaN(PL)
then bn
else PLBar;
def PHL = if !isNaN(PH)
then PH
else PHL;
def priorPHBar = if PHL != PHL
then PHBar
else priorPHBar;
def PLL = if !isNaN(PL)
then PL
else PLL;
def priorPLBar = if PLL != PLL
then PLBar
else priorPLBar;
def HighPivots = bn >= highestAll(priorPHBar);
def LowPivots = bn >= highestAll(priorPLBar);
def FirstRpoint = if HighPivots
then bn - PHBar
else 0;
def PriorRpoint = if HighPivots
then bn - PriorPHBar
else 0;
def RSlope = (getvalue(PH, FirstRpoint) - getvalue(PH, PriorRpoint))
/ (PHBar - PriorPHBar);
def FirstSpoint = if LowPivots
then bn - PLBar
else 0;
def PriorSpoint = if LowPivots
then bn - PriorPLBar
else 0;
def SSlope = (getvalue(PL, FirstSpoint) - getvalue(PL, PriorSpoint))
/ (PLBar - PriorPLBar);
def RExtend = if bn == highestall(PHBar)
then 1
else RExtend;
def SExtend = if bn == highestall(PLBar)
then 1
else SExtend;

def pivotHigh = if HighPivots
then PH
else double.NaN;

def pivotHighLine = if PHL > 0 and
HighPivots
then PHL
else double.NaN;

def pivotHighLineBound = if PHL > 0 and
HighPivots
then PHL * 0.98
else double.NaN;

def RLine = pivotHigh;

# Added code to limit resistance estension line (JQ 03.04.2019)
def calc_ResistanceExtension = if RExtend
then (bn - PHBar) * RSlope + PHL
else double.NaN;

# Low Plots
def pivotLow = if LowPivots
then PL
else double.NaN;

def PivotDot = if !isNaN(pivotHigh)
then pivotHigh
else if !isNaN(pivotLow)
then pivotLow
else double.NaN;

# End Code

def left_side_count = if !isNaN(pivotHigh) then 0 else if !isNan(left_side_count) then left_side_count + 1 else double.nan;
def right_side_count = if  !isNaN(pivotLow) then left_side_count else right_side_count - 1;

plot lsc = left_side_count;
plot rsc = right_side_count;
plot zero = 0;

def left_side_plot = if (!IsNaN(left_side_count)
# and left_side_count >= right_side_count
and right_side_count >= 0 )
then pivotHighLine else
if left_side_count == 0 then pivotHighLine else Double.NaN;

def left = if pivotHigh
# and isnan(pivotHigh)
then pivotHighLine
else if pivotLow then double.nan else left;

plot l = left;

def right_side_plot = if right_side_count > 0 then if !IsNaN(left_side_plot) then left_side_plot else right_side_plot else Double.NaN;

# plot ls = left_side_plot;
plot rs = right_side_plot;
addVerticalLine(!isNan(PivotLow) and !isNaN(left_side_plot), color = Color.BLUE, stroke = Curve.FIRM);
plot end_dot = if right_side_plot and isNaN(right_side_plot) then right_side_plot else double.nan;
end_Dot.SetDefaultColor(GetColor(7));
end_Dot.SetPaintingStrategy(PaintingStrategy.POINTS);
end_Dot.SetLineWeight(3);

def start_left_side = if pivotDot and pivotHigh and rsc != 0 and lsc == 0 then 1 else 0;

start_left_side
,high + 10,
"START_LEFT_SIDE!",
color.light_gray);

def end_left_side = pivotDot and pivotLow ;

# addchartbubble(pivotLow, low - 5, "LOW", color.light_gray);

# end_left_side
# ,high + 5,
# "END Left Side!",
# color.light_gray);

def left_side_line = if start_left_side != 0  then 50
# else if !isNaN(end_left_side) then double.nan
# else if !isNaN(left_side_line) then left_side_line
else 100;
def a =  if pivotDot then 100 else a;
plot new_left_side_plot =a;``````

There may be scale issues if it tries to plot some of the dots on the lower study. I turn things on and off all the time.
THANKS so much Mashume appreciate all of the hard work that has gone into this!

If anyone like @cos251 , @barbaros, @diazlaz, @AspaTrader, @markos and @horserider (no particular order, or anyone else I missed. ) If anyone wants to take a crack at this and help out Mashume with fine tuning and cleaning up this it would be greatly appreciated! Here is the link to the article describing this theory in case anyone has not read it.

https://mrtopstep.com/magic-t-theory/

Thanks and sorry to bother just figured I'd put it out there.
Joe

Last edited:

### Not the exact question you're looking for?

87k+ Posts
177 Online ## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

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?