TTM Trend Statistics


Category:
0
0

Hi Pete, I made this indicator https://tos.mx/3H7wbFH and was wondering if it works as intended. I want it to show the % of uptrending blue candles to red candles and I want it to show the average number of bars that the trend will last for. Thank you for any help, I hope you guys like my indicators.

Marked as spam
Posted by (Questions: 1, Answers: 1)
Asked on November 16, 2019 5:26 pm
179 views
0
Please do not use those share links. They have an expiration date and can be revoked by the user. This post will be around for many years and it's important to include the entire code instead of a link to it. If the code is too long to post with the question you can save it to a plain text file and include it as an attachment.
( at November 16, 2019 7:01 pm)
0
Private answer

I previously responded to your question via the comments section above. Since I received no response to that comment I am including that response here in addition to my answer below.

Please do not use those share links. They have an expiration date and can be revoked by the user. This post will be around for many years and it's important to include the entire code instead of a link to it. If the code is too long to post with the question you can save it to a plain text file and include it as an attachment.

The simple answer to your question, is to adjust your chart to display 1 month of daily candles. This will average about 21 bars on the chart and make it very easy to see if your code is accurate. This will also create the ideal environment to troubleshoot your code to correct any errors.

Since you provided your code via a share link to Thinkorswim I am including your code here for the benefit of our viewers. The only changes I have made were to correct minor imperfections in the way you wrote the code. I have not tried to check it for accuracy or made any changes that impact it's computations:

plot ttmTrend = TTM_Trend();
ttmTrend.hide();
def totalBars = if SimpleMovingAvg() != SimpleMovingAvg()[1] then totalBars[1] + 1 else totalBars[1];
def trend = if ttmTrend != ttmTrend[1] then trend[1] + 1 else trend[1];
def upTrend = if ttmTrend == 1 then + 1 + upTrend[1] else upTrend[1];
def downTrend = if ttmTrend == 0 then + 1 + downTrend[1] else downTrend[1];
def trendUp = Round(upTrend / totalBars) * 100 > 50;
def trendDown = Round(downTrend / totalBars) * 100 > 50;
def trendUpAvg = Round(upTrend / (trend / 2));
def trendDownAvg = Round(downTrend / (trend / 2));
Addlabel(yes, "ttmTrend", Color.WHITE);
Addlabel(yes, "trend Up " + Round(upTrend / totalBars) * 100 + "%", if trendUp then CreateColor(150, 150, 255) else Color.WHITE);
Addlabel(yes, "trend Down " + Round(downTrend / totalBars) * 100 + "%", if trendDown then Color.RED else Color.WHITE);
Addlabel(yes, "trend Up Avg :" + Round(upTrend / (trend / 2)), if trendUpAvg > trendDownAvg then CreateColor(150, 150, 255) else Color.WHITE);
Addlabel(yes, "trend Down Avg :" + Round(downTrend / (trend / 2)), if trendDownAvg > trendUpAvg then Color.RED else Color.WHITE);

Marked as spam
Posted by (Questions: 37, Answers: 4087)
Answered on November 24, 2019 12:27 pm
0
Ok, thank you. I checked the 1 month 1 day time frame and it seems to be working, but the numbers are a little off. I changed the chart to 1Y 1D and it is almost accurate only 2 bars off the totalBars and only 5 bars off the percent. And the trend Up Avg seems to work on the 3 month 1 day time frame. I changed the totalBars from simple to adaptiveEMA and it seems to be a little more accurate. I also used what you updated and added in a count down for how many numbers it has left on average or how many numbers it went over on average. Should I post the updated version as a comment or an answer?
( at November 24, 2019 1:42 pm)
0
If you have an updated version of the code that improves on the original then by all means post it as a new answer to the question. Thank you!
( at November 24, 2019 6:39 pm)
0
Private answer

plot ttmTrend = TTM_Trend();
ttmTrend.Hide();
def totalBars = if AdaptiveEMA() != AdaptiveEMA()[1] then totalBars[1] + 1 else totalBars[1];
def trend = if ttmTrend != ttmTrend[1] then trend[1] + 1 else trend[1];
def upTrend = if ttmTrend == 1 then + 1 + upTrend[1] else upTrend[1];
def upTrend2 = if ttmTrend == 1 then + 1 + UpTrend2[1] else 0;
def downTrend = if ttmTrend == 0 then + 1 + downTrend[1] else downTrend[1];
def downTrend2 = if ttmTrend == 0 then + 1 + DownTrend2[1] else 0;
def trendUp = Round(upTrend / totalBars) * 100 > 50;
def trendDown = Round(downTrend / totalBars) * 100 > 50;
def trendUpAvg = Round(upTrend / (trend / 2));
def trendDownAvg = Round(downTrend / (trend / 2));
Addlabel(yes, "ttmTrend " + "totalBars " + totalBars, Color.WHITE);
AddLabel(yes, "trend Up total " + upTrend + " : " + Round(upTrend / totalBars) * 100 + "%", if trendUp then CreateColor(150, 150, 255) else Color.WHITE);
AddLabel(yes, "trend Down total " + downTrend + " : " + Round(downTrend / totalBars) * 100 + "%", if trendDown then Color.RED else Color.WHITE);
AddLabel(yes, "trend Up Avg: " + Round(upTrend / (Trend / 2)) + " " + "Avg Left: " + Round((upTrend / (trend / 2))- upTrend2), if trendUpAvg > trendDownAvg then CreateColor(150, 150, 255) else Color.WHITE);
AddLabel(yes, "trend Down Avg :" + Round(downTrend / (trend / 2)) + " " + "Avg Left: " + Round((downTrend / (trend / 2))- downTrend2), if trendDownAvg > trendUpAvg then Color.RED else Color.WHITE);

Marked as spam
Posted by (Questions: 1, Answers: 1)
Answered on November 25, 2019 1:12 pm