Scan 200 week sma from lower time frame


Category:
0
0

Due to the limitation of using secondary aggregation on scans, I want to calculate the 200 weekly MA in a different way:

I am trying to get 200 weekly MA with your suggested method.

def newWeek = GetWeek() <> GetWeek()[1];
def firstDayOfWeek = newWeek and !newWeek[1];

def closeLastWeek = if firstdayofweek then close else
closeLastWeek[1];

plot = average(closelastweek, 200);

But the result doesn’t equal the 200 weekly MA. Any idea to solve it?

Marked as spam
Posted by (Questions: 1, Answers: 1)
Asked on November 28, 2021 6:20 am
100 views
0
Private answer

I updated the title of your question so that it correctly describes the context of your request.

There is no solution to this request and I will take a few minutes to explain the issues and what is lacking in the Thinkorswim language to make something like this possible.

First off, this is not a "suggested method" that I have published for computing a moving average. That section of code you copied was for a completely different purpose. It was provided along with a solution for a chart strategy to buy on the first trading day of the week. I applaud your effort to try and make it work for this solution but there are issues that make this entirely impossible on Thinkorswim.

Specifically, Thinkorswim lacks the ability to create a custom sized array. A solution of this type requires that you create a custom sized array of 200 elements. In a real programming language we could do what you are trying to do. But Thinkorswim is a very basic scripting language.

The next hurdle that makes this impossible to do on Thinkorswim is actually more important. The historical data available to scans on Thinkorswim are limited based on the selected time frame.

Details here:

https://tlc.thinkorswim.com/center/howToTos/thinkManual/Scan/Stock-Hacker/studyfilters

You will find that you would need to use the daily time frame at the very minimum in order to have access to 200 weeks worth of historical data for your scan. You did not mention which time frame you were trying to apply this too. But daily time frame is the bare minimum.

I suggest you reconsider why you are trying to do this in the first place. If you want to find stocks that are trading above or below the 200 week moving average, just use a weekly time frame for your Study Filter. If you want to find stocks that have just crossed their 200 week moving average, just use the weekly time frame. There are very few reasons I can think of that would truly require building this scan on a lower time frame. But you did not include any of those details so I cannot offer any other suggestions to help you achieve your goals.

There is a very slim chance something like this can be created using the "fold" looping structure in Thinkorswim. But the mental gymnastics required to work out that code would be enough to drive a really good programmer stark raving mad.

 

Marked as spam
Posted by (Questions: 37, Answers: 4084)
Answered on November 28, 2021 9:47 am
0
Thanks for your reply. Let me shortly explain why I want to calculate the 200 weekly MA as described above. I have coded a scan that provides me a lower high on the daily timeframe and the candle when the lower high is confirmed should be below the 200 weekly MA. Therefore, I have to refer to secondary aggregation. I thought that it should be possible because the data limitation is six years on the weekly timeframe. But, it seems that according to your explanations, it is not possible.
( at November 28, 2021 8:58 pm)
0
Add a second Study Filter to your scan. Leave the original one set to daily time frame. Put the test for below the 200 sma on the second Study Filter and set that one to weekly time frame. If this is something you have never considered before I suggest you check out the MTF MACD Scan we have published on this site: https://www.hahn-tech.com/thinkorswim-mtf-macd-scan/ And once you have completed that, I suggest you watch our video titled "Thinkorswim Scans Beginner To Advanced" and "Thinkorswim Condition Wizard". Yes this will take some time. But the knowledge you gain from these videos will pay dividends you can't even imagine.
( at November 28, 2021 9:06 pm)
0
I considered that but that doesn’t work because on the study filter you can only choose close as the reference point for being below the SMA. But, my higher lowcandle has nothing to do with the current close candle. I made some progress: def lastweek = if getweek()getlastweek()-1 then 1 else 0; def rth = if secondsFromTime(0930) >= 0 and secondsTillTime(1600) >= 0 then 1 else 0; def closeLastWeek = if lastweek==1 and rth==1 then close else closeLastWeek[1]; This code gives me the correct closing price of last week. I am thinking to use fold function to iterate through 200 times closelastweek / length. Do you have an idea how to implement with fold function? I tried the iteration I = i +1 and calculating the total sum and it is cumulating. It shows me that there are a maximum of 275 weekly candles available. But, I am not able to calculate somehow the sum of the last 200 candles / length = 200 weekly MA
( at November 29, 2021 2:11 am)
0
I suggest you watch the videos I linked in my previous comment. You can reference open, high, low or close for each candle. What you stated is not correct. You really do need to take the time to learn how to use the tools available to you. I don't have any further assistance or suggestions. Everything you need to know go complete your stated goals was provided in my previous comment. 
( at November 29, 2021 7:28 am)
0
input MajorLeftStr = 13; input MajorRightStr = 13; input MinorLeftStr = 5; input MinorRightStr = 5; input SwingTickOffset = 2; input AlertsOn = yes; def offset = TickSize() * (HighestAll(high) – LowestAll(low)) * SwingTickOffset; #Calculate Major Swings def pivotH = if high > Highest(high[1], MajorLeftStr) and high > Highest(high[-MajorRightStr], MajorRightStr) then 1 else 0; def pValH = if pivotH then high + offset else Double.NaN; def pivotL = if low < Lowest(low[1], MajorLeftStr) and low plow[1] then 1 else if plow = 1 and plow > plow[1] then hl[1] + plowct[1] else hl[1] ; plot data = if hl!=0 then plow else 0; Now, if you pick this study and do a custom filter saying if data is less than Simple Moving Average 200 on weekly timeframe, it ignores the 200 weekly MA. It also returns higher lows that are above the 200 weekly MA.
( at November 29, 2021 6:14 pm)