simple pullback trend indicator help

METAL

Active member
Plus
I am attempting to have a histogram that plots green bars when in uptrend. Uptrend is when price is above 20 ema and 5ema (Both ema's) If price goes below 5 ema (ema2) then Histo bar is yellow and visa versa for downtrend. At the moment I am only getting bars when price crosses the ema's and not while price is above or below and I am not getting a pullback (yellow) bar at all. Thanks in advance!

Code:
# Trend_Pullback_Indicator

input emaLength1 = 20;
input emaLength2 = 5;

def closePrice = close;
def ema1 = ExpAverage(closePrice, emaLength1);
def ema2 = ExpAverage(closePrice, emaLength2);

def isUptrend = close > ema1 and close[1] <= ema1[1];
def isDowntrend = close < ema1 and close[1] >= ema1[1];

def isPullbackUp = close > ema1 and close <= ema2;
def isPullbackDown = close < ema1 and close >= ema2;

def histogramValue = if isUptrend or isDowntrend then close - ema1 else Double.NaN;

plot histogram = histogramValue;
histogram.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
histogram.SetLineWeight(3);
histogram.AssignValueColor(if isUptrend then Color.GREEN else if isDowntrend then Color.RED else if isPullbackUp or isPullbackDown then Color.YELLOW else Color.GRAY);
 
Here is my latest attempt. Still not working 100%. Hopefully one of you guys can help me with this.
Code:
# Trend Indicator for TOS

input emaLength1 = 20;
input emaLength2 = 5;

def closePrice = close;
def ema1 = ExpAverage(closePrice, emaLength1);
def ema2 = ExpAverage(closePrice, emaLength2);

def isUptrend = close > ema1 and close > ema2;
def isDowntrend = close < ema1 and close < ema2;

def isPullbackUp = close > ema1 and close crosses below ema2;
def isPullbackDown = close < ema1 and close crosses above ema2;

def histogramValue = if isUptrend then close - ema1
                    else if isDowntrend then close - ema1
                    else if isPullbackUp then close - ema2
                    else if isPullbackDown then close - ema2
                    else Double.NaN;

plot zeroLine = 0;
zeroLine.SetDefaultColor(Color.WHITE);

plot histogram = histogramValue;
histogram.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
histogram.SetLineWeight(3);
histogram.AssignValueColor(if isUptrend then Color.GREEN
                          else if isDowntrend then Color.RED
                          else if isPullbackUp then Color.YELLOW
                          else if isPullbackDown then Color.MAGENTA
                          else Color.GRAY);

1701891374954.png
 
def isPullbackUp = close > ema1 and close crosses below ema2;
def isPullbackDown = close < ema1 and close crosses above ema2;

try this instead:

def isPullbackUp = close > ema1 and close < ema2;
def isPullbackDown = close < ema1 and close > ema2;

and please, don't cry because I get emotional
That worked for the coloring but it still plots the PBU below the zero and the PBD above the zero. Do you have any Idea what I need to change to get that correct. It isn't a huge issue but I would still likt it to represent the up or down pullback. Thanks..
 
That worked for the coloring but it still plots the PBU below the zero and the PBD above the zero. Do you have any Idea what I need to change to get that correct. It isn't a huge issue but I would still likt it to represent the up or down pullback. Thanks..
def histogramValue = if isUptrend then close - ema1
else if isDowntrend then close - ema1
else if isPullbackUp then close - ema2
else if isPullbackDown then close - ema2
else Double.NaN;

not sure if I understood what you refer to but as I see instead of ema2 all the emas should be ema1. try that and see if it is what you are looking after
 
def histogramValue = if isUptrend then close - ema1
else if isDowntrend then close - ema1
else if isPullbackUp then close - ema2
else if isPullbackDown then close - ema2
else Double.NaN;

not sure if I understood what you refer to but as I see instead of ema2 all the emas should be ema1. try that and see if it is what you are looking after
That was it. I thought I tried that when I was having other issues. Thanks a lot!
 
I am attempting to have a histogram that plots green bars when in uptrend. Uptrend is when price is above 20 ema and 5ema (Both ema's) If price goes below 5 ema (ema2) then Histo bar is yellow and visa versa for downtrend. At the moment I am only getting bars when price crosses the ema's and not while price is above or below and I am not getting a pullback (yellow) bar at all. Thanks in advance!

Code:
# Trend_Pullback_Indicator

input emaLength1 = 20;
input emaLength2 = 5;

def closePrice = close;
def ema1 = ExpAverage(closePrice, emaLength1);
def ema2 = ExpAverage(closePrice, emaLength2);

def isUptrend = close > ema1 and close[1] <= ema1[1];
def isDowntrend = close < ema1 and close[1] >= ema1[1];

def isPullbackUp = close > ema1 and close <= ema2;
def isPullbackDown = close < ema1 and close >= ema2;

def histogramValue = if isUptrend or isDowntrend then close - ema1 else Double.NaN;

plot histogram = histogramValue;
histogram.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
histogram.SetLineWeight(3);
histogram.AssignValueColor(if isUptrend then Color.GREEN else if isDowntrend then Color.RED else if isPullbackUp or isPullbackDown then Color.YELLOW else Color.GRAY);

the last few posts were just patches, so i wasn't sure what the final study was, so i made one.

i started by looking at the chart, and determined there are 3 zones to have different colors.
there are 2 average lines, so,
above top line, in between the lines, below bottom line.
i decided i would want 2 different colors for the in between, 1 for upwards, 1 for down. so i added cyan.

i simplified this,
def histogramValue = if isUptrend or isDowntrend then close - ema1 else Double.NaN;
to this,
def histogramValue = close - ema1;

i never use a specific average function (like ExpAverage() ).
i always use MovingAverage( ), so i can change the type of average.
because the averages could be different numbers, i renamed them to avg1 and avg2.


Code:
#histo_colors_with_emas

#https://usethinkscript.com/threads/simple-pullback-trend-indicator-help.17371/#post-135114
#simple pullback trend indicator help

declare lower;

def na = double.nan;
def bn = barnumber();

def c = close;
input avg1_type = AverageType.exponential;
input avg1_length = 5;
def avg1 = MovingAverage( avg1_type , c , avg1_length );
input avg2_type = AverageType.exponential;
input avg2_length = 20;
def avg2 = MovingAverage( avg2_type , c, avg2_length );

def trendup = avg1 > avg2;
def trenddwn = avg1 < avg2;

def above = c > max(avg1, avg2);
def bet_dwn = trendup and between(c, min(avg1, avg2), max(avg1, avg2));
def bet_up = trenddwn and between(c, min(avg1, avg2), max(avg1, avg2));
def below = c < min(avg1, avg2);

def histogramValue = close - avg1;

plot histogram = histogramValue;
histogram.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
histogram.SetLineWeight(3);
histogram.AssignValueColor(
 if above then color.green
 else if bet_up then color.cyan
 else if bet_dwn then color.yellow
 else if below then color.red
 else color.gray);
#

COST hour
qrFhKQb.jpg
 
the last few posts were just patches, so i wasn't sure what the final study was, so i made one.

i started by looking at the chart, and determined there are 3 zones to have different colors.
there are 2 average lines, so,
above top line, in between the lines, below bottom line.
i decided i would want 2 different colors for the in between, 1 for upwards, 1 for down. so i added cyan.

i simplified this,
def histogramValue = if isUptrend or isDowntrend then close - ema1 else Double.NaN;
to this,
def histogramValue = close - ema1;

i never use a specific average function (like ExpAverage() ).
i always use MovingAverage( ), so i can change the type of average.
because the averages could be different numbers, i renamed them to avg1 and avg2.


Code:
#histo_colors_with_emas

#https://usethinkscript.com/threads/simple-pullback-trend-indicator-help.17371/#post-135114
#simple pullback trend indicator help

declare lower;

def na = double.nan;
def bn = barnumber();

def c = close;
input avg1_type = AverageType.exponential;
input avg1_length = 5;
def avg1 = MovingAverage( avg1_type , c , avg1_length );
input avg2_type = AverageType.exponential;
input avg2_length = 20;
def avg2 = MovingAverage( avg2_type , c, avg2_length );

def trendup = avg1 > avg2;
def trenddwn = avg1 < avg2;

def above = c > max(avg1, avg2);
def bet_dwn = trendup and between(c, min(avg1, avg2), max(avg1, avg2));
def bet_up = trenddwn and between(c, min(avg1, avg2), max(avg1, avg2));
def below = c < min(avg1, avg2);

def histogramValue = close - avg1;

plot histogram = histogramValue;
histogram.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
histogram.SetLineWeight(3);
histogram.AssignValueColor(
 if above then color.green
 else if bet_up then color.cyan
 else if bet_dwn then color.yellow
 else if below then color.red
 else color.gray);
#

COST hour
qrFhKQb.jpg
Exactly what I was attempting. One for uptrend, one for pullback up, one for downtrend, and one for pullback down. The last one I have is similar to yours. I will post it as well. BTW, this is actually really good. I use it on the 2m and I use 3 different variations.
 
Last edited:

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
326 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top