Pivot High Between Two Signals

markbeach

New member
Is there a way to find a pivot between two signals? I have tried to use the fold function and can't seem to get it to work. The image below was taken from the SPY today (11/16/2022). Below is the code I wrote to generate the signals.

6IpzkMC.jpg


Code:
# Trend Pivot using EMA
# Created by Mark Beach - 11/2022

input ShortEmaLength = 10;
input LongEmaLength = 50;

def price_h = high;
plot ema_long = ExpAverage(price_h, ShortEmaLength);
ema_long.setDefaultColor(Color.Cyan);

plot ema_short = ExpAverage(price_h, LongEmaLength);
ema_short.setDefaultColor(Color.Yellow);

def above_trend_start = if ema_short[1] > ema_long[1] and ema_short[0] < ema_long[0] then 1 else Double.NaN;
def above_trend_end = if ema_short[1] < ema_long[1] and ema_short[0] > ema_long[0] then 1 else Double.NaN;
def above_trend;

if above_trend_start {
    above_trend = 1;
} else if above_trend_end {
    above_trend = Double.NaN;
} else {
    above_trend = above_trend[1];
}

AddChartBubble(yes, price_h,
   "AT: " + above_trend +
   "\nATS: " + above_trend_start +
   "\nATE: " + above_trend_end +
   "\nH: " + price_h
, Color.WHITE, yes);
 
Solution
Is there a way to find a pivot between two signals? I have tried to use the fold function and can't seem to get it to work. The image below was taken from the SPY today (11/16/2022). Below is the code I wrote to generate the signals.


this will look at the bars between EMA crossings,
and find the highest high and lowest low.
draws a green or red dot on the bar.

can draw vertical lines when the ema's cross

it runs a loop when the EMAs cross. the loop reads prices from future bars,
the loops use WHILE to keep looping, while the same ema is above the other one.


Code:
# pivot_between_signals_02

# https://usethinkscript.com/threads/pivot-high-between-two-signals.13393/
# Pivot High Between Two Signals
# markbeach   11/16...
Is there a way to find a pivot between two signals? I have tried to use the fold function and can't seem to get it to work. The image below was taken from the SPY today (11/16/2022). Below is the code I wrote to generate the signals.


this will look at the bars between EMA crossings,
and find the highest high and lowest low.
draws a green or red dot on the bar.

can draw vertical lines when the ema's cross

it runs a loop when the EMAs cross. the loop reads prices from future bars,
the loops use WHILE to keep looping, while the same ema is above the other one.


Code:
# pivot_between_signals_02

# https://usethinkscript.com/threads/pivot-high-between-two-signals.13393/
# Pivot High Between Two Signals
# markbeach   11/16

#Is there a way to find a pivot between two signals? I have tried to use the fold function and can't seem to get it to work.
#The image below was taken from the SPY today (11/16/2022). Below is the code I wrote to generate the signals.


def na = double.nan;
def bn = BarNumber();

input ShortEmaLength = 10;
input LongEmaLength = 50;
#input price_h = high;
input price = close;

#def ema_short = ExpAverage(price_h, LongEmaLength);
#def ema_long = ExpAverage(price_h, ShortEmaLength);

input avg_type =  AverageType.EXPONENTIAL;
def ma_short = MovingAverage(avg_type, price, ShortEmaLength);
def ma_long = MovingAverage(avg_type, price, LongEmaLength);

input show_ema_lines = yes;
plot zemashort = if show_ema_lines then ma_short else na;
zemashort.SetDefaultColor(Color.YELLOW);
plot zemalong = if  show_ema_lines then ma_long else na;
zemalong.SetDefaultColor(Color.CYAN);

def xup = (ma_short crosses above ma_long);
def xdwn = (ma_short crosses below ma_long);


def trend1;
if xup then {
   trend1 = 1;
} else if xdwn then {
   trend1 = -1;
} else {
   trend1 = trend1[1];
}

# look at future bars, between 2 ema crossings,
#  find highest peak , and lowest valley

#  a number that should be big enough to span the bars between ema crossings
def xlen = 500;

# a big number, the starting point, when finding a lowest # (can't start at 0)
def big = 99999;

