The question title you entered was far too brief to be of any assistance for the rest of our viewers. So I updated the title to describe the full context of your request. This will help the rest of our viewers locate this post when searching for a similar solution. When I updated the title it also changes the URL, which breaks the links for everyone that receives email notifications of new posts. So in the future please make sure the title of your questions explains the context of your request within around 6-8 words.
I examined your code and see where you went wrong. You tried using the function named "Highest()" where you should have been using the function named "Sum()". I'll explain a bit more here because I sense that you really want to learn what you are doing. Take the following section of code from what you provided:
highest((close > sma5), consecBarsThreshold) >= consecBarsThreshold
In English that says: "Get the highest value of (close > sma5) within the last x number of bars, then check if that value is greater than or equal to x number of bars". The highest values you will ever see for (close > sma5) is 1. It will be a value of 1 when it is true and a value of 0 when it is false. It will never be greater than 1 because a boolean statement can only be true/false (1/0). The end result is that your check for consecutive closes above or below the moving average will never be true, always false. That's why you are getting no signals. Change the user input "consecBarsThreshold" of your code to a value of 1 and every bar will be marked yellow.
I decided to write this from scratch using my own methods so that you can see the differences and follow my example. Minor point, please check the capitalization applied to the code. It makes things much easier to read when you follow the pattern provided in the examples from Thinkorswim. The capitalization rules are standard for most programming languages:
- Capitalize the first letter of all built-in functions
- All caps for built-in constants
- lower case first letter for all user defined variables
There is a bit more to it than that. But that will cover the vast majority of code you will write.
Here is my solution:
input consecutiveBars = 5;
input maLengthOne = 5;
input maTypeOne = AverageType.SIMPLE;
input maPriceOne = close;
plot maOne = MovingAverage(maTypeOne, maPriceOne, maLengthOne);
def barsAbove = Sum(close > maOne, consecutiveBars) >= consecutiveBars;
def barsBelow = Sum(close < maOne, consecutiveBars) >= consecutiveBars;
AssignPriceColor(if barsAbove then Color.YELLOW else if barsBelow then Color.YELLOW else Color.CURRENT);