close above moving average for 2 days.

JE $$

Member
VIP
Im trying to create a scan to identify stocks that have two sequential closes above a 10D SMA of the high
in other words, i want to find stocks who closed above a 10D SMA of the highs for two days in a row
I can get part of the way but have a hard time figuring out how to get the two days in a row part.
what i have so far is below - trying to accomplish the two days in a row part by altering the "within" parameter but it doesnt work well
can anyone help?
basically using this as a part of a moving average channel strategy(MAC) - two closes above the MAC = buy signal

close is greater than SimpleMovingAvg("price" = HIGH, "length" = 10)."SMA" and close is greater than SimpleMovingAvg("price" = HIGH, "length" = 10)."SMA" within 2 bars and close crosses above SimpleMovingAvg("price" = HIGH, "length" = 10)."SMA" within 3 bars
 

mashume

Well-known member
VIP
@rad14733's code will give you the most recent close against historical moving average points. This will give you the historical close against the historical sma points:
Code:
close is greater than SimpleMovingAvg("price" = HIGH, "length" = 10)."SMA" 
and close[1] is greater than SimpleMovingAvg("price" = HIGH, "length" = 10)."SMA"[1] 
and close[2] crosses above SimpleMovingAvg("price" = HIGH, "length" = 10)."SMA"[2]
All I added to the good code above is an index value on the close prices. Both are good code, but do different things. I didn't dig into your strategy to know which one you wanted, but thought I would put this into the thread just for good measure.

happy trading,
mashume
 

JE $$

Member
VIP
@JE $$ Try:

Ruby:
close is greater than SimpleMovingAvg("price" = HIGH, "length" = 10)."SMA"
and close is greater than SimpleMovingAvg("price" = HIGH, "length" = 10)."SMA"[1]
and close crosses above SimpleMovingAvg("price" = HIGH, "length" = 10)."SMA"[2]

Also check out https://tlc.thinkorswim.com/center/reference/thinkScript/Operators/Indexing for more information...
Thanks for looking into this. I really appreciate it. I used yourcode to come up with the following scan:

https://tos.mx/c4ajrOt

It seems to get some of what I'm looking for but also some aberrant results and I cant figure out why.

My strategy is that I have a moving average channel with the top line of the channel being the highs of the day, for 10 days.
So a 10D SMA of the highs.

I want to find stocks who 3 days ago were not above that level.
two days ago closed above that level
and then today closed above that level.

So essentially I get a stock who has closed above the 10D SMA of the highs for 2 days only.
The thought being that this indicates the start of a new uptrend (jake bernstein strategy)

Can you think of how to modify it to accomplish this? Also, I was wrong in my initial code where I put "close crosses" and it shouldhave been "close is greater than" for all of them.
 

JE $$

Member
VIP
@rad14733's code will give you the most recent close against historical moving average points. This will give you the historical close against the historical sma points:
Code:
close is greater than SimpleMovingAvg("price" = HIGH, "length" = 10)."SMA"
and close[1] is greater than SimpleMovingAvg("price" = HIGH, "length" = 10)."SMA"[1]
and close[2] crosses above SimpleMovingAvg("price" = HIGH, "length" = 10)."SMA"[2]
All I added to the good code above is an index value on the close prices. Both are good code, but do different things. I didn't dig into your strategy to know which one you wanted, but thought I would put this into the thread just for good measure.

happy trading,
mashume
Mashume, thanks for looking into this. I wasnt as clear as I should have been.
My strategy is that I have a moving average channel with the top line of the channel being the highs of the day, for 10 days.
So a 10D SMA of the highs.

I want to find stocks who 3 days ago were not above that level.
two days ago closed above that level
and then today closed above that level.

So essentially I get a stock who has closed above the 10D SMA of the highs for 2 days only.
The thought being that this indicates the start of a new uptrend (jake bernstein strategy)

Can you think of how to modify it to accomplish this? Also, I was wrong in my initial code where I put "close crosses" and it shouldhave been "close is greater than" for all of them.

I asked rad14733 too, so we can see his thoughts as well.
thanks for everyones time - you guys are awesome
 

JE $$

Member
VIP
I think this gets stocks who closed for 2 days above the 10D SMA of the highs:
sum(close > Average(High,10), 2) >= 2

But, how to make the condition that 3 days ago it was below the 10D SMA of the highs?
 

JE $$

Member
VIP
@rad14733's code will give you the most recent close against historical moving average points. This will give you the historical close against the historical sma points:
Code:
close is greater than SimpleMovingAvg("price" = HIGH, "length" = 10)."SMA"
and close[1] is greater than SimpleMovingAvg("price" = HIGH, "length" = 10)."SMA"[1]
and close[2] crosses above SimpleMovingAvg("price" = HIGH, "length" = 10)."SMA"[2]
All I added to the good code above is an index value on the close prices. Both are good code, but do different things. I didn't dig into your strategy to know which one you wanted, but thought I would put this into the thread just for good measure.

happy trading,
mashume
sorry- now i see the [] you put in
 

Similar threads

Top