def pivhi;
def pivlo;
if bn == 1 then {
    pivhi = 0;
    pivlo = 0;

} else if xup then {
  pivhi = fold i = 0 to xlen
    with p
    while getvalue(ma_short, -i) > getvalue(ma_long, -i) and getvalue(!isnan(close), -i)
#    do (if getvalue(price, -i) > p then getvalue(price, -i) else p);
    do (if getvalue(high, -i) > p then getvalue(high, -i) else p);

  pivlo = fold j = 0 to xlen
    with q = big
    while getvalue(ma_short, -j) > getvalue(ma_long, -j) and getvalue(!isnan(close), -j)
#    do (if getvalue(price, -j) < q then getvalue(price, -j) else q);
    do (if getvalue(low, -j) < q then getvalue(low, -j) else q);

} else if xdwn then {
  pivhi = fold k = 0 to xlen
    with r
    while getvalue(ma_short, -k) < getvalue(ma_long, -k) and getvalue(!isnan(close), -k)
#    do (if getvalue(price, -k) > r then getvalue(price, -k) else r);
    do (if getvalue(high, -k) > r then getvalue(high, -k) else r);

  pivlo = fold l = 0 to xlen
    with s = big
    while getvalue(ma_short, -l) < getvalue(ma_long, -l) and getvalue(!isnan(close), -l)
#    do (if getvalue(price, -l) < s then getvalue(price, -l) else s);
    do (if getvalue(low, -l) < s then getvalue(low, -l) else s);

} else {
    pivhi = pivhi[1];
    pivlo = pivlo[1];
}

# true when a peak or valley is found
def hihi = (pivhi == high);
def lolo = (pivlo == low);

input high_low_points = yes;
def vert = 0.002;

plot hi_dot = if high_low_points and hihi then (high*(1+vert)) else na;
hi_dot.SetPaintingStrategy(PaintingStrategy.points);
#hi_dot.SetDefaultColor(Color.cyan);
hi_dot.SetDefaultColor(Color.green);
hi_dot.SetLineWeight(5);

plot lo_dot = if high_low_points and lolo then (low*(1-vert)) else na;
lo_dot.SetPaintingStrategy(PaintingStrategy.points);
#lo_dot.SetDefaultColor(Color.yellow);
lo_dot.SetDefaultColor(Color.red);
lo_dot.SetLineWeight(5);

input vert_lines_on_ema_crossings = no;
addverticalline(vert_lines_on_ema_crossings and xup, "-", color.green);
addverticalline(vert_lines_on_ema_crossings and xdwn, "-", color.red);


#--------------------------

input test1 = no;
addchartbubble(test1, low*0.990,
xup + "\n" +
pivhi + "\n" +
hihi + "\n\n" +
xdwn + "\n" +
pivlo + "\n" +
lolo
, (if xup then color.cyan else if xdwn then color.yellow else
if hihi then color.green else if lolo then color.red else color.gray), no);
#

vertical lines turned on, at ema crossings
green dot above bar with highest high between crossings
red dot below bar with lowest low between crossings
ZVXi0j0.jpg
 
Solution
E
this will look at the bars between EMA crossings,
and find the highest high and lowest low.
draws a green or red dot on the bar.

can draw vertical lines when the ema's cross

it runs a loop when the EMAs cross. the loop reads prices from future bars,
the loops use WHILE to keep looping, while the same ema is above the other one.


Code:
# pivot_between_signals_02

# https://usethinkscript.com/threads/pivot-high-between-two-signals.13393/
# Pivot High Between Two Signals
# markbeach   11/16

#Is there a way to find a pivot between two signals? I have tried to use the fold function and can't seem to get it to work.
#The image below was taken from the SPY today (11/16/2022). Below is the code I wrote to generate the signals.


def na = double.nan;
def bn = BarNumber();

input ShortEmaLength = 10;
input LongEmaLength = 50;
#input price_h = high;
input price = close;

#def ema_short = ExpAverage(price_h, LongEmaLength);
#def ema_long = ExpAverage(price_h, ShortEmaLength);

