Count consecutive bars SMA and MACD in same direction


Category:
0
0

Hi Pete,
Please help me with my script attached. The column currently shows if the SMA and MACD are moving in the same direction. If both are increasing the column turns green and states “Long”. If both are decreasing the column turns red and states “Short”. Instead of stating long or short I would rather it states the number of consecutive bars. For example, if SMA and MACD are both increasing 3 bars in a row the field would appear green with “3” listed. If decreasing the field would appear red with “-3”. Thank you!

Attachments:
RESOLVED
Marked as spam
Posted by (Questions: 19, Answers: 18)
Asked on August 1, 2024 1:44 pm
52 views
0
Private answer

For this solution I decided to retain your original text descriptions of long and short and I just added the count value to the end of that description.

I see you built your bullish and bearish conditions using the Condition Wizard. And then you modified those statements to create variables and assign background colors based on those conditions. Nice work. For those who are interested in learning more, those steps were demonstrated on the following videos:

Brief overview:

https://www.hahn-tech.com/thinkorswim-condition-wizard-watchlist-2/

Detailed instruction:

https://www.hahn-tech.com/thinkorswim-condition-wizard-watchlist/

And here is your solution:

#This column shows if the stock SMA and Macd are moving in the same direction
def bullish = SimpleMovingAvg("length" = 20)."SMA" is greater than SimpleMovingAvg("length" = 20)."SMA" from 1 bars ago and MACDHistogram()."Diff" is greater than MACDHistogram()."Diff" from 1 bars ago;
def bearish = SimpleMovingAvg("length" = 20)."SMA" is less than SimpleMovingAvg("length" = 20)."SMA" from 1 bars ago and MACDHistogram()."Diff" is less than MACDHistogram()."Diff" from 1 bars ago;
rec countBullish = if !bullish[1] and bullish then 1 else if bullish then countBullish[1] + 1 else 0;
rec countBearish = if !bearish[1] and bearish then 1 else if bearish then countBearish[1] + 1 else 0;
AssignBackgroundColor(if bullish then Color.GREEN else if bearish then Color.RED else Color.CURRENT);
AddLabel(yes, if bullish then Concat("Long: ", countBullish) else if bearish then Concat("Short: ", -countBearish) else " " , Color.BLACK);

 

Marked as spam
Posted by (Questions: 37, Answers: 4117)
Answered on August 1, 2024 1:59 pm
0
Thank you Pete, it works perfectly. Also, thank you very much for the Condition Wizard tutorials. They have been very helpful and I am able to accomplish a lot without contacting you thanks to the videos. Cheers!
( at August 1, 2024 4:06 pm)