Candle Body Script help

VIP_TOS

Member
The code below shows a chart bubble for every candles body height. How can this be modified so a chart bubble ONLY appears if
body height is between 2 - 4 in height?


def CandleBodyTop = MidBodyVal() + 0.5 * BodyHeight();

addchartbubble(1, candleBodyTop,BodyHeight() ,color.cyAN);
 
Solution
Hey @Svanoy thank you for following up with me & thank you @halcyonguy for chiming in to help out.
I am only a few weeks in on learning thinkscript so excuse me on the confusion I may have caused. I truly appreciate the help & guidance.
as for the script I believe i got it to work with fold. It now seems to sum the correct amount when I change the period input & time frame aggregation input. I attached what i have completed any input on if of if not correct would be appreciated.


Ruby:
#Short Term Candle Body Sentiment

input ShortTermSentiment = aggregationPeriod.fivE_MIN;
input ShortTermSumPeriods = 6;

def ST_O = open(period = ShortTermSentiment);
def ST_C = close(period = ShortTermSentiment);
def ST_bodyHeight =...
Ruby:
input agg = aggregationPeriod.fIFTEEN_MIN;

input Count = 2;

def green_Candle_NAME = Green;

def o = open(period = agg);

def c = close(period = agg);

def bodyHeight = (AbsValue(o-c));

def sumBH = fold i = 1 to count with green_Candle_NAME_Counter = 0 do if GetValue(green_Candle_NAME,i)==1 then green_Candle_NAME_Counter+GetValue(BodyHeight,i) else green_Candle_NAME_Counter ;





addlabel(1, " Sum Body Height " + SumBH + "|Periods = " + count , color.white);


#Candle Colors are defining candle names/ colors earlier in the code.
I got the new above script to work correctly on the 15m chart. The sum is correct for input count 2 on 15m chart
but when I load a 5min chart the sum is not the same anymore as a 15min chart. it not even the sum for 5m chart

I am trying to sum the body height for 15m chart candles & see chart label on 5m chart for that sum
 

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

@VIP_TOS
change
Ruby:
def sumBH = fold i = 1 to count with green_Candle_NAME_Counter = 0 do if GetValue(green_Candle_NAME,i)==1 then green_Candle_NAME_Counter+GetValue(BodyHeight,i) else green_Candle_NAME_Counter ;
to
Ruby:
def sumBH = fold i = 1 to count with green_Candle_NAME_Counter = 0 do if GetValue(green_Candle_NAME,i)==1 then green_Candle_NAME_Counter+GetValue(BodyHeight,i) else sumBH[1] ;
 
@Svanoy thank you for trying to help. I updated it & it did not work. It gave me a number way off in the thousands.



To make this easier i started a script from scratch with the same concept & coded it to count basic red vs green candles. You can put the attached script in TOS & see for yourself. When you have a 5min chart open & keep aggregationPeriod at 5min it correctly sums the bodies of red vs green in the labels. the problem comes in when you change the input aggregationPeriod to another time frame say 15min the opened 5min chart labels do not sum the correct number for 15min candles. it is a random number I do not know where it is coming from.

The outcome i am trying to achieve is have only the 5min chart open & the chart labels to sum green candles vs red candles for a period of time on the 15min aggregationPeriod.


(For reference to check if the sum is correct, I have 2 charts open the 5min and the 15min the 5min label does not match the 15min body sums)
Ruby:
input BubbleCandleCount = 6;
def CandleBodyTop = MidBodyVal() + 0.5 * BodyHeight();
def cond = BodyHeight() is greater than 0  ;
def hcount  = CompoundValue(1, if cond then hcount[1] + 1 else hcount[1], 0);
AddChartBubble(cond and HighestAll(hcount) - hcount <= BubbleCandleCount- 1 , CandleBodyTop,

 + BodyHeight()
, Color.CYAN);





def RED = close < open;
def Green = close > open;

input agg = aggregationPeriod.fivE_MIN;

input GreenRedCandleCount = 6;
def o = open(period = agg);
def c = close(period = agg);
def bodyHeight = (AbsValue(o-c));

def RED_BODY_Count = fold i = 1 to GreenRedCandleCount with Red_Counter = 0 do if GetValue(Red,i)==1 then Red_Counter+GetValue(BodyHeight,i) else Red_Counter ;