input avg_type =  AverageType.EXPONENTIAL;
def ma_short = MovingAverage(avg_type, price, ShortEmaLength);
def ma_long = MovingAverage(avg_type, price, LongEmaLength);

input show_ema_lines = yes;
plot zemashort = if show_ema_lines then ma_short else na;
zemashort.SetDefaultColor(Color.YELLOW);
plot zemalong = if  show_ema_lines then ma_long else na;
zemalong.SetDefaultColor(Color.CYAN);

def xup = (ma_short crosses above ma_long);
def xdwn = (ma_short crosses below ma_long);


def trend1;
if xup then {
   trend1 = 1;
} else if xdwn then {
   trend1 = -1;
} else {
   trend1 = trend1[1];
}

# look at future bars, between 2 ema crossings,
#  find highest peak , and lowest valley

#  a number that should be big enough to span the bars between ema crossings
def xlen = 500;

# a big number, the starting point, when finding a lowest # (can't start at 0)
def big = 99999;

def pivhi;
def pivlo;
if bn == 1 then {
    pivhi = 0;
    pivlo = 0;

} else if xup then {
  pivhi = fold i = 0 to xlen
    with p
    while getvalue(ma_short, -i) > getvalue(ma_long, -i) and getvalue(!isnan(close), -i)
#    do (if getvalue(price, -i) > p then getvalue(price, -i) else p);
    do (if getvalue(high, -i) > p then getvalue(high, -i) else p);

  pivlo = fold j = 0 to xlen
    with q = big
    while getvalue(ma_short, -j) > getvalue(ma_long, -j) and getvalue(!isnan(close), -j)
#    do (if getvalue(price, -j) < q then getvalue(price, -j) else q);
    do (if getvalue(low, -j) < q then getvalue(low, -j) else q);

} else if xdwn then {
  pivhi = fold k = 0 to xlen
    with r
    while getvalue(ma_short, -k) < getvalue(ma_long, -k) and getvalue(!isnan(close), -k)
#    do (if getvalue(price, -k) > r then getvalue(price, -k) else r);
    do (if getvalue(high, -k) > r then getvalue(high, -k) else r);

  pivlo = fold l = 0 to xlen
    with s = big
    while getvalue(ma_short, -l) < getvalue(ma_long, -l) and getvalue(!isnan(close), -l)
#    do (if getvalue(price, -l) < s then getvalue(price, -l) else s);
    do (if getvalue(low, -l) < s then getvalue(low, -l) else s);

} else {
    pivhi = pivhi[1];
    pivlo = pivlo[1];
}

# true when a peak or valley is found
def hihi = (pivhi == high);
def lolo = (pivlo == low);

input high_low_points = yes;
def vert = 0.002;

plot hi_dot = if high_low_points and hihi then (high*(1+vert)) else na;
hi_dot.SetPaintingStrategy(PaintingStrategy.points);
#hi_dot.SetDefaultColor(Color.cyan);
hi_dot.SetDefaultColor(Color.green);
hi_dot.SetLineWeight(5);

plot lo_dot = if high_low_points and lolo then (low*(1-vert)) else na;
lo_dot.SetPaintingStrategy(PaintingStrategy.points);
#lo_dot.SetDefaultColor(Color.yellow);
lo_dot.SetDefaultColor(Color.red);
lo_dot.SetLineWeight(5);

input vert_lines_on_ema_crossings = no;
addverticalline(vert_lines_on_ema_crossings and xup, "-", color.green);
addverticalline(vert_lines_on_ema_crossings and xdwn, "-", color.red);


#--------------------------

input test1 = no;
addchartbubble(test1, low*0.990,
xup + "\n" +
pivhi + "\n" +
hihi + "\n\n" +
xdwn + "\n" +
pivlo + "\n" +
lolo
, (if xup then color.cyan else if xdwn then color.yellow else
if hihi then color.green else if lolo then color.red else color.gray), no);
#

vertical lines turned on, at ema crossings
green dot above bar with highest high between crossings
red dot below bar with lowest low between cros
 
it works well. is it possible to update the code to identify 2 Max Highs and 2 max Lows with in that cross over period and a trend line joining those 2 points (High with High and Low with Low)?
 

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
484 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