How to recall a previous high or low at a crossover point

subharmonic

Member
I am trying to make a plot for higher crossovers or lower crossovers. So for example if I have a HMA and a displaced HMA cross up I want to return the low of that bar. Then I want to return the next low that it crosses above it, same with crossing below returning the high of that bar as a variable. I can't seem to make it work, I think I am missing a bar variable.

Code:
input Value = close;
input Length = 55;

def HMA = MovingAverage(AverageType.HULL, Value, length);
def HMADisplaced = MovingAverage(AverageType.HULL, Value, length)[1];

def cu = Crosses(HMA, HMADisplaced, CrossingDirection.ABOVE);
def cd = Crosses(HMA, HMADisplaced, CrossingDirection.BELOW);
def Sell = if cd then high else Double.NEGATIVE_INFINITY;
def Buy =  if cu then low else Double.POSITIVE_INFINITY;

plot uptrend = Sell > Sell[1] and Buy > Buy[1];
plot dntrend = Sell < Sell[1] and Buy < Buy[1];
 
Here goes. and value is verified by label. this plots and labels the respective price low and price high at the last MA crossing points.

I have no clue what you mean by " Then I want to return the next low that it crosses above it, same with crossing below returning the high of that bar as a variable."

Code:
input Value = close;
input Length = 55;

def HMA = MovingAverage(AverageType.HULL, Value, Length);
def HMADisplaced = MovingAverage(AverageType.HULL, Value, Length)[1];

def cu = Crosses(HMA, HMADisplaced, CrossingDirection.ABOVE);
def cd = Crosses(HMA, HMADisplaced, CrossingDirection.BELOW);
def Sell = if cd then high else Double.NEGATIVE_INFINITY;
def Buy =  if cu then low else Double.POSITIVE_INFINITY;

plot scan = cd;
plot scan2 = cu;

#MA crossing below returning the high of that bar
def high_of_bar = if cd then high else high_of_bar[1];
#MA cross up return the low of that bar.
def low_of_bar = if cu then low else low_of_bar[1];

AddLabel (yes, "High of Crosses Down" + high_of_bar);
AddLabel (yes, "Low of Cross Up" + low_of_bar);
 

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

@XeoNoX Thanks for your help.

So the general idea is I am trying to make a trend detector based on 2 consecutive higher highs and 2 higher lows or reverse for a downtrend LL, LH, LL, LH.

Your "def high_of_bar = if cd then high else high_of_bar[1];" gives me the value of the last HH or HL based on the HMA crossing itself, perfect. When I take those values and try to plot them it will not give me a plot....

"plot ut = if low_of_bar[1] > low_of_bar[2] and high_of_bar[1] > high_of_bar[2] then 1 else 0;
plot dt = if low_of_bar < low_of_bar[1] and high_of_bar < high_of_bar[1]then -1 else 0;"
 
dont understand what you are trying to do but here is code for what you defined:
2 consecutive higher highs and 2 higher lows
Code:
high  is greater than high from 1 bars ago and low is greater than low from 1 bars ago and high  from 1 bars ago is greater than high from 2 bars ago and low  from 1 bars ago is greater than low from 2 bars ago
 
@XeoNoX
Sorry for the late reply, been a little busy with winter arriving.

If you look at this I am looking to make an alert when the crossovers form a high, low, higher high, higher low or vise versa. So I am not looking for the previous bar but the previous cross. These highs and lows are a crossovers marked by the orange arrows in the pic. I labelled them as such H(high), L (low), HH (Higher High), HL (Higher Low), LH (Lower High) and LL( Lower Low).
cHm5BCg.png
 
you stated....
"the crossovers form a high, low, higher high, higher low "

your attempting to compare the value of the crossover highs and value of the crossover lows.... because you are comparing crossover value there is no high or low of the crossover at a specified bar, it will be a single number for each specified MA, therefore negating any "crossover high value "or "crossover low value". In other words there is no high and low crossover value at a previous bar to compare because it is a single digit number for each MA.

what you can do is compare and make sure the MA values that you are comparing are higher than the previous bar

#MA CROSS IS HIGHER FROM PREVIOUS BAR
plot MA_CROSS_Higher = (HMA and HMADisplaced is greater than HMA from 1 bars ago and HMADisplaced from 1 bars ago) and (HMA from 1 bars ago and HMADisplaced from 1 bars ago is greater than HMA from 2 bars ago and HMADisplaced from 2 bars ago);
 
Last edited:
Code:
#cycles highs and lows to determine the trend direction
declare upper;
input Value = ohlc4;
input Length = 10;

def HMA = MovingAverage(AverageType.HULL, Value, Length);
def HMADisplaced = MovingAverage(AverageType.HULL, Value, Length)[1];

def cu = Crosses(HMA, HMADisplaced, CrossingDirection.ABOVE);
def cd = Crosses(HMA, HMADisplaced, CrossingDirection.BELOW);
def Sell = if cd then high else Double.NEGATIVE_INFINITY;
def Buy =  if cu then low else Double.POSITIVE_INFINITY;

#MA crossing below returning the high of that bar
def high_of_bar = if cd then high else high_of_bar[1];
#MA cross up return the low of that bar.
def low_of_bar = if cu then low else low_of_bar[1];

AddLabel (yes, "High of Crosses Down" + high_of_bar);
AddLabel (yes, "Low of Cross Up" + low_of_bar);

def HH = if high_of_bar > high_of_bar[1] then 2 else 0;
def HL = if low_of_bar > low_of_bar[1] then -2 else 0;
def LH = if high_of_bar < high_of_bar[1] then 2 else 0;
def LL = if low_of_bar < low_of_bar[1] then -2 else 0;
plot NuT = if HL and high_of_bar >= HH then 1 else Double.NaN;
plot NdT = if LH and low_of_bar >= LL then 1 else Double.NaN;
NuT.SetPaintingStrategy(PaintingStrategy.Boolean_Arrow_Up);
NuT.SetLineWeight(5);
NuT.AssignValueColor(Color.blue);

NdT.SetPaintingStrategy(PaintingStrategy.Boolean_Arrow_Down);
NdT.SetLineWeight(5);
NdT.AssignValueColor(Color.blue);

http://tos.mx/Uk4Oagr
 
actually this is correct below, i was a bit off yeserday, i was a tad bit sleepy

plot MA_CROSS_Higher = (HMA is greater than HMA from 1 bars ago and HMADisplaced is greater than HMADisplaced from 1 bars ago) and (HMA from 1 bars ago is greater than HMA from 2 bars ago and HMADisplaced from 1 bars ago is greater than HMADisplaced from 2 bars ago);

#Appreciaction goes a long ways, remember to hit that like button if you found the info helpful!
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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