Adding up specific plot values during market hours

joekim1

New member
I'm trying to add up the values of an indicator when they reach a certain condition to have a total by the end of the day but can't get anything going with sum or compoundvalue. It's doing my head in.

For example, let's say I have an RSI indicator, and I want to add up its peak values only after it crosses below some number like 60. So my indicator would find the highest value between the time it crossed above 60 and crossed below 60. After the market closes, I want to add up all the highest values from the start of the day to the end, that met my condition.

My actual use case is more complicated than this but I'm hoping I can get some clues using this example. Would appreciate any help!
 
Solution
I'm trying to add up the values of an indicator when they reach a certain condition to have a total by the end of the day but can't get anything going with sum or compoundvalue. It's doing my head in.

For example, let's say I have an RSI indicator, and I want to add up its peak values only after it crosses below some number like 60. So my indicator would find the highest value between the time it crossed above 60 and crossed below 60. After the market closes, I want to add up all the highest values from the start of the day to the end, that met my condition.

My actual use case is more complicated than this but I'm hoping I can get some clues using this example. Would appreciate any help!


draw a bubble on last bar of day...
I'm trying to add up the values of an indicator when they reach a certain condition to have a total by the end of the day but can't get anything going with sum or compoundvalue. It's doing my head in.

For example, let's say I have an RSI indicator, and I want to add up its peak values only after it crosses below some number like 60. So my indicator would find the highest value between the time it crossed above 60 and crossed below 60. After the market closes, I want to add up all the highest values from the start of the day to the end, that met my condition.

My actual use case is more complicated than this but I'm hoping I can get some clues using this example. Would appreciate any help!


draw a bubble on last bar of day, with
..sum of prices,
..quantity of peaks in a day
..average of the peaks in a day


Code:
#sum_peaks_in_day_00

#https://usethinkscript.com/threads/adding-up-specific-plot-values-during-market-hours.16562/
#Adding up specific plot values during market hours

declare lower;

def na = double.nan;
def bn = barnumber();

def data = rsi();

input peak_level = 60;
#input valley_level = 40;

def newday = getday() != getday()[1];
def dayend = getday() != getday()[-1];

def xpkup = data crosses above peak_level;
def xpkdwn = data crosses below peak_level;

def pkactive = data > peak_level;

def pkcnt;
def pkhi;
def pkdaysum;
if bn == 1 then {
 pkcnt = 0;
 pkhi = 0;
 pkdaysum = 0;
} else if newday and !pkactive then {
 pkcnt = 0;
 pkhi = 0;
 pkdaysum = 0;
} else if newday and pkactive then {
 pkcnt = 0;
 pkhi = data;
 pkdaysum = 0;
} else if xpkdwn or (dayend and pkactive) then {
 pkdaysum = round(pkdaysum[1] + pkhi[1],2);
 pkcnt = pkcnt[1]+1;
 pkhi = 0;
} else if xpkup then {
 pkdaysum = pkdaysum[1];
 pkcnt = pkcnt[1];
 pkhi = data;
} else {
 pkdaysum = pkdaysum[1];
 pkcnt = pkcnt[1];
 pkhi = if pkactive then max(pkhi[1], data) else 0;
}

def pkdayavg = if pkcnt == 0 then 0 else round(pkdaysum/pkcnt,2);

plot z1 = data;
plot z2 = peak_level;

input show_daily_numbers = yes;
addchartbubble(show_daily_numbers and dayend, data,
 pkdaysum + " sum\n" +
 pkcnt + " cnt\n" +
 pkdayavg + " avg"
, color.green, yes);

input show_day_lines = yes;
addverticalline(show_day_lines and newday, "-", color.cyan);


input test1 = no;
addchartbubble(test1, data*0.97,
 xpkup + " up\n" +
 xpkdwn + " dwn\n" +
 pkactive + " act\n" +
 pkhi + " pkhi\n" +
 pkdaysum + " sum\n" +
 pkcnt + " cnt\n" +
 pkdayavg + " avg"
, color.yellow, no);

#

INTC 15min
3 peaks above 60
sum them , 211
average them , 70.47
mN9h147.jpg
 
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
440 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