def GREEN_BODY_Count = fold h = 1 to GreenRedCandleCount with Green_Counter = 0 do if GetValue(Green,h)==1 then Green_Counter+GetValue(BodyHeight,h) else Green_Counter ;


addlabel(1, " Sum Body Height  " + RED_BODY_Count+ "|Periods =  " + GreenRedCandleCount , color.red);
addlabel(1, " Sum Body Height  " + GREEN_BODY_Count+ "|Periods =  " + GreenRedCandleCount , color.grEEN);
 
@VIP_TOS
I've not had time to look at this yet.
Just glancing over it, how does it know which bar to start counting on?
Without that it would start counting at the first bar on the chart and on a five minute chart that may not be the same open as the first bar on a 15 min chart.
 
the folds are reading from these formulas,
def RED = close < open;
def Green = close > open;

which do not use 2nd aggregation.

The red and green formulas should have been after the O and C formulas
and referenced O and C.

def RED = c < o;
def Green = c > o;

i think sum( ) would be easier to use than fold, to add up values.
 
Last edited:
Hey @Svanoy thank you for following up with me & thank you @halcyonguy for chiming in to help out.
I am only a few weeks in on learning thinkscript so excuse me on the confusion I may have caused. I truly appreciate the help & guidance.
as for the script I believe i got it to work with fold. It now seems to sum the correct amount when I change the period input & time frame aggregation input. I attached what i have completed any input on if of if not correct would be appreciated.


Ruby:
#Short Term Candle Body Sentiment

input ShortTermSentiment = aggregationPeriod.fivE_MIN;
input ShortTermSumPeriods = 6;

def ST_O = open(period = ShortTermSentiment);
def ST_C = close(period = ShortTermSentiment);
def ST_bodyHeight = (AbsValue(ST_O-ST_C));

def ST_sumGreen= fold i = 0 to ShortTermSumPeriods with s do s + if(getValue(ST_C > ST_C[1], i), getValue(ST_bodyHeight, i), 0);
def ST_sumRed= fold h = 0 to ShortTermSumPeriods with g do g + if(getValue(ST_C < ST_C[1], h), getValue(ST_bodyHeight, h), 0);



#ST LABELS
AddLabel(yes, "|Sum Green Body" + (ST_sumGreen));
AddLabel(yes, "|Sum Red Body" + (ST_sumRed));
 
Hey @Svanoy thank you for following up with me & thank you @halcyonguy for chiming in to help out.
I am only a few weeks in on learning thinkscript so excuse me on the confusion I may have caused. I truly appreciate the help & guidance.
as for the script I believe i got it to work with fold. It now seems to sum the correct amount when I change the period input & time frame aggregation input. I attached what i have completed any input on if of if not correct would be appreciated.


Ruby:
#Short Term Candle Body Sentiment

input ShortTermSentiment = aggregationPeriod.fivE_MIN;
input ShortTermSumPeriods = 6;

def ST_O = open(period = ShortTermSentiment);
def ST_C = close(period = ShortTermSentiment);
def ST_bodyHeight = (AbsValue(ST_O-ST_C));

def ST_sumGreen= fold i = 0 to ShortTermSumPeriods with s do s + if(getValue(ST_C > ST_C[1], i), getValue(ST_bodyHeight, i), 0);
def ST_sumRed= fold h = 0 to ShortTermSumPeriods with g do g + if(getValue(ST_C < ST_C[1], h), getValue(ST_bodyHeight, h), 0);



#ST LABELS
AddLabel(yes, "|Sum Green Body" + (ST_sumGreen));
AddLabel(yes, "|Sum Red Body" + (ST_sumRed));

if you can understand a fold line after just a couple of weeks in thinkscript, you are doing pretty good.

here is an alternative to the fold loops. add this to the end of your study in post#27

Code:
def g2 = if ST_C > ST_C[1] then ST_bodyHeight else 0;
def Green2 = sum( g2, ShortTermSumPeriods );

def r2 = if ST_C < ST_C[1] then ST_bodyHeight else 0;
def red2 = sum( r2, ShortTermSumPeriods );

AddLabel(yes, "Sum green2 Body " + green2, color.magenta);
AddLabel(yes, "Sum red2 Body " + red2, color.magenta);
 
Solution

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
488 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