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
19 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 daily 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://tlc.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: 39, Answers: 3734)
Answered on September 16, 2021 3:43 pm
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)
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)
Thank you Pete!
( at September 17, 2021 8:29 am)
Is it possible to turn this into a custom column to show the pct
( at September 19, 2021 12:14 pm)
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)
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)
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)
Fair enough. Thank you so much for your help & time!
( at September 19, 2021 4:18 pm)