EMA cross from (x) days ago


Category:
0
0

Hello, I know I can scan for EMA’s that crossed within 4 bars (here is the code:). What I need is to be able to scan for are stocks that crossed 4 days ago. Not 4 days ago and 3 days ago and 2 days ago. What I need is to be able to scan for are EMAs that crossed exactly 4 days ago. Thank you so much! 🙂

<pre>

MovAvgExponential(“length” = 10) crosses above MovAvgExponential(“length” = 25) within 4 bars

</pre>

Marked as spam
Posted by (Questions: 34, Answers: 56)
Asked on April 19, 2017 2:55 pm
1567 views
2
Private answer

Super easy. Especially once we leave behind the training wheels and work with the actual programing language of Thinkscript. Phrases like “crosses above” and “within x bars” are very handy for folks to learn the basics. But they lack the flexibility to perform more advanced operations. Not to mention that I have witnessed nonsensical results when using those training wheels.

We’ll be using the ExpAverage() built in function. You can read more here: http://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Tech-Analysis/ExpAverage.html

We are going to define two exponential moving averages and then define a true/false variable that captures the crossing event. Once we have the crossing event defined we simply scan for that event having occurred 4 bars in the past. By using the index [3]. Why use an index of 3? Because the current bar is always at index [0]. So we count backward four bars like this (0, 1, 2, 3). Ok, here is the code:

def ema1 = ExpAverage(close, 10);
def ema2 = ExpAverage(close, 25);
def emaCrossingAbove = ema1[1] < ema2[1] and ema1 > ema2;
plot scan = emaCrossingAbove[3];

Included below is a screenshot showing this code in action. I find it super handy to use the Study Alert screen to test out and visualize potential scans. It allows me to clearly see the result and I don’t have to clutter up my library by creating a custom study to test it out on a chart.

Don’t forget to up-vote any questions that best solve your questions!

Attachments:
Marked as spam
Posted by (Questions: 37, Answers: 4084)
Answered on April 19, 2017 3:54 pm
0

This EMA crossover scan was very helpful and very close to what I am looking for.  I am looking for the crossover scan Mike was referring to where EMA crossed with in 4 bars, not exactly 4 bars ago. Although I like having that available too.  I think I want the more simple version where it returns everything from day 4,3,2, &1, not just 4 days ago.

I am new to trying thinkscript but I would think it would be a modification of the last line.
plot scan = emaCrossingAbove [3];

Thanks!

( at July 28, 2018 3:57 pm)
0

No, what you would need for that is the original code posted by Mike D’Antonio.

( at July 28, 2018 4:33 pm)
0

I did not see the original code posted by Mr. Mike D’Antonio, there was no link in the original question by him, or at least one that is not still active even thought it looks like this
“(here is the code:).”

( at July 29, 2018 8:04 am)
0

This is his code. You probably missed it because it is only one line:
MovAvgExponential(“length” = 10) crosses above MovAvgExponential(“length” = 25) within 4 bars

( at July 29, 2018 8:19 am)
0

I searched Mike’s previous posts and found this past question. New Highs above EMA cross .  I am thinking it is the link he was referring to and I am going to give it a try. 

( at July 29, 2018 8:32 am)
0

So I do not think that was exactly the link Mike was referring to, but I was able to figure out how to combine the 2 so I could have an ema cross that happens exactly (X) days and ema1 stays greater than ema2. I just would like the same thing but instead of the ema cross being exactly on (X) days ago, looking for this to happen in the last X days, or in other words at day X and since.
Thanks!

( at July 29, 2018 9:00 am)
0

Thanks so much! I see that was pretty easy to use the custom condition wizard then just add the within X bars at the end in the editor. As you can tell, I am just beginning to explore the scanning capability in TOS beyond the Custom wizard. I’ve already picked up some basics by perusing your site and I see the efficiencies that can be created by using the thinkscript. Do yo have any recommendation on where to begin learning so I do not have to ask “stupid questions” as I like to understand how to build these myself.

( at July 29, 2018 9:38 am)
0

Most of my scan videos simply provide code and show how to run the scan. If you want to know how to build your own I don’t have any material specifically committed to showing how to build scans…. outside of this Q&A forum that is. The forum contains tons of examples. It should be difficult not to find them.

The only video in which I spend a considerable amount of time demonstrating the Condition Wizard is “Thinkorswim AutoTrade Almost”: https://www.hahn-tech.com/thinkorswim-autotrade-almost/

In this video I show how to use the condition wizard to build conditional orders. But the mechanics are identical across many of the tools in Thinkorswim. Including Study Alerts, Study Filters (scans) and even chart studies.

( at July 29, 2018 10:12 am)