How do I create a subset of data?

Arrive8848

New member
My goal is to Average the green candlestick bodies and the red candlestick bodies separately. I can easily parse out the green candlesticks over a range, but I cannot seem to figure out a way to ignore the red values. For example, using a simple "if/then" statement I can differentiate between green and red. But I have to do something with the red. If I set it to zero or any positive number, the average is skewed. I need to actually just ignore it altogether, i.e. creating a subset of the whole. I am not used to languages that require an "else" to the "if" statement! In every other language I know, I would just have an "if" statement by itself. This would automatically discard anything that does not satisfy the "if" statement requirements.

Figure 1: Example dataset with various results

1725472309399.png


Figure 2: Example code for column "RPL w/ avg" above

Code:
def greenCandles;
switch(METRIC) {
    case "open/close":
        if(close > open) { # Green candle
            greenCandles = 100 - (open / close) * 100;
        } else { # Red candle
            greenCandles = Average((100 - (open / close) * 100), LENGTH);
        }
    case "low/high":
        if(close > open) {
            greenCandles = 100 - (low / high) * 100;
        } else {
            greenCandles = Average((100 - (low / high) * 100), LENGTH);
        }
}
 
Solution
So ToS doesn't work quite like you may think. ThinkScript is a largely functional programming language.

This script will give you the average of the up and down bars in the last 20 bars, or whatever you set it to. It DOES NOT give you the average of the last 20 up bars or down bars. That's beyond by ability.

-mashume

Code:
declare lower;

input lookback = 50;

def upBodySize = if close > open then close - open else Double.NaN;
def isUpCandle =  if close > open then 1 else 0;


def dnBodySize = if close < open then  -(close - open) else Double.NaN;
def isDnCandle =  if close < open then 1 else 0;

def upCandleCount = Sum(isUpCandle, lookback);
def dnCandleCount = Sum(isDnCandle, lookback);


def upCandleSums = fold i = 0 to lookback...
So ToS doesn't work quite like you may think. ThinkScript is a largely functional programming language.

This script will give you the average of the up and down bars in the last 20 bars, or whatever you set it to. It DOES NOT give you the average of the last 20 up bars or down bars. That's beyond by ability.

-mashume

Code:
declare lower;

input lookback = 50;

def upBodySize = if close > open then close - open else Double.NaN;
def isUpCandle =  if close > open then 1 else 0;


def dnBodySize = if close < open then  -(close - open) else Double.NaN;
def isDnCandle =  if close < open then 1 else 0;

def upCandleCount = Sum(isUpCandle, lookback);
def dnCandleCount = Sum(isDnCandle, lookback);


def upCandleSums = fold i = 0 to lookback with s do s + if !isNan(upBodySize[i]) then upBodySize[i] else 0;

def dnCandleSums = fold k = 0 to lookback with d do d + if !isNan(dnBodySize[k]) then dnBodySize[k] else 0;


plot avgUpCandleSize = upCandleSums / upCandleCount;
plot avgDnCandleSize = dnCandleSums / dnCandleCount;

avgUpCandleSize.SetDefaultColor(color.dark_green);
avgDnCandleSize.SetDefaultColor(color.dark_red);

AddLabel(yes, "  Last " + lookback + " bars: " + upCandleCount + " up bars     " , Color.DARK_GREEN);
AddLabel(yes, "  Last " + lookback + " bars: " + dnCandleCount + " down bars     " , Color.DARK_RED);

# AddLabel(yes, "  Current Up sum: " + upCandleSums + "    ", color.dark_green);
# AddLabel(yes, "  Current Down sum: " + dnCandleSums + "    ", color.dark_red);
 
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
378 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