How does Sum function work

Scotty123

New member
VIP
Hi,

I am trying to find stocks that are up 2 of the last 3 days, starting from yesterday. If you can help me do that, you can ignore my question below.

Thanks for any help!
Scott

Question Below:

I think that I can use the Sum function for my problem. But in my testing, I was surprised to see that stocks that were up for the last 3 days (yesterday, the day before yesterday and the day before that), had values of 1, 2 or 3 when I added a column with the code below.

def priceCondition = close[1] > close[2] and close[2] > close[3] and close[3] > close[4];
plot out = Sum(priceCondition, 3);

Can anyone explain why that is?
 
Solution
Hi Ben,

Thanks for your quick reply.

I'm wanting code that will be used in a filter to return stocks that have gone up 2 of the past 3 days, starting yesterday.

I can't be sure, but this code seems to work.

def priceCondition = close[1] > close[2];
plot signal = Sum(priceCondition, 3) >= 2;

I believe it reads that the priceCondition is defined as yesterdays close is greater than the previous days close. And the Sum function says to filter the previous 3 days and output stocks that meet the priceCondition twice.

Does that seem right?

i agree with this .
but i like to make formulas that interact with current bar. then use an offset on the variable name , as needed. so my version of sum uses this ...
I don't think you need to use the Sum function for this.

You can create your own pattern using the Candlestick Pattern Editor in TOS.

Code:
# Generation time: 2025-03-06T00:18:14.381729600Z

def IsUp = close > open;
def IsDown = close < open;
def IsDoji = IsDoji();
def avgRange = 0.05 * Average(high - low, 20);
plot PatternPlot =
    IsUp[2] and
    IsUp[1] and
    IsUp[0] and
    close[2] < close[1] and
    close[1] < close[0];

PatternPlot.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
PatternPlot.SetDefaultColor(GetColor(0));

1741220351872.png
 

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

I don't think you need to use the Sum function for this.

You can create your own pattern using the Candlestick Pattern Editor in TOS.

Code:
# Generation time: 2025-03-06T00:18:14.381729600Z

def IsUp = close > open;
def IsDown = close < open;
def IsDoji = IsDoji();
def avgRange = 0.05 * Average(high - low, 20);
plot PatternPlot =
    IsUp[2] and
    IsUp[1] and
    IsUp[0] and
    close[2] < close[1] and
    close[1] < close[0];

PatternPlot.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
PatternPlot.SetDefaultColor(GetColor(0));

View attachment 24239

Hi Ben,

Thanks for your quick reply.

I'm wanting code that will be used in a filter to return stocks that have gone up 2 of the past 3 days, starting yesterday.

I can't be sure, but this code seems to work.

def priceCondition = close[1] > close[2];
plot signal = Sum(priceCondition, 3) >= 2;

I believe it reads that the priceCondition is defined as yesterdays close is greater than the previous days close. And the Sum function says to filter the previous 3 days and output stocks that meet the priceCondition twice.

Does that seem right?
 
I don't think you need to use the Sum function for this.

You can create your own pattern using the Candlestick Pattern Editor in TOS.

Code:
# Generation time: 2025-03-06T00:18:14.381729600Z

def IsUp = close > open;
def IsDown = close < open;
def IsDoji = IsDoji();
def avgRange = 0.05 * Average(high - low, 20);
plot PatternPlot =
    IsUp[2] and
    IsUp[1] and
    IsUp[0] and
    close[2] < close[1] and
    close[1] < close[0];

PatternPlot.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
PatternPlot.SetDefaultColor(GetColor(0));

the code doesn't match the image.
the code deal with open and close., starting with the current bar.
the image deals with comparing close, bar to bar, but still starts with current bar.
he wants to start with previous bar.
 
Hi,

I am trying to find stocks that are up 2 of the last 3 days, starting from yesterday. If you can help me do that, you can ignore my question below.

Thanks for any help!
Scott

Question Below:

I think that I can use the Sum function for my problem. But in my testing, I was surprised to see that stocks that were up for the last 3 days (yesterday, the day before yesterday and the day before that), had values of 1, 2 or 3 when I added a column with the code below.

def priceCondition = close[1] > close[2] and close[2] > close[3] and close[3] > close[4];
plot out = Sum(priceCondition, 3);

Can anyone explain why that is?

because you are evaluating 5 comparisons, not 3

your formula looks at 3 comparisons.
then by using sum(), you are looking at 3 different sets of bars, offsets, 123, 234, 345
so 5 different comparisons
 
Hi Ben,

Thanks for your quick reply.

I'm wanting code that will be used in a filter to return stocks that have gone up 2 of the past 3 days, starting yesterday.

I can't be sure, but this code seems to work.

def priceCondition = close[1] > close[2];
plot signal = Sum(priceCondition, 3) >= 2;

I believe it reads that the priceCondition is defined as yesterdays close is greater than the previous days close. And the Sum function says to filter the previous 3 days and output stocks that meet the priceCondition twice.

Does that seem right?

i agree with this .
but i like to make formulas that interact with current bar. then use an offset on the variable name , as needed. so my version of sum uses this , priceCondition[1], to start at the previous bar.

i made this as a lower, for testing
added a bubble to show count, to verify

Code:
declare lower;
#def priceCondition = close[1] > close[2];
#plot signal = Sum(priceCondition, 3) >= 2;

def priceCondition = close[0] > close[1];
plot signal = Sum(priceCondition[1], 3) >= 2;

addchartbubble(1,0, Sum(priceCondition[1], 3) , color.yellow, yes);
 
Solution
i agree with this .
but i like to make formulas that interact with current bar. then use an offset on the variable name , as needed. so my version of sum uses this , priceCondition[1], to start at the previous bar.

i made this as a lower, for testing
added a bubble to show count, to verify

Code:
declare lower;
#def priceCondition = close[1] > close[2];
#plot signal = Sum(priceCondition, 3) >= 2;

def priceCondition = close[0] > close[1];
plot signal = Sum(priceCondition[1], 3) >= 2;

addchartbubble(1,0, Sum(priceCondition[1], 3) , color.yellow, yes);

Thanks!
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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