Recursive not adding up value

kkrac

New member
I want to be able to show how many candles within a certain period of time are above EMA21.
So, if at the end of that period, there are 15 candles above EMA21, it should show a bubble with "15" above the last candle of that period.

In my code below, it shows "0" for some reason even though there are candles above EMA21, so it seems my recursive logic is not working as expected, not sure why.
Can anyone help out? Thanks!

Code:
def ema21 = ExpAverage(close, 21);
input startTime = 0929;
input endTime = 1300;
def startCounter = SecondsFromTime(startTime);
def endCounter = SecondsTillTime(endTime);
def targetPeriod = if startCounter >= 0 and endCounter >= 0 then 1 else 0;
def candleToShowBubbleOn = SecondsFromTime(1300) == 0 and secondsTillTime(1300) == 0;

def count = if targetPeriod and !targetPeriod[1] then close > ema21
            else if targetPeriod then close > ema21 + count[1]
            else count[1];

addChartBubble(candleToShowBubbleOn, high, count, Color.RED, yes);
 
Solution
Here's a version that will work, and includes an option to show only today or all days, as well as dynamic inputs to change the EMA length, time periods, and bubble color. You may or may not like the bubble formatting, so you can remove lines 50-52 if you just want the number.

Ruby:
# DECLARATIONS
declare upper;
declare once_per_bar;
declare hide_on_daily;


# INPUTS
input emaLength      = 21;
input startTimeEST   = 0929;
input endTimeEST     = 1300;
input showOnlyToday  = no;


# GLOBAL COLOR DEFINITIONS
DefineGlobalColor("BubbleColor", CreateColor(225, 0, 0));


# DEFINITIONS / CALCULATIONS
def isToday = GetDay() == GetLastDay();
def isRTH = GetTime() >= regularTradingStart(getYYYYMMDD()) and GetTime() <=...
Here's a version that will work, and includes an option to show only today or all days, as well as dynamic inputs to change the EMA length, time periods, and bubble color. You may or may not like the bubble formatting, so you can remove lines 50-52 if you just want the number.

Ruby:
# DECLARATIONS
declare upper;
declare once_per_bar;
declare hide_on_daily;


# INPUTS
input emaLength      = 21;
input startTimeEST   = 0929;
input endTimeEST     = 1300;
input showOnlyToday  = no;


# GLOBAL COLOR DEFINITIONS
DefineGlobalColor("BubbleColor", CreateColor(225, 0, 0));


# DEFINITIONS / CALCULATIONS
def isToday = GetDay() == GetLastDay();
def isRTH = GetTime() >= regularTradingStart(getYYYYMMDD()) and GetTime() <= regularTradingEnd(getYYYYMMDD());
def isETH = !isRTH;
def targetPeriod =  (!showOnlyToday or isToday)
                    and isRTH
                    and SecondsFromTime(startTimeEST) >= 0
                    and secondsTillTime(endTimeEST) >= 0;

def ema = ExpAverage(close, emaLength);
def candleToShowBubbleOn = SecondsFromTime(endTimeEST)[1] < 0 and SecondsFromTime(endTimeEST) >= 0;
def aboveEMA = if targetPeriod and close > ema then 1 else 0;
def count = if targetPeriod and !targetPeriod[1] then 1 else aboveEMA + count[1];
def totalCandles = if targetPeriod then 1 else 0;
def totalCandleCount = if targetPeriod and !targetPeriod[1] then 1 else totalCandles + totalCandleCount[1];


# PLOTS
#N/A


# PLOT FORMATTING
#N/A


# BUBBLES
addChartBubble(
    (showOnlyToday and isToday and candleToShowBubbleOn)
    or (!showOnlyToday and candleToShowBubbleOn)
    , high
    , count
      + " / "
      + totalCandleCount
      + " bars above EMA "
      + emaLength
    , GlobalColor("BubbleColor")
    , yes
);
 
Solution

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
471 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