Capture peak or valleys when price crosses 200-SMA?

Mohammad

New member
VIP
@halcyonguy,

Could I make a request: Capture peak or valleys when a price crosses above or below a 200-SMA? I have seen several similar codes from you, such as box for an oscillator when ob/os and for fibs in addition to the new one you added to check if price is near a moving average; so, I think you are the right person. But, not being a developer, the indicators seem hard to modify for my needs.

Specifically:

1. After the prices crosses above and closes above, plot two Median Prices (MP) on the chart as Cyan Squares or Yellow Squares on the identified bars:
A: First Median Price: the valley (lowest) between this cross above and the prior cross below.
B: Second Median Price: the bar when it Crossed and closed Above the 200-SMA.

2. For example, Apple crossed above the 200SMA on Feb 2nd, 2023. Prior to it, it cross below from above on Aug 30th, 2022.
A: First Lowest MP: Of candle when AAPL crossed above on a Daily chart was: HL2 on Jan 5, 2023.
B: 2nd Median Price of the candle when it Crossed Above (and closed above 200SMA): HL2 of Feb 2nd, 2023

3. Condition: Use the first cross (Above or Below) of the 4 bars near each other (input variable) and ignore the other within in the 4 bars; for cases of multiple up and downs. (or if you recommend different number of bars to ignore the noise).

4. Detect if the median price crossed above or below the 200SMA

5. Colors: If Crossed Above from below, plot Cyan squares, for the two Median Price of the Bars (MA Cross Above and the Lowest MP). And, Yellow Squares for the opposite scenario: the two Median Prices, when the prices crosses Below from Above, of the median price of the highest bar/candle.

6. Time-frame: entire chart (I usually use a 2-3 year, Daily; but can reduce to 1-year if there are system performance constraint. but plots on the entire chart, rather than last ones are highly desirable)

Any help would be greatly appreciated!

I hope this is a complete description of what I am trying to plot. Please let me know if I can clarify anything.

AAPL Daily
 
Last edited:
@halcyonguy,

Could I make a request: Capture peak or valleys when a price crosses above or below a 200-SMA? I have seen several similar codes from you, such as box for an oscillator when ob/os and for fibs in addition to the new one you added to check if price is near a moving average; so, I think you are the right person. But, not being a developer, the indicators seem hard to modify for my needs.

Specifically:

1. After the prices crosses above and closes above, plot two Median Prices (MP) on the chart as Cyan Squares or Yellow Squares on the identified bars:
A: First Median Price: the valley (lowest) between this cross above and the prior cross below.
B: Second Median Price: the bar when it Crossed and closed Above the 200-SMA.

2. For example, Apple crossed above the 200SMA on Feb 2nd, 2023. Prior to it, it cross below from above on Aug 30th, 2022.
A: First Lowest MP: Of candle when AAPL crossed above on a Daily chart was: HL2 on Jan 5, 2023.
B: 2nd Median Price of the candle when it Crossed Above (and closed above 200SMA): HL2 of Feb 2nd, 2023

3. Condition: Use the first cross (Above or Below) of the 4 bars near each other (input variable) and ignore the other within in the 4 bars; for cases of multiple up and downs. (or if you recommend different number of bars to ignore the noise).

4. Detect if the median price crossed above or below the 200SMA

5. Colors: If Crossed Above from below, plot Cyan squares, for the two Median Price of the Bars (MA Cross Above and the Lowest MP). And, Yellow Squares for the opposite scenario: the two Median Prices, when the prices crosses Below from Above, of the median price of the highest bar/candle.

6. Time-frame: entire chart (I usually use a 2-3 year, Daily; but can reduce to 1-year if there are system performance constraint. but plots on the entire chart, rather than last ones are highly desirable)

Any help would be greatly appreciated!

I hope this is a complete description of what I am trying to plot. Please let me know if I can clarify anything.

hello,
make a request of a specific person? sure. although by doing do, may limit the answers you get. there are many good programmers here who may have a good answer.

