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

subharmonic

Member
VIP
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];
 

XeoNoX

Well-known member
VIP
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);
 

subharmonic

Member
VIP
@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;"
 

XeoNoX

Well-known member
VIP
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
 

subharmonic

Member
VIP
@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
 

XeoNoX

Well-known member
VIP
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:

subharmonic

Member
VIP
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
 

XeoNoX

Well-known member
VIP
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

Top