daily MACD and Stoch Scan for last three days


Category:
0
0

I would like to use the scan from the ToS MACD and Stochastics example video for daily scans.  However, since I am not a professional trader there might be days when I can’t run the scan and therefore I would like to scan for the MACD/stoch trigger event happening anytime in the last X amount of days.  I am not sure if one (or any) of these two variations would work.

Option 1: plot data = ((SlowK[1]<SlowD[1]) and (SlowK>SlowD)) within 3 bars and (lowest (SlowK[1],3)<25)and (Diff[1]<Diff);
Option 2: plot data = ((SlowK[1]<SlowD[1]) and (SlowK>SlowD) and (lowest (SlowK[1],3)<25)and (Diff[1]<Diff)) within 3 bars;

Thanks,

Diego

Marked as spam
Posted by (Questions: 1, Answers: 1)
Asked on July 25, 2019 8:24 am
140 views
0
Private answer

Since this is a scan, I have moved this question out of the Chart Study topic and into the Stock Scanners topic.

True/False; Yes/No; 1/0

The signal for any scan are a true/false result. The stocks returned in the results list of the scan are for those signals that evaluate as true. This is the foundational concept for understand the rest of this solution. Make sure you understand this.

In Thinkorswim, true/false is actually expressed as yes/no. So if the signal is true, the platform sees that as a yes, and returns that stock in the list of results. Got that? Make sure you do because there is the next step in the solution.

Thinkorswim uses a numeric value for yes and no. Yes is equal to 1 and no is equal to zero. So, from the top. We have true/false, expressed as yes/no, which equals 1/0. This comes in very handy because we can perform math on the yes/no expressions.

Performing Math on the Yes/No Results

If you have five consecutive bars in which the signal is true (yes), then you can add these all together and you get a final value of 5. Got that? Stated another way: yes + yes + yes + yes + yes = 5.

Likewise we can take a true/false signal in Thinkorswim, then we can sum those values for the last 5 bars.

def test = 1 == 1;
def sumOfSignal = Sum(test, 5);

Whatever result we get from these two lines of code will tell us the exact number of true conditions that were present in the most recent 5 bars.

Hope you stayed with me for all that foundational knowledge because I am about to wrap things up and put a bow on it.

Putting it All Together

Similar to the Sum() function, we have one that checks for the highest in the most recent x number of bars:

Highest()

http://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Tech-Analysis/Highest.html

As explained in the link above, this function checks the most recent x number of bars and returns the highest value found. And remember we are searching for a value of yes in those most recent bars. This is why it's so handy that yes == 1 in Thinkorswim. So if we want to know if a condition has been true for any of the 5 most recent bars....

def upBar = close > open and close > close[1];
def findAnyUpBars = Highest(upBar, 5) > 0;

That first line checks each bar to see if the close is higher than the open and also checks if the close of the current bar is greater than the close of the previous bar. The second line applies the Highest() function to find the highest value of the upBar variable within the most recent 5 bars. The last step is to check if the value returned by the Highest() function is greater than zero. If it is, we now that upBar has been true for at least of the most recent 5 bars.

Summary

That is everything you need to know to configure a scan statement that will return a list of stocks for which your signal was true in any of the most recent bars. And you can simply change the value from 5 to something else, depending on how many bars back you want to check.

Marked as spam
Posted by (Questions: 37, Answers: 4084)
Answered on July 25, 2019 11:48 am
0
Pete, Thanks for that great explanation! I always forget about the "doing math with Boolean" concept. But what about the other tests that I am checking for like the value of SlowK and MACD? How would we know if they are evaluating the correct bar if the findAnyUpBars test is matching 1 or more out of a total of 5 bars?
( at July 25, 2019 2:42 pm)
0

What is your true/false signal in the code you presented? Option one states:

plot data = ((SlowK[1] < SlowD[1]) and (SlowK > SlowD)) within 3 bars and (lowest(SlowK[1], 3) < 25) and (Diff[1] < Diff);

But we don't need the "within 3 bars" so we take that out. And we also change the 'plot' to 'def' because this is now our true/false variable statement.

def data = ((SlowK[1] < SlowD[1]) and (SlowK > SlowD)) and (lowest(SlowK[1],3) < 25)and (Diff[1] < Diff);

Now that we have our true/false variable named 'data', we can insert that in place of the 'upBar' variable in my answer:

def findAnyUpBars = Highest(data, 5) > 0;

Actually, since we are working on a scan you can just do this instead:

plot scan = Highest(data, 5) > 0;

Make sense now?

( at July 25, 2019 5:17 pm)
0
Absolutely, thank you very much sir!
( at July 26, 2019 7:38 am)