---------

if i don't understand something, i ask a lot of questions. please don't take it wrong. i am just trying to get you to describe the problem in a different way.

------------

i'm sorry, i am confused by your words and have no idea what you are asking for.
i think you want to find valleys..??
between signal crossings?

there are several things that need clarifying.

in your step 1, it's like you are starting out in the middle of a story. mentioning things that haven't been described, so it doesn't make sense.

from your #'s
1. you don't say what is crossed. you mentioned sma200 earlier , but didn't mention in step 1.
what defines a median price (mp)?
when did a mp happen?
draw a square on the bar with a crossing? or on the bar with a mp?
should start out by describing what they are and when they happened.

1a ?? not sure. mp is a valley?
1b ?? not sure. mp is a bar at a crossing?
i have no idea what you are describing.

2b. is mp HL2?
that should have been mentioned in the first sentence.
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Fundamentals/hl2


i suggest you rewrite the rules, and if you make up terms, like median price, then describe them at the beginning.

I have used Valley in my studies so I think I know you mean, but it's still best to describe what it means to you.


you have an image with numbers 1, 2, 3.. but i don't know what they represent.
could have listed descriptions right above the picture.
1. is this..
2. is this
3. is this..


you used a shortcutcut link instead of a bbcode link for the image.
doing so places that logo for imgur.
https://usethinkscript.com/threads/how-to-insert-image-in-a-post-thread.277/page-3#post-98334
 
@halcyonguy,

Could I make a request: Capture peak or valleys when a price crosses above or below a 200-SMA? I have seen several similar codes from you, such as box for an oscillator when ob/os and for fibs in addition to the new one you added to check if price is near a moving average; so, I think you are the right person. But, not being a developer, the indicators seem hard to modify for my needs.

Specifically:

1. After the prices crosses above and closes above, plot two Median Prices (MP) on the chart as Cyan Squares or Yellow Squares on the identified bars:
A: First Median Price: the valley (lowest) between this cross above and the prior cross below.
B: Second Median Price: the bar when it Crossed and closed Above the 200-SMA.

2. For example, Apple crossed above the 200SMA on Feb 2nd, 2023. Prior to it, it cross below from above on Aug 30th, 2022.
A: First Lowest MP: Of candle when AAPL crossed above on a Daily chart was: HL2 on Jan 5, 2023.
B: 2nd Median Price of the candle when it Crossed Above (and closed above 200SMA): HL2 of Feb 2nd, 2023

3. Condition: Use the first cross (Above or Below) of the 4 bars near each other (input variable) and ignore the other within in the 4 bars; for cases of multiple up and downs. (or if you recommend different number of bars to ignore the noise).

4. Detect if the median price crossed above or below the 200SMA

5. Colors: If Crossed Above from below, plot Cyan squares, for the two Median Price of the Bars (MA Cross Above and the Lowest MP). And, Yellow Squares for the opposite scenario: the two Median Prices, when the prices crosses Below from Above, of the median price of the highest bar/candle.

6. Time-frame: entire chart (I usually use a 2-3 year, Daily; but can reduce to 1-year if there are system performance constraint. but plots on the entire chart, rather than last ones are highly desirable)

Any help would be greatly appreciated!

I hope this is a complete description of what I am trying to plot. Please let me know if I can clarify anything.

AAPL Daily


maybe this can be a starting point for what you want ?


Code:
# avg_hl2_crossings_peak_00

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

# last bar  ( most recent)
#def lastbar = !isnan(close[0]) and isnan(close[-1]);

def hl = hl2;
def price = close;

input avg1_len = 200;
#input ma1_type =  AverageType.EXPONENTIAL;
input avg1_type =  AverageType.simple;
def avg1 = MovingAverage(avg1_type, price, avg1_len);

input show_lines = yes;
plot zavg = if show_lines then avg1 else na;
zavg.setdefaultcolor(getcolor(1));
#zavg.setlineweight(1);
zavg.hidebubble();


