January closing higher at least 75 percent of time


Category:
0
0

Hi Pete,

I’m trying to make a scan using a daily chart to find stocks going back historically, as long as possible.
I’m looking for stocks that are up 75% of the time every January over numerous years.

Is something like that possible?  I’m fiddling with the following code to turn it into a scan but no luck so far. I’d appreciate any guidance you have to offer. Thank you.

# CODE START
# Seasonal Study for January
def jan = getMonth() == 1;
def janOpen = if jan and !jan[1] then open else janOpen[1];
def janClose = if jan and !jan[-1] then close else janClose[1];
def janDiff = if jan and !jan[-1] then (janClose-janOpen)/janOpen else 0;
def janUp = TotalSum(if jan and !jan[-1] and janDiff > 0 then 1 else 0);
def janDown = TotalSum(if jan and !jan[-1] and janDiff <= 0 then 1 else 0);
def janTotalCount = TotalSum(jan and !jan[-1]);
def janAvgDiff = TotalSum(janDiff)/janTotalCount;

AddLabel(yes, “Jan: ” + AsPercent(janUp/janTotalCount) + ” | ” + AsPercent(janAvgDiff), if(janUp/janTotalCount) > 0.5 and janAvgDiff > 0 then color.BLUE else color.DARK_ORANGE);

# CODE END

RESOLVED
Marked as spam
Posted by (Questions: 3, Answers: 1)
Asked on September 16, 2021 1:36 pm
107 views
0
Private answer

When using a daily time frame for a scans you can only look back 2 years. You will have to move to the Monthly time frame to go back further. But even the monthly time frame is limited to only 20 years. And using the monthly time frame makes this code much easier to write.

If you are looking for details about these data limitations for scans you can find them here, at the bottom of the following wegpage:

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

I updated the title of your question to help it show up in the search results for any viewers that just might be looking for this. It certainly is well off the path most traders are thinking.

The following code will only work as you requested if the scan is set to Monthly time frame:

input threshold = 75.0;
input monthNumber = 1;
def targetMonth = GetMonth() == monthNumber;
def upBar = close > open;
def markUpMonths = if targetMonth then upBar else 0;
rec countTargetMonths = if targetMonth then countTargetMonths[1] + 1 else countTargetMonths[1];
rec countUpMonths = if markUpMonths then countUpMonths[1] + 1 else countUpMonths[1];
def keyMetric = 100 * (countUpMonths / countTargetMonths);
plot scan = keyMetric > threshold;

I have included user inputs to allow viewers to adjust the percent threshold and the month number without modifying the code.

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on September 16, 2021 3:43 pm
0
Hi Pete, thank you very much. I used your code and set the scan to monthly. I am getting results, but some are lower than 75% and a few are actually down for the month (negative) according to the chart label/badge from my code. I have more confidence in your scan code. Do you see any mistakes in my code. Thanks again. # CODE START # Seasonal Study for January def jan = getMonth() == 1; def janOpen = if jan and !jan[1] then open else janOpen[1]; def janClose = if jan and !jan[-1] then close else janClose[1]; def janDiff = if jan and !jan[-1] then (janClose-janOpen)/janOpen else 0; def janUp = TotalSum(if jan and !jan[-1] and janDiff > 0 then 1 else 0); def janDown = TotalSum(if jan and !jan[-1] and janDiff 0.5 and janAvgDiff > 0 then color.BLUE else color.DARK_ORANGE); # CODE END
( at September 17, 2021 7:12 am)
0
I barely had enough time to create my solution from scratch. Believe it or not, it is much faster to create solutions from scratch than trying to fix someone else's code. I not have any time left to work on this solution so I will not be able to troubleshoot your code and provide feedback regarding its accuracy or efficacy. When I created my solution I confirmed the key metric is being computed correctly. Meaning that if 4 years are counted, and 3 out of 4 years January closed above it's open, the key metric results in 75%.
( at September 17, 2021 7:30 am)
0
Thank you Pete!
( at September 17, 2021 8:29 am)
0
Is it possible to turn this into a custom column to show the pct
( at September 19, 2021 12:14 pm)
0
Remove the last line and change the second to last line to the following: plot keyMetric = 100 * (countUpMonths / countTargetMonths); Set the time frame of the custom watchlist column to Monthly time frame.
( at September 19, 2021 12:59 pm)
0
Thanks again Pete! Lastly, is there a way to add a lookback threshold? Example, over the previous X number of years while using the Month timeframe
( at September 19, 2021 2:01 pm)
0
That sort of functionality would have needed to be included from the start as the entire structure of the code would need to be modified to include that specification. And if you had requested that from the start it would have made your request too complex for me to provide a free solution in the Q&A Forum.
( at September 19, 2021 2:53 pm)
0
Fair enough. Thank you so much for your help & time!
( at September 19, 2021 4:18 pm)
0
Hi Pete, I've noticed the scan includes stocks who have been around for only a couple of years or so. Is there a way to add a length condition?
( at June 30, 2023 11:57 am)
0
The first thing you need to know is stated in the first two sentences of my solution: "When using a daily time frame for a scans you can only look back 2 years. You will have to move to the Monthly time frame to go back further." So you can add a second study filter to your scan, set to either a weekly (6 years max) or monthly (20 years max) time frame. Then you would need to add a line of code to that study filter to check if there is any chart data available beyond whatever span of time you use for a limit. For example there are 156 weekly bars in a 3 year period. So check if the bar number of the last bar on the chart is greater than 156 on the weekly time frame. plot scan = BarNumber() >= 156;
( at June 30, 2023 12:44 pm)
0
What did you mean by adding a second study filter to the scan?
( at July 1, 2023 10:22 pm)
0

When building a custom scan for Thinkorswim, you get to add filters. And those filters are grouped by type. When you click the "Add filter" button you first get to select the "type". Which are listed as follows: Stock, Option, Fundamental, Study and Pattern.

You can add as many of each filter type to your scan as you want. (perhaps there is a limit but most folks will never exceed that). So you just add two Study Filters to the scan. One to run the solution I provided in my answer. And another to run the solution I described in my comment.

Since you are asking this question, I strongly recommend you take the time to learn how to get the most from the scan tool on Thinkorswim. Which you will accomplish by viewing the following video:

https://www.hahn-tech.com/thinkorswim-scans-beginner-to-advanced/

You may also find the following article helps you understand how to create scans based on multiple time frames:

https://www.hahn-tech.com/thinkorswim-mtf-macd-scan/
( at July 2, 2023 9:12 am)
0
I finally figured out what you're talking about. I knew about adding separate studies, but always thought you could only have one scan at a time. Had no idea that if you can have multiples if you put it in its own study. Thanks Pete!
( at July 2, 2023 7:24 pm)