BAR & CANDLE Studies for Lower Chart In ThinkOrSwim

Carl-not-Karl

ToS Expert
VIP
<edited code 08-09-2025>:
Added user-controlled colors for BARs, CANDLEs, Highest & Lowest, Last price

1754766830290.png


The following are enhanced studies based on the works of @petergluis and @halcyonguy found on this thread:
Heikin-Ashi Candles Lower Chart For ThinkorSwim (assorted versions)

In the image are default tos BAR and CANDLE charts on the top with the enhanced CANDLE and BAR studies in the lower charts.
The tos default BAR chart does not treat 'doji' bars (color of WHITE) as defined in Investopedia:
"...in which a security has an open and close that are virtually equal, which resembles a candlestick on a chart."
The enhanced BAR study meets the above definition.
The colors of both BARs and CANDLEs may be changed to user's preference in the code.
CANDLE fill is user controlled (on/off).

1754750623548.png


CANDLE code:
Code:
declare lower;

input Fill = yes;

def O = open;
def H = high;
def L = low;
def C = close;
#== Up CANDLE
def UpO;
def UpH;
def UpL;
def UpC;
if O <= C
then {
    UpO = if Fill then C else O;
    UpH = H;
    UpL = L;
    UpC = if Fill then O else C;
} else {
    UpO = Double.NaN;
    UpH = Double.NaN;
    UpL = Double.NaN;
    UpC = Double.NaN;
}
#== Down CANDLE
def DnO;
def DnH;
def DnL;
def DnC;
if O >= C
then {
    DnO = if Fill then O else C;
    DnH = H;
    DnL = L;
    DnC = if Fill then C else O;
} else {
    DnO = Double.NaN;
    DnH = Double.NaN;
    DnL = Double.NaN;
    DnC = Double.NaN;
}
#== Doji CANDLE
def DUpO;
def DUpH;
def DUpL;
def DUpC;
if O == C
then {
    DUpO = O;
    DUpH = H;
    DUpL = L;
    DUpC = C;
} else {
    DUpO = Double.NaN;
    DUpH = Double.NaN;
    DUpL = Double.NaN;
    DUpC = Double.NaN;
}

def DDnO;
def DDnH;
def DDnL;
def DDnC;
if O == C
then {
    DDnO = O;
    DDnH = H;
    DDnL = L;
    DDnC = C;
} else {
    DDnO = Double.NaN;
    DDnH = Double.NaN;
    DDnL = Double.NaN;
    DDnC = Double.NaN;
}
#== Up & Down CANDLE colors
DefineGlobalColor("Up", GetColor(6));
AddChart(high = UpH, low = UpL, open = UpO, close = UpC, type = ChartType.CANDLE, GlobalColor("Up"));
DefineGlobalColor("Down", GetColor(5));
AddChart(high = DnH, low = DnL, open = DnO, close = DnC, type = ChartType.CANDLE, GlobalColor("Down"));

#== Doji CANDLE color
AddChart(high = DUpH, low = DUpL, open = DUpO, close = DUpO, type = ChartType.CANDLE, growColor = Color.WHITE);
AddChart(high = DDnH, low = DDnL, open = DDnO, close = DDnO, type = ChartType.CANDLE, growColor = Color.WHITE);

#== Last Price color
DefineGlobalColor("Last", GetColor(9));
AddChartBubble(IsNaN(C[-1]), text = Round(C, 2), "price location" = C, GlobalColor("Last"));

#== Highest & Lowest of period color
def HA = HighestAll(H);
def LA = LowestAll(L);
DefineGlobalColor("Highest", GetColor(6));
AddChartBubble(H == HA, H, "Hi: " + Round(HA, 2), GlobalColor("Highest"));
DefineGlobalColor("Lowest", GetColor(5));
AddChartBubble(L == LA, L, "Lo: " + Round(LA, 2), GlobalColor("Lowest"), no);

BAR code:
Code:
declare lower;

def O = open;
def H = high;
def L = low;
def C = close;
#== Up BAR
def UpO;
def UpH;
def UpL;
def UpC;
if O <= C
then {
    UpO = O;
    UpH = H;
    UpL = L;
    UpC = C;
} else {
    UpO = Double.NaN;
    UpH = Double.NaN;
    UpL = Double.NaN;
    UpC = Double.NaN;
}
#== Down BAR
def DnO;
def DnH;
def DnL;
def DnC;
if O >= C
then {
    DnO = O;
    DnH = H;
    DnL = L;
    DnC = C;
} else {
    DnO = Double.NaN;
    DnH = Double.NaN;
    DnL = Double.NaN;
    DnC = Double.NaN;
}
#== Doji BAR
def DUpO;
def DUpH;
def DUpL;
def DUpC;
if O == C
then {
    DUpO = O;
    DUpH = H;
    DUpL = L;
    DUpC = C;
} else {
    DUpO = Double.NaN;
    DUpH = Double.NaN;
    DUpL = Double.NaN;
    DUpC = Double.NaN;
}

def DDnO;
def DDnH;
def DDnL;
def DDnC;
if O == C
then {
    DDnO = O;
    DDnH = H;
    DDnL = L;
    DDnC = C;
} else {
    DDnO = Double.NaN;
    DDnH = Double.NaN;
    DDnL = Double.NaN;
    DDnC = Double.NaN;
}
#== Up & Down BAR colors
DefineGlobalColor("Up", GetColor(6));
AddChart(high = UpH, low = UpL, open = UpO, close = UpC, type = ChartType.BAR, GlobalColor("Up"));
DefineGlobalColor("Down", GetColor(5));
AddChart(high = DnH, low = DnL, open = DnO, close = DnC, type = ChartType.BAR, GlobalColor("Down"));

#== Doji BAR color
AddChart(high = DUpH, low = DUpL, open = DUpO, close = DUpO, type = ChartType.BAR, growColor = Color.WHITE);
AddChart(high = DDnH, low = DDnL, open = DDnO, close = DDnO, type = ChartType.BAR, growColor = Color.WHITE);

#== Last Price color
DefineGlobalColor("Last", GetColor(9));
AddChartBubble(IsNaN(C[-1]), text = Round(C, 2), "price location" = C, GlobalColor("Last"));

#== Highest & Lowest of period color
def HA = HighestAll(H);
def LA = LowestAll(L);
DefineGlobalColor("Highest", GetColor(6));
AddChartBubble(H == HA, H, "Hi: " + Round(HA, 2), GlobalColor("Highest"));
DefineGlobalColor("Lowest", GetColor(5));
AddChartBubble(L == LA, L, "Lo: " + Round(LA, 2), GlobalColor("Lowest"), no);
 
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
329 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