input show_hl_line = yes;
plot zhl = if show_hl_line then hl else na;
zhl.setdefaultcolor(color.yellow);
zhl.hidebubble();


def xup = (hl crosses above avg1);
def xdwn = (hl crosses below avg1);

#plot zxup = if xup or xdwn then hl2 else na;
plot zxup = if xup or xdwn then avg1 else na;
zxup.SetPaintingStrategy(PaintingStrategy.points);
zxup.AssignValueColor(if xup then color.green else if xdwn then color.red else color.gray);
zxup.setlineweight(4);
zxup.hidebubble();



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

#https://usethinkscript.com/threads/zigzag-high-low-with-supply-demand-zones-for-thinkorswim.172/#post-7048
# original code, bar qty includes current bar
# define swing low points , robert payne
#
# modifified by halcyonguy to ignore last bar , and bar qty does not include current bar
#------------------------
#def bn = BarNumber();
#def na = double.nan;
input peak_valley_bars = 12;
def length = peak_valley_bars;
def lastbn = HighestAll(if IsNaN(close) then 0 else bn);
def offset = Min(length - 1, lastbn - bn);
input ignore_last_bar = yes;
def ignorelast = if (ignore_last_bar and bn == lastbn) then 0 else 1;

# original robert code, bar qty includes current bar
#def peak = ignorelast and high > highest(high[1], length - 1) and high == GetValue(highest(high, length), -offset);
#def valley = ignorelast and low < Lowest(low[1], length - 1) and low == GetValue(Lowest(low, length), -offset);

# chg so 2+ price levels in a row won't be skipped. trigger on the last bar in set, >= prev bars
def peak = ignorelast and high >= highest(high[1], length) and high > GetValue(highest(high, length), -(offset+1));
def valley = ignorelast and low <= Lowest(low[1], length) and low < GetValue(Lowest(low, length), -(offset+1));


input show_arrows = yes;
def vert = 0.001;
plot z1 = if show_arrows and peak then high*(1+vert) else na;
z1.SetPaintingStrategy(PaintingStrategy.arrow_down);
z1.SetDefaultColor(Color.cyan);
z1.setlineweight(2);
z1.hidebubble();

plot z2 = if show_arrows and valley then low*(1-vert) else na;
z2.SetPaintingStrategy(PaintingStrategy.arrow_up);
z2.SetDefaultColor(Color.cyan);
z2.setlineweight(2);
z2.hidebubble();
#


AAPL day chart
cyan line - SMA200
yellow line - HL2 prices
green and red squares - when HL2 crosses SMA200
cyan arrows - peaks and valleys defined by 12 bars
EBY1aFx.jpg
 
hello,
make a request of a specific person? sure. although by doing do, may limit the answers you get. there are many good programmers here who may have a good answer.

---------

if i don't understand something, i ask a lot of questions. please don't take it wrong. i am just trying to get you to describe the problem in a different way.

------------

i'm sorry, i am confused by your words and have no idea what you are asking for.
i think you want to find valleys..??
between signal crossings?

there are several things that need clarifying.

in your step 1, it's like you are starting out in the middle of a story. mentioning things that haven't been described, so it doesn't make sense.

from your #'s
1. you don't say what is crossed. you mentioned sma200 earlier , but didn't mention in step 1.
what defines a median price (mp)?
when did a mp happen?
draw a square on the bar with a crossing? or on the bar with a mp?
should start out by describing what they are and when they happened.

1a ?? not sure. mp is a valley?
1b ?? not sure. mp is a bar at a crossing?
i have no idea what you are describing.

2b. is mp HL2?
that should have been mentioned in the first sentence.
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Fundamentals/hl2


i suggest you rewrite the rules, and if you make up terms, like median price, then describe them at the beginning.

I have used Valley in my studies so I think I know you mean, but it's still best to describe what it means to you.


you have an image with numbers 1, 2, 3.. but i don't know what they represent.
could have listed descriptions right above the picture.
1. is this..
2. is this
3. is this..


