Color candles for consecutive bars above or below a moving average


Category:
0
0

Hi Pete. I’m trying to color candles based on consecutive bars above or below a selected moving average. I’m attempting to do this with a selectable user input option for number of bars above/below. In the attached example script, I’m using 5 bars above/below a 5-period SMA.

I know I’m doing something wrong as I’m not getting any signals but also not generating an errors. Having a hard time figuring out what it is and would really appreciate your help. Thanks so much.

declare lower;

input price = close;
input avgLength = 5;
input avgType = averageType.simple;
input consecBarsThreshold = 5;

def sma5 = movingAverage(avgType, close,(avgLength));

plot overBought = if highest((close > sma5), consecBarsThreshold) >= consecBarsThreshold then 1 else 0;

plot overSold = if highest((close < sma5), consecBarsThreshold) >= consecBarsThreshold then -1 else 0;

AssignPriceColor(if overBought then color.yellow else if oversold then color.yellow else color.current);

Marked as spam
Posted by (Questions: 5, Answers: 9)
Asked on October 30, 2020 11:59 pm
273 views
0
Private answer

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:

  1. Capitalize the first letter of all built-in functions
  2. All caps for built-in constants
  3. 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);

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on October 31, 2020 9:05 am
0
Pete, thank you so much for taking the time and effort to provide a detailed explanation. Very much appreciated. And yes, you are correct in that I do want to learn how to do this properly, and you've been a tremendous resource for that. And in the future I'll try to do a better job with a more descriptive title.
( at October 31, 2020 10:51 am)