Sum function to count throughout the trading session

ZZzz2000

New member
HI all, hope you guys are having a great Friday. Does anyone know why the code I posted below does not work?


=================================================
input day = 0;
input begin = 0630;
input end = 1300;

def condition1 = close > open and close > close[1];
def condition2 = close[1] > open[1] and close[1] > close[2] and close[1] crosses above Average(close, 400)[1];
def condition3 = Average(close, 400) > Average(close, 200) and Average(close, 400) > Average(close, 100) and Average(close, 400) > Average(close, 50);
def condition0 = condition1 + condition2 + condition3 == 3;

plot signal = Sum(condition0) >= 1 between begin and end;

=================================================


I am very new to thinkscript. I want to create a script indicator that plot a given signal (close price crosses above 400-day moving average) appears more than once during the trading session (06:30 - 13:00, PST) in 1-minute chart.

Thanks in advance for any help.
 
Last edited:
Solution
HI all, hope you guys are having a great Friday. Does anyone know why the code I posted below does not work?


=================================================
input day = 0;
input begin = 0630;
input end = 1300;

def condition1 = close > open and close > close[1];
def condition2 = close[1] > open[1] and close[1] > close[2] and close[1] crosses above Average(close, 400)[1];
def condition3 = Average(close, 400) > Average(close, 200) and Average(close, 400) > Average(close, 100) and Average(close, 400) > Average(close, 50);
def condition0 = condition1 + condition2 + condition3 == 3;

plot signal = Sum(condition0) >= 1 between begin and end;

=================================================


I am very new to thinkscript. I want to...
HI all, hope you guys are having a great Friday. Does anyone know why the code I posted below does not work?


=================================================
input day = 0;
input begin = 0630;
input end = 1300;

def condition1 = close > open and close > close[1];
def condition2 = close[1] > open[1] and close[1] > close[2] and close[1] crosses above Average(close, 400)[1];
def condition3 = Average(close, 400) > Average(close, 200) and Average(close, 400) > Average(close, 100) and Average(close, 400) > Average(close, 50);
def condition0 = condition1 + condition2 + condition3 == 3;

plot signal = Sum(condition0) >= 1 between begin and end;

=================================================


I am very new to thinkscript. I want to create a script indicator that plot a given signal (close price crosses above 400-day moving average) appears more than once during the trading session (06:30 - 13:00, PST) in 1-minute chart.

Thanks in advance for any help.

Thank you for posting your code. It really helps.

The following are modification to your code:
TOS uses EasternTimes in script, not your local time.
The begin and end need secondsfromtime()/secondstilltime() type formatting.
The sum() function was not necessary in this code. Usually it is used if you were looking for condition0 to be 3 for x number of bars.

Added a testing bubble and plots, which I use with most coding I do, but especially with condtions It is defaulted to no, but is shown wih yes in the image below.

Capture.jpg
Ruby:
input day   = 0;
input begin = 0930;
input end   = 1600;

def condition1 = close > open and
                 close > close[1];
def condition2 = close[1] > open[1] and
                 close[1] > close[2] and
                 close[1] crosses above Average(close, 400)[1];
def condition3 = Average(close, 400) > Average(close, 200) and
                 Average(close, 400) > Average(close, 100) and
                 Average(close, 400) > Average(close, 50);
def condition0 = condition1 + condition2 + condition3;

plot signal    = condition0 == 3 and
                 secondsfromtime(begin)>=0 and
                 secondsFromTime(end)<=0;

#############
input testing = no;
AddChartBubble(testing, low - .1, condition1 + "\n" + condition2 + "\n" + condition3 + "\n" + condition0 + "\n" + signal, if signal == 1 then Color.CYAN else Color.GRAY, no);
plot avg400 = if !testing then double.nan else Average(close, 400);
plot avg200 = if !testing then double.nan else Average(close, 200);
plot avg100 = if !testing then double.nan else Average(close, 100);
plot avg50  = if !testing then double.nan else Average(close, 50);
;
 
Solution

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

HI all, hope you guys are having a great Friday. Does anyone know why the code I posted below does not work?


=================================================
input day = 0;
input begin = 0630;
input end = 1300;

def condition1 = close > open and close > close[1];
def condition2 = close[1] > open[1] and close[1] > close[2] and close[1] crosses above Average(close, 400)[1];
def condition3 = Average(close, 400) > Average(close, 200) and Average(close, 400) > Average(close, 100) and Average(close, 400) > Average(close, 50);
def condition0 = condition1 + condition2 + condition3 == 3;

plot signal = Sum(condition0) >= 1 between begin and end;

=================================================


I am very new to thinkscript. I want to create a script indicator that plot a given signal (close price crosses above 400-day moving average) appears more than once during the trading session (06:30 - 13:00, PST) in 1-minute chart.

Thanks in advance for any help.
Thank you for your modification to my code. It works.

Finally, I am able to to add a label to 1-minute chart. If the close price crosses above 400-day moving average effectively once during the trading session, the label turns green and stays green no matter whatever happens during the rest of the trading session. Here is the code I used:

==============================================

input day = 0;
input begin = 0930;
input end = 1600;

def condition1 = close > open and
close > close[1];
def condition2 = close[1] > open[1] and
close[1] > close[2] and
close[1] crosses above Average(close, 400)[1];
def condition3 = Average(close, 400) > Average(close, 200) and
Average(close, 400) > Average(close, 100) and
Average(close, 400) > Average(close, 50);
def condition0 = condition1 + condition2 + condition3 == 3;

plot signal = Sum(condition0) >= 1 between
secondsFromTime(begin)>=0 and
secondsFromTime(end)<=0;

AddLabel(yes, "M400_BK", if Sum(condition0) >= 1 between
secondsFromTime(begin)>=0 and
secondsFromTime(end)<=0 then Color.GREEN else color.RED);

================================================
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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