Monthly Average Labels Problem

bp805

New member
Hi All,

I am creating a set of labels to display on a monthly chart to show the historical average return by month, and the % of time that that month is a positive returning month (trying to mirror the seasonality chart available on stockcharts.com). I have the labels coded up correctly (I manually calculated the values that the labels should display, and they are working right), and January/February/March display the correct return. However, once I begin to add April and May to the code, the percentages on the labels all of a sudden change to incorrect percentages and I cannot figure out why this is. Does anyone know why this might be the case? See screenshot examples below.

I am using the chart of Visa (V) displayed on a 10 year Monthly chart as my reference point while I build this out.

These are the correct percentages:
24WGHCN.jpg


Once I begin to add additional months to the code, the percentages change to this:
JrVrKJH.jpg


This is the thinkscript code I am using. All I am doing for each new month is changing the GetMonth() == # to the appropriate month #, and changing the three letter month prefix (Jan, Feb, March, etc) throughout the code. I have tried switching back and fourth from a 5 and 20 year monthly and back to the 10 year to see if the problem is "refreshed" and fixed so to speak, but that doesn't do it. Can't seem to figure out why this is happening. The % Positive labels on a 5 and 10 year monthly chart should display in whole 10% increments, but you'll notice in the second image this is not the case once I add more months the code, and the "January Average" changes from .2% to .11%. Any thoughts or help would be appreciated. Thanks!

Code:
#January
def JanCount = if GetMonth() == 1 then JanCount[1] + 1 else JanCount[1];
def JanChg = if GetMonth() == 1 then JanChg[1] + ((close - open) / open) else JanChg[1];

def JanPOSCount = if GetMonth() == 1 AND Close > Open then JanPOSCount[1] + 1 else JanPosCount[1];

def JanPOSChg = if GetMonth() == 1 AND Close > Open then JanPOSChg[1] + ((close-open) / open) else JanPOSChg[1];

AddLabel(1, "January Average: " + AsPercent(JanChg / JanCount),  Color.CYAN);

addLabel(1, "% Positive: " + AsPercent(JanPosCount/JanCount), Color.CYAN);
 

Slippage

Active member
I modified your label to include the count:

Ruby:
def JanCount = if GetMonth() == 1 then JanCount[1] + 1 else JanCount[1];
def JanChg = if GetMonth() == 1 then JanChg[1] + ((close - open) / open) else JanChg[1];
def JanPOSCount = if GetMonth() == 1 AND Close > Open then JanPOSCount[1] + 1 else JanPosCount[1];
AddLabel(1,
  "Jan Avg: " + AsPercent(JanChg / JanCount) + " | % Positive: " + AsPercent(JanPOSCount / JanCount) + " of " + JanCount,
   Color.CYAN
);

I copy/paste/modified that to have labels through July. When I have Jan through Apr there are 10 Jans. When I add May there are 11 Jans. The same occurs with SPY and TSLA so it's not anything symbol-specific. I'm guessing this happens when the current month is added to the code. Next month May won't break the code but June will. It seems to be an issue with how TOS provides the rolling time period. I adjusted my expansion area but, as I expected, that had no effect.

The calculations are accurate if you consider them in the context of calendar years. In the last 10 whole calendar years plus the current partial calendar year, there have been 11 Jans and 10 Junes. If you set the chart to a fixed time range of 5/4/12 to 5/4/21 it gives you 10 Jans, 10 Mays, 9 Junes, 9 Julys.

Now that there's an explanation for this and the data is accurate, with the caveat of months in the current year that have already passed are 11 yr averages instead of 10, you could choose to live with it. Another option is to look back instead of tracking as time passes, like the code I gave you in the other thread, but the problem with that is symbols that don't have enough data for the average length won't show anything.

If you stick with this code, you might want to consider modifying to use a single label per month like I did above. That way if the labels take up too much horizontal space and end up wrapping, the values for a single month will stay together.
 
Last edited:

bp805

New member
I modified your label to include the count:

