Colored Candles - Assign Price Color

dvorakm

New member
2019 Donor
VIP
I am wanting to add instruction to a script to only color the first candle in a series.

Below you will see BULL condition will color the candles green and BEAR condition will color red. But...for example in an uptrend there will be many green candles painted and then change to red candles when trend changes. I am looking to color only the first candle (GREEN) that meets the BULL condition and only the first (RED) that meets BEAR condition. I hope I explained this sufficiently.

Code:
declare upper;
input price = close;
input TF1 = AggregationPeriod.MIN;
input TF2 = AggregationPeriod.FIVE_MIN;
input TF3 = AggregationPeriod.FIFTEEN_MIN;
input TF4 = AggregationPeriod.HOUR;
def o1 = open(period = TF1);
def c1 = close(period = TF1);
def o2 = open(period = TF2);
def c2 = close(period = TF2);
def o3 = open(period = TF3);
def c3 = close(period = TF3);
def o4 = open(period = TF4);
def c4 = close(period = TF4);
def BULL = (o1 < c1) and (o2 < c2) and (o3 < c3) and (o4 < c4);
def BEAR = (o1 > c1) and (o2 > c2) and (o3 > c3) and (o4 > c4);
DefineGlobalColor("BULL", Color.GREEN);
DefineGlobalColor("BEAR", Color.RED);
DefineGlobalColor( "default", Color.GRAY );
AssignPriceColor( if BULL then globalColor( "BULL" ) else if BEAR then globalColor( "BEAR" ) else globalColor( "default" ) );
hen globalColor( "BULL" ) else if BEAR then globalColor( "BEAR" ) else globalColor( "default" ) );
 

Froejank12

New member
I used lowercase letters to show what is added.

Code:
DefineGlobalColor("BULL", Color.GREEN);
DefineGlobalColor("BEAR", Color.RED);
DefineGlobalColor( "default", Color.GRAY );
AssignPriceColor( if BULL and !bull[2] and !bull[3] and !bull[4] and !bull[5] and !bull[6] and !bull[7] and !bull[8] and !bull[9] then globalColor( "BULL" ) else if BEAR and !bear[2] and !bear[3] and !bear[4] and !bear[5] and !bear[6] and !bear[7] and !bear[8] and !bear[9] then globalColor( "BEAR" ) else globalColor( "default" ) );

I also left the first most previous bar out because I was looking at an entire day of 1m candles and it looked better that way. You can add [1] back in if you are looking at a very small time interval.
 

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.
Top