you used a shortcutcut link instead of a bbcode link for the image.
doing so places that logo for imgur.
https://usethinkscript.com/threads/how-to-insert-image-in-a-post-thread.277/page-3#post-98334
Thank you @halcyonguy . I am trying out the code above you created and let you know by either answering the above questions or if I have any questions.

Also, no doubt we have many capable and generous leaders in addition to yourself. Forever indebted to you and everyone who openly shares great and open ideas.

maybe this can be a starting point for what you want ?


Code:
# avg_hl2_crossings_peak_00

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

# last bar  ( most recent)
#def lastbar = !isnan(close[0]) and isnan(close[-1]);

def hl = hl2;
def price = close;

input avg1_len = 200;
#input ma1_type =  AverageType.EXPONENTIAL;
input avg1_type =  AverageType.simple;
def avg1 = MovingAverage(avg1_type, price, avg1_len);

input show_lines = yes;
plot zavg = if show_lines then avg1 else na;
zavg.setdefaultcolor(getcolor(1));
#zavg.setlineweight(1);
zavg.hidebubble();


input show_hl_line = yes;
plot zhl = if show_hl_line then hl else na;
zhl.setdefaultcolor(color.yellow);
zhl.hidebubble();


def xup = (hl crosses above avg1);
def xdwn = (hl crosses below avg1);

#plot zxup = if xup or xdwn then hl2 else na;
plot zxup = if xup or xdwn then avg1 else na;
zxup.SetPaintingStrategy(PaintingStrategy.points);
zxup.AssignValueColor(if xup then color.green else if xdwn then color.red else color.gray);
zxup.setlineweight(4);
zxup.hidebubble();



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

#https://usethinkscript.com/threads/zigzag-high-low-with-supply-demand-zones-for-thinkorswim.172/#post-7048
# original code, bar qty includes current bar
# define swing low points , robert payne
#
# modifified by halcyonguy to ignore last bar , and bar qty does not include current bar
#------------------------
#def bn = BarNumber();
#def na = double.nan;
input peak_valley_bars = 12;
def length = peak_valley_bars;
def lastbn = HighestAll(if IsNaN(close) then 0 else bn);
def offset = Min(length - 1, lastbn - bn);
input ignore_last_bar = yes;
def ignorelast = if (ignore_last_bar and bn == lastbn) then 0 else 1;

# original robert code, bar qty includes current bar
#def peak = ignorelast and high > highest(high[1], length - 1) and high == GetValue(highest(high, length), -offset);
#def valley = ignorelast and low < Lowest(low[1], length - 1) and low == GetValue(Lowest(low, length), -offset);

# chg so 2+ price levels in a row won't be skipped. trigger on the last bar in set, >= prev bars
def peak = ignorelast and high >= highest(high[1], length) and high > GetValue(highest(high, length), -(offset+1));
def valley = ignorelast and low <= Lowest(low[1], length) and low < GetValue(Lowest(low, length), -(offset+1));


input show_arrows = yes;
def vert = 0.001;
plot z1 = if show_arrows and peak then high*(1+vert) else na;
z1.SetPaintingStrategy(PaintingStrategy.arrow_down);
z1.SetDefaultColor(Color.cyan);
z1.setlineweight(2);
z1.hidebubble();

plot z2 = if show_arrows and valley then low*(1-vert) else na;
z2.SetPaintingStrategy(PaintingStrategy.arrow_up);
z2.SetDefaultColor(Color.cyan);
z2.setlineweight(2);
z2.hidebubble();
#


AAPL day chart
cyan line - SMA200
yellow line - HL2 prices
green and red squares - when HL2 crosses SMA200
cyan arrows - peaks and valleys defined by 12 bars
EBY1aFx.jpg

Thanks again. I have been looking at it. It's very good and very close. Also, leveraging the zig zag study makes sense. Please allow me to provide better details and attach an image or two as well.
 

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

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
438 Online
Create Post

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