Apply shorter moving average for stocks lacking 200 days of data


Category:
0
0

Hello,

As part of a larger scan that I’m running in Thinkorswim, I screen for stocks above their 200-day moving average.  An issue that is occurring is the removal of stocks that meet all the other criteria but don’t have 200 days of trading data.  I’m trying to resolve the issue by using barnumber() in the script.  However it still reverts to using the 200 day.  Below is the code that I created.  Any help in fixing the issue would be greatly appreciated.

def MovingAvgcount=if barnumber() < 200 then average(close,50) else average(close,200);
def priceaboveavg=close>=MovingAvgcount;
plot scan=priceaboveavg;

Thanks

Marked as spam
Posted by (Questions: 1, Answers: 0)
Asked on July 2, 2020 7:56 pm
30 views
0
Private answer

I updated the title of the question because you are doing much more here than your original title implied.

I also decided to write my own solution because your code has that very annoying feature of lacking spaces between the operators and your variable names. Very annoying. In addition to that your capitalization scheme is completely backwards. I'm not trying to pick on you. We have a lot of newbies in this forum and I need to make sure everyone learns the proper way to write code. So I try to correct mistakes whenever I see them.

Here is some code that works for what you have requested:

def lastBarNumber = HighestAll(BarNumber());
def switchAverage = if lastBarNumber < 200 and lastBarNumber > 50 then Average(close, 50) else Average(close, 200);
plot scan = close > switchAverage;

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on July 2, 2020 8:42 pm