Ruby:
def JanCount = if GetMonth() == 1 then JanCount[1] + 1 else JanCount[1];
def JanChg = if GetMonth() == 1 then JanChg[1] + ((close - open) / open) else JanChg[1];
def JanPOSCount = if GetMonth() == 1 AND Close > Open then JanPOSCount[1] + 1 else JanPosCount[1];
AddLabel(1,
  "Jan Avg: " + AsPercent(JanChg / JanCount) + " | % Positive: " + AsPercent(JanPOSCount / JanCount) + " of " + JanCount,
   Color.CYAN
);

I copy/paste/modified that to have labels through July. When I have Jan through Apr there are 10 Jans. When I add May there are 11 Jans. The same occurs with SPY and TSLA so it's not anything symbol-specific. I'm guessing this happens when the current month is added to the code. Next month May won't break the code but June will. It seems to be an issue with how TOS provides the rolling time period. I adjusted my expansion area but, as I expected, that had no effect.

The calculations are accurate if you consider them in the context of calendar years. In the last 10 whole calendar years plus the current partial calendar year, there have been 11 Jans and 10 Junes. If you set the chart to a fixed time range of 5/4/12 to 5/4/21 it gives you 10 Jans, 10 Mays, 9 Junes, 9 Julys.

Now that there's an explanation for this and the data is accurate, with the caveat of months in the current year that have already passed are 11 yr averages instead of 10, you could choose to live with it. Another option is to look back instead of tracking as time passes, like the code I gave you in the other thread, but the problem with that is symbols that don't have enough data for the average length won't show anything.

If you stick with this code, you might want to consider modifying to use a single label per month like I did above. That way if the labels take up too much horizontal space and end up wrapping, the values for a single month will stay together.

Thanks for looking at it. I had a feeling that something to the effect of what you're saying was taking place (the # of months that it was dividing by was off by 1 in certain instances), but then when looking at the chart and actually counting the # of months on a 5 year monthly chart for example there are an equal number of each month displayed (a 5 year chart starts with June currently and ends with May). But perhaps since the current month doesn't have a closing value yet that it throws the code off, or does it use the current days close in the calculation? I guess that may throw the average % off a bit.

But what it doesn't explain is when I am using two separate studies loaded onto the chart with the exact same code for the same month (March) and getting two completely different values on a 5 year monthly chart of Visa (V) (see screen shot below). Any thoughts?

FhA54Nm.png


I looked at your code from the other thread and may end up using that one and just adjust the code to be fixed to a 5 year chart instead of 10 and be happy with it. Do you feel like that code is more accurate? Thanks!
 

Slippage

Active member
or does it use the current days close in the calculation? I guess that may throw the average % off a bit.

Yes, once the market opens any time in the calendar month that monthly bar is created and has the day's open and the current price as the month's close. Your code will include that in the average. The code I posted in the other thread will not.

But what it doesn't explain is when I am using two separate studies loaded onto the chart with the exact same code for the same month (March) and getting two completely different values on a 5 year monthly chart of Visa (V) (see screen shot below). Any thoughts?

FhA54Nm.png

That one I can't explain. That seems like a bug on their end, in addition to this other weirdness probably also being a bug. You could contact customer support so they can be aware of the problem and maybe fix it some day but I wouldn't expect it to be fixed in a hurry.
 

bp805

New member
Yes, once the market opens any time in the calendar month that monthly bar is created and has the day's open and the current price as the month's close. Your code will include that in the average. The code I posted in the other thread will not.



That one I can't explain. That seems like a bug on their end, in addition to this other weirdness probably also being a bug. You could contact customer support so they can be aware of the problem and maybe fix it some day but I wouldn't expect it to be fixed in a hurry.
So I finally figured out why these labels were way off from my manual calculations. For some reason when I have multiple months loaded into the same study, the returns on the labels start to get thrown off. I ended up creating a separate study for each month to see if that fixed it after talking to some people in the TOS thinkscript lounge. After making a separate study for each month and loading them onto a 5 year monthly chart, all of a sudden everything was accurate and matched my manual calculations. Not sure why this is the case, but for some reason having multiple months together in a study was too much to handle for TOS.
 

Similar threads

Top