scan for the (last) crossing in the last 20 bars

Max_k

New member
I tried to make a scan searching for the Moving average crossing in the last 78 bars but I end up searching for the longer than 20 bars no matter what coding I use .
Here is the code i used ;


input price = close;
input length1 = 55;
input length2 = 21;
def AverageType1 = averageType.SIMPLE;
def AverageType2 = averageType.HULL;
def avg1 = movingAverage(averageType1,price,length1);
def avg2 = movingAverage(averageType2,price,length2);
plot scan = (avg2 crosses above avg1 ) within 78 bars ;
 
Last edited:
Solution
sorry for my bad explaining above.
and thank you so much for your replay,

what i tried to do is building a lower indicator looking for "the most recent" crossing in the last x numbers of bar.

I've seen a code for you in another topic but that one is finding the crossing at “anytime" in the last X price candles link below:

( https://usethinkscript.com/threads/moving-average-cross-anytime-in-the-last-x-price-candles.12512/ )

i hope that I explained it clearly this time.

also i used the code you submitted above and didn't work, sorry.

thanks again, and your help means a lot to me.

no need for sorrys. i just state things as i see them.

don't say 'didn't work' it doesn't explain anything.


will try to...
I tried to make a scan searching for the Moving average crossing in the last 78 bars but I end up searching for the longer than 20 bars no matter what coding I use .
Here is the code i used ;


input price = close;
input length1 = 55;
input length2 = 21;
def AverageType1 = averageType.SIMPLE;
def AverageType2 = averageType.HULL;
def avg1 = movingAverage(averageType1,price,length1);
def avg2 = movingAverage(averageType2,price,length2);
plot scan = (avg2 crosses above avg1 ) within 78 bars ;

i have no idea what your words mean, 78 , 20 , ...?

it looks like your code should work as a lower study, to be true on the last bar.

here is another version to try ( i tested this as a lower, i didn't test it as a scan, i don't scan)


Code:
declare lower;

def na = Double.NaN;
def bn = BarNumber();
def lastbar = !isnan(close[0]) and isnan(close[-1]);
def price = close;
input length1 = 55;
input length2 = 21;
input bars = 78;
def AverageType1 = averageType.SIMPLE;
def AverageType2 = averageType.HULL;
def avg1 = movingAverage(averageType1,price,length1);
def avg2 = movingAverage(averageType2,price,length2);
def x = (avg2 crosses above avg1);

def x2 = if lastbar and sum(x, bars) > 0 then 1 else 0;
plot z = x2;

# test data , each crossing
#plot y = (x * 0.5);
#
 

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

sorry for my bad explaining above.
and thank you so much for your replay,

what i tried to do is building a lower indicator looking for "the most recent" crossing in the last x numbers of bar.

I've seen a code for you in another topic but that one is finding the crossing at “anytime" in the last X price candles link below:

( https://usethinkscript.com/threads/moving-average-cross-anytime-in-the-last-x-price-candles.12512/ )

i hope that I explained it clearly this time.

also i used the code you submitted above and didn't work, sorry.

thanks again, and your help means a lot to me.
 
Last edited:
sorry for my bad explaining above.
and thank you so much for your replay,

what i tried to do is building a lower indicator looking for "the most recent" crossing in the last x numbers of bar.

I've seen a code for you in another topic but that one is finding the crossing at “anytime" in the last X price candles link below:

( https://usethinkscript.com/threads/moving-average-cross-anytime-in-the-last-x-price-candles.12512/ )

i hope that I explained it clearly this time.

also i used the code you submitted above and didn't work, sorry.

thanks again, and your help means a lot to me.

no need for sorrys. i just state things as i see them.

don't say 'didn't work' it doesn't explain anything.


will try to make another version... typing on cell. .

Code:
declare lower;

def na = Double.NaN;
def bn = BarNumber();
def lastbar = !isnan(close[0]) and isnan(close[-1]);
def price = close;
input length1 = 55;
input length2 = 21;
input bars = 78;
def AverageType1 = averageType.SIMPLE;
def AverageType2 = averageType.HULL;
def avg1 = movingAverage(averageType1,price,length1);
def avg2 = movingAverage(averageType2,price,length2);
def x = (avg2 crosses above avg1);

def lastbn = highestall(if !isnan(close) then bn else 0);

def xbn = if x then bn else 0;
def x3 = if (highestall(xbn) == bn and bn >= (lastbn - bars) ) then 1 else 0;
plot z3 = x3;


#def x2 = if lastbar and sum(x, bars) > 0 then 1 else 0;
#plot z = x2;

# test data , each crossing
#plot y = (x * 0.5);
#
 
Solution
no need for sorrys. i just state things as i see them.

don't say 'didn't work' it doesn't explain anything.


will try to make another version... typing on cell. .

Code:
declare lower;

def na = Double.NaN;
def bn = BarNumber();
def lastbar = !isnan(close[0]) and isnan(close[-1]);
def price = close;
input length1 = 55;
input length2 = 21;
input bars = 78;
def AverageType1 = averageType.SIMPLE;
def AverageType2 = averageType.HULL;
def avg1 = movingAverage(averageType1,price,length1);
def avg2 = movingAverage(averageType2,price,length2);
def x = (avg2 crosses above avg1);

def lastbn = highestall(if !isnan(close) then bn else 0);

def xbn = if x then bn else 0;
def x3 = if (highestall(xbn) == bn and bn >= (lastbn - bars) ) then 1 else 0;
plot z3 = x3;


#def x2 = if lastbar and sum(x, bars) > 0 then 1 else 0;
#plot z = x2;

# test data , each crossing
#plot y = (x * 0.5);
#
Thank you very much it worked perfectly.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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